BattleAction.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #define VCMI_DLL
  2. #include "BattleAction.h"
  3. #include "BattleState.h"
  4. /*
  5. * BattleAction.h, part of VCMI engine
  6. *
  7. * Authors: listed in file AUTHORS in main folder
  8. *
  9. * License: GNU General Public License v2.0 or later
  10. * Full text of license available in license.txt file, in main folder
  11. *
  12. */
  13. BattleAction::BattleAction()
  14. {
  15. side = -1;
  16. stackNumber = -1;
  17. actionType = INVALID;
  18. destinationTile = -1;
  19. additionalInfo = -1;
  20. }
  21. BattleAction BattleAction::makeDefend(const CStack *stack)
  22. {
  23. BattleAction ba;
  24. ba.side = !stack->attackerOwned;
  25. ba.actionType = DEFEND;
  26. ba.stackNumber = stack->ID;
  27. return ba;
  28. }
  29. BattleAction BattleAction::makeMeleeAttack( const CStack *stack, const CStack * attacked, std::vector<THex> reachableByAttacker )
  30. {
  31. BattleAction ba;
  32. ba.side = !stack->attackerOwned;
  33. ba.actionType = WALK_AND_ATTACK;
  34. ba.stackNumber = stack->ID;
  35. ba.destinationTile = -1;
  36. for (int g=0; g<reachableByAttacker.size(); ++g)
  37. {
  38. if (THex::mutualPosition(reachableByAttacker[g], attacked->position) >= 0 )
  39. {
  40. ba.destinationTile = reachableByAttacker[g];
  41. break;
  42. }
  43. }
  44. if (ba.destinationTile == -1)
  45. {
  46. //we couldn't determine appropriate pos
  47. //TODO: should we throw an exception?
  48. return makeDefend(stack);
  49. }
  50. ba.additionalInfo = attacked->position;
  51. return ba;
  52. }
  53. BattleAction BattleAction::makeWait(const CStack *stack)
  54. {
  55. BattleAction ba;
  56. ba.side = !stack->attackerOwned;
  57. ba.actionType = WAIT;
  58. ba.stackNumber = stack->ID;
  59. return ba;
  60. }
  61. BattleAction BattleAction::makeShotAttack(const CStack *shooter, const CStack *target)
  62. {
  63. BattleAction ba;
  64. ba.side = !shooter->attackerOwned;
  65. ba.actionType = SHOOT;
  66. ba.stackNumber = shooter->ID;
  67. ba.destinationTile = target->position;
  68. return ba;
  69. }
  70. BattleAction BattleAction::makeMove(const CStack *stack, THex dest)
  71. {
  72. BattleAction ba;
  73. ba.side = !stack->attackerOwned;
  74. ba.actionType = WALK;
  75. ba.stackNumber = stack->ID;
  76. ba.destinationTile = dest;
  77. return ba;
  78. }