Catapult.cpp 8.3 KB

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