CGPandoraBox.cpp 13 KB

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