CCreatureSet.h 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. void init();
  32. CStackInstance();
  33. CStackInstance(TCreature id, TQuantity count, const CArmedInstance *ArmyObj = NULL);
  34. CStackInstance(const CCreature *cre, TQuantity count);
  35. void setType(int creID);
  36. };
  37. typedef std::map<TSlot, CStackInstance> TSlots;
  38. class DLL_EXPORT CCreatureSet //seven combined creatures
  39. {
  40. public:
  41. TSlots slots; //slots[slot_id]=> pair(creature_id,creature_quantity)
  42. ui8 formation; //false - wide, true - tight
  43. const CStackInstance operator[](TSlot slot) const;
  44. const TSlots &Slots() const {return slots;}
  45. void addToSlot(TSlot slot, TCreature cre, TQuantity count, bool allowMerging = true); //Adds stack to slot. Slot must be empty or with same type creature
  46. void addToSlot(TSlot slot, const CStackInstance &stack, bool allowMerging = true); //Adds stack to slot. Slot must be empty or with same type creature
  47. void addStack(TSlot slot, const CStackInstance &stack); //adds new stack to the army, slot must be empty
  48. bool setCreature (TSlot slot, TCreature type, TQuantity quantity); //slots 0 to 6, if quantity=0, erases stack
  49. void clear();
  50. void setFormation(bool tight);
  51. void setStackCount(TSlot slot, TQuantity count); //stack must exist!
  52. void eraseStack(TSlot slot);
  53. const CStackInstance& getStack(TSlot slot) const;
  54. const CCreature* getCreature(TSlot slot) const; //workaround of map issue;
  55. int getAmount (TSlot slot) const;
  56. TSlot getSlotFor(TCreature creature, ui32 slotsAmount=ARMY_SIZE) const; //returns -1 if no slot available
  57. bool mergableStacks(std::pair<TSlot, TSlot> &out, TSlot preferable = -1) const; //looks for two same stacks, returns slot positions;
  58. bool validTypes(bool allowUnrandomized = false) const; //checks if all types of creatures are set properly
  59. bool slotEmpty(TSlot slot) const;
  60. int stacksCount() const;
  61. virtual bool needsLastStack() const; //true if last stack cannot be taken
  62. int getArmyStrength() const; //sum of AI values of creatures
  63. ui64 getPower (TSlot slot) const; //value of specific stack
  64. std::string getRoughAmount (TSlot slot) const; //rough size of specific stack
  65. bool contains(const CStackInstance *stack) const;
  66. template <typename Handler> void serialize(Handler &h, const int version)
  67. {
  68. h & slots & formation;
  69. }
  70. operator bool() const
  71. {
  72. return slots.size() > 0;
  73. }
  74. void sweep();
  75. };
  76. #endif // __CCREATURESET_H__