@rpgjs/action-battle 5.0.0-beta.11 → 5.0.0-beta.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (111) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/dist/client/ai.server.d.ts +57 -8
  3. package/dist/client/attack-input.d.ts +3 -0
  4. package/dist/client/core/action-use.d.ts +18 -0
  5. package/dist/client/core/ai-behavior-tree.d.ts +99 -0
  6. package/dist/client/core/attack-runtime.d.ts +2 -0
  7. package/dist/client/core/defaults.d.ts +3 -2
  8. package/dist/client/core/equipment.d.ts +1 -0
  9. package/dist/client/core/targets.d.ts +15 -0
  10. package/dist/client/enemies/factory.d.ts +2 -0
  11. package/dist/client/index.d.ts +12 -7
  12. package/dist/client/index.js +16 -11
  13. package/dist/client/index10.js +32 -56
  14. package/dist/client/index11.js +99 -52
  15. package/dist/client/index12.js +76 -103
  16. package/dist/client/index13.js +72 -135
  17. package/dist/client/index14.js +67 -23
  18. package/dist/client/index15.js +197 -63
  19. package/dist/client/index16.js +112 -1337
  20. package/dist/client/index17.js +203 -7
  21. package/dist/client/index18.js +32 -58
  22. package/dist/client/index19.js +70 -8
  23. package/dist/client/index20.js +57 -501
  24. package/dist/client/index21.js +70 -0
  25. package/dist/client/index22.js +226 -0
  26. package/dist/client/index23.js +16 -0
  27. package/dist/client/index24.js +25 -0
  28. package/dist/client/index25.js +107 -0
  29. package/dist/client/index26.js +1949 -0
  30. package/dist/client/index27.js +12 -0
  31. package/dist/client/index28.js +589 -0
  32. package/dist/client/index4.js +79 -38
  33. package/dist/client/index6.js +65 -306
  34. package/dist/client/index7.js +33 -33
  35. package/dist/client/index8.js +24 -100
  36. package/dist/client/index9.js +293 -61
  37. package/dist/client/locomotion.d.ts +16 -0
  38. package/dist/client/movement.d.ts +14 -0
  39. package/dist/client/server.d.ts +7 -3
  40. package/dist/client/ui.d.ts +22 -0
  41. package/dist/client/visual.d.ts +15 -0
  42. package/dist/server/ai.server.d.ts +57 -8
  43. package/dist/server/attack-input.d.ts +3 -0
  44. package/dist/server/core/action-use.d.ts +18 -0
  45. package/dist/server/core/ai-behavior-tree.d.ts +99 -0
  46. package/dist/server/core/attack-runtime.d.ts +2 -0
  47. package/dist/server/core/defaults.d.ts +3 -2
  48. package/dist/server/core/equipment.d.ts +1 -0
  49. package/dist/server/core/targets.d.ts +15 -0
  50. package/dist/server/enemies/factory.d.ts +2 -0
  51. package/dist/server/index.d.ts +12 -7
  52. package/dist/server/index.js +14 -9
  53. package/dist/server/index10.js +64 -1336
  54. package/dist/server/index11.js +33 -33
  55. package/dist/server/index13.js +67 -11
  56. package/dist/server/index14.js +207 -484
  57. package/dist/server/index15.js +15 -9
  58. package/dist/server/index16.js +26 -0
  59. package/dist/server/index17.js +25 -0
  60. package/dist/server/index18.js +107 -0
  61. package/dist/server/index19.js +1949 -0
  62. package/dist/server/index2.js +10 -2
  63. package/dist/server/index20.js +37 -0
  64. package/dist/server/index21.js +588 -0
  65. package/dist/server/index22.js +78 -0
  66. package/dist/server/index23.js +12 -0
  67. package/dist/server/index5.js +79 -38
  68. package/dist/server/index6.js +192 -129
  69. package/dist/server/index7.js +208 -24
  70. package/dist/server/index8.js +28 -66
  71. package/dist/server/index9.js +68 -51
  72. package/dist/server/locomotion.d.ts +16 -0
  73. package/dist/server/movement.d.ts +14 -0
  74. package/dist/server/server.d.ts +7 -3
  75. package/dist/server/ui.d.ts +22 -0
  76. package/dist/server/visual.d.ts +15 -0
  77. package/package.json +5 -5
  78. package/src/ai.server.spec.ts +380 -1
  79. package/src/ai.server.ts +963 -137
  80. package/src/animations.spec.ts +40 -0
  81. package/src/animations.ts +31 -9
  82. package/src/attack-input.spec.ts +51 -0
  83. package/src/attack-input.ts +59 -0
  84. package/src/client.ts +75 -62
  85. package/src/config.ts +84 -37
  86. package/src/core/action-use.spec.ts +317 -0
  87. package/src/core/action-use.ts +387 -0
  88. package/src/core/ai-behavior-tree.spec.ts +116 -0
  89. package/src/core/ai-behavior-tree.ts +272 -0
  90. package/src/core/attack-profile.spec.ts +46 -0
  91. package/src/core/attack-runtime.spec.ts +35 -0
  92. package/src/core/attack-runtime.ts +32 -0
  93. package/src/core/context.ts +9 -0
  94. package/src/core/contracts.ts +146 -1
  95. package/src/core/defaults.ts +72 -1
  96. package/src/core/equipment.ts +9 -5
  97. package/src/core/hit.spec.ts +21 -0
  98. package/src/core/targets.spec.ts +124 -0
  99. package/src/core/targets.ts +150 -0
  100. package/src/enemies/factory.ts +8 -0
  101. package/src/index.ts +111 -2
  102. package/src/locomotion.spec.ts +51 -0
  103. package/src/locomotion.ts +48 -0
  104. package/src/movement.spec.ts +78 -0
  105. package/src/movement.ts +46 -0
  106. package/src/server.ts +242 -66
  107. package/src/types.ts +105 -35
  108. package/src/ui.ts +113 -0
  109. package/src/visual.spec.ts +166 -0
  110. package/src/visual.ts +285 -0
  111. package/README.md +0 -1242
