CGTownInstance.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * CGTownInstance.h, 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. #pragma once
  11. #include "CObjectHandler.h"
  12. #include "CGMarket.h" // For IMarket interface
  13. #include "CArmedInstance.h"
  14. #include "../CTownHandler.h" // For CTown
  15. VCMI_LIB_NAMESPACE_BEGIN
  16. class CCastleEvent;
  17. class CGTownInstance;
  18. class CGDwelling;
  19. class DLL_LINKAGE CSpecObjInfo
  20. {
  21. public:
  22. CSpecObjInfo();
  23. virtual ~CSpecObjInfo() = default;
  24. virtual void serializeJson(JsonSerializeFormat & handler) = 0;
  25. const CGDwelling * owner;
  26. };
  27. class DLL_LINKAGE CCreGenAsCastleInfo : public virtual CSpecObjInfo
  28. {
  29. public:
  30. bool asCastle = false;
  31. ui32 identifier = 0;//h3m internal identifier
  32. std::vector<bool> allowedFactions;
  33. std::string instanceId;//vcmi map instance identifier
  34. void serializeJson(JsonSerializeFormat & handler) override;
  35. };
  36. class DLL_LINKAGE CCreGenLeveledInfo : public virtual CSpecObjInfo
  37. {
  38. public:
  39. ui8 minLevel = 0;
  40. ui8 maxLevel = 7; //minimal and maximal level of creature in dwelling: <1, 7>
  41. void serializeJson(JsonSerializeFormat & handler) override;
  42. };
  43. class DLL_LINKAGE CCreGenLeveledCastleInfo : public CCreGenAsCastleInfo, public CCreGenLeveledInfo
  44. {
  45. public:
  46. CCreGenLeveledCastleInfo() = default;
  47. void serializeJson(JsonSerializeFormat & handler) override;
  48. };
  49. class DLL_LINKAGE CGDwelling : public CArmedInstance
  50. {
  51. public:
  52. typedef std::vector<std::pair<ui32, std::vector<CreatureID> > > TCreaturesSet;
  53. CSpecObjInfo * info; //random dwelling options; not serialized
  54. TCreaturesSet creatures; //creatures[level] -> <vector of alternative ids (base creature and upgrades, creatures amount>
  55. CGDwelling();
  56. ~CGDwelling() override;
  57. void initRandomObjectInfo();
  58. protected:
  59. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  60. private:
  61. void initObj(CRandomGenerator & rand) override;
  62. void onHeroVisit(const CGHeroInstance * h) const override;
  63. void newTurn(CRandomGenerator & rand) const override;
  64. void setPropertyDer(ui8 what, ui32 val) override;
  65. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  66. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  67. void updateGuards() const;
  68. void heroAcceptsCreatures(const CGHeroInstance *h) const;
  69. public:
  70. template <typename Handler> void serialize(Handler &h, const int version)
  71. {
  72. h & static_cast<CArmedInstance&>(*this);
  73. h & creatures;
  74. }
  75. };
  76. class DLL_LINKAGE CGTownBuilding : public IObjectInterface
  77. {
  78. ///basic class for town structures handled as map objects
  79. public:
  80. si32 indexOnTV = 0; //identifies its index on towns vector
  81. CGTownInstance *town = nullptr;
  82. STRONG_INLINE
  83. BuildingSubID::EBuildingSubID getBuildingSubtype() const
  84. {
  85. return bType;
  86. }
  87. STRONG_INLINE
  88. const BuildingID & getBuildingType() const
  89. {
  90. return bID;
  91. }
  92. STRONG_INLINE
  93. void setBuildingSubtype(BuildingSubID::EBuildingSubID subId)
  94. {
  95. bType = subId;
  96. }
  97. PlayerColor getOwner() const override;
  98. int32_t getObjGroupIndex() const override;
  99. int32_t getObjTypeIndex() const override;
  100. int3 visitablePos() const override;
  101. int3 getPosition() const override;
  102. template <typename Handler> void serialize(Handler &h, const int version)
  103. {
  104. h & bID;
  105. h & indexOnTV;
  106. h & bType;
  107. }
  108. protected:
  109. BuildingID bID; //from buildig list
  110. BuildingSubID::EBuildingSubID bType = BuildingSubID::NONE;
  111. std::string getVisitingBonusGreeting() const;
  112. std::string getCustomBonusGreeting(const Bonus & bonus) const;
  113. };
  114. class DLL_LINKAGE COPWBonus : public CGTownBuilding
  115. {///used for OPW bonusing structures
  116. public:
  117. std::set<si32> visitors;
  118. void setProperty(ui8 what, ui32 val) override;
  119. void onHeroVisit (const CGHeroInstance * h) const override;
  120. COPWBonus(const BuildingID & index, BuildingSubID::EBuildingSubID subId, CGTownInstance * TOWN);
  121. COPWBonus() = default;
  122. template <typename Handler> void serialize(Handler &h, const int version)
  123. {
  124. h & static_cast<CGTownBuilding&>(*this);
  125. h & visitors;
  126. }
  127. };
  128. class DLL_LINKAGE CTownBonus : public CGTownBuilding
  129. {
  130. ///used for one-time bonusing structures
  131. ///feel free to merge inheritance tree
  132. public:
  133. std::set<ObjectInstanceID> visitors;
  134. void setProperty(ui8 what, ui32 val) override;
  135. void onHeroVisit (const CGHeroInstance * h) const override;
  136. CTownBonus(const BuildingID & index, BuildingSubID::EBuildingSubID subId, CGTownInstance * TOWN);
  137. CTownBonus() = default;
  138. template <typename Handler> void serialize(Handler &h, const int version)
  139. {
  140. h & static_cast<CGTownBuilding&>(*this);
  141. h & visitors;
  142. }
  143. private:
  144. void applyBonuses(CGHeroInstance * h, const BonusList & bonuses) const;
  145. };
  146. class DLL_LINKAGE CTownAndVisitingHero : public CBonusSystemNode
  147. {
  148. public:
  149. CTownAndVisitingHero();
  150. };
  151. struct DLL_LINKAGE GrowthInfo
  152. {
  153. struct Entry
  154. {
  155. int count;
  156. std::string description;
  157. Entry(const std::string &format, int _count);
  158. Entry(int subID, const BuildingID & building, int _count);
  159. Entry(int _count, std::string fullDescription);
  160. };
  161. std::vector<Entry> entries;
  162. int totalGrowth() const;
  163. };
  164. class DLL_LINKAGE CGTownInstance : public CGDwelling, public IShipyard, public IMarket
  165. {
  166. std::string name; // name of town
  167. public:
  168. enum EFortLevel {NONE = 0, FORT = 1, CITADEL = 2, CASTLE = 3};
  169. CTownAndVisitingHero townAndVis;
  170. const CTown * town;
  171. si32 builded; //how many buildings has been built this turn
  172. si32 destroyed; //how many buildings has been destroyed this turn
  173. ConstTransitivePtr<CGHeroInstance> garrisonHero, visitingHero;
  174. ui32 identifier; //special identifier from h3m (only > RoE maps)
  175. si32 alignment;
  176. std::set<BuildingID> forbiddenBuildings;
  177. std::set<BuildingID> builtBuildings;
  178. std::set<BuildingID> overriddenBuildings; ///buildings which bonuses are overridden and should not be applied
  179. std::vector<CGTownBuilding*> bonusingBuildings;
  180. std::vector<SpellID> possibleSpells, obligatorySpells;
  181. std::vector<std::vector<SpellID> > spells; //spells[level] -> vector of spells, first will be available in guild
  182. std::list<CCastleEvent> events;
  183. std::pair<si32, si32> bonusValue;//var to store town bonuses (rampart = resources from mystic pond);
  184. //////////////////////////////////////////////////////////////////////////
  185. static std::vector<const CArtifact *> merchantArtifacts; //vector of artifacts available at Artifact merchant, NULLs possible (for making empty space when artifact is bought)
  186. static std::vector<int> universitySkills;//skills for university of magic
  187. template <typename Handler> void serialize(Handler &h, const int version)
  188. {
  189. h & static_cast<CGDwelling&>(*this);
  190. h & static_cast<IShipyard&>(*this);
  191. h & static_cast<IMarket&>(*this);
  192. h & name;
  193. h & builded;
  194. h & destroyed;
  195. h & identifier;
  196. h & garrisonHero;
  197. h & visitingHero;
  198. h & alignment;
  199. h & forbiddenBuildings;
  200. h & builtBuildings;
  201. h & bonusValue;
  202. h & possibleSpells;
  203. h & obligatorySpells;
  204. h & spells;
  205. h & events;
  206. h & bonusingBuildings;
  207. for(auto * bonusingBuilding : bonusingBuildings)
  208. bonusingBuilding->town = this;
  209. h & town;
  210. h & townAndVis;
  211. BONUS_TREE_DESERIALIZATION_FIX
  212. vstd::erase_if(builtBuildings, [this](BuildingID building) -> bool
  213. {
  214. if(!town->buildings.count(building) || !town->buildings.at(building))
  215. {
  216. logGlobal->error("#1444-like issue in CGTownInstance::serialize. From town %s at %s removing the bogus builtBuildings item %s", name, pos.toString(), building);
  217. return true;
  218. }
  219. return false;
  220. });
  221. h & overriddenBuildings;
  222. if(!h.saving)
  223. this->setNodeType(CBonusSystemNode::TOWN);
  224. }
  225. //////////////////////////////////////////////////////////////////////////
  226. CBonusSystemNode & whatShouldBeAttached() override;
  227. std::string nodeName() const override;
  228. void updateMoraleBonusFromArmy() override;
  229. void deserializationFix();
  230. void recreateBuildingsBonuses();
  231. void setVisitingHero(CGHeroInstance *h);
  232. void setGarrisonedHero(CGHeroInstance *h);
  233. const CArmedInstance *getUpperArmy() const; //garrisoned hero if present or the town itself
  234. std::string getNameTranslated() const;
  235. void setNameTranslated(const std::string & newName);
  236. //////////////////////////////////////////////////////////////////////////
  237. bool passableFor(PlayerColor color) const override;
  238. //int3 getSightCenter() const override; //"center" tile from which the sight distance is calculated
  239. int getSightRadius() const override; //returns sight distance
  240. int getBoatType() const override; //0 - evil (if a ship can be evil...?), 1 - good, 2 - neutral
  241. void getOutOffsets(std::vector<int3> &offsets) const override; //offsets to obj pos when we boat can be placed. Parameter will be cleared
  242. int getMarketEfficiency() const override; //=market count
  243. bool allowsTrade(EMarketMode::EMarketMode mode) const override;
  244. std::vector<int> availableItemsIds(EMarketMode::EMarketMode mode) const override;
  245. void setType(si32 ID, si32 subID) override;
  246. void updateAppearance();
  247. //////////////////////////////////////////////////////////////////////////
  248. bool needsLastStack() const override;
  249. CGTownInstance::EFortLevel fortLevel() const;
  250. int hallLevel() const; // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  251. int mageGuildLevel() const; // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  252. int getHordeLevel(const int & HID) const; //HID - 0 or 1; returns creature level or -1 if that horde structure is not present
  253. int creatureGrowth(const int & level) const;
  254. GrowthInfo getGrowthInfo(int level) const;
  255. bool hasFort() const;
  256. bool hasCapitol() const;
  257. const CGTownBuilding * getBonusingBuilding(BuildingSubID::EBuildingSubID subId) const;
  258. bool hasBuiltSomeTradeBuilding() const;
  259. //checks if special building with type buildingID is constructed
  260. bool hasBuilt(BuildingSubID::EBuildingSubID buildingID) const;
  261. //checks if building is constructed and town has same subID
  262. bool hasBuilt(const BuildingID & buildingID) const;
  263. bool hasBuilt(const BuildingID & buildingID, int townID) const;
  264. TResources getBuildingCost(const BuildingID & buildingID) const;
  265. TResources dailyIncome() const; //calculates daily income of this town
  266. int spellsAtLevel(int level, bool checkGuild) const; //levels are counted from 1 (1 - 5)
  267. bool armedGarrison() const; //true if town has creatures in garrison or garrisoned hero
  268. int getTownLevel() const;
  269. CBuilding::TRequired genBuildingRequirements(const BuildingID & build, bool deep = false) const;
  270. void mergeGarrisonOnSiege() const; // merge garrison into army of visiting hero
  271. void removeCapitols(const PlayerColor & owner) const;
  272. void clearArmy() const;
  273. void addHeroToStructureVisitors(const CGHeroInstance *h, si64 structureInstanceID) const; //hero must be visiting or garrisoned in town
  274. void deleteTownBonus(BuildingID::EBuildingID bid);
  275. /// Returns damage range for secondary towers of this town
  276. TDmgRange getTowerDamageRange() const;
  277. /// Returns damage range for central tower(keep) of this town
  278. TDmgRange getKeepDamageRange() const;
  279. const CTown * getTown() const ;
  280. CGTownInstance();
  281. virtual ~CGTownInstance();
  282. ///IObjectInterface overrides
  283. void newTurn(CRandomGenerator & rand) const override;
  284. void onHeroVisit(const CGHeroInstance * h) const override;
  285. void onHeroLeave(const CGHeroInstance * h) const override;
  286. void initObj(CRandomGenerator & rand) override;
  287. void battleFinished(const CGHeroInstance * hero, const BattleResult & result) const override;
  288. std::string getObjectName() const override;
  289. void afterAddToMap(CMap * map) override;
  290. void afterRemoveFromMap(CMap * map) override;
  291. static void reset();
  292. inline bool isBattleOutsideTown(const CGHeroInstance * defendingHero) const
  293. {
  294. return defendingHero && garrisonHero && defendingHero != garrisonHero;
  295. }
  296. protected:
  297. void setPropertyDer(ui8 what, ui32 val) override;
  298. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  299. private:
  300. void setOwner(const PlayerColor & owner) const;
  301. void onTownCaptured(const PlayerColor & winner) const;
  302. int getDwellingBonus(const std::vector<CreatureID>& creatureIds, const std::vector<ConstTransitivePtr<CGDwelling> >& dwellings) const;
  303. bool hasBuiltInOldWay(ETownType::ETownType type, const BuildingID & bid) const;
  304. bool townEnvisagesBuilding(BuildingSubID::EBuildingSubID bid) const;
  305. bool isBonusingBuildingAdded(BuildingID::EBuildingID bid) const;
  306. void tryAddOnePerWeekBonus(BuildingSubID::EBuildingSubID subID);
  307. void tryAddVisitingBonus(BuildingSubID::EBuildingSubID subID);
  308. void initOverriddenBids();
  309. void addTownBonuses();
  310. };
  311. VCMI_LIB_NAMESPACE_END