BlueGit

timfa questward

Have attacking creatures run when closing from more than two tiles.

Mobs and humanoids walk the last two tiles and when standing to swing; players keep their own gait.

timfa Tim Falken · pushed by timfa

Authored 2026-09-03 · 2 months ago

Hash
c7595b13afc82af6a5b9618386110e6a0e7796a7
Parent
093f370f
2 files changed +80 −9
server/src/combat/service.js +48 −8
@@ -65,7 +65,7 @@ import { awardCombatHitXp, awardCombatAvoidanceXp, awardDamageHealthXp, syncPlay
65 65 import { chunkBoundsAround, findPath } from "../navigation/astar.js";
66 66 import { tileToChunk } from "../world/coordinates.js";
67 67 import { findPathToNearestGoal } from "../../../shared/navigation/bfs.js";
68 import { CHUNK_SIZE, VISIBLE_CHUNK_RADIUS } from "../../../shared/protocol/constants.js";
68 import { CHUNK_SIZE, GAIT, VISIBLE_CHUNK_RADIUS } from "../../../shared/protocol/constants.js";
69 69 import { stackQuantity, insertStack } from "../../../shared/items/inventory.js";
70 70 import { addGroundItem, publicGroundItem, bagView } from "../items/bags.js";
71 71 import { resolveItemDualTint } from "../../../shared/render/tint.js";
@@ -92,6 +92,42 @@ export function chebyshev (ax, ay, bx, by)
92 92 return Math.max(Math.abs(ax - bx), Math.abs(ay - by));
93 93 }
94 94
95 /** Creatures run while closing; they walk the last two tiles (and when standing to attack). */
96 export const COMBAT_CHASE_RUN_TILES = 2;
97
98 function creatureCanRun (world, handle)
99 {
100 const stamina = world.ecs.get(handle, Component.stamina);
101 if (!stamina)
102 { return true; }
103 return Number(stamina.current) > 0;
104 }
105
106 function isCombatCreature (world, handle)
107 {
108 return !!world.ecs.get(handle, Component.creature)
109 && !world.ecs.get(handle, Component.character);
110 }
111
112 function syncCreatureChaseGait (world, handle, dist, chasing)
113 {
114 if (!isCombatCreature(world, handle))
115 { return; }
116 const movement = world.ecs.get(handle, Component.movement);
117 if (!movement)
118 { return; }
119 if (chasing && dist > COMBAT_CHASE_RUN_TILES && creatureCanRun(world, handle))
120 { movement.gait = GAIT.run; }
121 else
122 { movement.gait = GAIT.walk; }
123 }
124
125 function dropCombatTarget (world, entity)
126 {
127 entity.combat.targetEntityId = null;
128 syncCreatureChaseGait(world, entity.handle, 0, false);
129 }
130
95 131 export function attackProfileOf (ecs, handle)
96 132 {
97 133 const creature = ecs.get(handle, Component.creature);
@@ -994,7 +1030,7 @@ export function stepCombat (world, now = Date.now())
994 1030 {
995 1031 if (world.ecs.has(entity.handle, Component.dead))
996 1032 {
997 entity.combat.targetEntityId = null;
1033 dropCombatTarget(world, entity);
998 1034 continue;
999 1035 }
1000 1036 if (!world.chunkIsSimulating?.(entity.position.tileX, entity.position.tileY))
@@ -1006,12 +1042,12 @@ export function stepCombat (world, now = Date.now())
1006 1042 const target = resolveCombatTarget(world, targetId);
1007 1043 if (!target)
1008 1044 {
1009 combat.targetEntityId = null;
1045 dropCombatTarget(world, entity);
1010 1046 continue;
1011 1047 }
1012 1048 if (target.health && target.health.current <= 0)
1013 1049 {
1014 combat.targetEntityId = null;
1050 dropCombatTarget(world, entity);
1015 1051 continue;
1016 1052 }
1017 1053
@@ -1043,12 +1079,11 @@ export function stepCombat (world, now = Date.now())
1043 1079
1044 1080 const profile = attackProfileOf(world.ecs, entity.handle);
1045 1081 const targetPos = target.pos;
1046 const isCreature = !!world.ecs.get(entity.handle, Component.creature)
1047 && !world.ecs.get(entity.handle, Component.character);
1082 const isCreature = isCombatCreature(world, entity.handle);
1083 const dist = chebyshev(entity.position.tileX, entity.position.tileY, targetPos.tileX, targetPos.tileY);
1048 1084
1049 1085 if (profile.mode === "ranged")
1050 1086 {
1051 const dist = chebyshev(entity.position.tileX, entity.position.tileY, targetPos.tileX, targetPos.tileY);
1052 1087 const maxRange = profile.rangeTiles | 0 || COMBAT_RANGE_TILES;
1053 1088 const equipment = world.ecs.get(entity.handle, Component.equipment);
1054 1089 const hasArrows = isCreature
@@ -1073,9 +1108,11 @@ export function stepCombat (world, now = Date.now())
1073 1108 sessionId: world.ecs.get(entity.handle, Component.networkIdentity)?.sessionId ?? null
1074 1109 });
1075 1110 }
1111 syncCreatureChaseGait(world, entity.handle, dist, true);
1076 1112 queueRangedApproach(world, entity.entityId, entity.handle, entity.position, targetPos, maxRange);
1077 1113 continue;
1078 1114 }
1115 syncCreatureChaseGait(world, entity.handle, dist, false);
1079 1116 clearCombatApproachPath(world.ecs, entity.handle);
1080 1117 if (now < (combat.nextSwingAt || 0))
1081 1118 { continue; }
@@ -1088,7 +1125,6 @@ export function stepCombat (world, now = Date.now())
1088 1125 if (profile.mode !== "melee")
1089 1126 { continue; }
1090 1127
1091 const dist = chebyshev(entity.position.tileX, entity.position.tileY, targetPos.tileX, targetPos.tileY);
1092 1128 const canSwing = dist === 1
1093 1129 && world.isClearStanding?.(entity.entityId, entity.position.tileX, entity.position.tileY);
1094 1130 const attackSpells = isCreature ? eligibleMobAttackSpells(world.ecs, entity.handle) : [];
@@ -1108,6 +1144,7 @@ export function stepCombat (world, now = Date.now())
1108 1144 );
1109 1145 if (!inSpellRange)
1110 1146 {
1147 syncCreatureChaseGait(world, entity.handle, dist, true);
1111 1148 queueStandInRangeApproach(
1112 1149 world,
1113 1150 entity.entityId,
@@ -1120,6 +1157,7 @@ export function stepCombat (world, now = Date.now())
1120 1157 );
1121 1158 continue;
1122 1159 }
1160 syncCreatureChaseGait(world, entity.handle, dist, false);
1123 1161 clearCombatApproachPath(world.ecs, entity.handle);
1124 1162 if (now < (combat.nextSwingAt || 0))
1125 1163 { continue; }
@@ -1136,9 +1174,11 @@ export function stepCombat (world, now = Date.now())
1136 1174
1137 1175 if (!canSwing)
1138 1176 {
1177 syncCreatureChaseGait(world, entity.handle, dist, true);
1139 1178 queueMeleeApproach(world, entity.entityId, entity.handle, entity.position, targetPos);
1140 1179 continue;
1141 1180 }
1181 syncCreatureChaseGait(world, entity.handle, dist, false);
1142 1182 clearCombatApproachPath(world.ecs, entity.handle);
1143 1183 const attackerHealth = world.ecs.get(entity.handle, Component.health);
1144 1184 if (attackerHealth && attackerHealth.current <= 0)
@@ -65,7 +65,7 @@ import { awardCombatHitXp, awardCombatAvoidanceXp, awardDamageHealthXp, syncPlay
65import { chunkBoundsAround, findPath } from "../navigation/astar.js";
65import { chunkBoundsAround, findPath } from "../navigation/astar.js";
66import { tileToChunk } from "../world/coordinates.js";
66import { tileToChunk } from "../world/coordinates.js";
67import { findPathToNearestGoal } from "../../../shared/navigation/bfs.js";
67import { findPathToNearestGoal } from "../../../shared/navigation/bfs.js";
68import { CHUNK_SIZE, VISIBLE_CHUNK_RADIUS } from "../../../shared/protocol/constants.js";
68import { CHUNK_SIZE, GAIT, VISIBLE_CHUNK_RADIUS } from "../../../shared/protocol/constants.js";
69import { stackQuantity, insertStack } from "../../../shared/items/inventory.js";
69import { stackQuantity, insertStack } from "../../../shared/items/inventory.js";
70import { addGroundItem, publicGroundItem, bagView } from "../items/bags.js";
70import { addGroundItem, publicGroundItem, bagView } from "../items/bags.js";
71import { resolveItemDualTint } from "../../../shared/render/tint.js";
71import { resolveItemDualTint } from "../../../shared/render/tint.js";
@@ -92,6 +92,42 @@ export function chebyshev (ax, ay, bx, by)
92 return Math.max(Math.abs(ax - bx), Math.abs(ay - by));
92 return Math.max(Math.abs(ax - bx), Math.abs(ay - by));
93}
93}
94
94
95/** Creatures run while closing; they walk the last two tiles (and when standing to attack). */
96export const COMBAT_CHASE_RUN_TILES = 2;
97
98function creatureCanRun (world, handle)
99{
100 const stamina = world.ecs.get(handle, Component.stamina);
101 if (!stamina)
102 { return true; }
103 return Number(stamina.current) > 0;
104}
105
106function isCombatCreature (world, handle)
107{
108 return !!world.ecs.get(handle, Component.creature)
109 && !world.ecs.get(handle, Component.character);
110}
111
112function syncCreatureChaseGait (world, handle, dist, chasing)
113{
114 if (!isCombatCreature(world, handle))
115 { return; }
116 const movement = world.ecs.get(handle, Component.movement);
117 if (!movement)
118 { return; }
119 if (chasing && dist > COMBAT_CHASE_RUN_TILES && creatureCanRun(world, handle))
120 { movement.gait = GAIT.run; }
121 else
122 { movement.gait = GAIT.walk; }
123}
124
125function dropCombatTarget (world, entity)
126{
127 entity.combat.targetEntityId = null;
128 syncCreatureChaseGait(world, entity.handle, 0, false);
129}
130
95export function attackProfileOf (ecs, handle)
131export function attackProfileOf (ecs, handle)
96{
132{
97 const creature = ecs.get(handle, Component.creature);
133 const creature = ecs.get(handle, Component.creature);
@@ -994,7 +1030,7 @@ export function stepCombat (world, now = Date.now())
994 {
1030 {
995 if (world.ecs.has(entity.handle, Component.dead))
1031 if (world.ecs.has(entity.handle, Component.dead))
996 {
1032 {
997 entity.combat.targetEntityId = null;
1033 dropCombatTarget(world, entity);
998 continue;
1034 continue;
999 }
1035 }
1000 if (!world.chunkIsSimulating?.(entity.position.tileX, entity.position.tileY))
1036 if (!world.chunkIsSimulating?.(entity.position.tileX, entity.position.tileY))
@@ -1006,12 +1042,12 @@ export function stepCombat (world, now = Date.now())
1006 const target = resolveCombatTarget(world, targetId);
1042 const target = resolveCombatTarget(world, targetId);
1007 if (!target)
1043 if (!target)
1008 {
1044 {
1009 combat.targetEntityId = null;
1045 dropCombatTarget(world, entity);
1010 continue;
1046 continue;
1011 }
1047 }
1012 if (target.health && target.health.current <= 0)
1048 if (target.health && target.health.current <= 0)
1013 {
1049 {
1014 combat.targetEntityId = null;
1050 dropCombatTarget(world, entity);
1015 continue;
1051 continue;
1016 }
1052 }
1017
1053
@@ -1043,12 +1079,11 @@ export function stepCombat (world, now = Date.now())
1043
1079
1044 const profile = attackProfileOf(world.ecs, entity.handle);
1080 const profile = attackProfileOf(world.ecs, entity.handle);
1045 const targetPos = target.pos;
1081 const targetPos = target.pos;
1046 const isCreature = !!world.ecs.get(entity.handle, Component.creature)
1082 const isCreature = isCombatCreature(world, entity.handle);
1047 && !world.ecs.get(entity.handle, Component.character);
1083 const dist = chebyshev(entity.position.tileX, entity.position.tileY, targetPos.tileX, targetPos.tileY);
1048
1084
1049 if (profile.mode === "ranged")
1085 if (profile.mode === "ranged")
1050 {
1086 {
1051 const dist = chebyshev(entity.position.tileX, entity.position.tileY, targetPos.tileX, targetPos.tileY);
1052 const maxRange = profile.rangeTiles | 0 || COMBAT_RANGE_TILES;
1087 const maxRange = profile.rangeTiles | 0 || COMBAT_RANGE_TILES;
1053 const equipment = world.ecs.get(entity.handle, Component.equipment);
1088 const equipment = world.ecs.get(entity.handle, Component.equipment);
1054 const hasArrows = isCreature
1089 const hasArrows = isCreature
@@ -1073,9 +1108,11 @@ export function stepCombat (world, now = Date.now())
1073 sessionId: world.ecs.get(entity.handle, Component.networkIdentity)?.sessionId ?? null
1108 sessionId: world.ecs.get(entity.handle, Component.networkIdentity)?.sessionId ?? null
1074 });
1109 });
1075 }
1110 }
1111 syncCreatureChaseGait(world, entity.handle, dist, true);
1076 queueRangedApproach(world, entity.entityId, entity.handle, entity.position, targetPos, maxRange);
1112 queueRangedApproach(world, entity.entityId, entity.handle, entity.position, targetPos, maxRange);
1077 continue;
1113 continue;
1078 }
1114 }
1115 syncCreatureChaseGait(world, entity.handle, dist, false);
1079 clearCombatApproachPath(world.ecs, entity.handle);
1116 clearCombatApproachPath(world.ecs, entity.handle);
1080 if (now < (combat.nextSwingAt || 0))
1117 if (now < (combat.nextSwingAt || 0))
1081 { continue; }
1118 { continue; }
@@ -1088,7 +1125,6 @@ export function stepCombat (world, now = Date.now())
1088 if (profile.mode !== "melee")
1125 if (profile.mode !== "melee")
1089 { continue; }
1126 { continue; }
1090
1127
1091 const dist = chebyshev(entity.position.tileX, entity.position.tileY, targetPos.tileX, targetPos.tileY);
1092 const canSwing = dist === 1
1128 const canSwing = dist === 1
1093 && world.isClearStanding?.(entity.entityId, entity.position.tileX, entity.position.tileY);
1129 && world.isClearStanding?.(entity.entityId, entity.position.tileX, entity.position.tileY);
1094 const attackSpells = isCreature ? eligibleMobAttackSpells(world.ecs, entity.handle) : [];
1130 const attackSpells = isCreature ? eligibleMobAttackSpells(world.ecs, entity.handle) : [];
@@ -1108,6 +1144,7 @@ export function stepCombat (world, now = Date.now())
1108 );
1144 );
1109 if (!inSpellRange)
1145 if (!inSpellRange)
1110 {
1146 {
1147 syncCreatureChaseGait(world, entity.handle, dist, true);
1111 queueStandInRangeApproach(
1148 queueStandInRangeApproach(
1112 world,
1149 world,
1113 entity.entityId,
1150 entity.entityId,
@@ -1120,6 +1157,7 @@ export function stepCombat (world, now = Date.now())
1120 );
1157 );
1121 continue;
1158 continue;
1122 }
1159 }
1160 syncCreatureChaseGait(world, entity.handle, dist, false);
1123 clearCombatApproachPath(world.ecs, entity.handle);
1161 clearCombatApproachPath(world.ecs, entity.handle);
1124 if (now < (combat.nextSwingAt || 0))
1162 if (now < (combat.nextSwingAt || 0))
1125 { continue; }
1163 { continue; }
@@ -1136,9 +1174,11 @@ export function stepCombat (world, now = Date.now())
1136
1174
1137 if (!canSwing)
1175 if (!canSwing)
1138 {
1176 {
1177 syncCreatureChaseGait(world, entity.handle, dist, true);
1139 queueMeleeApproach(world, entity.entityId, entity.handle, entity.position, targetPos);
1178 queueMeleeApproach(world, entity.entityId, entity.handle, entity.position, targetPos);
1140 continue;
1179 continue;
1141 }
1180 }
1181 syncCreatureChaseGait(world, entity.handle, dist, false);
1142 clearCombatApproachPath(world.ecs, entity.handle);
1182 clearCombatApproachPath(world.ecs, entity.handle);
1143 const attackerHealth = world.ecs.get(entity.handle, Component.health);
1183 const attackerHealth = world.ecs.get(entity.handle, Component.health);
1144 if (attackerHealth && attackerHealth.current <= 0)
1184 if (attackerHealth && attackerHealth.current <= 0)
server/tests/combat.test.js +32 −1
@@ -14,7 +14,8 @@ import { tileToRegion } from "../src/world/coordinates.js";
14 14 import { SkillId } from "../../shared/definitions/skills.js";
15 15 import { SpellId } from "../../shared/definitions/spells.js";
16 16 import { totalXpForLevel } from "../../shared/skills/progression.js";
17 import { resolveMeleeSwing, attackProfileOf, hasCombatLineOfSight, queueMeleeApproach, markSeekCover, engageTarget, applyDamageAndKill, markFleeOnHit, stepCombat, chebyshev } from "../src/combat/service.js";
17 import { GAIT } from "../../shared/protocol/constants.js";
18 import { resolveMeleeSwing, attackProfileOf, hasCombatLineOfSight, queueMeleeApproach, markSeekCover, engageTarget, applyDamageAndKill, markFleeOnHit, stepCombat, chebyshev, COMBAT_CHASE_RUN_TILES } from "../src/combat/service.js";
18 19 import { combatDamageRollLevel } from "../../shared/combat/rolls.js";
19 20 import { SKULL_KILL_BONUS } from "../../shared/combat/skull.js";
20 21 import { makeCharacter, makeSimulation, pinEastSplit, stepTimes } from "./helpers/simulationHarness.js";
@@ -550,6 +551,36 @@ test("aggressive rats engage players within aggro range and LOS", () =>
550 551 { harness.close(); }
551 552 });
552 553
554 test("aggressive creatures run when chasing farther than two tiles", () =>
555 {
556 const harness = makeSimulation({ workerCount: 1 });
557 try
558 {
559 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
560 const playerId = harness.simulation.spawnCharacter(1, character);
561 const ratId = harness.simulation.spawnEntity(mobComponents({
562 tileX: 12,
563 tileY: 8,
564 mobTypeId: MobId.rat,
565 spawnerId: null
566 }));
567
568 harness.simulation.step();
569 assert.equal(componentsOf(harness.simulation, ratId).combat?.targetEntityId, playerId);
570 assert.equal(componentsOf(harness.simulation, ratId).movement.gait, GAIT.run);
571 assert.equal(chebyshev(12, 8, 8, 8) > COMBAT_CHASE_RUN_TILES, true);
572 assert.equal(componentsOf(harness.simulation, playerId).movement.gait, GAIT.walk);
573
574 const worker = workerOf(harness.simulation, ratId);
575 teleportEntity(worker, ratId, 10, 8);
576 harness.simulation.step();
577 assert.equal(chebyshev(10, 8, 8, 8), COMBAT_CHASE_RUN_TILES);
578 assert.equal(componentsOf(harness.simulation, ratId).movement.gait, GAIT.walk);
579 }
580 finally
581 { harness.close(); }
582 });
583
553 584 test("aggressive mobs and town guards aggro each other", () =>
554 585 {
555 586 const harness = makeSimulation({ workerCount: 1 });
@@ -14,7 +14,8 @@ import { tileToRegion } from "../src/world/coordinates.js";
14import { SkillId } from "../../shared/definitions/skills.js";
14import { SkillId } from "../../shared/definitions/skills.js";
15import { SpellId } from "../../shared/definitions/spells.js";
15import { SpellId } from "../../shared/definitions/spells.js";
16import { totalXpForLevel } from "../../shared/skills/progression.js";
16import { totalXpForLevel } from "../../shared/skills/progression.js";
17import { resolveMeleeSwing, attackProfileOf, hasCombatLineOfSight, queueMeleeApproach, markSeekCover, engageTarget, applyDamageAndKill, markFleeOnHit, stepCombat, chebyshev } from "../src/combat/service.js";
17import { GAIT } from "../../shared/protocol/constants.js";
18import { resolveMeleeSwing, attackProfileOf, hasCombatLineOfSight, queueMeleeApproach, markSeekCover, engageTarget, applyDamageAndKill, markFleeOnHit, stepCombat, chebyshev, COMBAT_CHASE_RUN_TILES } from "../src/combat/service.js";
18import { combatDamageRollLevel } from "../../shared/combat/rolls.js";
19import { combatDamageRollLevel } from "../../shared/combat/rolls.js";
19import { SKULL_KILL_BONUS } from "../../shared/combat/skull.js";
20import { SKULL_KILL_BONUS } from "../../shared/combat/skull.js";
20import { makeCharacter, makeSimulation, pinEastSplit, stepTimes } from "./helpers/simulationHarness.js";
21import { makeCharacter, makeSimulation, pinEastSplit, stepTimes } from "./helpers/simulationHarness.js";
@@ -550,6 +551,36 @@ test("aggressive rats engage players within aggro range and LOS", () =>
550 { harness.close(); }
551 { harness.close(); }
551});
552});
552
553
554test("aggressive creatures run when chasing farther than two tiles", () =>
555{
556 const harness = makeSimulation({ workerCount: 1 });
557 try
558 {
559 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
560 const playerId = harness.simulation.spawnCharacter(1, character);
561 const ratId = harness.simulation.spawnEntity(mobComponents({
562 tileX: 12,
563 tileY: 8,
564 mobTypeId: MobId.rat,
565 spawnerId: null
566 }));
567
568 harness.simulation.step();
569 assert.equal(componentsOf(harness.simulation, ratId).combat?.targetEntityId, playerId);
570 assert.equal(componentsOf(harness.simulation, ratId).movement.gait, GAIT.run);
571 assert.equal(chebyshev(12, 8, 8, 8) > COMBAT_CHASE_RUN_TILES, true);
572 assert.equal(componentsOf(harness.simulation, playerId).movement.gait, GAIT.walk);
573
574 const worker = workerOf(harness.simulation, ratId);
575 teleportEntity(worker, ratId, 10, 8);
576 harness.simulation.step();
577 assert.equal(chebyshev(10, 8, 8, 8), COMBAT_CHASE_RUN_TILES);
578 assert.equal(componentsOf(harness.simulation, ratId).movement.gait, GAIT.walk);
579 }
580 finally
581 { harness.close(); }
582});
583
553test("aggressive mobs and town guards aggro each other", () =>
584test("aggressive mobs and town guards aggro each other", () =>
554{
585{
555 const harness = makeSimulation({ workerCount: 1 });
586 const harness = makeSimulation({ workerCount: 1 });