BattleAction.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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::INVALID),
  20. actionSubtype(-1)
  21. {
  22. }
  23. BattleAction BattleAction::makeHeal(const battle::Unit * healer, const battle::Unit * healed)
  24. {
  25. BattleAction ba;
  26. ba.side = healer->unitSide();
  27. ba.actionType = EActionType::STACK_HEAL;
  28. ba.stackNumber = healer->unitId();
  29. ba.aimToUnit(healed);
  30. return ba;
  31. }
  32. BattleAction BattleAction::makeDefend(const battle::Unit * stack)
  33. {
  34. BattleAction ba;
  35. ba.side = stack->unitSide();
  36. ba.actionType = EActionType::DEFEND;
  37. ba.stackNumber = stack->unitId();
  38. return ba;
  39. }
  40. BattleAction BattleAction::makeMeleeAttack(const battle::Unit * stack, BattleHex destination, BattleHex attackFrom, bool returnAfterAttack)
  41. {
  42. BattleAction ba;
  43. ba.side = stack->unitSide(); //FIXME: will it fail if stack mind controlled?
  44. ba.actionType = EActionType::WALK_AND_ATTACK;
  45. ba.stackNumber = stack->unitId();
  46. ba.aimToHex(attackFrom);
  47. ba.aimToHex(destination);
  48. if(returnAfterAttack && stack->hasBonusOfType(Bonus::RETURN_AFTER_STRIKE))
  49. ba.aimToHex(stack->getPosition());
  50. return ba;
  51. }
  52. BattleAction BattleAction::makeWait(const battle::Unit * stack)
  53. {
  54. BattleAction ba;
  55. ba.side = stack->unitSide();
  56. ba.actionType = EActionType::WAIT;
  57. ba.stackNumber = stack->unitId();
  58. return ba;
  59. }
  60. BattleAction BattleAction::makeShotAttack(const battle::Unit * shooter, const battle::Unit * target)
  61. {
  62. BattleAction ba;
  63. ba.side = shooter->unitSide();
  64. ba.actionType = EActionType::SHOOT;
  65. ba.stackNumber = shooter->unitId();
  66. ba.aimToUnit(target);
  67. return ba;
  68. }
  69. BattleAction BattleAction::makeCreatureSpellcast(const battle::Unit * stack, const battle::Target & target, const SpellID & spellID)
  70. {
  71. BattleAction ba;
  72. ba.actionType = EActionType::MONSTER_SPELL;
  73. ba.actionSubtype = spellID;
  74. ba.setTarget(target);
  75. ba.side = stack->unitSide();
  76. ba.stackNumber = stack->unitId();
  77. return ba;
  78. }
  79. BattleAction BattleAction::makeMove(const battle::Unit * stack, BattleHex dest)
  80. {
  81. BattleAction ba;
  82. ba.side = stack->unitSide();
  83. ba.actionType = EActionType::WALK;
  84. ba.stackNumber = stack->unitId();
  85. ba.aimToHex(dest);
  86. return ba;
  87. }
  88. BattleAction BattleAction::makeEndOFTacticPhase(ui8 side)
  89. {
  90. BattleAction ba;
  91. ba.side = side;
  92. ba.actionType = EActionType::END_TACTIC_PHASE;
  93. return ba;
  94. }
  95. BattleAction BattleAction::makeSurrender(ui8 side)
  96. {
  97. BattleAction ba;
  98. ba.side = side;
  99. ba.actionType = EActionType::SURRENDER;
  100. return ba;
  101. }
  102. BattleAction BattleAction::makeRetreat(ui8 side)
  103. {
  104. BattleAction ba;
  105. ba.side = side;
  106. ba.actionType = EActionType::RETREAT;
  107. return ba;
  108. }
  109. std::string BattleAction::toString() const
  110. {
  111. std::stringstream actionTypeStream;
  112. actionTypeStream << actionType;
  113. std::stringstream targetStream;
  114. for(const DestinationInfo & info : target)
  115. {
  116. if(info.unitValue == INVALID_UNIT_ID)
  117. {
  118. targetStream << info.hexValue;
  119. }
  120. else
  121. {
  122. targetStream << info.unitValue;
  123. targetStream << "@";
  124. targetStream << info.hexValue;
  125. }
  126. targetStream << ",";
  127. }
  128. boost::format fmt("{BattleAction: side '%d', stackNumber '%d', actionType '%s', actionSubtype '%d', target {%s}}");
  129. fmt % static_cast<int>(side) % stackNumber % actionTypeStream.str() % actionSubtype % targetStream.str();
  130. return fmt.str();
  131. }
  132. void BattleAction::aimToHex(const BattleHex & destination)
  133. {
  134. DestinationInfo info;
  135. info.hexValue = destination;
  136. info.unitValue = INVALID_UNIT_ID;
  137. target.push_back(info);
  138. }
  139. void BattleAction::aimToUnit(const battle::Unit * destination)
  140. {
  141. DestinationInfo info;
  142. info.hexValue = destination->getPosition();
  143. info.unitValue = destination->unitId();
  144. target.push_back(info);
  145. }
  146. battle::Target BattleAction::getTarget(const CBattleInfoCallback * cb) const
  147. {
  148. battle::Target ret;
  149. for(const auto & destination : target)
  150. {
  151. if(destination.unitValue == INVALID_UNIT_ID)
  152. ret.emplace_back(destination.hexValue);
  153. else
  154. ret.emplace_back(cb->battleGetUnitByID(destination.unitValue));
  155. }
  156. return ret;
  157. }
  158. void BattleAction::setTarget(const battle::Target & target_)
  159. {
  160. target.clear();
  161. for(const auto & destination : target_)
  162. {
  163. if(destination.unitValue == nullptr)
  164. aimToHex(destination.hexValue);
  165. else
  166. aimToUnit(destination.unitValue);
  167. }
  168. }
  169. std::ostream & operator<<(std::ostream & os, const BattleAction & ba)
  170. {
  171. os << ba.toString();
  172. return os;
  173. }
  174. VCMI_LIB_NAMESPACE_END