CCreatureHandler.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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
  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, walkAnimationTime, attackAnimationTime, flightAnimationDistance;
  46. int upperRightMissleOffsetX, rightMissleOffsetX, lowerRightMissleOffsetX,
  47. upperRightMissleOffsetY, rightMissleOffsetY, lowerRightMissleOffsetY;
  48. std::vector<double> missleFrameAngles;
  49. int troopCountLocationOffset, attackClimaxFrame;
  50. std::string projectileImageName;
  51. //bool projectileSpin; //if true, appropriate projectile is spinning during flight
  52. template <typename Handler> void serialize(Handler &h, const int version)
  53. {
  54. h & timeBetweenFidgets & walkAnimationTime & attackAnimationTime & flightAnimationDistance;
  55. h & upperRightMissleOffsetX & rightMissleOffsetX & lowerRightMissleOffsetX;
  56. h & upperRightMissleOffsetY & rightMissleOffsetY & lowerRightMissleOffsetY;
  57. h & missleFrameAngles & troopCountLocationOffset & attackClimaxFrame;
  58. h & projectileImageName;
  59. }
  60. } animation;
  61. //sound info
  62. struct CreatureBattleSounds
  63. {
  64. std::string attack;
  65. std::string defend;
  66. std::string killed; // was killed or died
  67. std::string move;
  68. std::string shoot; // range attack
  69. std::string wince; // attacked but did not die
  70. std::string startMoving;
  71. std::string endMoving;
  72. template <typename Handler> void serialize(Handler &h, const int version)
  73. {
  74. h & attack & defend & killed & move & shoot & wince & startMoving & endMoving;
  75. }
  76. } sounds;
  77. bool isItNativeTerrain(int terrain) const;
  78. bool isDoubleWide() const; //returns true if unit is double wide on battlefield
  79. bool isFlying() const; //returns true if it is a flying unit
  80. bool isShooting() const; //returns true if unit can shoot
  81. bool isUndead() const; //returns true if unit is undead
  82. bool isGood () const;
  83. bool isEvil () const;
  84. si32 maxAmount(const std::vector<si32> &res) const; //how many creatures can be bought
  85. 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
  86. static int estimateCreatureCount(ui32 countID); //reverse version of above function, returns middle of range
  87. bool isMyUpgrade(const CCreature *anotherCre) const;
  88. bool valid() const;
  89. void addBonus(int val, Bonus::BonusType type, int subtype = -1);
  90. std::string nodeName() const override;
  91. template<typename RanGen>
  92. int getRandomAmount(RanGen ranGen) const
  93. {
  94. if(ammMax == ammMin)
  95. return ammMax;
  96. else
  97. return ammMin + (ranGen() % (ammMax - ammMin));
  98. }
  99. template <typename Handler> void serialize(Handler &h, const int version)
  100. {
  101. h & static_cast<CBonusSystemNode&>(*this);
  102. h & namePl & nameSing & nameRef
  103. & cost & upgrades
  104. & fightValue & AIValue & growth & hordeGrowth
  105. & ammMin & ammMax & level
  106. & abilityText & animDefName & advMapDef;
  107. h & iconIndex & smallIconName & largeIconName;
  108. h & idNumber & faction & sounds & animation;
  109. h & doubleWide & special;
  110. }
  111. CCreature();
  112. };
  113. class DLL_LINKAGE CCreatureHandler : public IHandlerBase
  114. {
  115. private:
  116. CBonusSystemNode allCreatures;
  117. CBonusSystemNode creaturesOfLevel[GameConstants::CREATURES_PER_TOWN + 1];//index 0 is used for creatures of unknown tier or outside <1-7> range
  118. /// load one creature from json config
  119. CCreature * loadFromJson(const JsonNode & node);
  120. void loadJsonAnimation(CCreature * creature, const JsonNode & graphics);
  121. void loadStackExperience(CCreature * creature, const JsonNode &input);
  122. void loadCreatureJson(CCreature * creature, const JsonNode & config);
  123. /// loading functions
  124. /// adding abilities from ZCRTRAIT.TXT
  125. void loadBonuses(JsonNode & creature, std::string bonuses);
  126. /// load all creatures from H3 files
  127. void load();
  128. void loadCommanders();
  129. /// load creature from json structure
  130. void load(std::string creatureID, const JsonNode & node);
  131. /// read cranim.txt file from H3
  132. void loadAnimationInfo(std::vector<JsonNode> & h3Data);
  133. /// read one line from cranim.txt
  134. void loadUnitAnimInfo(JsonNode & unit, CLegacyConfigParser &parser);
  135. /// parse crexpbon.txt file from H3
  136. void loadStackExp(Bonus & b, BonusList & bl, CLegacyConfigParser &parser);
  137. /// help function for parsing CREXPBON.txt
  138. int stringToNumber(std::string & s);
  139. public:
  140. std::set<CreatureID> doubledCreatures; //they get double week
  141. std::vector<ConstTransitivePtr<CCreature> > creatures; //creature ID -> creature info.
  142. //stack exp
  143. std::vector<std::vector<ui32> > expRanks; // stack experience needed for certain rank, index 0 for other tiers (?)
  144. std::vector<ui32> maxExpPerBattle; //%, tiers same as above
  145. si8 expAfterUpgrade;//multiplier in %
  146. //Commanders
  147. BonusList commanderLevelPremy; //bonus values added with each level-up
  148. 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
  149. std::vector <std::pair <Bonus, std::pair <ui8, ui8> > > skillRequirements; // first - Bonus, second - which two skills are needed to use it
  150. void deserializationFix();
  151. CreatureID pickRandomMonster(const boost::function<int()> &randGen = 0, int tier = -1) const; //tier <1 - CREATURES_PER_TOWN> or -1 for any
  152. void addBonusForTier(int tier, Bonus *b); //tier must be <1-7>
  153. void addBonusForAllCreatures(Bonus *b);
  154. CCreatureHandler();
  155. ~CCreatureHandler();
  156. /// load all creatures from H3 files
  157. void loadCrExpBon();
  158. /// generates tier-specific bonus tree entries
  159. void buildBonusTreeForTiers();
  160. std::vector<JsonNode> loadLegacyData(size_t dataSize) override;
  161. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  162. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  163. std::vector<bool> getDefaultAllowed() const override;
  164. template <typename Handler> void serialize(Handler &h, const int version)
  165. {
  166. //TODO: should be optimized, not all these informations needs to be serialized (same for ccreature)
  167. h & doubledCreatures & creatures;
  168. h & expRanks & maxExpPerBattle & expAfterUpgrade;
  169. h & skillLevels & skillRequirements & commanderLevelPremy;
  170. h & allCreatures;
  171. h & creaturesOfLevel;
  172. BONUS_TREE_DESERIALIZATION_FIX
  173. }
  174. };