CGPandoraBox.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /*
  2. * CGPandoraBox.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CGPandoraBox.h"
  12. #include <vcmi/spells/Spell.h>
  13. #include <vcmi/spells/Service.h>
  14. #include "../NetPacks.h"
  15. #include "../CSoundBase.h"
  16. #include "../CSkillHandler.h"
  17. #include "../StartInfo.h"
  18. #include "../IGameCallback.h"
  19. #include "../constants/StringConstants.h"
  20. #include "../serializer/JsonSerializeFormat.h"
  21. VCMI_LIB_NAMESPACE_BEGIN
  22. void CGPandoraBox::initObj(CRandomGenerator & rand)
  23. {
  24. blockVisit = (ID==Obj::PANDORAS_BOX); //block only if it's really pandora's box (events also derive from that class)
  25. hasGuardians = stacks.size();
  26. }
  27. void CGPandoraBox::onHeroVisit(const CGHeroInstance * h) const
  28. {
  29. BlockingDialog bd (true, false);
  30. bd.player = h->getOwner();
  31. bd.text.appendLocalString (EMetaText::ADVOB_TXT, 14);
  32. cb->showBlockingDialog (&bd);
  33. }
  34. void CGPandoraBox::giveContentsUpToExp(const CGHeroInstance *h) const
  35. {
  36. afterSuccessfulVisit();
  37. InfoWindow iw;
  38. iw.type = EInfoWindowMode::AUTO;
  39. iw.player = h->getOwner();
  40. bool changesPrimSkill = false;
  41. for(const auto & elem : primskills)
  42. {
  43. if(elem)
  44. {
  45. changesPrimSkill = true;
  46. break;
  47. }
  48. }
  49. std::vector<std::pair<SecondarySkill, ui8>> unpossessedAbilities; //ability + ability level
  50. int abilitiesRequiringSlot = 0;
  51. //filter out unnecessary secondary skills
  52. for (int i = 0; i < abilities.size(); i++)
  53. {
  54. int curLev = h->getSecSkillLevel(abilities[i]);
  55. bool abilityCanUseSlot = !curLev && ((h->secSkills.size() + abilitiesRequiringSlot) < GameConstants::SKILL_PER_HERO); //limit new abilities to number of slots
  56. if (abilityCanUseSlot)
  57. abilitiesRequiringSlot++;
  58. if ((curLev && curLev < abilityLevels[i]) || abilityCanUseSlot)
  59. {
  60. unpossessedAbilities.emplace_back(abilities[i], abilityLevels[i]);
  61. }
  62. }
  63. if(gainedExp || changesPrimSkill || !unpossessedAbilities.empty())
  64. {
  65. TExpType expVal = h->calculateXp(gainedExp);
  66. //getText(iw,afterBattle,175,h); //wtf?
  67. iw.text.appendLocalString(EMetaText::ADVOB_TXT, 175); //%s learns something
  68. iw.text.replaceRawString(h->getNameTranslated());
  69. if(expVal)
  70. iw.components.emplace_back(Component::EComponentType::EXPERIENCE, 0, static_cast<si32>(expVal), 0);
  71. for(int i=0; i<primskills.size(); i++)
  72. if(primskills[i])
  73. iw.components.emplace_back(Component::EComponentType::PRIM_SKILL, i, primskills[i], 0);
  74. for(const auto & abilityData : unpossessedAbilities)
  75. iw.components.emplace_back(Component::EComponentType::SEC_SKILL, abilityData.first, abilityData.second, 0);
  76. cb->showInfoDialog(&iw);
  77. //give sec skills
  78. for(const auto & abilityData : unpossessedAbilities)
  79. cb->changeSecSkill(h, abilityData.first, abilityData.second, true);
  80. assert(h->secSkills.size() <= GameConstants::SKILL_PER_HERO);
  81. //give prim skills
  82. for(int i=0; i<primskills.size(); i++)
  83. if(primskills[i])
  84. cb->changePrimSkill(h,static_cast<PrimarySkill>(i),primskills[i],false);
  85. assert(!cb->isVisitCoveredByAnotherQuery(this, h));
  86. //give exp
  87. if(expVal)
  88. cb->changePrimSkill(h, PrimarySkill::EXPERIENCE, expVal, false);
  89. }
  90. //else { } //TODO:Create information that box was empty for now, and deliver to CGPandoraBox::giveContentsAfterExp or refactor
  91. if(!cb->isVisitCoveredByAnotherQuery(this, h))
  92. giveContentsAfterExp(h);
  93. //Otherwise continuation occurs via post-level-up callback.
  94. }
  95. void CGPandoraBox::giveContentsAfterExp(const CGHeroInstance *h) const
  96. {
  97. bool hadGuardians = hasGuardians; //copy, because flag will be emptied after issuing first post-battle message
  98. std::string msg = message; //in case box is removed in the meantime
  99. InfoWindow iw;
  100. iw.type = EInfoWindowMode::AUTO;
  101. iw.player = h->getOwner();
  102. //TODO: reuse this code for Scholar skill
  103. if(!spells.empty())
  104. {
  105. std::set<SpellID> spellsToGive;
  106. auto i = spells.cbegin();
  107. while (i != spells.cend())
  108. {
  109. iw.components.clear();
  110. iw.text.clear();
  111. spellsToGive.clear();
  112. for (; i != spells.cend(); i++)
  113. {
  114. const auto * spell = (*i).toSpell(VLC->spells());
  115. if(h->canLearnSpell(spell))
  116. {
  117. iw.components.emplace_back(Component::EComponentType::SPELL, *i, 0, 0);
  118. spellsToGive.insert(*i);
  119. }
  120. if(spellsToGive.size() == 8) //display up to 8 spells at once
  121. {
  122. break;
  123. }
  124. }
  125. if (!spellsToGive.empty())
  126. {
  127. if (spellsToGive.size() > 1)
  128. {
  129. iw.text.appendLocalString(EMetaText::ADVOB_TXT, 188); //%s learns spells
  130. }
  131. else
  132. {
  133. iw.text.appendLocalString(EMetaText::ADVOB_TXT, 184); //%s learns a spell
  134. }
  135. iw.text.replaceRawString(h->getNameTranslated());
  136. cb->changeSpells(h, true, spellsToGive);
  137. cb->showInfoDialog(&iw);
  138. }
  139. }
  140. }
  141. if(manaDiff)
  142. {
  143. getText(iw,hadGuardians,manaDiff,176,177,h);
  144. iw.components.emplace_back(Component::EComponentType::PRIM_SKILL, 5, manaDiff, 0);
  145. cb->showInfoDialog(&iw);
  146. cb->setManaPoints(h->id, h->mana + manaDiff);
  147. }
  148. if(moraleDiff)
  149. {
  150. getText(iw,hadGuardians,moraleDiff,178,179,h);
  151. iw.components.emplace_back(Component::EComponentType::MORALE, 0, moraleDiff, 0);
  152. cb->showInfoDialog(&iw);
  153. GiveBonus gb;
  154. gb.bonus = Bonus(BonusDuration::ONE_BATTLE,BonusType::MORALE,BonusSource::OBJECT,moraleDiff,id.getNum(),"");
  155. gb.id = h->id.getNum();
  156. cb->giveHeroBonus(&gb);
  157. }
  158. if(luckDiff)
  159. {
  160. getText(iw,hadGuardians,luckDiff,180,181,h);
  161. iw.components.emplace_back(Component::EComponentType::LUCK, 0, luckDiff, 0);
  162. cb->showInfoDialog(&iw);
  163. GiveBonus gb;
  164. gb.bonus = Bonus(BonusDuration::ONE_BATTLE,BonusType::LUCK,BonusSource::OBJECT,luckDiff,id.getNum(),"");
  165. gb.id = h->id.getNum();
  166. cb->giveHeroBonus(&gb);
  167. }
  168. iw.components.clear();
  169. iw.text.clear();
  170. for(int i=0; i<resources.size(); i++)
  171. {
  172. if(resources[i] < 0)
  173. iw.components.emplace_back(Component::EComponentType::RESOURCE, i, resources[i], 0);
  174. }
  175. if(!iw.components.empty())
  176. {
  177. getText(iw,hadGuardians,182,h);
  178. cb->showInfoDialog(&iw);
  179. }
  180. iw.components.clear();
  181. iw.text.clear();
  182. for(int i=0; i<resources.size(); i++)
  183. {
  184. if(resources[i] > 0)
  185. iw.components.emplace_back(Component::EComponentType::RESOURCE, i, resources[i], 0);
  186. }
  187. if(!iw.components.empty())
  188. {
  189. getText(iw,hadGuardians,183,h);
  190. cb->showInfoDialog(&iw);
  191. }
  192. iw.components.clear();
  193. // getText(iw,afterBattle,183,h);
  194. iw.text.appendLocalString(EMetaText::ADVOB_TXT, 183); //% has found treasure
  195. iw.text.replaceRawString(h->getNameTranslated());
  196. for(const auto & elem : artifacts)
  197. {
  198. iw.components.emplace_back(Component::EComponentType::ARTIFACT, elem, 0, 0);
  199. if(iw.components.size() >= 14)
  200. {
  201. cb->showInfoDialog(&iw);
  202. iw.components.clear();
  203. iw.text.appendLocalString(EMetaText::ADVOB_TXT, 183); //% has found treasure - once more?
  204. iw.text.replaceRawString(h->getNameTranslated());
  205. }
  206. }
  207. if(!iw.components.empty())
  208. {
  209. cb->showInfoDialog(&iw);
  210. }
  211. cb->giveResources(h->getOwner(), resources);
  212. for(const auto & elem : artifacts)
  213. cb->giveHeroNewArtifact(h, VLC->arth->objects[elem],ArtifactPosition::FIRST_AVAILABLE);
  214. iw.components.clear();
  215. iw.text.clear();
  216. if(creatures.stacksCount())
  217. { //this part is taken straight from creature bank
  218. MetaString loot;
  219. for(const auto & elem : creatures.Slots())
  220. { //build list of joined creatures
  221. iw.components.emplace_back(*elem.second);
  222. loot.appendRawString("%s");
  223. loot.replaceCreatureName(*elem.second);
  224. }
  225. if(creatures.stacksCount() == 1 && creatures.Slots().begin()->second->count == 1)
  226. iw.text.appendLocalString(EMetaText::ADVOB_TXT, 185);
  227. else
  228. iw.text.appendLocalString(EMetaText::ADVOB_TXT, 186);
  229. iw.text.replaceRawString(loot.buildList());
  230. iw.text.replaceRawString(h->getNameTranslated());
  231. cb->showInfoDialog(&iw);
  232. cb->giveCreatures(this, h, creatures, false);
  233. }
  234. if(!hasGuardians && !msg.empty())
  235. {
  236. iw.text.appendRawString(msg);
  237. cb->showInfoDialog(&iw);
  238. }
  239. }
  240. void CGPandoraBox::getText( InfoWindow &iw, bool &afterBattle, int text, const CGHeroInstance * h ) const
  241. {
  242. if(afterBattle || message.empty())
  243. {
  244. iw.text.appendLocalString(EMetaText::ADVOB_TXT,text);//%s has lost treasure.
  245. iw.text.replaceRawString(h->getNameTranslated());
  246. }
  247. else
  248. {
  249. iw.text.appendRawString(message);
  250. afterBattle = true;
  251. }
  252. }
  253. void CGPandoraBox::getText( InfoWindow &iw, bool &afterBattle, int val, int negative, int positive, const CGHeroInstance * h ) const
  254. {
  255. iw.components.clear();
  256. iw.text.clear();
  257. if(afterBattle || message.empty())
  258. {
  259. iw.text.appendLocalString(EMetaText::ADVOB_TXT,val < 0 ? negative : positive); //%s's luck takes a turn for the worse / %s's luck increases
  260. iw.text.replaceRawString(h->getNameTranslated());
  261. }
  262. else
  263. {
  264. iw.text.appendRawString(message);
  265. afterBattle = true;
  266. }
  267. }
  268. void CGPandoraBox::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
  269. {
  270. if(result.winner == 0)
  271. {
  272. giveContentsUpToExp(hero);
  273. }
  274. }
  275. void CGPandoraBox::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  276. {
  277. if(answer)
  278. {
  279. if(stacksCount() > 0) //if pandora's box is protected by army
  280. {
  281. hero->showInfoDialog(16, 0, EInfoWindowMode::MODAL);
  282. cb->startBattleI(hero, this); //grants things after battle
  283. }
  284. else if(message.empty() && resources.empty()
  285. && primskills.empty() && abilities.empty()
  286. && abilityLevels.empty() && artifacts.empty()
  287. && spells.empty() && creatures.stacksCount() == 0
  288. && gainedExp == 0 && manaDiff == 0 && moraleDiff == 0 && luckDiff == 0) //if it gives nothing without battle
  289. {
  290. hero->showInfoDialog(15);
  291. cb->removeObject(this);
  292. }
  293. else //if it gives something without battle
  294. {
  295. giveContentsUpToExp(hero);
  296. }
  297. }
  298. }
  299. void CGPandoraBox::heroLevelUpDone(const CGHeroInstance *hero) const
  300. {
  301. giveContentsAfterExp(hero);
  302. }
  303. void CGPandoraBox::afterSuccessfulVisit() const
  304. {
  305. cb->removeAfterVisit(this);
  306. }
  307. void CGPandoraBox::serializeJsonOptions(JsonSerializeFormat & handler)
  308. {
  309. CCreatureSet::serializeJson(handler, "guards", 7);
  310. handler.serializeString("guardMessage", message);
  311. handler.serializeInt("experience", gainedExp, 0);
  312. handler.serializeInt("mana", manaDiff, 0);
  313. handler.serializeInt("morale", moraleDiff, 0);
  314. handler.serializeInt("luck", luckDiff, 0);
  315. resources.serializeJson(handler, "resources");
  316. {
  317. bool haveSkills = false;
  318. if(handler.saving)
  319. {
  320. for(int primskill : primskills)
  321. if(primskill != 0)
  322. haveSkills = true;
  323. }
  324. else
  325. {
  326. primskills.resize(GameConstants::PRIMARY_SKILLS,0);
  327. haveSkills = true;
  328. }
  329. if(haveSkills)
  330. {
  331. auto s = handler.enterStruct("primarySkills");
  332. for(int idx = 0; idx < primskills.size(); idx ++)
  333. handler.serializeInt(NPrimarySkill::names[idx], primskills[idx], 0);
  334. }
  335. }
  336. if(handler.saving)
  337. {
  338. if(!abilities.empty())
  339. {
  340. auto s = handler.enterStruct("secondarySkills");
  341. for(size_t idx = 0; idx < abilities.size(); idx++)
  342. {
  343. handler.serializeEnum(CSkillHandler::encodeSkill(abilities[idx]), abilityLevels[idx], NSecondarySkill::levels);
  344. }
  345. }
  346. }
  347. else
  348. {
  349. auto s = handler.enterStruct("secondarySkills");
  350. const JsonNode & skillMap = handler.getCurrent();
  351. abilities.clear();
  352. abilityLevels.clear();
  353. for(const auto & p : skillMap.Struct())
  354. {
  355. const std::string skillName = p.first;
  356. const std::string levelId = p.second.String();
  357. const int rawId = CSkillHandler::decodeSkill(skillName);
  358. if(rawId < 0)
  359. {
  360. logGlobal->error("Invalid secondary skill %s", skillName);
  361. continue;
  362. }
  363. const int level = vstd::find_pos(NSecondarySkill::levels, levelId);
  364. if(level < 0)
  365. {
  366. logGlobal->error("Invalid secondary skill level %s", levelId);
  367. continue;
  368. }
  369. abilities.emplace_back(rawId);
  370. abilityLevels.push_back(level);
  371. }
  372. }
  373. handler.serializeIdArray("artifacts", artifacts);
  374. handler.serializeIdArray("spells", spells);
  375. creatures.serializeJson(handler, "creatures");
  376. }
  377. void CGEvent::onHeroVisit( const CGHeroInstance * h ) const
  378. {
  379. if(!(availableFor & (1 << h->tempOwner.getNum())))
  380. return;
  381. if(cb->getPlayerSettings(h->tempOwner)->isControlledByHuman())
  382. {
  383. if(humanActivate)
  384. activated(h);
  385. }
  386. else if(computerActivate)
  387. activated(h);
  388. }
  389. void CGEvent::activated( const CGHeroInstance * h ) const
  390. {
  391. if(stacksCount() > 0)
  392. {
  393. InfoWindow iw;
  394. iw.player = h->tempOwner;
  395. if(!message.empty())
  396. iw.text.appendRawString(message);
  397. else
  398. iw.text.appendLocalString(EMetaText::ADVOB_TXT, 16);
  399. cb->showInfoDialog(&iw);
  400. cb->startBattleI(h, this);
  401. }
  402. else
  403. {
  404. giveContentsUpToExp(h);
  405. }
  406. }
  407. void CGEvent::afterSuccessfulVisit() const
  408. {
  409. if(removeAfterVisit)
  410. {
  411. cb->removeAfterVisit(this);
  412. }
  413. else if(hasGuardians)
  414. hasGuardians = false;
  415. }
  416. void CGEvent::serializeJsonOptions(JsonSerializeFormat & handler)
  417. {
  418. CGPandoraBox::serializeJsonOptions(handler);
  419. handler.serializeBool<bool>("aIActivable", computerActivate, true, false, false);
  420. handler.serializeBool<bool>("humanActivable", humanActivate, true, false, true);
  421. handler.serializeBool<bool>("removeAfterVisit", removeAfterVisit, true, false, false);
  422. {
  423. auto decodePlayer = [](const std::string & id)->si32
  424. {
  425. return vstd::find_pos(GameConstants::PLAYER_COLOR_NAMES, id);
  426. };
  427. auto encodePlayer = [](si32 idx)->std::string
  428. {
  429. return GameConstants::PLAYER_COLOR_NAMES[idx];
  430. };
  431. handler.serializeIdArray<ui8, PlayerColor::PLAYER_LIMIT_I>("availableFor", availableFor, GameConstants::ALL_PLAYERS, decodePlayer, encodePlayer);
  432. }
  433. }
  434. VCMI_LIB_NAMESPACE_END