Catapult.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Catapult.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 "Catapult.h"
  12. #include "Registry.h"
  13. #include "../ISpellMechanics.h"
  14. #include "../../NetPacks.h"
  15. #include "../../battle/IBattleState.h"
  16. #include "../../battle/CBattleInfoCallback.h"
  17. #include "../../battle/Unit.h"
  18. #include "../../mapObjects/CGTownInstance.h"
  19. #include "../../serializer/JsonSerializeFormat.h"
  20. static const std::string EFFECT_NAME = "core:catapult";
  21. namespace spells
  22. {
  23. namespace effects
  24. {
  25. VCMI_REGISTER_SPELL_EFFECT(Catapult, EFFECT_NAME);
  26. Catapult::Catapult()
  27. : LocationEffect(),
  28. targetsToAttack(0)
  29. {
  30. }
  31. Catapult::~Catapult() = default;
  32. bool Catapult::applicable(Problem & problem, const Mechanics * m) const
  33. {
  34. auto town = m->cb->battleGetDefendedTown();
  35. if(nullptr == town)
  36. {
  37. return m->adaptProblem(ESpellCastProblem::NO_APPROPRIATE_TARGET, problem);
  38. }
  39. if(CGTownInstance::NONE == town->fortLevel())
  40. {
  41. return m->adaptProblem(ESpellCastProblem::NO_APPROPRIATE_TARGET, problem);
  42. }
  43. if(m->isSmart() && m->casterSide != BattleSide::ATTACKER)
  44. {
  45. //if spell targeting is smart, then only attacker can use it
  46. return m->adaptProblem(ESpellCastProblem::NO_APPROPRIATE_TARGET, problem);
  47. }
  48. const auto attackableBattleHexes = m->cb->getAttackableBattleHexes();
  49. if(attackableBattleHexes.empty())
  50. return m->adaptProblem(ESpellCastProblem::NO_APPROPRIATE_TARGET, problem);
  51. return true;
  52. }
  53. void Catapult::apply(BattleStateProxy * battleState, RNG & rng, const Mechanics * m, const EffectTarget & target) const
  54. {
  55. //start with all destructible parts
  56. static const std::set<EWallPart::EWallPart> possibleTargets =
  57. {
  58. EWallPart::KEEP,
  59. EWallPart::BOTTOM_TOWER,
  60. EWallPart::BOTTOM_WALL,
  61. EWallPart::BELOW_GATE,
  62. EWallPart::OVER_GATE,
  63. EWallPart::UPPER_WALL,
  64. EWallPart::UPPER_TOWER,
  65. EWallPart::GATE
  66. };
  67. assert(possibleTargets.size() == EWallPart::PARTS_COUNT);
  68. CatapultAttack ca;
  69. ca.attacker = -1;
  70. BattleUnitsChanged removeUnits;
  71. for(int i = 0; i < targetsToAttack; i++)
  72. {
  73. //Any destructible part can be hit regardless of its HP. Multiple hit on same target is allowed.
  74. EWallPart::EWallPart target = *RandomGeneratorUtil::nextItem(possibleTargets, rng);
  75. auto state = m->cb->battleGetWallState(target);
  76. if(state == EWallState::DESTROYED || state == EWallState::NONE)
  77. continue;
  78. CatapultAttack::AttackInfo attackInfo;
  79. attackInfo.damageDealt = 1;
  80. attackInfo.attackedPart = target;
  81. attackInfo.destinationTile = m->cb->wallPartToBattleHex(target);
  82. ca.attackedParts.push_back(attackInfo);
  83. //removing creatures in turrets / keep if one is destroyed
  84. BattleHex posRemove;
  85. switch(target)
  86. {
  87. case EWallPart::KEEP:
  88. posRemove = -2;
  89. break;
  90. case EWallPart::BOTTOM_TOWER:
  91. posRemove = -3;
  92. break;
  93. case EWallPart::UPPER_TOWER:
  94. posRemove = -4;
  95. break;
  96. }
  97. if(posRemove != BattleHex::INVALID && state - attackInfo.damageDealt <= 0) //HP enum subtraction not intuitive, consider using SiegeInfo::applyDamage
  98. {
  99. auto all = m->cb->battleGetUnitsIf([=](const battle::Unit * unit)
  100. {
  101. return !unit->isGhost();
  102. });
  103. for(auto & elem : all)
  104. {
  105. if(elem->getPosition() == posRemove)
  106. {
  107. removeUnits.changedStacks.emplace_back(elem->unitId(), UnitChanges::EOperation::REMOVE);
  108. break;
  109. }
  110. }
  111. }
  112. }
  113. battleState->apply(&ca);
  114. if(!removeUnits.changedStacks.empty())
  115. battleState->apply(&removeUnits);
  116. }
  117. void Catapult::serializeJsonEffect(JsonSerializeFormat & handler)
  118. {
  119. //TODO: add configuration unifying with Catapult ability
  120. handler.serializeInt("targetsToAttack", targetsToAttack);
  121. }
  122. }
  123. }