CCreatureHandler.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #ifndef __CCREATUREHANDLER_H__
  2. #define __CCREATUREHANDLER_H__
  3. #include "../global.h"
  4. #include <string>
  5. #include <vector>
  6. #include <map>
  7. #include <set>
  8. #include "../lib/HeroBonus.h"
  9. #include "../lib/ConstTransitivePtr.h"
  10. #include "ResourceSet.h"
  11. /*
  12. * CCreatureHandler.h, part of VCMI engine
  13. *
  14. * Authors: listed in file AUTHORS in main folder
  15. *
  16. * License: GNU General Public License v2.0 or later
  17. * Full text of license available in license.txt file, in main folder
  18. *
  19. */
  20. #define ALLCREATURESGETDOUBLEMONTHS false
  21. class CLodHandler;
  22. class CCreatureHandler;
  23. class CCreature;
  24. class DLL_EXPORT CCreature : public CBonusSystemNode
  25. {
  26. public:
  27. std::string namePl, nameSing, nameRef; //name in singular and plural form; and reference name
  28. TResources cost; //cost[res_id] - amount of that resource
  29. std::set<ui32> upgrades; // IDs of creatures to which this creature can be upgraded
  30. ui32 hitPoints, speed, attack, defence;
  31. ui32 fightValue, AIValue, growth, hordeGrowth, shots, spells;
  32. ui32 damageMin, damageMax;
  33. ui32 ammMin, ammMax;
  34. ui8 level; // 0 - unknown
  35. std::string abilityText; //description of abilities
  36. std::string abilityRefs; //references to abilities, in text format
  37. std::string animDefName;
  38. si32 idNumber;
  39. si8 faction; //-1 = neutral
  40. ui8 doubleWide;
  41. ///animation info
  42. float timeBetweenFidgets, walkAnimationTime, attackAnimationTime, flightAnimationDistance;
  43. int upperRightMissleOffsetX, rightMissleOffsetX, lowerRightMissleOffsetX, upperRightMissleOffsetY, rightMissleOffsetY, lowerRightMissleOffsetY;
  44. float missleFrameAngles[12];
  45. int troopCountLocationOffset, attackClimaxFrame;
  46. ///end of anim info
  47. bool isDoubleWide() const; //returns true if unit is double wide on battlefield
  48. bool isFlying() const; //returns true if it is a flying unit
  49. bool isShooting() const; //returns true if unit can shoot
  50. bool isUndead() const; //returns true if unit is undead
  51. bool isGood () const;
  52. bool isEvil () const;
  53. si32 maxAmount(const std::vector<si32> &res) const; //how many creatures can be bought
  54. 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
  55. static int estimateCreatureCount(unsigned int countID); //reverse version of above function, returns middle of range
  56. bool isMyUpgrade(const CCreature *anotherCre) const;
  57. bool valid() const;
  58. void addBonus(int val, int type, int subtype = -1);
  59. std::string nodeName() const OVERRIDE;
  60. //void getParents(TCNodes &out, const CBonusSystemNode *root /*= NULL*/) const;
  61. template<typename RanGen>
  62. int getRandomAmount(RanGen &ranGen) const
  63. {
  64. if(ammMax == ammMin)
  65. return ammMax;
  66. else
  67. return ammMin + (ranGen() % (ammMax - ammMin));
  68. }
  69. template <typename Handler> void serialize(Handler &h, const int version)
  70. {
  71. h & static_cast<CBonusSystemNode&>(*this);
  72. h & namePl & nameSing & nameRef
  73. & cost & upgrades
  74. & fightValue & AIValue & growth & hordeGrowth & hitPoints & speed & attack & defence & shots & spells
  75. & damageMin & damageMax & ammMin & ammMax & level
  76. & abilityText & abilityRefs & animDefName
  77. & idNumber & faction
  78. & timeBetweenFidgets & walkAnimationTime & attackAnimationTime & flightAnimationDistance
  79. & upperRightMissleOffsetX & rightMissleOffsetX & lowerRightMissleOffsetX & upperRightMissleOffsetY & rightMissleOffsetY & lowerRightMissleOffsetY
  80. & missleFrameAngles & troopCountLocationOffset & attackClimaxFrame;
  81. h & doubleWide;
  82. }
  83. CCreature();
  84. friend class CCreatureHandler;
  85. };
  86. class DLL_EXPORT CCreatureHandler
  87. {
  88. private: //?
  89. CBonusSystemNode allCreatures, creaturesOfLevel[CREATURES_PER_TOWN + 1];//index 0 is used for creatures of unknown tier or outside <1-7> range
  90. public:
  91. std::set<int> notUsedMonsters;
  92. std::set<TCreature> doubledCreatures; //they get double week
  93. std::vector<ConstTransitivePtr<CCreature> > creatures; //creature ID -> creature info
  94. bmap<std::string,int> nameToID;
  95. bmap<int,std::string> idToProjectile;
  96. bmap<int,bool> idToProjectileSpin; //if true, appropriate projectile is spinning during flight
  97. std::vector<si8> factionAlignments; //1 for good, 0 for neutral and -1 for evil with faction ID as index
  98. int factionToTurretCreature[F_NUMBER]; //which creature's animation should be used to dispaly creature in turret while siege
  99. std::map<TBonusType, std::pair<std::string, std::string> > stackBonuses; // bonus => name, description
  100. std::vector<std::vector<ui32> > expRanks; // stack experience needed for certain rank, index 0 for other tiers (?)
  101. std::vector<ui32> maxExpPerBattle; //%, tiers same as above
  102. si8 expAfterUpgrade;//multiplier in %
  103. void deserializationFix();
  104. void loadCreatures();
  105. void buildBonusTreeForTiers();
  106. void loadAnimationInfo();
  107. void loadUnitAnimInfo(CCreature & unit, std::string & src, int & i);
  108. void loadStackExp(Bonus & b, BonusList & bl, std::string & src, int & it);
  109. void loadMindImmunity(Bonus & b, BonusList & bl, std::string & src, int & it); //multiple bonuses at once
  110. int stringToNumber(std::string & s);//help function for parsing CREXPBON.txt
  111. bool isGood (si8 faction) const;
  112. bool isEvil (si8 faction) const;
  113. int pickRandomMonster(const boost::function<int()> &randGen = 0, int tier = -1) const; //tier <1 - CREATURES_PER_TOWN> or -1 for any
  114. void addBonusForTier(int tier, Bonus *b); //tier must be <1-7>
  115. void addBonusForAllCreatures(Bonus *b);
  116. CCreatureHandler();
  117. ~CCreatureHandler();
  118. template <typename Handler> void serialize(Handler &h, const int version)
  119. {
  120. //TODO: should be optimized, not all these informations needs to be serialized (same for ccreature)
  121. h & notUsedMonsters & creatures & nameToID & idToProjectile & idToProjectileSpin & factionToTurretCreature;
  122. h & stackBonuses & expRanks & maxExpPerBattle & expAfterUpgrade;
  123. h & allCreatures;
  124. h & creaturesOfLevel;
  125. BONUS_TREE_DESERIALIZATION_FIX
  126. }
  127. };
  128. #endif // __CCREATUREHANDLER_H__