CGTownInstance.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. static std::vector<const CArtifact *> merchantArtifacts; //vector of artifacts available at Artifact merchant, NULLs possible (for making empty space when artifact is bought)
  59. static std::vector<int> universitySkills;//skills for university of magic
  60. template <typename Handler> void serialize(Handler &h, const int version)
  61. {
  62. h & static_cast<CGDwelling&>(*this);
  63. h & nameTextId;
  64. h & builded;
  65. h & destroyed;
  66. h & identifier;
  67. h & garrisonHero;
  68. h & visitingHero;
  69. h & alignmentToPlayer;
  70. h & forbiddenBuildings;
  71. h & builtBuildings;
  72. h & bonusValue;
  73. h & possibleSpells;
  74. h & obligatorySpells;
  75. h & spells;
  76. h & events;
  77. h & bonusingBuildings;
  78. for(auto * bonusingBuilding : bonusingBuildings)
  79. bonusingBuilding->town = this;
  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", nameTextId, 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 setNameTextId(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. EGeneratorState shipyardStatus() const override;
  117. const IObjectInterface * getObject() const override;
  118. int getMarketEfficiency() const override; //=market count
  119. bool allowsTrade(EMarketMode mode) const override;
  120. std::vector<int> availableItemsIds(EMarketMode mode) const override;
  121. void setType(si32 ID, si32 subID) override;
  122. void updateAppearance();
  123. //////////////////////////////////////////////////////////////////////////
  124. bool needsLastStack() const override;
  125. CGTownInstance::EFortLevel fortLevel() const;
  126. int hallLevel() const; // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  127. int mageGuildLevel() const; // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  128. int getHordeLevel(const int & HID) const; //HID - 0 or 1; returns creature level or -1 if that horde structure is not present
  129. int creatureGrowth(const int & level) const;
  130. GrowthInfo getGrowthInfo(int level) const;
  131. bool hasFort() const;
  132. bool hasCapitol() const;
  133. std::vector<const CGTownBuilding *> getBonusingBuildings(BuildingSubID::EBuildingSubID subId) const;
  134. bool hasBuiltSomeTradeBuilding() const;
  135. //checks if special building with type buildingID is constructed
  136. bool hasBuilt(BuildingSubID::EBuildingSubID buildingID) const;
  137. //checks if building is constructed and town has same subID
  138. bool hasBuilt(const BuildingID & buildingID) const;
  139. bool hasBuilt(const BuildingID & buildingID, int townID) const;
  140. TResources getBuildingCost(const BuildingID & buildingID) const;
  141. TResources dailyIncome() const; //calculates daily income of this town
  142. int spellsAtLevel(int level, bool checkGuild) const; //levels are counted from 1 (1 - 5)
  143. bool armedGarrison() const; //true if town has creatures in garrison or garrisoned hero
  144. int getTownLevel() const;
  145. CBuilding::TRequired genBuildingRequirements(const BuildingID & build, bool deep = false) const;
  146. void mergeGarrisonOnSiege() const; // merge garrison into army of visiting hero
  147. void removeCapitols(const PlayerColor & owner) const;
  148. void clearArmy() const;
  149. void addHeroToStructureVisitors(const CGHeroInstance *h, si64 structureInstanceID) const; //hero must be visiting or garrisoned in town
  150. void deleteTownBonus(BuildingID bid);
  151. /// Returns damage range for secondary towers of this town
  152. DamageRange getTowerDamageRange() const;
  153. /// Returns damage range for central tower(keep) of this town
  154. DamageRange getKeepDamageRange() const;
  155. const CTown * getTown() const;
  156. /// INativeTerrainProvider
  157. FactionID getFaction() const override;
  158. TerrainId getNativeTerrain() const override;
  159. CGTownInstance();
  160. virtual ~CGTownInstance();
  161. ///IObjectInterface overrides
  162. void newTurn(CRandomGenerator & rand) const override;
  163. void onHeroVisit(const CGHeroInstance * h) const override;
  164. void onHeroLeave(const CGHeroInstance * h) const override;
  165. void initObj(CRandomGenerator & rand) override;
  166. void battleFinished(const CGHeroInstance * hero, const BattleResult & result) const override;
  167. std::string getObjectName() const override;
  168. void fillUpgradeInfo(UpgradeInfo & info, const CStackInstance &stack) const override;
  169. void afterAddToMap(CMap * map) override;
  170. void afterRemoveFromMap(CMap * map) override;
  171. static void reset();
  172. inline bool isBattleOutsideTown(const CGHeroInstance * defendingHero) const
  173. {
  174. return defendingHero && garrisonHero && defendingHero != garrisonHero;
  175. }
  176. protected:
  177. void setPropertyDer(ui8 what, ui32 val) override;
  178. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  179. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  180. private:
  181. void setOwner(const PlayerColor & owner) const;
  182. void onTownCaptured(const PlayerColor & winner) const;
  183. int getDwellingBonus(const std::vector<CreatureID>& creatureIds, const std::vector<ConstTransitivePtr<CGDwelling> >& dwellings) const;
  184. bool townEnvisagesBuilding(BuildingSubID::EBuildingSubID bid) const;
  185. bool isBonusingBuildingAdded(BuildingID bid) const;
  186. void initOverriddenBids();
  187. void addTownBonuses(CRandomGenerator & rand);
  188. };
  189. VCMI_LIB_NAMESPACE_END