BlueGit

timfa questward

Fairies flee to two tiles from a hit source, then stop.

On a successful hit they path away from the attacker’s tile until Chebyshev distance ≥ 2, then resume combat until the next hit.

timfa Tim Falken · pushed by timfa

Authored 2026-08-31 · 2 months ago

Hash
d81af2d449f8462e2b057532292465d1197d27ac
Parent
700600bf
5 files changed +193 −4
server/src/combat/service.js +127 −2
@@ -117,7 +117,7 @@ export function ensureMobCombat (ecs, entityId)
117 117 const def = mobDefinition(creature.mobTypeId);
118 118 const level = creatureLevel(ecs, handle);
119 119 if (!ecs.get(handle, Component.combat))
120 { ecs.add(handle, Component.combat, { targetEntityId: null, nextSwingAt: 0, seekCover: false }); }
120 { ecs.add(handle, Component.combat, { targetEntityId: null, nextSwingAt: 0, seekCover: false, fleeFrom: null }); }
121 121 if (!ecs.get(handle, Component.skills))
122 122 { ecs.add(handle, Component.skills, { levels: mobSkillLevels(def, level) }); }
123 123 return true;
@@ -209,7 +209,7 @@ export function engageTarget (ecs, attackerId, targetId, now = Date.now())
209 209 { return false; }
210 210 let combat = ecs.get(handle, Component.combat);
211 211 if (!combat)
212 { combat = ecs.add(handle, Component.combat, { targetEntityId: null, nextSwingAt: 0, seekCover: false }); }
212 { combat = ecs.add(handle, Component.combat, { targetEntityId: null, nextSwingAt: 0, seekCover: false, fleeFrom: null }); }
213 213 const targetChanged = combat.targetEntityId !== targetId;
214 214 combat.targetEntityId = targetId;
215 215 if (targetChanged)
@@ -232,6 +232,45 @@ export function markSeekCover (ecs, entityId)
232 232 { combat.seekCover = true; }
233 233 }
234 234
235 /**
236 * Flee-on-hit mobs (fairies) path away from the hit source until far enough,
237 * then stop fleeing until the next successful hit.
238 */
239 export function markFleeOnHit (world, targetId, attackerId)
240 {
241 const ecs = world.ecs;
242 const targetHandle = ecs.handleOf(targetId);
243 if (targetHandle == null)
244 { return; }
245 const creature = ecs.get(targetHandle, Component.creature);
246 if (!creature || ecs.get(targetHandle, Component.character))
247 { return; }
248 const fleeDist = mobDefinition(creature.mobTypeId)?.fleeOnHitTiles | 0;
249 if (fleeDist <= 0)
250 { return; }
251 if (!ensureMobCombat(ecs, targetId))
252 { return; }
253 const combat = ecs.get(targetHandle, Component.combat);
254 const targetPos = ecs.get(targetHandle, Component.position);
255 const attackerHandle = ecs.handleOf(attackerId);
256 const attackerPos = attackerHandle != null ? ecs.get(attackerHandle, Component.position) : null;
257 if (!combat || !targetPos || !attackerPos)
258 { return; }
259 if (chebyshev(targetPos.tileX, targetPos.tileY, attackerPos.tileX, attackerPos.tileY) >= fleeDist)
260 {
261 combat.fleeFrom = null;
262 return;
263 }
264 combat.fleeFrom = {
265 tileX: attackerPos.tileX,
266 tileY: attackerPos.tileY,
267 minDist: fleeDist
268 };
269 const path = ecs.get(targetHandle, Component.path);
270 if (path)
271 { path.tiles = []; }
272 }
273
235 274 /** Pending cast or entity-spell auto-repeat — physical combat must not run. */
236 275 export function isSpellCombatBusy (ecs, handle)
237 276 {
@@ -267,6 +306,7 @@ export function clearEngage (ecs, entityId)
267 306 {
268 307 combat.targetEntityId = null;
269 308 combat.seekCover = false;
309 combat.fleeFrom = null;
270 310 }
271 311 }
272 312
@@ -638,6 +678,28 @@ export function stepCombat (world, now = Date.now())
638 678 if (isSpellCombatBusy(world.ecs, entity.handle))
639 679 { continue; }
640 680
681 if (combat.fleeFrom)
682 {
683 const flee = combat.fleeFrom;
684 const minDist = Math.max(1, flee.minDist | 0 || 2);
685 const fleeDist = chebyshev(
686 entity.position.tileX,
687 entity.position.tileY,
688 flee.tileX,
689 flee.tileY
690 );
691 if (fleeDist >= minDist)
692 {
693 combat.fleeFrom = null;
694 clearCombatApproachPath(world.ecs, entity.handle);
695 }
696 else
697 {
698 queueFleeFromHitSource(world, entity.entityId, entity.handle, entity.position, flee, minDist);
699 continue;
700 }
701 }
702
641 703 const profile = attackProfileOf(world.ecs, entity.handle);
642 704 const targetPos = target.pos;
643 705 const isCreature = !!world.ecs.get(entity.handle, Component.creature)
@@ -991,6 +1053,64 @@ function queueFleeToCover (world, entityId, handle, position, threatPos)
991 1053 { movement.walkCarry = 0; }
992 1054 }
993 1055
1056 /** Path to the nearest standable tile at least `minDist` from the hit source. */
1057 function queueFleeFromHitSource (world, entityId, handle, position, source, minDist)
1058 {
1059 if (world.frozen?.has?.(entityId))
1060 { return; }
1061 let path = world.ecs.get(handle, Component.path);
1062 if (!path)
1063 { path = world.ecs.add(handle, Component.path, { tiles: [] }); }
1064 const aloneHere = world.isClearStanding?.(entityId, position.tileX, position.tileY) ?? true;
1065 if (
1066 aloneHere
1067 && chebyshev(position.tileX, position.tileY, source.tileX, source.tileY) >= minDist
1068 )
1069 {
1070 path.tiles = [];
1071 return;
1072 }
1073 const bounds = chunkBoundsAround(position.tileX, position.tileY, VISIBLE_CHUNK_RADIUS);
1074 const isWalkable = (x, y) => world.walkableFor(entityId, x, y);
1075 const isGoal = (x, y) =>
1076 {
1077 if (chebyshev(x, y, source.tileX, source.tileY) < minDist)
1078 { return false; }
1079 return world.isClearStanding?.(entityId, x, y) ?? world.walkableFor(entityId, x, y);
1080 };
1081 if (isGoal(position.tileX, position.tileY) && aloneHere)
1082 {
1083 path.tiles = [];
1084 return;
1085 }
1086 const dest = path.tiles?.length ? path.tiles[path.tiles.length - 1] : null;
1087 if (
1088 dest
1089 && isGoal(dest.x, dest.y)
1090 && path.tiles.every((step) => isWalkable(step.x, step.y))
1091 )
1092 { return; }
1093 const tiles = findPathToNearestGoal(
1094 position.tileX,
1095 position.tileY,
1096 isGoal,
1097 isWalkable,
1098 bounds.minX,
1099 bounds.minY,
1100 bounds.maxX,
1101 bounds.maxY
1102 );
1103 if (!tiles.length)
1104 {
1105 path.tiles = [];
1106 return;
1107 }
1108 path.tiles = tiles;
1109 const movement = world.ecs.get(handle, Component.movement);
1110 if (movement)
1111 { movement.walkCarry = 0; }
1112 }
1113
994 1114 export function queueMeleeApproach (world, entityId, handle, position, targetPos)
995 1115 {
996 1116 if (world.frozen?.has?.(entityId))
@@ -1717,6 +1837,9 @@ function applyLocalMeleeHit (
1717 1837 }
1718 1838 }
1719 1839
1840 if (attackerHandle != null)
1841 { markFleeOnHit(world, targetId, attackerId); }
1842
1720 1843 if (attackerHandle != null)
1721 1844 {
1722 1845 const attackerCharacter = world.ecs.get(attackerHandle, Component.character);
@@ -1867,6 +1990,8 @@ export function applyDamageAndKill (world, attackerId, targetId, damage, now = D
1867 1990 if (attackerIsPlayer)
1868 1991 { markSeekCover(world.ecs, targetId); }
1869 1992 }
1993 if (attackerHandle != null)
1994 { markFleeOnHit(world, targetId, attackerId); }
1870 1995 if (attackerHandle != null && world.ecs.get(targetHandle, Component.character))
1871 1996 {
1872 1997 const attackerCreature = world.ecs.get(attackerHandle, Component.creature);
@@ -117,7 +117,7 @@ export function ensureMobCombat (ecs, entityId)
117 const def = mobDefinition(creature.mobTypeId);
117 const def = mobDefinition(creature.mobTypeId);
118 const level = creatureLevel(ecs, handle);
118 const level = creatureLevel(ecs, handle);
119 if (!ecs.get(handle, Component.combat))
119 if (!ecs.get(handle, Component.combat))
120 { ecs.add(handle, Component.combat, { targetEntityId: null, nextSwingAt: 0, seekCover: false }); }
120 { ecs.add(handle, Component.combat, { targetEntityId: null, nextSwingAt: 0, seekCover: false, fleeFrom: null }); }
121 if (!ecs.get(handle, Component.skills))
121 if (!ecs.get(handle, Component.skills))
122 { ecs.add(handle, Component.skills, { levels: mobSkillLevels(def, level) }); }
122 { ecs.add(handle, Component.skills, { levels: mobSkillLevels(def, level) }); }
123 return true;
123 return true;
@@ -209,7 +209,7 @@ export function engageTarget (ecs, attackerId, targetId, now = Date.now())
209 { return false; }
209 { return false; }
210 let combat = ecs.get(handle, Component.combat);
210 let combat = ecs.get(handle, Component.combat);
211 if (!combat)
211 if (!combat)
212 { combat = ecs.add(handle, Component.combat, { targetEntityId: null, nextSwingAt: 0, seekCover: false }); }
212 { combat = ecs.add(handle, Component.combat, { targetEntityId: null, nextSwingAt: 0, seekCover: false, fleeFrom: null }); }
213 const targetChanged = combat.targetEntityId !== targetId;
213 const targetChanged = combat.targetEntityId !== targetId;
214 combat.targetEntityId = targetId;
214 combat.targetEntityId = targetId;
215 if (targetChanged)
215 if (targetChanged)
@@ -232,6 +232,45 @@ export function markSeekCover (ecs, entityId)
232 { combat.seekCover = true; }
232 { combat.seekCover = true; }
233}
233}
234
234
235/**
236 * Flee-on-hit mobs (fairies) path away from the hit source until far enough,
237 * then stop fleeing until the next successful hit.
238 */
239export function markFleeOnHit (world, targetId, attackerId)
240{
241 const ecs = world.ecs;
242 const targetHandle = ecs.handleOf(targetId);
243 if (targetHandle == null)
244 { return; }
245 const creature = ecs.get(targetHandle, Component.creature);
246 if (!creature || ecs.get(targetHandle, Component.character))
247 { return; }
248 const fleeDist = mobDefinition(creature.mobTypeId)?.fleeOnHitTiles | 0;
249 if (fleeDist <= 0)
250 { return; }
251 if (!ensureMobCombat(ecs, targetId))
252 { return; }
253 const combat = ecs.get(targetHandle, Component.combat);
254 const targetPos = ecs.get(targetHandle, Component.position);
255 const attackerHandle = ecs.handleOf(attackerId);
256 const attackerPos = attackerHandle != null ? ecs.get(attackerHandle, Component.position) : null;
257 if (!combat || !targetPos || !attackerPos)
258 { return; }
259 if (chebyshev(targetPos.tileX, targetPos.tileY, attackerPos.tileX, attackerPos.tileY) >= fleeDist)
260 {
261 combat.fleeFrom = null;
262 return;
263 }
264 combat.fleeFrom = {
265 tileX: attackerPos.tileX,
266 tileY: attackerPos.tileY,
267 minDist: fleeDist
268 };
269 const path = ecs.get(targetHandle, Component.path);
270 if (path)
271 { path.tiles = []; }
272}
273
235/** Pending cast or entity-spell auto-repeat — physical combat must not run. */
274/** Pending cast or entity-spell auto-repeat — physical combat must not run. */
236export function isSpellCombatBusy (ecs, handle)
275export function isSpellCombatBusy (ecs, handle)
237{
276{
@@ -267,6 +306,7 @@ export function clearEngage (ecs, entityId)
267 {
306 {
268 combat.targetEntityId = null;
307 combat.targetEntityId = null;
269 combat.seekCover = false;
308 combat.seekCover = false;
309 combat.fleeFrom = null;
270 }
310 }
271}
311}
272
312
@@ -638,6 +678,28 @@ export function stepCombat (world, now = Date.now())
638 if (isSpellCombatBusy(world.ecs, entity.handle))
678 if (isSpellCombatBusy(world.ecs, entity.handle))
639 { continue; }
679 { continue; }
640
680
681 if (combat.fleeFrom)
682 {
683 const flee = combat.fleeFrom;
684 const minDist = Math.max(1, flee.minDist | 0 || 2);
685 const fleeDist = chebyshev(
686 entity.position.tileX,
687 entity.position.tileY,
688 flee.tileX,
689 flee.tileY
690 );
691 if (fleeDist >= minDist)
692 {
693 combat.fleeFrom = null;
694 clearCombatApproachPath(world.ecs, entity.handle);
695 }
696 else
697 {
698 queueFleeFromHitSource(world, entity.entityId, entity.handle, entity.position, flee, minDist);
699 continue;
700 }
701 }
702
641 const profile = attackProfileOf(world.ecs, entity.handle);
703 const profile = attackProfileOf(world.ecs, entity.handle);
642 const targetPos = target.pos;
704 const targetPos = target.pos;
643 const isCreature = !!world.ecs.get(entity.handle, Component.creature)
705 const isCreature = !!world.ecs.get(entity.handle, Component.creature)
@@ -991,6 +1053,64 @@ function queueFleeToCover (world, entityId, handle, position, threatPos)
991 { movement.walkCarry = 0; }
1053 { movement.walkCarry = 0; }
992}
1054}
993
1055
1056/** Path to the nearest standable tile at least `minDist` from the hit source. */
1057function queueFleeFromHitSource (world, entityId, handle, position, source, minDist)
1058{
1059 if (world.frozen?.has?.(entityId))
1060 { return; }
1061 let path = world.ecs.get(handle, Component.path);
1062 if (!path)
1063 { path = world.ecs.add(handle, Component.path, { tiles: [] }); }
1064 const aloneHere = world.isClearStanding?.(entityId, position.tileX, position.tileY) ?? true;
1065 if (
1066 aloneHere
1067 && chebyshev(position.tileX, position.tileY, source.tileX, source.tileY) >= minDist
1068 )
1069 {
1070 path.tiles = [];
1071 return;
1072 }
1073 const bounds = chunkBoundsAround(position.tileX, position.tileY, VISIBLE_CHUNK_RADIUS);
1074 const isWalkable = (x, y) => world.walkableFor(entityId, x, y);
1075 const isGoal = (x, y) =>
1076 {
1077 if (chebyshev(x, y, source.tileX, source.tileY) < minDist)
1078 { return false; }
1079 return world.isClearStanding?.(entityId, x, y) ?? world.walkableFor(entityId, x, y);
1080 };
1081 if (isGoal(position.tileX, position.tileY) && aloneHere)
1082 {
1083 path.tiles = [];
1084 return;
1085 }
1086 const dest = path.tiles?.length ? path.tiles[path.tiles.length - 1] : null;
1087 if (
1088 dest
1089 && isGoal(dest.x, dest.y)
1090 && path.tiles.every((step) => isWalkable(step.x, step.y))
1091 )
1092 { return; }
1093 const tiles = findPathToNearestGoal(
1094 position.tileX,
1095 position.tileY,
1096 isGoal,
1097 isWalkable,
1098 bounds.minX,
1099 bounds.minY,
1100 bounds.maxX,
1101 bounds.maxY
1102 );
1103 if (!tiles.length)
1104 {
1105 path.tiles = [];
1106 return;
1107 }
1108 path.tiles = tiles;
1109 const movement = world.ecs.get(handle, Component.movement);
1110 if (movement)
1111 { movement.walkCarry = 0; }
1112}
1113
994export function queueMeleeApproach (world, entityId, handle, position, targetPos)
1114export function queueMeleeApproach (world, entityId, handle, position, targetPos)
995{
1115{
996 if (world.frozen?.has?.(entityId))
1116 if (world.frozen?.has?.(entityId))
@@ -1717,6 +1837,9 @@ function applyLocalMeleeHit (
1717 }
1837 }
1718 }
1838 }
1719
1839
1840 if (attackerHandle != null)
1841 { markFleeOnHit(world, targetId, attackerId); }
1842
1720 if (attackerHandle != null)
1843 if (attackerHandle != null)
1721 {
1844 {
1722 const attackerCharacter = world.ecs.get(attackerHandle, Component.character);
1845 const attackerCharacter = world.ecs.get(attackerHandle, Component.character);
@@ -1867,6 +1990,8 @@ export function applyDamageAndKill (world, attackerId, targetId, damage, now = D
1867 if (attackerIsPlayer)
1990 if (attackerIsPlayer)
1868 { markSeekCover(world.ecs, targetId); }
1991 { markSeekCover(world.ecs, targetId); }
1869 }
1992 }
1993 if (attackerHandle != null)
1994 { markFleeOnHit(world, targetId, attackerId); }
1870 if (attackerHandle != null && world.ecs.get(targetHandle, Component.character))
1995 if (attackerHandle != null && world.ecs.get(targetHandle, Component.character))
1871 {
1996 {
1872 const attackerCreature = world.ecs.get(attackerHandle, Component.creature);
1997 const attackerCreature = world.ecs.get(attackerHandle, Component.creature);
server/src/simulation/spawners.js +1 −1
@@ -338,7 +338,7 @@ export function mobComponents (request)
338 338 }
339 339 if (mobCanFight(def))
340 340 {
341 components.combat = { targetEntityId: null, nextSwingAt: 0 };
341 components.combat = { targetEntityId: null, nextSwingAt: 0, seekCover: false, fleeFrom: null };
342 342 components.skills = { levels: mobSkillLevels(def, level) };
343 343 }
344 344
@@ -338,7 +338,7 @@ export function mobComponents (request)
338 }
338 }
339 if (mobCanFight(def))
339 if (mobCanFight(def))
340 {
340 {
341 components.combat = { targetEntityId: null, nextSwingAt: 0 };
341 components.combat = { targetEntityId: null, nextSwingAt: 0, seekCover: false, fleeFrom: null };
342 components.skills = { levels: mobSkillLevels(def, level) };
342 components.skills = { levels: mobSkillLevels(def, level) };
343 }
343 }
344
344
server/tests/combat.test.js +62 −1
@@ -13,7 +13,7 @@ import { WorkerMessage } from "../src/simulation/protocol.js";
13 13 import { tileToRegion } from "../src/world/coordinates.js";
14 14 import { SkillId } from "../../shared/definitions/skills.js";
15 15 import { totalXpForLevel } from "../../shared/skills/progression.js";
16 import { resolveMeleeSwing, attackProfileOf, hasCombatLineOfSight, queueMeleeApproach, markSeekCover, engageTarget, applyDamageAndKill } from "../src/combat/service.js";
16 import { resolveMeleeSwing, attackProfileOf, hasCombatLineOfSight, queueMeleeApproach, markSeekCover, engageTarget, applyDamageAndKill, markFleeOnHit, stepCombat, chebyshev } from "../src/combat/service.js";
17 17 import { makeCharacter, makeSimulation, pinEastSplit, stepTimes } from "./helpers/simulationHarness.js";
18 18
19 19 function workerOf (simulation, entityId)
@@ -1316,3 +1316,64 @@ test("seekCover mob flees to a no-LOS tile when melee approach is impossible", (
1316 1316 finally
1317 1317 { harness.close(); }
1318 1318 });
1319
1320 test("fairy flees to 2 tiles from hit source, then stops until hit again", () =>
1321 {
1322 const harness = makeSimulation({ workerCount: 1 });
1323 try
1324 {
1325 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
1326 const playerId = harness.simulation.spawnCharacter(1, character);
1327 const fairyId = harness.simulation.spawnEntity(mobComponents({
1328 tileX: 9,
1329 tileY: 8,
1330 mobTypeId: MobId.fairy,
1331 spawnerId: null
1332 }));
1333 const worker = workerOf(harness.simulation, fairyId);
1334 const world = worker.world;
1335 const fairyHandle = world.ecs.handleOf(fairyId);
1336
1337 applyDamageAndKill(world, playerId, fairyId, 1, Date.now());
1338 const combat = world.ecs.get(fairyHandle, Component.combat);
1339 assert.equal(combat?.fleeFrom?.tileX, 8);
1340 assert.equal(combat?.fleeFrom?.tileY, 8);
1341 assert.equal(combat?.fleeFrom?.minDist, 2);
1342 assert.equal(combat?.targetEntityId, playerId);
1343
1344 stepCombat(world, Date.now());
1345 const path = world.ecs.get(fairyHandle, Component.path)?.tiles ?? [];
1346 assert.ok(path.length > 0, "fairy should path away while fleeing");
1347 const dest = path[path.length - 1];
1348 assert.ok(
1349 chebyshev(dest.x, dest.y, 8, 8) >= 2,
1350 `flee dest should be ≥2 from hit source, got ${dest.x},${dest.y}`
1351 );
1352
1353 teleportEntity(worker, fairyId, dest.x, dest.y);
1354 world.ecs.get(fairyHandle, Component.path).tiles = [];
1355 stepCombat(world, Date.now());
1356 assert.equal(world.ecs.get(fairyHandle, Component.combat)?.fleeFrom, null);
1357
1358 stepCombat(world, Date.now() + 5000);
1359 assert.equal(
1360 world.ecs.get(fairyHandle, Component.combat)?.fleeFrom,
1361 null,
1362 "no flee until hit again"
1363 );
1364
1365 teleportEntity(worker, fairyId, 9, 8);
1366 applyDamageAndKill(world, playerId, fairyId, 1, Date.now());
1367 assert.equal(world.ecs.get(fairyHandle, Component.combat)?.fleeFrom?.minDist, 2);
1368
1369 teleportEntity(worker, fairyId, 12, 8);
1370 markFleeOnHit(world, fairyId, playerId);
1371 assert.equal(
1372 world.ecs.get(fairyHandle, Component.combat)?.fleeFrom,
1373 null,
1374 "already ≥2 tiles away does not start a flee"
1375 );
1376 }
1377 finally
1378 { harness.close(); }
1379 });
@@ -13,7 +13,7 @@ import { WorkerMessage } from "../src/simulation/protocol.js";
13import { tileToRegion } from "../src/world/coordinates.js";
13import { tileToRegion } from "../src/world/coordinates.js";
14import { SkillId } from "../../shared/definitions/skills.js";
14import { SkillId } from "../../shared/definitions/skills.js";
15import { totalXpForLevel } from "../../shared/skills/progression.js";
15import { totalXpForLevel } from "../../shared/skills/progression.js";
16import { resolveMeleeSwing, attackProfileOf, hasCombatLineOfSight, queueMeleeApproach, markSeekCover, engageTarget, applyDamageAndKill } from "../src/combat/service.js";
16import { resolveMeleeSwing, attackProfileOf, hasCombatLineOfSight, queueMeleeApproach, markSeekCover, engageTarget, applyDamageAndKill, markFleeOnHit, stepCombat, chebyshev } from "../src/combat/service.js";
17import { makeCharacter, makeSimulation, pinEastSplit, stepTimes } from "./helpers/simulationHarness.js";
17import { makeCharacter, makeSimulation, pinEastSplit, stepTimes } from "./helpers/simulationHarness.js";
18
18
19function workerOf (simulation, entityId)
19function workerOf (simulation, entityId)
@@ -1316,3 +1316,64 @@ test("seekCover mob flees to a no-LOS tile when melee approach is impossible", (
1316 finally
1316 finally
1317 { harness.close(); }
1317 { harness.close(); }
1318});
1318});
1319
1320test("fairy flees to 2 tiles from hit source, then stops until hit again", () =>
1321{
1322 const harness = makeSimulation({ workerCount: 1 });
1323 try
1324 {
1325 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
1326 const playerId = harness.simulation.spawnCharacter(1, character);
1327 const fairyId = harness.simulation.spawnEntity(mobComponents({
1328 tileX: 9,
1329 tileY: 8,
1330 mobTypeId: MobId.fairy,
1331 spawnerId: null
1332 }));
1333 const worker = workerOf(harness.simulation, fairyId);
1334 const world = worker.world;
1335 const fairyHandle = world.ecs.handleOf(fairyId);
1336
1337 applyDamageAndKill(world, playerId, fairyId, 1, Date.now());
1338 const combat = world.ecs.get(fairyHandle, Component.combat);
1339 assert.equal(combat?.fleeFrom?.tileX, 8);
1340 assert.equal(combat?.fleeFrom?.tileY, 8);
1341 assert.equal(combat?.fleeFrom?.minDist, 2);
1342 assert.equal(combat?.targetEntityId, playerId);
1343
1344 stepCombat(world, Date.now());
1345 const path = world.ecs.get(fairyHandle, Component.path)?.tiles ?? [];
1346 assert.ok(path.length > 0, "fairy should path away while fleeing");
1347 const dest = path[path.length - 1];
1348 assert.ok(
1349 chebyshev(dest.x, dest.y, 8, 8) >= 2,
1350 `flee dest should be ≥2 from hit source, got ${dest.x},${dest.y}`
1351 );
1352
1353 teleportEntity(worker, fairyId, dest.x, dest.y);
1354 world.ecs.get(fairyHandle, Component.path).tiles = [];
1355 stepCombat(world, Date.now());
1356 assert.equal(world.ecs.get(fairyHandle, Component.combat)?.fleeFrom, null);
1357
1358 stepCombat(world, Date.now() + 5000);
1359 assert.equal(
1360 world.ecs.get(fairyHandle, Component.combat)?.fleeFrom,
1361 null,
1362 "no flee until hit again"
1363 );
1364
1365 teleportEntity(worker, fairyId, 9, 8);
1366 applyDamageAndKill(world, playerId, fairyId, 1, Date.now());
1367 assert.equal(world.ecs.get(fairyHandle, Component.combat)?.fleeFrom?.minDist, 2);
1368
1369 teleportEntity(worker, fairyId, 12, 8);
1370 markFleeOnHit(world, fairyId, playerId);
1371 assert.equal(
1372 world.ecs.get(fairyHandle, Component.combat)?.fleeFrom,
1373 null,
1374 "already ≥2 tiles away does not start a flee"
1375 );
1376 }
1377 finally
1378 { harness.close(); }
1379});
server/tests/shearing.test.js +1 −0
@@ -120,6 +120,7 @@ test("fairy uses own sprites, dual-channel kit, and random-element spell bolts",
120 120 assert.equal(fairy.dualChannelAppearance, true);
121 121 assert.equal(fairy.preferSpellAttack, true);
122 122 assert.equal(fairy.randomElementSpell, true);
123 assert.equal(fairy.fleeOnHitTiles, 2);
123 124 assert.equal(fairy.randomElementMelee, undefined);
124 125 assert.equal(fairy.lightRadiusTiles, 1);
125 126 assert.equal(fairy.frames, 2);
@@ -120,6 +120,7 @@ test("fairy uses own sprites, dual-channel kit, and random-element spell bolts",
120 assert.equal(fairy.dualChannelAppearance, true);
120 assert.equal(fairy.dualChannelAppearance, true);
121 assert.equal(fairy.preferSpellAttack, true);
121 assert.equal(fairy.preferSpellAttack, true);
122 assert.equal(fairy.randomElementSpell, true);
122 assert.equal(fairy.randomElementSpell, true);
123 assert.equal(fairy.fleeOnHitTiles, 2);
123 assert.equal(fairy.randomElementMelee, undefined);
124 assert.equal(fairy.randomElementMelee, undefined);
124 assert.equal(fairy.lightRadiusTiles, 1);
125 assert.equal(fairy.lightRadiusTiles, 1);
125 assert.equal(fairy.frames, 2);
126 assert.equal(fairy.frames, 2);
shared/definitions/mobs.js +2 −0
@@ -311,6 +311,8 @@ export const MOB_DEFINITIONS = Object.freeze({
311 311 preferSpellAttack: true,
312 312 /** Cast a random-element sprite bolt each attack (no fixed spawn element). */
313 313 randomElementSpell: true,
314 /** On hit, flee until at least this many tiles from the hit source (then stop until hit again). */
315 fleeOnHitTiles: 2,
314 316 /** Dimmer than elemental sprites (radius 2). */
315 317 lightRadiusTiles: 1,
316 318 bloomBody: true,
@@ -311,6 +311,8 @@ export const MOB_DEFINITIONS = Object.freeze({
311 preferSpellAttack: true,
311 preferSpellAttack: true,
312 /** Cast a random-element sprite bolt each attack (no fixed spawn element). */
312 /** Cast a random-element sprite bolt each attack (no fixed spawn element). */
313 randomElementSpell: true,
313 randomElementSpell: true,
314 /** On hit, flee until at least this many tiles from the hit source (then stop until hit again). */
315 fleeOnHitTiles: 2,
314 /** Dimmer than elemental sprites (radius 2). */
316 /** Dimmer than elemental sprites (radius 2). */
315 lightRadiusTiles: 1,
317 lightRadiusTiles: 1,
316 bloomBody: true,
318 bloomBody: true,