@vibemancer/core 0.1.0 → 0.1.1

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 (50) hide show
  1. package/README.md +28 -28
  2. package/dist/{chunk-L7Z7OFXD.js → chunk-7VDJHO22.js} +3 -3
  3. package/dist/chunk-7VDJHO22.js.map +1 -0
  4. package/dist/index-browser.d.ts +1 -1
  5. package/dist/index-browser.js +1 -1
  6. package/dist/index.js +1 -1
  7. package/package.json +79 -78
  8. package/src/bots/berserker/01_Stormchaser.ts +457 -457
  9. package/src/bots/berserker/02_Stormcaller.ts +417 -417
  10. package/src/bots/berserker/03_Stormforger.ts +481 -481
  11. package/src/bots/caster/01_Flamecaller.ts +286 -286
  12. package/src/bots/caster/02_Pyromancer.ts +350 -350
  13. package/src/bots/caster/03_Infernalist.ts +492 -492
  14. package/src/bots/defensive/01_Turtle.ts +151 -151
  15. package/src/bots/defensive/02_Sentinel.ts +134 -134
  16. package/src/bots/defensive/03_Golem.ts +357 -357
  17. package/src/bots/duelist/01_Battlemage.ts +433 -433
  18. package/src/bots/duelist/02_Warmage.ts +438 -438
  19. package/src/bots/duelist/03_Archmage.ts +588 -588
  20. package/src/bots/homing/01_Bonemancer.ts +67 -67
  21. package/src/bots/homing/02_Lich.ts +356 -356
  22. package/src/bots/homing/03_Archlich.ts +220 -220
  23. package/src/bots/kiter/01_Spellspinner.ts +398 -398
  24. package/src/bots/kiter/02_Spellweaver.ts +378 -378
  25. package/src/bots/kiter/03_Spellbinder.ts +448 -448
  26. package/src/bots/melee/01_Shadowblade.ts +270 -270
  27. package/src/bots/melee/02_Nightblade.ts +437 -437
  28. package/src/bots/melee/03_Voidblade.ts +582 -582
  29. package/src/bots/sniper/01_Spellshot.ts +385 -385
  30. package/src/bots/sniper/02_Spelltracer.ts +441 -441
  31. package/src/bots/sniper/03_Spellseeker.ts +546 -546
  32. package/src/bots/standalone/Critter.ts +89 -89
  33. package/src/bots/standalone/Doombringer.ts +91 -91
  34. package/src/bots/standalone/Hogger.ts +228 -228
  35. package/src/bots/standalone/Rookie.ts +50 -50
  36. package/src/bots/standalone/TargetDummy.ts +21 -21
  37. package/src/bots/test/cheater.ts +405 -405
  38. package/src/bots/test/crasher.ts +81 -81
  39. package/src/engine/hooks-runtime.ts +394 -394
  40. package/src/engine/manual-match.ts +289 -289
  41. package/src/engine/missile-templates.ts +155 -155
  42. package/src/engine/physics.ts +143 -143
  43. package/src/engine/simulation.ts +828 -828
  44. package/src/engine/spells.ts +128 -128
  45. package/src/engine-version.ts +1 -1
  46. package/src/index.ts +23 -23
  47. package/src/rules.ts +254 -254
  48. package/src/types.ts +193 -193
  49. package/src/utils/index.ts +6 -6
  50. package/dist/chunk-L7Z7OFXD.js.map +0 -1
