CGTownInstance.h 9.5 KB

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