CCreatureHandler.h 7.7 KB

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