CCreatureHandler.h 8.0 KB

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