CGPandoraBox.cpp 13 KB

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