CGTownInstance.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. * CGTownInstance.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "CObjectHandler.h"
  12. #include "CGMarket.h" // For IMarket interface
  13. #include "CArmedInstance.h"
  14. #include "../CTownHandler.h" // For CTown
  15. class CCastleEvent;
  16. class CGTownInstance;
  17. class CGDwelling;
  18. class DLL_LINKAGE CSpecObjInfo
  19. {
  20. public:
  21. CSpecObjInfo();
  22. virtual ~CSpecObjInfo() = default;
  23. virtual void serializeJson(JsonSerializeFormat & handler) = 0;
  24. const CGDwelling * owner;
  25. };
  26. class DLL_LINKAGE CCreGenAsCastleInfo : public virtual CSpecObjInfo
  27. {
  28. public:
  29. CCreGenAsCastleInfo();
  30. bool asCastle;
  31. ui32 identifier;//h3m internal identifier
  32. std::vector<bool> allowedFactions;
  33. std::string instanceId;//vcmi map instance identifier
  34. void serializeJson(JsonSerializeFormat & handler) override;
  35. };
  36. class DLL_LINKAGE CCreGenLeveledInfo : public virtual CSpecObjInfo
  37. {
  38. public:
  39. CCreGenLeveledInfo();
  40. ui8 minLevel, maxLevel; //minimal and maximal level of creature in dwelling: <1, 7>
  41. void serializeJson(JsonSerializeFormat & handler) override;
  42. };
  43. class DLL_LINKAGE CCreGenLeveledCastleInfo : public CCreGenAsCastleInfo, public CCreGenLeveledInfo
  44. {
  45. public:
  46. CCreGenLeveledCastleInfo() = default;
  47. void serializeJson(JsonSerializeFormat & handler) override;
  48. };
  49. class DLL_LINKAGE CGDwelling : public CArmedInstance
  50. {
  51. public:
  52. typedef std::vector<std::pair<ui32, std::vector<CreatureID> > > TCreaturesSet;
  53. CSpecObjInfo * info; //random dwelling options; not serialized
  54. TCreaturesSet creatures; //creatures[level] -> <vector of alternative ids (base creature and upgrades, creatures amount>
  55. CGDwelling();
  56. virtual ~CGDwelling();
  57. void initRandomObjectInfo();
  58. protected:
  59. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  60. private:
  61. void initObj(CRandomGenerator & rand) override;
  62. void onHeroVisit(const CGHeroInstance * h) const override;
  63. void newTurn(CRandomGenerator & rand) const override;
  64. void setPropertyDer(ui8 what, ui32 val) override;
  65. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  66. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  67. void updateGuards() const;
  68. void heroAcceptsCreatures(const CGHeroInstance *h) const;
  69. public:
  70. template <typename Handler> void serialize(Handler &h, const int version)
  71. {
  72. h & static_cast<CArmedInstance&>(*this);
  73. h & creatures;
  74. }
  75. };
  76. class DLL_LINKAGE CGTownBuilding : public IObjectInterface
  77. {
  78. ///basic class for town structures handled as map objects
  79. public:
  80. si32 indexOnTV; //identifies its index on towns vector
  81. CGTownInstance *town;
  82. CGTownBuilding() : bType(BuildingSubID::NONE), indexOnTV(0), town(nullptr) {};
  83. STRONG_INLINE
  84. BuildingSubID::EBuildingSubID getBuildingSubtype() const
  85. {
  86. return bType;
  87. }
  88. STRONG_INLINE
  89. const BuildingID & getBuildingType() const
  90. {
  91. return bID;
  92. }
  93. STRONG_INLINE
  94. void setBuildingSubtype(BuildingSubID::EBuildingSubID subId)
  95. {
  96. bType = subId;
  97. }
  98. template <typename Handler> void serialize(Handler &h, const int version)
  99. {
  100. h & bID;
  101. h & indexOnTV;
  102. if(version >= 792)
  103. h & bType;
  104. }
  105. protected:
  106. BuildingID bID; //from buildig list
  107. BuildingSubID::EBuildingSubID bType;
  108. };
  109. class DLL_LINKAGE COPWBonus : public CGTownBuilding
  110. {///used for OPW bonusing structures
  111. public:
  112. std::set<si32> visitors;
  113. void setProperty(ui8 what, ui32 val) override;
  114. void onHeroVisit (const CGHeroInstance * h) const override;
  115. COPWBonus (BuildingID index, BuildingSubID::EBuildingSubID subId, CGTownInstance *TOWN);
  116. COPWBonus () {};
  117. template <typename Handler> void serialize(Handler &h, const int version)
  118. {
  119. h & static_cast<CGTownBuilding&>(*this);
  120. h & visitors;
  121. }
  122. };
  123. class DLL_LINKAGE CTownBonus : public CGTownBuilding
  124. {
  125. ///used for one-time bonusing structures
  126. ///feel free to merge inheritance tree
  127. public:
  128. std::set<ObjectInstanceID> visitors;
  129. void setProperty(ui8 what, ui32 val) override;
  130. void onHeroVisit (const CGHeroInstance * h) const override;
  131. CTownBonus (BuildingID index, BuildingSubID::EBuildingSubID subId, CGTownInstance *TOWN);
  132. CTownBonus () {};
  133. template <typename Handler> void serialize(Handler &h, const int version)
  134. {
  135. h & static_cast<CGTownBuilding&>(*this);
  136. h & visitors;
  137. }
  138. };
  139. class DLL_LINKAGE CTownAndVisitingHero : public CBonusSystemNode
  140. {
  141. public:
  142. CTownAndVisitingHero();
  143. };
  144. struct DLL_LINKAGE GrowthInfo
  145. {
  146. struct Entry
  147. {
  148. int count;
  149. std::string description;
  150. Entry(const std::string &format, int _count);
  151. Entry(int subID, BuildingID building, int _count);
  152. Entry(int _count, const std::string &fullDescription);
  153. };
  154. std::vector<Entry> entries;
  155. int totalGrowth() const;
  156. };
  157. class DLL_LINKAGE CGTownInstance : public CGDwelling, public IShipyard, public IMarket
  158. {
  159. public:
  160. enum EFortLevel {NONE = 0, FORT = 1, CITADEL = 2, CASTLE = 3};
  161. CTownAndVisitingHero townAndVis;
  162. const CTown * town;
  163. std::string name; // name of town
  164. si32 builded; //how many buildings has been built this turn
  165. si32 destroyed; //how many buildings has been destroyed this turn
  166. ConstTransitivePtr<CGHeroInstance> garrisonHero, visitingHero;
  167. ui32 identifier; //special identifier from h3m (only > RoE maps)
  168. si32 alignment;
  169. std::set<BuildingID> forbiddenBuildings, builtBuildings;
  170. std::vector<CGTownBuilding*> bonusingBuildings;
  171. std::vector<SpellID> possibleSpells, obligatorySpells;
  172. std::vector<std::vector<SpellID> > spells; //spells[level] -> vector of spells, first will be available in guild
  173. std::list<CCastleEvent> events;
  174. std::pair<si32, si32> bonusValue;//var to store town bonuses (rampart = resources from mystic pond);
  175. //////////////////////////////////////////////////////////////////////////
  176. static std::vector<const CArtifact *> merchantArtifacts; //vector of artifacts available at Artifact merchant, NULLs possible (for making empty space when artifact is bought)
  177. static std::vector<int> universitySkills;//skills for university of magic
  178. template <typename Handler> void serialize(Handler &h, const int version)
  179. {
  180. h & static_cast<CGDwelling&>(*this);
  181. h & static_cast<IShipyard&>(*this);
  182. h & static_cast<IMarket&>(*this);
  183. h & name;
  184. h & builded;
  185. h & destroyed;
  186. h & identifier;
  187. h & garrisonHero;
  188. h & visitingHero;
  189. h & alignment;
  190. h & forbiddenBuildings;
  191. h & builtBuildings;
  192. h & bonusValue;
  193. h & possibleSpells;
  194. h & obligatorySpells;
  195. h & spells;
  196. h & events;
  197. h & bonusingBuildings;
  198. for (std::vector<CGTownBuilding*>::iterator i = bonusingBuildings.begin(); i!=bonusingBuildings.end(); i++)
  199. (*i)->town = this;
  200. h & town;
  201. h & townAndVis;
  202. BONUS_TREE_DESERIALIZATION_FIX
  203. vstd::erase_if(builtBuildings, [this](BuildingID building) -> bool
  204. {
  205. if(!town->buildings.count(building) || !town->buildings.at(building))
  206. {
  207. logGlobal->error("#1444-like issue in CGTownInstance::serialize. From town %s at %s removing the bogus builtBuildings item %s", name, pos.toString(), building);
  208. return true;
  209. }
  210. return false;
  211. });
  212. if(!h.saving && version < 792)
  213. updateBonusingBuildings();
  214. }
  215. //////////////////////////////////////////////////////////////////////////
  216. CBonusSystemNode *whatShouldBeAttached() override;
  217. std::string nodeName() const override;
  218. void updateMoraleBonusFromArmy() override;
  219. void deserializationFix();
  220. void recreateBuildingsBonuses();
  221. ///bid: param to bind a building with a bonus, subId: param to check if already built
  222. bool addBonusIfBuilt(BuildingSubID::EBuildingSubID subId, Bonus::BonusType type, int val, TPropagatorPtr & prop, int subtype = -1);
  223. bool addBonusIfBuilt(BuildingSubID::EBuildingSubID subId, Bonus::BonusType type, int val, int subtype = -1);
  224. 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
  225. bool addBonusIfBuilt(BuildingID building, Bonus::BonusType type, int val, int subtype = -1); //convienence version of above
  226. void setVisitingHero(CGHeroInstance *h);
  227. void setGarrisonedHero(CGHeroInstance *h);
  228. const CArmedInstance *getUpperArmy() const; //garrisoned hero if present or the town itself
  229. //////////////////////////////////////////////////////////////////////////
  230. bool passableFor(PlayerColor color) const override;
  231. //int3 getSightCenter() const override; //"center" tile from which the sight distance is calculated
  232. int getSightRadius() const override; //returns sight distance
  233. int getBoatType() const override; //0 - evil (if a ship can be evil...?), 1 - good, 2 - neutral
  234. void getOutOffsets(std::vector<int3> &offsets) const override; //offsets to obj pos when we boat can be placed. Parameter will be cleared
  235. int getMarketEfficiency() const override; //=market count
  236. bool allowsTrade(EMarketMode::EMarketMode mode) const override;
  237. std::vector<int> availableItemsIds(EMarketMode::EMarketMode mode) const override;
  238. void setType(si32 ID, si32 subID) override;
  239. void updateAppearance();
  240. //////////////////////////////////////////////////////////////////////////
  241. bool needsLastStack() const override;
  242. CGTownInstance::EFortLevel fortLevel() const;
  243. int hallLevel() const; // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  244. int mageGuildLevel() const; // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  245. int getHordeLevel(const int & HID) const; //HID - 0 or 1; returns creature level or -1 if that horde structure is not present
  246. int creatureGrowth(const int & level) const;
  247. GrowthInfo getGrowthInfo(int level) const;
  248. bool hasFort() const;
  249. bool hasCapitol() const;
  250. const CGTownBuilding * getBonusingBuilding(BuildingSubID::EBuildingSubID subId) const;
  251. bool hasBuiltSomeTradeBuilding() const;
  252. //checks if special building with type buildingID is constructed
  253. bool hasBuilt(BuildingSubID::EBuildingSubID buildingID) const;
  254. //checks if building is constructed and town has same subID
  255. bool hasBuilt(BuildingID buildingID) const;
  256. bool hasBuilt(BuildingID buildingID, int townID) const;
  257. TResources getBuildingCost(BuildingID buildingID) const;
  258. TResources dailyIncome() const; //calculates daily income of this town
  259. int spellsAtLevel(int level, bool checkGuild) const; //levels are counted from 1 (1 - 5)
  260. bool armedGarrison() const; //true if town has creatures in garrison or garrisoned hero
  261. int getTownLevel() const;
  262. CBuilding::TRequired genBuildingRequirements(BuildingID build, bool deep = false) const;
  263. void mergeGarrisonOnSiege() const; // merge garrison into army of visiting hero
  264. void removeCapitols (PlayerColor owner) const;
  265. void clearArmy() const;
  266. void addHeroToStructureVisitors(const CGHeroInstance *h, si64 structureInstanceID) const; //hero must be visiting or garrisoned in town
  267. const CTown * getTown() const ;
  268. CGTownInstance();
  269. virtual ~CGTownInstance();
  270. ///IObjectInterface overrides
  271. void newTurn(CRandomGenerator & rand) const override;
  272. void onHeroVisit(const CGHeroInstance * h) const override;
  273. void onHeroLeave(const CGHeroInstance * h) const override;
  274. void initObj(CRandomGenerator & rand) override;
  275. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  276. std::string getObjectName() const override;
  277. void afterAddToMap(CMap * map) override;
  278. static void reset();
  279. protected:
  280. static TPropagatorPtr emptyPropagator;
  281. void setPropertyDer(ui8 what, ui32 val) override;
  282. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  283. private:
  284. int getDwellingBonus(const std::vector<CreatureID>& creatureIds, const std::vector<ConstTransitivePtr<CGDwelling> >& dwellings) const;
  285. void updateBonusingBuildings();
  286. bool hasBuiltInOldWay(ETownType::ETownType type, BuildingID bid) const;
  287. bool addBonusImpl(BuildingID building, Bonus::BonusType type, int val, TPropagatorPtr & prop, const std::string & description, int subtype = -1);
  288. bool townEnvisagesBuilding(BuildingSubID::EBuildingSubID bid) const;
  289. void tryAddOnePerWeekBonus(BuildingSubID::EBuildingSubID subID);
  290. void tryAddVisitingBonus(BuildingSubID::EBuildingSubID subID);
  291. void addTownBonuses();
  292. };