Catapult.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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> 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() == size_t(EWallPart::PARTS_COUNT));
  67. std::set<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. for(int i = 0; i < targetsToAttack; i++)
  80. {
  81. // Hit on any existing, not destroyed targets are allowed
  82. // Multiple hit on same target are allowed.
  83. // Potential overshots (more hits on same targets than remaining HP) are allowed
  84. EWallPart target = *RandomGeneratorUtil::nextItem(allowedTargets, *server->getRNG());
  85. auto attackInfo = ca.attackedParts.begin();
  86. for ( ; attackInfo != ca.attackedParts.end(); ++attackInfo)
  87. if ( attackInfo->attackedPart == target )
  88. break;
  89. if (attackInfo == ca.attackedParts.end()) // new part
  90. {
  91. CatapultAttack::AttackInfo newInfo;
  92. newInfo.damageDealt = 1;
  93. newInfo.attackedPart = target;
  94. newInfo.destinationTile = m->battle()->wallPartToBattleHex(target);
  95. ca.attackedParts.push_back(newInfo);
  96. attackInfo = ca.attackedParts.end() - 1;
  97. }
  98. else // already damaged before, update damage
  99. {
  100. attackInfo->damageDealt += 1;
  101. }
  102. }
  103. server->apply(&ca);
  104. BattleUnitsChanged removeUnits;
  105. for (auto const wallPart : { EWallPart::KEEP, EWallPart::BOTTOM_TOWER, EWallPart::UPPER_TOWER })
  106. {
  107. //removing creatures in turrets / keep if one is destroyed
  108. BattleHex posRemove;
  109. auto state = m->battle()->battleGetWallState(wallPart);
  110. switch(wallPart)
  111. {
  112. case EWallPart::KEEP:
  113. posRemove = BattleHex::CASTLE_CENTRAL_TOWER;
  114. break;
  115. case EWallPart::BOTTOM_TOWER:
  116. posRemove = BattleHex::CASTLE_BOTTOM_TOWER;
  117. break;
  118. case EWallPart::UPPER_TOWER:
  119. posRemove = BattleHex::CASTLE_UPPER_TOWER;
  120. break;
  121. }
  122. if(state == EWallState::DESTROYED) //HP enum subtraction not intuitive, consider using SiegeInfo::applyDamage
  123. {
  124. auto all = m->battle()->battleGetUnitsIf([=](const battle::Unit * unit)
  125. {
  126. return !unit->isGhost() && unit->getPosition() == posRemove;
  127. });
  128. assert(all.size() == 0 || all.size() == 1);
  129. for(auto & elem : all)
  130. removeUnits.changedStacks.emplace_back(elem->unitId(), UnitChanges::EOperation::REMOVE);
  131. }
  132. }
  133. if(!removeUnits.changedStacks.empty())
  134. server->apply(&removeUnits);
  135. }
  136. void Catapult::serializeJsonEffect(JsonSerializeFormat & handler)
  137. {
  138. //TODO: add configuration unifying with Catapult ability
  139. handler.serializeInt("targetsToAttack", targetsToAttack);
  140. }
  141. }
  142. }
  143. VCMI_LIB_NAMESPACE_END