CGTownInstance.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 & static_cast<IMarket&>(*this);
  66. h & name;
  67. h & builded;
  68. h & destroyed;
  69. h & identifier;
  70. h & garrisonHero;
  71. h & visitingHero;
  72. h & alignmentToPlayer;
  73. h & forbiddenBuildings;
  74. h & builtBuildings;
  75. h & bonusValue;
  76. h & possibleSpells;
  77. h & obligatorySpells;
  78. h & spells;
  79. h & events;
  80. h & bonusingBuildings;
  81. h & town;
  82. h & townAndVis;
  83. BONUS_TREE_DESERIALIZATION_FIX
  84. if(town)
  85. {
  86. vstd::erase_if(builtBuildings, [this](BuildingID building) -> bool
  87. {
  88. if(!town->buildings.count(building) || !town->buildings.at(building))
  89. {
  90. logGlobal->error("#1444-like issue in CGTownInstance::serialize. From town %s at %s removing the bogus builtBuildings item %s", name, pos.toString(), building);
  91. return true;
  92. }
  93. return false;
  94. });
  95. }
  96. h & overriddenBuildings;
  97. if(!h.saving)
  98. this->setNodeType(CBonusSystemNode::TOWN);
  99. }
  100. //////////////////////////////////////////////////////////////////////////
  101. CBonusSystemNode & whatShouldBeAttached() override;
  102. std::string nodeName() const override;
  103. void updateMoraleBonusFromArmy() override;
  104. void deserializationFix();
  105. void recreateBuildingsBonuses();
  106. void setVisitingHero(CGHeroInstance *h);
  107. void setGarrisonedHero(CGHeroInstance *h);
  108. const CArmedInstance *getUpperArmy() const; //garrisoned hero if present or the town itself
  109. std::string getNameTranslated() const;
  110. void setNameTranslated(const std::string & newName);
  111. //////////////////////////////////////////////////////////////////////////
  112. bool passableFor(PlayerColor color) const override;
  113. //int3 getSightCenter() const override; //"center" tile from which the sight distance is calculated
  114. int getSightRadius() const override; //returns sight distance
  115. BoatId getBoatType() const override; //0 - evil (if a ship can be evil...?), 1 - good, 2 - neutral
  116. void getOutOffsets(std::vector<int3> &offsets) const override; //offsets to obj pos when we boat can be placed. Parameter will be cleared
  117. int getMarketEfficiency() const override; //=market count
  118. bool allowsTrade(EMarketMode::EMarketMode mode) const override;
  119. std::vector<int> availableItemsIds(EMarketMode::EMarketMode mode) const override;
  120. void setType(si32 ID, si32 subID) override;
  121. void updateAppearance();
  122. //////////////////////////////////////////////////////////////////////////
  123. bool needsLastStack() const override;
  124. CGTownInstance::EFortLevel fortLevel() const;
  125. int hallLevel() const; // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  126. int mageGuildLevel() const; // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  127. int getHordeLevel(const int & HID) const; //HID - 0 or 1; returns creature level or -1 if that horde structure is not present
  128. int creatureGrowth(const int & level) const;
  129. GrowthInfo getGrowthInfo(int level) const;
  130. bool hasFort() const;
  131. bool hasCapitol() const;
  132. const CGTownBuilding * getBonusingBuilding(BuildingSubID::EBuildingSubID subId) const;
  133. bool hasBuiltSomeTradeBuilding() const;
  134. //checks if special building with type buildingID is constructed
  135. bool hasBuilt(BuildingSubID::EBuildingSubID buildingID) const;
  136. //checks if building is constructed and town has same subID
  137. bool hasBuilt(const BuildingID & buildingID) const;
  138. bool hasBuilt(const BuildingID & buildingID, int townID) const;
  139. TResources getBuildingCost(const BuildingID & buildingID) const;
  140. TResources dailyIncome() const; //calculates daily income of this town
  141. int spellsAtLevel(int level, bool checkGuild) const; //levels are counted from 1 (1 - 5)
  142. bool armedGarrison() const; //true if town has creatures in garrison or garrisoned hero
  143. int getTownLevel() const;
  144. CBuilding::TRequired genBuildingRequirements(const BuildingID & build, bool deep = false) const;
  145. void mergeGarrisonOnSiege() const; // merge garrison into army of visiting hero
  146. void removeCapitols(const PlayerColor & owner) const;
  147. void clearArmy() const;
  148. void addHeroToStructureVisitors(const CGHeroInstance *h, si64 structureInstanceID) const; //hero must be visiting or garrisoned in town
  149. void deleteTownBonus(BuildingID::EBuildingID bid);
  150. /// Returns damage range for secondary towers of this town
  151. DamageRange getTowerDamageRange() const;
  152. /// Returns damage range for central tower(keep) of this town
  153. DamageRange getKeepDamageRange() const;
  154. const CTown * getTown() const;
  155. /// INativeTerrainProvider
  156. FactionID getFaction() const override;
  157. TerrainId getNativeTerrain() const override;
  158. CGTownInstance();
  159. virtual ~CGTownInstance();
  160. ///IObjectInterface overrides
  161. void newTurn(CRandomGenerator & rand) const override;
  162. void onHeroVisit(const CGHeroInstance * h) const override;
  163. void onHeroLeave(const CGHeroInstance * h) const override;
  164. void initObj(CRandomGenerator & rand) override;
  165. void battleFinished(const CGHeroInstance * hero, const BattleResult & result) const override;
  166. std::string getObjectName() const override;
  167. void afterAddToMap(CMap * map) override;
  168. void afterRemoveFromMap(CMap * map) override;
  169. static void reset();
  170. inline bool isBattleOutsideTown(const CGHeroInstance * defendingHero) const
  171. {
  172. return defendingHero && garrisonHero && defendingHero != garrisonHero;
  173. }
  174. protected:
  175. void setPropertyDer(ui8 what, ui32 val) override;
  176. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  177. private:
  178. void setOwner(const PlayerColor & owner) const;
  179. void onTownCaptured(const PlayerColor & winner) const;
  180. int getDwellingBonus(const std::vector<CreatureID>& creatureIds, const std::vector<ConstTransitivePtr<CGDwelling> >& dwellings) const;
  181. bool hasBuiltInOldWay(ETownType::ETownType type, const BuildingID & bid) const;
  182. bool townEnvisagesBuilding(BuildingSubID::EBuildingSubID bid) const;
  183. bool isBonusingBuildingAdded(BuildingID::EBuildingID bid) const;
  184. void tryAddOnePerWeekBonus(BuildingSubID::EBuildingSubID subID);
  185. void tryAddVisitingBonus(BuildingSubID::EBuildingSubID subID);
  186. void initOverriddenBids();
  187. void addTownBonuses();
  188. };
  189. VCMI_LIB_NAMESPACE_END