BattleAction.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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::makeCreatureSpellcast(const battle::Unit * stack, const battle::Target & target, SpellID spellID)
  69. {
  70. BattleAction ba;
  71. ba.actionType = EActionType::MONSTER_SPELL;
  72. ba.actionSubtype = 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. std::string BattleAction::toString() const
  95. {
  96. std::stringstream actionTypeStream;
  97. actionTypeStream << actionType;
  98. std::stringstream targetStream;
  99. for(const DestinationInfo & info : target)
  100. {
  101. if(info.unitValue == INVALID_UNIT_ID)
  102. {
  103. targetStream << info.hexValue;
  104. }
  105. else
  106. {
  107. targetStream << info.unitValue;
  108. targetStream << "@";
  109. targetStream << info.hexValue;
  110. }
  111. targetStream << ",";
  112. }
  113. boost::format fmt("{BattleAction: side '%d', stackNumber '%d', actionType '%s', actionSubtype '%d', target {%s}}");
  114. fmt % static_cast<int>(side) % stackNumber % actionTypeStream.str() % actionSubtype % targetStream.str();
  115. return fmt.str();
  116. }
  117. void BattleAction::aimToHex(const BattleHex & destination)
  118. {
  119. DestinationInfo info;
  120. info.hexValue = destination;
  121. info.unitValue = INVALID_UNIT_ID;
  122. target.push_back(info);
  123. }
  124. void BattleAction::aimToUnit(const battle::Unit * destination)
  125. {
  126. DestinationInfo info;
  127. info.hexValue = destination->getPosition();
  128. info.unitValue = destination->unitId();
  129. target.push_back(info);
  130. }
  131. battle::Target BattleAction::getTarget(const CBattleInfoCallback * cb) const
  132. {
  133. battle::Target ret;
  134. for(auto & destination : target)
  135. {
  136. if(destination.unitValue == INVALID_UNIT_ID)
  137. ret.emplace_back(destination.hexValue);
  138. else
  139. ret.emplace_back(cb->battleGetUnitByID(destination.unitValue));
  140. }
  141. return ret;
  142. }
  143. void BattleAction::setTarget(const battle::Target & target_)
  144. {
  145. target.clear();
  146. for(auto & destination : target_)
  147. {
  148. if(destination.unitValue == nullptr)
  149. aimToHex(destination.hexValue);
  150. else
  151. aimToUnit(destination.unitValue);
  152. }
  153. }
  154. std::ostream & operator<<(std::ostream & os, const BattleAction & ba)
  155. {
  156. os << ba.toString();
  157. return os;
  158. }