CGTownInstance.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 "IMarket.h"
  12. #include "CGDwelling.h"
  13. #include "../entities/faction/CFaction.h" // TODO: remove
  14. #include "../entities/faction/CTown.h" // TODO: remove
  15. VCMI_LIB_NAMESPACE_BEGIN
  16. class CCastleEvent;
  17. class CTown;
  18. class TownBuildingInstance;
  19. struct TownFortifications;
  20. class TownRewardableBuildingInstance;
  21. struct DamageRange;
  22. template<typename ContainedClass>
  23. class LogicalExpression;
  24. class DLL_LINKAGE CTownAndVisitingHero : public CBonusSystemNode
  25. {
  26. public:
  27. CTownAndVisitingHero();
  28. };
  29. struct DLL_LINKAGE GrowthInfo
  30. {
  31. struct Entry
  32. {
  33. int count;
  34. std::string description;
  35. Entry(const std::string &format, int _count);
  36. Entry(int subID, const BuildingID & building, int _count);
  37. Entry(int _count, std::string fullDescription);
  38. };
  39. std::vector<Entry> entries;
  40. int totalGrowth() const;
  41. int handicapPercentage;
  42. };
  43. class DLL_LINKAGE CGTownInstance : public CGDwelling, public IShipyard, public IMarket, public INativeTerrainProvider, public ICreatureUpgrader
  44. {
  45. friend class CTownInstanceConstructor;
  46. std::string nameTextId; // name of town
  47. std::map<BuildingID, TownRewardableBuildingInstance*> convertOldBuildings(std::vector<TownRewardableBuildingInstance*> oldVector);
  48. std::set<BuildingID> builtBuildings;
  49. public:
  50. enum EFortLevel {NONE = 0, FORT = 1, CITADEL = 2, CASTLE = 3};
  51. CTownAndVisitingHero townAndVis;
  52. si32 built; //how many buildings has been built this turn
  53. si32 destroyed; //how many buildings has been destroyed this turn
  54. ConstTransitivePtr<CGHeroInstance> garrisonHero, visitingHero;
  55. ui32 identifier; //special identifier from h3m (only > RoE maps)
  56. PlayerColor alignmentToPlayer; // if set to non-neutral, random town will have same faction as specified player
  57. std::set<BuildingID> forbiddenBuildings;
  58. std::map<BuildingID, TownRewardableBuildingInstance*> rewardableBuildings;
  59. std::vector<SpellID> possibleSpells, obligatorySpells;
  60. std::vector<std::vector<SpellID> > spells; //spells[level] -> vector of spells, first will be available in guild
  61. std::vector<CCastleEvent> events;
  62. std::pair<si32, si32> bonusValue;//var to store town bonuses (rampart = resources from mystic pond, factory = save debts);
  63. int spellResearchCounterDay;
  64. int spellResearchAcceptedCounter;
  65. bool spellResearchAllowed;
  66. //////////////////////////////////////////////////////////////////////////
  67. template <typename Handler> void serialize(Handler &h)
  68. {
  69. h & static_cast<CGDwelling&>(*this);
  70. h & nameTextId;
  71. h & built;
  72. h & destroyed;
  73. h & identifier;
  74. h & garrisonHero;
  75. h & visitingHero;
  76. h & alignmentToPlayer;
  77. h & forbiddenBuildings;
  78. h & builtBuildings;
  79. h & bonusValue;
  80. h & possibleSpells;
  81. h & obligatorySpells;
  82. h & spells;
  83. h & events;
  84. if (h.version >= Handler::Version::SPELL_RESEARCH)
  85. {
  86. h & spellResearchCounterDay;
  87. h & spellResearchAcceptedCounter;
  88. h & spellResearchAllowed;
  89. }
  90. if (h.version >= Handler::Version::NEW_TOWN_BUILDINGS)
  91. {
  92. h & rewardableBuildings;
  93. }
  94. else
  95. {
  96. std::vector<TownRewardableBuildingInstance*> oldVector;
  97. h & oldVector;
  98. rewardableBuildings = convertOldBuildings(oldVector);
  99. }
  100. if (h.version < Handler::Version::REMOVE_TOWN_PTR)
  101. {
  102. FactionID faction;
  103. bool isNull = false;
  104. h & isNull;
  105. if (!isNull)
  106. h & faction;
  107. }
  108. h & townAndVis;
  109. BONUS_TREE_DESERIALIZATION_FIX
  110. if (h.version < Handler::Version::NEW_TOWN_BUILDINGS)
  111. {
  112. std::set<BuildingID> overriddenBuildings;
  113. h & overriddenBuildings;
  114. }
  115. if(!h.saving)
  116. postDeserialize();
  117. }
  118. //////////////////////////////////////////////////////////////////////////
  119. CBonusSystemNode & whatShouldBeAttached() override;
  120. std::string nodeName() const override;
  121. void updateMoraleBonusFromArmy() override;
  122. void deserializationFix();
  123. void postDeserialize();
  124. void recreateBuildingsBonuses();
  125. void setVisitingHero(CGHeroInstance *h);
  126. void setGarrisonedHero(CGHeroInstance *h);
  127. const CArmedInstance *getUpperArmy() const; //garrisoned hero if present or the town itself
  128. std::string getNameTranslated() const;
  129. std::string getNameTextID() const;
  130. void setNameTextId(const std::string & newName);
  131. //////////////////////////////////////////////////////////////////////////
  132. bool passableFor(PlayerColor color) const override;
  133. //int3 getSightCenter() const override; //"center" tile from which the sight distance is calculated
  134. int getSightRadius() const override; //returns sight distance
  135. BoatId getBoatType() const override; //0 - evil (if a ship can be evil...?), 1 - good, 2 - neutral
  136. void getOutOffsets(std::vector<int3> &offsets) const override; //offsets to obj pos when we boat can be placed. Parameter will be cleared
  137. EGeneratorState shipyardStatus() const override;
  138. const IObjectInterface * getObject() const override;
  139. int getMarketEfficiency() const override; //=market count
  140. std::set<EMarketMode> availableModes() const override;
  141. std::vector<TradeItemBuy> availableItemsIds(EMarketMode mode) const override;
  142. ObjectInstanceID getObjInstanceID() const override;
  143. void updateAppearance();
  144. //////////////////////////////////////////////////////////////////////////
  145. bool needsLastStack() const override;
  146. CGTownInstance::EFortLevel fortLevel() const;
  147. TownFortifications fortificationsLevel() const;
  148. int hallLevel() const; // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  149. int mageGuildLevel() const; // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  150. int getHordeLevel(const int & HID) const; //HID - 0 or 1; returns creature level or -1 if that horde structure is not present
  151. int creatureGrowth(const int & level) const;
  152. GrowthInfo getGrowthInfo(int level) const;
  153. bool hasFort() const;
  154. bool hasCapitol() const;
  155. bool hasBuiltSomeTradeBuilding() const;
  156. //checks if special building with type buildingID is constructed
  157. bool hasBuilt(BuildingSubID::EBuildingSubID buildingID) const;
  158. //checks if building is constructed and town has same subID
  159. bool hasBuilt(const BuildingID & buildingID) const;
  160. bool hasBuilt(const BuildingID & buildingID, FactionID townID) const;
  161. void addBuilding(const BuildingID & buildingID);
  162. void removeBuilding(const BuildingID & buildingID);
  163. void removeAllBuildings();
  164. std::set<BuildingID> getBuildings() const;
  165. TResources getBuildingCost(const BuildingID & buildingID) const;
  166. ResourceSet dailyIncome() const override;
  167. std::vector<CreatureID> providedCreatures() const override;
  168. int spellsAtLevel(int level, bool checkGuild) const; //levels are counted from 1 (1 - 5)
  169. bool armedGarrison() const; //true if town has creatures in garrison or garrisoned hero
  170. int getTownLevel() const;
  171. LogicalExpression<BuildingID> genBuildingRequirements(const BuildingID & build, bool deep = false) const;
  172. void mergeGarrisonOnSiege() const; // merge garrison into army of visiting hero
  173. void removeCapitols(const PlayerColor & owner) const;
  174. void clearArmy() const;
  175. void addHeroToStructureVisitors(const CGHeroInstance *h, si64 structureInstanceID) const; //hero must be visiting or garrisoned in town
  176. void deleteTownBonus(BuildingID bid);
  177. /// Returns damage range for secondary towers of this town
  178. DamageRange getTowerDamageRange() const;
  179. /// Returns damage range for central tower(keep) of this town
  180. DamageRange getKeepDamageRange() const;
  181. const CTown * getTown() const;
  182. const CFaction * getFaction() const;
  183. /// INativeTerrainProvider
  184. FactionID getFactionID() const override;
  185. TerrainId getNativeTerrain() const override;
  186. /// Returns ID of war machine that is produced by specified building or NONE if this is not built or if building does not produce war machines
  187. ArtifactID getWarMachineInBuilding(BuildingID) const;
  188. /// Returns true if provided war machine is available in any of built buildings of this town
  189. bool isWarMachineAvailable(ArtifactID) const;
  190. CGTownInstance(IGameCallback *cb);
  191. virtual ~CGTownInstance();
  192. ///IObjectInterface overrides
  193. void newTurn(vstd::RNG & rand) const override;
  194. void onHeroVisit(const CGHeroInstance * h) const override;
  195. void onHeroLeave(const CGHeroInstance * h) const override;
  196. void initObj(vstd::RNG & rand) override;
  197. void pickRandomObject(vstd::RNG & rand) override;
  198. void battleFinished(const CGHeroInstance * hero, const BattleResult & result) const override;
  199. std::string getObjectName() const override;
  200. void fillUpgradeInfo(UpgradeInfo & info, const CStackInstance &stack) const override;
  201. void afterAddToMap(CMap * map) override;
  202. void afterRemoveFromMap(CMap * map) override;
  203. inline bool isBattleOutsideTown(const CGHeroInstance * defendingHero) const
  204. {
  205. return defendingHero && garrisonHero && defendingHero != garrisonHero;
  206. }
  207. protected:
  208. void setPropertyDer(ObjProperty what, ObjPropertyID identifier) override;
  209. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  210. private:
  211. FactionID randomizeFaction(vstd::RNG & rand);
  212. void setOwner(const PlayerColor & owner) const;
  213. void onTownCaptured(const PlayerColor & winner) const;
  214. int getDwellingBonus(const std::vector<CreatureID>& creatureIds, const std::vector<const CGObjectInstance* >& dwellings) const;
  215. bool townEnvisagesBuilding(BuildingSubID::EBuildingSubID bid) const;
  216. void initializeConfigurableBuildings(vstd::RNG & rand);
  217. void initializeNeutralTownGarrison(vstd::RNG & rand);
  218. };
  219. VCMI_LIB_NAMESPACE_END