BattleAction.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. static const int32_t INVALID_UNIT_ID = -1000;
  15. BattleAction::BattleAction():
  16. side(-1),
  17. stackNumber(-1),
  18. actionType(EActionType::INVALID),
  19. actionSubtype(-1)
  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(Bonus::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::makeMove(const battle::Unit * stack, BattleHex dest)
  69. {
  70. BattleAction ba;
  71. ba.side = stack->unitSide();
  72. ba.actionType = EActionType::WALK;
  73. ba.stackNumber = stack->unitId();
  74. ba.aimToHex(dest);
  75. return ba;
  76. }
  77. BattleAction BattleAction::makeEndOFTacticPhase(ui8 side)
  78. {
  79. BattleAction ba;
  80. ba.side = side;
  81. ba.actionType = EActionType::END_TACTIC_PHASE;
  82. return ba;
  83. }
  84. std::string BattleAction::toString() const
  85. {
  86. std::stringstream actionTypeStream;
  87. actionTypeStream << actionType;
  88. std::stringstream targetStream;
  89. for(const DestinationInfo & info : target)
  90. {
  91. if(info.unitValue == INVALID_UNIT_ID)
  92. {
  93. targetStream << info.hexValue;
  94. }
  95. else
  96. {
  97. targetStream << info.unitValue;
  98. targetStream << "@";
  99. targetStream << info.hexValue;
  100. }
  101. targetStream << ",";
  102. }
  103. boost::format fmt("{BattleAction: side '%d', stackNumber '%d', actionType '%s', actionSubtype '%d', target {%s}}");
  104. fmt % static_cast<int>(side) % stackNumber % actionTypeStream.str() % actionSubtype % targetStream.str();
  105. return fmt.str();
  106. }
  107. void BattleAction::aimToHex(const BattleHex & destination)
  108. {
  109. DestinationInfo info;
  110. info.hexValue = destination;
  111. info.unitValue = INVALID_UNIT_ID;
  112. target.push_back(info);
  113. }
  114. void BattleAction::aimToUnit(const battle::Unit * destination)
  115. {
  116. DestinationInfo info;
  117. info.hexValue = destination->getPosition();
  118. info.unitValue = destination->unitId();
  119. target.push_back(info);
  120. }
  121. battle::Target BattleAction::getTarget(const CBattleInfoCallback * cb) const
  122. {
  123. battle::Target ret;
  124. for(auto & destination : target)
  125. {
  126. if(destination.unitValue == INVALID_UNIT_ID)
  127. ret.emplace_back(destination.hexValue);
  128. else
  129. ret.emplace_back(cb->battleGetUnitByID(destination.unitValue));
  130. }
  131. return ret;
  132. }
  133. void BattleAction::setTarget(const battle::Target & target_)
  134. {
  135. target.clear();
  136. for(auto & destination : target_)
  137. {
  138. if(destination.unitValue == nullptr)
  139. aimToHex(destination.hexValue);
  140. else
  141. aimToUnit(destination.unitValue);
  142. }
  143. }
  144. std::ostream & operator<<(std::ostream & os, const BattleAction & ba)
  145. {
  146. os << ba.toString();
  147. return os;
  148. }