CGTownInstance.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. PlayerColor getOwner() const override;
  99. int32_t getObjGroupIndex() const override;
  100. int32_t getObjTypeIndex() const override;
  101. int3 visitablePos() const override;
  102. int3 getPosition() const override;
  103. template <typename Handler> void serialize(Handler &h, const int version)
  104. {
  105. h & bID;
  106. h & indexOnTV;
  107. h & bType;
  108. }
  109. protected:
  110. BuildingID bID; //from buildig list
  111. BuildingSubID::EBuildingSubID bType;
  112. const std::string getVisitingBonusGreeting() const;
  113. const std::string getCustomBonusGreeting(const Bonus & bonus) const;
  114. };
  115. class DLL_LINKAGE COPWBonus : public CGTownBuilding
  116. {///used for OPW bonusing structures
  117. public:
  118. std::set<si32> visitors;
  119. void setProperty(ui8 what, ui32 val) override;
  120. void onHeroVisit (const CGHeroInstance * h) const override;
  121. COPWBonus (BuildingID index, BuildingSubID::EBuildingSubID subId, CGTownInstance *TOWN);
  122. COPWBonus () {};
  123. template <typename Handler> void serialize(Handler &h, const int version)
  124. {
  125. h & static_cast<CGTownBuilding&>(*this);
  126. h & visitors;
  127. }
  128. };
  129. class DLL_LINKAGE CTownBonus : public CGTownBuilding
  130. {
  131. ///used for one-time bonusing structures
  132. ///feel free to merge inheritance tree
  133. public:
  134. std::set<ObjectInstanceID> visitors;
  135. void setProperty(ui8 what, ui32 val) override;
  136. void onHeroVisit (const CGHeroInstance * h) const override;
  137. CTownBonus (BuildingID index, BuildingSubID::EBuildingSubID subId, CGTownInstance *TOWN);
  138. CTownBonus () {};
  139. template <typename Handler> void serialize(Handler &h, const int version)
  140. {
  141. h & static_cast<CGTownBuilding&>(*this);
  142. h & visitors;
  143. }
  144. private:
  145. void applyBonuses(CGHeroInstance * h, const BonusList & bonuses) const;
  146. };
  147. class DLL_LINKAGE CTownAndVisitingHero : public CBonusSystemNode
  148. {
  149. public:
  150. CTownAndVisitingHero();
  151. };
  152. struct DLL_LINKAGE GrowthInfo
  153. {
  154. struct Entry
  155. {
  156. int count;
  157. std::string description;
  158. Entry(const std::string &format, int _count);
  159. Entry(int subID, BuildingID building, int _count);
  160. Entry(int _count, const std::string &fullDescription);
  161. };
  162. std::vector<Entry> entries;
  163. int totalGrowth() const;
  164. };
  165. class DLL_LINKAGE CGTownInstance : public CGDwelling, public IShipyard, public IMarket
  166. {
  167. public:
  168. enum EFortLevel {NONE = 0, FORT = 1, CITADEL = 2, CASTLE = 3};
  169. CTownAndVisitingHero townAndVis;
  170. const CTown * town;
  171. std::string name; // name of town
  172. si32 builded; //how many buildings has been built this turn
  173. si32 destroyed; //how many buildings has been destroyed this turn
  174. ConstTransitivePtr<CGHeroInstance> garrisonHero, visitingHero;
  175. ui32 identifier; //special identifier from h3m (only > RoE maps)
  176. si32 alignment;
  177. std::set<BuildingID> forbiddenBuildings;
  178. std::set<BuildingID> builtBuildings;
  179. std::set<BuildingID> overriddenBuildings; ///buildings which bonuses are overridden and should not be applied
  180. std::vector<CGTownBuilding*> bonusingBuildings;
  181. std::vector<SpellID> possibleSpells, obligatorySpells;
  182. std::vector<std::vector<SpellID> > spells; //spells[level] -> vector of spells, first will be available in guild
  183. std::list<CCastleEvent> events;
  184. std::pair<si32, si32> bonusValue;//var to store town bonuses (rampart = resources from mystic pond);
  185. //////////////////////////////////////////////////////////////////////////
  186. static std::vector<const CArtifact *> merchantArtifacts; //vector of artifacts available at Artifact merchant, NULLs possible (for making empty space when artifact is bought)
  187. static std::vector<int> universitySkills;//skills for university of magic
  188. template <typename Handler> void serialize(Handler &h, const int version)
  189. {
  190. h & static_cast<CGDwelling&>(*this);
  191. h & static_cast<IShipyard&>(*this);
  192. h & static_cast<IMarket&>(*this);
  193. h & name;
  194. h & builded;
  195. h & destroyed;
  196. h & identifier;
  197. h & garrisonHero;
  198. h & visitingHero;
  199. h & alignment;
  200. h & forbiddenBuildings;
  201. h & builtBuildings;
  202. h & bonusValue;
  203. h & possibleSpells;
  204. h & obligatorySpells;
  205. h & spells;
  206. h & events;
  207. h & bonusingBuildings;
  208. for(auto * bonusingBuilding : bonusingBuildings)
  209. bonusingBuilding->town = this;
  210. h & town;
  211. h & townAndVis;
  212. BONUS_TREE_DESERIALIZATION_FIX
  213. vstd::erase_if(builtBuildings, [this](BuildingID building) -> bool
  214. {
  215. if(!town->buildings.count(building) || !town->buildings.at(building))
  216. {
  217. logGlobal->error("#1444-like issue in CGTownInstance::serialize. From town %s at %s removing the bogus builtBuildings item %s", name, pos.toString(), building);
  218. return true;
  219. }
  220. return false;
  221. });
  222. h & overriddenBuildings;
  223. if(!h.saving)
  224. this->setNodeType(CBonusSystemNode::TOWN);
  225. }
  226. //////////////////////////////////////////////////////////////////////////
  227. CBonusSystemNode *whatShouldBeAttached() override;
  228. std::string nodeName() const override;
  229. void updateMoraleBonusFromArmy() override;
  230. void deserializationFix();
  231. void recreateBuildingsBonuses();
  232. void setVisitingHero(CGHeroInstance *h);
  233. void setGarrisonedHero(CGHeroInstance *h);
  234. const CArmedInstance *getUpperArmy() const; //garrisoned hero if present or the town itself
  235. //////////////////////////////////////////////////////////////////////////
  236. bool passableFor(PlayerColor color) const override;
  237. //int3 getSightCenter() const override; //"center" tile from which the sight distance is calculated
  238. int getSightRadius() const override; //returns sight distance
  239. int getBoatType() const override; //0 - evil (if a ship can be evil...?), 1 - good, 2 - neutral
  240. void getOutOffsets(std::vector<int3> &offsets) const override; //offsets to obj pos when we boat can be placed. Parameter will be cleared
  241. int getMarketEfficiency() const override; //=market count
  242. bool allowsTrade(EMarketMode::EMarketMode mode) const override;
  243. std::vector<int> availableItemsIds(EMarketMode::EMarketMode mode) const override;
  244. void setType(si32 ID, si32 subID) override;
  245. void updateAppearance();
  246. //////////////////////////////////////////////////////////////////////////
  247. bool needsLastStack() const override;
  248. CGTownInstance::EFortLevel fortLevel() const;
  249. int hallLevel() const; // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  250. int mageGuildLevel() const; // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  251. int getHordeLevel(const int & HID) const; //HID - 0 or 1; returns creature level or -1 if that horde structure is not present
  252. int creatureGrowth(const int & level) const;
  253. GrowthInfo getGrowthInfo(int level) const;
  254. bool hasFort() const;
  255. bool hasCapitol() const;
  256. const CGTownBuilding * getBonusingBuilding(BuildingSubID::EBuildingSubID subId) const;
  257. bool hasBuiltSomeTradeBuilding() const;
  258. //checks if special building with type buildingID is constructed
  259. bool hasBuilt(BuildingSubID::EBuildingSubID buildingID) const;
  260. //checks if building is constructed and town has same subID
  261. bool hasBuilt(BuildingID buildingID) const;
  262. bool hasBuilt(BuildingID buildingID, int townID) const;
  263. TResources getBuildingCost(BuildingID buildingID) const;
  264. TResources dailyIncome() const; //calculates daily income of this town
  265. int spellsAtLevel(int level, bool checkGuild) const; //levels are counted from 1 (1 - 5)
  266. bool armedGarrison() const; //true if town has creatures in garrison or garrisoned hero
  267. int getTownLevel() const;
  268. CBuilding::TRequired genBuildingRequirements(BuildingID build, bool deep = false) const;
  269. void mergeGarrisonOnSiege() const; // merge garrison into army of visiting hero
  270. void removeCapitols (PlayerColor owner) const;
  271. void clearArmy() const;
  272. void addHeroToStructureVisitors(const CGHeroInstance *h, si64 structureInstanceID) const; //hero must be visiting or garrisoned in town
  273. void deleteTownBonus(BuildingID::EBuildingID bid);
  274. const CTown * getTown() const ;
  275. CGTownInstance();
  276. virtual ~CGTownInstance();
  277. ///IObjectInterface overrides
  278. void newTurn(CRandomGenerator & rand) const override;
  279. void onHeroVisit(const CGHeroInstance * h) const override;
  280. void onHeroLeave(const CGHeroInstance * h) const override;
  281. void initObj(CRandomGenerator & rand) override;
  282. void battleFinished(const CGHeroInstance * hero, const BattleResult & result) const override;
  283. std::string getObjectName() const override;
  284. void afterAddToMap(CMap * map) override;
  285. static void reset();
  286. inline bool isBattleOutsideTown(const CGHeroInstance * defendingHero) const
  287. {
  288. return defendingHero && garrisonHero && defendingHero != garrisonHero;
  289. }
  290. protected:
  291. void setPropertyDer(ui8 what, ui32 val) override;
  292. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  293. private:
  294. void setOwner(const PlayerColor owner) const;
  295. void onTownCaptured(const PlayerColor winner) const;
  296. int getDwellingBonus(const std::vector<CreatureID>& creatureIds, const std::vector<ConstTransitivePtr<CGDwelling> >& dwellings) const;
  297. bool hasBuiltInOldWay(ETownType::ETownType type, BuildingID bid) const;
  298. bool townEnvisagesBuilding(BuildingSubID::EBuildingSubID bid) const;
  299. bool isBonusingBuildingAdded(BuildingID::EBuildingID bid) const;
  300. void tryAddOnePerWeekBonus(BuildingSubID::EBuildingSubID subID);
  301. void tryAddVisitingBonus(BuildingSubID::EBuildingSubID subID);
  302. void initOverriddenBids();
  303. void addTownBonuses();
  304. };