CCreatureHandler.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * CCreatureHandler.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 <vcmi/Creature.h>
  12. #include <vcmi/CreatureService.h>
  13. #include "HeroBonus.h"
  14. #include "ConstTransitivePtr.h"
  15. #include "ResourceSet.h"
  16. #include "GameConstants.h"
  17. #include "JsonNode.h"
  18. #include "IHandlerBase.h"
  19. #include "CRandomGenerator.h"
  20. #include "Color.h"
  21. VCMI_LIB_NAMESPACE_BEGIN
  22. class CLegacyConfigParser;
  23. class CCreatureHandler;
  24. class CCreature;
  25. class JsonSerializeFormat;
  26. class DLL_LINKAGE CCreature : public Creature, public CBonusSystemNode
  27. {
  28. friend class CCreatureHandler;
  29. std::string modScope;
  30. std::string identifier;
  31. std::string getNameTranslated() const override;
  32. std::string getNameTextID() const override;
  33. public:
  34. CreatureID idNumber;
  35. TFaction faction = 0;
  36. ui8 level = 0; // 0 - unknown; 1-7 for "usual" creatures
  37. //stats that are not handled by bonus system
  38. ui32 fightValue, AIValue, growth, hordeGrowth;
  39. ui32 ammMin, ammMax; // initial size of stack of these creatures on adventure map (if not set in editor)
  40. bool doubleWide = false;
  41. bool special = true; // Creature is not available normally (war machines, commanders, several unused creatures, etc
  42. TResources cost; //cost[res_id] - amount of that resource required to buy creature from dwelling
  43. std::set<CreatureID> upgrades; // IDs of creatures to which this creature can be upgraded
  44. std::string animDefName; // creature animation used during battles
  45. std::string advMapDef; //for new creatures only, image for adventure map
  46. si32 iconIndex = -1; // index of icon in files like twcrport
  47. /// names of files with appropriate icons. Used only during loading
  48. std::string smallIconName;
  49. std::string largeIconName;
  50. enum class CreatureQuantityId
  51. {
  52. FEW = 1,
  53. SEVERAL,
  54. PACK,
  55. LOTS,
  56. HORDE,
  57. THRONG,
  58. SWARM,
  59. ZOUNDS,
  60. LEGION
  61. };
  62. struct CreatureAnimation
  63. {
  64. struct RayColor {
  65. ColorRGBA start;
  66. ColorRGBA end;
  67. template <typename Handler> void serialize(Handler &h, const int version)
  68. {
  69. h & start & end;
  70. }
  71. };
  72. double timeBetweenFidgets, idleAnimationTime,
  73. walkAnimationTime, attackAnimationTime;
  74. int upperRightMissleOffsetX, rightMissleOffsetX, lowerRightMissleOffsetX,
  75. upperRightMissleOffsetY, rightMissleOffsetY, lowerRightMissleOffsetY;
  76. std::vector<double> missleFrameAngles;
  77. int troopCountLocationOffset, attackClimaxFrame;
  78. std::string projectileImageName;
  79. std::vector<RayColor> projectileRay;
  80. //bool projectileSpin; //if true, appropriate projectile is spinning during flight
  81. template <typename Handler> void serialize(Handler &h, const int version)
  82. {
  83. h & timeBetweenFidgets;
  84. h & idleAnimationTime;
  85. h & walkAnimationTime;
  86. h & attackAnimationTime;
  87. if (version < 814)
  88. {
  89. float unused = 0.f;
  90. h & unused;
  91. }
  92. h & upperRightMissleOffsetX;
  93. h & rightMissleOffsetX;
  94. h & lowerRightMissleOffsetX;
  95. h & upperRightMissleOffsetY;
  96. h & rightMissleOffsetY;
  97. h & lowerRightMissleOffsetY;
  98. h & missleFrameAngles;
  99. h & troopCountLocationOffset;
  100. h & attackClimaxFrame;
  101. h & projectileImageName;
  102. h & projectileRay;
  103. }
  104. } animation;
  105. //sound info
  106. struct CreatureBattleSounds
  107. {
  108. std::string attack;
  109. std::string defend;
  110. std::string killed; // was killed or died
  111. std::string move;
  112. std::string shoot; // range attack
  113. std::string wince; // attacked but did not die
  114. std::string startMoving;
  115. std::string endMoving;
  116. template <typename Handler> void serialize(Handler &h, const int version)
  117. {
  118. h & attack;
  119. h & defend;
  120. h & killed;
  121. h & move;
  122. h & shoot;
  123. h & wince;
  124. h & startMoving;
  125. h & endMoving;
  126. }
  127. } sounds;
  128. ArtifactID warMachine;
  129. std::string getNamePluralTranslated() const override;
  130. std::string getNameSingularTranslated() const override;
  131. std::string getNamePluralTextID() const override;
  132. std::string getNameSingularTextID() const override;
  133. bool isItNativeTerrain(TerrainId terrain) const;
  134. /**
  135. Returns creature native terrain considering some terrain bonuses.
  136. @param considerBonus is used to avoid Dead Lock when this method is called inside getAllBonuses
  137. considerBonus = true is called from Pathfinder and fills actual nativeTerrain considering bonus(es).
  138. considerBonus = false is called on Battle init and returns already prepared nativeTerrain without Bonus system calling.
  139. */
  140. TerrainId getNativeTerrain() const;
  141. int32_t getIndex() const override;
  142. int32_t getIconIndex() const override;
  143. std::string getJsonKey() const override;
  144. void registerIcons(const IconRegistar & cb) const override;
  145. CreatureID getId() const override;
  146. virtual const IBonusBearer * accessBonuses() const override;
  147. uint32_t getMaxHealth() const override;
  148. int32_t getAdvMapAmountMin() const override;
  149. int32_t getAdvMapAmountMax() const override;
  150. int32_t getAIValue() const override;
  151. int32_t getFightValue() const override;
  152. int32_t getLevel() const override;
  153. int32_t getGrowth() const override;
  154. int32_t getHorde() const override;
  155. int32_t getFactionIndex() const override;
  156. int32_t getBaseAttack() const override;
  157. int32_t getBaseDefense() const override;
  158. int32_t getBaseDamageMin() const override;
  159. int32_t getBaseDamageMax() const override;
  160. int32_t getBaseHitPoints() const override;
  161. int32_t getBaseSpellPoints() const override;
  162. int32_t getBaseSpeed() const override;
  163. int32_t getBaseShots() const override;
  164. int32_t getCost(int32_t resIndex) const override;
  165. bool isDoubleWide() const override; //returns true if unit is double wide on battlefield
  166. bool isGood () const;
  167. bool isEvil () const;
  168. si32 maxAmount(const std::vector<si32> &res) const; //how many creatures can be bought
  169. static CCreature::CreatureQuantityId getQuantityID(const int & quantity);
  170. static std::string getQuantityRangeStringForId(const CCreature::CreatureQuantityId & quantityId);
  171. static int estimateCreatureCount(ui32 countID); //reverse version of above function, returns middle of range
  172. bool isMyUpgrade(const CCreature *anotherCre) const;
  173. bool valid() const;
  174. void addBonus(int val, Bonus::BonusType type, int subtype = -1);
  175. std::string nodeName() const override;
  176. template<typename RanGen>
  177. int getRandomAmount(RanGen ranGen) const
  178. {
  179. if(ammMax == ammMin)
  180. return ammMax;
  181. else
  182. return ammMin + (ranGen() % (ammMax - ammMin));
  183. }
  184. void updateFrom(const JsonNode & data);
  185. void serializeJson(JsonSerializeFormat & handler);
  186. template <typename Handler> void serialize(Handler &h, const int version)
  187. {
  188. h & static_cast<CBonusSystemNode&>(*this);
  189. h & cost;
  190. h & upgrades;
  191. h & fightValue;
  192. h & AIValue;
  193. h & growth;
  194. h & hordeGrowth;
  195. h & ammMin;
  196. h & ammMax;
  197. h & level;
  198. h & animDefName;
  199. h & advMapDef;
  200. h & iconIndex;
  201. h & smallIconName;
  202. h & largeIconName;
  203. h & idNumber;
  204. h & faction;
  205. h & sounds;
  206. h & animation;
  207. h & doubleWide;
  208. h & special;
  209. h & identifier;
  210. h & modScope;
  211. h & warMachine;
  212. }
  213. CCreature();
  214. private:
  215. static const std::map<CreatureQuantityId, std::string> creatureQuantityRanges;
  216. };
  217. class DLL_LINKAGE CCreatureHandler : public CHandlerBase<CreatureID, Creature, CCreature, CreatureService>
  218. {
  219. private:
  220. CBonusSystemNode allCreatures;
  221. CBonusSystemNode creaturesOfLevel[GameConstants::CREATURES_PER_TOWN + 1];//index 0 is used for creatures of unknown tier or outside <1-7> range
  222. void loadJsonAnimation(CCreature * creature, const JsonNode & graphics) const;
  223. void loadStackExperience(CCreature * creature, const JsonNode & input) const;
  224. void loadCreatureJson(CCreature * creature, const JsonNode & config) const;
  225. /// adding abilities from ZCRTRAIT.TXT
  226. void loadBonuses(JsonNode & creature, std::string bonuses) const;
  227. /// load all creatures from H3 files
  228. void load();
  229. void loadCommanders();
  230. /// load creature from json structure
  231. void load(std::string creatureID, const JsonNode & node);
  232. /// read cranim.txt file from H3
  233. void loadAnimationInfo(std::vector<JsonNode> & h3Data) const;
  234. /// read one line from cranim.txt
  235. void loadUnitAnimInfo(JsonNode & unit, CLegacyConfigParser & parser) const;
  236. /// parse crexpbon.txt file from H3
  237. void loadStackExp(Bonus & b, BonusList & bl, CLegacyConfigParser & parser) const;
  238. /// help function for parsing CREXPBON.txt
  239. int stringToNumber(std::string & s) const;
  240. protected:
  241. const std::vector<std::string> & getTypeNames() const override;
  242. CCreature * loadFromJson(const std::string & scope, const JsonNode & node, const std::string & identifier, size_t index) override;
  243. public:
  244. std::set<CreatureID> doubledCreatures; //they get double week
  245. //stack exp
  246. std::vector<std::vector<ui32> > expRanks; // stack experience needed for certain rank, index 0 for other tiers (?)
  247. std::vector<ui32> maxExpPerBattle; //%, tiers same as above
  248. si8 expAfterUpgrade;//multiplier in %
  249. //Commanders
  250. BonusList commanderLevelPremy; //bonus values added with each level-up
  251. std::vector< std::vector <ui8> > skillLevels; //how much of a bonus will be given to commander with every level. SPELL_POWER also gives CASTS and RESISTANCE
  252. std::vector <std::pair <std::shared_ptr<Bonus>, std::pair <ui8, ui8> > > skillRequirements; // first - Bonus, second - which two skills are needed to use it
  253. const CCreature * getCreature(const std::string & scope, const std::string & identifier) const;
  254. void deserializationFix();
  255. CreatureID pickRandomMonster(CRandomGenerator & rand, int tier = -1) const; //tier <1 - CREATURES_PER_TOWN> or -1 for any
  256. void addBonusForTier(int tier, const std::shared_ptr<Bonus> & b); //tier must be <1-7>
  257. void addBonusForAllCreatures(const std::shared_ptr<Bonus> & b); //due to CBonusSystem::addNewBonus(const std::shared_ptr<Bonus>& b);
  258. void removeBonusesFromAllCreatures();
  259. CCreatureHandler();
  260. ~CCreatureHandler();
  261. /// load all creatures from H3 files
  262. void loadCrExpBon();
  263. /// generates tier-specific bonus tree entries
  264. void buildBonusTreeForTiers();
  265. void afterLoadFinalization() override;
  266. std::vector<JsonNode> loadLegacyData() override;
  267. std::vector<bool> getDefaultAllowed() const override;
  268. template <typename Handler> void serialize(Handler &h, const int version)
  269. {
  270. //TODO: should be optimized, not all these informations needs to be serialized (same for ccreature)
  271. h & doubledCreatures;
  272. h & objects;
  273. h & expRanks;
  274. h & maxExpPerBattle;
  275. h & expAfterUpgrade;
  276. h & skillLevels;
  277. h & skillRequirements;
  278. h & commanderLevelPremy;
  279. h & allCreatures;
  280. h & creaturesOfLevel;
  281. BONUS_TREE_DESERIALIZATION_FIX
  282. }
  283. };
  284. VCMI_LIB_NAMESPACE_END