BattleAction.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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(-1),
  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, BattleHex destination, 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, 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(ui8 side)
  88. {
  89. BattleAction ba;
  90. ba.side = side;
  91. ba.actionType = EActionType::END_TACTIC_PHASE;
  92. return ba;
  93. }
  94. BattleAction BattleAction::makeSurrender(ui8 side)
  95. {
  96. BattleAction ba;
  97. ba.side = side;
  98. ba.actionType = EActionType::SURRENDER;
  99. return ba;
  100. }
  101. BattleAction BattleAction::makeRetreat(ui8 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 actionTypeStream;
  111. actionTypeStream << actionType;
  112. std::stringstream targetStream;
  113. for(const DestinationInfo & info : target)
  114. {
  115. if(info.unitValue == INVALID_UNIT_ID)
  116. {
  117. targetStream << info.hexValue;
  118. }
  119. else
  120. {
  121. targetStream << info.unitValue;
  122. targetStream << "@";
  123. targetStream << info.hexValue;
  124. }
  125. targetStream << ",";
  126. }
  127. boost::format fmt("{BattleAction: side '%d', stackNumber '%d', actionType '%s', actionSubtype '%d', target {%s}}");
  128. fmt % static_cast<int>(side) % stackNumber % actionTypeStream.str() % spell.getNum() % targetStream.str();
  129. return fmt.str();
  130. }
  131. void BattleAction::aimToHex(const BattleHex & destination)
  132. {
  133. DestinationInfo info;
  134. info.hexValue = destination;
  135. info.unitValue = INVALID_UNIT_ID;
  136. target.push_back(info);
  137. }
  138. void BattleAction::aimToUnit(const battle::Unit * destination)
  139. {
  140. DestinationInfo info;
  141. info.hexValue = destination->getPosition();
  142. info.unitValue = destination->unitId();
  143. target.push_back(info);
  144. }
  145. battle::Target BattleAction::getTarget(const CBattleInfoCallback * cb) const
  146. {
  147. battle::Target ret;
  148. for(const auto & destination : target)
  149. {
  150. if(destination.unitValue == INVALID_UNIT_ID)
  151. ret.emplace_back(destination.hexValue);
  152. else
  153. ret.emplace_back(cb->battleGetUnitByID(destination.unitValue));
  154. }
  155. return ret;
  156. }
  157. void BattleAction::setTarget(const battle::Target & target_)
  158. {
  159. target.clear();
  160. for(const auto & destination : target_)
  161. {
  162. if(destination.unitValue == nullptr)
  163. aimToHex(destination.hexValue);
  164. else
  165. aimToUnit(destination.unitValue);
  166. }
  167. }
  168. bool BattleAction::isUnitAction() const
  169. {
  170. static const std::array<EActionType, 109> actions = {
  171. EActionType::NO_ACTION,
  172. EActionType::WALK,
  173. EActionType::WAIT,
  174. EActionType::DEFEND,
  175. EActionType::WALK_AND_ATTACK,
  176. EActionType::SHOOT,
  177. EActionType::CATAPULT,
  178. EActionType::MONSTER_SPELL,
  179. EActionType::BAD_MORALE,
  180. EActionType::STACK_HEAL
  181. };
  182. return vstd::contains(actions, actionType);
  183. }
  184. bool BattleAction::isSpellAction() const
  185. {
  186. static const std::array<EActionType, 2> actions = {
  187. EActionType::HERO_SPELL,
  188. EActionType::MONSTER_SPELL
  189. };
  190. return vstd::contains(actions, actionType);
  191. }
  192. bool BattleAction::isBattleEndAction() const
  193. {
  194. static const std::array<EActionType, 2> actions = {
  195. EActionType::RETREAT,
  196. EActionType::SURRENDER
  197. };
  198. return vstd::contains(actions, actionType);
  199. }
  200. bool BattleAction::isTacticsAction() const
  201. {
  202. static const std::array<EActionType, 9> actions = {
  203. EActionType::WALK,
  204. EActionType::END_TACTIC_PHASE,
  205. EActionType::RETREAT,
  206. EActionType::SURRENDER
  207. };
  208. return vstd::contains(actions, actionType);
  209. }
  210. std::ostream & operator<<(std::ostream & os, const BattleAction & ba)
  211. {
  212. os << ba.toString();
  213. return os;
  214. }
  215. VCMI_LIB_NAMESPACE_END