CGTownInstance.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. };
  114. std::vector<Entry> entries;
  115. int totalGrowth() const;
  116. };
  117. class DLL_LINKAGE CGTownInstance : public CGDwelling, public IShipyard, public IMarket
  118. {
  119. public:
  120. enum EFortLevel {NONE = 0, FORT = 1, CITADEL = 2, CASTLE = 3};
  121. CTownAndVisitingHero townAndVis;
  122. const CTown * town;
  123. std::string name; // name of town
  124. si32 builded; //how many buildings has been built this turn
  125. si32 destroyed; //how many buildings has been destroyed this turn
  126. ConstTransitivePtr<CGHeroInstance> garrisonHero, visitingHero;
  127. ui32 identifier; //special identifier from h3m (only > RoE maps)
  128. si32 alignment;
  129. std::set<BuildingID> forbiddenBuildings, builtBuildings;
  130. std::vector<CGTownBuilding*> bonusingBuildings;
  131. std::vector<SpellID> possibleSpells, obligatorySpells;
  132. std::vector<std::vector<SpellID> > spells; //spells[level] -> vector of spells, first will be available in guild
  133. std::list<CCastleEvent> events;
  134. std::pair<si32, si32> bonusValue;//var to store town bonuses (rampart = resources from mystic pond);
  135. //////////////////////////////////////////////////////////////////////////
  136. static std::vector<const CArtifact *> merchantArtifacts; //vector of artifacts available at Artifact merchant, NULLs possible (for making empty space when artifact is bought)
  137. static std::vector<int> universitySkills;//skills for university of magic
  138. template <typename Handler> void serialize(Handler &h, const int version)
  139. {
  140. h & static_cast<CGDwelling&>(*this);
  141. h & static_cast<IShipyard&>(*this);
  142. h & static_cast<IMarket&>(*this);
  143. h & name & builded & destroyed & identifier;
  144. h & garrisonHero & visitingHero;
  145. h & alignment & forbiddenBuildings & builtBuildings & bonusValue
  146. & possibleSpells & obligatorySpells & spells & /*strInfo & */events & bonusingBuildings;
  147. for (std::vector<CGTownBuilding*>::iterator i = bonusingBuildings.begin(); i!=bonusingBuildings.end(); i++)
  148. (*i)->town = this;
  149. h & town & townAndVis;
  150. BONUS_TREE_DESERIALIZATION_FIX
  151. vstd::erase_if(builtBuildings, [this](BuildingID building) -> bool
  152. {
  153. if(!town->buildings.count(building) || !town->buildings.at(building))
  154. {
  155. logGlobal->errorStream() << boost::format("#1444-like issue in CGTownInstance::serialize. From town %s at %s removing the bogus builtBuildings item %s")
  156. % name % pos % building;
  157. return true;
  158. }
  159. return false;
  160. });
  161. }
  162. //////////////////////////////////////////////////////////////////////////
  163. CBonusSystemNode *whatShouldBeAttached() override;
  164. std::string nodeName() const override;
  165. void updateMoraleBonusFromArmy() override;
  166. void deserializationFix();
  167. void recreateBuildingsBonuses();
  168. 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
  169. bool addBonusIfBuilt(BuildingID building, Bonus::BonusType type, int val, int subtype = -1); //convienence version of above
  170. void setVisitingHero(CGHeroInstance *h);
  171. void setGarrisonedHero(CGHeroInstance *h);
  172. const CArmedInstance *getUpperArmy() const; //garrisoned hero if present or the town itself
  173. //////////////////////////////////////////////////////////////////////////
  174. bool passableFor(PlayerColor color) const;
  175. //int3 getSightCenter() const override; //"center" tile from which the sight distance is calculated
  176. int getSightRadious() const override; //returns sight distance
  177. int getBoatType() const; //0 - evil (if a ship can be evil...?), 1 - good, 2 - neutral
  178. void getOutOffsets(std::vector<int3> &offsets) const; //offsets to obj pos when we boat can be placed. Parameter will be cleared
  179. int getMarketEfficiency() const override; //=market count
  180. bool allowsTrade(EMarketMode::EMarketMode mode) const;
  181. std::vector<int> availableItemsIds(EMarketMode::EMarketMode mode) const;
  182. void setType(si32 ID, si32 subID);
  183. void updateAppearance();
  184. //////////////////////////////////////////////////////////////////////////
  185. bool needsLastStack() const;
  186. CGTownInstance::EFortLevel fortLevel() const;
  187. int hallLevel() const; // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  188. int mageGuildLevel() const; // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  189. int getHordeLevel(const int & HID) const; //HID - 0 or 1; returns creature level or -1 if that horde structure is not present
  190. int creatureGrowth(const int & level) const;
  191. GrowthInfo getGrowthInfo(int level) const;
  192. bool hasFort() const;
  193. bool hasCapitol() const;
  194. //checks if building is constructed and town has same subID
  195. bool hasBuilt(BuildingID buildingID) const;
  196. bool hasBuilt(BuildingID buildingID, int townID) const;
  197. TResources dailyIncome() const; //calculates daily income of this town
  198. int spellsAtLevel(int level, bool checkGuild) const; //levels are counted from 1 (1 - 5)
  199. bool armedGarrison() const; //true if town has creatures in garrison or garrisoned hero
  200. int getTownLevel() const;
  201. CBuilding::TRequired genBuildingRequirements(BuildingID build, bool includeUpgrade=true) const;
  202. void removeCapitols (PlayerColor owner) const;
  203. void addHeroToStructureVisitors(const CGHeroInstance *h, si32 structureInstanceID) const; //hero must be visiting or garrisoned in town
  204. CGTownInstance();
  205. virtual ~CGTownInstance();
  206. ///IObjectInterface overrides
  207. void newTurn() const override;
  208. void onHeroVisit(const CGHeroInstance * h) const override;
  209. void onHeroLeave(const CGHeroInstance * h) const override;
  210. void initObj() override;
  211. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  212. std::string getObjectName() const override;
  213. protected:
  214. void setPropertyDer(ui8 what, ui32 val) override;
  215. };