CGTownInstance.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. VCMI_LIB_NAMESPACE_BEGIN
  16. class CCastleEvent;
  17. class CGTownInstance;
  18. class CGDwelling;
  19. struct DamageRange;
  20. class DLL_LINKAGE CSpecObjInfo
  21. {
  22. public:
  23. CSpecObjInfo();
  24. virtual ~CSpecObjInfo() = default;
  25. virtual void serializeJson(JsonSerializeFormat & handler) = 0;
  26. const CGDwelling * owner;
  27. };
  28. class DLL_LINKAGE CCreGenAsCastleInfo : public virtual CSpecObjInfo
  29. {
  30. public:
  31. bool asCastle = false;
  32. ui32 identifier = 0;//h3m internal identifier
  33. std::vector<bool> allowedFactions;
  34. std::string instanceId;//vcmi map instance identifier
  35. void serializeJson(JsonSerializeFormat & handler) override;
  36. };
  37. class DLL_LINKAGE CCreGenLeveledInfo : public virtual CSpecObjInfo
  38. {
  39. public:
  40. ui8 minLevel = 0;
  41. ui8 maxLevel = 7; //minimal and maximal level of creature in dwelling: <1, 7>
  42. void serializeJson(JsonSerializeFormat & handler) override;
  43. };
  44. class DLL_LINKAGE CCreGenLeveledCastleInfo : public CCreGenAsCastleInfo, public CCreGenLeveledInfo
  45. {
  46. public:
  47. CCreGenLeveledCastleInfo() = default;
  48. void serializeJson(JsonSerializeFormat & handler) override;
  49. };
  50. class DLL_LINKAGE CGDwelling : public CArmedInstance
  51. {
  52. public:
  53. typedef std::vector<std::pair<ui32, std::vector<CreatureID> > > TCreaturesSet;
  54. CSpecObjInfo * info; //random dwelling options; not serialized
  55. TCreaturesSet creatures; //creatures[level] -> <vector of alternative ids (base creature and upgrades, creatures amount>
  56. CGDwelling();
  57. ~CGDwelling() override;
  58. void initRandomObjectInfo();
  59. protected:
  60. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  61. private:
  62. void initObj(CRandomGenerator & rand) override;
  63. void onHeroVisit(const CGHeroInstance * h) const override;
  64. void newTurn(CRandomGenerator & rand) const override;
  65. void setPropertyDer(ui8 what, ui32 val) override;
  66. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  67. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  68. void updateGuards() const;
  69. void heroAcceptsCreatures(const CGHeroInstance *h) const;
  70. public:
  71. template <typename Handler> void serialize(Handler &h, const int version)
  72. {
  73. h & static_cast<CArmedInstance&>(*this);
  74. h & creatures;
  75. }
  76. };
  77. class DLL_LINKAGE CGTownBuilding : public IObjectInterface
  78. {
  79. ///basic class for town structures handled as map objects
  80. public:
  81. si32 indexOnTV = 0; //identifies its index on towns vector
  82. CGTownInstance *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 = BuildingSubID::NONE;
  112. std::string getVisitingBonusGreeting() const;
  113. 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(const BuildingID & index, BuildingSubID::EBuildingSubID subId, CGTownInstance * TOWN);
  122. COPWBonus() = default;
  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(const BuildingID & index, BuildingSubID::EBuildingSubID subId, CGTownInstance * TOWN);
  138. CTownBonus() = default;
  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, const BuildingID & building, int _count);
  160. Entry(int _count, 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, public INativeTerrainProvider
  166. {
  167. std::string name; // name of town
  168. public:
  169. enum EFortLevel {NONE = 0, FORT = 1, CITADEL = 2, CASTLE = 3};
  170. CTownAndVisitingHero townAndVis;
  171. const CTown * 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. PlayerColor alignmentToPlayer; // if set to non-neutral, random town will have same faction as specified player
  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 & alignmentToPlayer;
  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. if(town)
  214. {
  215. vstd::erase_if(builtBuildings, [this](BuildingID building) -> bool
  216. {
  217. if(!town->buildings.count(building) || !town->buildings.at(building))
  218. {
  219. logGlobal->error("#1444-like issue in CGTownInstance::serialize. From town %s at %s removing the bogus builtBuildings item %s", name, pos.toString(), building);
  220. return true;
  221. }
  222. return false;
  223. });
  224. }
  225. h & overriddenBuildings;
  226. if(!h.saving)
  227. this->setNodeType(CBonusSystemNode::TOWN);
  228. }
  229. //////////////////////////////////////////////////////////////////////////
  230. CBonusSystemNode & whatShouldBeAttached() override;
  231. std::string nodeName() const override;
  232. void updateMoraleBonusFromArmy() override;
  233. void deserializationFix();
  234. void recreateBuildingsBonuses();
  235. void setVisitingHero(CGHeroInstance *h);
  236. void setGarrisonedHero(CGHeroInstance *h);
  237. const CArmedInstance *getUpperArmy() const; //garrisoned hero if present or the town itself
  238. std::string getNameTranslated() const;
  239. void setNameTranslated(const std::string & newName);
  240. //////////////////////////////////////////////////////////////////////////
  241. bool passableFor(PlayerColor color) const override;
  242. //int3 getSightCenter() const override; //"center" tile from which the sight distance is calculated
  243. int getSightRadius() const override; //returns sight distance
  244. BoatId getBoatType() const override; //0 - evil (if a ship can be evil...?), 1 - good, 2 - neutral
  245. void getOutOffsets(std::vector<int3> &offsets) const override; //offsets to obj pos when we boat can be placed. Parameter will be cleared
  246. int getMarketEfficiency() const override; //=market count
  247. bool allowsTrade(EMarketMode::EMarketMode mode) const override;
  248. std::vector<int> availableItemsIds(EMarketMode::EMarketMode mode) const override;
  249. void setType(si32 ID, si32 subID) override;
  250. void updateAppearance();
  251. //////////////////////////////////////////////////////////////////////////
  252. bool needsLastStack() const override;
  253. CGTownInstance::EFortLevel fortLevel() const;
  254. int hallLevel() const; // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  255. int mageGuildLevel() const; // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  256. int getHordeLevel(const int & HID) const; //HID - 0 or 1; returns creature level or -1 if that horde structure is not present
  257. int creatureGrowth(const int & level) const;
  258. GrowthInfo getGrowthInfo(int level) const;
  259. bool hasFort() const;
  260. bool hasCapitol() const;
  261. const CGTownBuilding * getBonusingBuilding(BuildingSubID::EBuildingSubID subId) const;
  262. bool hasBuiltSomeTradeBuilding() const;
  263. //checks if special building with type buildingID is constructed
  264. bool hasBuilt(BuildingSubID::EBuildingSubID buildingID) const;
  265. //checks if building is constructed and town has same subID
  266. bool hasBuilt(const BuildingID & buildingID) const;
  267. bool hasBuilt(const BuildingID & buildingID, int townID) const;
  268. TResources getBuildingCost(const BuildingID & buildingID) const;
  269. TResources dailyIncome() const; //calculates daily income of this town
  270. int spellsAtLevel(int level, bool checkGuild) const; //levels are counted from 1 (1 - 5)
  271. bool armedGarrison() const; //true if town has creatures in garrison or garrisoned hero
  272. int getTownLevel() const;
  273. CBuilding::TRequired genBuildingRequirements(const BuildingID & build, bool deep = false) const;
  274. void mergeGarrisonOnSiege() const; // merge garrison into army of visiting hero
  275. void removeCapitols(const PlayerColor & owner) const;
  276. void clearArmy() const;
  277. void addHeroToStructureVisitors(const CGHeroInstance *h, si64 structureInstanceID) const; //hero must be visiting or garrisoned in town
  278. void deleteTownBonus(BuildingID::EBuildingID bid);
  279. /// Returns damage range for secondary towers of this town
  280. DamageRange getTowerDamageRange() const;
  281. /// Returns damage range for central tower(keep) of this town
  282. DamageRange getKeepDamageRange() const;
  283. const CTown * getTown() const;
  284. /// INativeTerrainProvider
  285. FactionID getFaction() const override;
  286. TerrainId getNativeTerrain() const override;
  287. CGTownInstance();
  288. virtual ~CGTownInstance();
  289. ///IObjectInterface overrides
  290. void newTurn(CRandomGenerator & rand) const override;
  291. void onHeroVisit(const CGHeroInstance * h) const override;
  292. void onHeroLeave(const CGHeroInstance * h) const override;
  293. void initObj(CRandomGenerator & rand) override;
  294. void battleFinished(const CGHeroInstance * hero, const BattleResult & result) const override;
  295. std::string getObjectName() const override;
  296. void afterAddToMap(CMap * map) override;
  297. void afterRemoveFromMap(CMap * map) override;
  298. static void reset();
  299. inline bool isBattleOutsideTown(const CGHeroInstance * defendingHero) const
  300. {
  301. return defendingHero && garrisonHero && defendingHero != garrisonHero;
  302. }
  303. protected:
  304. void setPropertyDer(ui8 what, ui32 val) override;
  305. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  306. private:
  307. void setOwner(const PlayerColor & owner) const;
  308. void onTownCaptured(const PlayerColor & winner) const;
  309. int getDwellingBonus(const std::vector<CreatureID>& creatureIds, const std::vector<ConstTransitivePtr<CGDwelling> >& dwellings) const;
  310. bool hasBuiltInOldWay(ETownType::ETownType type, const BuildingID & bid) const;
  311. bool townEnvisagesBuilding(BuildingSubID::EBuildingSubID bid) const;
  312. bool isBonusingBuildingAdded(BuildingID::EBuildingID bid) const;
  313. void tryAddOnePerWeekBonus(BuildingSubID::EBuildingSubID subID);
  314. void tryAddVisitingBonus(BuildingSubID::EBuildingSubID subID);
  315. void initOverriddenBids();
  316. void addTownBonuses();
  317. };
  318. VCMI_LIB_NAMESPACE_END