hex.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. * hex.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 "vcmi/spells/Service.h"
  12. #include "vcmi/spells/Spell.h"
  13. #include "BAI/v13/hex.h"
  14. #include "common.h"
  15. #include "schema/v13/constants.h"
  16. namespace MMAI::BAI::V13
  17. {
  18. namespace S13 = Schema::V13;
  19. using HA = Schema::V13::HexAttribute;
  20. using HS = Schema::V13::HexState;
  21. using SA = Schema::V13::StackAttribute;
  22. constexpr HexStateMask S_PASSABLE = 1 << EI(HexState::PASSABLE);
  23. constexpr HexStateMask S_STOPPING = 1 << EI(HexState::STOPPING);
  24. constexpr HexStateMask S_DAMAGING_L = 1 << EI(HexState::DAMAGING_L);
  25. constexpr HexStateMask S_DAMAGING_R = 1 << EI(HexState::DAMAGING_R);
  26. constexpr HexStateMask S_DAMAGING_ALL = 1 << EI(HexState::DAMAGING_L) | 1 << EI(HexState::DAMAGING_R);
  27. // static
  28. int Hex::CalcId(const BattleHex & bh)
  29. {
  30. ASSERT(bh.isAvailable(), "Hex unavailable: " + std::to_string(bh.toInt()));
  31. return bh.getX() - 1 + (bh.getY() * 15);
  32. }
  33. // static
  34. std::pair<int, int> Hex::CalcXY(const BattleHex & bh)
  35. {
  36. return {bh.getX() - 1, bh.getY()};
  37. }
  38. //
  39. // Return bh's neighbouring hexes for setting action mask
  40. //
  41. // return nearby hexes for "X":
  42. //
  43. // . . . . . . . . . .
  44. // . . .11 5 0 6 . . .
  45. // . .10 4 X 1 7 . . .
  46. // . . . 9 3 2 8 . . .
  47. // . . . . . . . . . .
  48. //
  49. // NOTE:
  50. // The index of each hex in the returned array corresponds to a
  51. // the respective AMOVE_* HexAction w.r.t. "X" (see hexaction.h)
  52. //
  53. // static
  54. HexActionHex Hex::NearbyBattleHexes(const BattleHex & bh)
  55. {
  56. static_assert(EI(HexAction::AMOVE_TR) == 0);
  57. static_assert(EI(HexAction::AMOVE_R) == 1);
  58. static_assert(EI(HexAction::AMOVE_BR) == 2);
  59. static_assert(EI(HexAction::AMOVE_BL) == 3);
  60. static_assert(EI(HexAction::AMOVE_L) == 4);
  61. static_assert(EI(HexAction::AMOVE_TL) == 5);
  62. static_assert(EI(HexAction::AMOVE_2TR) == 6);
  63. static_assert(EI(HexAction::AMOVE_2R) == 7);
  64. static_assert(EI(HexAction::AMOVE_2BR) == 8);
  65. static_assert(EI(HexAction::AMOVE_2BL) == 9);
  66. static_assert(EI(HexAction::AMOVE_2L) == 10);
  67. static_assert(EI(HexAction::AMOVE_2TL) == 11);
  68. auto nbhR = bh.cloneInDirection(BattleHex::EDir::RIGHT, false);
  69. auto nbhL = bh.cloneInDirection(BattleHex::EDir::LEFT, false);
  70. return HexActionHex{
  71. // The 6 basic directions
  72. bh.cloneInDirection(BattleHex::EDir::TOP_RIGHT, false),
  73. nbhR,
  74. bh.cloneInDirection(BattleHex::EDir::BOTTOM_RIGHT, false),
  75. bh.cloneInDirection(BattleHex::EDir::BOTTOM_LEFT, false),
  76. nbhL,
  77. bh.cloneInDirection(BattleHex::EDir::TOP_LEFT, false),
  78. // Extended directions for R-side wide creatures
  79. nbhR.cloneInDirection(BattleHex::EDir::TOP_RIGHT, false),
  80. nbhR.cloneInDirection(BattleHex::EDir::RIGHT, false),
  81. nbhR.cloneInDirection(BattleHex::EDir::BOTTOM_RIGHT, false),
  82. // Extended directions for L-side wide creatures
  83. nbhL.cloneInDirection(BattleHex::EDir::BOTTOM_LEFT, false),
  84. nbhL.cloneInDirection(BattleHex::EDir::LEFT, false),
  85. nbhL.cloneInDirection(BattleHex::EDir::TOP_LEFT, false)
  86. };
  87. }
  88. Hex::Hex(
  89. const BattleHex & bhex_,
  90. const EAccessibility accessibility,
  91. const EGateState gatestate,
  92. const std::vector<std::shared_ptr<const CObstacleInstance>> & obstacles,
  93. const std::map<BattleHex, std::shared_ptr<Stack>> & hexstacks,
  94. const std::shared_ptr<ActiveStackInfo> & astackinfo
  95. )
  96. : bhex(bhex_), id(CalcId(bhex_))
  97. {
  98. attrs.fill(S13::NULL_VALUE_UNENCODED);
  99. auto [x, y] = CalcXY(bhex);
  100. auto it = hexstacks.find(bhex);
  101. stack = it == hexstacks.end() ? nullptr : it->second;
  102. setattr(HA::Y_COORD, y);
  103. setattr(HA::X_COORD, x);
  104. // This is never N/A => set separately (not within the if below)
  105. setattr(HA::IS_REAR, stack && bhex == stack->cstack->occupiedHex());
  106. static_assert(EI(SA::_count) == 25, "whistleblower in case attributes change");
  107. auto attrmap = std::map<HA, SA>{
  108. {HA::STACK_SIDE, SA::SIDE },
  109. {HA::STACK_SLOT, SA::SLOT },
  110. {HA::STACK_QUANTITY, SA::QUANTITY },
  111. {HA::STACK_ATTACK, SA::ATTACK },
  112. {HA::STACK_DEFENSE, SA::DEFENSE },
  113. {HA::STACK_SHOTS, SA::SHOTS },
  114. {HA::STACK_DMG_MIN, SA::DMG_MIN },
  115. {HA::STACK_DMG_MAX, SA::DMG_MAX },
  116. {HA::STACK_HP, SA::HP },
  117. {HA::STACK_HP_LEFT, SA::HP_LEFT },
  118. {HA::STACK_SPEED, SA::SPEED },
  119. {HA::STACK_QUEUE, SA::QUEUE },
  120. {HA::STACK_VALUE_ONE, SA::VALUE_ONE },
  121. {HA::STACK_FLAGS1, SA::FLAGS1 },
  122. {HA::STACK_FLAGS2, SA::FLAGS2 },
  123. {HA::STACK_VALUE_REL, SA::VALUE_REL },
  124. {HA::STACK_VALUE_REL0, SA::VALUE_REL0 },
  125. {HA::STACK_VALUE_KILLED_REL, SA::VALUE_KILLED_REL },
  126. {HA::STACK_VALUE_KILLED_ACC_REL0, SA::VALUE_KILLED_ACC_REL0},
  127. {HA::STACK_VALUE_LOST_REL, SA::VALUE_LOST_REL },
  128. {HA::STACK_VALUE_LOST_ACC_REL0, SA::VALUE_LOST_ACC_REL0 },
  129. {HA::STACK_DMG_DEALT_REL, SA::DMG_DEALT_REL },
  130. {HA::STACK_DMG_DEALT_ACC_REL0, SA::DMG_DEALT_ACC_REL0 },
  131. {HA::STACK_DMG_RECEIVED_REL, SA::DMG_RECEIVED_REL },
  132. {HA::STACK_DMG_RECEIVED_ACC_REL0, SA::DMG_RECEIVED_ACC_REL0},
  133. };
  134. if(stack)
  135. {
  136. int i = 0;
  137. for(const auto & [a, sa] : attrmap)
  138. {
  139. setattr(a, stack->attr(sa));
  140. ++i;
  141. }
  142. ASSERT(i == EI(SA::_count), "not all stack attributes encoded: i=" + std::to_string(i));
  143. }
  144. if(astackinfo)
  145. {
  146. setStateMask(accessibility, obstacles, astackinfo->stack->cstack->unitSide());
  147. setActionMask(astackinfo, hexstacks);
  148. }
  149. else
  150. {
  151. setStateMask(accessibility, obstacles, BattleSide::ATTACKER);
  152. }
  153. finalize();
  154. }
  155. const HexAttrs & Hex::getAttrs() const
  156. {
  157. return attrs;
  158. }
  159. int Hex::getID() const
  160. {
  161. return id;
  162. }
  163. int Hex::getAttr(HexAttribute a) const
  164. {
  165. return attr(a);
  166. }
  167. int Hex::attr(HexAttribute a) const
  168. {
  169. return attrs.at(EI(a));
  170. };
  171. void Hex::setattr(HexAttribute a, int value)
  172. {
  173. attrs.at(EI(a)) = value;
  174. };
  175. std::string Hex::name() const
  176. {
  177. return "(" + std::to_string(attr(HA::Y_COORD)) + "," + std::to_string(attr(HA::X_COORD)) + ")";
  178. }
  179. void Hex::finalize()
  180. {
  181. attrs.at(EI(HA::ACTION_MASK)) = actmask.to_ulong();
  182. attrs.at(EI(HA::STATE_MASK)) = statemask.to_ulong();
  183. }
  184. const Stack * Hex::getStack() const
  185. {
  186. return stack.get();
  187. }
  188. // private
  189. void Hex::setStateMask(const EAccessibility accessibility, const std::vector<std::shared_ptr<const CObstacleInstance>> & obstacles, BattleSide side)
  190. {
  191. // First process obstacles
  192. // XXX: set only non-PASSABLE flags
  193. // (e.g. there may be a stack standing on the obstacle (firewall, moat))
  194. // so the PASSABLE mask bit will set later
  195. // XXX: moats are a weird obstacle:
  196. // * if dispellable (Tower mines?) => type=SPELL_CREATED
  197. // * otherwise => type=MOAT
  198. // * their trigger ability is a spell, as it seems
  199. // (which is not found in spells.json, neither is available as a SpellID constant)
  200. //
  201. // Ref: Moat::placeObstacles()
  202. // BattleEvaluator::goTowardsNearest() // var triggerAbility
  203. //
  204. for(const auto & obstacle : obstacles)
  205. {
  206. switch(obstacle->obstacleType)
  207. {
  208. case CObstacleInstance::USUAL:
  209. case CObstacleInstance::ABSOLUTE_OBSTACLE:
  210. statemask &= ~S_PASSABLE;
  211. break;
  212. case CObstacleInstance::MOAT:
  213. statemask |= (S_STOPPING | S_DAMAGING_ALL);
  214. break;
  215. case CObstacleInstance::SPELL_CREATED:
  216. // XXX: the public Obstacle / Spell API does not seem to expose
  217. // any useful methods for checking if friendly creatures
  218. // would get damaged by an obstacle.
  219. switch(SpellID(obstacle->ID))
  220. {
  221. case SpellID::QUICKSAND:
  222. statemask |= S_STOPPING;
  223. break;
  224. case SpellID::LAND_MINE:
  225. auto casterside = dynamic_cast<const SpellCreatedObstacle *>(obstacle.get())->casterSide;
  226. // XXX: in practice, there is no situation where enemy
  227. // mines are visible (e.g. when our army has a stack
  228. // which is native to the battlefield terrain),
  229. // as the UI simply does not allow to cast the spell
  230. // in this case .
  231. if(side == casterside)
  232. statemask |= (side == BattleSide::DEFENDER ? S_DAMAGING_L : S_DAMAGING_R);
  233. else
  234. statemask |= (side == BattleSide::DEFENDER ? S_DAMAGING_R : S_DAMAGING_L);
  235. }
  236. break;
  237. default:
  238. THROW_FORMAT("Unexpected obstacle type: %d", EI(obstacle->obstacleType));
  239. }
  240. }
  241. switch(accessibility)
  242. {
  243. case EAccessibility::ACCESSIBLE:
  244. ASSERT(!stack, "accessibility is ACCESSIBLE, but a stack was found on hex");
  245. statemask |= S_PASSABLE;
  246. break;
  247. case EAccessibility::OBSTACLE:
  248. ASSERT(!stack, "accessibility is OBSTACLE, but a stack was found on hex");
  249. statemask &= ~S_PASSABLE;
  250. break;
  251. case EAccessibility::ALIVE_STACK:
  252. // XXX: stack can be NULL if it was left out of the observation
  253. // ASSERT(stack, "accessibility is ALIVE_STACK, but no stack was found on hex");
  254. statemask &= ~S_PASSABLE;
  255. break;
  256. case EAccessibility::DESTRUCTIBLE_WALL:
  257. // XXX: Destroyed walls become ACCESSIBLE.
  258. ASSERT(!stack, "accessibility is DESTRUCTIBLE_WALL, but a stack was found on hex");
  259. statemask &= ~S_PASSABLE;
  260. break;
  261. case EAccessibility::GATE:
  262. // See BattleProcessor::updateGateState() for gate states
  263. // See CBattleInfoCallback::getAccessibility() for accessibility on gate
  264. //
  265. // TL; DR:
  266. // -> GATE means closed, non-blocked gate
  267. // -> UNAVAILABLE means blocked
  268. // -> ACCESSIBLE otherwise (open, destroyed)
  269. //
  270. // Regardless of the gate state, we always set the GATE flag
  271. // purely based on the hex coordinates and not on the accessibility
  272. // => not setting GATE flag here
  273. //
  274. // However, in case of GATE accessibility, we still need
  275. // to set the PASSABLE flag accordingly.
  276. side == BattleSide::DEFENDER ? statemask.set(EI(HS::PASSABLE)) : statemask.reset(EI(HS::PASSABLE));
  277. break;
  278. case EAccessibility::UNAVAILABLE:
  279. statemask &= ~S_PASSABLE;
  280. break;
  281. default:
  282. THROW_FORMAT("Unexpected hex accessibility for bhex %d: %d", bhex.toInt() % EI(accessibility));
  283. }
  284. }
  285. void Hex::setActionMask(const std::shared_ptr<ActiveStackInfo> & astackinfo, const std::map<BattleHex, std::shared_ptr<Stack>> & hexstacks)
  286. {
  287. const auto * astack = astackinfo->stack;
  288. // XXX: for statehist, astack may be enemy stack
  289. // in this case building the actmask is redundant
  290. if(astackinfo->canshoot && stack && stack->cstack->unitSide() != astack->cstack->unitSide())
  291. actmask.set(EI(HexAction::SHOOT));
  292. // XXX: ReachabilityInfo::isReachable() must not be used as it
  293. // returns true even if speed is insufficient => use distances.
  294. // NOTE: distances is 0 for the stack's main hex and 1 for its rear hex
  295. // (100000 if it can't fit there)
  296. if(astackinfo->rinfo->distances.at(bhex.toInt()) <= astack->attr(SA::SPEED))
  297. actmask.set(EI(HexAction::MOVE));
  298. else
  299. // astack can't MOVE here => AMOVE_* will never be possible
  300. return;
  301. const auto & nbhexes = NearbyBattleHexes(bhex);
  302. const auto * const a_cstack = astack->cstack;
  303. for(int i = 0; i < nbhexes.size(); ++i)
  304. {
  305. const auto & n_bhex = nbhexes.at(i);
  306. if(!n_bhex.isAvailable())
  307. continue;
  308. auto it = hexstacks.find(n_bhex);
  309. if(it == hexstacks.end())
  310. continue;
  311. const auto & n_cstack = it->second->cstack;
  312. auto hexaction = static_cast<HexAction>(i);
  313. if(n_cstack->unitSide() == a_cstack->unitSide())
  314. return;
  315. if(hexaction <= HexAction::AMOVE_TL)
  316. {
  317. ASSERT(CStack::isMeleeAttackPossible(a_cstack, n_cstack, bhex), "vcmi says melee attack is IMPOSSIBLE [1]");
  318. actmask.set(i);
  319. }
  320. else if(hexaction > HexAction::AMOVE_2BR)
  321. {
  322. // only wide L stacks can perform 2TL/2L/2BL attacks
  323. if(a_cstack->unitSide() == BattleSide::ATTACKER && a_cstack->doubleWide())
  324. {
  325. ASSERT(CStack::isMeleeAttackPossible(a_cstack, n_cstack, bhex), "vcmi says melee attack is IMPOSSIBLE");
  326. actmask.set(i);
  327. }
  328. }
  329. // only wide R stacks can perform 2TR/2R/2BR attacks
  330. else if(a_cstack->unitSide() == BattleSide::DEFENDER && a_cstack->doubleWide())
  331. {
  332. ASSERT(CStack::isMeleeAttackPossible(a_cstack, n_cstack, bhex), "vcmi says melee attack is IMPOSSIBLE [2]");
  333. actmask.set(i);
  334. }
  335. }
  336. }
  337. }