CCreatureHandler.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 "CSoundBase.h"
  9. #include "../lib/StackFeature.h"
  10. /*
  11. * CCreatureHandler.h, part of VCMI engine
  12. *
  13. * Authors: listed in file AUTHORS in main folder
  14. *
  15. * License: GNU General Public License v2.0 or later
  16. * Full text of license available in license.txt file, in main folder
  17. *
  18. */
  19. class CLodHandler;
  20. class DLL_EXPORT CCreature
  21. {
  22. public:
  23. std::string namePl, nameSing, nameRef; //name in singular and plural form; and reference name
  24. std::vector<ui32> cost; //cost[res_id] - amount of that resource
  25. std::set<ui32> upgrades; // IDs of creatures to which this creature can be upgraded
  26. ui32 fightValue, AIValue, growth, hordeGrowth, hitPoints, speed, attack, defence, shots, spells;
  27. ui32 damageMin, damageMax;
  28. ui32 ammMin, ammMax;
  29. ui8 level; // 0 - unknown
  30. std::string abilityText; //description of abilities
  31. std::string abilityRefs; //references to abilities, in textformat
  32. std::string animDefName;
  33. ui32 idNumber;
  34. std::vector<StackFeature> abilities;
  35. si8 faction; //-1 = neutral
  36. ///animation info
  37. float timeBetweenFidgets, walkAnimationTime, attackAnimationTime, flightAnimationDistance;
  38. int upperRightMissleOffsetX, rightMissleOffsetX, lowerRightMissleOffsetX, upperRightMissleOffsetY, rightMissleOffsetY, lowerRightMissleOffsetY;
  39. float missleFrameAngles[12];
  40. int troopCountLocationOffset, attackClimaxFrame;
  41. ///end of anim info
  42. // Sound infos
  43. struct {
  44. soundBase::soundID attack;
  45. soundBase::soundID defend;
  46. soundBase::soundID killed; // was killed died
  47. soundBase::soundID move;
  48. soundBase::soundID shoot; // range attack
  49. soundBase::soundID wince; // attacked but did not die
  50. soundBase::soundID ext1; // creature specific extension
  51. soundBase::soundID ext2; // creature specific extension
  52. soundBase::soundID startMoving; // usually same as ext1
  53. soundBase::soundID endMoving; // usually same as ext2
  54. template <typename Handler> void serialize(Handler &h, const int version)
  55. {
  56. h & attack & defend & killed & move & shoot & wince & ext1 & ext2 & startMoving & endMoving;
  57. }
  58. } sounds;
  59. bool isDoubleWide() const; //returns true if unit is double wide on battlefield
  60. bool isFlying() const; //returns true if it is a flying unit
  61. bool isShooting() const; //returns true if unit can shoot
  62. bool isUndead() const; //returns true if unit is undead
  63. bool isGood () const;
  64. bool isEvil () const;
  65. si32 maxAmount(const std::vector<si32> &res) const; //how many creatures can be bought
  66. 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
  67. template<typename RanGen>
  68. int getRandomAmount(RanGen &ranGen)
  69. {
  70. if(ammMax == ammMin)
  71. return ammMax;
  72. else
  73. return ammMin + (ranGen() % (ammMax - ammMin));
  74. }
  75. template <typename Handler> void serialize(Handler &h, const int version)
  76. {
  77. h & namePl & nameSing & nameRef
  78. & cost & upgrades
  79. & fightValue & AIValue & growth & hordeGrowth & hitPoints & speed & attack & defence & shots & spells
  80. & damageMin & damageMax & ammMin & ammMax & level
  81. & abilityText & abilityRefs & animDefName
  82. & idNumber & abilities & faction
  83. & timeBetweenFidgets & walkAnimationTime & attackAnimationTime & flightAnimationDistance
  84. & upperRightMissleOffsetX & rightMissleOffsetX & lowerRightMissleOffsetX & upperRightMissleOffsetY & rightMissleOffsetY & lowerRightMissleOffsetY
  85. & missleFrameAngles & troopCountLocationOffset & attackClimaxFrame;
  86. h & sounds;
  87. }
  88. };
  89. class DLL_EXPORT CCreatureHandler
  90. {
  91. public:
  92. std::set<int> notUsedMonsters;
  93. std::vector<CCreature> creatures; //creature ID -> creature info
  94. std::map<int,std::vector<CCreature*> > levelCreatures; //level -> list of creatures
  95. std::map<std::string,int> nameToID;
  96. std::map<int,std::string> idToProjectile;
  97. std::map<int,bool> idToProjectileSpin; //if true, appropriate projectile is spinning during flight
  98. std::vector<si8> factionAlignments; //1 for good, 0 for neutral and -1 for evil with faction ID as index
  99. int factionToTurretCreature[F_NUMBER]; //which creature's animation should be used to dispaly creature in turret while siege
  100. void loadCreatures();
  101. void loadAnimationInfo();
  102. void loadUnitAnimInfo(CCreature & unit, std::string & src, int & i);
  103. bool isGood (si8 faction) const;
  104. bool isEvil (si8 faction) const;
  105. CCreatureHandler();
  106. ~CCreatureHandler();
  107. template <typename Handler> void serialize(Handler &h, const int version)
  108. {
  109. //TODO: should be optimized, not all these informations needs to be serialized (same for ccreature)
  110. h & notUsedMonsters & creatures & nameToID & idToProjectile & idToProjectileSpin & factionToTurretCreature;
  111. if(!h.saving)
  112. {
  113. for (int i=0; i<creatures.size(); i++) //recreate levelCreatures map
  114. {
  115. levelCreatures[creatures[i].level].push_back(&creatures[i]);
  116. }
  117. }
  118. }
  119. };
  120. #endif // __CCREATUREHANDLER_H__