CCreatureSet.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #ifndef __CCREATURESET_H__
  2. #define __CCREATURESET_H__
  3. #include "../global.h"
  4. #include <map>
  5. #include "HeroBonus.h"
  6. class CCreature;
  7. class CGHeroInstance;
  8. class CArmedInstance;
  9. //a few typedefs for CCreatureSet
  10. typedef si32 TSlot;
  11. typedef si32 TQuantity;
  12. typedef ui32 TCreature; //creature id
  13. const int ARMY_SIZE = 7;
  14. class DLL_EXPORT CStackInstance : public CBonusSystemNode
  15. {
  16. public:
  17. int idRand; //hlp variable used during loading game -> "id" placeholder for randomization
  18. const CArmedInstance *armyObj; //stack must be part of some army, army must be part of some object
  19. const CCreature *type;
  20. TQuantity count;
  21. ui32 experience; //TODO: handle
  22. //TODO: stack artifacts
  23. template <typename Handler> void serialize(Handler &h, const int version)
  24. {
  25. h & static_cast<CBonusSystemNode&>(*this);
  26. h & armyObj & type & count & experience;
  27. }
  28. //overrides CBonusSystemNode
  29. //void getParents(TCNodes &out, const CBonusSystemNode *source = NULL) const; //retrieves list of parent nodes (nodes to inherit bonuses from), source is the prinary asker
  30. int getQuantityID() const;
  31. std::string getQuantityTXT(bool capitalized = true) const;
  32. void init();
  33. CStackInstance();
  34. CStackInstance(TCreature id, TQuantity count, const CArmedInstance *ArmyObj = NULL);
  35. CStackInstance(const CCreature *cre, TQuantity count);
  36. void setType(int creID);
  37. };
  38. typedef std::map<TSlot, CStackInstance> TSlots;
  39. class DLL_EXPORT CCreatureSet //seven combined creatures
  40. {
  41. public:
  42. TSlots slots; //slots[slot_id]=> pair(creature_id,creature_quantity)
  43. ui8 formation; //false - wide, true - tight
  44. const CStackInstance operator[](TSlot slot) const;
  45. const TSlots &Slots() const {return slots;}
  46. void addToSlot(TSlot slot, TCreature cre, TQuantity count, bool allowMerging = true); //Adds stack to slot. Slot must be empty or with same type creature
  47. void addToSlot(TSlot slot, const CStackInstance &stack, bool allowMerging = true); //Adds stack to slot. Slot must be empty or with same type creature
  48. void addStack(TSlot slot, const CStackInstance &stack); //adds new stack to the army, slot must be empty
  49. bool setCreature (TSlot slot, TCreature type, TQuantity quantity); //slots 0 to 6, if quantity=0, erases stack
  50. void clear();
  51. void setFormation(bool tight);
  52. void setStackCount(TSlot slot, TQuantity count); //stack must exist!
  53. void eraseStack(TSlot slot);
  54. const CStackInstance& getStack(TSlot slot) const;
  55. const CCreature* getCreature(TSlot slot) const; //workaround of map issue;
  56. int getAmount (TSlot slot) const;
  57. TSlot getSlotFor(TCreature creature, ui32 slotsAmount=ARMY_SIZE) const; //returns -1 if no slot available
  58. bool mergableStacks(std::pair<TSlot, TSlot> &out, TSlot preferable = -1) const; //looks for two same stacks, returns slot positions;
  59. bool validTypes(bool allowUnrandomized = false) const; //checks if all types of creatures are set properly
  60. bool slotEmpty(TSlot slot) const;
  61. int stacksCount() const;
  62. virtual bool needsLastStack() const; //true if last stack cannot be taken
  63. int getArmyStrength() const; //sum of AI values of creatures
  64. ui64 getPower (TSlot slot) const; //value of specific stack
  65. std::string getRoughAmount (TSlot slot) const; //rough size of specific stack
  66. bool contains(const CStackInstance *stack) const;
  67. template <typename Handler> void serialize(Handler &h, const int version)
  68. {
  69. h & slots & formation;
  70. }
  71. operator bool() const
  72. {
  73. return slots.size() > 0;
  74. }
  75. void sweep();
  76. };
  77. #endif // __CCREATURESET_H__