BlueGit

timfa questward

Merge branch 'feature/talk-over-table'

timfa Tim Falken · pushed by timfa

Authored 2026-09-07 · 2 months ago

Hash
efe620c745a0012ae7bb175c986e55fe20ae044c
6 files changed +293 −22
client/src/play/main.js +102 −9
@@ -5,7 +5,7 @@ import { COMBAT_RANGE_TILES } from "/shared/spells/constants.js";
5 5 import { BlockType, BLOCK_DEFINITIONS, GROUND_DEFINITIONS, normalizeGround, resourceNodeDisplayName, isChopableTreeBlock, isPickablePlantBlock, plantGatherForBlock } from "/shared/definitions/tiles.js";
6 6 import { OVERWORLD_ID } from "/shared/world/dimensions.js";
7 7 import { findPathToNearestGoal } from "/shared/navigation/bfs.js";
8 import { isAdjacentTile } from "/shared/navigation/adjacency.js";
8 import { isAdjacentTile, canTalkOrShopNear } from "/shared/navigation/adjacency.js";
9 9 import { GameSocket, defaultSocketUrl } from "../net/socket.js";
10 10 import { fetchAuthStatus, formatAccountLabel, loginUrl, logoutUrl } from "../net/auth.js";
11 11 import { loadSpriteCatalog, preloadCatalogImages, applySpriteSheets, useNearestNeighbour, playImagesFromCatalog } from "../render/sprites.js";
@@ -478,6 +478,79 @@ function approachAdjacentTile (tileX, tileY)
478 478 return false;
479 479 }
480 480
481 /** Walk to a stance from which talk/shop is allowed (adjacent, or over a table/counter). */
482 function approachTalkTarget (npcTileX, npcTileY)
483 {
484 const local = view.entities.get(view.localEntityId);
485 if (!local)
486 { return false; }
487 const getBlockType = (x, y) => blockTypeAt(view, x, y);
488 if (canTalkOrShopNear(local.tileX, local.tileY, npcTileX, npcTileY, getBlockType))
489 {
490 approachMove = null;
491 return true;
492 }
493
494 const radius = Math.max(1, STATIC_CHUNK_RADIUS | 0) * CHUNK_SIZE;
495 const isStance = (x, y) =>
496 canTalkOrShopNear(x, y, npcTileX, npcTileY, getBlockType) && isClientApproachable(x, y);
497 const path = findPathToNearestGoal(
498 local.tileX,
499 local.tileY,
500 isStance,
501 (x, y) => isClientApproachable(x, y),
502 local.tileX - radius,
503 local.tileY - radius,
504 local.tileX + radius,
505 local.tileY + radius
506 );
507 let best = path.length ? path[path.length - 1] : null;
508 if (!best)
509 {
510 let bestAny = null;
511 let bestAnyDist = Infinity;
512 for (let dy = -2; dy <= 2; dy++)
513 {
514 for (let dx = -2; dx <= 2; dx++)
515 {
516 if (dx === 0 && dy === 0)
517 { continue; }
518 const destX = npcTileX + dx;
519 const destY = npcTileY + dy;
520 if (!isStance(destX, destY))
521 { continue; }
522 const dist = chebyshev(local.tileX, local.tileY, destX, destY);
523 if (dist < bestAnyDist)
524 {
525 bestAnyDist = dist;
526 bestAny = { tileX: destX, tileY: destY };
527 }
528 }
529 }
530 best = bestAny ? { x: bestAny.tileX, y: bestAny.tileY } : null;
531 }
532 if (!best)
533 { return false; }
534 const destX = best.x ?? best.tileX;
535 const destY = best.y ?? best.tileY;
536 if (
537 approachMove
538 && approachMove.focusX === npcTileX
539 && approachMove.focusY === npcTileY
540 && approachMove.destX === destX
541 && approachMove.destY === destY
542 )
543 { return false; }
544 approachMove = {
545 focusX: npcTileX,
546 focusY: npcTileY,
547 destX,
548 destY
549 };
550 sendPlayerMoveTo(destX, destY);
551 return false;
552 }
553
481 554 function requestOpenBank (tileX, tileY, { getCheques = false } = {})
482 555 {
483 556 clearAllPendingActions();
@@ -661,7 +734,7 @@ function requestTalk (targetEntityId, purpose = null)
661 734 clearPendingInteract();
662 735 return;
663 736 }
664 if (approachAdjacentTile(target.tileX, target.tileY))
737 if (approachTalkTarget(target.tileX, target.tileY))
665 738 { flushPendingInteract(); }
666 739 }
667 740
@@ -758,8 +831,22 @@ function closeOpenUiIfTooFar ()
758 831 if (uiFocus.entityId != null)
759 832 {
760 833 const target = view.entities.get(uiFocus.entityId);
761 tooFar = !target
762 || !isAdjacentTile(local.tileX, local.tileY, target.tileX, target.tileY);
834 if (!target)
835 { tooFar = true; }
836 else if (uiFocus.kind === "shop" || uiFocus.kind === "dialogue")
837 {
838 tooFar = !canTalkOrShopNear(
839 local.tileX,
840 local.tileY,
841 target.tileX,
842 target.tileY,
843 (x, y) => blockTypeAt(view, x, y)
844 );
845 }
846 else
847 {
848 tooFar = !isAdjacentTile(local.tileX, local.tileY, target.tileX, target.tileY);
849 }
763 850 }
764 851 else if (uiFocus.tileX != null && uiFocus.tileY != null)
765 852 {
@@ -1018,10 +1105,16 @@ function flushPendingInteract ()
1018 1105 clearPendingInteract();
1019 1106 return;
1020 1107 }
1021 if (!isAdjacentTile(local.tileX, local.tileY, target.tileX, target.tileY))
1022 {
1023 // Target may have moved — keep chasing.
1024 approachAdjacentTile(target.tileX, target.tileY);
1108 if (!canTalkOrShopNear(
1109 local.tileX,
1110 local.tileY,
1111 target.tileX,
1112 target.tileY,
1113 (x, y) => blockTypeAt(view, x, y)
1114 ))
1115 {
1116 // Target may have moved — keep chasing to a talk stance.
1117 approachTalkTarget(target.tileX, target.tileY);
1025 1118 return;
1026 1119 }
1027 1120 socket.send({
@@ -1029,7 +1122,7 @@ function flushPendingInteract ()
1029 1122 targetEntityId: pendingInteract.targetEntityId,
1030 1123 purpose: pendingInteract.purpose || undefined
1031 1124 });
1032 // Shop vs dialogue is decided server-side; both use NPC adjacency.
1125 // Shop vs dialogue is decided server-side; both use talk/shop reach.
1033 1126 setUiFocus({
1034 1127 kind: pendingInteract.purpose === "shop" ? "shop" : "dialogue",
1035 1128 entityId: pendingInteract.targetEntityId
@@ -5,7 +5,7 @@ import { COMBAT_RANGE_TILES } from "/shared/spells/constants.js";
5import { BlockType, BLOCK_DEFINITIONS, GROUND_DEFINITIONS, normalizeGround, resourceNodeDisplayName, isChopableTreeBlock, isPickablePlantBlock, plantGatherForBlock } from "/shared/definitions/tiles.js";
5import { BlockType, BLOCK_DEFINITIONS, GROUND_DEFINITIONS, normalizeGround, resourceNodeDisplayName, isChopableTreeBlock, isPickablePlantBlock, plantGatherForBlock } from "/shared/definitions/tiles.js";
6import { OVERWORLD_ID } from "/shared/world/dimensions.js";
6import { OVERWORLD_ID } from "/shared/world/dimensions.js";
7import { findPathToNearestGoal } from "/shared/navigation/bfs.js";
7import { findPathToNearestGoal } from "/shared/navigation/bfs.js";
8import { isAdjacentTile } from "/shared/navigation/adjacency.js";
8import { isAdjacentTile, canTalkOrShopNear } from "/shared/navigation/adjacency.js";
9import { GameSocket, defaultSocketUrl } from "../net/socket.js";
9import { GameSocket, defaultSocketUrl } from "../net/socket.js";
10import { fetchAuthStatus, formatAccountLabel, loginUrl, logoutUrl } from "../net/auth.js";
10import { fetchAuthStatus, formatAccountLabel, loginUrl, logoutUrl } from "../net/auth.js";
11import { loadSpriteCatalog, preloadCatalogImages, applySpriteSheets, useNearestNeighbour, playImagesFromCatalog } from "../render/sprites.js";
11import { loadSpriteCatalog, preloadCatalogImages, applySpriteSheets, useNearestNeighbour, playImagesFromCatalog } from "../render/sprites.js";
@@ -478,6 +478,79 @@ function approachAdjacentTile (tileX, tileY)
478 return false;
478 return false;
479}
479}
480
480
481/** Walk to a stance from which talk/shop is allowed (adjacent, or over a table/counter). */
482function approachTalkTarget (npcTileX, npcTileY)
483{
484 const local = view.entities.get(view.localEntityId);
485 if (!local)
486 { return false; }
487 const getBlockType = (x, y) => blockTypeAt(view, x, y);
488 if (canTalkOrShopNear(local.tileX, local.tileY, npcTileX, npcTileY, getBlockType))
489 {
490 approachMove = null;
491 return true;
492 }
493
494 const radius = Math.max(1, STATIC_CHUNK_RADIUS | 0) * CHUNK_SIZE;
495 const isStance = (x, y) =>
496 canTalkOrShopNear(x, y, npcTileX, npcTileY, getBlockType) && isClientApproachable(x, y);
497 const path = findPathToNearestGoal(
498 local.tileX,
499 local.tileY,
500 isStance,
501 (x, y) => isClientApproachable(x, y),
502 local.tileX - radius,
503 local.tileY - radius,
504 local.tileX + radius,
505 local.tileY + radius
506 );
507 let best = path.length ? path[path.length - 1] : null;
508 if (!best)
509 {
510 let bestAny = null;
511 let bestAnyDist = Infinity;
512 for (let dy = -2; dy <= 2; dy++)
513 {
514 for (let dx = -2; dx <= 2; dx++)
515 {
516 if (dx === 0 && dy === 0)
517 { continue; }
518 const destX = npcTileX + dx;
519 const destY = npcTileY + dy;
520 if (!isStance(destX, destY))
521 { continue; }
522 const dist = chebyshev(local.tileX, local.tileY, destX, destY);
523 if (dist < bestAnyDist)
524 {
525 bestAnyDist = dist;
526 bestAny = { tileX: destX, tileY: destY };
527 }
528 }
529 }
530 best = bestAny ? { x: bestAny.tileX, y: bestAny.tileY } : null;
531 }
532 if (!best)
533 { return false; }
534 const destX = best.x ?? best.tileX;
535 const destY = best.y ?? best.tileY;
536 if (
537 approachMove
538 && approachMove.focusX === npcTileX
539 && approachMove.focusY === npcTileY
540 && approachMove.destX === destX
541 && approachMove.destY === destY
542 )
543 { return false; }
544 approachMove = {
545 focusX: npcTileX,
546 focusY: npcTileY,
547 destX,
548 destY
549 };
550 sendPlayerMoveTo(destX, destY);
551 return false;
552}
553
481function requestOpenBank (tileX, tileY, { getCheques = false } = {})
554function requestOpenBank (tileX, tileY, { getCheques = false } = {})
482{
555{
483 clearAllPendingActions();
556 clearAllPendingActions();
@@ -661,7 +734,7 @@ function requestTalk (targetEntityId, purpose = null)
661 clearPendingInteract();
734 clearPendingInteract();
662 return;
735 return;
663 }
736 }
664 if (approachAdjacentTile(target.tileX, target.tileY))
737 if (approachTalkTarget(target.tileX, target.tileY))
665 { flushPendingInteract(); }
738 { flushPendingInteract(); }
666}
739}
667
740
@@ -758,8 +831,22 @@ function closeOpenUiIfTooFar ()
758 if (uiFocus.entityId != null)
831 if (uiFocus.entityId != null)
759 {
832 {
760 const target = view.entities.get(uiFocus.entityId);
833 const target = view.entities.get(uiFocus.entityId);
761 tooFar = !target
834 if (!target)
762 || !isAdjacentTile(local.tileX, local.tileY, target.tileX, target.tileY);
835 { tooFar = true; }
836 else if (uiFocus.kind === "shop" || uiFocus.kind === "dialogue")
837 {
838 tooFar = !canTalkOrShopNear(
839 local.tileX,
840 local.tileY,
841 target.tileX,
842 target.tileY,
843 (x, y) => blockTypeAt(view, x, y)
844 );
845 }
846 else
847 {
848 tooFar = !isAdjacentTile(local.tileX, local.tileY, target.tileX, target.tileY);
849 }
763 }
850 }
764 else if (uiFocus.tileX != null && uiFocus.tileY != null)
851 else if (uiFocus.tileX != null && uiFocus.tileY != null)
765 {
852 {
@@ -1018,10 +1105,16 @@ function flushPendingInteract ()
1018 clearPendingInteract();
1105 clearPendingInteract();
1019 return;
1106 return;
1020 }
1107 }
1021 if (!isAdjacentTile(local.tileX, local.tileY, target.tileX, target.tileY))
1108 if (!canTalkOrShopNear(
1022 {
1109 local.tileX,
1023 // Target may have moved — keep chasing.
1110 local.tileY,
1024 approachAdjacentTile(target.tileX, target.tileY);
1111 target.tileX,
1112 target.tileY,
1113 (x, y) => blockTypeAt(view, x, y)
1114 ))
1115 {
1116 // Target may have moved — keep chasing to a talk stance.
1117 approachTalkTarget(target.tileX, target.tileY);
1025 return;
1118 return;
1026 }
1119 }
1027 socket.send({
1120 socket.send({
@@ -1029,7 +1122,7 @@ function flushPendingInteract ()
1029 targetEntityId: pendingInteract.targetEntityId,
1122 targetEntityId: pendingInteract.targetEntityId,
1030 purpose: pendingInteract.purpose || undefined
1123 purpose: pendingInteract.purpose || undefined
1031 });
1124 });
1032 // Shop vs dialogue is decided server-side; both use NPC adjacency.
1125 // Shop vs dialogue is decided server-side; both use talk/shop reach.
1033 setUiFocus({
1126 setUiFocus({
1034 kind: pendingInteract.purpose === "shop" ? "shop" : "dialogue",
1127 kind: pendingInteract.purpose === "shop" ? "shop" : "dialogue",
1035 entityId: pendingInteract.targetEntityId
1128 entityId: pendingInteract.targetEntityId
server/src/shop/service.js +27 −9
@@ -29,7 +29,8 @@ import {
29 29 shopRestockDayKey
30 30 } from "../../../shared/shop/pricing.js";
31 31 import { DEFAULT_WORLD_SETTINGS } from "../../../shared/protocol/constants.js";
32 import { isAdjacentTile } from "../../../shared/navigation/adjacency.js";
32 import { canTalkOrShopNear } from "../../../shared/navigation/adjacency.js";
33 import { OVERWORLD_ID, normalizeDimensionId } from "../../../shared/world/dimensions.js";
33 34 import { objectPronoun } from "../../../shared/speech/pronouns.js";
34 35
35 36 function userError (message)
@@ -39,6 +40,29 @@ function userError (message)
39 40 return error;
40 41 }
41 42
43 function talkOrShopGetBlockTypeAt (world, dimensionId)
44 {
45 const dim = normalizeDimensionId(dimensionId ?? OVERWORLD_ID);
46 return (tileX, tileY) => world.blockTypeAt?.(tileX, tileY, dim) ?? null;
47 }
48
49 function canTalkOrShopWithNpc (world, playerPos, npcPos)
50 {
51 if (!playerPos || !npcPos)
52 { return false; }
53 const playerDim = normalizeDimensionId(playerPos.dimensionId ?? OVERWORLD_ID);
54 const npcDim = normalizeDimensionId(npcPos.dimensionId ?? OVERWORLD_ID);
55 if (playerDim !== npcDim)
56 { return false; }
57 return canTalkOrShopNear(
58 playerPos.tileX,
59 playerPos.tileY,
60 npcPos.tileX,
61 npcPos.tileY,
62 talkOrShopGetBlockTypeAt(world, npcDim)
63 );
64 }
65
42 66 function npcSex (world, npcHandle)
43 67 {
44 68 const appearance = world.ecs.get(npcHandle, Component.appearance);
@@ -114,13 +138,7 @@ export function pruneShopBrowsers (world)
114 138 }
115 139 const playerHandle = world.ecs.handleOf(playerEntityId);
116 140 const playerPos = world.ecs.get(playerHandle, Component.position);
117 const stillNear = playerPos
118 && isAdjacentTile(
119 playerPos.tileX,
120 playerPos.tileY,
121 entity.position.tileX,
122 entity.position.tileY
123 );
141 const stillNear = canTalkOrShopWithNpc(world, playerPos, entity.position);
124 142 if (stillNear)
125 143 { continue; }
126 144 removeShopBrowser(shop, characterId);
@@ -447,7 +465,7 @@ function requireShopAccess (world, sessionId, characterId, npcEntityId)
447 465
448 466 const playerPos = world.ecs.get(handle, Component.position);
449 467 const npcPos = world.ecs.get(npcHandle, Component.position);
450 if (!playerPos || !npcPos || !isAdjacentTile(playerPos.tileX, playerPos.tileY, npcPos.tileX, npcPos.tileY))
468 if (!canTalkOrShopWithNpc(world, playerPos, npcPos))
451 469 { throw userError(`You need to stand next to ${objectPronoun(sex)}.`); }
452 470
453 471 return { handle, npcHandle, shop, entityId };
@@ -29,7 +29,8 @@ import {
29 shopRestockDayKey
29 shopRestockDayKey
30} from "../../../shared/shop/pricing.js";
30} from "../../../shared/shop/pricing.js";
31import { DEFAULT_WORLD_SETTINGS } from "../../../shared/protocol/constants.js";
31import { DEFAULT_WORLD_SETTINGS } from "../../../shared/protocol/constants.js";
32import { isAdjacentTile } from "../../../shared/navigation/adjacency.js";
32import { canTalkOrShopNear } from "../../../shared/navigation/adjacency.js";
33import { OVERWORLD_ID, normalizeDimensionId } from "../../../shared/world/dimensions.js";
33import { objectPronoun } from "../../../shared/speech/pronouns.js";
34import { objectPronoun } from "../../../shared/speech/pronouns.js";
34
35
35function userError (message)
36function userError (message)
@@ -39,6 +40,29 @@ function userError (message)
39 return error;
40 return error;
40}
41}
41
42
43function talkOrShopGetBlockTypeAt (world, dimensionId)
44{
45 const dim = normalizeDimensionId(dimensionId ?? OVERWORLD_ID);
46 return (tileX, tileY) => world.blockTypeAt?.(tileX, tileY, dim) ?? null;
47}
48
49function canTalkOrShopWithNpc (world, playerPos, npcPos)
50{
51 if (!playerPos || !npcPos)
52 { return false; }
53 const playerDim = normalizeDimensionId(playerPos.dimensionId ?? OVERWORLD_ID);
54 const npcDim = normalizeDimensionId(npcPos.dimensionId ?? OVERWORLD_ID);
55 if (playerDim !== npcDim)
56 { return false; }
57 return canTalkOrShopNear(
58 playerPos.tileX,
59 playerPos.tileY,
60 npcPos.tileX,
61 npcPos.tileY,
62 talkOrShopGetBlockTypeAt(world, npcDim)
63 );
64}
65
42function npcSex (world, npcHandle)
66function npcSex (world, npcHandle)
43{
67{
44 const appearance = world.ecs.get(npcHandle, Component.appearance);
68 const appearance = world.ecs.get(npcHandle, Component.appearance);
@@ -114,13 +138,7 @@ export function pruneShopBrowsers (world)
114 }
138 }
115 const playerHandle = world.ecs.handleOf(playerEntityId);
139 const playerHandle = world.ecs.handleOf(playerEntityId);
116 const playerPos = world.ecs.get(playerHandle, Component.position);
140 const playerPos = world.ecs.get(playerHandle, Component.position);
117 const stillNear = playerPos
141 const stillNear = canTalkOrShopWithNpc(world, playerPos, entity.position);
118 && isAdjacentTile(
119 playerPos.tileX,
120 playerPos.tileY,
121 entity.position.tileX,
122 entity.position.tileY
123 );
124 if (stillNear)
142 if (stillNear)
125 { continue; }
143 { continue; }
126 removeShopBrowser(shop, characterId);
144 removeShopBrowser(shop, characterId);
@@ -447,7 +465,7 @@ function requireShopAccess (world, sessionId, characterId, npcEntityId)
447
465
448 const playerPos = world.ecs.get(handle, Component.position);
466 const playerPos = world.ecs.get(handle, Component.position);
449 const npcPos = world.ecs.get(npcHandle, Component.position);
467 const npcPos = world.ecs.get(npcHandle, Component.position);
450 if (!playerPos || !npcPos || !isAdjacentTile(playerPos.tileX, playerPos.tileY, npcPos.tileX, npcPos.tileY))
468 if (!canTalkOrShopWithNpc(world, playerPos, npcPos))
451 { throw userError(`You need to stand next to ${objectPronoun(sex)}.`); }
469 { throw userError(`You need to stand next to ${objectPronoun(sex)}.`); }
452
470
453 return { handle, npcHandle, shop, entityId };
471 return { handle, npcHandle, shop, entityId };
server/src/simulation/workerWorld.js +37 −3
@@ -102,6 +102,8 @@ import {
102 102 PATH_GROUND_STEP_COST,
103 103 SOFT_OCCUPIED_DWELL_MS
104 104 } from "../../../shared/navigation/pathGround.js";
105 import { canTalkOrShopNear } from "../../../shared/navigation/adjacency.js";
106 import { objectPronoun } from "../../../shared/speech/pronouns.js";
105 107 import { withCombatBackstepCost } from "../../../shared/navigation/combatPathCost.js";
106 108 import { GroundType, normalizeGround } from "../../../shared/definitions/tiles.js";
107 109 import { clearFollow, setFollow, stepFollow, recordFollowTrail, consumeFollowTrail } from "./follow.js";
@@ -282,6 +284,14 @@ export class WorkerWorld
282 284 return this.#chunkData(chunkX, chunkY, dimensionId);
283 285 }
284 286
287 blockTypeAt (tileX, tileY, dimensionId = OVERWORLD_ID)
288 {
289 const dim = normalizeDimensionId(dimensionId);
290 const data = this.#chunkData(tileToChunk(tileX), tileToChunk(tileY), dim);
291 const block = data?.blocks?.[tileIndex(tileToLocal(tileX), tileToLocal(tileY))];
292 return block?.typeId ?? null;
293 }
294
285 295 #configure (message)
286 296 {
287 297 if (message.settings)
@@ -1252,6 +1262,27 @@ export class WorkerWorld
1252 1262 error.userFacing = true;
1253 1263 throw error;
1254 1264 }
1265 const playerEntityId = this.characterEntities.get(characterId);
1266 const playerHandle = this.ecs.handleOf(playerEntityId);
1267 const playerPos = playerHandle != null ? this.ecs.get(playerHandle, Component.position) : null;
1268 const npcPos = this.ecs.get(targetHandle, Component.position);
1269 const playerDim = normalizeDimensionId(playerPos?.dimensionId ?? OVERWORLD_ID);
1270 const npcDim = normalizeDimensionId(npcPos?.dimensionId ?? OVERWORLD_ID);
1271 if (!playerPos || !npcPos || playerDim !== npcDim
1272 || !canTalkOrShopNear(
1273 playerPos.tileX,
1274 playerPos.tileY,
1275 npcPos.tileX,
1276 npcPos.tileY,
1277 (x, y) => this.blockTypeAt(x, y, npcDim)
1278 ))
1279 {
1280 const appearance = this.ecs.get(targetHandle, Component.appearance);
1281 const sex = appearance?.sex === "female" ? "female" : "male";
1282 const error = new Error(`You need to stand next to ${objectPronoun(sex)}.`);
1283 error.userFacing = true;
1284 throw error;
1285 }
1255 1286 const creature = this.ecs.get(targetHandle, Component.creature);
1256 1287 const def = creature ? mobDefinition(creature.mobTypeId) : null;
1257 1288 const shop = this.ecs.get(targetHandle, Component.shop);
@@ -2084,13 +2115,16 @@ export class WorkerWorld
2084 2115 }
2085 2116 const playerHandle = this.ecs.handleOf(playerEntityId);
2086 2117 const playerPos = this.ecs.get(playerHandle, Component.position);
2118 const npcDim = normalizeDimensionId(entity.position.dimensionId ?? OVERWORLD_ID);
2087 2119 const stillNear = playerPos
2088 && chebyshev(
2120 && normalizeDimensionId(playerPos.dimensionId ?? OVERWORLD_ID) === npcDim
2121 && canTalkOrShopNear(
2089 2122 playerPos.tileX,
2090 2123 playerPos.tileY,
2091 2124 entity.position.tileX,
2092 entity.position.tileY
2093 ) <= 1;
2125 entity.position.tileY,
2126 (x, y) => this.blockTypeAt(x, y, npcDim)
2127 );
2094 2128 if (stillNear)
2095 2129 { continue; }
2096 2130 delete browsers[characterId];
@@ -102,6 +102,8 @@ import {
102 PATH_GROUND_STEP_COST,
102 PATH_GROUND_STEP_COST,
103 SOFT_OCCUPIED_DWELL_MS
103 SOFT_OCCUPIED_DWELL_MS
104} from "../../../shared/navigation/pathGround.js";
104} from "../../../shared/navigation/pathGround.js";
105import { canTalkOrShopNear } from "../../../shared/navigation/adjacency.js";
106import { objectPronoun } from "../../../shared/speech/pronouns.js";
105import { withCombatBackstepCost } from "../../../shared/navigation/combatPathCost.js";
107import { withCombatBackstepCost } from "../../../shared/navigation/combatPathCost.js";
106import { GroundType, normalizeGround } from "../../../shared/definitions/tiles.js";
108import { GroundType, normalizeGround } from "../../../shared/definitions/tiles.js";
107import { clearFollow, setFollow, stepFollow, recordFollowTrail, consumeFollowTrail } from "./follow.js";
109import { clearFollow, setFollow, stepFollow, recordFollowTrail, consumeFollowTrail } from "./follow.js";
@@ -282,6 +284,14 @@ export class WorkerWorld
282 return this.#chunkData(chunkX, chunkY, dimensionId);
284 return this.#chunkData(chunkX, chunkY, dimensionId);
283 }
285 }
284
286
287 blockTypeAt (tileX, tileY, dimensionId = OVERWORLD_ID)
288 {
289 const dim = normalizeDimensionId(dimensionId);
290 const data = this.#chunkData(tileToChunk(tileX), tileToChunk(tileY), dim);
291 const block = data?.blocks?.[tileIndex(tileToLocal(tileX), tileToLocal(tileY))];
292 return block?.typeId ?? null;
293 }
294
285 #configure (message)
295 #configure (message)
286 {
296 {
287 if (message.settings)
297 if (message.settings)
@@ -1252,6 +1262,27 @@ export class WorkerWorld
1252 error.userFacing = true;
1262 error.userFacing = true;
1253 throw error;
1263 throw error;
1254 }
1264 }
1265 const playerEntityId = this.characterEntities.get(characterId);
1266 const playerHandle = this.ecs.handleOf(playerEntityId);
1267 const playerPos = playerHandle != null ? this.ecs.get(playerHandle, Component.position) : null;
1268 const npcPos = this.ecs.get(targetHandle, Component.position);
1269 const playerDim = normalizeDimensionId(playerPos?.dimensionId ?? OVERWORLD_ID);
1270 const npcDim = normalizeDimensionId(npcPos?.dimensionId ?? OVERWORLD_ID);
1271 if (!playerPos || !npcPos || playerDim !== npcDim
1272 || !canTalkOrShopNear(
1273 playerPos.tileX,
1274 playerPos.tileY,
1275 npcPos.tileX,
1276 npcPos.tileY,
1277 (x, y) => this.blockTypeAt(x, y, npcDim)
1278 ))
1279 {
1280 const appearance = this.ecs.get(targetHandle, Component.appearance);
1281 const sex = appearance?.sex === "female" ? "female" : "male";
1282 const error = new Error(`You need to stand next to ${objectPronoun(sex)}.`);
1283 error.userFacing = true;
1284 throw error;
1285 }
1255 const creature = this.ecs.get(targetHandle, Component.creature);
1286 const creature = this.ecs.get(targetHandle, Component.creature);
1256 const def = creature ? mobDefinition(creature.mobTypeId) : null;
1287 const def = creature ? mobDefinition(creature.mobTypeId) : null;
1257 const shop = this.ecs.get(targetHandle, Component.shop);
1288 const shop = this.ecs.get(targetHandle, Component.shop);
@@ -2084,13 +2115,16 @@ export class WorkerWorld
2084 }
2115 }
2085 const playerHandle = this.ecs.handleOf(playerEntityId);
2116 const playerHandle = this.ecs.handleOf(playerEntityId);
2086 const playerPos = this.ecs.get(playerHandle, Component.position);
2117 const playerPos = this.ecs.get(playerHandle, Component.position);
2118 const npcDim = normalizeDimensionId(entity.position.dimensionId ?? OVERWORLD_ID);
2087 const stillNear = playerPos
2119 const stillNear = playerPos
2088 && chebyshev(
2120 && normalizeDimensionId(playerPos.dimensionId ?? OVERWORLD_ID) === npcDim
2121 && canTalkOrShopNear(
2089 playerPos.tileX,
2122 playerPos.tileX,
2090 playerPos.tileY,
2123 playerPos.tileY,
2091 entity.position.tileX,
2124 entity.position.tileX,
2092 entity.position.tileY
2125 entity.position.tileY,
2093 ) <= 1;
2126 (x, y) => this.blockTypeAt(x, y, npcDim)
2127 );
2094 if (stillNear)
2128 if (stillNear)
2095 { continue; }
2129 { continue; }
2096 delete browsers[characterId];
2130 delete browsers[characterId];
server/tests/adjacency-interaction.test.js +19 −1
@@ -3,7 +3,7 @@ import assert from "node:assert/strict";
3 3 import { BlockType } from "../../shared/definitions/tiles.js";
4 4 import { ItemId } from "../../shared/definitions/items.js";
5 5 import { SkillId } from "../../shared/definitions/skills.js";
6 import { isAdjacentTile } from "../../shared/navigation/adjacency.js";
6 import { isAdjacentTile, canTalkOrShopNear } from "../../shared/navigation/adjacency.js";
7 7 import { totalXpForLevel } from "../../shared/skills/progression.js";
8 8 import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
9 9 import { Component } from "../src/ecs/components.js";
@@ -15,6 +15,24 @@ test("isAdjacentTile excludes same tile and allows neighbours", () =>
15 15 assert.equal(isAdjacentTile(5, 5, 7, 5), false);
16 16 });
17 17
18 test("canTalkOrShopNear allows standing across a table or marketplace counter", () =>
19 {
20 const blocks = new Map([
21 ["10,8", BlockType.table],
22 ["20,8", BlockType.marketplace]
23 ]);
24 const getBlock = (x, y) => blocks.get(`${x},${y}`) ?? null;
25 // NPC at 10,9; table at 10,8; player at 10,7 — over the table.
26 assert.equal(canTalkOrShopNear(10, 7, 10, 9, getBlock), true);
27 assert.equal(isAdjacentTile(10, 7, 10, 9), false);
28 // Direct adjacency still works without furniture.
29 assert.equal(canTalkOrShopNear(11, 9, 10, 9, getBlock), true);
30 // Two tiles away with no counter is too far.
31 assert.equal(canTalkOrShopNear(10, 7, 10, 9, () => null), false);
32 // Marketplace counter works the same way.
33 assert.equal(canTalkOrShopNear(20, 7, 20, 9, getBlock), true);
34 });
35
18 36 function placeOre (worker, tileX, tileY, state)
19 37 {
20 38 const data = worker.world.chunkData(Math.floor(tileX / 16), Math.floor(tileY / 16));
@@ -3,7 +3,7 @@ import assert from "node:assert/strict";
3import { BlockType } from "../../shared/definitions/tiles.js";
3import { BlockType } from "../../shared/definitions/tiles.js";
4import { ItemId } from "../../shared/definitions/items.js";
4import { ItemId } from "../../shared/definitions/items.js";
5import { SkillId } from "../../shared/definitions/skills.js";
5import { SkillId } from "../../shared/definitions/skills.js";
6import { isAdjacentTile } from "../../shared/navigation/adjacency.js";
6import { isAdjacentTile, canTalkOrShopNear } from "../../shared/navigation/adjacency.js";
7import { totalXpForLevel } from "../../shared/skills/progression.js";
7import { totalXpForLevel } from "../../shared/skills/progression.js";
8import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
8import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
9import { Component } from "../src/ecs/components.js";
9import { Component } from "../src/ecs/components.js";
@@ -15,6 +15,24 @@ test("isAdjacentTile excludes same tile and allows neighbours", () =>
15 assert.equal(isAdjacentTile(5, 5, 7, 5), false);
15 assert.equal(isAdjacentTile(5, 5, 7, 5), false);
16});
16});
17
17
18test("canTalkOrShopNear allows standing across a table or marketplace counter", () =>
19{
20 const blocks = new Map([
21 ["10,8", BlockType.table],
22 ["20,8", BlockType.marketplace]
23 ]);
24 const getBlock = (x, y) => blocks.get(`${x},${y}`) ?? null;
25 // NPC at 10,9; table at 10,8; player at 10,7 — over the table.
26 assert.equal(canTalkOrShopNear(10, 7, 10, 9, getBlock), true);
27 assert.equal(isAdjacentTile(10, 7, 10, 9), false);
28 // Direct adjacency still works without furniture.
29 assert.equal(canTalkOrShopNear(11, 9, 10, 9, getBlock), true);
30 // Two tiles away with no counter is too far.
31 assert.equal(canTalkOrShopNear(10, 7, 10, 9, () => null), false);
32 // Marketplace counter works the same way.
33 assert.equal(canTalkOrShopNear(20, 7, 20, 9, getBlock), true);
34});
35
18function placeOre (worker, tileX, tileY, state)
36function placeOre (worker, tileX, tileY, state)
19{
37{
20 const data = worker.world.chunkData(Math.floor(tileX / 16), Math.floor(tileY / 16));
38 const data = worker.world.chunkData(Math.floor(tileX / 16), Math.floor(tileY / 16));
server/tests/shop.test.js +73 −0
@@ -1300,3 +1300,76 @@ test("vendor bought dye stays split by colour and resells that colour", () =>
1300 1300 finally
1301 1301 { harness.close(); }
1302 1302 });
1303
1304 test("shop opens across a table when adjacent tiles to the NPC are blocked", () =>
1305 {
1306 const harness = makeSimulation({ workerCount: 1 });
1307 try
1308 {
1309 const data = createEmptyChunkData();
1310 data.open = true;
1311 // NPC at 9,8; table south at 9,9; player further south at 9,10.
1312 data.blocks[9 * 16 + 9] = { typeId: BlockType.table, state: 0, config: null };
1313 data.spawners = [{
1314 localX: 9,
1315 localY: 8,
1316 config: {
1317 mobTypeId: MobId.humanoid,
1318 maxAlive: 1,
1319 spawnRangeTiles: 0,
1320 maxWanderRangeTiles: 0,
1321 sex: "female",
1322 displayName: "Barmaid",
1323 sells: [{ itemId: ItemId.paper, quantity: 0 }],
1324 hiddenDiscount: 1,
1325 equipment: { chest: ItemId.clothShirt }
1326 }
1327 }];
1328 harness.worldDb.saveChunkVersion(0, 0, harness.worldDb.getActiveRevisionId(), true, encodeChunkBlob(data));
1329 harness.simulation.reloadWorld();
1330
1331 const character = makeCharacter(harness.stateDb, { tileX: 9, tileY: 10 });
1332 const playerId = harness.simulation.spawnCharacter(1, character);
1333 harness.simulation.step();
1334 harness.advance(50);
1335 harness.simulation.step();
1336
1337 const record = harness.simulation.entityRecord(playerId);
1338 const worker = harness.simulation.workers[record.workerId];
1339 let npcId = null;
1340 for (const entity of worker.world.ecs.query(Component.shop, Component.creature))
1341 {
1342 if (entity.creature.mobTypeId === MobId.humanoid)
1343 {
1344 npcId = entity.entityId;
1345 break;
1346 }
1347 }
1348 assert.ok(npcId, "expected a humanoid shopkeeper");
1349
1350 const npcHandle = worker.world.ecs.handleOf(npcId);
1351 const npcPos = worker.world.ecs.get(npcHandle, Component.position);
1352 npcPos.tileX = 9;
1353 npcPos.tileY = 8;
1354 const playerHandle = worker.world.ecs.handleOf(playerId);
1355 const playerPos = worker.world.ecs.get(playerHandle, Component.position);
1356 playerPos.tileX = 9;
1357 playerPos.tileY = 10;
1358
1359 assert.equal(worker.world.blockTypeAt(9, 9), BlockType.table);
1360
1361 harness.simulation.enqueue(1, {
1362 type: "interact",
1363 characterId: character.character_id,
1364 targetEntityId: npcId,
1365 purpose: "shop"
1366 });
1367 const open = harness.simulation.step();
1368 assert.ok(
1369 open.events.some((event) => event.type === "shop-open"),
1370 JSON.stringify(open.events)
1371 );
1372 }
1373 finally
1374 { harness.close(); }
1375 });
@@ -1300,3 +1300,76 @@ test("vendor bought dye stays split by colour and resells that colour", () =>
1300 finally
1300 finally
1301 { harness.close(); }
1301 { harness.close(); }
1302});
1302});
1303
1304test("shop opens across a table when adjacent tiles to the NPC are blocked", () =>
1305{
1306 const harness = makeSimulation({ workerCount: 1 });
1307 try
1308 {
1309 const data = createEmptyChunkData();
1310 data.open = true;
1311 // NPC at 9,8; table south at 9,9; player further south at 9,10.
1312 data.blocks[9 * 16 + 9] = { typeId: BlockType.table, state: 0, config: null };
1313 data.spawners = [{
1314 localX: 9,
1315 localY: 8,
1316 config: {
1317 mobTypeId: MobId.humanoid,
1318 maxAlive: 1,
1319 spawnRangeTiles: 0,
1320 maxWanderRangeTiles: 0,
1321 sex: "female",
1322 displayName: "Barmaid",
1323 sells: [{ itemId: ItemId.paper, quantity: 0 }],
1324 hiddenDiscount: 1,
1325 equipment: { chest: ItemId.clothShirt }
1326 }
1327 }];
1328 harness.worldDb.saveChunkVersion(0, 0, harness.worldDb.getActiveRevisionId(), true, encodeChunkBlob(data));
1329 harness.simulation.reloadWorld();
1330
1331 const character = makeCharacter(harness.stateDb, { tileX: 9, tileY: 10 });
1332 const playerId = harness.simulation.spawnCharacter(1, character);
1333 harness.simulation.step();
1334 harness.advance(50);
1335 harness.simulation.step();
1336
1337 const record = harness.simulation.entityRecord(playerId);
1338 const worker = harness.simulation.workers[record.workerId];
1339 let npcId = null;
1340 for (const entity of worker.world.ecs.query(Component.shop, Component.creature))
1341 {
1342 if (entity.creature.mobTypeId === MobId.humanoid)
1343 {
1344 npcId = entity.entityId;
1345 break;
1346 }
1347 }
1348 assert.ok(npcId, "expected a humanoid shopkeeper");
1349
1350 const npcHandle = worker.world.ecs.handleOf(npcId);
1351 const npcPos = worker.world.ecs.get(npcHandle, Component.position);
1352 npcPos.tileX = 9;
1353 npcPos.tileY = 8;
1354 const playerHandle = worker.world.ecs.handleOf(playerId);
1355 const playerPos = worker.world.ecs.get(playerHandle, Component.position);
1356 playerPos.tileX = 9;
1357 playerPos.tileY = 10;
1358
1359 assert.equal(worker.world.blockTypeAt(9, 9), BlockType.table);
1360
1361 harness.simulation.enqueue(1, {
1362 type: "interact",
1363 characterId: character.character_id,
1364 targetEntityId: npcId,
1365 purpose: "shop"
1366 });
1367 const open = harness.simulation.step();
1368 assert.ok(
1369 open.events.some((event) => event.type === "shop-open"),
1370 JSON.stringify(open.events)
1371 );
1372 }
1373 finally
1374 { harness.close(); }
1375});
shared/navigation/adjacency.js +35 −0
@@ -1,3 +1,5 @@
1 import { BlockType } from "../definitions/tiles.js";
2
1 3 /** Chebyshev distance between tile centres (0 = same tile). */
2 4 export function chebyshevTileDistance (ax, ay, bx, by)
3 5 {
@@ -15,6 +17,39 @@ export function isSameTile (ax, ay, bx, by)
15 17 return ax === bx && ay === by;
16 18 }
17 19
20 /** Furniture you can talk / shop across (NPC counter side). */
21 export function isNpcCounterBlockType (typeId)
22 {
23 return typeId === BlockType.table || typeId === BlockType.marketplace;
24 }
25
26 /**
27 * Talk / shop reach: direct adjacency, or both sides of a table/counter.
28 * @param {(tileX: number, tileY: number) => number|null|undefined} getBlockTypeAt
29 */
30 export function canTalkOrShopNear (playerX, playerY, npcX, npcY, getBlockTypeAt)
31 {
32 if (isAdjacentTile(playerX, playerY, npcX, npcY))
33 { return true; }
34 if (typeof getBlockTypeAt !== "function")
35 { return false; }
36 for (let dy = -1; dy <= 1; dy++)
37 {
38 for (let dx = -1; dx <= 1; dx++)
39 {
40 if (dx === 0 && dy === 0)
41 { continue; }
42 const counterX = npcX + dx;
43 const counterY = npcY + dy;
44 if (!isNpcCounterBlockType(getBlockTypeAt(counterX, counterY)))
45 { continue; }
46 if (isAdjacentTile(playerX, playerY, counterX, counterY))
47 { return true; }
48 }
49 }
50 return false;
51 }
52
18 53 /** True when blockType exists on a tile adjacent to (tileX, tileY), not on that tile. */
19 54 export function hasAdjacentBlockType (getBlockTypeAt, tileX, tileY, blockType)
20 55 {
@@ -1,3 +1,5 @@
1import { BlockType } from "../definitions/tiles.js";
2
1/** Chebyshev distance between tile centres (0 = same tile). */
3/** Chebyshev distance between tile centres (0 = same tile). */
2export function chebyshevTileDistance (ax, ay, bx, by)
4export function chebyshevTileDistance (ax, ay, bx, by)
3{
5{
@@ -15,6 +17,39 @@ export function isSameTile (ax, ay, bx, by)
15 return ax === bx && ay === by;
17 return ax === bx && ay === by;
16}
18}
17
19
20/** Furniture you can talk / shop across (NPC counter side). */
21export function isNpcCounterBlockType (typeId)
22{
23 return typeId === BlockType.table || typeId === BlockType.marketplace;
24}
25
26/**
27 * Talk / shop reach: direct adjacency, or both sides of a table/counter.
28 * @param {(tileX: number, tileY: number) => number|null|undefined} getBlockTypeAt
29 */
30export function canTalkOrShopNear (playerX, playerY, npcX, npcY, getBlockTypeAt)
31{
32 if (isAdjacentTile(playerX, playerY, npcX, npcY))
33 { return true; }
34 if (typeof getBlockTypeAt !== "function")
35 { return false; }
36 for (let dy = -1; dy <= 1; dy++)
37 {
38 for (let dx = -1; dx <= 1; dx++)
39 {
40 if (dx === 0 && dy === 0)
41 { continue; }
42 const counterX = npcX + dx;
43 const counterY = npcY + dy;
44 if (!isNpcCounterBlockType(getBlockTypeAt(counterX, counterY)))
45 { continue; }
46 if (isAdjacentTile(playerX, playerY, counterX, counterY))
47 { return true; }
48 }
49 }
50 return false;
51}
52
18/** True when blockType exists on a tile adjacent to (tileX, tileY), not on that tile. */
53/** True when blockType exists on a tile adjacent to (tileX, tileY), not on that tile. */
19export function hasAdjacentBlockType (getBlockTypeAt, tileX, tileY, blockType)
54export function hasAdjacentBlockType (getBlockTypeAt, tileX, tileY, blockType)
20{
55{