CCreatureHandler.h 7.3 KB

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