CGTownBuilding.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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. SetMovePoints mp;
  134. mp.val = 600;
  135. mp.absolute = false;
  136. mp.hid = heroID;
  137. cb->setMovePoints(&mp);
  138. iw.text.appendRawString(VLC->generaltexth->allTexts[580]);
  139. cb->showInfoDialog(&iw);
  140. }
  141. break;
  142. case BuildingSubID::MANA_VORTEX:
  143. if(visitors.empty())
  144. {
  145. if(h->mana < h->manaLimit() * 2)
  146. cb->setManaPoints (heroID, 2 * h->manaLimit());
  147. //TODO: investigate line below
  148. //cb->setObjProperty (town->id, ObjProperty::VISITED, true);
  149. iw.text.appendRawString(getVisitingBonusGreeting());
  150. cb->showInfoDialog(&iw);
  151. //extra visit penalty if hero alredy had double mana points (or even more?!)
  152. town->addHeroToStructureVisitors(h, indexOnTV);
  153. }
  154. break;
  155. }
  156. }
  157. }
  158. CTownBonus::CTownBonus(const BuildingID & index, BuildingSubID::EBuildingSubID subId, CGTownInstance * cgTown)
  159. {
  160. bID = index;
  161. bType = subId;
  162. town = cgTown;
  163. indexOnTV = static_cast<si32>(town->bonusingBuildings.size());
  164. }
  165. void CTownBonus::setProperty(ObjProperty what, ObjPropertyID identifier)
  166. {
  167. if(what == ObjProperty::VISITORS)
  168. visitors.insert(identifier.as<ObjectInstanceID>());
  169. }
  170. void CTownBonus::onHeroVisit (const CGHeroInstance * h) const
  171. {
  172. ObjectInstanceID heroID = h->id;
  173. if(town->hasBuilt(bID) && visitors.find(heroID) == visitors.end())
  174. {
  175. si64 val = 0;
  176. InfoWindow iw;
  177. PrimarySkill what = PrimarySkill::NONE;
  178. switch(bType)
  179. {
  180. case BuildingSubID::KNOWLEDGE_VISITING_BONUS: //wall of knowledge
  181. what = PrimarySkill::KNOWLEDGE;
  182. val = 1;
  183. iw.components.emplace_back(ComponentType::PRIM_SKILL, PrimarySkill::KNOWLEDGE, 1);
  184. break;
  185. case BuildingSubID::SPELL_POWER_VISITING_BONUS: //order of fire
  186. what = PrimarySkill::SPELL_POWER;
  187. val = 1;
  188. iw.components.emplace_back(ComponentType::PRIM_SKILL, PrimarySkill::SPELL_POWER, 1);
  189. break;
  190. case BuildingSubID::ATTACK_VISITING_BONUS: //hall of Valhalla
  191. what = PrimarySkill::ATTACK;
  192. val = 1;
  193. iw.components.emplace_back(ComponentType::PRIM_SKILL, PrimarySkill::ATTACK, 1);
  194. break;
  195. case BuildingSubID::EXPERIENCE_VISITING_BONUS: //academy of battle scholars
  196. what = PrimarySkill::EXPERIENCE;
  197. val = static_cast<int>(h->calculateXp(1000));
  198. iw.components.emplace_back(ComponentType::EXPERIENCE, val);
  199. break;
  200. case BuildingSubID::DEFENSE_VISITING_BONUS: //cage of warlords
  201. what = PrimarySkill::DEFENSE;
  202. val = 1;
  203. iw.components.emplace_back(ComponentType::PRIM_SKILL, PrimarySkill::DEFENSE, 1);
  204. break;
  205. case BuildingSubID::CUSTOM_VISITING_BONUS:
  206. const auto building = town->getTown()->buildings.at(bID);
  207. if(!h->hasBonusFrom(BonusSource::TOWN_STRUCTURE, BonusSourceID(building->getUniqueTypeID())))
  208. {
  209. const auto & bonuses = building->onVisitBonuses;
  210. applyBonuses(const_cast<CGHeroInstance *>(h), bonuses);
  211. }
  212. break;
  213. }
  214. if(what != PrimarySkill::NONE)
  215. {
  216. iw.player = cb->getOwner(heroID);
  217. iw.text.appendRawString(getVisitingBonusGreeting());
  218. cb->showInfoDialog(&iw);
  219. if (what == PrimarySkill::EXPERIENCE)
  220. cb->giveExperience(cb->getHero(heroID), val);
  221. else
  222. cb->changePrimSkill(cb->getHero(heroID), what, val);
  223. town->addHeroToStructureVisitors(h, indexOnTV);
  224. }
  225. }
  226. }
  227. void CTownBonus::applyBonuses(CGHeroInstance * h, const BonusList & bonuses) const
  228. {
  229. auto addToVisitors = false;
  230. for(const auto & bonus : bonuses)
  231. {
  232. GiveBonus gb;
  233. InfoWindow iw;
  234. if(bonus->type == BonusType::TOWN_MAGIC_WELL)
  235. {
  236. if(h->mana >= h->manaLimit())
  237. return;
  238. cb->setManaPoints(h->id, h->manaLimit());
  239. bonus->duration = BonusDuration::ONE_DAY;
  240. }
  241. gb.bonus = * bonus;
  242. gb.id = h->id;
  243. cb->giveHeroBonus(&gb);
  244. if(bonus->duration == BonusDuration::PERMANENT)
  245. addToVisitors = true;
  246. iw.player = cb->getOwner(h->id);
  247. iw.text.appendRawString(getCustomBonusGreeting(gb.bonus));
  248. cb->showInfoDialog(&iw);
  249. }
  250. if(addToVisitors)
  251. town->addHeroToStructureVisitors(h, indexOnTV);
  252. }
  253. CTownRewardableBuilding::CTownRewardableBuilding(const BuildingID & index, BuildingSubID::EBuildingSubID subId, CGTownInstance * cgTown, CRandomGenerator & rand)
  254. {
  255. bID = index;
  256. bType = subId;
  257. town = cgTown;
  258. indexOnTV = static_cast<si32>(town->bonusingBuildings.size());
  259. initObj(rand);
  260. }
  261. void CTownRewardableBuilding::initObj(CRandomGenerator & rand)
  262. {
  263. assert(town && town->town);
  264. auto building = town->town->buildings.at(bID);
  265. building->rewardableObjectInfo.configureObject(configuration, rand);
  266. for(auto & rewardInfo : configuration.info)
  267. {
  268. for (auto & bonus : rewardInfo.reward.bonuses)
  269. {
  270. bonus.source = BonusSource::TOWN_STRUCTURE;
  271. bonus.sid = BonusSourceID(building->getUniqueTypeID());
  272. }
  273. }
  274. }
  275. void CTownRewardableBuilding::newTurn(CRandomGenerator & rand) const
  276. {
  277. if (configuration.resetParameters.period != 0 && cb->getDate(Date::DAY) > 1 && ((cb->getDate(Date::DAY)-1) % configuration.resetParameters.period) == 0)
  278. {
  279. if(configuration.resetParameters.rewards)
  280. {
  281. cb->setObjPropertyValue(town->id, ObjProperty::REWARD_RANDOMIZE, indexOnTV);
  282. }
  283. if(configuration.resetParameters.visitors)
  284. {
  285. cb->setObjPropertyValue(town->id, ObjProperty::STRUCTURE_CLEAR_VISITORS, indexOnTV);
  286. }
  287. }
  288. }
  289. void CTownRewardableBuilding::setProperty(ObjProperty what, ObjPropertyID identifier)
  290. {
  291. switch (what)
  292. {
  293. case ObjProperty::VISITORS:
  294. visitors.insert(identifier.as<ObjectInstanceID>());
  295. break;
  296. case ObjProperty::STRUCTURE_CLEAR_VISITORS:
  297. visitors.clear();
  298. break;
  299. case ObjProperty::REWARD_RANDOMIZE:
  300. initObj(cb->gameState()->getRandomGenerator());
  301. break;
  302. case ObjProperty::REWARD_SELECT:
  303. selectedReward = identifier.getNum();
  304. break;
  305. }
  306. }
  307. void CTownRewardableBuilding::heroLevelUpDone(const CGHeroInstance *hero) const
  308. {
  309. grantRewardAfterLevelup(cb, configuration.info.at(selectedReward), town, hero);
  310. }
  311. void CTownRewardableBuilding::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  312. {
  313. if(answer == 0)
  314. return; // player refused
  315. if(visitors.find(hero->id) != visitors.end())
  316. return; // query not for this building
  317. if(answer > 0 && answer-1 < configuration.info.size())
  318. {
  319. auto list = getAvailableRewards(hero, Rewardable::EEventType::EVENT_FIRST_VISIT);
  320. grantReward(list[answer - 1], hero);
  321. }
  322. else
  323. {
  324. throw std::runtime_error("Unhandled choice");
  325. }
  326. }
  327. void CTownRewardableBuilding::grantReward(ui32 rewardID, const CGHeroInstance * hero) const
  328. {
  329. town->addHeroToStructureVisitors(hero, indexOnTV);
  330. grantRewardBeforeLevelup(cb, configuration.info.at(rewardID), hero);
  331. // hero is not blocked by levelup dialog - grant remainer immediately
  332. if(!cb->isVisitCoveredByAnotherQuery(town, hero))
  333. {
  334. grantRewardAfterLevelup(cb, configuration.info.at(rewardID), town, hero);
  335. }
  336. }
  337. bool CTownRewardableBuilding::wasVisitedBefore(const CGHeroInstance * contextHero) const
  338. {
  339. switch (configuration.visitMode)
  340. {
  341. case Rewardable::VISIT_UNLIMITED:
  342. return false;
  343. case Rewardable::VISIT_ONCE:
  344. return !visitors.empty();
  345. case Rewardable::VISIT_PLAYER:
  346. return false; //not supported
  347. case Rewardable::VISIT_BONUS:
  348. {
  349. const auto building = town->getTown()->buildings.at(bID);
  350. return contextHero->hasBonusFrom(BonusSource::TOWN_STRUCTURE, BonusSourceID(building->getUniqueTypeID()));
  351. }
  352. case Rewardable::VISIT_HERO:
  353. return visitors.find(contextHero->id) != visitors.end();
  354. case Rewardable::VISIT_LIMITER:
  355. return configuration.visitLimiter.heroAllowed(contextHero);
  356. default:
  357. return false;
  358. }
  359. }
  360. void CTownRewardableBuilding::onHeroVisit(const CGHeroInstance *h) const
  361. {
  362. auto grantRewardWithMessage = [&](int index) -> void
  363. {
  364. auto vi = configuration.info.at(index);
  365. logGlobal->debug("Granting reward %d. Message says: %s", index, vi.message.toString());
  366. town->addHeroToStructureVisitors(h, indexOnTV); //adding to visitors
  367. InfoWindow iw;
  368. iw.player = h->tempOwner;
  369. iw.text = vi.message;
  370. vi.reward.loadComponents(iw.components, h);
  371. iw.type = EInfoWindowMode::MODAL;
  372. if(!iw.components.empty() || !iw.text.toString().empty())
  373. cb->showInfoDialog(&iw);
  374. grantReward(index, h);
  375. };
  376. auto selectRewardsMessage = [&](const std::vector<ui32> & rewards, const MetaString & dialog) -> void
  377. {
  378. BlockingDialog sd(configuration.canRefuse, rewards.size() > 1);
  379. sd.player = h->tempOwner;
  380. sd.text = dialog;
  381. if (rewards.size() > 1)
  382. for (auto index : rewards)
  383. sd.components.push_back(configuration.info.at(index).reward.getDisplayedComponent(h));
  384. if (rewards.size() == 1)
  385. configuration.info.at(rewards.front()).reward.loadComponents(sd.components, h);
  386. cb->showBlockingDialog(&sd);
  387. };
  388. if(!town->hasBuilt(bID) || cb->isVisitCoveredByAnotherQuery(town, h))
  389. return;
  390. if(!wasVisitedBefore(h))
  391. {
  392. auto rewards = getAvailableRewards(h, Rewardable::EEventType::EVENT_FIRST_VISIT);
  393. logGlobal->debug("Visiting object with %d possible rewards", rewards.size());
  394. switch (rewards.size())
  395. {
  396. case 0: // no available rewards, e.g. visiting School of War without gold
  397. {
  398. auto emptyRewards = getAvailableRewards(h, Rewardable::EEventType::EVENT_NOT_AVAILABLE);
  399. if (!emptyRewards.empty())
  400. grantRewardWithMessage(emptyRewards[0]);
  401. else
  402. logMod->warn("No applicable message for visiting empty object!");
  403. break;
  404. }
  405. case 1: // one reward. Just give it with message
  406. {
  407. if (configuration.canRefuse)
  408. selectRewardsMessage(rewards, configuration.info.at(rewards.front()).message);
  409. else
  410. grantRewardWithMessage(rewards.front());
  411. break;
  412. }
  413. default: // multiple rewards. Act according to select mode
  414. {
  415. switch (configuration.selectMode) {
  416. case Rewardable::SELECT_PLAYER: // player must select
  417. selectRewardsMessage(rewards, configuration.onSelect);
  418. break;
  419. case Rewardable::SELECT_FIRST: // give first available
  420. grantRewardWithMessage(rewards.front());
  421. break;
  422. case Rewardable::SELECT_RANDOM: // give random
  423. grantRewardWithMessage(*RandomGeneratorUtil::nextItem(rewards, cb->gameState()->getRandomGenerator()));
  424. break;
  425. }
  426. break;
  427. }
  428. }
  429. }
  430. else
  431. {
  432. logGlobal->debug("Revisiting already visited object");
  433. auto visitedRewards = getAvailableRewards(h, Rewardable::EEventType::EVENT_ALREADY_VISITED);
  434. if (!visitedRewards.empty())
  435. grantRewardWithMessage(visitedRewards[0]);
  436. else
  437. logMod->debug("No applicable message for visiting already visited object!");
  438. }
  439. }
  440. VCMI_LIB_NAMESPACE_END