@@ -1,3 +1,12 @@
1
+ var playResolvedAnimation = (entity, animation) => {
2
+ if (typeof entity.setGraphicAnimation === "function") {
3
+ if (animation.graphic !== void 0) entity.setGraphicAnimation(animation.animationName, animation.graphic, animation.repeat);
4
+ else entity.setGraphicAnimation(animation.animationName, animation.repeat);
5
+ return;
6
+ }
7
+ if (typeof entity.setAnimation === "function") if (animation.graphic !== void 0) entity.setAnimation(animation.animationName, animation.graphic, animation.repeat);
8
+ else entity.setAnimation(animation.animationName, animation.repeat);
9
+ };
1
10
  var DEFAULT_ANIMATION_BY_KEY = {
2
11
  attack: "attack",
3
12
  hurt: "hurt",
@@ -48,8 +57,7 @@ function resolveActionBattleAnimation(key, entity, animations, context, defaults
48
57
  function playActionBattleAnimation(key, entity, animations, context, defaults = {}) {
49
58
  const animation = resolveActionBattleAnimation(key, entity, animations, context, defaults);
50
59
  if (!animation) return null;
51
- if (animation.graphic !== void 0) entity.setGraphicAnimation(animation.animationName, animation.graphic, animation.repeat);
52
- else entity.setGraphicAnimation(animation.animationName, animation.repeat);
60
+ playResolvedAnimation(entity, animation);
53
61
  return animation;
54
62
  }
55
63
  function getActionBattleAnimationRemovalDelay(animation) {
@@ -0,0 +1,37 @@
1
+ //#region src/targeting.ts
2
+ var normalizeMaskRows = (mask) => {
3
+ if (!mask) return ["#"];
4
+ if (Array.isArray(mask)) return mask;
5
+ return mask.trim().split("\n").map((row) => row.replace(/\r/g, ""));
6
+ };
7
+ var parseAoeMask = (mask) => {
8
+ const rows = normalizeMaskRows(mask);
9
+ const height = rows.length;
10
+ const width = rows.reduce((max, row) => Math.max(max, row.length), 0);
11
+ const centerX = Math.floor(width / 2);
12
+ const centerY = Math.floor(height / 2);
13
+ const cells = [];
14
+ rows.forEach((row, y) => {
15
+ for (let x = 0; x < row.length; x++) {
16
+ const char = row[x];
17
+ if (char && char !== "." && char !== " ") cells.push({
18
+ dx: x - centerX,
19
+ dy: y - centerY
20
+ });
21
+ }
22
+ });
23
+ if (cells.length === 0) cells.push({
24
+ dx: 0,
25
+ dy: 0
26
+ });
27
+ return {
28
+ width,
29
+ height,
30
+ centerX,
31
+ centerY,
32
+ cells
33
+ };
34
+ };
35
+ var manhattanDistance = (a, b) => Math.abs(a.x - b.x) + Math.abs(a.y - b.y);
36
+ //#endregion
37
+ export { manhattanDistance, parseAoeMask };
@@ -0,0 +1,588 @@
1
+ import "./index3.js";
2
+ import { normalizeActionBattleAttackProfile } from "./index4.js";
3
+ import { normalizeActionBattleOptions, setActionBattleOptions } from "./index5.js";
4
+ import { emitActionBattleClientVisual } from "./index6.js";
5
+ import { DEFAULT_ZELDA_PLAYER_HITBOXES } from "./index7.js";
6
+ import { getActionBattleSystems, setActionBattleSystems } from "./index8.js";
7
+ import "./index9.js";
8
+ import { ActionBattleHitTracker, createActionBattleAttackId, getNormalizedActionBattleAttackProfile, runActionBattleActiveHitbox } from "./index10.js";
9
+ import { applyActionBattleAttackDirection, resolveActionBattleAttackDirection } from "./index11.js";
10
+ import { applyActionBattleHit } from "./index12.js";
11
+ import { canActionBattleTarget, getActionBattleTargets } from "./index13.js";
12
+ import { canActionBattleUseTarget, executeActionBattleUse, getActionBattleActionConfig, handleActionBattleProjectileDestroy, handleActionBattleProjectileImpact } from "./index14.js";
13
+ import { resolveActionBattleWeapon, resolveActionBattleWeaponAttackProfile } from "./index15.js";
14
+ import { forceActionBattleLocomotionAnimation, withActionBattleAnimationUnlocked } from "./index16.js";
15
+ import { DEFAULT_KNOCKBACK } from "./index19.js";
16
+ import { manhattanDistance, parseAoeMask } from "./index20.js";
17
+ import { RpgEvent } from "@rpgjs/server";
18
+ import { Control, defineModule } from "@rpgjs/common";
19
+ //#region src/server.ts
20
+ var ACTION_BATTLE_ACTION_BAR_GUI_ID = "action-battle-action-bar";
21
+ var DEFAULT_ATTACK_LOCK_DURATION_MS = 350;
22
+ /**
23
+ * Default player attack hitboxes offsets for each direction
24
+ *
25
+ * These hitboxes define the attack areas relative to the player's position
26
+ * for each cardinal direction. They are converted to absolute coordinates
27
+ * when creating the moving hitbox.
28
+ */
29
+ var DEFAULT_PLAYER_ATTACK_HITBOXES = { ...DEFAULT_ZELDA_PLAYER_HITBOXES };
30
+ var beginPlayerAttackLock = (player, map, durationMs, locks) => {
31
+ if (durationMs <= 0) return true;
32
+ const runtimePlayer = player;
33
+ const now = Date.now();
34
+ if (typeof runtimePlayer.__actionBattleAttackLockedUntil === "number" && runtimePlayer.__actionBattleAttackLockedUntil > now) return false;
35
+ const lockId = (runtimePlayer.__actionBattleAttackLockId ?? 0) + 1;
36
+ runtimePlayer.__actionBattleAttackLockId = lockId;
37
+ runtimePlayer.__actionBattleAttackLockedUntil = now + durationMs;
38
+ const previousCanMove = player.canMove;
39
+ const previousDirectionFixed = player.directionFixed;
40
+ const previousAnimationFixed = player.animationFixed;
41
+ if (locks.movement) {
42
+ player.pendingInputs = [];
43
+ player.lastProcessedInputTs = 0;
44
+ map?.stopMovement?.(player);
45
+ player.canMove = false;
46
+ }
47
+ if (locks.direction) player.directionFixed = true;
48
+ setTimeout(() => {
49
+ if (runtimePlayer.__actionBattleAttackLockId !== lockId) return;
50
+ runtimePlayer.__actionBattleAttackLockedUntil = 0;
51
+ player.canMove = previousCanMove;
52
+ player.directionFixed = previousDirectionFixed;
53
+ player.animationFixed = previousAnimationFixed;
54
+ if (locks.movement && !previousAnimationFixed) forceActionBattleLocomotionAnimation(player, "stand");
55
+ }, durationMs);
56
+ return true;
57
+ };
58
+ var isBattleEvent = (event) => !!event.battleAi;
59
+ var rectsOverlap = (a, b) => a.x < b.x + b.width && a.x + a.width > b.x && a.y < b.y + b.height && a.y + a.height > b.y;
60
+ var entityRect = (entity) => {
61
+ const hitbox = typeof entity.hitbox === "function" ? entity.hitbox() : entity.hitbox;
62
+ return {
63
+ x: entity.x(),
64
+ y: entity.y(),
65
+ width: hitbox?.w ?? 32,
66
+ height: hitbox?.h ?? 32
67
+ };
68
+ };
69
+ var getVisibleActionEvents = (player, map, hitboxes) => {
70
+ if (!map) return [];
71
+ const eventsById = /* @__PURE__ */ new Map();
72
+ const addEvent = (event) => {
73
+ if (!event) return;
74
+ if (!(typeof map.isEventVisibleForPlayer === "function" ? map.isEventVisibleForPlayer(event, player) : true)) return;
75
+ eventsById.set(event.id, event);
76
+ };
77
+ const collisions = map.getCollisions?.(player.id);
78
+ if (Array.isArray(collisions)) collisions.forEach((id) => addEvent(map.getEvent(id)));
79
+ const direction = typeof player.getDirection === "function" ? player.getDirection() : void 0;
80
+ const interactionCollisions = map.getInteractionCollisions?.(player.id, direction);
81
+ if (Array.isArray(interactionCollisions)) interactionCollisions.forEach((id) => addEvent(map.getEvent(id)));
82
+ for (const event of map.getEvents()) {
83
+ const rect = entityRect(event);
84
+ if (hitboxes.some((hitbox) => rectsOverlap(hitbox, rect))) addEvent(event);
85
+ }
86
+ return Array.from(eventsById.values());
87
+ };
88
+ var isActionReservedForNormalEvent = (player, map, hitboxes) => {
89
+ const events = getVisibleActionEvents(player, map, hitboxes);
90
+ return events.length > 0 && !events.some(isBattleEvent);
91
+ };
92
+ /**
93
+ * Get knockback force from player's equipped weapon
94
+ *
95
+ * Retrieves the knockbackForce property from the player's equipped weapon.
96
+ * Falls back to DEFAULT_KNOCKBACK.force if no weapon or property is set.
97
+ *
98
+ * @param player - The player to get weapon knockback from
99
+ * @returns Knockback force value
100
+ *
101
+ * @example
102
+ * ```ts
103
+ * // Player with weapon having knockbackForce: 80
104
+ * const force = getPlayerWeaponKnockbackForce(player); // 80
105
+ *
106
+ * // No weapon equipped
107
+ * const force = getPlayerWeaponKnockbackForce(player); // 50 (default)
108
+ * ```
109
+ */
110
+ function getPlayerWeaponKnockbackForce(player) {
111
+ try {
112
+ const equipments = player.equipments?.() || [];
113
+ for (const item of equipments) {
114
+ const itemData = player.databaseById?.(item.id());
115
+ if (itemData?._type === "weapon" && itemData.knockbackForce !== void 0) return itemData.knockbackForce;
116
+ }
117
+ } catch {}
118
+ return DEFAULT_KNOCKBACK.force;
119
+ }
120
+ /**
121
+ * Apply hit from player to target (event with AI)
122
+ *
123
+ * Handles damage calculation, knockback based on weapon, and visual effects.
124
+ * Can be customized using hooks.
125
+ *
126
+ * @param player - The attacking player
127
+ * @param target - The event being hit
128
+ * @param hooks - Optional hooks for customizing hit behavior
129
+ * @returns Hit result if AI exists, undefined otherwise
130
+ *
131
+ * @example
132
+ * ```ts
133
+ * // Basic hit
134
+ * const result = applyPlayerHitToEvent(player, event);
135
+ *
136
+ * // With custom hooks
137
+ * const result = applyPlayerHitToEvent(player, event, {
138
+ * onBeforeHit(result) {
139
+ * result.knockbackForce *= 2; // Double knockback
140
+ * return result;
141
+ * },
142
+ * onAfterHit(result) {
143
+ * if (result.defeated) {
144
+ * player.gold += 10;
145
+ * }
146
+ * }
147
+ * });
148
+ * ```
149
+ */
150
+ function applyPlayerHitToEvent(player, target, hooks, metadata) {
151
+ if (!target.battleAi) return void 0;
152
+ return applyActionBattleEntityHit(player, target, hooks, metadata);
153
+ }
154
+ function applyActionBattleEntityHit(attacker, target, hooks, metadata) {
155
+ const ai = target.battleAi;
156
+ if (target instanceof RpgEvent && !ai) return void 0;
157
+ const systems = getActionBattleSystems();
158
+ const result = applyActionBattleHit({
159
+ ...systems.combat,
160
+ hooks: hooks ? {
161
+ ...systems.combat.hooks,
162
+ beforeHit(context) {
163
+ const before = systems.combat.hooks?.beforeHit?.(context);
164
+ if (before === false) return false;
165
+ const nextContext = before || context;
166
+ const legacyResult = toLegacyHitResult(nextContext);
167
+ const modified = hooks.onBeforeHit?.(legacyResult);
168
+ if (!modified) return nextContext;
169
+ return {
170
+ ...nextContext,
171
+ damage: {
172
+ damage: modified.damage,
173
+ defeated: modified.defeated,
174
+ raw: nextContext.damage?.raw
175
+ },
176
+ knockback: {
177
+ force: modified.knockbackForce,
178
+ duration: modified.knockbackDuration,
179
+ direction: nextContext.knockback?.direction
180
+ }
181
+ };
182
+ },
183
+ afterHit(result) {
184
+ systems.combat.hooks?.afterHit?.(result);
185
+ hooks.onAfterHit?.(result);
186
+ }
187
+ } : systems.combat.hooks
188
+ }, {
189
+ attacker,
190
+ target,
191
+ metadata,
192
+ reaction: metadata?.reaction
193
+ });
194
+ if (!result.cancelled && ai) ai.handleDamage(attacker, {
195
+ damage: result.damage,
196
+ defeated: result.defeated,
197
+ raw: result.rawDamage,
198
+ reaction: result.reaction
199
+ });
200
+ return result;
201
+ }
202
+ var toLegacyHitResult = (context) => ({
203
+ damage: context.damage?.damage ?? 0,
204
+ knockbackForce: context.knockback?.force ?? getPlayerWeaponKnockbackForce(context.attacker),
205
+ knockbackDuration: context.knockback?.duration ?? DEFAULT_KNOCKBACK.duration,
206
+ defeated: context.damage?.defeated ?? false,
207
+ attacker: context.attacker,
208
+ target: context.target
209
+ });
210
+ var resolvePlayerAttackHitboxes = (player, directionKey, options, profile) => {
211
+ const configuredHitboxes = {
212
+ ...DEFAULT_PLAYER_ATTACK_HITBOXES,
213
+ ...options.attack?.hitboxes,
214
+ ...profile.hitboxes
215
+ };
216
+ const hitboxConfig = configuredHitboxes[directionKey] || configuredHitboxes.default;
217
+ const defaultHitboxes = [{
218
+ x: player.x() + hitboxConfig.offsetX,
219
+ y: player.y() + hitboxConfig.offsetY,
220
+ width: hitboxConfig.width,
221
+ height: hitboxConfig.height
222
+ }];
223
+ return options.attack?.resolveHitboxes?.({
224
+ player,
225
+ direction: directionKey,
226
+ defaultHitboxes
227
+ }) ?? defaultHitboxes;
228
+ };
229
+ var getActionBattleHitboxCandidates = (map, hitboxes, options = {}) => {
230
+ if (!map) return [];
231
+ if (typeof map.queryHitbox === "function") {
232
+ const candidates = /* @__PURE__ */ new Map();
233
+ for (const hitbox of hitboxes) for (const entity of map.queryHitbox(hitbox, options)) if (entity?.id) candidates.set(entity.id, entity);
234
+ return Array.from(candidates.values());
235
+ }
236
+ const candidates = /* @__PURE__ */ new Map();
237
+ const excluded = new Set(options.excludeIds ?? []);
238
+ const add = (entity) => {
239
+ if (!entity?.id) return;
240
+ if (excluded.has(entity.id)) return;
241
+ const rect = entityRect(entity);
242
+ if (hitboxes.some((hitbox) => rectsOverlap(hitbox, rect))) candidates.set(entity.id, entity);
243
+ };
244
+ const kinds = new Set(options.kinds ?? ["players", "events"]);
245
+ if (kinds.has("players")) map.getPlayers?.().forEach(add);
246
+ if (kinds.has("events")) map.getEvents?.().forEach(add);
247
+ return Array.from(candidates.values());
248
+ };
249
+ var mergeAttackProfileOverrides = (base, override) => ({
250
+ ...base,
251
+ ...override,
252
+ reaction: {
253
+ ...base.reaction,
254
+ ...override.reaction
255
+ },
256
+ hitboxes: {
257
+ ...base.hitboxes,
258
+ ...override.hitboxes
259
+ }
260
+ });
261
+ var resolvePlayerAttackProfile = (player, options) => {
262
+ const baseProfile = getNormalizedActionBattleAttackProfile(options);
263
+ const weaponProfile = resolveActionBattleWeaponAttackProfile(player);
264
+ if (!weaponProfile) return baseProfile;
265
+ return normalizeActionBattleAttackProfile(mergeAttackProfileOverrides(baseProfile, weaponProfile), {
266
+ lockMovement: options.attack?.lockMovement,
267
+ lockDurationMs: options.attack?.lockDurationMs,
268
+ hitboxes: options.attack?.hitboxes
269
+ });
270
+ };
271
+ var resolveSignal = (value) => typeof value === "function" ? value() : value;
272
+ var resolveItemData = (player, itemId) => {
273
+ try {
274
+ return player.databaseById?.(itemId);
275
+ } catch {
276
+ return null;
277
+ }
278
+ };
279
+ var resolveSkillData = (player, skillId) => {
280
+ try {
281
+ return player.databaseById?.(skillId);
282
+ } catch {
283
+ return null;
284
+ }
285
+ };
286
+ var resolvePlayerSkillUsable = (player, skillId) => {
287
+ try {
288
+ return player.getSkill?.(skillId) ?? resolveSkillData(player, skillId);
289
+ } catch {
290
+ return resolveSkillData(player, skillId);
291
+ }
292
+ };
293
+ var resolveSkillTargeting = (player, skillId, options) => {
294
+ const skillsOptions = options.skills;
295
+ const skillData = resolveSkillData(player, skillId);
296
+ if (skillsOptions?.getTargeting) return skillsOptions.getTargeting(skillData);
297
+ const range = skillData?.range ?? skillData?.targeting?.range ?? skillData?.targeting?.distance;
298
+ const aoeMask = skillData?.aoeMask ?? skillData?.targeting?.aoeMask ?? skillData?.targeting?.mask;
299
+ if (range === void 0 && aoeMask === void 0) return null;
300
+ return {
301
+ range: range ?? 0,
302
+ aoeMask
303
+ };
304
+ };
305
+ var normalizeMaskRows = (mask) => {
306
+ if (!mask) return [];
307
+ if (Array.isArray(mask)) return mask;
308
+ return mask.trim().split("\n").map((row) => row.replace(/\r/g, ""));
309
+ };
310
+ var buildActionBarData = (player, options) => {
311
+ return {
312
+ items: (player.items?.() || []).map((item) => {
313
+ const id = item.id?.() ?? item.id;
314
+ const data = resolveItemData(player, id);
315
+ const name = resolveSignal(data?.name) ?? resolveSignal(item.name) ?? id;
316
+ const description = resolveSignal(data?.description) ?? resolveSignal(item.description) ?? "";
317
+ const icon = resolveSignal(data?.icon) ?? resolveSignal(item.icon);
318
+ const quantity = resolveSignal(item.quantity) ?? 1;
319
+ const consumable = resolveSignal(data?.consumable);
320
+ const itemType = resolveSignal(data?._type);
321
+ return {
322
+ id,
323
+ name,
324
+ description,
325
+ icon,
326
+ quantity,
327
+ usable: quantity > 0 && consumable !== false && (itemType ? itemType === "item" : true)
328
+ };
329
+ }),
330
+ skills: (player.skills?.() || []).map((skill) => {
331
+ const id = skill.id?.() ?? skill.id;
332
+ const data = resolveSkillData(player, id) || skill;
333
+ const name = resolveSignal(data?.name) ?? resolveSignal(skill.name) ?? id;
334
+ const description = resolveSignal(data?.description) ?? resolveSignal(skill.description) ?? "";
335
+ const icon = resolveSignal(data?.icon) ?? resolveSignal(skill.icon);
336
+ const spCost = resolveSignal(data?.spCost) ?? resolveSignal(skill.spCost) ?? 0;
337
+ const usable = spCost <= player.sp;
338
+ const targeting = resolveSkillTargeting(player, id, options);
339
+ const skillEntry = {
340
+ id,
341
+ name,
342
+ description,
343
+ icon,
344
+ spCost,
345
+ usable,
346
+ range: targeting?.range ?? 0
347
+ };
348
+ if (targeting) {
349
+ const mask = targeting.aoeMask ?? options.skills?.defaultAoeMask;
350
+ if (mask) skillEntry.aoeMask = normalizeMaskRows(mask);
351
+ }
352
+ return skillEntry;
353
+ })
354
+ };
355
+ };
356
+ var ensureActionBarGui = (player, options) => {
357
+ const gui = player.getGui?.("action-battle-action-bar") || player.gui("action-battle-action-bar");
358
+ if (!gui.__actionBattleReady) {
359
+ gui.__actionBattleReady = true;
360
+ gui.on("useItem", ({ id }) => {
361
+ try {
362
+ player.useItem(id);
363
+ } catch {}
364
+ gui.update(buildActionBarData(player, options));
365
+ });
366
+ gui.on("useSkill", ({ id, target }) => {
367
+ handleActionBattleSkillUse(player, id, target, options);
368
+ gui.update(buildActionBarData(player, options));
369
+ });
370
+ gui.on("refresh", () => {
371
+ gui.update(buildActionBarData(player, options));
372
+ });
373
+ }
374
+ return gui;
375
+ };
376
+ var openActionBattleActionBar = (player, rawOptions = {}) => {
377
+ const options = normalizeActionBattleOptions(rawOptions);
378
+ ensureActionBarGui(player, options).open(buildActionBarData(player, options));
379
+ };
380
+ var updateActionBattleActionBar = (player, rawOptions = {}) => {
381
+ const options = normalizeActionBattleOptions(rawOptions);
382
+ const gui = player.getGui?.(ACTION_BATTLE_ACTION_BAR_GUI_ID);
383
+ if (gui) gui.update(buildActionBarData(player, options));
384
+ };
385
+ var getTileSize = (map) => ({
386
+ width: map?.tileWidth ?? 32,
387
+ height: map?.tileHeight ?? 32
388
+ });
389
+ var getEntityTile = (entity, tileSize) => {
390
+ const hitbox = entity.hitbox?.() || {
391
+ w: tileSize.width,
392
+ h: tileSize.height
393
+ };
394
+ return {
395
+ x: Math.floor((entity.x() + hitbox.w / 2) / tileSize.width),
396
+ y: Math.floor((entity.y() + hitbox.h / 2) / tileSize.height)
397
+ };
398
+ };
399
+ var handleActionBattleSkillUse = (player, skillId, target, options) => {
400
+ const skillData = resolvePlayerSkillUsable(player, skillId);
401
+ const actionConfig = getActionBattleActionConfig(skillData);
402
+ if (actionConfig?.target === "self") {
403
+ executeActionBattleUse({
404
+ attacker: player,
405
+ target: player,
406
+ usable: skillData,
407
+ skill: skillData
408
+ });
409
+ return;
410
+ }
411
+ const map = player.getCurrentMap();
412
+ if (!map) {
413
+ emitActionBattleClientVisual({
414
+ moment: "castSkill",
415
+ entity: player,
416
+ skill: skillData
417
+ });
418
+ player.useSkill(skillId);
419
+ return;
420
+ }
421
+ const targeting = resolveSkillTargeting(player, skillId, options);
422
+ if (!targeting || !target) {
423
+ emitActionBattleClientVisual({
424
+ moment: "castSkill",
425
+ entity: player,
426
+ skill: skillData
427
+ });
428
+ player.useSkill(skillId);
429
+ return;
430
+ }
431
+ const tileSize = getTileSize(map);
432
+ const origin = getEntityTile(player, tileSize);
433
+ const targetTile = {
434
+ x: target.x,
435
+ y: target.y
436
+ };
437
+ if (manhattanDistance(origin, targetTile) > targeting.range) return;
438
+ const mask = parseAoeMask(targeting.aoeMask || options.skills?.defaultAoeMask);
439
+ const affected = /* @__PURE__ */ new Set();
440
+ mask.cells.forEach((cell) => {
441
+ const x = targetTile.x + cell.dx;
442
+ const y = targetTile.y + cell.dy;
443
+ affected.add(`${x},${y}`);
444
+ });
445
+ const targets = [];
446
+ const actionTarget = actionConfig?.target ?? "enemy";
447
+ const affects = options.targeting?.affects || "events";
448
+ if (affects === "events" || affects === "both") map.getEvents().forEach((event) => {
449
+ const tile = getEntityTile(event, tileSize);
450
+ if (affected.has(`${tile.x},${tile.y}`) && canActionBattleUseTarget(player, event, actionTarget, options.combat?.targets)) targets.push(event);
451
+ });
452
+ if (affects === "players" || affects === "both") map.getPlayers().forEach((other) => {
453
+ if (other.id === player.id) return;
454
+ const tile = getEntityTile(other, tileSize);
455
+ if (affected.has(`${tile.x},${tile.y}`) && canActionBattleUseTarget(player, other, actionTarget, options.combat?.targets)) targets.push(other);
456
+ });
457
+ if (!options.targeting?.allowEmptyTarget && targets.length === 0) return;
458
+ executeActionBattleUse({
459
+ attacker: player,
460
+ target: targets,
461
+ usable: skillData,
462
+ skill: skillData
463
+ });
464
+ };
465
+ var createActionBattleServer = (rawOptions = {}) => {
466
+ const options = normalizeActionBattleOptions(rawOptions);
467
+ setActionBattleOptions(options);
468
+ setActionBattleSystems(options);
469
+ return defineModule({
470
+ player: {
471
+ /**
472
+ * Handle player input for combat actions
473
+ *
474
+ * When a player presses the action key, create an attack hitbox
475
+ * that can damage AI enemies within range and knockback the event.
476
+ * Knockback force is based on the player's equipped weapon.
477
+ * Triggers attack animation and visual effects.
478
+ *
479
+ * @param player - The player performing the action
480
+ * @param input - Input data containing pressed keys
481
+ */
482
+ onInput(player, input) {
483
+ if (input.action == Control.Action) {
484
+ const map = player.getCurrentMap();
485
+ const direction = resolveActionBattleAttackDirection(player, input);
486
+ applyActionBattleAttackDirection(player, direction);
487
+ const attackProfile = resolvePlayerAttackProfile(player, options);
488
+ const directionKey = direction;
489
+ const resolveActiveHitboxes = () => resolvePlayerAttackHitboxes(player, directionKey, options, attackProfile);
490
+ if (isActionReservedForNormalEvent(player, map, resolveActiveHitboxes())) return;
491
+ const lockMovement = attackProfile.movementLock;
492
+ const lockDirection = attackProfile.directionLock;
493
+ const lockDurationMs = attackProfile.totalDurationMs ?? DEFAULT_ATTACK_LOCK_DURATION_MS;
494
+ const actionLocked = (lockMovement || lockDirection) && lockDurationMs > 0;
495
+ if (actionLocked && !beginPlayerAttackLock(player, map, Math.max(0, lockDurationMs), {
496
+ movement: lockMovement,
497
+ direction: lockDirection
498
+ })) return;
499
+ withActionBattleAnimationUnlocked(player, () => {
500
+ emitActionBattleClientVisual({
501
+ moment: "attack",
502
+ entity: player
503
+ });
504
+ });
505
+ if (actionLocked) player.animationFixed = true;
506
+ const attackId = createActionBattleAttackId(player.id, attackProfile.id);
507
+ const weapon = resolveActionBattleWeapon(player);
508
+ const hitTracker = new ActionBattleHitTracker(attackProfile.hitPolicy);
509
+ const targetSelector = getActionBattleTargets(player, "events");
510
+ const processHits = (hits) => {
511
+ hits.forEach((hit) => {
512
+ if (!canActionBattleTarget(player, hit, targetSelector, options.combat?.targets)) return;
513
+ if (!hitTracker.tryHit(hit)) return;
514
+ if (weapon && executeActionBattleUse({
515
+ attacker: player,
516
+ target: hit,
517
+ usable: weapon,
518
+ weapon,
519
+ profile: attackProfile,
520
+ playVisual: false
521
+ })) return;
522
+ applyActionBattleEntityHit(player, hit, void 0, {
523
+ attackId,
524
+ attackProfileId: attackProfile.id,
525
+ reaction: attackProfile.reaction
526
+ });
527
+ });
528
+ };
529
+ runActionBattleActiveHitbox(attackProfile, resolveActiveHitboxes, (activeHitboxes) => {
530
+ processHits(getActionBattleHitboxCandidates(map, activeHitboxes, {
531
+ excludeIds: [player.id],
532
+ kinds: ["players", "events"]
533
+ }));
534
+ });
535
+ }
536
+ },
537
+ onConnected(player) {
538
+ const actionBar = options.ui?.actionBar;
539
+ if (actionBar?.enabled && actionBar?.autoOpen) openActionBattleActionBar(player, options);
540
+ }
541
+ },
542
+ event: {
543
+ /**
544
+ * Handle player detection when entering AI vision
545
+ *
546
+ * Called when a player enters an AI event's vision range.
547
+ * The AI will start pursuing and attacking the player.
548
+ *
549
+ * @param event - The AI event
550
+ * @param player - The player entering vision
551
+ * @param shape - The vision shape
552
+ */
553
+ onDetectInShape(event, player, shape) {
554
+ event.battleAi?.onDetectInShape(player, shape);
555
+ },
556
+ /**
557
+ * Handle player leaving AI vision
558
+ *
559
+ * Called when a player leaves an AI event's vision range.
560
+ * The AI will stop pursuing the player.
561
+ *
562
+ * @param event - The AI event
563
+ * @param player - The player leaving vision
564
+ * @param shape - The vision shape
565
+ */
566
+ onDetectOutShape(event, player, shape) {
567
+ event.battleAi?.onDetectOutShape(player, shape);
568
+ }
569
+ },
570
+ projectiles: {
571
+ onImpact(context) {
572
+ handleActionBattleProjectileImpact({
573
+ attacker: context.projectile?.payload?.attackerId ? context.map?.getObjectById?.(context.projectile.payload.attackerId) : void 0,
574
+ target: context.target,
575
+ projectile: context.projectile,
576
+ hit: context.hit,
577
+ map: context.map
578
+ });
579
+ },
580
+ onDestroy(context) {
581
+ handleActionBattleProjectileDestroy(context.projectile?.id);
582
+ }
583
+ }
584
+ });
585
+ };
586
+ var server_default = createActionBattleServer();
587
+ //#endregion
588
+ export { ACTION_BATTLE_ACTION_BAR_GUI_ID, DEFAULT_PLAYER_ATTACK_HITBOXES, applyActionBattleEntityHit, applyPlayerHitToEvent, createActionBattleServer, server_default as default, getPlayerWeaponKnockbackForce, openActionBattleActionBar, updateActionBattleActionBar };