CGTownInstance.h 10 KB

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