CGTownInstance.h 9.1 KB

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