Catapult.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. namespace spells
  22. {
  23. namespace effects
  24. {
  25. bool Catapult::applicable(Problem & problem, const Mechanics * m) const
  26. {
  27. const auto *town = m->battle()->battleGetDefendedTown();
  28. if(nullptr == town)
  29. {
  30. return m->adaptProblem(ESpellCastProblem::NO_APPROPRIATE_TARGET, problem);
  31. }
  32. if(CGTownInstance::NONE == town->fortLevel())
  33. {
  34. return m->adaptProblem(ESpellCastProblem::NO_APPROPRIATE_TARGET, problem);
  35. }
  36. if(m->isSmart() && m->casterSide != BattleSide::ATTACKER)
  37. {
  38. //if spell targeting is smart, then only attacker can use it
  39. return m->adaptProblem(ESpellCastProblem::NO_APPROPRIATE_TARGET, problem);
  40. }
  41. const auto attackableBattleHexes = m->battle()->getAttackableBattleHexes();
  42. return !attackableBattleHexes.empty() || m->adaptProblem(ESpellCastProblem::NO_APPROPRIATE_TARGET, problem);
  43. }
  44. void Catapult::apply(ServerCallback * server, const Mechanics * m, const EffectTarget & eTarget) const
  45. {
  46. if(m->isMassive())
  47. applyMassive(server, m); // Like earthquake
  48. else
  49. applyTargeted(server, m, eTarget); // Like catapult shots
  50. }
  51. void Catapult::applyMassive(ServerCallback * server, const Mechanics * m) const
  52. {
  53. //start with all destructible parts
  54. std::vector<EWallPart> allowedTargets = getPotentialTargets(m, true, true);
  55. assert(!allowedTargets.empty());
  56. if (allowedTargets.empty())
  57. return;
  58. CatapultAttack ca;
  59. ca.attacker = m->caster->getCasterUnitId();
  60. for(int i = 0; i < targetsToAttack; i++)
  61. {
  62. // Hit on any existing, not destroyed targets are allowed
  63. // Multiple hit on same target are allowed.
  64. // Potential overshots (more hits on same targets than remaining HP) are allowed
  65. EWallPart target = *RandomGeneratorUtil::nextItem(allowedTargets, *server->getRNG());
  66. auto attackInfo = ca.attackedParts.begin();
  67. for ( ; attackInfo != ca.attackedParts.end(); ++attackInfo)
  68. if ( attackInfo->attackedPart == target )
  69. break;
  70. if (attackInfo == ca.attackedParts.end()) // new part
  71. {
  72. CatapultAttack::AttackInfo newInfo;
  73. newInfo.damageDealt = getRandomDamage(server);
  74. newInfo.attackedPart = target;
  75. newInfo.destinationTile = m->battle()->wallPartToBattleHex(target);
  76. ca.attackedParts.push_back(newInfo);
  77. attackInfo = ca.attackedParts.end() - 1;
  78. }
  79. else // already damaged before, update damage
  80. {
  81. attackInfo->damageDealt += getRandomDamage(server);
  82. }
  83. }
  84. server->apply(&ca);
  85. removeTowerShooters(server, m);
  86. }
  87. void Catapult::applyTargeted(ServerCallback * server, const Mechanics * m, const EffectTarget & target) const
  88. {
  89. assert(!target.empty());
  90. auto destination = target.at(0).hexValue;
  91. auto desiredTarget = m->battle()->battleHexToWallPart(destination);
  92. for(int i = 0; i < targetsToAttack; i++)
  93. {
  94. auto actualTarget = EWallPart::INVALID;
  95. if ( m->battle()->isWallPartAttackable(desiredTarget) &&
  96. server->getRNG()->getInt64Range(0, 99)() < getCatapultHitChance(desiredTarget))
  97. {
  98. actualTarget = desiredTarget;
  99. }
  100. else
  101. {
  102. std::vector<EWallPart> potentialTargets = getPotentialTargets(m, false, false);
  103. if (potentialTargets.empty())
  104. break; // everything is gone, can't attack anymore
  105. actualTarget = *RandomGeneratorUtil::nextItem(potentialTargets, *server->getRNG());
  106. }
  107. assert(actualTarget != EWallPart::INVALID);
  108. CatapultAttack::AttackInfo attack;
  109. attack.attackedPart = actualTarget;
  110. attack.destinationTile = m->battle()->wallPartToBattleHex(actualTarget);
  111. attack.damageDealt = getRandomDamage(server);
  112. CatapultAttack ca; //package for clients
  113. ca.attacker = m->caster->getCasterUnitId();
  114. ca.attackedParts.push_back(attack);
  115. server->apply(&ca);
  116. removeTowerShooters(server, m);
  117. }
  118. }
  119. int Catapult::getCatapultHitChance(EWallPart part) const
  120. {
  121. switch(part)
  122. {
  123. case EWallPart::GATE:
  124. return gate;
  125. case EWallPart::KEEP:
  126. return keep;
  127. case EWallPart::BOTTOM_TOWER:
  128. case EWallPart::UPPER_TOWER:
  129. return tower;
  130. case EWallPart::BOTTOM_WALL:
  131. case EWallPart::BELOW_GATE:
  132. case EWallPart::OVER_GATE:
  133. case EWallPart::UPPER_WALL:
  134. return wall;
  135. default:
  136. return 0;
  137. }
  138. }
  139. int Catapult::getRandomDamage (ServerCallback * server) const
  140. {
  141. std::array<int, 3> damageChances = { noDmg, hit, crit }; //dmgChance[i] - chance for doing i dmg when hit is successful
  142. int totalChance = std::accumulate(damageChances.begin(), damageChances.end(), 0);
  143. int damageRandom = server->getRNG()->getInt64Range(0, totalChance - 1)();
  144. int dealtDamage = 0;
  145. //calculating dealt damage
  146. for (int damage = 0; damage < damageChances.size(); ++damage)
  147. {
  148. if (damageRandom <= damageChances[damage])
  149. {
  150. dealtDamage = damage;
  151. break;
  152. }
  153. damageRandom -= damageChances[damage];
  154. }
  155. return dealtDamage;
  156. }
  157. void Catapult::removeTowerShooters(ServerCallback * server, const Mechanics * m) const
  158. {
  159. BattleUnitsChanged removeUnits;
  160. for (auto const wallPart : { EWallPart::KEEP, EWallPart::BOTTOM_TOWER, EWallPart::UPPER_TOWER })
  161. {
  162. //removing creatures in turrets / keep if one is destroyed
  163. BattleHex posRemove;
  164. auto state = m->battle()->battleGetWallState(wallPart);
  165. switch(wallPart)
  166. {
  167. case EWallPart::KEEP:
  168. posRemove = BattleHex::CASTLE_CENTRAL_TOWER;
  169. break;
  170. case EWallPart::BOTTOM_TOWER:
  171. posRemove = BattleHex::CASTLE_BOTTOM_TOWER;
  172. break;
  173. case EWallPart::UPPER_TOWER:
  174. posRemove = BattleHex::CASTLE_UPPER_TOWER;
  175. break;
  176. }
  177. if(state == EWallState::DESTROYED) //HP enum subtraction not intuitive, consider using SiegeInfo::applyDamage
  178. {
  179. auto all = m->battle()->battleGetUnitsIf([=](const battle::Unit * unit)
  180. {
  181. return !unit->isGhost() && unit->getPosition() == posRemove;
  182. });
  183. assert(all.size() == 0 || all.size() == 1);
  184. for(auto & elem : all)
  185. removeUnits.changedStacks.emplace_back(elem->unitId(), UnitChanges::EOperation::REMOVE);
  186. }
  187. }
  188. if(!removeUnits.changedStacks.empty())
  189. server->apply(&removeUnits);
  190. }
  191. std::vector<EWallPart> Catapult::getPotentialTargets(const Mechanics * m, bool bypassGateCheck, bool bypassTowerCheck) const
  192. {
  193. std::vector<EWallPart> potentialTargets;
  194. constexpr std::array<EWallPart, 4> walls = { EWallPart::BOTTOM_WALL, EWallPart::BELOW_GATE, EWallPart::OVER_GATE, EWallPart::UPPER_WALL };
  195. constexpr std::array<EWallPart, 3> towers= { EWallPart::BOTTOM_TOWER, EWallPart::KEEP, EWallPart::UPPER_TOWER };
  196. constexpr EWallPart gates = EWallPart::GATE;
  197. // in H3, catapult under automatic control will attack objects in following order:
  198. // walls, gates, towers
  199. for (auto & part : walls)
  200. if (m->battle()->isWallPartAttackable(part))
  201. potentialTargets.push_back(part);
  202. if ((potentialTargets.empty() || bypassGateCheck) && (m->battle()->isWallPartAttackable(gates)))
  203. potentialTargets.push_back(gates);
  204. if (potentialTargets.empty() || bypassTowerCheck)
  205. for (auto & part : towers)
  206. if (m->battle()->isWallPartAttackable(part))
  207. potentialTargets.push_back(part);
  208. return potentialTargets;
  209. }
  210. void Catapult::adjustHitChance()
  211. {
  212. vstd::abetween(keep, 0, 100);
  213. vstd::abetween(tower, 0, 100);
  214. vstd::abetween(gate, 0, 100);
  215. vstd::abetween(wall, 0, 100);
  216. vstd::abetween(crit, 0, 100);
  217. vstd::abetween(hit, 0, 100 - crit);
  218. vstd::amin(noDmg, 100 - hit - crit);
  219. }
  220. void Catapult::serializeJsonEffect(JsonSerializeFormat & handler)
  221. {
  222. handler.serializeInt("targetsToAttack", targetsToAttack);
  223. handler.serializeInt("chanceToHitKeep", keep);
  224. handler.serializeInt("chanceToHitGate", gate);
  225. handler.serializeInt("chanceToHitTower", tower);
  226. handler.serializeInt("chanceToHitWall", wall);
  227. handler.serializeInt("chanceToNormalHit", hit);
  228. handler.serializeInt("chanceToCrit", crit);
  229. adjustHitChance();
  230. }
  231. }
  232. }
  233. VCMI_LIB_NAMESPACE_END