CGameStateCampaign.cpp 20 KB

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