CCreatureHandler.h 10 KB

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