CGTownInstance.h 8.8 KB

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