BattleAction.cpp 4.5 KB

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