2022-07-26 15:07:42 +02:00
|
|
|
/*
|
2014-06-05 20:26:50 +03:00
|
|
|
* CGPandoraBox.cpp, part of VCMI engine
|
2014-06-05 19:52:14 +03:00
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "StdInc.h"
|
|
|
|
#include "CGPandoraBox.h"
|
|
|
|
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
#include <vcmi/spells/Spell.h>
|
|
|
|
#include <vcmi/spells/Service.h>
|
|
|
|
|
2014-06-05 20:26:50 +03:00
|
|
|
#include "../NetPacks.h"
|
2014-06-05 23:51:24 +03:00
|
|
|
#include "../CSoundBase.h"
|
2014-06-05 19:52:14 +03:00
|
|
|
|
2018-03-31 07:56:40 +02:00
|
|
|
#include "../CSkillHandler.h"
|
2014-06-25 17:11:07 +03:00
|
|
|
#include "../StartInfo.h"
|
|
|
|
#include "../IGameCallback.h"
|
2023-08-19 23:22:31 +02:00
|
|
|
#include "../constants/StringConstants.h"
|
2016-11-13 12:38:42 +02:00
|
|
|
#include "../serializer/JsonSerializeFormat.h"
|
2014-06-05 19:52:14 +03:00
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2023-09-15 15:29:41 +02:00
|
|
|
void CGPandoraBox::init()
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
2023-09-15 15:29:41 +02:00
|
|
|
blockVisit = true;
|
2023-09-17 22:19:45 +02:00
|
|
|
configuration.info.emplace_back();
|
|
|
|
configuration.info.back().visitType = Rewardable::EEventType::EVENT_FIRST_VISIT;
|
2023-09-15 15:29:41 +02:00
|
|
|
|
|
|
|
for(auto & i : configuration.info)
|
2023-09-17 22:19:45 +02:00
|
|
|
{
|
2023-09-15 15:29:41 +02:00
|
|
|
i.reward.removeObject = true;
|
2023-09-17 22:19:45 +02:00
|
|
|
if(!message.empty() && i.message.empty())
|
|
|
|
i.message = MetaString::createFromRawString(message);
|
|
|
|
}
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
|
|
|
|
2023-09-15 15:29:41 +02:00
|
|
|
void CGPandoraBox::initObj(CRandomGenerator & rand)
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
2023-09-15 15:29:41 +02:00
|
|
|
init();
|
|
|
|
|
|
|
|
CRewardableObject::initObj(rand);
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
|
|
|
|
2023-09-17 18:02:24 +02:00
|
|
|
void CGPandoraBox::grantRewardWithMessage(const CGHeroInstance * h, int index, bool markAsVisit) const
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
2023-09-17 18:02:24 +02:00
|
|
|
auto vi = configuration.info.at(index);
|
|
|
|
if(!vi.message.empty())
|
|
|
|
{
|
|
|
|
CRewardableObject::grantRewardWithMessage(h, index, markAsVisit);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//split reward message for pandora box
|
|
|
|
auto setText = [](bool cond, int posId, int negId, const CGHeroInstance * h)
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
2023-09-17 18:02:24 +02:00
|
|
|
MetaString text;
|
|
|
|
text.appendLocalString(EMetaText::ADVOB_TXT, cond ? posId : negId);
|
2023-09-15 15:29:41 +02:00
|
|
|
text.replaceRawString(h->getNameTranslated());
|
2023-09-17 18:02:24 +02:00
|
|
|
return text;
|
2023-09-15 15:29:41 +02:00
|
|
|
};
|
|
|
|
|
2023-09-17 18:02:24 +02:00
|
|
|
auto sendInfoWindow = [h](const MetaString & text, const Rewardable::Reward & reward)
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
2023-09-17 18:02:24 +02:00
|
|
|
InfoWindow iw;
|
|
|
|
iw.player = h->tempOwner;
|
|
|
|
iw.text = text;
|
|
|
|
reward.loadComponents(iw.components, h);
|
|
|
|
iw.type = EInfoWindowMode::MODAL;
|
|
|
|
if(!iw.components.empty())
|
|
|
|
cb->showInfoDialog(&iw);
|
|
|
|
};
|
|
|
|
|
|
|
|
Rewardable::Reward temp;
|
|
|
|
temp.spells = vi.reward.spells;
|
|
|
|
temp.heroExperience = vi.reward.heroExperience;
|
|
|
|
temp.heroLevel = vi.reward.heroLevel;
|
|
|
|
temp.primary = vi.reward.primary;
|
|
|
|
temp.secondary = vi.reward.secondary;
|
|
|
|
temp.bonuses = vi.reward.bonuses;
|
|
|
|
temp.manaDiff = vi.reward.manaDiff;
|
|
|
|
temp.manaPercentage = vi.reward.manaPercentage;
|
|
|
|
|
|
|
|
MetaString txt;
|
|
|
|
if(!vi.reward.spells.empty())
|
|
|
|
txt = setText(temp.spells.size() == 1, 184, 188, h);
|
|
|
|
|
|
|
|
if(vi.reward.heroExperience || vi.reward.heroLevel || !vi.reward.secondary.empty())
|
|
|
|
txt = setText(true, 175, 175, h);
|
|
|
|
|
|
|
|
for(int i : vi.reward.primary)
|
|
|
|
{
|
|
|
|
if(i)
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
2023-09-17 18:02:24 +02:00
|
|
|
txt = setText(true, 175, 175, h);
|
|
|
|
break;
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
2023-09-17 18:02:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(vi.reward.manaDiff || vi.reward.manaPercentage)
|
|
|
|
txt = setText(temp.manaDiff > 0, 177, 176, h);
|
|
|
|
|
|
|
|
for(auto b : vi.reward.bonuses)
|
|
|
|
{
|
|
|
|
if(b.val && b.type == BonusType::MORALE)
|
|
|
|
txt = setText(b.val > 0, 179, 178, h);
|
|
|
|
if(b.val && b.type == BonusType::LUCK)
|
|
|
|
txt = setText(b.val > 0, 181, 180, h);
|
|
|
|
}
|
|
|
|
sendInfoWindow(txt, temp);
|
|
|
|
|
|
|
|
//resource message
|
|
|
|
temp = Rewardable::Reward{};
|
|
|
|
temp.resources = vi.reward.resources;
|
|
|
|
sendInfoWindow(setText(vi.reward.resources.marketValue() > 0, 183, 182, h), temp);
|
|
|
|
|
|
|
|
//artifacts message
|
|
|
|
temp = Rewardable::Reward{};
|
|
|
|
temp.artifacts = vi.reward.artifacts;
|
|
|
|
sendInfoWindow(setText(true, 183, 183, h), temp);
|
|
|
|
|
|
|
|
//creatures message
|
|
|
|
temp = Rewardable::Reward{};
|
|
|
|
temp.creatures = vi.reward.creatures;
|
|
|
|
txt.clear();
|
|
|
|
if(!vi.reward.creatures.empty())
|
|
|
|
{
|
|
|
|
MetaString loot;
|
|
|
|
for(auto c : vi.reward.creatures)
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
2023-09-17 18:02:24 +02:00
|
|
|
loot.appendRawString("%s");
|
|
|
|
loot.replaceCreatureName(c);
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
2023-09-15 15:29:41 +02:00
|
|
|
|
2023-09-17 18:02:24 +02:00
|
|
|
if(vi.reward.creatures.size() == 1 && vi.reward.creatures[0].count == 1)
|
|
|
|
txt.appendLocalString(EMetaText::ADVOB_TXT, 185);
|
|
|
|
else
|
|
|
|
txt.appendLocalString(EMetaText::ADVOB_TXT, 186);
|
2023-09-15 15:29:41 +02:00
|
|
|
|
2023-09-17 18:02:24 +02:00
|
|
|
txt.replaceRawString(loot.buildList());
|
|
|
|
txt.replaceRawString(h->getNameTranslated());
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
2023-09-17 18:02:24 +02:00
|
|
|
sendInfoWindow(txt, temp);
|
|
|
|
|
|
|
|
//everything else
|
|
|
|
temp = vi.reward;
|
|
|
|
temp.heroExperience = 0;
|
|
|
|
temp.heroLevel = 0;
|
|
|
|
temp.secondary.clear();
|
|
|
|
temp.primary.clear();
|
|
|
|
temp.resources.amin(0);
|
|
|
|
temp.resources.amax(0);
|
|
|
|
temp.manaDiff = 0;
|
|
|
|
temp.manaPercentage = 0;
|
|
|
|
temp.spells.clear();
|
|
|
|
temp.creatures.clear();
|
|
|
|
temp.bonuses.clear();
|
|
|
|
temp.artifacts.clear();
|
|
|
|
sendInfoWindow(setText(true, 175, 175, h), temp);
|
2023-09-15 15:29:41 +02:00
|
|
|
|
2023-09-17 18:02:24 +02:00
|
|
|
// grant reward afterwards. Note that it may remove object
|
|
|
|
if(markAsVisit)
|
|
|
|
markAsVisited(h);
|
|
|
|
grantReward(index, h);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGPandoraBox::onHeroVisit(const CGHeroInstance * h) const
|
|
|
|
{
|
2023-09-15 15:29:41 +02:00
|
|
|
BlockingDialog bd (true, false);
|
|
|
|
bd.player = h->getOwner();
|
|
|
|
bd.text.appendLocalString(EMetaText::ADVOB_TXT, 14);
|
|
|
|
cb->showBlockingDialog(&bd);
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CGPandoraBox::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
|
|
|
|
{
|
2016-01-26 07:41:09 +02:00
|
|
|
if(result.winner == 0)
|
|
|
|
{
|
2023-09-15 15:29:41 +02:00
|
|
|
CRewardableObject::onHeroVisit(hero);
|
2016-01-26 07:41:09 +02:00
|
|
|
}
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CGPandoraBox::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
|
|
|
|
{
|
2015-12-24 20:30:57 +02:00
|
|
|
if(answer)
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
2015-12-24 20:30:57 +02:00
|
|
|
if(stacksCount() > 0) //if pandora's box is protected by army
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
2023-03-08 00:32:21 +02:00
|
|
|
hero->showInfoDialog(16, 0, EInfoWindowMode::MODAL);
|
2014-06-05 19:52:14 +03:00
|
|
|
cb->startBattleI(hero, this); //grants things after battle
|
|
|
|
}
|
2023-09-15 15:29:41 +02:00
|
|
|
else if(getAvailableRewards(hero, Rewardable::EEventType::EVENT_FIRST_VISIT).empty())
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
2023-03-08 00:32:21 +02:00
|
|
|
hero->showInfoDialog(15);
|
2023-09-18 21:09:55 +02:00
|
|
|
cb->removeObject(this, hero->getOwner());
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
|
|
|
else //if it gives something without battle
|
|
|
|
{
|
2023-09-15 15:29:41 +02:00
|
|
|
CRewardableObject::onHeroVisit(hero);
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
void CGPandoraBox::serializeJsonOptions(JsonSerializeFormat & handler)
|
|
|
|
{
|
2023-09-15 15:29:41 +02:00
|
|
|
CRewardableObject::serializeJsonOptions(handler);
|
2023-09-17 22:19:45 +02:00
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
handler.serializeString("guardMessage", message);
|
2023-09-15 15:29:41 +02:00
|
|
|
|
|
|
|
if(!handler.saving)
|
2016-11-13 12:38:42 +02:00
|
|
|
{
|
2023-09-17 22:19:45 +02:00
|
|
|
//backward compatibility for VCMI maps that use old Pandora Box format
|
|
|
|
if(!handler.getCurrent()["guards"].Vector().empty())
|
|
|
|
CCreatureSet::serializeJson(handler, "guards", 7);
|
|
|
|
|
|
|
|
bool hasSomething = false;
|
|
|
|
Rewardable::VisitInfo vinfo;
|
2023-09-17 13:03:42 +02:00
|
|
|
vinfo.visitType = Rewardable::EEventType::EVENT_FIRST_VISIT;
|
|
|
|
|
|
|
|
handler.serializeInt("experience", vinfo.reward.heroExperience, 0);
|
|
|
|
handler.serializeInt("mana", vinfo.reward.manaDiff, 0);
|
2023-09-15 21:08:14 +02:00
|
|
|
|
2023-09-17 18:02:24 +02:00
|
|
|
int val;
|
2023-09-15 15:29:41 +02:00
|
|
|
handler.serializeInt("morale", val, 0);
|
|
|
|
if(val)
|
2023-09-17 13:03:42 +02:00
|
|
|
vinfo.reward.bonuses.emplace_back(BonusDuration::ONE_BATTLE, BonusType::MORALE, BonusSource::OBJECT, val, id);
|
2023-09-15 21:08:14 +02:00
|
|
|
|
2023-09-15 15:29:41 +02:00
|
|
|
handler.serializeInt("luck", val, 0);
|
|
|
|
if(val)
|
2023-09-17 13:03:42 +02:00
|
|
|
vinfo.reward.bonuses.emplace_back(BonusDuration::ONE_BATTLE, BonusType::LUCK, BonusSource::OBJECT, val, id);
|
2023-09-15 15:29:41 +02:00
|
|
|
|
2023-09-17 13:03:42 +02:00
|
|
|
vinfo.reward.resources.serializeJson(handler, "resources");
|
2017-07-20 06:08:49 +02:00
|
|
|
{
|
|
|
|
auto s = handler.enterStruct("primarySkills");
|
2023-09-17 13:03:42 +02:00
|
|
|
for(int idx = 0; idx < vinfo.reward.primary.size(); idx ++)
|
2023-09-15 21:08:14 +02:00
|
|
|
{
|
2023-09-17 13:03:42 +02:00
|
|
|
handler.serializeInt(NPrimarySkill::names[idx], vinfo.reward.primary[idx], 0);
|
2023-09-17 22:19:45 +02:00
|
|
|
if(vinfo.reward.primary[idx])
|
|
|
|
hasSomething = true;
|
2023-09-15 21:08:14 +02:00
|
|
|
}
|
2017-07-20 06:08:49 +02:00
|
|
|
}
|
2023-09-15 15:29:41 +02:00
|
|
|
|
2023-09-17 13:03:42 +02:00
|
|
|
handler.serializeIdArray("artifacts", vinfo.reward.artifacts);
|
|
|
|
handler.serializeIdArray("spells", vinfo.reward.spells);
|
|
|
|
handler.enterArray("creatures").serializeStruct(vinfo.reward.creatures);
|
2023-09-15 15:29:41 +02:00
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
{
|
|
|
|
auto s = handler.enterStruct("secondarySkills");
|
2023-09-15 15:29:41 +02:00
|
|
|
for(const auto & p : handler.getCurrent().Struct())
|
2016-11-13 12:38:42 +02:00
|
|
|
{
|
2023-09-15 15:29:41 +02:00
|
|
|
const std::string skillName = p.first;
|
|
|
|
const std::string levelId = p.second.String();
|
|
|
|
|
|
|
|
const int rawId = CSkillHandler::decodeSkill(skillName);
|
|
|
|
if(rawId < 0)
|
|
|
|
{
|
|
|
|
logGlobal->error("Invalid secondary skill %s", skillName);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const int level = vstd::find_pos(NSecondarySkill::levels, levelId);
|
|
|
|
if(level < 0)
|
|
|
|
{
|
|
|
|
logGlobal->error("Invalid secondary skill level %s", levelId);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-09-17 13:03:42 +02:00
|
|
|
vinfo.reward.secondary[rawId] = level;
|
2016-11-13 12:38:42 +02:00
|
|
|
}
|
|
|
|
}
|
2023-09-17 22:19:45 +02:00
|
|
|
|
|
|
|
hasSomething = hasSomething
|
|
|
|
|| vinfo.reward.heroExperience
|
|
|
|
|| vinfo.reward.manaDiff
|
|
|
|
|| vinfo.reward.resources.nonZero()
|
|
|
|
|| !vinfo.reward.bonuses.empty()
|
|
|
|
|| !vinfo.reward.artifacts.empty()
|
|
|
|
|| !vinfo.reward.secondary.empty()
|
|
|
|
|| !vinfo.reward.artifacts.empty()
|
|
|
|
|| !vinfo.reward.creatures.empty();
|
|
|
|
|
|
|
|
if(hasSomething)
|
|
|
|
configuration.info.push_back(vinfo);
|
2016-11-13 12:38:42 +02:00
|
|
|
}
|
2023-09-15 15:29:41 +02:00
|
|
|
}
|
2016-11-13 12:38:42 +02:00
|
|
|
|
2023-09-15 15:29:41 +02:00
|
|
|
void CGEvent::init()
|
|
|
|
{
|
|
|
|
blockVisit = false;
|
2023-09-19 17:11:03 +02:00
|
|
|
configuration.infoWindowType = EInfoWindowMode::MODAL;
|
2023-09-15 15:29:41 +02:00
|
|
|
|
|
|
|
for(auto & i : configuration.info)
|
2023-09-17 18:02:24 +02:00
|
|
|
{
|
2023-09-15 15:29:41 +02:00
|
|
|
i.reward.removeObject = removeAfterVisit;
|
2023-09-17 18:02:24 +02:00
|
|
|
if(!message.empty() && i.message.empty())
|
|
|
|
i.message = MetaString::createFromRawString(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGEvent::grantRewardWithMessage(const CGHeroInstance * contextHero, int rewardIndex, bool markAsVisit) const
|
|
|
|
{
|
|
|
|
CRewardableObject::grantRewardWithMessage(contextHero, rewardIndex, markAsVisit);
|
2016-11-13 12:38:42 +02:00
|
|
|
}
|
|
|
|
|
2014-06-05 19:52:14 +03:00
|
|
|
void CGEvent::onHeroVisit( const CGHeroInstance * h ) const
|
|
|
|
{
|
2023-08-25 20:40:19 +02:00
|
|
|
if(availableFor.count(h->tempOwner) == 0)
|
2014-06-05 19:52:14 +03:00
|
|
|
return;
|
2023-08-25 20:40:19 +02:00
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
if(cb->getPlayerSettings(h->tempOwner)->isControlledByHuman())
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
|
|
|
if(humanActivate)
|
|
|
|
activated(h);
|
|
|
|
}
|
|
|
|
else if(computerActivate)
|
|
|
|
activated(h);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGEvent::activated( const CGHeroInstance * h ) const
|
|
|
|
{
|
|
|
|
if(stacksCount() > 0)
|
|
|
|
{
|
|
|
|
InfoWindow iw;
|
|
|
|
iw.player = h->tempOwner;
|
2023-02-12 22:39:17 +02:00
|
|
|
if(!message.empty())
|
2023-06-18 11:18:25 +02:00
|
|
|
iw.text.appendRawString(message);
|
2014-06-05 19:52:14 +03:00
|
|
|
else
|
2023-06-18 11:18:25 +02:00
|
|
|
iw.text.appendLocalString(EMetaText::ADVOB_TXT, 16);
|
2014-06-05 19:52:14 +03:00
|
|
|
cb->showInfoDialog(&iw);
|
|
|
|
cb->startBattleI(h, this);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-09-15 15:29:41 +02:00
|
|
|
CRewardableObject::onHeroVisit(h);
|
2015-11-28 22:03:26 +02:00
|
|
|
}
|
|
|
|
}
|
2016-11-13 12:38:42 +02:00
|
|
|
|
|
|
|
void CGEvent::serializeJsonOptions(JsonSerializeFormat & handler)
|
|
|
|
{
|
|
|
|
CGPandoraBox::serializeJsonOptions(handler);
|
|
|
|
|
2023-09-15 15:29:41 +02:00
|
|
|
handler.serializeBool("aIActivable", computerActivate, false);
|
|
|
|
handler.serializeBool("humanActivable", humanActivate, true);
|
|
|
|
handler.serializeBool("removeAfterVisit", removeAfterVisit, false);
|
2023-08-25 20:40:19 +02:00
|
|
|
handler.serializeIdArray("availableFor", availableFor);
|
2016-11-13 12:38:42 +02:00
|
|
|
}
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|