NewTurnProcessor.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. /*
  2. * NewTurnProcessor.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 "NewTurnProcessor.h"
  12. #include "HeroPoolProcessor.h"
  13. #include "../CGameHandler.h"
  14. #include "../../lib/CPlayerState.h"
  15. #include "../../lib/GameSettings.h"
  16. #include "../../lib/StartInfo.h"
  17. #include "../../lib/TerrainHandler.h"
  18. #include "../../lib/entities/building/CBuilding.h"
  19. #include "../../lib/entities/faction/CTownHandler.h"
  20. #include "../../lib/gameState/CGameState.h"
  21. #include "../../lib/gameState/SThievesGuildInfo.h"
  22. #include "../../lib/mapObjects/CGHeroInstance.h"
  23. #include "../../lib/mapObjects/CGTownInstance.h"
  24. #include "../../lib/mapObjects/IOwnableObject.h"
  25. #include "../../lib/mapping/CMap.h"
  26. #include "../../lib/networkPacks/PacksForClient.h"
  27. #include "../../lib/pathfinder/TurnInfo.h"
  28. #include "../../lib/texts/CGeneralTextHandler.h"
  29. #include <vstd/RNG.h>
  30. NewTurnProcessor::NewTurnProcessor(CGameHandler * gameHandler)
  31. :gameHandler(gameHandler)
  32. {
  33. }
  34. void NewTurnProcessor::handleTimeEvents(PlayerColor color)
  35. {
  36. for (auto const & event : gameHandler->gameState()->map->events)
  37. {
  38. if (!event.occursToday(gameHandler->gameState()->day))
  39. continue;
  40. if (!event.affectsPlayer(color, gameHandler->getPlayerState(color)->isHuman()))
  41. continue;
  42. InfoWindow iw;
  43. iw.player = color;
  44. iw.text = event.message;
  45. //give resources
  46. if (!event.resources.empty())
  47. {
  48. gameHandler->giveResources(color, event.resources);
  49. for (GameResID i : GameResID::ALL_RESOURCES())
  50. if (event.resources[i])
  51. iw.components.emplace_back(ComponentType::RESOURCE, i, event.resources[i]);
  52. }
  53. gameHandler->sendAndApply(&iw); //show dialog
  54. }
  55. }
  56. void NewTurnProcessor::handleTownEvents(const CGTownInstance * town)
  57. {
  58. for (auto const & event : town->events)
  59. {
  60. if (!event.occursToday(gameHandler->gameState()->day))
  61. continue;
  62. PlayerColor player = town->getOwner();
  63. if (!event.affectsPlayer(player, gameHandler->getPlayerState(player)->isHuman()))
  64. continue;
  65. // dialog
  66. InfoWindow iw;
  67. iw.player = player;
  68. iw.text = event.message;
  69. if (event.resources.nonZero())
  70. {
  71. gameHandler->giveResources(player, event.resources);
  72. for (GameResID i : GameResID::ALL_RESOURCES())
  73. if (event.resources[i])
  74. iw.components.emplace_back(ComponentType::RESOURCE, i, event.resources[i]);
  75. }
  76. for (auto & i : event.buildings)
  77. {
  78. // Only perform action if:
  79. // 1. Building exists in town (don't attempt to build Lvl 5 guild in Fortress
  80. // 2. Building was not built yet
  81. // othervice, silently ignore / skip it
  82. if (town->town->buildings.count(i) && !town->hasBuilt(i))
  83. {
  84. gameHandler->buildStructure(town->id, i, true);
  85. iw.components.emplace_back(ComponentType::BUILDING, BuildingTypeUniqueID(town->getFaction(), i));
  86. }
  87. }
  88. if (!event.creatures.empty())
  89. {
  90. SetAvailableCreatures sac;
  91. sac.tid = town->id;
  92. sac.creatures = town->creatures;
  93. for (si32 i=0;i<event.creatures.size();i++) //creature growths
  94. {
  95. if (!town->creatures.at(i).second.empty() && event.creatures.at(i) > 0)//there is dwelling
  96. {
  97. sac.creatures[i].first += event.creatures.at(i);
  98. iw.components.emplace_back(ComponentType::CREATURE, town->creatures.at(i).second.back(), event.creatures.at(i));
  99. }
  100. }
  101. }
  102. gameHandler->sendAndApply(&iw); //show dialog
  103. }
  104. }
  105. void NewTurnProcessor::onPlayerTurnStarted(PlayerColor which)
  106. {
  107. const auto * playerState = gameHandler->gameState()->getPlayerState(which);
  108. handleTimeEvents(which);
  109. for (const auto * t : playerState->getTowns())
  110. handleTownEvents(t);
  111. for (const auto * t : playerState->getTowns())
  112. {
  113. //garrison hero first - consistent with original H3 Mana Vortex and Battle Scholar Academy levelup windows order
  114. if (t->garrisonHero != nullptr)
  115. gameHandler->objectVisited(t, t->garrisonHero);
  116. if (t->visitingHero != nullptr)
  117. gameHandler->objectVisited(t, t->visitingHero);
  118. }
  119. }
  120. void NewTurnProcessor::onPlayerTurnEnded(PlayerColor which)
  121. {
  122. const auto * playerState = gameHandler->gameState()->getPlayerState(which);
  123. assert(playerState->status == EPlayerStatus::INGAME);
  124. if (playerState->getTowns().empty())
  125. {
  126. DaysWithoutTown pack;
  127. pack.player = which;
  128. pack.daysWithoutCastle = playerState->daysWithoutCastle.value_or(0) + 1;
  129. gameHandler->sendAndApply(&pack);
  130. }
  131. else
  132. {
  133. if (playerState->daysWithoutCastle.has_value())
  134. {
  135. DaysWithoutTown pack;
  136. pack.player = which;
  137. pack.daysWithoutCastle = std::nullopt;
  138. gameHandler->sendAndApply(&pack);
  139. }
  140. }
  141. // check for 7 days without castle
  142. gameHandler->checkVictoryLossConditionsForPlayer(which);
  143. bool newWeek = gameHandler->getDate(Date::DAY_OF_WEEK) == 7; // end of 7th day
  144. if (newWeek) //new heroes in tavern
  145. gameHandler->heroPool->onNewWeek(which);
  146. }
  147. ResourceSet NewTurnProcessor::generatePlayerIncome(PlayerColor playerID, bool newWeek)
  148. {
  149. const auto & playerSettings = gameHandler->getPlayerSettings(playerID);
  150. const PlayerState & state = gameHandler->gameState()->players.at(playerID);
  151. ResourceSet income;
  152. for (const auto & town : state.getTowns())
  153. {
  154. if (newWeek && town->hasBuilt(BuildingSubID::TREASURY))
  155. {
  156. //give 10% of starting gold
  157. income[EGameResID::GOLD] += state.resources[EGameResID::GOLD] / 10;
  158. }
  159. //give resources if there's a Mystic Pond
  160. if (newWeek && town->hasBuilt(BuildingSubID::MYSTIC_POND))
  161. {
  162. static constexpr std::array rareResources = {
  163. GameResID::MERCURY,
  164. GameResID::SULFUR,
  165. GameResID::CRYSTAL,
  166. GameResID::GEMS
  167. };
  168. auto resID = *RandomGeneratorUtil::nextItem(rareResources, gameHandler->getRandomGenerator());
  169. int resVal = gameHandler->getRandomGenerator().nextInt(1, 4);
  170. income[resID] += resVal;
  171. gameHandler->setObjPropertyValue(town->id, ObjProperty::BONUS_VALUE_FIRST, resID);
  172. gameHandler->setObjPropertyValue(town->id, ObjProperty::BONUS_VALUE_SECOND, resVal);
  173. }
  174. }
  175. for (GameResID k = GameResID::WOOD; k < GameResID::COUNT; k++)
  176. {
  177. income += state.valOfBonuses(BonusType::RESOURCES_CONSTANT_BOOST, BonusSubtypeID(k));
  178. income += state.valOfBonuses(BonusType::RESOURCES_TOWN_MULTIPLYING_BOOST, BonusSubtypeID(k)) * state.getTowns().size();
  179. }
  180. if(newWeek) //weekly crystal generation if 1 or more crystal dragons in any hero army or town garrison
  181. {
  182. bool hasCrystalGenCreature = false;
  183. for (const auto & hero : state.getHeroes())
  184. for(auto stack : hero->stacks)
  185. if(stack.second->hasBonusOfType(BonusType::SPECIAL_CRYSTAL_GENERATION))
  186. hasCrystalGenCreature = true;
  187. for(const auto & town : state.getTowns())
  188. for(auto stack : town->stacks)
  189. if(stack.second->hasBonusOfType(BonusType::SPECIAL_CRYSTAL_GENERATION))
  190. hasCrystalGenCreature = true;
  191. if(hasCrystalGenCreature)
  192. income[EGameResID::CRYSTAL] += 3;
  193. }
  194. TResources incomeHandicapped = income;
  195. incomeHandicapped.applyHandicap(playerSettings->handicap.percentIncome);
  196. for (auto obj : state.getOwnedObjects())
  197. incomeHandicapped += obj->asOwnable()->dailyIncome();
  198. if (!state.isHuman())
  199. {
  200. // Initialize bonuses for different resources
  201. std::array<int, GameResID::COUNT> weeklyBonuses = {};
  202. // Calculate weekly bonuses based on difficulty
  203. if (gameHandler->gameState()->getStartInfo()->difficulty == 0)
  204. {
  205. weeklyBonuses[EGameResID::GOLD] = static_cast<int>(std::round(incomeHandicapped[EGameResID::GOLD] * (0.75 - 1) * 7));
  206. }
  207. else if (gameHandler->gameState()->getStartInfo()->difficulty == 3)
  208. {
  209. weeklyBonuses[EGameResID::GOLD] = static_cast<int>(std::round(incomeHandicapped[EGameResID::GOLD] * 0.25 * 7));
  210. weeklyBonuses[EGameResID::WOOD] = static_cast<int>(std::round(incomeHandicapped[EGameResID::WOOD] * 0.39 * 7));
  211. weeklyBonuses[EGameResID::ORE] = static_cast<int>(std::round(incomeHandicapped[EGameResID::ORE] * 0.39 * 7));
  212. weeklyBonuses[EGameResID::MERCURY] = static_cast<int>(std::round(incomeHandicapped[EGameResID::MERCURY] * 0.14 * 7));
  213. weeklyBonuses[EGameResID::CRYSTAL] = static_cast<int>(std::round(incomeHandicapped[EGameResID::CRYSTAL] * 0.14 * 7));
  214. weeklyBonuses[EGameResID::SULFUR] = static_cast<int>(std::round(incomeHandicapped[EGameResID::SULFUR] * 0.14 * 7));
  215. weeklyBonuses[EGameResID::GEMS] = static_cast<int>(std::round(incomeHandicapped[EGameResID::GEMS] * 0.14 * 7));
  216. }
  217. else if (gameHandler->gameState()->getStartInfo()->difficulty == 4)
  218. {
  219. weeklyBonuses[EGameResID::GOLD] = static_cast<int>(std::round(incomeHandicapped[EGameResID::GOLD] * 0.5 * 7));
  220. weeklyBonuses[EGameResID::WOOD] = static_cast<int>(std::round(incomeHandicapped[EGameResID::WOOD] * 0.53 * 7));
  221. weeklyBonuses[EGameResID::ORE] = static_cast<int>(std::round(incomeHandicapped[EGameResID::ORE] * 0.53 * 7));
  222. weeklyBonuses[EGameResID::MERCURY] = static_cast<int>(std::round(incomeHandicapped[EGameResID::MERCURY] * 0.28 * 7));
  223. weeklyBonuses[EGameResID::CRYSTAL] = static_cast<int>(std::round(incomeHandicapped[EGameResID::CRYSTAL] * 0.28 * 7));
  224. weeklyBonuses[EGameResID::SULFUR] = static_cast<int>(std::round(incomeHandicapped[EGameResID::SULFUR] * 0.28 * 7));
  225. weeklyBonuses[EGameResID::GEMS] = static_cast<int>(std::round(incomeHandicapped[EGameResID::GEMS] * 0.28 * 7));
  226. }
  227. // Distribute weekly bonuses over 7 days, depending on the current day of the week
  228. for (int i = 0; i < GameResID::COUNT; ++i)
  229. {
  230. int dailyBonus = weeklyBonuses[i] / 7;
  231. int remainderBonus = weeklyBonuses[i] % 7;
  232. // Apply the daily bonus for each day, and distribute the remainder accordingly
  233. incomeHandicapped[static_cast<GameResID>(i)] += dailyBonus;
  234. if (gameHandler->gameState()->getDate(Date::DAY_OF_WEEK) - 1 < remainderBonus)
  235. {
  236. incomeHandicapped[static_cast<GameResID>(i)] += 1;
  237. }
  238. }
  239. }
  240. return incomeHandicapped;
  241. }
  242. SetAvailableCreatures NewTurnProcessor::generateTownGrowth(const CGTownInstance * t, EWeekType weekType, CreatureID creatureWeek, bool firstDay)
  243. {
  244. SetAvailableCreatures sac;
  245. PlayerColor player = t->tempOwner;
  246. sac.tid = t->id;
  247. sac.creatures = t->creatures;
  248. for (int k=0; k < t->town->creatures.size(); k++)
  249. {
  250. if (t->creatures.at(k).second.empty())
  251. continue;
  252. uint32_t creaturesBefore = t->creatures.at(k).first;
  253. uint32_t creatureGrowth = 0;
  254. const CCreature *cre = t->creatures.at(k).second.back().toCreature();
  255. if (firstDay)
  256. {
  257. creatureGrowth = cre->getGrowth();
  258. }
  259. else
  260. {
  261. creatureGrowth = t->creatureGrowth(k);
  262. //Deity of fire week - upgrade both imps and upgrades
  263. if (weekType == EWeekType::DEITYOFFIRE && vstd::contains(t->creatures.at(k).second, creatureWeek))
  264. creatureGrowth += 15;
  265. //bonus week, effect applies only to identical creatures
  266. if (weekType == EWeekType::BONUS_GROWTH && cre->getId() == creatureWeek)
  267. creatureGrowth += 5;
  268. }
  269. // Neutral towns have halved creature growth
  270. if (!player.isValidPlayer())
  271. creatureGrowth /= 2;
  272. uint32_t resultingCreatures = 0;
  273. if (weekType == EWeekType::PLAGUE)
  274. resultingCreatures = creaturesBefore / 2;
  275. else if (weekType == EWeekType::DOUBLE_GROWTH)
  276. resultingCreatures = (creaturesBefore + creatureGrowth) * 2;
  277. else
  278. resultingCreatures = creaturesBefore + creatureGrowth;
  279. sac.creatures.at(k).first = resultingCreatures;
  280. }
  281. return sac;
  282. }
  283. RumorState NewTurnProcessor::pickNewRumor()
  284. {
  285. RumorState newRumor;
  286. static const std::vector<RumorState::ERumorType> rumorTypes = {RumorState::TYPE_MAP, RumorState::TYPE_SPECIAL, RumorState::TYPE_RAND, RumorState::TYPE_RAND};
  287. std::vector<RumorState::ERumorTypeSpecial> sRumorTypes = {
  288. RumorState::RUMOR_OBELISKS, RumorState::RUMOR_ARTIFACTS, RumorState::RUMOR_ARMY, RumorState::RUMOR_INCOME};
  289. if(gameHandler->gameState()->map->grailPos.valid()) // Grail should always be on map, but I had related crash I didn't manage to reproduce
  290. sRumorTypes.push_back(RumorState::RUMOR_GRAIL);
  291. int rumorId = -1;
  292. int rumorExtra = -1;
  293. auto & rand = gameHandler->getRandomGenerator();
  294. newRumor.type = *RandomGeneratorUtil::nextItem(rumorTypes, rand);
  295. do
  296. {
  297. switch(newRumor.type)
  298. {
  299. case RumorState::TYPE_SPECIAL:
  300. {
  301. SThievesGuildInfo tgi;
  302. gameHandler->gameState()->obtainPlayersStats(tgi, 20);
  303. rumorId = *RandomGeneratorUtil::nextItem(sRumorTypes, rand);
  304. if(rumorId == RumorState::RUMOR_GRAIL)
  305. {
  306. rumorExtra = gameHandler->gameState()->getTile(gameHandler->gameState()->map->grailPos)->terType->getIndex();
  307. break;
  308. }
  309. std::vector<PlayerColor> players = {};
  310. switch(rumorId)
  311. {
  312. case RumorState::RUMOR_OBELISKS:
  313. players = tgi.obelisks[0];
  314. break;
  315. case RumorState::RUMOR_ARTIFACTS:
  316. players = tgi.artifacts[0];
  317. break;
  318. case RumorState::RUMOR_ARMY:
  319. players = tgi.army[0];
  320. break;
  321. case RumorState::RUMOR_INCOME:
  322. players = tgi.income[0];
  323. break;
  324. }
  325. rumorExtra = RandomGeneratorUtil::nextItem(players, rand)->getNum();
  326. break;
  327. }
  328. case RumorState::TYPE_MAP:
  329. // Makes sure that map rumors only used if there enough rumors too choose from
  330. if(!gameHandler->gameState()->map->rumors.empty() && (gameHandler->gameState()->map->rumors.size() > 1 || !gameHandler->gameState()->currentRumor.last.count(RumorState::TYPE_MAP)))
  331. {
  332. rumorId = rand.nextInt(gameHandler->gameState()->map->rumors.size() - 1);
  333. break;
  334. }
  335. else
  336. newRumor.type = RumorState::TYPE_RAND;
  337. [[fallthrough]];
  338. case RumorState::TYPE_RAND:
  339. auto vector = VLC->generaltexth->findStringsWithPrefix("core.randtvrn");
  340. rumorId = rand.nextInt((int)vector.size() - 1);
  341. break;
  342. }
  343. }
  344. while(!newRumor.update(rumorId, rumorExtra));
  345. return newRumor;
  346. }
  347. std::tuple<EWeekType, CreatureID> NewTurnProcessor::pickWeekType(bool newMonth)
  348. {
  349. for (const CGTownInstance *t : gameHandler->gameState()->map->towns)
  350. {
  351. if (t->hasBuilt(BuildingID::GRAIL, ETownType::INFERNO))
  352. return { EWeekType::DEITYOFFIRE, CreatureID::IMP };
  353. }
  354. if(!VLC->settings()->getBoolean(EGameSettings::CREATURES_ALLOW_RANDOM_SPECIAL_WEEKS))
  355. return { EWeekType::NORMAL, CreatureID::NONE};
  356. int monthType = gameHandler->getRandomGenerator().nextInt(99);
  357. if (newMonth) //new month
  358. {
  359. if (monthType < 40) //double growth
  360. {
  361. if (VLC->settings()->getBoolean(EGameSettings::CREATURES_ALLOW_ALL_FOR_DOUBLE_MONTH))
  362. {
  363. CreatureID creatureID = VLC->creh->pickRandomMonster(gameHandler->getRandomGenerator());
  364. return { EWeekType::DOUBLE_GROWTH, creatureID};
  365. }
  366. else if (VLC->creh->doubledCreatures.size())
  367. {
  368. CreatureID creatureID = *RandomGeneratorUtil::nextItem(VLC->creh->doubledCreatures, gameHandler->getRandomGenerator());
  369. return { EWeekType::DOUBLE_GROWTH, creatureID};
  370. }
  371. else
  372. {
  373. gameHandler->complain("Cannot find creature that can be spawned!");
  374. return { EWeekType::NORMAL, CreatureID::NONE};
  375. }
  376. }
  377. if (monthType < 50)
  378. return { EWeekType::PLAGUE, CreatureID::NONE};
  379. return { EWeekType::NORMAL, CreatureID::NONE};
  380. }
  381. else //it's a week, but not full month
  382. {
  383. if (monthType < 25)
  384. {
  385. std::pair<int, CreatureID> newMonster(54, CreatureID());
  386. do
  387. {
  388. newMonster.second = VLC->creh->pickRandomMonster(gameHandler->getRandomGenerator());
  389. } while (VLC->creh->objects[newMonster.second] &&
  390. (*VLC->townh)[VLC->creatures()->getById(newMonster.second)->getFaction()]->town == nullptr); // find first non neutral creature
  391. return { EWeekType::BONUS_GROWTH, newMonster.second};
  392. }
  393. return { EWeekType::NORMAL, CreatureID::NONE};
  394. }
  395. }
  396. std::vector<SetMana> NewTurnProcessor::updateHeroesManaPoints()
  397. {
  398. std::vector<SetMana> result;
  399. for (auto & elem : gameHandler->gameState()->players)
  400. {
  401. for (CGHeroInstance *h : elem.second.getHeroes())
  402. {
  403. int32_t newMana = h->getManaNewTurn();
  404. if (newMana != h->mana)
  405. result.emplace_back(h->id, newMana, true);
  406. }
  407. }
  408. return result;
  409. }
  410. std::vector<SetMovePoints> NewTurnProcessor::updateHeroesMovementPoints()
  411. {
  412. std::vector<SetMovePoints> result;
  413. for (auto & elem : gameHandler->gameState()->players)
  414. {
  415. for (CGHeroInstance *h : elem.second.getHeroes())
  416. {
  417. auto ti = std::make_unique<TurnInfo>(h, 1);
  418. // NOTE: this code executed when bonuses of previous day not yet updated (this happen in NewTurn::applyGs). See issue 2356
  419. int32_t newMovementPoints = h->movementPointsLimitCached(gameHandler->gameState()->map->getTile(h->visitablePos()).terType->isLand(), ti.get());
  420. if (newMovementPoints != h->movementPointsRemaining())
  421. result.emplace_back(h->id, newMovementPoints, true);
  422. }
  423. }
  424. return result;
  425. }
  426. InfoWindow NewTurnProcessor::createInfoWindow(EWeekType weekType, CreatureID creatureWeek, bool newMonth)
  427. {
  428. InfoWindow iw;
  429. switch (weekType)
  430. {
  431. case EWeekType::DOUBLE_GROWTH:
  432. iw.text.appendLocalString(EMetaText::ARRAY_TXT, 131);
  433. iw.text.replaceNameSingular(creatureWeek);
  434. iw.text.replaceNameSingular(creatureWeek);
  435. break;
  436. case EWeekType::PLAGUE:
  437. iw.text.appendLocalString(EMetaText::ARRAY_TXT, 132);
  438. break;
  439. case EWeekType::BONUS_GROWTH:
  440. iw.text.appendLocalString(EMetaText::ARRAY_TXT, 134);
  441. iw.text.replaceNameSingular(creatureWeek);
  442. iw.text.replaceNameSingular(creatureWeek);
  443. break;
  444. case EWeekType::DEITYOFFIRE:
  445. iw.text.appendLocalString(EMetaText::ARRAY_TXT, 135);
  446. iw.text.replaceNameSingular(CreatureID::IMP); //%s imp
  447. iw.text.replaceNameSingular(CreatureID::IMP); //%s imp
  448. iw.text.replacePositiveNumber(15);//%+d 15
  449. iw.text.replaceNameSingular(CreatureID::FAMILIAR); //%s familiar
  450. iw.text.replacePositiveNumber(15);//%+d 15
  451. break;
  452. default:
  453. if (newMonth)
  454. {
  455. iw.text.appendLocalString(EMetaText::ARRAY_TXT, (130));
  456. iw.text.replaceLocalString(EMetaText::ARRAY_TXT, gameHandler->getRandomGenerator().nextInt(32, 41));
  457. }
  458. else
  459. {
  460. iw.text.appendLocalString(EMetaText::ARRAY_TXT, (133));
  461. iw.text.replaceLocalString(EMetaText::ARRAY_TXT, gameHandler->getRandomGenerator().nextInt(43, 57));
  462. }
  463. }
  464. return iw;
  465. }
  466. NewTurn NewTurnProcessor::generateNewTurnPack()
  467. {
  468. NewTurn n;
  469. n.specialWeek = EWeekType::FIRST_WEEK;
  470. n.creatureid = CreatureID::NONE;
  471. n.day = gameHandler->gameState()->day + 1;
  472. bool firstTurn = !gameHandler->getDate(Date::DAY);
  473. bool newWeek = gameHandler->getDate(Date::DAY_OF_WEEK) == 7; //day numbers are confusing, as day was not yet switched
  474. bool newMonth = gameHandler->getDate(Date::DAY_OF_MONTH) == 28;
  475. if (!firstTurn)
  476. {
  477. for (const auto & player : gameHandler->gameState()->players)
  478. n.playerIncome[player.first] = generatePlayerIncome(player.first, newWeek);
  479. }
  480. if (newWeek && !firstTurn)
  481. {
  482. auto [specialWeek, creatureID] = pickWeekType(newMonth);
  483. n.specialWeek = specialWeek;
  484. n.creatureid = creatureID;
  485. }
  486. n.heroesMana = updateHeroesManaPoints();
  487. n.heroesMovement = updateHeroesMovementPoints();
  488. if (newWeek)
  489. {
  490. for (CGTownInstance *t : gameHandler->gameState()->map->towns)
  491. n.availableCreatures.push_back(generateTownGrowth(t, n.specialWeek, n.creatureid, firstTurn));
  492. }
  493. if (newWeek)
  494. n.newRumor = pickNewRumor();
  495. if (newWeek)
  496. {
  497. //new week info popup
  498. if (n.specialWeek != EWeekType::FIRST_WEEK)
  499. n.newWeekNotification = createInfoWindow(n.specialWeek, n.creatureid, newMonth);
  500. }
  501. return n;
  502. }
  503. void NewTurnProcessor::onNewTurn()
  504. {
  505. NewTurn n = generateNewTurnPack();
  506. bool newWeek = gameHandler->getDate(Date::DAY_OF_WEEK) == 7; //day numbers are confusing, as day was not yet switched
  507. bool newMonth = gameHandler->getDate(Date::DAY_OF_MONTH) == 28;
  508. gameHandler->sendAndApply(&n);
  509. if (newWeek)
  510. {
  511. for (CGTownInstance *t : gameHandler->gameState()->map->towns)
  512. if (t->hasBuilt(BuildingSubID::PORTAL_OF_SUMMONING))
  513. gameHandler->setPortalDwelling(t, true, (n.specialWeek == EWeekType::PLAGUE ? true : false)); //set creatures for Portal of Summoning
  514. }
  515. //spawn wandering monsters
  516. if (newMonth && (n.specialWeek == EWeekType::DOUBLE_GROWTH || n.specialWeek == EWeekType::DEITYOFFIRE))
  517. {
  518. gameHandler->spawnWanderingMonsters(n.creatureid);
  519. }
  520. logGlobal->trace("Info about turn %d has been sent!", n.day);
  521. }