CCreatureHandler.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 "HeroBonus.h"
  12. #include "ConstTransitivePtr.h"
  13. #include "ResourceSet.h"
  14. #include "GameConstants.h"
  15. #include "JsonNode.h"
  16. #include "IHandlerBase.h"
  17. #include "CRandomGenerator.h"
  18. class CLegacyConfigParser;
  19. class CCreatureHandler;
  20. class CCreature;
  21. class DLL_LINKAGE CCreature : public CBonusSystemNode
  22. {
  23. public:
  24. std::string identifier;
  25. std::string nameRef; // reference name, stringID
  26. std::string nameSing;// singular name, e.g. Centaur
  27. std::string namePl; // plural name, e.g. Centaurs
  28. std::string abilityText; //description of abilities
  29. CreatureID idNumber;
  30. TFaction faction;
  31. ui8 level; // 0 - unknown; 1-7 for "usual" creatures
  32. //stats that are not handled by bonus system
  33. ui32 fightValue, AIValue, growth, hordeGrowth;
  34. ui32 ammMin, ammMax; // initial size of stack of these creatures on adventure map (if not set in editor)
  35. bool doubleWide;
  36. bool special; // Creature is not available normally (war machines, commanders, several unused creatures, etc
  37. TResources cost; //cost[res_id] - amount of that resource required to buy creature from dwelling
  38. std::set<CreatureID> upgrades; // IDs of creatures to which this creature can be upgraded
  39. std::string animDefName; // creature animation used during battles
  40. std::string advMapDef; //for new creatures only, image for adventure map
  41. si32 iconIndex; // index of icon in files like twcrport
  42. /// names of files with appropriate icons. Used only during loading
  43. std::string smallIconName;
  44. std::string largeIconName;
  45. struct CreatureAnimation
  46. {
  47. double timeBetweenFidgets, idleAnimationTime,
  48. walkAnimationTime, attackAnimationTime, flightAnimationDistance;
  49. int upperRightMissleOffsetX, rightMissleOffsetX, lowerRightMissleOffsetX,
  50. upperRightMissleOffsetY, rightMissleOffsetY, lowerRightMissleOffsetY;
  51. std::vector<double> missleFrameAngles;
  52. int troopCountLocationOffset, attackClimaxFrame;
  53. std::string projectileImageName;
  54. //bool projectileSpin; //if true, appropriate projectile is spinning during flight
  55. template <typename Handler> void serialize(Handler &h, const int version)
  56. {
  57. h & timeBetweenFidgets;
  58. h & idleAnimationTime;
  59. h & walkAnimationTime;
  60. h & attackAnimationTime;
  61. h & flightAnimationDistance;
  62. h & upperRightMissleOffsetX;
  63. h & rightMissleOffsetX;
  64. h & lowerRightMissleOffsetX;
  65. h & upperRightMissleOffsetY;
  66. h & rightMissleOffsetY;
  67. h & lowerRightMissleOffsetY;
  68. h & missleFrameAngles;
  69. h & troopCountLocationOffset;
  70. h & attackClimaxFrame;
  71. h & projectileImageName;
  72. }
  73. } animation;
  74. //sound info
  75. struct CreatureBattleSounds
  76. {
  77. std::string attack;
  78. std::string defend;
  79. std::string killed; // was killed or died
  80. std::string move;
  81. std::string shoot; // range attack
  82. std::string wince; // attacked but did not die
  83. std::string startMoving;
  84. std::string endMoving;
  85. template <typename Handler> void serialize(Handler &h, const int version)
  86. {
  87. h & attack;
  88. h & defend;
  89. h & killed;
  90. h & move;
  91. h & shoot;
  92. h & wince;
  93. h & startMoving;
  94. h & endMoving;
  95. }
  96. } sounds;
  97. ArtifactID warMachine;
  98. bool isItNativeTerrain(ETerrainType::EETerrainType terrain) const;
  99. /**
  100. Returns creature native terrain considering some terrain bonuses.
  101. @param considerBonus is used to avoid Dead Lock when this method is called inside getAllBonuses
  102. considerBonus = true is called from Pathfinder and fills actual nativeTerrain considering bonus(es).
  103. considerBonus = false is called on Battle init and returns already prepared nativeTerrain without Bonus system calling.
  104. */
  105. ETerrainType::EETerrainType getNativeTerrain() const;
  106. bool isDoubleWide() const; //returns true if unit is double wide on battlefield
  107. bool isFlying() const; //returns true if it is a flying unit
  108. bool isShooting() const; //returns true if unit can shoot
  109. bool isUndead() const; //returns true if unit is undead
  110. bool isGood () const;
  111. bool isEvil () const;
  112. si32 maxAmount(const std::vector<si32> &res) const; //how many creatures can be bought
  113. 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
  114. static int estimateCreatureCount(ui32 countID); //reverse version of above function, returns middle of range
  115. bool isMyUpgrade(const CCreature *anotherCre) const;
  116. bool valid() const;
  117. void setId(CreatureID ID); //assigns idNumber and updates bonuses to reference it
  118. void addBonus(int val, Bonus::BonusType type, int subtype = -1);
  119. std::string nodeName() const override;
  120. template<typename RanGen>
  121. int getRandomAmount(RanGen ranGen) const
  122. {
  123. if(ammMax == ammMin)
  124. return ammMax;
  125. else
  126. return ammMin + (ranGen() % (ammMax - ammMin));
  127. }
  128. template <typename Handler> void serialize(Handler &h, const int version)
  129. {
  130. h & static_cast<CBonusSystemNode&>(*this);
  131. h & namePl;
  132. h & nameSing;
  133. h & nameRef;
  134. h & cost;
  135. h & upgrades;
  136. h & fightValue;
  137. h & AIValue;
  138. h & growth;
  139. h & hordeGrowth;
  140. h & ammMin;
  141. h & ammMax;
  142. h & level;
  143. h & abilityText;
  144. h & animDefName;
  145. h & advMapDef;
  146. h & iconIndex;
  147. h & smallIconName;
  148. h & largeIconName;
  149. h & idNumber;
  150. h & faction;
  151. h & sounds;
  152. h & animation;
  153. h & doubleWide;
  154. h & special;
  155. if(version>=759)
  156. {
  157. h & identifier;
  158. }
  159. if(version >= 771)
  160. {
  161. h & warMachine;
  162. }
  163. else if(!h.saving)
  164. {
  165. fillWarMachine();
  166. }
  167. }
  168. CCreature();
  169. private:
  170. void fillWarMachine();
  171. };
  172. class DLL_LINKAGE CCreatureHandler : public IHandlerBase
  173. {
  174. private:
  175. CBonusSystemNode allCreatures;
  176. CBonusSystemNode creaturesOfLevel[GameConstants::CREATURES_PER_TOWN + 1];//index 0 is used for creatures of unknown tier or outside <1-7> range
  177. /// load one creature from json config
  178. CCreature * loadFromJson(const JsonNode & node, const std::string & identifier);
  179. void loadJsonAnimation(CCreature * creature, const JsonNode & graphics);
  180. void loadStackExperience(CCreature * creature, const JsonNode &input);
  181. void loadCreatureJson(CCreature * creature, const JsonNode & config);
  182. /// loading functions
  183. /// adding abilities from ZCRTRAIT.TXT
  184. void loadBonuses(JsonNode & creature, std::string bonuses);
  185. /// load all creatures from H3 files
  186. void load();
  187. void loadCommanders();
  188. /// load creature from json structure
  189. void load(std::string creatureID, const JsonNode & node);
  190. /// read cranim.txt file from H3
  191. void loadAnimationInfo(std::vector<JsonNode> & h3Data);
  192. /// read one line from cranim.txt
  193. void loadUnitAnimInfo(JsonNode & unit, CLegacyConfigParser &parser);
  194. /// parse crexpbon.txt file from H3
  195. void loadStackExp(Bonus & b, BonusList & bl, CLegacyConfigParser &parser);
  196. /// help function for parsing CREXPBON.txt
  197. int stringToNumber(std::string & s);
  198. public:
  199. std::set<CreatureID> doubledCreatures; //they get double week
  200. std::vector<ConstTransitivePtr<CCreature> > creatures; //creature ID -> creature info.
  201. //stack exp
  202. std::vector<std::vector<ui32> > expRanks; // stack experience needed for certain rank, index 0 for other tiers (?)
  203. std::vector<ui32> maxExpPerBattle; //%, tiers same as above
  204. si8 expAfterUpgrade;//multiplier in %
  205. //Commanders
  206. BonusList commanderLevelPremy; //bonus values added with each level-up
  207. 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
  208. std::vector <std::pair <std::shared_ptr<Bonus>, std::pair <ui8, ui8> > > skillRequirements; // first - Bonus, second - which two skills are needed to use it
  209. const CCreature * getCreature(const std::string & scope, const std::string & identifier) const;
  210. void deserializationFix();
  211. CreatureID pickRandomMonster(CRandomGenerator & rand, int tier = -1) const; //tier <1 - CREATURES_PER_TOWN> or -1 for any
  212. void addBonusForTier(int tier, std::shared_ptr<Bonus> b); //tier must be <1-7>
  213. void addBonusForAllCreatures(std::shared_ptr<Bonus> b);
  214. void removeBonusesFromAllCreatures();
  215. CCreatureHandler();
  216. ~CCreatureHandler();
  217. /// load all creatures from H3 files
  218. void loadCrExpBon();
  219. /// generates tier-specific bonus tree entries
  220. void buildBonusTreeForTiers();
  221. void afterLoadFinalization() override;
  222. std::vector<JsonNode> loadLegacyData(size_t dataSize) override;
  223. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  224. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  225. std::vector<bool> getDefaultAllowed() const override;
  226. template <typename Handler> void serialize(Handler &h, const int version)
  227. {
  228. //TODO: should be optimized, not all these informations needs to be serialized (same for ccreature)
  229. h & doubledCreatures;
  230. h & creatures;
  231. h & expRanks;
  232. h & maxExpPerBattle;
  233. h & expAfterUpgrade;
  234. h & skillLevels;
  235. h & skillRequirements;
  236. h & commanderLevelPremy;
  237. h & allCreatures;
  238. h & creaturesOfLevel;
  239. BONUS_TREE_DESERIALIZATION_FIX
  240. }
  241. };