CGTownInstance.h 12 KB

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