CGTownInstance.h 8.9 KB

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