CGTownInstance.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. class CCastleEvent;
  16. class CGTownInstance;
  17. class CGDwelling;
  18. class DLL_LINKAGE CSpecObjInfo
  19. {
  20. public:
  21. CSpecObjInfo();
  22. virtual ~CSpecObjInfo() = default;
  23. virtual void serializeJson(JsonSerializeFormat & handler) = 0;
  24. const CGDwelling * owner;
  25. };
  26. class DLL_LINKAGE CCreGenAsCastleInfo : public virtual CSpecObjInfo
  27. {
  28. public:
  29. CCreGenAsCastleInfo();
  30. bool asCastle;
  31. ui32 identifier;//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. CCreGenLeveledInfo();
  40. ui8 minLevel, maxLevel; //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. virtual ~CGDwelling();
  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. BuildingID ID; //from buildig list
  81. si32 id; //identifies its index on towns vector
  82. CGTownInstance *town;
  83. template <typename Handler> void serialize(Handler &h, const int version)
  84. {
  85. h & ID;
  86. h & id;
  87. }
  88. };
  89. class DLL_LINKAGE COPWBonus : public CGTownBuilding
  90. {///used for OPW bonusing structures
  91. public:
  92. std::set<si32> visitors;
  93. void setProperty(ui8 what, ui32 val) override;
  94. void onHeroVisit (const CGHeroInstance * h) const override;
  95. COPWBonus (BuildingID index, CGTownInstance *TOWN);
  96. COPWBonus (){ID = BuildingID::NONE; town = nullptr;};
  97. template <typename Handler> void serialize(Handler &h, const int version)
  98. {
  99. h & static_cast<CGTownBuilding&>(*this);
  100. h & visitors;
  101. }
  102. };
  103. class DLL_LINKAGE CTownBonus : public CGTownBuilding
  104. {
  105. ///used for one-time bonusing structures
  106. ///feel free to merge inheritance tree
  107. public:
  108. std::set<ObjectInstanceID> visitors;
  109. void setProperty(ui8 what, ui32 val) override;
  110. void onHeroVisit (const CGHeroInstance * h) const override;
  111. CTownBonus (BuildingID index, CGTownInstance *TOWN);
  112. CTownBonus (){ID = BuildingID::NONE; town = nullptr;};
  113. template <typename Handler> void serialize(Handler &h, const int version)
  114. {
  115. h & static_cast<CGTownBuilding&>(*this);
  116. h & visitors;
  117. }
  118. };
  119. class DLL_LINKAGE CTownAndVisitingHero : public CBonusSystemNode
  120. {
  121. public:
  122. CTownAndVisitingHero();
  123. };
  124. struct DLL_LINKAGE GrowthInfo
  125. {
  126. struct Entry
  127. {
  128. int count;
  129. std::string description;
  130. Entry(const std::string &format, int _count);
  131. Entry(int subID, BuildingID building, int _count);
  132. Entry(int _count, const std::string &fullDescription);
  133. };
  134. std::vector<Entry> entries;
  135. int totalGrowth() const;
  136. };
  137. class DLL_LINKAGE CGTownInstance : public CGDwelling, public IShipyard, public IMarket
  138. {
  139. public:
  140. enum EFortLevel {NONE = 0, FORT = 1, CITADEL = 2, CASTLE = 3};
  141. CTownAndVisitingHero townAndVis;
  142. const CTown * town;
  143. std::string name; // name of town
  144. si32 builded; //how many buildings has been built this turn
  145. si32 destroyed; //how many buildings has been destroyed this turn
  146. ConstTransitivePtr<CGHeroInstance> garrisonHero, visitingHero;
  147. ui32 identifier; //special identifier from h3m (only > RoE maps)
  148. si32 alignment;
  149. std::set<BuildingID> forbiddenBuildings, builtBuildings;
  150. std::vector<CGTownBuilding*> bonusingBuildings;
  151. std::vector<SpellID> possibleSpells, obligatorySpells;
  152. std::vector<std::vector<SpellID> > spells; //spells[level] -> vector of spells, first will be available in guild
  153. std::list<CCastleEvent> events;
  154. std::pair<si32, si32> bonusValue;//var to store town bonuses (rampart = resources from mystic pond);
  155. //////////////////////////////////////////////////////////////////////////
  156. static std::vector<const CArtifact *> merchantArtifacts; //vector of artifacts available at Artifact merchant, NULLs possible (for making empty space when artifact is bought)
  157. static std::vector<int> universitySkills;//skills for university of magic
  158. template <typename Handler> void serialize(Handler &h, const int version)
  159. {
  160. h & static_cast<CGDwelling&>(*this);
  161. h & static_cast<IShipyard&>(*this);
  162. h & static_cast<IMarket&>(*this);
  163. h & name;
  164. h & builded;
  165. h & destroyed;
  166. h & identifier;
  167. h & garrisonHero;
  168. h & visitingHero;
  169. h & alignment;
  170. h & forbiddenBuildings;
  171. h & builtBuildings;
  172. h & bonusValue;
  173. h & possibleSpells;
  174. h & obligatorySpells;
  175. h & spells;
  176. h & events;
  177. h & bonusingBuildings;
  178. for (std::vector<CGTownBuilding*>::iterator i = bonusingBuildings.begin(); i!=bonusingBuildings.end(); i++)
  179. (*i)->town = this;
  180. h & town;
  181. h & townAndVis;
  182. BONUS_TREE_DESERIALIZATION_FIX
  183. vstd::erase_if(builtBuildings, [this](BuildingID building) -> bool
  184. {
  185. if(!town->buildings.count(building) || !town->buildings.at(building))
  186. {
  187. logGlobal->error("#1444-like issue in CGTownInstance::serialize. From town %s at %s removing the bogus builtBuildings item %s", name, pos.toString(), building);
  188. return true;
  189. }
  190. return false;
  191. });
  192. }
  193. //////////////////////////////////////////////////////////////////////////
  194. CBonusSystemNode *whatShouldBeAttached() override;
  195. std::string nodeName() const override;
  196. void updateMoraleBonusFromArmy() override;
  197. void deserializationFix();
  198. void recreateBuildingsBonuses();
  199. bool addBonusIfBuilt(BuildingID building, Bonus::BonusType type, int val, TPropagatorPtr &prop, int subtype = -1); //returns true if building is built and bonus has been added
  200. bool addBonusIfBuilt(BuildingID building, Bonus::BonusType type, int val, int subtype = -1); //convienence version of above
  201. void setVisitingHero(CGHeroInstance *h);
  202. void setGarrisonedHero(CGHeroInstance *h);
  203. const CArmedInstance *getUpperArmy() const; //garrisoned hero if present or the town itself
  204. //////////////////////////////////////////////////////////////////////////
  205. bool passableFor(PlayerColor color) const override;
  206. //int3 getSightCenter() const override; //"center" tile from which the sight distance is calculated
  207. int getSightRadius() const override; //returns sight distance
  208. int getBoatType() const override; //0 - evil (if a ship can be evil...?), 1 - good, 2 - neutral
  209. void getOutOffsets(std::vector<int3> &offsets) const override; //offsets to obj pos when we boat can be placed. Parameter will be cleared
  210. int getMarketEfficiency() const override; //=market count
  211. bool allowsTrade(EMarketMode::EMarketMode mode) const override;
  212. std::vector<int> availableItemsIds(EMarketMode::EMarketMode mode) const override;
  213. void setType(si32 ID, si32 subID) override;
  214. void updateAppearance();
  215. //////////////////////////////////////////////////////////////////////////
  216. bool needsLastStack() const override;
  217. CGTownInstance::EFortLevel fortLevel() const;
  218. int hallLevel() const; // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  219. int mageGuildLevel() const; // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  220. int getHordeLevel(const int & HID) const; //HID - 0 or 1; returns creature level or -1 if that horde structure is not present
  221. int creatureGrowth(const int & level) const;
  222. GrowthInfo getGrowthInfo(int level) const;
  223. bool hasFort() const;
  224. bool hasCapitol() const;
  225. //checks if building is constructed and town has same subID
  226. bool hasBuilt(BuildingID buildingID) const;
  227. bool hasBuilt(BuildingID buildingID, int townID) const;
  228. TResources dailyIncome() const; //calculates daily income of this town
  229. int spellsAtLevel(int level, bool checkGuild) const; //levels are counted from 1 (1 - 5)
  230. bool armedGarrison() const; //true if town has creatures in garrison or garrisoned hero
  231. int getTownLevel() const;
  232. CBuilding::TRequired genBuildingRequirements(BuildingID build, bool deep = false) const;
  233. void mergeGarrisonOnSiege() const; // merge garrison into army of visiting hero
  234. void removeCapitols (PlayerColor owner) const;
  235. void clearArmy() const;
  236. void addHeroToStructureVisitors(const CGHeroInstance *h, si32 structureInstanceID) const; //hero must be visiting or garrisoned in town
  237. const CTown * getTown() const ;
  238. CGTownInstance();
  239. virtual ~CGTownInstance();
  240. ///IObjectInterface overrides
  241. void newTurn(CRandomGenerator & rand) const override;
  242. void onHeroVisit(const CGHeroInstance * h) const override;
  243. void onHeroLeave(const CGHeroInstance * h) const override;
  244. void initObj(CRandomGenerator & rand) override;
  245. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  246. std::string getObjectName() const override;
  247. void afterAddToMap(CMap * map) override;
  248. static void reset();
  249. protected:
  250. void setPropertyDer(ui8 what, ui32 val) override;
  251. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  252. private:
  253. int getDwellingBonus(const std::vector<CreatureID>& creatureIds, const std::vector<ConstTransitivePtr<CGDwelling> >& dwellings) const;
  254. };