CGTownInstance.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 DLL_LINKAGE CSpecObjInfo
  18. {
  19. public:
  20. virtual ~CSpecObjInfo(){};
  21. PlayerColor player; //owner
  22. };
  23. class DLL_LINKAGE CCreGenAsCastleInfo : public virtual CSpecObjInfo
  24. {
  25. public:
  26. bool asCastle;
  27. ui32 identifier;
  28. ui8 castles[2]; //allowed castles
  29. };
  30. class DLL_LINKAGE CCreGenLeveledInfo : public virtual CSpecObjInfo
  31. {
  32. public:
  33. ui8 minLevel, maxLevel; //minimal and maximal level of creature in dwelling: <0, 6>
  34. };
  35. class DLL_LINKAGE CCreGenLeveledCastleInfo : public CCreGenAsCastleInfo, public CCreGenLeveledInfo
  36. {
  37. };
  38. class DLL_LINKAGE CGDwelling : public CArmedInstance
  39. {
  40. public:
  41. typedef std::vector<std::pair<ui32, std::vector<CreatureID> > > TCreaturesSet;
  42. CSpecObjInfo * info; //h3m info about dewlling
  43. TCreaturesSet creatures; //creatures[level] -> <vector of alternative ids (base creature and upgrades, creatures amount>
  44. protected:
  45. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  46. private:
  47. void initObj() override;
  48. void onHeroVisit(const CGHeroInstance * h) const override;
  49. void newTurn() const override;
  50. void setPropertyDer(ui8 what, ui32 val) override;
  51. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  52. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  53. void updateGuards() const;
  54. void heroAcceptsCreatures(const CGHeroInstance *h) const;
  55. public:
  56. template <typename Handler> void serialize(Handler &h, const int version)
  57. {
  58. h & static_cast<CArmedInstance&>(*this) & creatures;
  59. }
  60. };
  61. class DLL_LINKAGE CGTownBuilding : public IObjectInterface
  62. {
  63. ///basic class for town structures handled as map objects
  64. public:
  65. BuildingID ID; //from buildig list
  66. si32 id; //identifies its index on towns vector
  67. CGTownInstance *town;
  68. template <typename Handler> void serialize(Handler &h, const int version)
  69. {
  70. h & ID & id;
  71. }
  72. };
  73. class DLL_LINKAGE COPWBonus : public CGTownBuilding
  74. {///used for OPW bonusing structures
  75. public:
  76. std::set<si32> visitors;
  77. void setProperty(ui8 what, ui32 val) override;
  78. void onHeroVisit (const CGHeroInstance * h) const override;
  79. COPWBonus (BuildingID index, CGTownInstance *TOWN);
  80. COPWBonus (){ID = BuildingID::NONE; town = nullptr;};
  81. template <typename Handler> void serialize(Handler &h, const int version)
  82. {
  83. h & static_cast<CGTownBuilding&>(*this);
  84. h & visitors;
  85. }
  86. };
  87. class DLL_LINKAGE CTownBonus : public CGTownBuilding
  88. {
  89. ///used for one-time bonusing structures
  90. ///feel free to merge inheritance tree
  91. public:
  92. std::set<ObjectInstanceID> visitors;
  93. void setProperty(ui8 what, ui32 val) override;
  94. void onHeroVisit (const CGHeroInstance * h) const override;
  95. CTownBonus (BuildingID index, CGTownInstance *TOWN);
  96. CTownBonus (){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 CTownAndVisitingHero : public CBonusSystemNode
  104. {
  105. public:
  106. CTownAndVisitingHero();
  107. };
  108. struct DLL_LINKAGE GrowthInfo
  109. {
  110. struct Entry
  111. {
  112. int count;
  113. std::string description;
  114. Entry(const std::string &format, int _count);
  115. Entry(int subID, BuildingID building, int _count);
  116. Entry(int _count, const std::string &fullDescription);
  117. };
  118. std::vector<Entry> entries;
  119. int totalGrowth() const;
  120. };
  121. class DLL_LINKAGE CGTownInstance : public CGDwelling, public IShipyard, public IMarket
  122. {
  123. public:
  124. enum EFortLevel {NONE = 0, FORT = 1, CITADEL = 2, CASTLE = 3};
  125. CTownAndVisitingHero townAndVis;
  126. const CTown * town;
  127. std::string name; // name of town
  128. si32 builded; //how many buildings has been built this turn
  129. si32 destroyed; //how many buildings has been destroyed this turn
  130. ConstTransitivePtr<CGHeroInstance> garrisonHero, visitingHero;
  131. ui32 identifier; //special identifier from h3m (only > RoE maps)
  132. si32 alignment;
  133. std::set<BuildingID> forbiddenBuildings, builtBuildings;
  134. std::vector<CGTownBuilding*> bonusingBuildings;
  135. std::vector<SpellID> possibleSpells, obligatorySpells;
  136. std::vector<std::vector<SpellID> > spells; //spells[level] -> vector of spells, first will be available in guild
  137. std::list<CCastleEvent> events;
  138. std::pair<si32, si32> bonusValue;//var to store town bonuses (rampart = resources from mystic pond);
  139. //////////////////////////////////////////////////////////////////////////
  140. static std::vector<const CArtifact *> merchantArtifacts; //vector of artifacts available at Artifact merchant, NULLs possible (for making empty space when artifact is bought)
  141. static std::vector<int> universitySkills;//skills for university of magic
  142. template <typename Handler> void serialize(Handler &h, const int version)
  143. {
  144. h & static_cast<CGDwelling&>(*this);
  145. h & static_cast<IShipyard&>(*this);
  146. h & static_cast<IMarket&>(*this);
  147. h & name & builded & destroyed & identifier;
  148. h & garrisonHero & visitingHero;
  149. h & alignment & forbiddenBuildings & builtBuildings & bonusValue
  150. & possibleSpells & obligatorySpells & spells & /*strInfo & */events & bonusingBuildings;
  151. for (std::vector<CGTownBuilding*>::iterator i = bonusingBuildings.begin(); i!=bonusingBuildings.end(); i++)
  152. (*i)->town = this;
  153. h & town & townAndVis;
  154. BONUS_TREE_DESERIALIZATION_FIX
  155. vstd::erase_if(builtBuildings, [this](BuildingID building) -> bool
  156. {
  157. if(!town->buildings.count(building) || !town->buildings.at(building))
  158. {
  159. logGlobal->errorStream() << boost::format("#1444-like issue in CGTownInstance::serialize. From town %s at %s removing the bogus builtBuildings item %s")
  160. % name % pos % building;
  161. return true;
  162. }
  163. return false;
  164. });
  165. }
  166. //////////////////////////////////////////////////////////////////////////
  167. CBonusSystemNode *whatShouldBeAttached() override;
  168. std::string nodeName() const override;
  169. void updateMoraleBonusFromArmy() override;
  170. void deserializationFix();
  171. void recreateBuildingsBonuses();
  172. 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
  173. bool addBonusIfBuilt(BuildingID building, Bonus::BonusType type, int val, int subtype = -1); //convienence version of above
  174. void setVisitingHero(CGHeroInstance *h);
  175. void setGarrisonedHero(CGHeroInstance *h);
  176. const CArmedInstance *getUpperArmy() const; //garrisoned hero if present or the town itself
  177. //////////////////////////////////////////////////////////////////////////
  178. bool passableFor(PlayerColor color) const override;
  179. //int3 getSightCenter() const override; //"center" tile from which the sight distance is calculated
  180. int getSightRadius() const override; //returns sight distance
  181. int getBoatType() const override; //0 - evil (if a ship can be evil...?), 1 - good, 2 - neutral
  182. void getOutOffsets(std::vector<int3> &offsets) const override; //offsets to obj pos when we boat can be placed. Parameter will be cleared
  183. int getMarketEfficiency() const override; //=market count
  184. bool allowsTrade(EMarketMode::EMarketMode mode) const override;
  185. std::vector<int> availableItemsIds(EMarketMode::EMarketMode mode) const override;
  186. void setType(si32 ID, si32 subID) override;
  187. void updateAppearance();
  188. //////////////////////////////////////////////////////////////////////////
  189. bool needsLastStack() const override;
  190. CGTownInstance::EFortLevel fortLevel() const;
  191. int hallLevel() const; // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  192. int mageGuildLevel() const; // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  193. int getHordeLevel(const int & HID) const; //HID - 0 or 1; returns creature level or -1 if that horde structure is not present
  194. int creatureGrowth(const int & level) const;
  195. GrowthInfo getGrowthInfo(int level) const;
  196. bool hasFort() const;
  197. bool hasCapitol() const;
  198. //checks if building is constructed and town has same subID
  199. bool hasBuilt(BuildingID buildingID) const;
  200. bool hasBuilt(BuildingID buildingID, int townID) const;
  201. TResources dailyIncome() const; //calculates daily income of this town
  202. int spellsAtLevel(int level, bool checkGuild) const; //levels are counted from 1 (1 - 5)
  203. bool armedGarrison() const; //true if town has creatures in garrison or garrisoned hero
  204. int getTownLevel() const;
  205. CBuilding::TRequired genBuildingRequirements(BuildingID build) const;
  206. void mergeGarrisonOnSiege() const; // merge garrison into army of visiting hero
  207. void removeCapitols (PlayerColor owner) const;
  208. void clearArmy() const;
  209. void addHeroToStructureVisitors(const CGHeroInstance *h, si32 structureInstanceID) const; //hero must be visiting or garrisoned in town
  210. CGTownInstance();
  211. virtual ~CGTownInstance();
  212. ///IObjectInterface overrides
  213. void newTurn() const override;
  214. void onHeroVisit(const CGHeroInstance * h) const override;
  215. void onHeroLeave(const CGHeroInstance * h) const override;
  216. void initObj() override;
  217. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  218. std::string getObjectName() const override;
  219. protected:
  220. void setPropertyDer(ui8 what, ui32 val) override;
  221. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  222. };