CCreatureHandler.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 setId(CreatureID ID); //assigns idNumber and updates bonuses to reference it
  90. void addBonus(int val, Bonus::BonusType type, int subtype = -1);
  91. std::string nodeName() const override;
  92. template<typename RanGen>
  93. int getRandomAmount(RanGen ranGen) const
  94. {
  95. if(ammMax == ammMin)
  96. return ammMax;
  97. else
  98. return ammMin + (ranGen() % (ammMax - ammMin));
  99. }
  100. template <typename Handler> void serialize(Handler &h, const int version)
  101. {
  102. h & static_cast<CBonusSystemNode&>(*this);
  103. h & namePl & nameSing & nameRef
  104. & cost & upgrades
  105. & fightValue & AIValue & growth & hordeGrowth
  106. & ammMin & ammMax & level
  107. & abilityText & animDefName & advMapDef;
  108. h & iconIndex & smallIconName & largeIconName;
  109. h & idNumber & faction & sounds & animation;
  110. h & doubleWide & special;
  111. }
  112. CCreature();
  113. };
  114. class DLL_LINKAGE CCreatureHandler : public IHandlerBase
  115. {
  116. private:
  117. CBonusSystemNode allCreatures;
  118. CBonusSystemNode creaturesOfLevel[GameConstants::CREATURES_PER_TOWN + 1];//index 0 is used for creatures of unknown tier or outside <1-7> range
  119. /// load one creature from json config
  120. CCreature * loadFromJson(const JsonNode & node);
  121. void loadJsonAnimation(CCreature * creature, const JsonNode & graphics);
  122. void loadStackExperience(CCreature * creature, const JsonNode &input);
  123. void loadCreatureJson(CCreature * creature, const JsonNode & config);
  124. /// loading functions
  125. /// adding abilities from ZCRTRAIT.TXT
  126. void loadBonuses(JsonNode & creature, std::string bonuses);
  127. /// load all creatures from H3 files
  128. void load();
  129. void loadCommanders();
  130. /// load creature from json structure
  131. void load(std::string creatureID, const JsonNode & node);
  132. /// read cranim.txt file from H3
  133. void loadAnimationInfo(std::vector<JsonNode> & h3Data);
  134. /// read one line from cranim.txt
  135. void loadUnitAnimInfo(JsonNode & unit, CLegacyConfigParser &parser);
  136. /// parse crexpbon.txt file from H3
  137. void loadStackExp(Bonus & b, BonusList & bl, CLegacyConfigParser &parser);
  138. /// help function for parsing CREXPBON.txt
  139. int stringToNumber(std::string & s);
  140. public:
  141. std::set<CreatureID> doubledCreatures; //they get double week
  142. std::vector<ConstTransitivePtr<CCreature> > creatures; //creature ID -> creature info.
  143. //stack exp
  144. std::vector<std::vector<ui32> > expRanks; // stack experience needed for certain rank, index 0 for other tiers (?)
  145. std::vector<ui32> maxExpPerBattle; //%, tiers same as above
  146. si8 expAfterUpgrade;//multiplier in %
  147. //Commanders
  148. BonusList commanderLevelPremy; //bonus values added with each level-up
  149. 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
  150. std::vector <std::pair <Bonus*, std::pair <ui8, ui8> > > skillRequirements; // first - Bonus, second - which two skills are needed to use it
  151. void deserializationFix();
  152. CreatureID pickRandomMonster(const boost::function<int()> &randGen = 0, int tier = -1) const; //tier <1 - CREATURES_PER_TOWN> or -1 for any
  153. void addBonusForTier(int tier, Bonus *b); //tier must be <1-7>
  154. void addBonusForAllCreatures(Bonus *b);
  155. CCreatureHandler();
  156. ~CCreatureHandler();
  157. /// load all creatures from H3 files
  158. void loadCrExpBon();
  159. /// generates tier-specific bonus tree entries
  160. void buildBonusTreeForTiers();
  161. std::vector<JsonNode> loadLegacyData(size_t dataSize) override;
  162. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  163. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  164. std::vector<bool> getDefaultAllowed() const override;
  165. template <typename Handler> void serialize(Handler &h, const int version)
  166. {
  167. //TODO: should be optimized, not all these informations needs to be serialized (same for ccreature)
  168. h & doubledCreatures & creatures;
  169. h & expRanks & maxExpPerBattle & expAfterUpgrade;
  170. h & skillLevels & skillRequirements & commanderLevelPremy;
  171. h & allCreatures;
  172. h & creaturesOfLevel;
  173. BONUS_TREE_DESERIALIZATION_FIX
  174. }
  175. };