CGTownBuilding.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 "../NetPacks.h"
  15. #include "../IGameCallback.h"
  16. VCMI_LIB_NAMESPACE_BEGIN
  17. PlayerColor CGTownBuilding::getOwner() const
  18. {
  19. return town->getOwner();
  20. }
  21. int32_t CGTownBuilding::getObjGroupIndex() const
  22. {
  23. return -1;
  24. }
  25. int32_t CGTownBuilding::getObjTypeIndex() const
  26. {
  27. return 0;
  28. }
  29. int3 CGTownBuilding::visitablePos() const
  30. {
  31. return town->visitablePos();
  32. }
  33. int3 CGTownBuilding::getPosition() const
  34. {
  35. return town->getPosition();
  36. }
  37. std::string CGTownBuilding::getVisitingBonusGreeting() const
  38. {
  39. auto bonusGreeting = town->getTown()->getGreeting(bType);
  40. if(!bonusGreeting.empty())
  41. return bonusGreeting;
  42. switch(bType)
  43. {
  44. case BuildingSubID::MANA_VORTEX:
  45. bonusGreeting = std::string(VLC->generaltexth->translate("vcmi.townHall.greetingManaVortex"));
  46. break;
  47. case BuildingSubID::KNOWLEDGE_VISITING_BONUS:
  48. bonusGreeting = std::string(VLC->generaltexth->translate("vcmi.townHall.greetingKnowledge"));
  49. break;
  50. case BuildingSubID::SPELL_POWER_VISITING_BONUS:
  51. bonusGreeting = std::string(VLC->generaltexth->translate("vcmi.townHall.greetingSpellPower"));
  52. break;
  53. case BuildingSubID::ATTACK_VISITING_BONUS:
  54. bonusGreeting = std::string(VLC->generaltexth->translate("vcmi.townHall.greetingAttack"));
  55. break;
  56. case BuildingSubID::EXPERIENCE_VISITING_BONUS:
  57. bonusGreeting = std::string(VLC->generaltexth->translate("vcmi.townHall.greetingExperience"));
  58. break;
  59. case BuildingSubID::DEFENSE_VISITING_BONUS:
  60. bonusGreeting = std::string(VLC->generaltexth->translate("vcmi.townHall.greetingDefence"));
  61. break;
  62. }
  63. auto buildingName = town->getTown()->getSpecialBuilding(bType)->getNameTranslated();
  64. if(bonusGreeting.empty())
  65. {
  66. bonusGreeting = "Error: Bonus greeting for '%s' is not localized.";
  67. logGlobal->error("'%s' building of '%s' faction has not localized bonus greeting.", buildingName, town->getTown()->faction->getNameTranslated());
  68. }
  69. boost::algorithm::replace_first(bonusGreeting, "%s", buildingName);
  70. town->getTown()->setGreeting(bType, bonusGreeting);
  71. return bonusGreeting;
  72. }
  73. std::string CGTownBuilding::getCustomBonusGreeting(const Bonus & bonus) const
  74. {
  75. if(bonus.type == Bonus::TOWN_MAGIC_WELL)
  76. {
  77. auto bonusGreeting = std::string(VLC->generaltexth->translate("vcmi.townHall.greetingInTownMagicWell"));
  78. auto buildingName = town->getTown()->getSpecialBuilding(bType)->getNameTranslated();
  79. boost::algorithm::replace_first(bonusGreeting, "%s", buildingName);
  80. return bonusGreeting;
  81. }
  82. auto bonusGreeting = std::string(VLC->generaltexth->translate("vcmi.townHall.greetingCustomBonus")); //"%s gives you +%d %s%s"
  83. std::string param;
  84. std::string until;
  85. if(bonus.type == Bonus::MORALE)
  86. param = VLC->generaltexth->allTexts[384];
  87. else if(bonus.type == Bonus::LUCK)
  88. param = VLC->generaltexth->allTexts[385];
  89. until = bonus.duration == static_cast<ui16>(Bonus::ONE_BATTLE)
  90. ? VLC->generaltexth->translate("vcmi.townHall.greetingCustomUntil")
  91. : ".";
  92. boost::format fmt = boost::format(bonusGreeting) % bonus.description % bonus.val % param % until;
  93. std::string greeting = fmt.str();
  94. return greeting;
  95. }
  96. COPWBonus::COPWBonus(const BuildingID & bid, BuildingSubID::EBuildingSubID subId, CGTownInstance * cgTown)
  97. {
  98. bID = bid;
  99. bType = subId;
  100. town = cgTown;
  101. indexOnTV = static_cast<si32>(town->bonusingBuildings.size());
  102. }
  103. void COPWBonus::setProperty(ui8 what, ui32 val)
  104. {
  105. switch (what)
  106. {
  107. case ObjProperty::VISITORS:
  108. visitors.insert(val);
  109. break;
  110. case ObjProperty::STRUCTURE_CLEAR_VISITORS:
  111. visitors.clear();
  112. break;
  113. }
  114. }
  115. void COPWBonus::onHeroVisit (const CGHeroInstance * h) const
  116. {
  117. ObjectInstanceID heroID = h->id;
  118. if(town->hasBuilt(bID))
  119. {
  120. InfoWindow iw;
  121. iw.player = h->tempOwner;
  122. switch (this->bType)
  123. {
  124. case BuildingSubID::STABLES:
  125. if(!h->hasBonusFrom(Bonus::OBJECT, Obj::STABLES)) //does not stack with advMap Stables
  126. {
  127. GiveBonus gb;
  128. gb.bonus = Bonus(Bonus::ONE_WEEK, Bonus::MOVEMENT, Bonus::OBJECT, 600, 94, VLC->generaltexth->arraytxt[100], 1);
  129. gb.id = heroID.getNum();
  130. cb->giveHeroBonus(&gb);
  131. SetMovePoints mp;
  132. mp.val = 600;
  133. mp.absolute = false;
  134. mp.hid = heroID;
  135. cb->setMovePoints(&mp);
  136. iw.text << VLC->generaltexth->allTexts[580];
  137. cb->showInfoDialog(&iw);
  138. }
  139. break;
  140. case BuildingSubID::MANA_VORTEX:
  141. if(visitors.empty())
  142. {
  143. if(h->mana < h->manaLimit() * 2)
  144. cb->setManaPoints (heroID, 2 * h->manaLimit());
  145. //TODO: investigate line below
  146. //cb->setObjProperty (town->id, ObjProperty::VISITED, true);
  147. iw.text << getVisitingBonusGreeting();
  148. cb->showInfoDialog(&iw);
  149. //extra visit penalty if hero alredy had double mana points (or even more?!)
  150. town->addHeroToStructureVisitors(h, indexOnTV);
  151. }
  152. break;
  153. }
  154. }
  155. }
  156. CTownBonus::CTownBonus(const BuildingID & index, BuildingSubID::EBuildingSubID subId, CGTownInstance * cgTown)
  157. {
  158. bID = index;
  159. bType = subId;
  160. town = cgTown;
  161. indexOnTV = static_cast<si32>(town->bonusingBuildings.size());
  162. }
  163. void CTownBonus::setProperty (ui8 what, ui32 val)
  164. {
  165. if(what == ObjProperty::VISITORS)
  166. visitors.insert(ObjectInstanceID(val));
  167. }
  168. void CTownBonus::onHeroVisit (const CGHeroInstance * h) const
  169. {
  170. ObjectInstanceID heroID = h->id;
  171. if(town->hasBuilt(bID) && visitors.find(heroID) == visitors.end())
  172. {
  173. si64 val = 0;
  174. InfoWindow iw;
  175. PrimarySkill::PrimarySkill what = PrimarySkill::NONE;
  176. switch(bType)
  177. {
  178. case BuildingSubID::KNOWLEDGE_VISITING_BONUS: //wall of knowledge
  179. what = PrimarySkill::KNOWLEDGE;
  180. val = 1;
  181. iw.components.emplace_back(Component::EComponentType::PRIM_SKILL, 3, 1, 0);
  182. break;
  183. case BuildingSubID::SPELL_POWER_VISITING_BONUS: //order of fire
  184. what = PrimarySkill::SPELL_POWER;
  185. val = 1;
  186. iw.components.emplace_back(Component::EComponentType::PRIM_SKILL, 2, 1, 0);
  187. break;
  188. case BuildingSubID::ATTACK_VISITING_BONUS: //hall of Valhalla
  189. what = PrimarySkill::ATTACK;
  190. val = 1;
  191. iw.components.emplace_back(Component::EComponentType::PRIM_SKILL, 0, 1, 0);
  192. break;
  193. case BuildingSubID::EXPERIENCE_VISITING_BONUS: //academy of battle scholars
  194. what = PrimarySkill::EXPERIENCE;
  195. val = static_cast<int>(h->calculateXp(1000));
  196. iw.components.emplace_back(Component::EComponentType::EXPERIENCE, 0, val, 0);
  197. break;
  198. case BuildingSubID::DEFENSE_VISITING_BONUS: //cage of warlords
  199. what = PrimarySkill::DEFENSE;
  200. val = 1;
  201. iw.components.emplace_back(Component::EComponentType::PRIM_SKILL, 1, 1, 0);
  202. break;
  203. case BuildingSubID::CUSTOM_VISITING_BONUS:
  204. const auto building = town->getTown()->buildings.at(bID);
  205. if(!h->hasBonusFrom(Bonus::TOWN_STRUCTURE, Bonus::getSid32(building->town->faction->getIndex(), building->bid)))
  206. {
  207. const auto & bonuses = building->onVisitBonuses;
  208. applyBonuses(const_cast<CGHeroInstance *>(h), bonuses);
  209. }
  210. break;
  211. }
  212. if(what != PrimarySkill::NONE)
  213. {
  214. iw.player = cb->getOwner(heroID);
  215. iw.text << getVisitingBonusGreeting();
  216. cb->showInfoDialog(&iw);
  217. cb->changePrimSkill (cb->getHero(heroID), what, val);
  218. town->addHeroToStructureVisitors(h, indexOnTV);
  219. }
  220. }
  221. }
  222. void CTownBonus::applyBonuses(CGHeroInstance * h, const BonusList & bonuses) const
  223. {
  224. auto addToVisitors = false;
  225. for(const auto & bonus : bonuses)
  226. {
  227. GiveBonus gb;
  228. InfoWindow iw;
  229. if(bonus->type == Bonus::TOWN_MAGIC_WELL)
  230. {
  231. if(h->mana >= h->manaLimit())
  232. return;
  233. cb->setManaPoints(h->id, h->manaLimit());
  234. bonus->duration = Bonus::ONE_DAY;
  235. }
  236. gb.bonus = * bonus;
  237. gb.id = h->id.getNum();
  238. cb->giveHeroBonus(&gb);
  239. if(bonus->duration == Bonus::PERMANENT)
  240. addToVisitors = true;
  241. iw.player = cb->getOwner(h->id);
  242. iw.text << getCustomBonusGreeting(gb.bonus);
  243. cb->showInfoDialog(&iw);
  244. }
  245. if(addToVisitors)
  246. town->addHeroToStructureVisitors(h, indexOnTV);
  247. }
  248. VCMI_LIB_NAMESPACE_END