Skip auto-retaliate during click orders; fix ghost mobs and ground coins.
Players mid walk/gather no longer engage or lose their order when hit. Incomplete entity-move payloads no longer spawn nameless humanoid stubs. Ground coins use the same quantity-tier sprites as inventory.
@@ -4,7 +4,7 @@ import { makeSnapshotLightOcclusion } from "/shared/lighting/tilePassable.js";
4
4
import { bakeChunkOcclusion } from "/shared/lighting/chunkOcclusion.js";
5
5
import { smoothLitCorner } from "/shared/lighting/lightCorners.js";
6
6
import { bloomCompanionName, spriteImageName } from "/shared/definitions/tileSprites.js";
7
import { EQUIPMENT_SLOT, itemEquipSprite, itemSpriteName, itemDefinition } from "/shared/definitions/items.js";
7
import { EQUIPMENT_SLOT, itemEquipSprite, itemSpriteName, itemSpriteNameForStack, itemDefinition } from "/shared/definitions/items.js";
8
8
import
9
9
{
10
10
resolveItemTint,
@@ -757,11 +757,10 @@ export class WorldView
757
757
moveEntity (payload)
758
758
{
759
759
const existing = this.entities.get(payload.entityId);
760
// Move payloads lack appearance/mobTypeId — never invent a stub entity
761
// (that looked like a nameless humanoid "target"). Wait for entity-spawn.
760
762
if (!existing)
761
{
762
this.#upsertEntity(payload);
763
return;
764
}
763
{ return; }
765
764
const size = this.tileSize();
766
765
existing.fromX = existing.visualX;
767
766
existing.fromY = existing.visualY;
@@ -1255,7 +1254,11 @@ export class WorldView
1255
1254
const potion = payload.instance?.kind === "potion" ? payload.instance : null;
1256
1255
const spriteName = potion
1257
1256
? spriteImageName("item", "potion", `potion-${Math.max(1, Math.min(5, potion.fillLevel | 0 || 4))}`)
1258
: (payload.sprite ?? itemSpriteName(payload.itemId, payload.instance));
1257
: (payload.sprite ?? itemSpriteNameForStack({
1258
itemId: payload.itemId,
1259
instance: payload.instance,
1260
quantity: payload.quantity
1261
}));
1259
1262
let item = this.groundItems.get(payload.groundItemId);
1260
1263
if (!item)
1261
1264
{
@@ -4,7 +4,7 @@ import { makeSnapshotLightOcclusion } from "/shared/lighting/tilePassable.js";4
import { bakeChunkOcclusion } from "/shared/lighting/chunkOcclusion.js";4
import { bakeChunkOcclusion } from "/shared/lighting/chunkOcclusion.js";5
import { smoothLitCorner } from "/shared/lighting/lightCorners.js";5
import { smoothLitCorner } from "/shared/lighting/lightCorners.js";6
import { bloomCompanionName, spriteImageName } from "/shared/definitions/tileSprites.js";6
import { bloomCompanionName, spriteImageName } from "/shared/definitions/tileSprites.js";7
import { EQUIPMENT_SLOT, itemEquipSprite, itemSpriteName, itemDefinition } from "/shared/definitions/items.js";7
import { EQUIPMENT_SLOT, itemEquipSprite, itemSpriteName, itemSpriteNameForStack, itemDefinition } from "/shared/definitions/items.js";8
import8
import9
{9
{10
resolveItemTint,10
resolveItemTint,@@ -757,11 +757,10 @@ export class WorldView757
moveEntity (payload)757
moveEntity (payload)758
{758
{759
const existing = this.entities.get(payload.entityId);759
const existing = this.entities.get(payload.entityId);760
// Move payloads lack appearance/mobTypeId — never invent a stub entity761
// (that looked like a nameless humanoid "target"). Wait for entity-spawn.760
if (!existing)762
if (!existing)761
{763
{ return; }762
this.#upsertEntity(payload);763
return;764
}765
const size = this.tileSize();764
const size = this.tileSize();766
existing.fromX = existing.visualX;765
existing.fromX = existing.visualX;767
existing.fromY = existing.visualY;766
existing.fromY = existing.visualY;@@ -1255,7 +1254,11 @@ export class WorldView1255
const potion = payload.instance?.kind === "potion" ? payload.instance : null;1254
const potion = payload.instance?.kind === "potion" ? payload.instance : null;1256
const spriteName = potion1255
const spriteName = potion1257
? spriteImageName("item", "potion", `potion-${Math.max(1, Math.min(5, potion.fillLevel | 0 || 4))}`)1256
? spriteImageName("item", "potion", `potion-${Math.max(1, Math.min(5, potion.fillLevel | 0 || 4))}`)1258
: (payload.sprite ?? itemSpriteName(payload.itemId, payload.instance));1257
: (payload.sprite ?? itemSpriteNameForStack({1258
itemId: payload.itemId,1259
instance: payload.instance,1260
quantity: payload.quantity1261
}));1259
let item = this.groundItems.get(payload.groundItemId);1262
let item = this.groundItems.get(payload.groundItemId);1260
if (!item)1263
if (!item)1261
{1264
{@@ -208,6 +208,20 @@ export function silenceDeadPlayer (ecs, handle)
208
208
}
209
209
}
210
210
211
/**
212
* Player is mid click-order (walk / harvest) — do not auto-retaliate or
213
* cancel that order when a mob swings.
214
*/
215
export function playerBusyWithClickOrder (ecs, handle)
216
{
217
if (handle == null)
218
{ return false; }
219
if (ecs.has(handle, Component.gather))
220
{ return true; }
221
const path = ecs.get(handle, Component.path);
222
return !!(path?.tiles?.length);
223
}
224
211
225
export function engageTarget (ecs, attackerId, targetId, now = Date.now())
212
226
{
213
227
const handle = ecs.handleOf(attackerId);
@@ -1851,7 +1865,7 @@ function applyLocalMeleeHit (
1851
1865
if (attackerIsPlayer === false)
1852
1866
{
1853
1867
const targetCharacter = world.ecs.get(targetHandle, Component.character);
1854
if (targetCharacter)
1868
if (targetCharacter && !playerBusyWithClickOrder(world.ecs, targetHandle))
1855
1869
{
1856
1870
engageTarget(world.ecs, targetId, attackerId, now);
1857
1871
const follow = world.ecs.get(targetHandle, Component.follow);
@@ -2170,7 +2184,7 @@ export function applyDamageAndKill (world, attackerId, targetId, damage, now = D
2170
2184
if (attackerHandle != null && world.ecs.get(targetHandle, Component.character))
2171
2185
{
2172
2186
const attackerCreature = world.ecs.get(attackerHandle, Component.creature);
2173
if (attackerCreature)
2187
if (attackerCreature && !playerBusyWithClickOrder(world.ecs, targetHandle))
2174
2188
{
2175
2189
engageTarget(world.ecs, targetId, attackerId, now);
2176
2190
}
@@ -208,6 +208,20 @@ export function silenceDeadPlayer (ecs, handle)208
}208
}209
}209
}210
210
211
/**212
* Player is mid click-order (walk / harvest) — do not auto-retaliate or213
* cancel that order when a mob swings.214
*/215
export function playerBusyWithClickOrder (ecs, handle)216
{217
if (handle == null)218
{ return false; }219
if (ecs.has(handle, Component.gather))220
{ return true; }221
const path = ecs.get(handle, Component.path);222
return !!(path?.tiles?.length);223
}224
211
export function engageTarget (ecs, attackerId, targetId, now = Date.now())225
export function engageTarget (ecs, attackerId, targetId, now = Date.now())212
{226
{213
const handle = ecs.handleOf(attackerId);227
const handle = ecs.handleOf(attackerId);@@ -1851,7 +1865,7 @@ function applyLocalMeleeHit (1851
if (attackerIsPlayer === false)1865
if (attackerIsPlayer === false)1852
{1866
{1853
const targetCharacter = world.ecs.get(targetHandle, Component.character);1867
const targetCharacter = world.ecs.get(targetHandle, Component.character);1854
if (targetCharacter)1868
if (targetCharacter && !playerBusyWithClickOrder(world.ecs, targetHandle))1855
{1869
{1856
engageTarget(world.ecs, targetId, attackerId, now);1870
engageTarget(world.ecs, targetId, attackerId, now);1857
const follow = world.ecs.get(targetHandle, Component.follow);1871
const follow = world.ecs.get(targetHandle, Component.follow);@@ -2170,7 +2184,7 @@ export function applyDamageAndKill (world, attackerId, targetId, damage, now = D2170
if (attackerHandle != null && world.ecs.get(targetHandle, Component.character))2184
if (attackerHandle != null && world.ecs.get(targetHandle, Component.character))2171
{2185
{2172
const attackerCreature = world.ecs.get(attackerHandle, Component.creature);2186
const attackerCreature = world.ecs.get(attackerHandle, Component.creature);2173
if (attackerCreature)2187
if (attackerCreature && !playerBusyWithClickOrder(world.ecs, targetHandle))2174
{2188
{2175
engageTarget(world.ecs, targetId, attackerId, now);2189
engageTarget(world.ecs, targetId, attackerId, now);2176
}2190
}@@ -4,7 +4,7 @@ import { canEquipItem } from "../../../shared/equipment/stats.js";
4
4
import { EquipmentKind } from "../../../shared/definitions/equipmentKinds.js";
5
5
import { stackRulesFor } from "../../../shared/items/stackRules.js";
6
6
import { BlockType } from "../../../shared/definitions/tiles.js";
7
import { ItemId, itemDefinition, itemSpriteName, itemDisplayName, LETTER_MAX_BODY_CHARS, BLANK_CHEQUE_CARRY_CAP } from "../../../shared/definitions/items.js";
7
import { ItemId, itemDefinition, itemSpriteNameForStack, itemDisplayName, LETTER_MAX_BODY_CHARS, BLANK_CHEQUE_CARRY_CAP } from "../../../shared/definitions/items.js";
8
8
import
9
9
{
10
10
emptyEquipment,
@@ -57,7 +57,7 @@ export function publicGroundItem (item)
57
57
itemId: item.stack.itemId,
58
58
quantity: item.stack.quantity,
59
59
displayName: itemDisplayName(item.stack),
60
sprite: itemSpriteName(item.stack.itemId, instance),
60
sprite: itemSpriteNameForStack(item.stack),
61
61
instance: instance && (instance.kind === "potion" || instance.kind === "letter" || instance.kind === "recipe-paper")
62
62
? instance
63
63
: null,
@@ -4,7 +4,7 @@ import { canEquipItem } from "../../../shared/equipment/stats.js";4
import { EquipmentKind } from "../../../shared/definitions/equipmentKinds.js";4
import { EquipmentKind } from "../../../shared/definitions/equipmentKinds.js";5
import { stackRulesFor } from "../../../shared/items/stackRules.js";5
import { stackRulesFor } from "../../../shared/items/stackRules.js";6
import { BlockType } from "../../../shared/definitions/tiles.js";6
import { BlockType } from "../../../shared/definitions/tiles.js";7
import { ItemId, itemDefinition, itemSpriteName, itemDisplayName, LETTER_MAX_BODY_CHARS, BLANK_CHEQUE_CARRY_CAP } from "../../../shared/definitions/items.js";7
import { ItemId, itemDefinition, itemSpriteNameForStack, itemDisplayName, LETTER_MAX_BODY_CHARS, BLANK_CHEQUE_CARRY_CAP } from "../../../shared/definitions/items.js";8
import8
import9
{9
{10
emptyEquipment,10
emptyEquipment,@@ -57,7 +57,7 @@ export function publicGroundItem (item)57
itemId: item.stack.itemId,57
itemId: item.stack.itemId,58
quantity: item.stack.quantity,58
quantity: item.stack.quantity,59
displayName: itemDisplayName(item.stack),59
displayName: itemDisplayName(item.stack),60
sprite: itemSpriteName(item.stack.itemId, instance),60
sprite: itemSpriteNameForStack(item.stack),61
instance: instance && (instance.kind === "potion" || instance.kind === "letter" || instance.kind === "recipe-paper")61
instance: instance && (instance.kind === "potion" || instance.kind === "letter" || instance.kind === "recipe-paper")62
? instance62
? instance63
: null,63
: null,@@ -1,7 +1,7 @@
1
1
import test from "node:test";
2
2
import assert from "node:assert/strict";
3
3
import { formatQuantity } from "../../shared/items/formatQuantity.js";
4
import { ItemId, coinSpriteTierForQuantity, COIN_MAX_STACK } from "../../shared/definitions/items.js";
4
import { ItemId, coinSpriteTierForQuantity, COIN_MAX_STACK, itemSpriteName, itemSpriteNameForStack } from "../../shared/definitions/items.js";
5
5
import { addCoinsToSlots, emptySlots, countCoinsInSlots } from "../../shared/items/inventory.js";
6
6
7
7
test("formatQuantity uses compact notation", () =>
@@ -39,3 +39,11 @@ test("coin sprite tier picks the largest denomination not exceeding stack size",
39
39
assert.equal(coinSpriteTierForQuantity(999), 500);
40
40
assert.equal(coinSpriteTierForQuantity(50_000_000), 50_000_000);
41
41
});
42
43
test("coin ground/inventory sprites use quantity tiers", () =>
44
{
45
assert.equal(itemSpriteNameForStack({ itemId: ItemId.coins, quantity: 1 }), "item:coins:coins-1");
46
assert.equal(itemSpriteNameForStack({ itemId: ItemId.coins, quantity: 500 }), "item:coins:coins-500");
47
assert.equal(itemSpriteNameForStack({ itemId: ItemId.coins, quantity: 999 }), "item:coins:coins-500");
48
assert.equal(itemSpriteName(ItemId.coins, null, 1500), "item:coins:coins-1500");
49
});
@@ -1,7 +1,7 @@1
import test from "node:test";1
import test from "node:test";2
import assert from "node:assert/strict";2
import assert from "node:assert/strict";3
import { formatQuantity } from "../../shared/items/formatQuantity.js";3
import { formatQuantity } from "../../shared/items/formatQuantity.js";4
import { ItemId, coinSpriteTierForQuantity, COIN_MAX_STACK } from "../../shared/definitions/items.js";4
import { ItemId, coinSpriteTierForQuantity, COIN_MAX_STACK, itemSpriteName, itemSpriteNameForStack } from "../../shared/definitions/items.js";5
import { addCoinsToSlots, emptySlots, countCoinsInSlots } from "../../shared/items/inventory.js";5
import { addCoinsToSlots, emptySlots, countCoinsInSlots } from "../../shared/items/inventory.js";6
6
7
test("formatQuantity uses compact notation", () =>7
test("formatQuantity uses compact notation", () =>@@ -39,3 +39,11 @@ test("coin sprite tier picks the largest denomination not exceeding stack size",39
assert.equal(coinSpriteTierForQuantity(999), 500);39
assert.equal(coinSpriteTierForQuantity(999), 500);40
assert.equal(coinSpriteTierForQuantity(50_000_000), 50_000_000);40
assert.equal(coinSpriteTierForQuantity(50_000_000), 50_000_000);41
});41
});42
43
test("coin ground/inventory sprites use quantity tiers", () =>44
{45
assert.equal(itemSpriteNameForStack({ itemId: ItemId.coins, quantity: 1 }), "item:coins:coins-1");46
assert.equal(itemSpriteNameForStack({ itemId: ItemId.coins, quantity: 500 }), "item:coins:coins-500");47
assert.equal(itemSpriteNameForStack({ itemId: ItemId.coins, quantity: 999 }), "item:coins:coins-500");48
assert.equal(itemSpriteName(ItemId.coins, null, 1500), "item:coins:coins-1500");49
});@@ -681,6 +681,105 @@ test("players retaliate when a mob swings at them", () =>
681
681
{ harness.close(); }
682
682
});
683
683
684
test("players do not auto-retaliate while walking from a click order", () =>
685
{
686
const harness = makeSimulation({ workerCount: 1 });
687
try
688
{
689
const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
690
const playerId = harness.simulation.spawnCharacter(1, character);
691
const ratId = harness.simulation.spawnEntity(mobComponents({
692
tileX: 9,
693
tileY: 8,
694
mobTypeId: MobId.rat,
695
spawnerId: null
696
}));
697
698
harness.simulation.enqueue(1, {
699
type: "move-to",
700
characterId: character.character_id,
701
tileX: 8,
702
tileY: 14
703
});
704
harness.simulation.step();
705
const path = componentsOf(harness.simulation, playerId).path;
706
assert.ok((path?.tiles?.length ?? 0) > 0, "player should be pathing");
707
708
let swung = false;
709
for (let i = 0; i < 80; i++)
710
{
711
harness.advance(50);
712
const events = harness.simulation.step().events;
713
if (events.some((event) =>
714
event.type === "combat-swing" && event.attackerId === ratId && event.targetId === playerId
715
))
716
{
717
swung = true;
718
break;
719
}
720
}
721
assert.ok(swung, "expected the rat to swing at the walking player");
722
assert.equal(
723
componentsOf(harness.simulation, playerId).combat?.targetEntityId ?? null,
724
null,
725
"walking player should not auto-retaliate"
726
);
727
assert.ok(
728
(componentsOf(harness.simulation, playerId).path?.tiles?.length ?? 0) > 0
729
|| componentsOf(harness.simulation, playerId).position.tileY !== 8,
730
"walk order should continue after the hit"
731
);
732
}
733
finally
734
{ harness.close(); }
735
});
736
737
test("players do not auto-retaliate while gathering", () =>
738
{
739
const harness = makeSimulation({ workerCount: 1 });
740
const restore = installDamagingCombatRng();
741
try
742
{
743
const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
744
const playerId = harness.simulation.spawnCharacter(1, character);
745
const ratId = harness.simulation.spawnEntity(mobComponents({
746
tileX: 9,
747
tileY: 8,
748
mobTypeId: MobId.rat,
749
spawnerId: null
750
}));
751
const worker = workerOf(harness.simulation, playerId);
752
const handle = worker.world.ecs.handleOf(playerId);
753
worker.world.ecs.add(handle, Component.gather, {
754
kind: "mining",
755
tileX: 7,
756
tileY: 8,
757
materialId: "copper",
758
durationMs: 60_000,
759
startedAt: Date.now(),
760
finishesAt: Date.now() + 60_000,
761
nextSwingAt: Date.now() + 60_000
762
});
763
const profile = attackProfileOf(worker.world.ecs, worker.world.ecs.handleOf(ratId));
764
const events = resolveMeleeSwing(worker.world, ratId, playerId, profile, Date.now());
765
assert.ok(events.some((event) => event.type === "combat-hit" || event.type === "combat-miss" || event.type === "combat-block"));
766
assert.equal(
767
componentsOf(harness.simulation, playerId).combat?.targetEntityId ?? null,
768
null,
769
"gathering player should not auto-retaliate"
770
);
771
assert.ok(
772
worker.world.ecs.has(handle, Component.gather),
773
"gather session should survive the hit"
774
);
775
}
776
finally
777
{
778
restore();
779
harness.close();
780
}
781
});
782
684
783
test("aggressive rats do not aggro through walls or beyond range", () =>
685
784
{
686
785
const harness = makeSimulation({ workerCount: 1 });
@@ -681,6 +681,105 @@ test("players retaliate when a mob swings at them", () =>681
{ harness.close(); }681
{ harness.close(); }682
});682
});683
683
684
test("players do not auto-retaliate while walking from a click order", () =>685
{686
const harness = makeSimulation({ workerCount: 1 });687
try688
{689
const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });690
const playerId = harness.simulation.spawnCharacter(1, character);691
const ratId = harness.simulation.spawnEntity(mobComponents({692
tileX: 9,693
tileY: 8,694
mobTypeId: MobId.rat,695
spawnerId: null696
}));697
698
harness.simulation.enqueue(1, {699
type: "move-to",700
characterId: character.character_id,701
tileX: 8,702
tileY: 14703
});704
harness.simulation.step();705
const path = componentsOf(harness.simulation, playerId).path;706
assert.ok((path?.tiles?.length ?? 0) > 0, "player should be pathing");707
708
let swung = false;709
for (let i = 0; i < 80; i++)710
{711
harness.advance(50);712
const events = harness.simulation.step().events;713
if (events.some((event) =>714
event.type === "combat-swing" && event.attackerId === ratId && event.targetId === playerId715
))716
{717
swung = true;718
break;719
}720
}721
assert.ok(swung, "expected the rat to swing at the walking player");722
assert.equal(723
componentsOf(harness.simulation, playerId).combat?.targetEntityId ?? null,724
null,725
"walking player should not auto-retaliate"726
);727
assert.ok(728
(componentsOf(harness.simulation, playerId).path?.tiles?.length ?? 0) > 0729
|| componentsOf(harness.simulation, playerId).position.tileY !== 8,730
"walk order should continue after the hit"731
);732
}733
finally734
{ harness.close(); }735
});736
737
test("players do not auto-retaliate while gathering", () =>738
{739
const harness = makeSimulation({ workerCount: 1 });740
const restore = installDamagingCombatRng();741
try742
{743
const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });744
const playerId = harness.simulation.spawnCharacter(1, character);745
const ratId = harness.simulation.spawnEntity(mobComponents({746
tileX: 9,747
tileY: 8,748
mobTypeId: MobId.rat,749
spawnerId: null750
}));751
const worker = workerOf(harness.simulation, playerId);752
const handle = worker.world.ecs.handleOf(playerId);753
worker.world.ecs.add(handle, Component.gather, {754
kind: "mining",755
tileX: 7,756
tileY: 8,757
materialId: "copper",758
durationMs: 60_000,759
startedAt: Date.now(),760
finishesAt: Date.now() + 60_000,761
nextSwingAt: Date.now() + 60_000762
});763
const profile = attackProfileOf(worker.world.ecs, worker.world.ecs.handleOf(ratId));764
const events = resolveMeleeSwing(worker.world, ratId, playerId, profile, Date.now());765
assert.ok(events.some((event) => event.type === "combat-hit" || event.type === "combat-miss" || event.type === "combat-block"));766
assert.equal(767
componentsOf(harness.simulation, playerId).combat?.targetEntityId ?? null,768
null,769
"gathering player should not auto-retaliate"770
);771
assert.ok(772
worker.world.ecs.has(handle, Component.gather),773
"gather session should survive the hit"774
);775
}776
finally777
{778
restore();779
harness.close();780
}781
});782
684
test("aggressive rats do not aggro through walls or beyond range", () =>783
test("aggressive rats do not aggro through walls or beyond range", () =>685
{784
{686
const harness = makeSimulation({ workerCount: 1 });785
const harness = makeSimulation({ workerCount: 1 });