CGTownInstance.h 8.6 KB

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