CGameStateCampaign.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. /*
  2. * CGameStateCampaign.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 "CGameStateCampaign.h"
  12. #include "CGameState.h"
  13. #include "QuestInfo.h"
  14. #include "../campaign/CampaignState.h"
  15. #include "../mapping/CMapEditManager.h"
  16. #include "../mapObjects/CGHeroInstance.h"
  17. #include "../mapObjects/CGTownInstance.h"
  18. #include "../networkPacks/ArtifactLocation.h"
  19. #include "../mapObjectConstructors/AObjectTypeHandler.h"
  20. #include "../mapObjectConstructors/CObjectClassesHandler.h"
  21. #include "../StartInfo.h"
  22. #include "../CBuildingHandler.h"
  23. #include "../CHeroHandler.h"
  24. #include "../mapping/CMap.h"
  25. #include "../ArtifactUtils.h"
  26. #include "../CPlayerState.h"
  27. #include "../serializer/CMemorySerializer.h"
  28. VCMI_LIB_NAMESPACE_BEGIN
  29. CampaignHeroReplacement::CampaignHeroReplacement(CGHeroInstance * hero, const ObjectInstanceID & heroPlaceholderId):
  30. hero(hero),
  31. heroPlaceholderId(heroPlaceholderId)
  32. {
  33. }
  34. CGameStateCampaign::CGameStateCampaign(CGameState * owner):
  35. gameState(owner)
  36. {
  37. assert(gameState->scenarioOps->mode == StartInfo::CAMPAIGN);
  38. assert(gameState->scenarioOps->campState != nullptr);
  39. }
  40. std::optional<CampaignBonus> CGameStateCampaign::currentBonus() const
  41. {
  42. auto campaignState = gameState->scenarioOps->campState;
  43. return campaignState->getBonus(*campaignState->currentScenario());
  44. }
  45. std::optional<CampaignScenarioID> CGameStateCampaign::getHeroesSourceScenario() const
  46. {
  47. auto campaignState = gameState->scenarioOps->campState;
  48. auto bonus = currentBonus();
  49. if(bonus && bonus->type == CampaignBonusType::HEROES_FROM_PREVIOUS_SCENARIO)
  50. return static_cast<CampaignScenarioID>(bonus->info2);
  51. return campaignState->lastScenario();
  52. }
  53. void CGameStateCampaign::trimCrossoverHeroesParameters(const CampaignTravel & travelOptions)
  54. {
  55. // TODO this logic (what should be kept) should be part of CScenarioTravel and be exposed via some clean set of methods
  56. if(!travelOptions.whatHeroKeeps.experience)
  57. {
  58. //trimming experience
  59. for(auto & hero : campaignHeroReplacements)
  60. {
  61. hero.hero->initExp(gameState->getRandomGenerator());
  62. }
  63. }
  64. if(!travelOptions.whatHeroKeeps.primarySkills)
  65. {
  66. //trimming prim skills
  67. for(auto & hero : campaignHeroReplacements)
  68. {
  69. for(auto g = PrimarySkill::BEGIN; g < PrimarySkill::END; ++g)
  70. {
  71. auto sel = Selector::type()(BonusType::PRIMARY_SKILL)
  72. .And(Selector::subtype()(BonusSubtypeID(g)))
  73. .And(Selector::sourceType()(BonusSource::HERO_BASE_SKILL));
  74. hero.hero->getLocalBonus(sel)->val = hero.hero->type->heroClass->primarySkillInitial[g.getNum()];
  75. }
  76. }
  77. }
  78. if(!travelOptions.whatHeroKeeps.secondarySkills)
  79. {
  80. //trimming sec skills
  81. for(auto & hero : campaignHeroReplacements)
  82. {
  83. hero.hero->secSkills = hero.hero->type->secSkillsInit;
  84. hero.hero->recreateSecondarySkillsBonuses();
  85. }
  86. }
  87. if(!travelOptions.whatHeroKeeps.spells)
  88. {
  89. for(auto & hero : campaignHeroReplacements)
  90. {
  91. hero.hero->removeSpellbook();
  92. }
  93. }
  94. if(!travelOptions.whatHeroKeeps.artifacts)
  95. {
  96. //trimming artifacts
  97. for(auto & hero : campaignHeroReplacements)
  98. {
  99. const auto & checkAndRemoveArtifact = [&](const ArtifactPosition & artifactPosition)
  100. {
  101. if(artifactPosition == ArtifactPosition::SPELLBOOK)
  102. return; // do not handle spellbook this way
  103. const ArtSlotInfo *info = hero.hero->getSlot(artifactPosition);
  104. if(!info)
  105. return;
  106. // TODO: why would there be nullptr artifacts?
  107. const CArtifactInstance *art = info->artifact;
  108. if(!art)
  109. return;
  110. bool takeable = travelOptions.artifactsKeptByHero.count(art->artType->getId());
  111. if (takeable)
  112. hero.transferrableArtifacts.push_back(artifactPosition);
  113. ArtifactLocation al(hero.hero->id, artifactPosition);
  114. if(!takeable && !hero.hero->getSlot(al.slot)->locked) //don't try removing locked artifacts -> it crashes #1719
  115. hero.hero->getArt(al.slot)->removeFrom(*hero.hero, al.slot);
  116. };
  117. // process on copy - removal of artifact will invalidate container
  118. auto artifactsWorn = hero.hero->artifactsWorn;
  119. for(const auto & art : artifactsWorn)
  120. checkAndRemoveArtifact(art.first);
  121. // process in reverse - removal of artifact will shift all artifacts after this one
  122. for(int slotNumber = hero.hero->artifactsInBackpack.size() - 1; slotNumber >= 0; slotNumber--)
  123. checkAndRemoveArtifact(ArtifactPosition::BACKPACK_START + slotNumber);
  124. }
  125. }
  126. //trimming creatures
  127. for(auto & hero : campaignHeroReplacements)
  128. {
  129. auto shouldSlotBeErased = [&](const std::pair<SlotID, CStackInstance *> & j) -> bool
  130. {
  131. CreatureID crid = j.second->getCreatureID();
  132. return !travelOptions.monstersKeptByHero.count(crid);
  133. };
  134. auto stacksCopy = hero.hero->stacks; //copy of the map, so we can iterate iover it and remove stacks
  135. for(auto &slotPair : stacksCopy)
  136. if(shouldSlotBeErased(slotPair))
  137. hero.hero->eraseStack(slotPair.first);
  138. }
  139. // Removing short-term bonuses
  140. for(auto & hero : campaignHeroReplacements)
  141. {
  142. hero.hero->removeBonusesRecursive(CSelector(Bonus::OneDay)
  143. .Or(CSelector(Bonus::OneWeek))
  144. .Or(CSelector(Bonus::NTurns))
  145. .Or(CSelector(Bonus::NDays))
  146. .Or(CSelector(Bonus::OneBattle)));
  147. }
  148. }
  149. void CGameStateCampaign::placeCampaignHeroes()
  150. {
  151. // place bonus hero
  152. auto campaignState = gameState->scenarioOps->campState;
  153. auto campaignBonus = campaignState->getBonus(*campaignState->currentScenario());
  154. bool campaignGiveHero = campaignBonus && campaignBonus->type == CampaignBonusType::HERO;
  155. if(campaignGiveHero)
  156. {
  157. auto playerColor = PlayerColor(campaignBonus->info1);
  158. auto it = gameState->scenarioOps->playerInfos.find(playerColor);
  159. if(it != gameState->scenarioOps->playerInfos.end())
  160. {
  161. HeroTypeID heroTypeId = HeroTypeID(campaignBonus->info2);
  162. if(heroTypeId.getNum() == 0xffff) // random bonus hero
  163. {
  164. heroTypeId = gameState->pickUnusedHeroTypeRandomly(playerColor);
  165. }
  166. gameState->placeStartingHero(playerColor, HeroTypeID(heroTypeId), gameState->map->players[playerColor.getNum()].posOfMainTown);
  167. }
  168. }
  169. logGlobal->debug("\tGenerate list of hero placeholders");
  170. generateCampaignHeroesToReplace();
  171. logGlobal->debug("\tPrepare crossover heroes");
  172. trimCrossoverHeroesParameters(campaignState->scenario(*campaignState->currentScenario()).travelOptions);
  173. // remove same heroes on the map which will be added through crossover heroes
  174. // INFO: we will remove heroes because later it may be possible that the API doesn't allow having heroes
  175. // with the same hero type id
  176. std::vector<CGHeroInstance *> removedHeroes;
  177. std::set<HeroTypeID> reservedHeroes = campaignState->getReservedHeroes();
  178. std::set<HeroTypeID> heroesToRemove;
  179. for (auto const & heroID : reservedHeroes )
  180. {
  181. // Do not replace reserved heroes initially, e.g. in 1st campaign scenario in which they appear
  182. if (!campaignState->getHeroByType(heroID).isNull())
  183. heroesToRemove.insert(heroID);
  184. }
  185. for(auto & replacement : campaignHeroReplacements)
  186. if (replacement.heroPlaceholderId.hasValue())
  187. heroesToRemove.insert(replacement.hero->getHeroType());
  188. for(auto & heroID : heroesToRemove)
  189. {
  190. auto * hero = gameState->getUsedHero(heroID);
  191. if(hero)
  192. {
  193. removedHeroes.push_back(hero);
  194. gameState->map->heroesOnMap -= hero;
  195. gameState->map->objects[hero->id.getNum()] = nullptr;
  196. gameState->map->removeBlockVisTiles(hero, true);
  197. }
  198. }
  199. logGlobal->debug("\tReplace placeholders with heroes");
  200. replaceHeroesPlaceholders();
  201. // now add removed heroes again with unused type ID
  202. for(auto * hero : removedHeroes)
  203. {
  204. HeroTypeID heroTypeId;
  205. if(hero->ID == Obj::HERO)
  206. {
  207. heroTypeId = gameState->pickUnusedHeroTypeRandomly(hero->tempOwner);
  208. }
  209. else if(hero->ID == Obj::PRISON)
  210. {
  211. auto unusedHeroTypeIds = gameState->getUnusedAllowedHeroes();
  212. if(!unusedHeroTypeIds.empty())
  213. {
  214. heroTypeId = (*RandomGeneratorUtil::nextItem(unusedHeroTypeIds, gameState->getRandomGenerator()));
  215. }
  216. else
  217. {
  218. logGlobal->error("No free hero type ID found to replace prison.");
  219. assert(0);
  220. }
  221. }
  222. else
  223. {
  224. assert(0); // should not happen
  225. }
  226. hero->setHeroType(heroTypeId);
  227. gameState->map->getEditManager()->insertObject(hero);
  228. }
  229. }
  230. void CGameStateCampaign::giveCampaignBonusToHero(CGHeroInstance * hero)
  231. {
  232. auto curBonus = currentBonus();
  233. if(!curBonus)
  234. return;
  235. assert(curBonus->isBonusForHero());
  236. //apply bonus
  237. switch(curBonus->type)
  238. {
  239. case CampaignBonusType::SPELL:
  240. {
  241. hero->addSpellToSpellbook(SpellID(curBonus->info2));
  242. break;
  243. }
  244. case CampaignBonusType::MONSTER:
  245. {
  246. for(int i = 0; i < GameConstants::ARMY_SIZE; i++)
  247. {
  248. if(hero->slotEmpty(SlotID(i)))
  249. {
  250. hero->addToSlot(SlotID(i), CreatureID(curBonus->info2), curBonus->info3);
  251. break;
  252. }
  253. }
  254. break;
  255. }
  256. case CampaignBonusType::ARTIFACT:
  257. {
  258. if(!gameState->giveHeroArtifact(hero, static_cast<ArtifactID>(curBonus->info2)))
  259. logGlobal->error("Cannot give starting artifact - no free slots!");
  260. break;
  261. }
  262. case CampaignBonusType::SPELL_SCROLL:
  263. {
  264. CArtifactInstance * scroll = ArtifactUtils::createScroll(SpellID(curBonus->info2));
  265. const auto slot = ArtifactUtils::getArtAnyPosition(hero, scroll->getTypeId());
  266. if(ArtifactUtils::isSlotEquipment(slot) || ArtifactUtils::isSlotBackpack(slot))
  267. scroll->putAt(*hero, slot);
  268. else
  269. logGlobal->error("Cannot give starting scroll - no free slots!");
  270. break;
  271. }
  272. case CampaignBonusType::PRIMARY_SKILL:
  273. {
  274. const ui8 * ptr = reinterpret_cast<const ui8 *>(&curBonus->info2);
  275. for(auto g = PrimarySkill::BEGIN; g < PrimarySkill::END; ++g)
  276. {
  277. int val = ptr[g.getNum()];
  278. if(val == 0)
  279. continue;
  280. auto currentScenario = *gameState->scenarioOps->campState->currentScenario();
  281. auto bb = std::make_shared<Bonus>( BonusDuration::PERMANENT, BonusType::PRIMARY_SKILL, BonusSource::CAMPAIGN_BONUS, val, BonusSourceID(currentScenario), BonusSubtypeID(g) );
  282. hero->addNewBonus(bb);
  283. }
  284. break;
  285. }
  286. case CampaignBonusType::SECONDARY_SKILL:
  287. {
  288. hero->setSecSkillLevel(SecondarySkill(curBonus->info2), curBonus->info3, true);
  289. break;
  290. }
  291. }
  292. }
  293. void CGameStateCampaign::replaceHeroesPlaceholders()
  294. {
  295. for(const auto & campaignHeroReplacement : campaignHeroReplacements)
  296. {
  297. if (!campaignHeroReplacement.heroPlaceholderId.hasValue())
  298. continue;
  299. auto * heroPlaceholder = dynamic_cast<CGHeroPlaceholder *>(gameState->getObjInstance(campaignHeroReplacement.heroPlaceholderId));
  300. CGHeroInstance *heroToPlace = campaignHeroReplacement.hero;
  301. heroToPlace->id = campaignHeroReplacement.heroPlaceholderId;
  302. if(heroPlaceholder->tempOwner.isValidPlayer())
  303. heroToPlace->tempOwner = heroPlaceholder->tempOwner;
  304. heroToPlace->pos = heroPlaceholder->pos;
  305. heroToPlace->type = heroToPlace->getHeroType().toHeroType();
  306. heroToPlace->appearance = VLC->objtypeh->getHandlerFor(Obj::HERO, heroToPlace->type->heroClass->getIndex())->getTemplates().front();
  307. gameState->map->removeBlockVisTiles(heroPlaceholder, true);
  308. gameState->map->objects[heroPlaceholder->id.getNum()] = nullptr;
  309. gameState->map->instanceNames.erase(heroPlaceholder->instanceName);
  310. gameState->map->heroesOnMap.emplace_back(heroToPlace);
  311. gameState->map->objects[heroToPlace->id.getNum()] = heroToPlace;
  312. gameState->map->addBlockVisTiles(heroToPlace);
  313. gameState->map->instanceNames[heroToPlace->instanceName] = heroToPlace;
  314. delete heroPlaceholder;
  315. }
  316. }
  317. void CGameStateCampaign::transferMissingArtifacts(const CampaignTravel & travelOptions)
  318. {
  319. CGHeroInstance * receiver = nullptr;
  320. for(auto obj : gameState->map->objects)
  321. {
  322. if (!obj)
  323. continue;
  324. if (obj->ID != Obj::HERO)
  325. continue;
  326. auto * hero = dynamic_cast<CGHeroInstance *>(obj.get());
  327. if (gameState->getPlayerState(hero->getOwner())->isHuman())
  328. {
  329. receiver = hero;
  330. break;
  331. }
  332. }
  333. assert(receiver);
  334. for(const auto & campaignHeroReplacement : campaignHeroReplacements)
  335. {
  336. if (campaignHeroReplacement.heroPlaceholderId.hasValue())
  337. continue;
  338. auto * donorHero = campaignHeroReplacement.hero;
  339. for (auto const & artLocation : campaignHeroReplacement.transferrableArtifacts)
  340. {
  341. auto * artifact = donorHero->getArt(artLocation);
  342. artifact->removeFrom(*donorHero, artLocation);
  343. if (receiver)
  344. {
  345. const auto slot = ArtifactUtils::getArtAnyPosition(receiver, artifact->getTypeId());
  346. if(ArtifactUtils::isSlotEquipment(slot) || ArtifactUtils::isSlotBackpack(slot))
  347. artifact->putAt(*receiver, slot);
  348. else
  349. logGlobal->error("Cannot transfer artifact - no free slots!");
  350. }
  351. else
  352. logGlobal->error("Cannot transfer artifact - no receiver hero!");
  353. }
  354. delete donorHero;
  355. }
  356. }
  357. void CGameStateCampaign::generateCampaignHeroesToReplace()
  358. {
  359. auto campaignState = gameState->scenarioOps->campState;
  360. std::vector<CGHeroPlaceholder *> placeholdersByPower;
  361. std::vector<CGHeroPlaceholder *> placeholdersByType;
  362. campaignHeroReplacements.clear();
  363. // find all placeholders on map
  364. for(auto obj : gameState->map->objects)
  365. {
  366. if(!obj)
  367. continue;
  368. if (obj->ID != Obj::HERO_PLACEHOLDER)
  369. continue;
  370. auto * heroPlaceholder = dynamic_cast<CGHeroPlaceholder *>(obj.get());
  371. // only 1 field must be set
  372. assert(heroPlaceholder->powerRank.has_value() != heroPlaceholder->heroType.has_value());
  373. if(heroPlaceholder->powerRank)
  374. placeholdersByPower.push_back(heroPlaceholder);
  375. if(heroPlaceholder->heroType)
  376. placeholdersByType.push_back(heroPlaceholder);
  377. }
  378. //selecting heroes by type
  379. for(const auto * placeholder : placeholdersByType)
  380. {
  381. const auto & node = campaignState->getHeroByType(*placeholder->heroType);
  382. if (node.isNull())
  383. {
  384. logGlobal->info("Hero crossover: Unable to replace placeholder for %d (%s)!", placeholder->heroType->getNum(), VLC->heroTypes()->getById(*placeholder->heroType)->getNameTranslated());
  385. continue;
  386. }
  387. CGHeroInstance * hero = campaignState->crossoverDeserialize(node, gameState->map);
  388. logGlobal->info("Hero crossover: Loading placeholder for %d (%s)", hero->getHeroType(), hero->getNameTranslated());
  389. campaignHeroReplacements.emplace_back(hero, placeholder->id);
  390. }
  391. auto lastScenario = getHeroesSourceScenario();
  392. if (lastScenario)
  393. {
  394. // sort hero placeholders descending power
  395. boost::range::sort(placeholdersByPower, [](const CGHeroPlaceholder * a, const CGHeroPlaceholder * b)
  396. {
  397. return *a->powerRank > *b->powerRank;
  398. });
  399. const auto & nodeList = campaignState->getHeroesByPower(lastScenario.value());
  400. auto nodeListIter = nodeList.begin();
  401. for(const auto * placeholder : placeholdersByPower)
  402. {
  403. if (nodeListIter == nodeList.end())
  404. break;
  405. CGHeroInstance * hero = campaignState->crossoverDeserialize(*nodeListIter, gameState->map);
  406. nodeListIter++;
  407. logGlobal->info("Hero crossover: Loading placeholder as %d (%s)", hero->getHeroType(), hero->getNameTranslated());
  408. campaignHeroReplacements.emplace_back(hero, placeholder->id);
  409. }
  410. // Add remaining heroes without placeholders - to transfer their artifacts to placed heroes
  411. for (;nodeListIter != nodeList.end(); ++nodeListIter)
  412. {
  413. CGHeroInstance * hero = campaignState->crossoverDeserialize(*nodeListIter, gameState->map);
  414. campaignHeroReplacements.emplace_back(hero, ObjectInstanceID::NONE);
  415. }
  416. }
  417. }
  418. void CGameStateCampaign::initHeroes()
  419. {
  420. auto chosenBonus = currentBonus();
  421. if (chosenBonus && chosenBonus->isBonusForHero() && chosenBonus->info1 != 0xFFFE) //exclude generated heroes
  422. {
  423. //find human player
  424. PlayerColor humanPlayer=PlayerColor::NEUTRAL;
  425. for (auto & elem : gameState->players)
  426. {
  427. if(elem.second.human)
  428. {
  429. humanPlayer = elem.first;
  430. break;
  431. }
  432. }
  433. assert(humanPlayer != PlayerColor::NEUTRAL);
  434. std::vector<ConstTransitivePtr<CGHeroInstance> > & heroes = gameState->players[humanPlayer].heroes;
  435. if (chosenBonus->info1 == 0xFFFD) //most powerful
  436. {
  437. int maxB = -1;
  438. for (int b=0; b<heroes.size(); ++b)
  439. {
  440. if (maxB == -1 || heroes[b]->getTotalStrength() > heroes[maxB]->getTotalStrength())
  441. {
  442. maxB = b;
  443. }
  444. }
  445. if(maxB < 0)
  446. logGlobal->warn("Cannot give bonus to hero cause there are no heroes!");
  447. else
  448. giveCampaignBonusToHero(heroes[maxB]);
  449. }
  450. else //specific hero
  451. {
  452. for (auto & heroe : heroes)
  453. {
  454. if (heroe->getHeroType().getNum() == chosenBonus->info1)
  455. {
  456. giveCampaignBonusToHero(heroe);
  457. break;
  458. }
  459. }
  460. }
  461. }
  462. auto campaignState = gameState->scenarioOps->campState;
  463. auto * yog = gameState->getUsedHero(HeroTypeID::SOLMYR);
  464. if (yog && boost::starts_with(campaignState->getFilename(), "DATA/YOG") && campaignState->currentScenario()->getNum() == 2)
  465. {
  466. assert(yog->isCampaignYog());
  467. gameState->giveHeroArtifact(yog, ArtifactID::ANGELIC_ALLIANCE);
  468. }
  469. transferMissingArtifacts(campaignState->scenario(*campaignState->currentScenario()).travelOptions);
  470. }
  471. void CGameStateCampaign::initStartingResources()
  472. {
  473. auto getHumanPlayerInfo = [&]() -> std::vector<const PlayerSettings *>
  474. {
  475. std::vector<const PlayerSettings *> ret;
  476. for(const auto & playerInfo : gameState->scenarioOps->playerInfos)
  477. {
  478. if(playerInfo.second.isControlledByHuman())
  479. ret.push_back(&playerInfo.second);
  480. }
  481. return ret;
  482. };
  483. auto chosenBonus = currentBonus();
  484. if(chosenBonus && chosenBonus->type == CampaignBonusType::RESOURCE)
  485. {
  486. std::vector<const PlayerSettings *> people = getHumanPlayerInfo(); //players we will give resource bonus
  487. for(const PlayerSettings *ps : people)
  488. {
  489. std::vector<GameResID> res; //resources we will give
  490. switch (chosenBonus->info1)
  491. {
  492. case 0: case 1: case 2: case 3: case 4: case 5: case 6:
  493. res.push_back(chosenBonus->info1);
  494. break;
  495. case 0xFD: //wood+ore
  496. res.push_back(GameResID(EGameResID::WOOD));
  497. res.push_back(GameResID(EGameResID::ORE));
  498. break;
  499. case 0xFE: //rare
  500. res.push_back(GameResID(EGameResID::MERCURY));
  501. res.push_back(GameResID(EGameResID::SULFUR));
  502. res.push_back(GameResID(EGameResID::CRYSTAL));
  503. res.push_back(GameResID(EGameResID::GEMS));
  504. break;
  505. default:
  506. assert(0);
  507. break;
  508. }
  509. //increasing resource quantity
  510. for (auto & re : res)
  511. {
  512. gameState->players[ps->color].resources[re] += chosenBonus->info2;
  513. }
  514. }
  515. }
  516. }
  517. void CGameStateCampaign::initTowns()
  518. {
  519. auto chosenBonus = currentBonus();
  520. if (!chosenBonus)
  521. return;
  522. if (chosenBonus->type != CampaignBonusType::BUILDING)
  523. return;
  524. for (int g=0; g<gameState->map->towns.size(); ++g)
  525. {
  526. CGTownInstance * town = gameState->map->towns[g];
  527. PlayerState * owner = gameState->getPlayerState(town->getOwner());
  528. if (!owner)
  529. continue;
  530. PlayerInfo & pi = gameState->map->players[owner->color.getNum()];
  531. if (!owner->human)
  532. continue;
  533. if (town->pos != pi.posOfMainTown)
  534. continue;
  535. BuildingID newBuilding;
  536. if(gameState->scenarioOps->campState->formatVCMI())
  537. newBuilding = BuildingID(chosenBonus->info1);
  538. else
  539. newBuilding = CBuildingHandler::campToERMU(chosenBonus->info1, town->getFaction(), town->builtBuildings);
  540. // Build granted building & all prerequisites - e.g. Mages Guild Lvl 3 should also give Mages Guild Lvl 1 & 2
  541. while(true)
  542. {
  543. if (newBuilding == BuildingID::NONE)
  544. break;
  545. if (town->builtBuildings.count(newBuilding) != 0)
  546. break;
  547. town->builtBuildings.insert(newBuilding);
  548. auto building = town->town->buildings.at(newBuilding);
  549. newBuilding = building->upgrade;
  550. }
  551. break;
  552. }
  553. }
  554. bool CGameStateCampaign::playerHasStartingHero(PlayerColor playerColor) const
  555. {
  556. auto campaignBonus = currentBonus();
  557. if (!campaignBonus)
  558. return false;
  559. if(campaignBonus->type == CampaignBonusType::HERO && playerColor == PlayerColor(campaignBonus->info1))
  560. return true;
  561. return false;
  562. }
  563. std::unique_ptr<CMap> CGameStateCampaign::getCurrentMap()
  564. {
  565. return gameState->scenarioOps->campState->getMap(CampaignScenarioID::NONE, gameState->callback);
  566. }
  567. VCMI_LIB_NAMESPACE_END