CCreatureHandler.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #pragma once
  2. #include "HeroBonus.h"
  3. #include "ConstTransitivePtr.h"
  4. #include "ResourceSet.h"
  5. #include "GameConstants.h"
  6. #include "JsonNode.h"
  7. #include "IHandlerBase.h"
  8. /*
  9. * CCreatureHandler.h, part of VCMI engine
  10. *
  11. * Authors: listed in file AUTHORS in main folder
  12. *
  13. * License: GNU General Public License v2.0 or later
  14. * Full text of license available in license.txt file, in main folder
  15. *
  16. */
  17. class CLegacyConfigParser;
  18. class CCreatureHandler;
  19. class CCreature;
  20. class DLL_LINKAGE CCreature : public CBonusSystemNode
  21. {
  22. public:
  23. std::string nameRef; // reference name, stringID
  24. std::string nameSing;// singular name, e.g. Centaur
  25. std::string namePl; // plural name, e.g. Centaurs
  26. std::string abilityText; //description of abilities
  27. CreatureID idNumber;
  28. TFaction faction;
  29. ui8 level; // 0 - unknown; 1-7 for "usual" creatures
  30. //stats that are not handled by bonus system
  31. ui32 fightValue, AIValue, growth, hordeGrowth;
  32. ui32 ammMin, ammMax; // initial size of stack of these creatures on adventure map (if not set in editor)
  33. bool doubleWide;
  34. bool special; // Creature is not available normally (war machines, commanders, several unused creatures, etc
  35. TResources cost; //cost[res_id] - amount of that resource required to buy creature from dwelling
  36. std::set<CreatureID> upgrades; // IDs of creatures to which this creature can be upgraded
  37. std::string animDefName; // creature animation used during battles
  38. std::string advMapDef; //for new creatures only, image for adventure map
  39. si32 iconIndex; // index of icon in files like twcrport
  40. /// names of files with appropriate icons. Used only during loading
  41. std::string smallIconName;
  42. std::string largeIconName;
  43. struct CreatureAnimation
  44. {
  45. double timeBetweenFidgets, idleAnimationTime,
  46. walkAnimationTime, attackAnimationTime, flightAnimationDistance;
  47. int upperRightMissleOffsetX, rightMissleOffsetX, lowerRightMissleOffsetX,
  48. upperRightMissleOffsetY, rightMissleOffsetY, lowerRightMissleOffsetY;
  49. std::vector<double> missleFrameAngles;
  50. int troopCountLocationOffset, attackClimaxFrame;
  51. std::string projectileImageName;
  52. //bool projectileSpin; //if true, appropriate projectile is spinning during flight
  53. template <typename Handler> void serialize(Handler &h, const int version)
  54. {
  55. h & timeBetweenFidgets & idleAnimationTime;
  56. h & walkAnimationTime & attackAnimationTime & flightAnimationDistance;
  57. h & upperRightMissleOffsetX & rightMissleOffsetX & lowerRightMissleOffsetX;
  58. h & upperRightMissleOffsetY & rightMissleOffsetY & lowerRightMissleOffsetY;
  59. h & missleFrameAngles & troopCountLocationOffset & attackClimaxFrame;
  60. h & projectileImageName;
  61. }
  62. } animation;
  63. //sound info
  64. struct CreatureBattleSounds
  65. {
  66. std::string attack;
  67. std::string defend;
  68. std::string killed; // was killed or died
  69. std::string move;
  70. std::string shoot; // range attack
  71. std::string wince; // attacked but did not die
  72. std::string startMoving;
  73. std::string endMoving;
  74. template <typename Handler> void serialize(Handler &h, const int version)
  75. {
  76. h & attack & defend & killed & move & shoot & wince & startMoving & endMoving;
  77. }
  78. } sounds;
  79. bool isItNativeTerrain(int terrain) const;
  80. bool isDoubleWide() const; //returns true if unit is double wide on battlefield
  81. bool isFlying() const; //returns true if it is a flying unit
  82. bool isShooting() const; //returns true if unit can shoot
  83. bool isUndead() const; //returns true if unit is undead
  84. bool isGood () const;
  85. bool isEvil () const;
  86. si32 maxAmount(const std::vector<si32> &res) const; //how many creatures can be bought
  87. static int getQuantityID(const int & quantity); //0 - a few, 1 - several, 2 - pack, 3 - lots, 4 - horde, 5 - throng, 6 - swarm, 7 - zounds, 8 - legion
  88. static int estimateCreatureCount(ui32 countID); //reverse version of above function, returns middle of range
  89. bool isMyUpgrade(const CCreature *anotherCre) const;
  90. bool valid() const;
  91. void setId(CreatureID ID); //assigns idNumber and updates bonuses to reference it
  92. void addBonus(int val, Bonus::BonusType type, int subtype = -1);
  93. std::string nodeName() const override;
  94. template<typename RanGen>
  95. int getRandomAmount(RanGen ranGen) const
  96. {
  97. if(ammMax == ammMin)
  98. return ammMax;
  99. else
  100. return ammMin + (ranGen() % (ammMax - ammMin));
  101. }
  102. template <typename Handler> void serialize(Handler &h, const int version)
  103. {
  104. h & static_cast<CBonusSystemNode&>(*this);
  105. h & namePl & nameSing & nameRef
  106. & cost & upgrades
  107. & fightValue & AIValue & growth & hordeGrowth
  108. & ammMin & ammMax & level
  109. & abilityText & animDefName & advMapDef;
  110. h & iconIndex & smallIconName & largeIconName;
  111. h & idNumber & faction & sounds & animation;
  112. h & doubleWide & special;
  113. }
  114. CCreature();
  115. };
  116. class DLL_LINKAGE CCreatureHandler : public IHandlerBase
  117. {
  118. private:
  119. CBonusSystemNode allCreatures;
  120. CBonusSystemNode creaturesOfLevel[GameConstants::CREATURES_PER_TOWN + 1];//index 0 is used for creatures of unknown tier or outside <1-7> range
  121. /// load one creature from json config
  122. CCreature * loadFromJson(const JsonNode & node);
  123. void loadJsonAnimation(CCreature * creature, const JsonNode & graphics);
  124. void loadStackExperience(CCreature * creature, const JsonNode &input);
  125. void loadCreatureJson(CCreature * creature, const JsonNode & config);
  126. /// loading functions
  127. /// adding abilities from ZCRTRAIT.TXT
  128. void loadBonuses(JsonNode & creature, std::string bonuses);
  129. /// load all creatures from H3 files
  130. void load();
  131. void loadCommanders();
  132. /// load creature from json structure
  133. void load(std::string creatureID, const JsonNode & node);
  134. /// read cranim.txt file from H3
  135. void loadAnimationInfo(std::vector<JsonNode> & h3Data);
  136. /// read one line from cranim.txt
  137. void loadUnitAnimInfo(JsonNode & unit, CLegacyConfigParser &parser);
  138. /// parse crexpbon.txt file from H3
  139. void loadStackExp(Bonus & b, BonusList & bl, CLegacyConfigParser &parser);
  140. /// help function for parsing CREXPBON.txt
  141. int stringToNumber(std::string & s);
  142. public:
  143. std::set<CreatureID> doubledCreatures; //they get double week
  144. std::vector<ConstTransitivePtr<CCreature> > creatures; //creature ID -> creature info.
  145. //stack exp
  146. std::vector<std::vector<ui32> > expRanks; // stack experience needed for certain rank, index 0 for other tiers (?)
  147. std::vector<ui32> maxExpPerBattle; //%, tiers same as above
  148. si8 expAfterUpgrade;//multiplier in %
  149. //Commanders
  150. BonusList commanderLevelPremy; //bonus values added with each level-up
  151. 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
  152. std::vector <std::pair <Bonus*, std::pair <ui8, ui8> > > skillRequirements; // first - Bonus, second - which two skills are needed to use it
  153. void deserializationFix();
  154. CreatureID pickRandomMonster(const std::function<int()> &randGen = nullptr, int tier = -1) const; //tier <1 - CREATURES_PER_TOWN> or -1 for any
  155. void addBonusForTier(int tier, Bonus *b); //tier must be <1-7>
  156. void addBonusForAllCreatures(Bonus *b);
  157. CCreatureHandler();
  158. ~CCreatureHandler();
  159. /// load all creatures from H3 files
  160. void loadCrExpBon();
  161. /// generates tier-specific bonus tree entries
  162. void buildBonusTreeForTiers();
  163. void afterLoadFinalization();
  164. std::vector<JsonNode> loadLegacyData(size_t dataSize) override;
  165. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  166. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  167. std::vector<bool> getDefaultAllowed() const override;
  168. template <typename Handler> void serialize(Handler &h, const int version)
  169. {
  170. //TODO: should be optimized, not all these informations needs to be serialized (same for ccreature)
  171. h & doubledCreatures & creatures;
  172. h & expRanks & maxExpPerBattle & expAfterUpgrade;
  173. h & skillLevels & skillRequirements & commanderLevelPremy;
  174. h & allCreatures;
  175. h & creaturesOfLevel;
  176. BONUS_TREE_DESERIALIZATION_FIX
  177. }
  178. };