CGPandoraBox.cpp 13 KB

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