Catapult.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. VCMI_LIB_NAMESPACE_BEGIN
  21. static const std::string EFFECT_NAME = "core:catapult";
  22. namespace spells
  23. {
  24. namespace effects
  25. {
  26. VCMI_REGISTER_SPELL_EFFECT(Catapult, EFFECT_NAME);
  27. Catapult::Catapult()
  28. : LocationEffect(),
  29. targetsToAttack(0)
  30. {
  31. }
  32. Catapult::~Catapult() = default;
  33. bool Catapult::applicable(Problem & problem, const Mechanics * m) const
  34. {
  35. auto town = m->battle()->battleGetDefendedTown();
  36. if(nullptr == town)
  37. {
  38. return m->adaptProblem(ESpellCastProblem::NO_APPROPRIATE_TARGET, problem);
  39. }
  40. if(CGTownInstance::NONE == town->fortLevel())
  41. {
  42. return m->adaptProblem(ESpellCastProblem::NO_APPROPRIATE_TARGET, problem);
  43. }
  44. if(m->isSmart() && m->casterSide != BattleSide::ATTACKER)
  45. {
  46. //if spell targeting is smart, then only attacker can use it
  47. return m->adaptProblem(ESpellCastProblem::NO_APPROPRIATE_TARGET, problem);
  48. }
  49. const auto attackableBattleHexes = m->battle()->getAttackableBattleHexes();
  50. return !attackableBattleHexes.empty() || m->adaptProblem(ESpellCastProblem::NO_APPROPRIATE_TARGET, problem);
  51. }
  52. void Catapult::apply(ServerCallback * server, const Mechanics * m, const EffectTarget & /* eTarget */) const
  53. {
  54. //start with all destructible parts
  55. static const std::set<EWallPart::EWallPart> potentialTargets =
  56. {
  57. EWallPart::KEEP,
  58. EWallPart::BOTTOM_TOWER,
  59. EWallPart::BOTTOM_WALL,
  60. EWallPart::BELOW_GATE,
  61. EWallPart::OVER_GATE,
  62. EWallPart::UPPER_WALL,
  63. EWallPart::UPPER_TOWER,
  64. EWallPart::GATE
  65. };
  66. assert(potentialTargets.size() == EWallPart::PARTS_COUNT);
  67. std::set<EWallPart::EWallPart> allowedTargets;
  68. for (auto const & target : potentialTargets)
  69. {
  70. auto state = m->battle()->battleGetWallState(target);
  71. if(state != EWallState::DESTROYED && state != EWallState::NONE)
  72. allowedTargets.insert(target);
  73. }
  74. assert(!allowedTargets.empty());
  75. if (allowedTargets.empty())
  76. return;
  77. CatapultAttack ca;
  78. ca.attacker = -1;
  79. BattleUnitsChanged removeUnits;
  80. for(int i = 0; i < targetsToAttack; i++)
  81. {
  82. // Hit on any existing, not destroyed targets are allowed
  83. // Multiple hit on same target are allowed.
  84. // Potential overshots (more hits on same targets than remaining HP) are allowed
  85. EWallPart::EWallPart target = *RandomGeneratorUtil::nextItem(allowedTargets, *server->getRNG());
  86. auto state = m->battle()->battleGetWallState(target);
  87. auto attackInfo = ca.attackedParts.begin();
  88. for ( ; attackInfo != ca.attackedParts.end(); ++attackInfo)
  89. if ( attackInfo->attackedPart == target )
  90. break;
  91. if (attackInfo == ca.attackedParts.end()) // new part
  92. {
  93. CatapultAttack::AttackInfo newInfo;
  94. newInfo.damageDealt = 1;
  95. newInfo.attackedPart = target;
  96. newInfo.destinationTile = m->battle()->wallPartToBattleHex(target);
  97. ca.attackedParts.push_back(newInfo);
  98. attackInfo = ca.attackedParts.end() - 1;
  99. }
  100. else // already damaged before, update damage
  101. {
  102. attackInfo->damageDealt += 1;
  103. }
  104. //removing creatures in turrets / keep if one is destroyed
  105. BattleHex posRemove;
  106. switch(target)
  107. {
  108. case EWallPart::KEEP:
  109. posRemove = BattleHex::CASTLE_CENTRAL_TOWER;
  110. break;
  111. case EWallPart::BOTTOM_TOWER:
  112. posRemove = BattleHex::CASTLE_BOTTOM_TOWER;
  113. break;
  114. case EWallPart::UPPER_TOWER:
  115. posRemove = BattleHex::CASTLE_UPPER_TOWER;
  116. break;
  117. }
  118. if(posRemove != BattleHex::INVALID && state - attackInfo->damageDealt <= 0) //HP enum subtraction not intuitive, consider using SiegeInfo::applyDamage
  119. {
  120. auto all = m->battle()->battleGetUnitsIf([=](const battle::Unit * unit)
  121. {
  122. return !unit->isGhost();
  123. });
  124. for(auto & elem : all)
  125. {
  126. if(elem->getPosition() != posRemove)
  127. continue;
  128. // if tower was hit multiple times, it may have been destroyed already
  129. bool stackWasRemovedBefore = false;
  130. for(auto & removed : removeUnits.changedStacks)
  131. {
  132. if (removed.id == elem->unitId())
  133. stackWasRemovedBefore = true;
  134. }
  135. if (!stackWasRemovedBefore)
  136. removeUnits.changedStacks.emplace_back(elem->unitId(), UnitChanges::EOperation::REMOVE);
  137. break;
  138. }
  139. }
  140. }
  141. server->apply(&ca);
  142. if(!removeUnits.changedStacks.empty())
  143. server->apply(&removeUnits);
  144. }
  145. void Catapult::serializeJsonEffect(JsonSerializeFormat & handler)
  146. {
  147. //TODO: add configuration unifying with Catapult ability
  148. handler.serializeInt("targetsToAttack", targetsToAttack);
  149. }
  150. }
  151. }
  152. VCMI_LIB_NAMESPACE_END