2
0

CGPandoraBox.cpp 13 KB

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