Catapult.cpp 4.5 KB

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