CGameStateCampaign.cpp 20 KB

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