2
0

CGameStateCampaign.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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 == EStartMode::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. if (!donorHero)
  340. throw std::runtime_error("Failed to find hero to take artifacts from! Scenario: " + gameState->map->name.toString());
  341. for (auto const & artLocation : campaignHeroReplacement.transferrableArtifacts)
  342. {
  343. auto * artifact = donorHero->getArt(artLocation);
  344. if (!donorHero)
  345. throw std::runtime_error("Failed to find artifacts to transfer to travelling hero! Scenario: " + gameState->map->name.toString());
  346. artifact->removeFrom(*donorHero, artLocation);
  347. if (receiver)
  348. {
  349. const auto slot = ArtifactUtils::getArtAnyPosition(receiver, artifact->getTypeId());
  350. if(ArtifactUtils::isSlotEquipment(slot) || ArtifactUtils::isSlotBackpack(slot))
  351. artifact->putAt(*receiver, slot);
  352. else
  353. logGlobal->error("Cannot transfer artifact - no free slots!");
  354. }
  355. else
  356. logGlobal->error("Cannot transfer artifact - no receiver hero!");
  357. }
  358. delete donorHero;
  359. }
  360. }
  361. void CGameStateCampaign::generateCampaignHeroesToReplace()
  362. {
  363. auto campaignState = gameState->scenarioOps->campState;
  364. std::vector<CGHeroPlaceholder *> placeholdersByPower;
  365. std::vector<CGHeroPlaceholder *> placeholdersByType;
  366. campaignHeroReplacements.clear();
  367. // find all placeholders on map
  368. for(auto obj : gameState->map->objects)
  369. {
  370. if(!obj)
  371. continue;
  372. if (obj->ID != Obj::HERO_PLACEHOLDER)
  373. continue;
  374. auto * heroPlaceholder = dynamic_cast<CGHeroPlaceholder *>(obj.get());
  375. // only 1 field must be set
  376. assert(heroPlaceholder->powerRank.has_value() != heroPlaceholder->heroType.has_value());
  377. if(heroPlaceholder->powerRank)
  378. placeholdersByPower.push_back(heroPlaceholder);
  379. if(heroPlaceholder->heroType)
  380. placeholdersByType.push_back(heroPlaceholder);
  381. }
  382. //selecting heroes by type
  383. for(const auto * placeholder : placeholdersByType)
  384. {
  385. const auto & node = campaignState->getHeroByType(*placeholder->heroType);
  386. if (node.isNull())
  387. {
  388. logGlobal->info("Hero crossover: Unable to replace placeholder for %d (%s)!", placeholder->heroType->getNum(), VLC->heroTypes()->getById(*placeholder->heroType)->getNameTranslated());
  389. continue;
  390. }
  391. CGHeroInstance * hero = campaignState->crossoverDeserialize(node, gameState->map);
  392. logGlobal->info("Hero crossover: Loading placeholder for %d (%s)", hero->getHeroType(), hero->getNameTranslated());
  393. campaignHeroReplacements.emplace_back(hero, placeholder->id);
  394. }
  395. auto lastScenario = getHeroesSourceScenario();
  396. if (lastScenario)
  397. {
  398. // sort hero placeholders descending power
  399. boost::range::sort(placeholdersByPower, [](const CGHeroPlaceholder * a, const CGHeroPlaceholder * b)
  400. {
  401. return *a->powerRank > *b->powerRank;
  402. });
  403. const auto & nodeList = campaignState->getHeroesByPower(lastScenario.value());
  404. auto nodeListIter = nodeList.begin();
  405. for(const auto * placeholder : placeholdersByPower)
  406. {
  407. if (nodeListIter == nodeList.end())
  408. break;
  409. CGHeroInstance * hero = campaignState->crossoverDeserialize(*nodeListIter, gameState->map);
  410. nodeListIter++;
  411. logGlobal->info("Hero crossover: Loading placeholder as %d (%s)", hero->getHeroType(), hero->getNameTranslated());
  412. campaignHeroReplacements.emplace_back(hero, placeholder->id);
  413. }
  414. // Add remaining heroes without placeholders - to transfer their artifacts to placed heroes
  415. for (;nodeListIter != nodeList.end(); ++nodeListIter)
  416. {
  417. CGHeroInstance * hero = campaignState->crossoverDeserialize(*nodeListIter, gameState->map);
  418. campaignHeroReplacements.emplace_back(hero, ObjectInstanceID::NONE);
  419. }
  420. }
  421. }
  422. void CGameStateCampaign::initHeroes()
  423. {
  424. auto chosenBonus = currentBonus();
  425. if (chosenBonus && chosenBonus->isBonusForHero() && chosenBonus->info1 != 0xFFFE) //exclude generated heroes
  426. {
  427. //find human player
  428. PlayerColor humanPlayer=PlayerColor::NEUTRAL;
  429. for (auto & elem : gameState->players)
  430. {
  431. if(elem.second.human)
  432. {
  433. humanPlayer = elem.first;
  434. break;
  435. }
  436. }
  437. assert(humanPlayer != PlayerColor::NEUTRAL);
  438. std::vector<ConstTransitivePtr<CGHeroInstance> > & heroes = gameState->players[humanPlayer].heroes;
  439. if (chosenBonus->info1 == 0xFFFD) //most powerful
  440. {
  441. int maxB = -1;
  442. for (int b=0; b<heroes.size(); ++b)
  443. {
  444. if (maxB == -1 || heroes[b]->getTotalStrength() > heroes[maxB]->getTotalStrength())
  445. {
  446. maxB = b;
  447. }
  448. }
  449. if(maxB < 0)
  450. logGlobal->warn("Cannot give bonus to hero cause there are no heroes!");
  451. else
  452. giveCampaignBonusToHero(heroes[maxB]);
  453. }
  454. else //specific hero
  455. {
  456. for (auto & heroe : heroes)
  457. {
  458. if (heroe->getHeroType().getNum() == chosenBonus->info1)
  459. {
  460. giveCampaignBonusToHero(heroe);
  461. break;
  462. }
  463. }
  464. }
  465. }
  466. auto campaignState = gameState->scenarioOps->campState;
  467. auto * yog = gameState->getUsedHero(HeroTypeID::SOLMYR);
  468. if (yog && boost::starts_with(campaignState->getFilename(), "DATA/YOG") && campaignState->currentScenario()->getNum() == 2)
  469. {
  470. assert(yog->isCampaignYog());
  471. gameState->giveHeroArtifact(yog, ArtifactID::ANGELIC_ALLIANCE);
  472. }
  473. transferMissingArtifacts(campaignState->scenario(*campaignState->currentScenario()).travelOptions);
  474. }
  475. void CGameStateCampaign::initStartingResources()
  476. {
  477. auto getHumanPlayerInfo = [&]() -> std::vector<const PlayerSettings *>
  478. {
  479. std::vector<const PlayerSettings *> ret;
  480. for(const auto & playerInfo : gameState->scenarioOps->playerInfos)
  481. {
  482. if(playerInfo.second.isControlledByHuman())
  483. ret.push_back(&playerInfo.second);
  484. }
  485. return ret;
  486. };
  487. auto chosenBonus = currentBonus();
  488. if(chosenBonus && chosenBonus->type == CampaignBonusType::RESOURCE)
  489. {
  490. std::vector<const PlayerSettings *> people = getHumanPlayerInfo(); //players we will give resource bonus
  491. for(const PlayerSettings *ps : people)
  492. {
  493. std::vector<GameResID> res; //resources we will give
  494. switch (chosenBonus->info1)
  495. {
  496. case 0: case 1: case 2: case 3: case 4: case 5: case 6:
  497. res.push_back(chosenBonus->info1);
  498. break;
  499. case 0xFD: //wood+ore
  500. res.push_back(GameResID(EGameResID::WOOD));
  501. res.push_back(GameResID(EGameResID::ORE));
  502. break;
  503. case 0xFE: //rare
  504. res.push_back(GameResID(EGameResID::MERCURY));
  505. res.push_back(GameResID(EGameResID::SULFUR));
  506. res.push_back(GameResID(EGameResID::CRYSTAL));
  507. res.push_back(GameResID(EGameResID::GEMS));
  508. break;
  509. default:
  510. assert(0);
  511. break;
  512. }
  513. //increasing resource quantity
  514. for (auto & re : res)
  515. {
  516. gameState->players[ps->color].resources[re] += chosenBonus->info2;
  517. }
  518. }
  519. }
  520. }
  521. void CGameStateCampaign::initTowns()
  522. {
  523. auto chosenBonus = currentBonus();
  524. if (!chosenBonus)
  525. return;
  526. if (chosenBonus->type != CampaignBonusType::BUILDING)
  527. return;
  528. for (int g=0; g<gameState->map->towns.size(); ++g)
  529. {
  530. CGTownInstance * town = gameState->map->towns[g];
  531. PlayerState * owner = gameState->getPlayerState(town->getOwner());
  532. if (!owner)
  533. continue;
  534. PlayerInfo & pi = gameState->map->players[owner->color.getNum()];
  535. if (!owner->human)
  536. continue;
  537. if (town->pos != pi.posOfMainTown)
  538. continue;
  539. BuildingID newBuilding;
  540. if(gameState->scenarioOps->campState->formatVCMI())
  541. newBuilding = BuildingID(chosenBonus->info1);
  542. else
  543. newBuilding = CBuildingHandler::campToERMU(chosenBonus->info1, town->getFaction(), town->builtBuildings);
  544. // Build granted building & all prerequisites - e.g. Mages Guild Lvl 3 should also give Mages Guild Lvl 1 & 2
  545. while(true)
  546. {
  547. if (newBuilding == BuildingID::NONE)
  548. break;
  549. if (town->builtBuildings.count(newBuilding) != 0)
  550. break;
  551. town->builtBuildings.insert(newBuilding);
  552. auto building = town->town->buildings.at(newBuilding);
  553. newBuilding = building->upgrade;
  554. }
  555. break;
  556. }
  557. }
  558. bool CGameStateCampaign::playerHasStartingHero(PlayerColor playerColor) const
  559. {
  560. auto campaignBonus = currentBonus();
  561. if (!campaignBonus)
  562. return false;
  563. if(campaignBonus->type == CampaignBonusType::HERO && playerColor == PlayerColor(campaignBonus->info1))
  564. return true;
  565. return false;
  566. }
  567. std::unique_ptr<CMap> CGameStateCampaign::getCurrentMap()
  568. {
  569. return gameState->scenarioOps->campState->getMap(CampaignScenarioID::NONE, gameState->callback);
  570. }
  571. VCMI_LIB_NAMESPACE_END