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