CGameStateCampaign.cpp 20 KB

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