CGTownInstance.h 8.5 KB

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