BattleAction.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * BattleAction.cpp, 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. #include "StdInc.h"
  11. #include "BattleAction.h"
  12. #include "Unit.h"
  13. #include "CBattleInfoCallback.h"
  14. VCMI_LIB_NAMESPACE_BEGIN
  15. static const int32_t INVALID_UNIT_ID = -1000;
  16. BattleAction::BattleAction():
  17. side(BattleSide::NONE),
  18. stackNumber(-1),
  19. actionType(EActionType::NO_ACTION)
  20. {
  21. }
  22. BattleAction BattleAction::makeHeal(const battle::Unit * healer, const battle::Unit * healed)
  23. {
  24. BattleAction ba;
  25. ba.side = healer->unitSide();
  26. ba.actionType = EActionType::STACK_HEAL;
  27. ba.stackNumber = healer->unitId();
  28. ba.aimToUnit(healed);
  29. return ba;
  30. }
  31. BattleAction BattleAction::makeDefend(const battle::Unit * stack)
  32. {
  33. BattleAction ba;
  34. ba.side = stack->unitSide();
  35. ba.actionType = EActionType::DEFEND;
  36. ba.stackNumber = stack->unitId();
  37. return ba;
  38. }
  39. BattleAction BattleAction::makeMeleeAttack(const battle::Unit * stack, const BattleHex & destination, const BattleHex & attackFrom, bool returnAfterAttack)
  40. {
  41. BattleAction ba;
  42. ba.side = stack->unitSide(); //FIXME: will it fail if stack mind controlled?
  43. ba.actionType = EActionType::WALK_AND_ATTACK;
  44. ba.stackNumber = stack->unitId();
  45. ba.aimToHex(attackFrom);
  46. ba.aimToHex(destination);
  47. if(returnAfterAttack && stack->hasBonusOfType(BonusType::RETURN_AFTER_STRIKE))
  48. ba.aimToHex(stack->getPosition());
  49. return ba;
  50. }
  51. BattleAction BattleAction::makeWait(const battle::Unit * stack)
  52. {
  53. BattleAction ba;
  54. ba.side = stack->unitSide();
  55. ba.actionType = EActionType::WAIT;
  56. ba.stackNumber = stack->unitId();
  57. return ba;
  58. }
  59. BattleAction BattleAction::makeShotAttack(const battle::Unit * shooter, const battle::Unit * target)
  60. {
  61. BattleAction ba;
  62. ba.side = shooter->unitSide();
  63. ba.actionType = EActionType::SHOOT;
  64. ba.stackNumber = shooter->unitId();
  65. ba.aimToUnit(target);
  66. return ba;
  67. }
  68. BattleAction BattleAction::makeCreatureSpellcast(const battle::Unit * stack, const battle::Target & target, const SpellID & spellID)
  69. {
  70. BattleAction ba;
  71. ba.actionType = EActionType::MONSTER_SPELL;
  72. ba.spell = spellID;
  73. ba.setTarget(target);
  74. ba.side = stack->unitSide();
  75. ba.stackNumber = stack->unitId();
  76. return ba;
  77. }
  78. BattleAction BattleAction::makeMove(const battle::Unit * stack, const BattleHex & dest)
  79. {
  80. BattleAction ba;
  81. ba.side = stack->unitSide();
  82. ba.actionType = EActionType::WALK;
  83. ba.stackNumber = stack->unitId();
  84. ba.aimToHex(dest);
  85. return ba;
  86. }
  87. BattleAction BattleAction::makeEndOFTacticPhase(BattleSide side)
  88. {
  89. BattleAction ba;
  90. ba.side = side;
  91. ba.actionType = EActionType::END_TACTIC_PHASE;
  92. return ba;
  93. }
  94. BattleAction BattleAction::makeSurrender(BattleSide side)
  95. {
  96. BattleAction ba;
  97. ba.side = side;
  98. ba.actionType = EActionType::SURRENDER;
  99. return ba;
  100. }
  101. BattleAction BattleAction::makeRetreat(BattleSide side)
  102. {
  103. BattleAction ba;
  104. ba.side = side;
  105. ba.actionType = EActionType::RETREAT;
  106. return ba;
  107. }
  108. std::string BattleAction::toString() const
  109. {
  110. std::stringstream targetStream;
  111. for(const DestinationInfo & info : target)
  112. {
  113. if(info.unitValue == INVALID_UNIT_ID)
  114. {
  115. targetStream << info.hexValue;
  116. }
  117. else
  118. {
  119. targetStream << info.unitValue;
  120. targetStream << "@";
  121. targetStream << info.hexValue;
  122. }
  123. targetStream << ",";
  124. }
  125. boost::format fmt("{BattleAction: side '%d', stackNumber '%d', actionType '%s', actionSubtype '%d', target {%s}}");
  126. fmt % static_cast<int>(side) % stackNumber % static_cast<int>(actionType) % spell.getNum() % targetStream.str();
  127. return fmt.str();
  128. }
  129. void BattleAction::aimToHex(const BattleHex & destination)
  130. {
  131. DestinationInfo info;
  132. info.hexValue = destination;
  133. info.unitValue = INVALID_UNIT_ID;
  134. target.push_back(info);
  135. }
  136. void BattleAction::aimToUnit(const battle::Unit * destination)
  137. {
  138. DestinationInfo info;
  139. info.hexValue = destination->getPosition();
  140. info.unitValue = destination->unitId();
  141. target.push_back(info);
  142. }
  143. battle::Target BattleAction::getTarget(const CBattleInfoCallback * cb) const
  144. {
  145. battle::Target ret;
  146. for(const auto & destination : target)
  147. {
  148. if(destination.unitValue == INVALID_UNIT_ID)
  149. ret.emplace_back(destination.hexValue);
  150. else
  151. ret.emplace_back(cb->battleGetUnitByID(destination.unitValue));
  152. }
  153. return ret;
  154. }
  155. void BattleAction::setTarget(const battle::Target & target_)
  156. {
  157. target.clear();
  158. for(const auto & destination : target_)
  159. {
  160. if(destination.unitValue == nullptr)
  161. aimToHex(destination.hexValue);
  162. else
  163. aimToUnit(destination.unitValue);
  164. }
  165. }
  166. bool BattleAction::isUnitAction() const
  167. {
  168. static const std::array<EActionType, 109> actions = {
  169. EActionType::NO_ACTION,
  170. EActionType::WALK,
  171. EActionType::WAIT,
  172. EActionType::DEFEND,
  173. EActionType::WALK_AND_ATTACK,
  174. EActionType::SHOOT,
  175. EActionType::CATAPULT,
  176. EActionType::MONSTER_SPELL,
  177. EActionType::BAD_MORALE,
  178. EActionType::STACK_HEAL
  179. };
  180. return vstd::contains(actions, actionType);
  181. }
  182. bool BattleAction::isSpellAction() const
  183. {
  184. static const std::array<EActionType, 2> actions = {
  185. EActionType::HERO_SPELL,
  186. EActionType::MONSTER_SPELL
  187. };
  188. return vstd::contains(actions, actionType);
  189. }
  190. bool BattleAction::isBattleEndAction() const
  191. {
  192. static const std::array<EActionType, 2> actions = {
  193. EActionType::RETREAT,
  194. EActionType::SURRENDER
  195. };
  196. return vstd::contains(actions, actionType);
  197. }
  198. bool BattleAction::isTacticsAction() const
  199. {
  200. static const std::array<EActionType, 9> actions = {
  201. EActionType::WALK,
  202. EActionType::END_TACTIC_PHASE,
  203. EActionType::RETREAT,
  204. EActionType::SURRENDER
  205. };
  206. return vstd::contains(actions, actionType);
  207. }
  208. std::ostream & operator<<(std::ostream & os, const BattleAction & ba)
  209. {
  210. os << ba.toString();
  211. return os;
  212. }
  213. VCMI_LIB_NAMESPACE_END