Actors.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * AINodeStorage.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "../../../lib/mapObjects/CGHeroInstance.h"
  12. #include "../AIUtility.h"
  13. #include "Actions/SpecialAction.h"
  14. namespace NKAI
  15. {
  16. extern const uint64_t MIN_ARMY_STRENGTH_FOR_CHAIN;
  17. class ChainActor;
  18. class HeroActor;
  19. class Nullkiller;
  20. class HeroExchangeArmy : public CArmedInstance
  21. {
  22. public:
  23. TResources armyCost;
  24. bool requireBuyArmy;
  25. bool needsLastStack() const override;
  26. std::shared_ptr<SpecialAction> getActorAction() const;
  27. HeroExchangeArmy(): CArmedInstance(nullptr, true), requireBuyArmy(false) {}
  28. };
  29. struct ExchangeResult
  30. {
  31. bool lockAcquired;
  32. ChainActor * actor;
  33. ExchangeResult() : lockAcquired(true), actor(nullptr) {}
  34. };
  35. class ChainActor
  36. {
  37. protected:
  38. ChainActor(const CGHeroInstance * hero, HeroRole heroRole, uint64_t chainMask);
  39. ChainActor(const ChainActor * carrier, const ChainActor * other, const CCreatureSet * heroArmy);
  40. ChainActor(const CGObjectInstance * obj, const CCreatureSet * army, uint64_t chainMask, int initialTurn);
  41. public:
  42. uint64_t chainMask;
  43. bool isMovable = false;
  44. bool allowUseResources = false;
  45. bool allowBattle = false;
  46. bool allowSpellCast = false;
  47. std::shared_ptr<SpecialAction> actorAction;
  48. const CGHeroInstance * hero;
  49. HeroRole heroRole;
  50. const CCreatureSet * creatureSet = nullptr;
  51. const ChainActor * battleActor = nullptr;
  52. const ChainActor * castActor = nullptr;
  53. const ChainActor * resourceActor = nullptr;
  54. const ChainActor * carrierParent = nullptr;
  55. const ChainActor * otherParent = nullptr;
  56. const ChainActor * baseActor = nullptr;
  57. int3 initialPosition;
  58. EPathfindingLayer layer;
  59. uint32_t initialMovement = 0;
  60. uint32_t initialTurn = 0;
  61. uint64_t armyValue;
  62. float heroFightingStrength;
  63. uint8_t actorExchangeCount;
  64. TResources armyCost;
  65. std::shared_ptr<TurnInfo> tiCache;
  66. ChainActor() = default;
  67. virtual ~ChainActor() = default;
  68. virtual std::string toString() const;
  69. ExchangeResult tryExchangeNoLock(const ChainActor * other) const { return tryExchangeNoLock(this, other); }
  70. void setBaseActor(HeroActor * base);
  71. virtual const CGObjectInstance * getActorObject() const { return hero; }
  72. int maxMovePoints(EPathfindingLayer layer);
  73. protected:
  74. virtual ExchangeResult tryExchangeNoLock(const ChainActor * specialActor, const ChainActor * other) const;
  75. };
  76. class HeroExchangeMap
  77. {
  78. private:
  79. const HeroActor * actor;
  80. std::map<const ChainActor *, HeroActor *> exchangeMap;
  81. const Nullkiller * ai;
  82. boost::shared_mutex sync;
  83. public:
  84. HeroExchangeMap(const HeroActor * actor, const Nullkiller * ai);
  85. ~HeroExchangeMap();
  86. ExchangeResult tryExchangeNoLock(const ChainActor * other);
  87. private:
  88. HeroExchangeArmy * pickBestCreatures(const CCreatureSet * army1, const CCreatureSet * army2) const;
  89. HeroExchangeArmy * tryUpgrade(const CCreatureSet * army, const CGObjectInstance * upgrader, TResources resources) const;
  90. };
  91. class HeroActor : public ChainActor
  92. {
  93. public:
  94. static const int SPECIAL_ACTORS_COUNT = 7;
  95. private:
  96. std::array<ChainActor, SPECIAL_ACTORS_COUNT> specialActors;
  97. std::unique_ptr<HeroExchangeMap> exchangeMap;
  98. void setupSpecialActors();
  99. public:
  100. std::shared_ptr<SpecialAction> exchangeAction;
  101. // chain flags, can be combined meaning hero exchange and so on
  102. HeroActor(const CGHeroInstance * hero, HeroRole heroRole, uint64_t chainMask, const Nullkiller * ai);
  103. HeroActor(const ChainActor * carrier, const ChainActor * other, const HeroExchangeArmy * army, const Nullkiller * ai);
  104. protected:
  105. ExchangeResult tryExchangeNoLock(const ChainActor * specialActor, const ChainActor * other) const override;
  106. };
  107. class ObjectActor : public ChainActor
  108. {
  109. private:
  110. const CGObjectInstance * object;
  111. public:
  112. ObjectActor(const CGObjectInstance * obj, const CCreatureSet * army, uint64_t chainMask, int initialTurn);
  113. std::string toString() const override;
  114. const CGObjectInstance * getActorObject() const override;
  115. };
  116. class HillFortActor : public ObjectActor
  117. {
  118. public:
  119. HillFortActor(const CGObjectInstance * hillFort, uint64_t chainMask);
  120. };
  121. class DwellingActor : public ObjectActor
  122. {
  123. private:
  124. const CGDwelling * dwelling;
  125. public:
  126. DwellingActor(const CGDwelling * dwelling, uint64_t chainMask, bool waitForGrowth, int dayOfWeek);
  127. ~DwellingActor();
  128. std::string toString() const override;
  129. protected:
  130. int getInitialTurn(bool waitForGrowth, int dayOfWeek);
  131. CCreatureSet * getDwellingCreatures(const CGDwelling * dwelling, bool waitForGrowth);
  132. };
  133. class TownGarrisonActor : public ObjectActor
  134. {
  135. private:
  136. const CGTownInstance * town;
  137. public:
  138. TownGarrisonActor(const CGTownInstance * town, uint64_t chainMask);
  139. std::string toString() const override;
  140. };
  141. }