CGTownInstance.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 "CObjectHandler.h"
  12. #include "CGMarket.h" // For IMarket interface
  13. #include "CArmedInstance.h"
  14. #include "CGDwelling.h"
  15. #include "CGTownBuilding.h"
  16. #include "../CTownHandler.h" // For CTown
  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. };
  38. class DLL_LINKAGE CGTownInstance : public CGDwelling, public IShipyard, public IMarket, public INativeTerrainProvider
  39. {
  40. std::string name; // name of town
  41. public:
  42. enum EFortLevel {NONE = 0, FORT = 1, CITADEL = 2, CASTLE = 3};
  43. CTownAndVisitingHero townAndVis;
  44. const CTown * town;
  45. si32 builded; //how many buildings has been built this turn
  46. si32 destroyed; //how many buildings has been destroyed this turn
  47. ConstTransitivePtr<CGHeroInstance> garrisonHero, visitingHero;
  48. ui32 identifier; //special identifier from h3m (only > RoE maps)
  49. PlayerColor alignmentToPlayer; // if set to non-neutral, random town will have same faction as specified player
  50. std::set<BuildingID> forbiddenBuildings;
  51. std::set<BuildingID> builtBuildings;
  52. std::set<BuildingID> overriddenBuildings; ///buildings which bonuses are overridden and should not be applied
  53. std::vector<CGTownBuilding*> bonusingBuildings;
  54. std::vector<SpellID> possibleSpells, obligatorySpells;
  55. std::vector<std::vector<SpellID> > spells; //spells[level] -> vector of spells, first will be available in guild
  56. std::list<CCastleEvent> events;
  57. std::pair<si32, si32> bonusValue;//var to store town bonuses (rampart = resources from mystic pond);
  58. //////////////////////////////////////////////////////////////////////////
  59. static std::vector<const CArtifact *> merchantArtifacts; //vector of artifacts available at Artifact merchant, NULLs possible (for making empty space when artifact is bought)
  60. static std::vector<int> universitySkills;//skills for university of magic
  61. template <typename Handler> void serialize(Handler &h, const int version)
  62. {
  63. h & static_cast<CGDwelling&>(*this);
  64. h & static_cast<IShipyard&>(*this);
  65. h & name;
  66. h & builded;
  67. h & destroyed;
  68. h & identifier;
  69. h & garrisonHero;
  70. h & visitingHero;
  71. h & alignmentToPlayer;
  72. h & forbiddenBuildings;
  73. h & builtBuildings;
  74. h & bonusValue;
  75. h & possibleSpells;
  76. h & obligatorySpells;
  77. h & spells;
  78. h & events;
  79. h & bonusingBuildings;
  80. h & town;
  81. h & townAndVis;
  82. BONUS_TREE_DESERIALIZATION_FIX
  83. if(town)
  84. {
  85. vstd::erase_if(builtBuildings, [this](BuildingID building) -> bool
  86. {
  87. if(!town->buildings.count(building) || !town->buildings.at(building))
  88. {
  89. logGlobal->error("#1444-like issue in CGTownInstance::serialize. From town %s at %s removing the bogus builtBuildings item %s", name, pos.toString(), building);
  90. return true;
  91. }
  92. return false;
  93. });
  94. }
  95. h & overriddenBuildings;
  96. if(!h.saving)
  97. this->setNodeType(CBonusSystemNode::TOWN);
  98. }
  99. //////////////////////////////////////////////////////////////////////////
  100. CBonusSystemNode & whatShouldBeAttached() override;
  101. std::string nodeName() const override;
  102. void updateMoraleBonusFromArmy() override;
  103. void deserializationFix();
  104. void recreateBuildingsBonuses();
  105. void setVisitingHero(CGHeroInstance *h);
  106. void setGarrisonedHero(CGHeroInstance *h);
  107. const CArmedInstance *getUpperArmy() const; //garrisoned hero if present or the town itself
  108. std::string getNameTranslated() const;
  109. void setNameTranslated(const std::string & newName);
  110. //////////////////////////////////////////////////////////////////////////
  111. bool passableFor(PlayerColor color) const override;
  112. //int3 getSightCenter() const override; //"center" tile from which the sight distance is calculated
  113. int getSightRadius() const override; //returns sight distance
  114. BoatId getBoatType() const override; //0 - evil (if a ship can be evil...?), 1 - good, 2 - neutral
  115. void getOutOffsets(std::vector<int3> &offsets) const override; //offsets to obj pos when we boat can be placed. Parameter will be cleared
  116. int getMarketEfficiency() const override; //=market count
  117. bool allowsTrade(EMarketMode::EMarketMode mode) const override;
  118. std::vector<int> availableItemsIds(EMarketMode::EMarketMode mode) const override;
  119. void setType(si32 ID, si32 subID) override;
  120. void updateAppearance();
  121. //////////////////////////////////////////////////////////////////////////
  122. bool needsLastStack() const override;
  123. CGTownInstance::EFortLevel fortLevel() 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. const CGTownBuilding * getBonusingBuilding(BuildingSubID::EBuildingSubID subId) const;
  132. bool hasBuiltSomeTradeBuilding() const;
  133. //checks if special building with type buildingID is constructed
  134. bool hasBuilt(BuildingSubID::EBuildingSubID buildingID) const;
  135. //checks if building is constructed and town has same subID
  136. bool hasBuilt(const BuildingID & buildingID) const;
  137. bool hasBuilt(const BuildingID & buildingID, int townID) const;
  138. TResources getBuildingCost(const BuildingID & buildingID) const;
  139. TResources dailyIncome() const; //calculates daily income of this town
  140. int spellsAtLevel(int level, bool checkGuild) const; //levels are counted from 1 (1 - 5)
  141. bool armedGarrison() const; //true if town has creatures in garrison or garrisoned hero
  142. int getTownLevel() const;
  143. CBuilding::TRequired genBuildingRequirements(const BuildingID & build, bool deep = false) const;
  144. void mergeGarrisonOnSiege() const; // merge garrison into army of visiting hero
  145. void removeCapitols(const PlayerColor & owner) const;
  146. void clearArmy() const;
  147. void addHeroToStructureVisitors(const CGHeroInstance *h, si64 structureInstanceID) const; //hero must be visiting or garrisoned in town
  148. void deleteTownBonus(BuildingID::EBuildingID bid);
  149. /// Returns damage range for secondary towers of this town
  150. DamageRange getTowerDamageRange() const;
  151. /// Returns damage range for central tower(keep) of this town
  152. DamageRange getKeepDamageRange() const;
  153. const CTown * getTown() const;
  154. /// INativeTerrainProvider
  155. FactionID getFaction() const override;
  156. TerrainId getNativeTerrain() const override;
  157. CGTownInstance();
  158. virtual ~CGTownInstance();
  159. ///IObjectInterface overrides
  160. void newTurn(CRandomGenerator & rand) const override;
  161. void onHeroVisit(const CGHeroInstance * h) const override;
  162. void onHeroLeave(const CGHeroInstance * h) const override;
  163. void initObj(CRandomGenerator & rand) override;
  164. void battleFinished(const CGHeroInstance * hero, const BattleResult & result) const override;
  165. std::string getObjectName() const override;
  166. void afterAddToMap(CMap * map) override;
  167. void afterRemoveFromMap(CMap * map) override;
  168. static void reset();
  169. inline bool isBattleOutsideTown(const CGHeroInstance * defendingHero) const
  170. {
  171. return defendingHero && garrisonHero && defendingHero != garrisonHero;
  172. }
  173. protected:
  174. void setPropertyDer(ui8 what, ui32 val) override;
  175. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  176. private:
  177. void setOwner(const PlayerColor & owner) const;
  178. void onTownCaptured(const PlayerColor & winner) const;
  179. int getDwellingBonus(const std::vector<CreatureID>& creatureIds, const std::vector<ConstTransitivePtr<CGDwelling> >& dwellings) const;
  180. bool townEnvisagesBuilding(BuildingSubID::EBuildingSubID bid) const;
  181. bool isBonusingBuildingAdded(BuildingID::EBuildingID bid) const;
  182. void initOverriddenBids();
  183. void addTownBonuses();
  184. };
  185. VCMI_LIB_NAMESPACE_END