CGTownBuilding.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /*
  2. * CGTownBuilding.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 "CGTownBuilding.h"
  12. #include "CGTownInstance.h"
  13. #include "../CGeneralTextHandler.h"
  14. #include "../IGameCallback.h"
  15. #include "../gameState/CGameState.h"
  16. #include "../mapObjects/CGHeroInstance.h"
  17. #include "../networkPacks/PacksForClient.h"
  18. VCMI_LIB_NAMESPACE_BEGIN
  19. PlayerColor CGTownBuilding::getOwner() const
  20. {
  21. return town->getOwner();
  22. }
  23. MapObjectID CGTownBuilding::getObjGroupIndex() const
  24. {
  25. return -1;
  26. }
  27. MapObjectSubID CGTownBuilding::getObjTypeIndex() const
  28. {
  29. return 0;
  30. }
  31. int3 CGTownBuilding::visitablePos() const
  32. {
  33. return town->visitablePos();
  34. }
  35. int3 CGTownBuilding::getPosition() const
  36. {
  37. return town->getPosition();
  38. }
  39. std::string CGTownBuilding::getVisitingBonusGreeting() const
  40. {
  41. auto bonusGreeting = town->getTown()->getGreeting(bType);
  42. if(!bonusGreeting.empty())
  43. return bonusGreeting;
  44. switch(bType)
  45. {
  46. case BuildingSubID::MANA_VORTEX:
  47. bonusGreeting = std::string(VLC->generaltexth->translate("vcmi.townHall.greetingManaVortex"));
  48. break;
  49. case BuildingSubID::KNOWLEDGE_VISITING_BONUS:
  50. bonusGreeting = std::string(VLC->generaltexth->translate("vcmi.townHall.greetingKnowledge"));
  51. break;
  52. case BuildingSubID::SPELL_POWER_VISITING_BONUS:
  53. bonusGreeting = std::string(VLC->generaltexth->translate("vcmi.townHall.greetingSpellPower"));
  54. break;
  55. case BuildingSubID::ATTACK_VISITING_BONUS:
  56. bonusGreeting = std::string(VLC->generaltexth->translate("vcmi.townHall.greetingAttack"));
  57. break;
  58. case BuildingSubID::EXPERIENCE_VISITING_BONUS:
  59. bonusGreeting = std::string(VLC->generaltexth->translate("vcmi.townHall.greetingExperience"));
  60. break;
  61. case BuildingSubID::DEFENSE_VISITING_BONUS:
  62. bonusGreeting = std::string(VLC->generaltexth->translate("vcmi.townHall.greetingDefence"));
  63. break;
  64. }
  65. auto buildingName = town->getTown()->getSpecialBuilding(bType)->getNameTranslated();
  66. if(bonusGreeting.empty())
  67. {
  68. bonusGreeting = "Error: Bonus greeting for '%s' is not localized.";
  69. logGlobal->error("'%s' building of '%s' faction has not localized bonus greeting.", buildingName, town->getTown()->faction->getNameTranslated());
  70. }
  71. boost::algorithm::replace_first(bonusGreeting, "%s", buildingName);
  72. town->getTown()->setGreeting(bType, bonusGreeting);
  73. return bonusGreeting;
  74. }
  75. std::string CGTownBuilding::getCustomBonusGreeting(const Bonus & bonus) const
  76. {
  77. if(bonus.type == BonusType::TOWN_MAGIC_WELL)
  78. {
  79. auto bonusGreeting = std::string(VLC->generaltexth->translate("vcmi.townHall.greetingInTownMagicWell"));
  80. auto buildingName = town->getTown()->getSpecialBuilding(bType)->getNameTranslated();
  81. boost::algorithm::replace_first(bonusGreeting, "%s", buildingName);
  82. return bonusGreeting;
  83. }
  84. auto bonusGreeting = std::string(VLC->generaltexth->translate("vcmi.townHall.greetingCustomBonus")); //"%s gives you +%d %s%s"
  85. std::string param;
  86. std::string until;
  87. if(bonus.type == BonusType::MORALE)
  88. param = VLC->generaltexth->allTexts[384];
  89. else if(bonus.type == BonusType::LUCK)
  90. param = VLC->generaltexth->allTexts[385];
  91. until = bonus.duration == BonusDuration::ONE_BATTLE
  92. ? VLC->generaltexth->translate("vcmi.townHall.greetingCustomUntil")
  93. : ".";
  94. boost::format fmt = boost::format(bonusGreeting) % bonus.description % bonus.val % param % until;
  95. std::string greeting = fmt.str();
  96. return greeting;
  97. }
  98. COPWBonus::COPWBonus(const BuildingID & bid, BuildingSubID::EBuildingSubID subId, CGTownInstance * cgTown)
  99. {
  100. bID = bid;
  101. bType = subId;
  102. town = cgTown;
  103. indexOnTV = static_cast<si32>(town->bonusingBuildings.size());
  104. }
  105. void COPWBonus::setProperty(ObjProperty what, ObjPropertyID identifier)
  106. {
  107. switch (what)
  108. {
  109. case ObjProperty::VISITORS:
  110. visitors.insert(identifier.as<ObjectInstanceID>());
  111. break;
  112. case ObjProperty::STRUCTURE_CLEAR_VISITORS:
  113. visitors.clear();
  114. break;
  115. }
  116. }
  117. void COPWBonus::onHeroVisit (const CGHeroInstance * h) const
  118. {
  119. ObjectInstanceID heroID = h->id;
  120. if(town->hasBuilt(bID))
  121. {
  122. InfoWindow iw;
  123. iw.player = h->tempOwner;
  124. switch (this->bType)
  125. {
  126. case BuildingSubID::STABLES:
  127. if(!h->hasBonusFrom(BonusSource::OBJECT_TYPE, BonusSourceID(Obj(Obj::STABLES)))) //does not stack with advMap Stables
  128. {
  129. GiveBonus gb;
  130. gb.bonus = Bonus(BonusDuration::ONE_WEEK, BonusType::MOVEMENT, BonusSource::OBJECT_TYPE, 600, BonusSourceID(Obj(Obj::STABLES)), BonusCustomSubtype::heroMovementLand, VLC->generaltexth->arraytxt[100]);
  131. gb.id = heroID;
  132. cb->giveHeroBonus(&gb);
  133. cb->setMovePoints(heroID, 600, false);
  134. iw.text.appendRawString(VLC->generaltexth->allTexts[580]);
  135. cb->showInfoDialog(&iw);
  136. }
  137. break;
  138. case BuildingSubID::MANA_VORTEX:
  139. if(visitors.empty())
  140. {
  141. if(h->mana < h->manaLimit() * 2)
  142. cb->setManaPoints (heroID, 2 * h->manaLimit());
  143. //TODO: investigate line below
  144. //cb->setObjProperty (town->id, ObjProperty::VISITED, true);
  145. iw.text.appendRawString(getVisitingBonusGreeting());
  146. cb->showInfoDialog(&iw);
  147. //extra visit penalty if hero alredy had double mana points (or even more?!)
  148. town->addHeroToStructureVisitors(h, indexOnTV);
  149. }
  150. break;
  151. }
  152. }
  153. }
  154. CTownBonus::CTownBonus(const BuildingID & index, BuildingSubID::EBuildingSubID subId, CGTownInstance * cgTown)
  155. {
  156. bID = index;
  157. bType = subId;
  158. town = cgTown;
  159. indexOnTV = static_cast<si32>(town->bonusingBuildings.size());
  160. }
  161. void CTownBonus::setProperty(ObjProperty what, ObjPropertyID identifier)
  162. {
  163. if(what == ObjProperty::VISITORS)
  164. visitors.insert(identifier.as<ObjectInstanceID>());
  165. }
  166. void CTownBonus::onHeroVisit (const CGHeroInstance * h) const
  167. {
  168. ObjectInstanceID heroID = h->id;
  169. if(town->hasBuilt(bID) && visitors.find(heroID) == visitors.end())
  170. {
  171. si64 val = 0;
  172. InfoWindow iw;
  173. PrimarySkill what = PrimarySkill::NONE;
  174. switch(bType)
  175. {
  176. case BuildingSubID::KNOWLEDGE_VISITING_BONUS: //wall of knowledge
  177. what = PrimarySkill::KNOWLEDGE;
  178. val = 1;
  179. iw.components.emplace_back(ComponentType::PRIM_SKILL, PrimarySkill::KNOWLEDGE, 1);
  180. break;
  181. case BuildingSubID::SPELL_POWER_VISITING_BONUS: //order of fire
  182. what = PrimarySkill::SPELL_POWER;
  183. val = 1;
  184. iw.components.emplace_back(ComponentType::PRIM_SKILL, PrimarySkill::SPELL_POWER, 1);
  185. break;
  186. case BuildingSubID::ATTACK_VISITING_BONUS: //hall of Valhalla
  187. what = PrimarySkill::ATTACK;
  188. val = 1;
  189. iw.components.emplace_back(ComponentType::PRIM_SKILL, PrimarySkill::ATTACK, 1);
  190. break;
  191. case BuildingSubID::EXPERIENCE_VISITING_BONUS: //academy of battle scholars
  192. what = PrimarySkill::EXPERIENCE;
  193. val = static_cast<int>(h->calculateXp(1000));
  194. iw.components.emplace_back(ComponentType::EXPERIENCE, val);
  195. break;
  196. case BuildingSubID::DEFENSE_VISITING_BONUS: //cage of warlords
  197. what = PrimarySkill::DEFENSE;
  198. val = 1;
  199. iw.components.emplace_back(ComponentType::PRIM_SKILL, PrimarySkill::DEFENSE, 1);
  200. break;
  201. case BuildingSubID::CUSTOM_VISITING_BONUS:
  202. const auto building = town->getTown()->buildings.at(bID);
  203. if(!h->hasBonusFrom(BonusSource::TOWN_STRUCTURE, BonusSourceID(building->getUniqueTypeID())))
  204. {
  205. const auto & bonuses = building->onVisitBonuses;
  206. applyBonuses(const_cast<CGHeroInstance *>(h), bonuses);
  207. }
  208. break;
  209. }
  210. if(what != PrimarySkill::NONE)
  211. {
  212. iw.player = cb->getOwner(heroID);
  213. iw.text.appendRawString(getVisitingBonusGreeting());
  214. cb->showInfoDialog(&iw);
  215. if (what == PrimarySkill::EXPERIENCE)
  216. cb->giveExperience(cb->getHero(heroID), val);
  217. else
  218. cb->changePrimSkill(cb->getHero(heroID), what, val);
  219. town->addHeroToStructureVisitors(h, indexOnTV);
  220. }
  221. }
  222. }
  223. void CTownBonus::applyBonuses(CGHeroInstance * h, const BonusList & bonuses) const
  224. {
  225. auto addToVisitors = false;
  226. for(const auto & bonus : bonuses)
  227. {
  228. GiveBonus gb;
  229. InfoWindow iw;
  230. if(bonus->type == BonusType::TOWN_MAGIC_WELL)
  231. {
  232. if(h->mana >= h->manaLimit())
  233. return;
  234. cb->setManaPoints(h->id, h->manaLimit());
  235. bonus->duration = BonusDuration::ONE_DAY;
  236. }
  237. gb.bonus = * bonus;
  238. gb.id = h->id;
  239. cb->giveHeroBonus(&gb);
  240. if(bonus->duration == BonusDuration::PERMANENT)
  241. addToVisitors = true;
  242. iw.player = cb->getOwner(h->id);
  243. iw.text.appendRawString(getCustomBonusGreeting(gb.bonus));
  244. cb->showInfoDialog(&iw);
  245. }
  246. if(addToVisitors)
  247. town->addHeroToStructureVisitors(h, indexOnTV);
  248. }
  249. CTownRewardableBuilding::CTownRewardableBuilding(const BuildingID & index, BuildingSubID::EBuildingSubID subId, CGTownInstance * cgTown, CRandomGenerator & rand)
  250. {
  251. bID = index;
  252. bType = subId;
  253. town = cgTown;
  254. indexOnTV = static_cast<si32>(town->bonusingBuildings.size());
  255. initObj(rand);
  256. }
  257. void CTownRewardableBuilding::initObj(CRandomGenerator & rand)
  258. {
  259. assert(town && town->town);
  260. auto building = town->town->buildings.at(bID);
  261. building->rewardableObjectInfo.configureObject(configuration, rand);
  262. for(auto & rewardInfo : configuration.info)
  263. {
  264. for (auto & bonus : rewardInfo.reward.bonuses)
  265. {
  266. bonus.source = BonusSource::TOWN_STRUCTURE;
  267. bonus.sid = BonusSourceID(building->getUniqueTypeID());
  268. }
  269. }
  270. }
  271. void CTownRewardableBuilding::newTurn(CRandomGenerator & rand) const
  272. {
  273. if (configuration.resetParameters.period != 0 && cb->getDate(Date::DAY) > 1 && ((cb->getDate(Date::DAY)-1) % configuration.resetParameters.period) == 0)
  274. {
  275. if(configuration.resetParameters.rewards)
  276. {
  277. cb->setObjPropertyValue(town->id, ObjProperty::REWARD_RANDOMIZE, indexOnTV);
  278. }
  279. if(configuration.resetParameters.visitors)
  280. {
  281. cb->setObjPropertyValue(town->id, ObjProperty::STRUCTURE_CLEAR_VISITORS, indexOnTV);
  282. }
  283. }
  284. }
  285. void CTownRewardableBuilding::setProperty(ObjProperty what, ObjPropertyID identifier)
  286. {
  287. switch (what)
  288. {
  289. case ObjProperty::VISITORS:
  290. visitors.insert(identifier.as<ObjectInstanceID>());
  291. break;
  292. case ObjProperty::STRUCTURE_CLEAR_VISITORS:
  293. visitors.clear();
  294. break;
  295. case ObjProperty::REWARD_RANDOMIZE:
  296. initObj(cb->gameState()->getRandomGenerator());
  297. break;
  298. case ObjProperty::REWARD_SELECT:
  299. selectedReward = identifier.getNum();
  300. break;
  301. }
  302. }
  303. void CTownRewardableBuilding::heroLevelUpDone(const CGHeroInstance *hero) const
  304. {
  305. grantRewardAfterLevelup(cb, configuration.info.at(selectedReward), town, hero);
  306. }
  307. void CTownRewardableBuilding::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  308. {
  309. if(answer == 0)
  310. return; // player refused
  311. if(visitors.find(hero->id) != visitors.end())
  312. return; // query not for this building
  313. if(answer > 0 && answer-1 < configuration.info.size())
  314. {
  315. auto list = getAvailableRewards(hero, Rewardable::EEventType::EVENT_FIRST_VISIT);
  316. grantReward(list[answer - 1], hero);
  317. }
  318. else
  319. {
  320. throw std::runtime_error("Unhandled choice");
  321. }
  322. }
  323. void CTownRewardableBuilding::grantReward(ui32 rewardID, const CGHeroInstance * hero) const
  324. {
  325. town->addHeroToStructureVisitors(hero, indexOnTV);
  326. grantRewardBeforeLevelup(cb, configuration.info.at(rewardID), hero);
  327. // hero is not blocked by levelup dialog - grant remainer immediately
  328. if(!cb->isVisitCoveredByAnotherQuery(town, hero))
  329. {
  330. grantRewardAfterLevelup(cb, configuration.info.at(rewardID), town, hero);
  331. }
  332. }
  333. bool CTownRewardableBuilding::wasVisitedBefore(const CGHeroInstance * contextHero) const
  334. {
  335. switch (configuration.visitMode)
  336. {
  337. case Rewardable::VISIT_UNLIMITED:
  338. return false;
  339. case Rewardable::VISIT_ONCE:
  340. return !visitors.empty();
  341. case Rewardable::VISIT_PLAYER:
  342. return false; //not supported
  343. case Rewardable::VISIT_BONUS:
  344. {
  345. const auto building = town->getTown()->buildings.at(bID);
  346. return contextHero->hasBonusFrom(BonusSource::TOWN_STRUCTURE, BonusSourceID(building->getUniqueTypeID()));
  347. }
  348. case Rewardable::VISIT_HERO:
  349. return visitors.find(contextHero->id) != visitors.end();
  350. case Rewardable::VISIT_LIMITER:
  351. return configuration.visitLimiter.heroAllowed(contextHero);
  352. default:
  353. return false;
  354. }
  355. }
  356. void CTownRewardableBuilding::onHeroVisit(const CGHeroInstance *h) const
  357. {
  358. auto grantRewardWithMessage = [&](int index) -> void
  359. {
  360. auto vi = configuration.info.at(index);
  361. logGlobal->debug("Granting reward %d. Message says: %s", index, vi.message.toString());
  362. town->addHeroToStructureVisitors(h, indexOnTV); //adding to visitors
  363. InfoWindow iw;
  364. iw.player = h->tempOwner;
  365. iw.text = vi.message;
  366. vi.reward.loadComponents(iw.components, h);
  367. iw.type = EInfoWindowMode::MODAL;
  368. if(!iw.components.empty() || !iw.text.toString().empty())
  369. cb->showInfoDialog(&iw);
  370. grantReward(index, h);
  371. };
  372. auto selectRewardsMessage = [&](const std::vector<ui32> & rewards, const MetaString & dialog) -> void
  373. {
  374. BlockingDialog sd(configuration.canRefuse, rewards.size() > 1);
  375. sd.player = h->tempOwner;
  376. sd.text = dialog;
  377. if (rewards.size() > 1)
  378. for (auto index : rewards)
  379. sd.components.push_back(configuration.info.at(index).reward.getDisplayedComponent(h));
  380. if (rewards.size() == 1)
  381. configuration.info.at(rewards.front()).reward.loadComponents(sd.components, h);
  382. cb->showBlockingDialog(&sd);
  383. };
  384. if(!town->hasBuilt(bID) || cb->isVisitCoveredByAnotherQuery(town, h))
  385. return;
  386. if(!wasVisitedBefore(h))
  387. {
  388. auto rewards = getAvailableRewards(h, Rewardable::EEventType::EVENT_FIRST_VISIT);
  389. logGlobal->debug("Visiting object with %d possible rewards", rewards.size());
  390. switch (rewards.size())
  391. {
  392. case 0: // no available rewards, e.g. visiting School of War without gold
  393. {
  394. auto emptyRewards = getAvailableRewards(h, Rewardable::EEventType::EVENT_NOT_AVAILABLE);
  395. if (!emptyRewards.empty())
  396. grantRewardWithMessage(emptyRewards[0]);
  397. else
  398. logMod->warn("No applicable message for visiting empty object!");
  399. break;
  400. }
  401. case 1: // one reward. Just give it with message
  402. {
  403. if (configuration.canRefuse)
  404. selectRewardsMessage(rewards, configuration.info.at(rewards.front()).message);
  405. else
  406. grantRewardWithMessage(rewards.front());
  407. break;
  408. }
  409. default: // multiple rewards. Act according to select mode
  410. {
  411. switch (configuration.selectMode) {
  412. case Rewardable::SELECT_PLAYER: // player must select
  413. selectRewardsMessage(rewards, configuration.onSelect);
  414. break;
  415. case Rewardable::SELECT_FIRST: // give first available
  416. grantRewardWithMessage(rewards.front());
  417. break;
  418. case Rewardable::SELECT_RANDOM: // give random
  419. grantRewardWithMessage(*RandomGeneratorUtil::nextItem(rewards, cb->gameState()->getRandomGenerator()));
  420. break;
  421. }
  422. break;
  423. }
  424. }
  425. }
  426. else
  427. {
  428. logGlobal->debug("Revisiting already visited object");
  429. auto visitedRewards = getAvailableRewards(h, Rewardable::EEventType::EVENT_ALREADY_VISITED);
  430. if (!visitedRewards.empty())
  431. grantRewardWithMessage(visitedRewards[0]);
  432. else
  433. logMod->debug("No applicable message for visiting already visited object!");
  434. }
  435. }
  436. VCMI_LIB_NAMESPACE_END