@@ -1,350 +1,350 @@
1
- import {WizardFunction, MissileAIFunction} from '../../types.js';
2
- import {angleTo} from '../../utils/angles.js';
3
- import {distanceTo} from '../../utils/distance.js';
4
- import {ARENA_SIZE, GCD_DURATION, SHIELD_CAST_TIME} from '../../rules.js';
5
- import {
6
- getThreats,
7
- getMostUrgentThreat,
8
- getDodgeDirectionForMovement,
9
- isSafeToAttack,
10
- getBlinkToCenter,
11
- fitMissileToBudget,
12
- shouldForceShield,
13
- MAX_CAST_BUDGET,
14
- MIN_USEFUL_DAMAGE,
15
- } from '../shared.js';
16
- import {useParam} from '../../engine/params-runtime.js';
17
- const WALL_BUFFER = 60;
18
-
19
- const SHIELD_BUFFER = 3;
20
-
21
- const HomingAI: MissileAIFunction = ({worldState}) =>
22
- {
23
- const enemy = worldState.enemies[0];
24
- return {turnToward: enemy?.position};
25
- };
26
-
27
- function isNearWall(position: {x: number; y: number}): boolean
28
- {
29
- return (
30
- position.x < WALL_BUFFER ||
31
- position.x > ARENA_SIZE - WALL_BUFFER ||
32
- position.y < WALL_BUFFER ||
33
- position.y > ARENA_SIZE - WALL_BUFFER
34
- );
35
- }
36
-
37
- function smartStrafe(
38
- position: {x: number; y: number},
39
- enemy: {position: {x: number; y: number}},
40
- dodgeDirection: {x: number; y: number} | null,
41
- tick: number,
42
- strafeCyclePeriod: number,
43
- strafeIntensity: number,
44
- ): {x: number; y: number}
45
- {
46
- const deltaX = enemy.position.x - position.x;
47
- const deltaY = enemy.position.y - position.y;
48
- const normalizedDistance = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
49
- if (normalizedDistance === 0) return {x: 0, y: 0};
50
-
51
- const strafeClockwise = {
52
- x: -deltaY / normalizedDistance,
53
- y: deltaX / normalizedDistance,
54
- };
55
- const strafeCounterClockwise = {
56
- x: deltaY / normalizedDistance,
57
- y: -deltaX / normalizedDistance,
58
- };
59
-
60
- let strafeDir: {x: number; y: number};
61
-
62
- if (dodgeDirection)
63
- {
64
- strafeDir = dodgeDirection;
65
- }
66
- else if (isNearWall(position))
67
- {
68
- const futureClockwise = {
69
- x: position.x + strafeClockwise.x * 100,
70
- y: position.y + strafeClockwise.y * 100,
71
- };
72
- const futureCounterClockwise = {
73
- x: position.x + strafeCounterClockwise.x * 100,
74
- y: position.y + strafeCounterClockwise.y * 100,
75
- };
76
- const scoreClockwise = Math.min(
77
- futureClockwise.x,
78
- ARENA_SIZE - futureClockwise.x,
79
- futureClockwise.y,
80
- ARENA_SIZE - futureClockwise.y,
81
- );
82
- const scoreCounterClockwise = Math.min(
83
- futureCounterClockwise.x,
84
- ARENA_SIZE - futureCounterClockwise.x,
85
- futureCounterClockwise.y,
86
- ARENA_SIZE - futureCounterClockwise.y,
87
- );
88
- strafeDir =
89
- scoreClockwise > scoreCounterClockwise
90
- ? strafeClockwise
91
- : strafeCounterClockwise;
92
- }
93
- else
94
- {
95
- const cycle = Math.floor(tick / strafeCyclePeriod) % 2;
96
- strafeDir = cycle === 0 ? strafeClockwise : strafeCounterClockwise;
97
- }
98
-
99
- const intensity = dodgeDirection ? 100 : strafeIntensity;
100
- return {x: strafeDir.x * intensity, y: strafeDir.y * intensity};
101
- }
102
-
103
- /**
104
- * Bot: Pyromancer
105
- *
106
- * BEHAVIOR: Adaptive homing missile specialist at long range. Maintains 400u
107
- * distance, strafes to dodge, and uses fitMissileToBudget with minTurnRate 0.5
108
- * to fire the highest-damage homing missile that fits in the safe window.
109
- * Shields undodgeable threats, emergency blinks. A versatile ranged caster
110
- * that adapts its missiles to the situation.
111
- *
112
- * PROGRESSION LINE: Flamecaller → Pyromancer → Infernalist
113
- * - Flamecaller (tier 1): Fixed homing missiles, basic strafe and defense
114
- * - Pyromancer (tier 2): + adaptive fitting, cast canceling, smart fallbacks
115
- * - Infernalist (tier 3): Future — overwhelming adaptive fire
116
- *
117
- * TIER: 2 (enhanced Flamecaller)
118
- */
119
- export const Pyromancer: WizardFunction = ({state}) =>
120
- {
121
- const targetDistance = useParam('targetDistance', 643, {
122
- range: 225,
123
- min: 0,
124
- });
125
- const minTurnRate = useParam('minTurnRate', 0.2, {range: 1.5, steps: 5});
126
- const strafeCyclePeriod = useParam('strafeCyclePeriod', 129, {
127
- range: 75,
128
- min: 80,
129
- max: 300,
130
- });
131
- const strafeIntensity = useParam('strafeIntensity', 33, {
132
- range: 40,
133
- min: 20,
134
- max: 100,
135
- steps: 7,
136
- });
137
- const approachSpeed = useParam('approachSpeed', 69, {
138
- range: 30,
139
- min: 10,
140
- max: 70,
141
- steps: 7,
142
- });
143
- const distanceDeadZone = useParam('distanceDeadZone', 18, {
144
- range: 30,
145
- min: 5,
146
- max: 80,
147
- steps: 7,
148
- });
149
- const shieldCancelThreshold = useParam('shieldCancelThreshold', 150, {
150
- range: 75,
151
- min: 50,
152
- max: 300,
153
- });
154
- const blinkWindowMax = useParam('blinkWindowMax', 26, {
155
- range: 40,
156
- min: 20,
157
- max: 120,
158
- steps: 7,
159
- });
160
- const flightTimeDivisor = useParam('flightTimeDivisor', 5.3, {
161
- range: 4,
162
- min: 2,
163
- max: 12,
164
- steps: 5,
165
- });
166
- const safeAttackCastEstimate = useParam('safeAttackCastEstimate', 35, {
167
- range: 25,
168
- min: 10,
169
- max: 80,
170
- steps: 5,
171
- });
172
-
173
- const enemy = state.enemies[0];
174
- if (!enemy)
175
- {
176
- return {move: {x: 0, y: 0}};
177
- }
178
-
179
- const distance = distanceTo(state.position, enemy.position);
180
-
181
- const threats = getThreats(state);
182
- const urgentThreat = getMostUrgentThreat(threats);
183
-
184
- const dodgeDirection = getDodgeDirectionForMovement(threats, state.position);
185
- const strafe = smartStrafe(
186
- state.position,
187
- enemy,
188
- dodgeDirection,
189
- state.tick,
190
- strafeCyclePeriod,
191
- strafeIntensity,
192
- );
193
-
194
- // Distance management (retreat if too close, approach if too far)
195
- let moveX = strafe.x;
196
- let moveY = strafe.y;
197
-
198
- if (!dodgeDirection)
199
- {
200
- const deltaX = enemy.position.x - state.position.x;
201
- const deltaY = enemy.position.y - state.position.y;
202
- const normalizedDistance = distance > 0 ? distance : 1;
203
-
204
- if (distance > targetDistance + distanceDeadZone)
205
- {
206
- moveX += (deltaX / normalizedDistance) * approachSpeed;
207
- moveY += (deltaY / normalizedDistance) * approachSpeed;
208
- }
209
- else if (distance < targetDistance - distanceDeadZone)
210
- {
211
- moveX -= (deltaX / normalizedDistance) * approachSpeed;
212
- moveY -= (deltaY / normalizedDistance) * approachSpeed;
213
- }
214
- }
215
-
216
- const magnitude = Math.sqrt(moveX * moveX + moveY * moveY);
217
- if (magnitude > 100)
218
- {
219
- moveX = (moveX / magnitude) * 100;
220
- moveY = (moveY / magnitude) * 100;
221
- }
222
-
223
- const move = {x: moveX, y: moveY};
224
-
225
- // === DEFENSE: Handle channeling ===
226
- if (state.state === 'channeling')
227
- {
228
- if (!urgentThreat || urgentThreat.ticksToImpact > shieldCancelThreshold)
229
- {
230
- return {move, cancel: true};
231
- }
232
- return {move: {x: 0, y: 0}};
233
- }
234
-
235
- // === DEFENSE: Cancel missile cast only for LETHAL incoming damage ===
236
- // DPS trade: we accepted the hit when we started casting. Only cancel to survive.
237
- if (
238
- state.state === 'casting' &&
239
- state.castingSpell === 'missile' &&
240
- urgentThreat &&
241
- urgentThreat.projectile.damage >= state.health
242
- )
243
- {
244
- const remainingCast = (state.castDuration ?? 0) - (state.castProgress ?? 0);
245
- if (
246
- urgentThreat.ticksToImpact <=
247
- remainingCast + GCD_DURATION + SHIELD_CAST_TIME + SHIELD_BUFFER
248
- )
249
- {
250
- return {move, cancel: true};
251
- }
252
- }
253
-
254
- // === BLINK: Dodge missile + reposition ===
255
- // Blink dodges the incoming missile — no GCD after, so can immediately fire back.
256
- // Wide window (10-80) prevents idle gaps where budget is too small for a missile
257
- // but the threat is too far for the old narrow blink window.
258
- if (
259
- state.state === 'idle' &&
260
- urgentThreat &&
261
- state.blinkCooldown === 0 &&
262
- urgentThreat.ticksToImpact >= 10 &&
263
- urgentThreat.ticksToImpact <= blinkWindowMax
264
- )
265
- {
266
- return {
267
- move: {x: 0, y: 0},
268
- startCast: {spell: 'blink', target: getBlinkToCenter(state.position)},
269
- };
270
- }
271
-
272
- // === DEFENSE: Shield undodgeable or high-damage homing threats (when blink on cooldown) ===
273
- if (
274
- state.state === 'idle' &&
275
- urgentThreat &&
276
- (!urgentThreat.bestDodgeDirection || shouldForceShield(urgentThreat))
277
- )
278
- {
279
- if (urgentThreat.canBlockInTime)
280
- {
281
- if (urgentThreat.ticksToStartShield <= SHIELD_BUFFER)
282
- {
283
- return {
284
- move: {x: 0, y: 0},
285
- startCast: {spell: 'shield'},
286
- };
287
- }
288
- }
289
- }
290
-
291
- // === OFFENSE: Adaptive homing missiles ===
292
- // Skip offense when a lethal missile could reach us during cast+GCD — dodge at full speed instead
293
- const myProjectileIds = new Set(state.myProjectiles.map((p) => p.id));
294
- const hasNearbyLethalMissile = state.projectiles.some(
295
- (p) =>
296
- !myProjectileIds.has(p.id) &&
297
- p.damage >= state.health &&
298
- distanceTo(state.position, p.position) / p.speed <
299
- MAX_CAST_BUDGET + GCD_DURATION,
300
- );
301
- if (state.state === 'idle' && !hasNearbyLethalMissile)
302
- {
303
- let nextThreatTime = urgentThreat?.ticksToImpact ?? Infinity;
304
-
305
- // Account for enemy's active cast — their missile will arrive after cast completes + flight time
306
- if (enemy.state === 'casting' && enemy.castingSpell === 'missile')
307
- {
308
- const remainingEnemyCast =
309
- (enemy.castDuration ?? 0) - (enemy.castProgress ?? 0);
310
- const estimatedFlightTime = distance / flightTimeDivisor;
311
- nextThreatTime = Math.min(
312
- nextThreatTime,
313
- remainingEnemyCast + estimatedFlightTime,
314
- );
315
- }
316
-
317
- const safeBudget =
318
- nextThreatTime - GCD_DURATION - SHIELD_CAST_TIME - SHIELD_BUFFER;
319
- const budgetTicks = Math.min(safeBudget, MAX_CAST_BUDGET);
320
-
321
- // Pyromancer wants homing missiles — no maxDamage cap so missiles punch through shields
322
- const enemyHP = Math.ceil(enemy.health);
323
- const minDmg = Math.min(MIN_USEFUL_DAMAGE, enemyHP);
324
- const config = fitMissileToBudget(budgetTicks, distance, {
325
- minTurnRate,
326
- lastMissileConfig: state.lastMissileConfig,
327
- });
328
-
329
- if (
330
- config &&
331
- config.damage >= minDmg &&
332
- isSafeToAttack(threats, safeAttackCastEstimate)
333
- )
334
- {
335
- return {
336
- move,
337
- startCast: {
338
- spell: 'missile',
339
- config,
340
- missileAI: HomingAI,
341
- direction: angleTo(state.position, enemy.position),
342
- },
343
- };
344
- }
345
-
346
- // Budget too small for useful missile — wait for threat to pass, then fire
347
- }
348
-
349
- return {move};
350
- };
1
+ import {WizardFunction, MissileAIFunction} from '../../types.js';
2
+ import {angleTo} from '../../utils/angles.js';
3
+ import {distanceTo} from '../../utils/distance.js';
4
+ import {ARENA_SIZE, GCD_DURATION, SHIELD_CAST_TIME} from '../../rules.js';
5
+ import {
6
+ getThreats,
7
+ getMostUrgentThreat,
8
+ getDodgeDirectionForMovement,
9
+ isSafeToAttack,
10
+ getBlinkToCenter,
11
+ fitMissileToBudget,
12
+ shouldForceShield,
13
+ MAX_CAST_BUDGET,
14
+ MIN_USEFUL_DAMAGE,
15
+ } from '../shared.js';
16
+ import {useParam} from '../../engine/params-runtime.js';
17
+ const WALL_BUFFER = 60;
18
+
19
+ const SHIELD_BUFFER = 3;
20
+
21
+ const HomingAI: MissileAIFunction = ({worldState}) =>
22
+ {
23
+ const enemy = worldState.enemies[0];
24
+ return {turnToward: enemy?.position};
25
+ };
26
+
27
+ function isNearWall(position: {x: number; y: number}): boolean
28
+ {
29
+ return (
30
+ position.x < WALL_BUFFER ||
31
+ position.x > ARENA_SIZE - WALL_BUFFER ||
32
+ position.y < WALL_BUFFER ||
33
+ position.y > ARENA_SIZE - WALL_BUFFER
34
+ );
35
+ }
36
+
37
+ function smartStrafe(
38
+ position: {x: number; y: number},
39
+ enemy: {position: {x: number; y: number}},
40
+ dodgeDirection: {x: number; y: number} | null,
41
+ tick: number,
42
+ strafeCyclePeriod: number,
43
+ strafeIntensity: number,
44
+ ): {x: number; y: number}
45
+ {
46
+ const deltaX = enemy.position.x - position.x;
47
+ const deltaY = enemy.position.y - position.y;
48
+ const normalizedDistance = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
49
+ if (normalizedDistance === 0) return {x: 0, y: 0};
50
+
51
+ const strafeClockwise = {
52
+ x: -deltaY / normalizedDistance,
53
+ y: deltaX / normalizedDistance,
54
+ };
55
+ const strafeCounterClockwise = {
56
+ x: deltaY / normalizedDistance,
57
+ y: -deltaX / normalizedDistance,
58
+ };
59
+
60
+ let strafeDir: {x: number; y: number};
61
+
62
+ if (dodgeDirection)
63
+ {
64
+ strafeDir = dodgeDirection;
65
+ }
66
+ else if (isNearWall(position))
67
+ {
68
+ const futureClockwise = {
69
+ x: position.x + strafeClockwise.x * 100,
70
+ y: position.y + strafeClockwise.y * 100,
71
+ };
72
+ const futureCounterClockwise = {
73
+ x: position.x + strafeCounterClockwise.x * 100,
74
+ y: position.y + strafeCounterClockwise.y * 100,
75
+ };
76
+ const scoreClockwise = Math.min(
77
+ futureClockwise.x,
78
+ ARENA_SIZE - futureClockwise.x,
79
+ futureClockwise.y,
80
+ ARENA_SIZE - futureClockwise.y,
81
+ );
82
+ const scoreCounterClockwise = Math.min(
83
+ futureCounterClockwise.x,
84
+ ARENA_SIZE - futureCounterClockwise.x,
85
+ futureCounterClockwise.y,
86
+ ARENA_SIZE - futureCounterClockwise.y,
87
+ );
88
+ strafeDir =
89
+ scoreClockwise > scoreCounterClockwise
90
+ ? strafeClockwise
91
+ : strafeCounterClockwise;
92
+ }
93
+ else
94
+ {
95
+ const cycle = Math.floor(tick / strafeCyclePeriod) % 2;
96
+ strafeDir = cycle === 0 ? strafeClockwise : strafeCounterClockwise;
97
+ }
98
+
99
+ const intensity = dodgeDirection ? 100 : strafeIntensity;
100
+ return {x: strafeDir.x * intensity, y: strafeDir.y * intensity};
101
+ }
102
+
103
+ /**
104
+ * Bot: Pyromancer
105
+ *
106
+ * BEHAVIOR: Adaptive homing missile specialist at long range. Maintains 400u
107
+ * distance, strafes to dodge, and uses fitMissileToBudget with minTurnRate 0.5
108
+ * to fire the highest-damage homing missile that fits in the safe window.
109
+ * Shields undodgeable threats, emergency blinks. A versatile ranged caster
110
+ * that adapts its missiles to the situation.
111
+ *
112
+ * PROGRESSION LINE: Flamecaller → Pyromancer → Infernalist
113
+ * - Flamecaller (tier 1): Fixed homing missiles, basic strafe and defense
114
+ * - Pyromancer (tier 2): + adaptive fitting, cast canceling, smart fallbacks
115
+ * - Infernalist (tier 3): Future — overwhelming adaptive fire
116
+ *
117
+ * TIER: 2 (enhanced Flamecaller)
118
+ */
119
+ export const Pyromancer: WizardFunction = ({state}) =>
120
+ {
121
+ const targetDistance = useParam('targetDistance', 643, {
122
+ range: 225,
123
+ min: 0,
124
+ });
125
+ const minTurnRate = useParam('minTurnRate', 0.2, {range: 1.5, steps: 5});
126
+ const strafeCyclePeriod = useParam('strafeCyclePeriod', 129, {
127
+ range: 75,
128
+ min: 80,
129
+ max: 300,
130
+ });
131
+ const strafeIntensity = useParam('strafeIntensity', 33, {
132
+ range: 40,
133
+ min: 20,
134
+ max: 100,
135
+ steps: 7,
136
+ });
137
+ const approachSpeed = useParam('approachSpeed', 69, {
138
+ range: 30,
139
+ min: 10,
140
+ max: 70,
141
+ steps: 7,
142
+ });
143
+ const distanceDeadZone = useParam('distanceDeadZone', 18, {
144
+ range: 30,
145
+ min: 5,
146
+ max: 80,
147
+ steps: 7,
148
+ });
149
+ const shieldCancelThreshold = useParam('shieldCancelThreshold', 150, {
150
+ range: 75,
151
+ min: 50,
152
+ max: 300,
153
+ });
154
+ const blinkWindowMax = useParam('blinkWindowMax', 26, {
155
+ range: 40,
156
+ min: 20,
157
+ max: 120,
158
+ steps: 7,
159
+ });
160
+ const flightTimeDivisor = useParam('flightTimeDivisor', 5.3, {
161
+ range: 4,
162
+ min: 2,
163
+ max: 12,
164
+ steps: 5,
165
+ });
166
+ const safeAttackCastEstimate = useParam('safeAttackCastEstimate', 35, {
167
+ range: 25,
168
+ min: 10,
169
+ max: 80,
170
+ steps: 5,
171
+ });
172
+
173
+ const enemy = state.enemies[0];
174
+ if (!enemy)
175
+ {
176
+ return {move: {x: 0, y: 0}};
177
+ }
178
+
179
+ const distance = distanceTo(state.position, enemy.position);
180
+
181
+ const threats = getThreats(state);
182
+ const urgentThreat = getMostUrgentThreat(threats);
183
+
184
+ const dodgeDirection = getDodgeDirectionForMovement(threats, state.position);
185
+ const strafe = smartStrafe(
186
+ state.position,
187
+ enemy,
188
+ dodgeDirection,
189
+ state.tick,
190
+ strafeCyclePeriod,
191
+ strafeIntensity,
192
+ );
193
+
194
+ // Distance management (retreat if too close, approach if too far)
195
+ let moveX = strafe.x;
196
+ let moveY = strafe.y;
197
+
198
+ if (!dodgeDirection)
199
+ {
200
+ const deltaX = enemy.position.x - state.position.x;
201
+ const deltaY = enemy.position.y - state.position.y;
202
+ const normalizedDistance = distance > 0 ? distance : 1;
203
+
204
+ if (distance > targetDistance + distanceDeadZone)
205
+ {
206
+ moveX += (deltaX / normalizedDistance) * approachSpeed;
207
+ moveY += (deltaY / normalizedDistance) * approachSpeed;
208
+ }
209
+ else if (distance < targetDistance - distanceDeadZone)
210
+ {
211
+ moveX -= (deltaX / normalizedDistance) * approachSpeed;
212
+ moveY -= (deltaY / normalizedDistance) * approachSpeed;
213
+ }
214
+ }
215
+
216
+ const magnitude = Math.sqrt(moveX * moveX + moveY * moveY);
217
+ if (magnitude > 100)
218
+ {
219
+ moveX = (moveX / magnitude) * 100;
220
+ moveY = (moveY / magnitude) * 100;
221
+ }
222
+
223
+ const move = {x: moveX, y: moveY};
224
+
225
+ // === DEFENSE: Handle channeling ===
226
+ if (state.state === 'channeling')
227
+ {
228
+ if (!urgentThreat || urgentThreat.ticksToImpact > shieldCancelThreshold)
229
+ {
230
+ return {move, cancel: true};
231
+ }
232
+ return {move: {x: 0, y: 0}};
233
+ }
234
+
235
+ // === DEFENSE: Cancel missile cast only for LETHAL incoming damage ===
236
+ // DPS trade: we accepted the hit when we started casting. Only cancel to survive.
237
+ if (
238
+ state.state === 'casting' &&
239
+ state.castingSpell === 'missile' &&
240
+ urgentThreat &&
241
+ urgentThreat.projectile.damage >= state.health
242
+ )
243
+ {
244
+ const remainingCast = (state.castDuration ?? 0) - (state.castProgress ?? 0);
245
+ if (
246
+ urgentThreat.ticksToImpact <=
247
+ remainingCast + GCD_DURATION + SHIELD_CAST_TIME + SHIELD_BUFFER
248
+ )
249
+ {
250
+ return {move, cancel: true};
251
+ }
252
+ }
253
+
254
+ // === BLINK: Dodge missile + reposition ===
255
+ // Blink dodges the incoming missile — no GCD after, so can immediately fire back.
256
+ // Wide window (10-80) prevents idle gaps where budget is too small for a missile
257
+ // but the threat is too far for the old narrow blink window.
258
+ if (
259
+ state.state === 'idle' &&
260
+ urgentThreat &&
261
+ state.blinkCooldown === 0 &&
262
+ urgentThreat.ticksToImpact >= 10 &&
263
+ urgentThreat.ticksToImpact <= blinkWindowMax
264
+ )
265
+ {
266
+ return {
267
+ move: {x: 0, y: 0},
268
+ startCast: {spell: 'blink', target: getBlinkToCenter(state.position)},
269
+ };
270
+ }
271
+
272
+ // === DEFENSE: Shield undodgeable or high-damage homing threats (when blink on cooldown) ===
273
+ if (
274
+ state.state === 'idle' &&
275
+ urgentThreat &&
276
+ (!urgentThreat.bestDodgeDirection || shouldForceShield(urgentThreat))
277
+ )
278
+ {
279
+ if (urgentThreat.canBlockInTime)
280
+ {
281
+ if (urgentThreat.ticksToStartShield <= SHIELD_BUFFER)
282
+ {
283
+ return {
284
+ move: {x: 0, y: 0},
285
+ startCast: {spell: 'shield'},
286
+ };
287
+ }
288
+ }
289
+ }
290
+
291
+ // === OFFENSE: Adaptive homing missiles ===
292
+ // Skip offense when a lethal missile could reach us during cast+GCD — dodge at full speed instead
293
+ const myProjectileIds = new Set(state.myProjectiles.map((p) => p.id));
294
+ const hasNearbyLethalMissile = state.projectiles.some(
295
+ (p) =>
296
+ !myProjectileIds.has(p.id) &&
297
+ p.damage >= state.health &&
298
+ distanceTo(state.position, p.position) / p.speed <
299
+ MAX_CAST_BUDGET + GCD_DURATION,
300
+ );
301
+ if (state.state === 'idle' && !hasNearbyLethalMissile)
302
+ {
303
+ let nextThreatTime = urgentThreat?.ticksToImpact ?? Infinity;
304
+
305
+ // Account for enemy's active cast — their missile will arrive after cast completes + flight time
306
+ if (enemy.state === 'casting' && enemy.castingSpell === 'missile')
307
+ {
308
+ const remainingEnemyCast =
309
+ (enemy.castDuration ?? 0) - (enemy.castProgress ?? 0);
310
+ const estimatedFlightTime = distance / flightTimeDivisor;
311
+ nextThreatTime = Math.min(
312
+ nextThreatTime,
313
+ remainingEnemyCast + estimatedFlightTime,
314
+ );
315
+ }
316
+
317
+ const safeBudget =
318
+ nextThreatTime - GCD_DURATION - SHIELD_CAST_TIME - SHIELD_BUFFER;
319
+ const budgetTicks = Math.min(safeBudget, MAX_CAST_BUDGET);
320
+
321
+ // Pyromancer wants homing missiles — no maxDamage cap so missiles punch through shields
322
+ const enemyHP = Math.ceil(enemy.health);
323
+ const minDmg = Math.min(MIN_USEFUL_DAMAGE, enemyHP);
324
+ const config = fitMissileToBudget(budgetTicks, distance, {
325
+ minTurnRate,
326
+ lastMissileConfig: state.lastMissileConfig,
327
+ });
328
+
329
+ if (
330
+ config &&
331
+ config.damage >= minDmg &&
332
+ isSafeToAttack(threats, safeAttackCastEstimate)
333
+ )
334
+ {
335
+ return {
336
+ move,
337
+ startCast: {
338
+ spell: 'missile',
339
+ config,
340
+ missileAI: HomingAI,
341
+ direction: angleTo(state.position, enemy.position),
342
+ },
343
+ };
344
+ }
345
+
346
+ // Budget too small for useful missile — wait for threat to pass, then fire
347
+ }
348
+
349
+ return {move};
350
+ };