CGTownInstance.h 9.7 KB

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