CGTownInstance.h 8.7 KB

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