CGPandoraBox.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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.type = EInfoWindowMode::AUTO;
  27. iw.soundID = soundID;
  28. iw.player = playerID;
  29. iw.text.addTxt(MetaString::ADVOB_TXT,txtID);
  30. IObjectInterface::cb->sendAndApply(&iw);
  31. }
  32. static void showInfoDialog(const CGHeroInstance* h, const ui32 txtID, const ui16 soundID)
  33. {
  34. const PlayerColor playerID = h->getOwner();
  35. showInfoDialog(playerID,txtID,soundID);
  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.type = EInfoWindowMode::AUTO;
  54. iw.player = h->getOwner();
  55. bool changesPrimSkill = false;
  56. for(const 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.emplace_back(abilities[i], abilityLevels[i]);
  76. }
  77. }
  78. if(gainedExp || changesPrimSkill || !unpossessedAbilities.empty())
  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->getNameTranslated());
  84. if(expVal)
  85. iw.components.emplace_back(Component::EXPERIENCE, 0, static_cast<si32>(expVal), 0);
  86. for(int i=0; i<primskills.size(); i++)
  87. if(primskills[i])
  88. iw.components.emplace_back(Component::PRIM_SKILL, i, primskills[i], 0);
  89. for(const auto & abilityData : unpossessedAbilities)
  90. iw.components.emplace_back(Component::SEC_SKILL, abilityData.first, abilityData.second, 0);
  91. cb->showInfoDialog(&iw);
  92. //give sec skills
  93. for(const 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.type = EInfoWindowMode::AUTO;
  116. iw.player = h->getOwner();
  117. //TODO: reuse this code for Scholar skill
  118. if(!spells.empty())
  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. const auto * spell = (*i).toSpell(VLC->spells());
  130. if(h->canLearnSpell(spell))
  131. {
  132. iw.components.emplace_back(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->getNameTranslated());
  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.emplace_back(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.emplace_back(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.emplace_back(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.emplace_back(Component::RESOURCE, i, resources[i], 0);
  189. }
  190. if(!iw.components.empty())
  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.emplace_back(Component::RESOURCE, i, resources[i], 0);
  201. }
  202. if(!iw.components.empty())
  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->getNameTranslated());
  211. for(const auto & elem : artifacts)
  212. {
  213. iw.components.emplace_back(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->getNameTranslated());
  220. }
  221. }
  222. if(!iw.components.empty())
  223. {
  224. cb->showInfoDialog(&iw);
  225. }
  226. cb->giveResources(h->getOwner(), resources);
  227. for(const 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(const auto & elem : creatures.Slots())
  235. { //build list of joined creatures
  236. iw.components.emplace_back(*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->getNameTranslated());
  246. cb->showInfoDialog(&iw);
  247. cb->giveCreatures(this, h, creatures, false);
  248. }
  249. if(!hasGuardians && !msg.empty())
  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.empty())
  258. {
  259. iw.text.addTxt(MetaString::ADVOB_TXT,text);//%s has lost treasure.
  260. iw.text.addReplacement(h->getNameTranslated());
  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.empty())
  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->getNameTranslated());
  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.empty() && resources.empty()
  300. && primskills.empty() && abilities.empty()
  301. && abilityLevels.empty() && artifacts.empty()
  302. && spells.empty() && 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. void CGPandoraBox::serializeJsonOptions(JsonSerializeFormat & handler)
  323. {
  324. CCreatureSet::serializeJson(handler, "guards", 7);
  325. handler.serializeString("guardMessage", message);
  326. handler.serializeInt("experience", gainedExp, 0);
  327. handler.serializeInt("mana", manaDiff, 0);
  328. handler.serializeInt("morale", moraleDiff, 0);
  329. handler.serializeInt("luck", luckDiff, 0);
  330. resources.serializeJson(handler, "resources");
  331. {
  332. bool haveSkills = false;
  333. if(handler.saving)
  334. {
  335. for(int primskill : primskills)
  336. if(primskill != 0)
  337. haveSkills = true;
  338. }
  339. else
  340. {
  341. primskills.resize(GameConstants::PRIMARY_SKILLS,0);
  342. haveSkills = true;
  343. }
  344. if(haveSkills)
  345. {
  346. auto s = handler.enterStruct("primarySkills");
  347. for(int idx = 0; idx < primskills.size(); idx ++)
  348. handler.serializeInt(PrimarySkill::names[idx], primskills[idx], 0);
  349. }
  350. }
  351. if(handler.saving)
  352. {
  353. if(!abilities.empty())
  354. {
  355. auto s = handler.enterStruct("secondarySkills");
  356. for(size_t idx = 0; idx < abilities.size(); idx++)
  357. {
  358. handler.serializeEnum(CSkillHandler::encodeSkill(abilities[idx]), abilityLevels[idx], NSecondarySkill::levels);
  359. }
  360. }
  361. }
  362. else
  363. {
  364. auto s = handler.enterStruct("secondarySkills");
  365. const JsonNode & skillMap = handler.getCurrent();
  366. abilities.clear();
  367. abilityLevels.clear();
  368. for(const auto & p : skillMap.Struct())
  369. {
  370. const std::string skillName = p.first;
  371. const std::string levelId = p.second.String();
  372. const int rawId = CSkillHandler::decodeSkill(skillName);
  373. if(rawId < 0)
  374. {
  375. logGlobal->error("Invalid secondary skill %s", skillName);
  376. continue;
  377. }
  378. const int level = vstd::find_pos(NSecondarySkill::levels, levelId);
  379. if(level < 0)
  380. {
  381. logGlobal->error("Invalid secondary skill level %s", levelId);
  382. continue;
  383. }
  384. abilities.emplace_back(rawId);
  385. abilityLevels.push_back(level);
  386. }
  387. }
  388. handler.serializeIdArray("artifacts", artifacts);
  389. handler.serializeIdArray("spells", spells);
  390. creatures.serializeJson(handler, "creatures");
  391. }
  392. void CGEvent::onHeroVisit( const CGHeroInstance * h ) const
  393. {
  394. if(!(availableFor & (1 << h->tempOwner.getNum())))
  395. return;
  396. if(cb->getPlayerSettings(h->tempOwner)->isControlledByHuman())
  397. {
  398. if(humanActivate)
  399. activated(h);
  400. }
  401. else if(computerActivate)
  402. activated(h);
  403. }
  404. void CGEvent::activated( const CGHeroInstance * h ) const
  405. {
  406. if(stacksCount() > 0)
  407. {
  408. InfoWindow iw;
  409. iw.player = h->tempOwner;
  410. if(!message.empty())
  411. iw.text << message;
  412. else
  413. iw.text.addTxt(MetaString::ADVOB_TXT, 16);
  414. cb->showInfoDialog(&iw);
  415. cb->startBattleI(h, this);
  416. }
  417. else
  418. {
  419. giveContentsUpToExp(h);
  420. }
  421. }
  422. void CGEvent::afterSuccessfulVisit() const
  423. {
  424. if(removeAfterVisit)
  425. {
  426. cb->removeAfterVisit(this);
  427. }
  428. else if(hasGuardians)
  429. hasGuardians = false;
  430. }
  431. void CGEvent::serializeJsonOptions(JsonSerializeFormat & handler)
  432. {
  433. CGPandoraBox::serializeJsonOptions(handler);
  434. handler.serializeBool<bool>("aIActivable", computerActivate, true, false, false);
  435. handler.serializeBool<bool>("humanActivable", humanActivate, true, false, true);
  436. handler.serializeBool<bool>("removeAfterVisit", removeAfterVisit, true, false, false);
  437. {
  438. auto decodePlayer = [](const std::string & id)->si32
  439. {
  440. return vstd::find_pos(GameConstants::PLAYER_COLOR_NAMES, id);
  441. };
  442. auto encodePlayer = [](si32 idx)->std::string
  443. {
  444. return GameConstants::PLAYER_COLOR_NAMES[idx];
  445. };
  446. handler.serializeIdArray<ui8, PlayerColor::PLAYER_LIMIT_I>("availableFor", availableFor, GameConstants::ALL_PLAYERS, decodePlayer, encodePlayer);
  447. }
  448. }
  449. VCMI_LIB_NAMESPACE_END