Catapult.cpp 8.2 KB

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