Catapult.cpp 8.3 KB

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