CGPandoraBox.cpp 13 KB

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