Merge branch 'feature/dimension-agnostic-systems'
@@ -849,7 +849,8 @@ function paintAt (localX, localY)
849
849
else if (tool.kind === "spawner")
850
850
{
851
851
const existing = spawnerAt(localX, localY);
852
const spawnerId = existing?.config?.spawnerId ?? `${chunkX}:${chunkY}:${index}`;
852
const spawnerId = existing?.config?.spawnerId
853
?? `${normalizeDimensionId(selectedDimensionId)}:${chunkX}:${chunkY}:${index}`;
853
854
const entry = {
854
855
localX,
855
856
localY,
@@ -849,7 +849,8 @@ function paintAt (localX, localY)849
else if (tool.kind === "spawner")849
else if (tool.kind === "spawner")850
{850
{851
const existing = spawnerAt(localX, localY);851
const existing = spawnerAt(localX, localY);852
const spawnerId = existing?.config?.spawnerId ?? `${chunkX}:${chunkY}:${index}`;852
const spawnerId = existing?.config?.spawnerId853
?? `${normalizeDimensionId(selectedDimensionId)}:${chunkX}:${chunkY}:${index}`;853
const entry = {854
const entry = {854
localX,855
localX,855
localY,856
localY,@@ -395,6 +395,7 @@ function grantEmptyGlassBottles (world, handle, characterId, quantity, events, n
395
395
const placed = placeOrMergeGroundStack(world, {
396
396
tileX: position.tileX,
397
397
tileY: position.tileY,
398
dimensionId: normalizeDimensionId(position.dimensionId ?? OVERWORLD_ID),
398
399
stack: { itemId: ItemId.glassBottle, quantity: remainder },
399
400
now,
400
401
source: "alchemy-overflow",
@@ -395,6 +395,7 @@ function grantEmptyGlassBottles (world, handle, characterId, quantity, events, n395
const placed = placeOrMergeGroundStack(world, {395
const placed = placeOrMergeGroundStack(world, {396
tileX: position.tileX,396
tileX: position.tileX,397
tileY: position.tileY,397
tileY: position.tileY,398
dimensionId: normalizeDimensionId(position.dimensionId ?? OVERWORLD_ID),398
stack: { itemId: ItemId.glassBottle, quantity: remainder },399
stack: { itemId: ItemId.glassBottle, quantity: remainder },399
now,400
now,400
source: "alchemy-overflow",401
source: "alchemy-overflow",@@ -732,9 +732,11 @@ function spawnMobDrops (world, targetHandle, tileX, tileY, now, attackerId = nul
732
732
});
733
733
if (tryAutoLootToKiller(world, attackerId, stack, events))
734
734
{ continue; }
735
const targetPos = world.ecs.get(targetHandle, Component.position);
735
736
const placed = placeOrMergeGroundStack(world, {
736
737
tileX,
737
738
tileY,
739
dimensionId: normalizeDimensionId(targetPos?.dimensionId ?? OVERWORLD_ID),
738
740
stack,
739
741
now,
740
742
source: "mob-drop"
@@ -732,9 +732,11 @@ function spawnMobDrops (world, targetHandle, tileX, tileY, now, attackerId = nul732
});732
});733
if (tryAutoLootToKiller(world, attackerId, stack, events))733
if (tryAutoLootToKiller(world, attackerId, stack, events))734
{ continue; }734
{ continue; }735
const targetPos = world.ecs.get(targetHandle, Component.position);735
const placed = placeOrMergeGroundStack(world, {736
const placed = placeOrMergeGroundStack(world, {736
tileX,737
tileX,737
tileY,738
tileY,739
dimensionId: normalizeDimensionId(targetPos?.dimensionId ?? OVERWORLD_ID),738
stack,740
stack,739
now,741
now,740
source: "mob-drop"742
source: "mob-drop"@@ -116,7 +116,7 @@ export class EditorService
116
116
if (Array.isArray(draft.spawners))
117
117
{ next.spawners = draft.spawners.map((entry) => normalizeSpawner(entry)).filter(Boolean); }
118
118
119
this.validateSpawnerDialogues(chunkX, chunkY, next.spawners);
119
this.validateSpawnerDialogues(chunkX, chunkY, next.spawners, dimId);
120
120
121
121
const diffEntries = diffChunks(chunkX, chunkY, previous.data, next, previous.authored);
122
122
const revisionId = this.worldDb.ensurePendingRevision(editorAdminId);
@@ -540,8 +540,9 @@ export class EditorService
540
540
}
541
541
542
542
/** Validate dialogueId / unique / maxAlive when saving a chunk's spawners. */
543
validateSpawnerDialogues (chunkX, chunkY, spawners)
543
validateSpawnerDialogues (chunkX, chunkY, spawners, dimensionId = OVERWORLD_ID)
544
544
{
545
const dimId = normalizeDimensionId(dimensionId);
545
546
const revisionId = this.worldDb.getEditorRevisionId();
546
547
const trees = new Map(
547
548
this.worldDb.listDialogueTrees(revisionId).map((raw) =>
@@ -569,7 +570,12 @@ export class EditorService
569
570
throw err;
570
571
}
571
572
}
572
const counts = this.#countDialogueAssignments(revisionId, { chunkX, chunkY, spawners });
573
const counts = this.#countDialogueAssignments(revisionId, {
574
chunkX,
575
chunkY,
576
dimensionId: dimId,
577
spawners
578
});
573
579
for (const [dialogueId, tree] of trees)
574
580
{
575
581
const n = counts.get(dialogueId) ?? 0;
@@ -587,11 +593,17 @@ export class EditorService
587
593
const settings = this.worldDb.getLiveSettings();
588
594
const counts = new Map();
589
595
const coords = this.worldDb.listChunkCoords(revisionId);
590
for (const { chunk_x: cx, chunk_y: cy } of coords)
596
for (const { chunk_x: cx, chunk_y: cy, dimension_id: dimensionId } of coords)
591
597
{
592
if (replaceChunk && cx === replaceChunk.chunkX && cy === replaceChunk.chunkY)
598
const dim = normalizeDimensionId(dimensionId ?? OVERWORLD_ID);
599
if (
600
replaceChunk
601
&& cx === replaceChunk.chunkX
602
&& cy === replaceChunk.chunkY
603
&& dim === normalizeDimensionId(replaceChunk.dimensionId ?? OVERWORLD_ID)
604
)
593
605
{ continue; }
594
const row = this.worldDb.getChunkVersion(cx, cy, revisionId);
606
const row = this.worldDb.getChunkVersion(cx, cy, revisionId, dim);
595
607
if (!row)
596
608
{ continue; }
597
609
const data = decodeChunkBlob(row.data_blob, settings.defaultGroundTypeId, cx, cy);
@@ -620,9 +632,10 @@ export class EditorService
620
632
{
621
633
const settings = this.worldDb.getLiveSettings();
622
634
const coords = this.worldDb.listChunkCoords(revisionId);
623
for (const { chunk_x: chunkX, chunk_y: chunkY } of coords)
635
for (const { chunk_x: chunkX, chunk_y: chunkY, dimension_id: dimensionId } of coords)
624
636
{
625
const row = this.worldDb.getChunkVersion(chunkX, chunkY, revisionId);
637
const dim = normalizeDimensionId(dimensionId ?? OVERWORLD_ID);
638
const row = this.worldDb.getChunkVersion(chunkX, chunkY, revisionId, dim);
626
639
if (!row)
627
640
{ continue; }
628
641
const data = decodeChunkBlob(row.data_blob, settings.defaultGroundTypeId, chunkX, chunkY);
@@ -116,7 +116,7 @@ export class EditorService116
if (Array.isArray(draft.spawners))116
if (Array.isArray(draft.spawners))117
{ next.spawners = draft.spawners.map((entry) => normalizeSpawner(entry)).filter(Boolean); }117
{ next.spawners = draft.spawners.map((entry) => normalizeSpawner(entry)).filter(Boolean); }118
118
119
this.validateSpawnerDialogues(chunkX, chunkY, next.spawners);119
this.validateSpawnerDialogues(chunkX, chunkY, next.spawners, dimId);120
120
121
const diffEntries = diffChunks(chunkX, chunkY, previous.data, next, previous.authored);121
const diffEntries = diffChunks(chunkX, chunkY, previous.data, next, previous.authored);122
const revisionId = this.worldDb.ensurePendingRevision(editorAdminId);122
const revisionId = this.worldDb.ensurePendingRevision(editorAdminId);@@ -540,8 +540,9 @@ export class EditorService540
}540
}541
541
542
/** Validate dialogueId / unique / maxAlive when saving a chunk's spawners. */542
/** Validate dialogueId / unique / maxAlive when saving a chunk's spawners. */543
validateSpawnerDialogues (chunkX, chunkY, spawners)543
validateSpawnerDialogues (chunkX, chunkY, spawners, dimensionId = OVERWORLD_ID)544
{544
{545
const dimId = normalizeDimensionId(dimensionId);545
const revisionId = this.worldDb.getEditorRevisionId();546
const revisionId = this.worldDb.getEditorRevisionId();546
const trees = new Map(547
const trees = new Map(547
this.worldDb.listDialogueTrees(revisionId).map((raw) =>548
this.worldDb.listDialogueTrees(revisionId).map((raw) =>@@ -569,7 +570,12 @@ export class EditorService569
throw err;570
throw err;570
}571
}571
}572
}572
const counts = this.#countDialogueAssignments(revisionId, { chunkX, chunkY, spawners });573
const counts = this.#countDialogueAssignments(revisionId, {574
chunkX,575
chunkY,576
dimensionId: dimId,577
spawners578
});573
for (const [dialogueId, tree] of trees)579
for (const [dialogueId, tree] of trees)574
{580
{575
const n = counts.get(dialogueId) ?? 0;581
const n = counts.get(dialogueId) ?? 0;@@ -587,11 +593,17 @@ export class EditorService587
const settings = this.worldDb.getLiveSettings();593
const settings = this.worldDb.getLiveSettings();588
const counts = new Map();594
const counts = new Map();589
const coords = this.worldDb.listChunkCoords(revisionId);595
const coords = this.worldDb.listChunkCoords(revisionId);590
for (const { chunk_x: cx, chunk_y: cy } of coords)596
for (const { chunk_x: cx, chunk_y: cy, dimension_id: dimensionId } of coords)591
{597
{592
if (replaceChunk && cx === replaceChunk.chunkX && cy === replaceChunk.chunkY)598
const dim = normalizeDimensionId(dimensionId ?? OVERWORLD_ID);599
if (600
replaceChunk601
&& cx === replaceChunk.chunkX602
&& cy === replaceChunk.chunkY603
&& dim === normalizeDimensionId(replaceChunk.dimensionId ?? OVERWORLD_ID)604
)593
{ continue; }605
{ continue; }594
const row = this.worldDb.getChunkVersion(cx, cy, revisionId);606
const row = this.worldDb.getChunkVersion(cx, cy, revisionId, dim);595
if (!row)607
if (!row)596
{ continue; }608
{ continue; }597
const data = decodeChunkBlob(row.data_blob, settings.defaultGroundTypeId, cx, cy);609
const data = decodeChunkBlob(row.data_blob, settings.defaultGroundTypeId, cx, cy);@@ -620,9 +632,10 @@ export class EditorService620
{632
{621
const settings = this.worldDb.getLiveSettings();633
const settings = this.worldDb.getLiveSettings();622
const coords = this.worldDb.listChunkCoords(revisionId);634
const coords = this.worldDb.listChunkCoords(revisionId);623
for (const { chunk_x: chunkX, chunk_y: chunkY } of coords)635
for (const { chunk_x: chunkX, chunk_y: chunkY, dimension_id: dimensionId } of coords)624
{636
{625
const row = this.worldDb.getChunkVersion(chunkX, chunkY, revisionId);637
const dim = normalizeDimensionId(dimensionId ?? OVERWORLD_ID);638
const row = this.worldDb.getChunkVersion(chunkX, chunkY, revisionId, dim);626
if (!row)639
if (!row)627
{ continue; }640
{ continue; }628
const data = decodeChunkBlob(row.data_blob, settings.defaultGroundTypeId, chunkX, chunkY);641
const data = decodeChunkBlob(row.data_blob, settings.defaultGroundTypeId, chunkX, chunkY);@@ -194,7 +194,7 @@ function consumeOneGroundLog (world, item, events)
194
194
});
195
195
}
196
196
197
function spawnAsh (world, tileX, tileY, woodTier, litByName, now, events)
197
function spawnAsh (world, tileX, tileY, woodTier, litByName, now, events, dimensionId = OVERWORLD_ID)
198
198
{
199
199
const ashItemId = ashItemIdForWood(woodTier);
200
200
if (!ashItemId)
@@ -211,6 +211,7 @@ function spawnAsh (world, tileX, tileY, woodTier, litByName, now, events)
211
211
const placed = placeOrMergeGroundStack(world, {
212
212
tileX,
213
213
tileY,
214
dimensionId: normalizeDimensionId(dimensionId),
214
215
stack,
215
216
now,
216
217
source: "campfire-ash"
@@ -496,7 +497,8 @@ export function stepCampfireExpires (world, now)
496
497
entry.woodTier,
497
498
entry.litByName,
498
499
now,
499
events
500
events,
501
dim
500
502
);
501
503
}
502
504
return { events };
@@ -194,7 +194,7 @@ function consumeOneGroundLog (world, item, events)194
});194
});195
}195
}196
196
197
function spawnAsh (world, tileX, tileY, woodTier, litByName, now, events)197
function spawnAsh (world, tileX, tileY, woodTier, litByName, now, events, dimensionId = OVERWORLD_ID)198
{198
{199
const ashItemId = ashItemIdForWood(woodTier);199
const ashItemId = ashItemIdForWood(woodTier);200
if (!ashItemId)200
if (!ashItemId)@@ -211,6 +211,7 @@ function spawnAsh (world, tileX, tileY, woodTier, litByName, now, events)211
const placed = placeOrMergeGroundStack(world, {211
const placed = placeOrMergeGroundStack(world, {212
tileX,212
tileX,213
tileY,213
tileY,214
dimensionId: normalizeDimensionId(dimensionId),214
stack,215
stack,215
now,216
now,216
source: "campfire-ash"217
source: "campfire-ash"@@ -496,7 +497,8 @@ export function stepCampfireExpires (world, now)496
entry.woodTier,497
entry.woodTier,497
entry.litByName,498
entry.litByName,498
now,499
now,499
events500
events,501
dim500
);502
);501
}503
}502
return { events };504
return { events };@@ -447,7 +447,8 @@ export function stepFishing (world, now)
447
447
position.tileX,
448
448
position.tileY,
449
449
session.tileX,
450
session.tileY
450
session.tileY,
451
normalizeDimensionId(position.dimensionId ?? OVERWORLD_ID)
451
452
));
452
453
// Also emit gather-swing so tool overlay shows.
453
454
events.push({
@@ -457,6 +458,7 @@ export function stepFishing (world, now)
457
458
tileY: session.tileY,
458
459
fromTileX: position.tileX,
459
460
fromTileY: position.tileY,
461
dimensionId: normalizeDimensionId(position.dimensionId ?? OVERWORLD_ID),
460
462
toolItemId: session.toolItemId,
461
463
swipe: false
462
464
});
@@ -499,7 +501,7 @@ export function stepFishing (world, now)
499
501
return { events, privates };
500
502
}
501
503
502
function spawnGroundStack (world, tileX, tileY, itemId, now, events, source = "fishing-line-break")
504
function spawnGroundStack (world, tileX, tileY, itemId, now, events, source = "fishing-line-break", dimensionId = OVERWORLD_ID)
503
505
{
504
506
const stack = attachOrigins({
505
507
itemId,
@@ -509,6 +511,7 @@ function spawnGroundStack (world, tileX, tileY, itemId, now, events, source = "f
509
511
const placed = placeOrMergeGroundStack(world, {
510
512
tileX,
511
513
tileY,
514
dimensionId: normalizeDimensionId(dimensionId),
512
515
stack,
513
516
now,
514
517
source
@@ -572,13 +575,13 @@ function maybeBreakFishingLine (world, handle, entityId, characterId, session, e
572
575
if (poleIns.ok && poleIns.remainder === 0)
573
576
{ slots = poleIns.slots; }
574
577
else
575
{ spawnGroundStack(world, position.tileX, position.tileY, poleItemId, now, events); }
578
{ spawnGroundStack(world, position.tileX, position.tileY, poleItemId, now, events, "fishing-line-break", normalizeDimensionId(position.dimensionId ?? OVERWORLD_ID)); }
576
579
577
580
const yarnIns = insertStack(slots, attachOrigins({ itemId: yarnId, quantity: 1, origins: [] }), yarnDef);
578
581
if (yarnIns.ok && yarnIns.remainder === 0)
579
582
{ slots = yarnIns.slots; }
580
583
else
581
{ spawnGroundStack(world, position.tileX, position.tileY, yarnId, now, events); }
584
{ spawnGroundStack(world, position.tileX, position.tileY, yarnId, now, events, "fishing-line-break", normalizeDimensionId(position.dimensionId ?? OVERWORLD_ID)); }
582
585
583
586
inventory.slots = slots;
584
587
@@ -447,7 +447,8 @@ export function stepFishing (world, now)447
position.tileX,447
position.tileX,448
position.tileY,448
position.tileY,449
session.tileX,449
session.tileX,450
session.tileY450
session.tileY,451
normalizeDimensionId(position.dimensionId ?? OVERWORLD_ID)451
));452
));452
// Also emit gather-swing so tool overlay shows.453
// Also emit gather-swing so tool overlay shows.453
events.push({454
events.push({@@ -457,6 +458,7 @@ export function stepFishing (world, now)457
tileY: session.tileY,458
tileY: session.tileY,458
fromTileX: position.tileX,459
fromTileX: position.tileX,459
fromTileY: position.tileY,460
fromTileY: position.tileY,461
dimensionId: normalizeDimensionId(position.dimensionId ?? OVERWORLD_ID),460
toolItemId: session.toolItemId,462
toolItemId: session.toolItemId,461
swipe: false463
swipe: false462
});464
});@@ -499,7 +501,7 @@ export function stepFishing (world, now)499
return { events, privates };501
return { events, privates };500
}502
}501
503
502
function spawnGroundStack (world, tileX, tileY, itemId, now, events, source = "fishing-line-break")504
function spawnGroundStack (world, tileX, tileY, itemId, now, events, source = "fishing-line-break", dimensionId = OVERWORLD_ID)503
{505
{504
const stack = attachOrigins({506
const stack = attachOrigins({505
itemId,507
itemId,@@ -509,6 +511,7 @@ function spawnGroundStack (world, tileX, tileY, itemId, now, events, source = "f509
const placed = placeOrMergeGroundStack(world, {511
const placed = placeOrMergeGroundStack(world, {510
tileX,512
tileX,511
tileY,513
tileY,514
dimensionId: normalizeDimensionId(dimensionId),512
stack,515
stack,513
now,516
now,514
source517
source@@ -572,13 +575,13 @@ function maybeBreakFishingLine (world, handle, entityId, characterId, session, e572
if (poleIns.ok && poleIns.remainder === 0)575
if (poleIns.ok && poleIns.remainder === 0)573
{ slots = poleIns.slots; }576
{ slots = poleIns.slots; }574
else577
else575
{ spawnGroundStack(world, position.tileX, position.tileY, poleItemId, now, events); }578
{ spawnGroundStack(world, position.tileX, position.tileY, poleItemId, now, events, "fishing-line-break", normalizeDimensionId(position.dimensionId ?? OVERWORLD_ID)); }576
579
577
const yarnIns = insertStack(slots, attachOrigins({ itemId: yarnId, quantity: 1, origins: [] }), yarnDef);580
const yarnIns = insertStack(slots, attachOrigins({ itemId: yarnId, quantity: 1, origins: [] }), yarnDef);578
if (yarnIns.ok && yarnIns.remainder === 0)581
if (yarnIns.ok && yarnIns.remainder === 0)579
{ slots = yarnIns.slots; }582
{ slots = yarnIns.slots; }580
else583
else581
{ spawnGroundStack(world, position.tileX, position.tileY, yarnId, now, events); }584
{ spawnGroundStack(world, position.tileX, position.tileY, yarnId, now, events, "fishing-line-break", normalizeDimensionId(position.dimensionId ?? OVERWORLD_ID)); }582
585
583
inventory.slots = slots;586
inventory.slots = slots;584
587
@@ -2183,9 +2183,31 @@ export function attachGateway ({ httpServer, auth, stateDb, characters, friends,
2183
2183
2184
2184
function publishEntity (event)
2185
2185
{
2186
const toDim = normalizeDimensionId(event.dimensionId
2187
?? simulation.entityRecord(event.entityId)?.dimensionId
2188
?? OVERWORLD_ID);
2186
const entityIds = [
2187
event.entityId,
2188
event.attackerId,
2189
event.casterId,
2190
event.targetId,
2191
event.targetEntityId
2192
];
2193
let resolvedDim = event.dimensionId != null
2194
? normalizeDimensionId(event.dimensionId)
2195
: null;
2196
if (resolvedDim == null)
2197
{
2198
for (const id of entityIds)
2199
{
2200
if (id == null)
2201
{ continue; }
2202
const record = simulation.entityRecord(id);
2203
if (record?.dimensionId != null)
2204
{
2205
resolvedDim = normalizeDimensionId(record.dimensionId);
2206
break;
2207
}
2208
}
2209
}
2210
const toDim = resolvedDim ?? OVERWORLD_ID;
2189
2211
const fromDim = event.fromDimensionId != null
2190
2212
? normalizeDimensionId(event.fromDimensionId)
2191
2213
: toDim;
@@ -2183,9 +2183,31 @@ export function attachGateway ({ httpServer, auth, stateDb, characters, friends,2183
2183
2184
function publishEntity (event)2184
function publishEntity (event)2185
{2185
{2186
const toDim = normalizeDimensionId(event.dimensionId2186
const entityIds = [2187
?? simulation.entityRecord(event.entityId)?.dimensionId2187
event.entityId,2188
?? OVERWORLD_ID);2188
event.attackerId,2189
event.casterId,2190
event.targetId,2191
event.targetEntityId2192
];2193
let resolvedDim = event.dimensionId != null2194
? normalizeDimensionId(event.dimensionId)2195
: null;2196
if (resolvedDim == null)2197
{2198
for (const id of entityIds)2199
{2200
if (id == null)2201
{ continue; }2202
const record = simulation.entityRecord(id);2203
if (record?.dimensionId != null)2204
{2205
resolvedDim = normalizeDimensionId(record.dimensionId);2206
break;2207
}2208
}2209
}2210
const toDim = resolvedDim ?? OVERWORLD_ID;2189
const fromDim = event.fromDimensionId != null2211
const fromDim = event.fromDimensionId != null2190
? normalizeDimensionId(event.fromDimensionId)2212
? normalizeDimensionId(event.fromDimensionId)2191
: toDim;2213
: toDim;@@ -920,6 +920,7 @@ function completeDebark (world, handle, entityId, character, gather, events, pri
920
920
const placedDebarked = placeOrMergeGroundStack(world, {
921
921
tileX,
922
922
tileY,
923
dimensionId: normalizeDimensionId(item.dimensionId ?? OVERWORLD_ID),
923
924
stack: debarkedStack,
924
925
now,
925
926
source: "debark",
@@ -931,6 +932,7 @@ function completeDebark (world, handle, entityId, character, gather, events, pri
931
932
const placedBark = placeOrMergeGroundStack(world, {
932
933
tileX,
933
934
tileY,
935
dimensionId: normalizeDimensionId(item.dimensionId ?? OVERWORLD_ID),
934
936
stack: attachOrigins({
935
937
itemId: gather.barkItemId,
936
938
quantity: 1,
@@ -920,6 +920,7 @@ function completeDebark (world, handle, entityId, character, gather, events, pri920
const placedDebarked = placeOrMergeGroundStack(world, {920
const placedDebarked = placeOrMergeGroundStack(world, {921
tileX,921
tileX,922
tileY,922
tileY,923
dimensionId: normalizeDimensionId(item.dimensionId ?? OVERWORLD_ID),923
stack: debarkedStack,924
stack: debarkedStack,924
now,925
now,925
source: "debark",926
source: "debark",@@ -931,6 +932,7 @@ function completeDebark (world, handle, entityId, character, gather, events, pri931
const placedBark = placeOrMergeGroundStack(world, {932
const placedBark = placeOrMergeGroundStack(world, {932
tileX,933
tileX,933
tileY,934
tileY,935
dimensionId: normalizeDimensionId(item.dimensionId ?? OVERWORLD_ID),934
stack: attachOrigins({936
stack: attachOrigins({935
itemId: gather.barkItemId,937
itemId: gather.barkItemId,936
quantity: 1,938
quantity: 1,@@ -58,6 +58,7 @@ export function publicGroundItem (item)
58
58
groundItemId: item.groundItemId,
59
59
tileX: item.tileX,
60
60
tileY: item.tileY,
61
dimensionId: normalizeDimensionId(item.dimensionId ?? OVERWORLD_ID),
61
62
itemId: item.stack.itemId,
62
63
quantity: item.stack.quantity,
63
64
displayName: itemDisplayName(item.stack),
@@ -208,24 +209,34 @@ export function expireGroundItems (world, now)
208
209
return events;
209
210
}
210
211
211
export function groundItemsInChunk (world, chunkX, chunkY)
212
export function groundItemsInChunk (world, chunkX, chunkY, dimensionId = OVERWORLD_ID)
212
213
{
214
const dim = normalizeDimensionId(dimensionId);
213
215
return [...world.groundItems.values()]
214
.filter((item) => tileToChunk(item.tileX) === chunkX && tileToChunk(item.tileY) === chunkY)
216
.filter((item) =>
217
tileToChunk(item.tileX) === chunkX
218
&& tileToChunk(item.tileY) === chunkY
219
&& normalizeDimensionId(item.dimensionId ?? OVERWORLD_ID) === dim
220
)
215
221
.map(publicGroundItem);
216
222
}
217
223
218
export function groundItemsInRegion (world, regionX, regionY, tileToRegion)
224
export function groundItemsInRegion (world, regionX, regionY, tileToRegion, dimensionId = OVERWORLD_ID)
219
225
{
226
const dim = normalizeDimensionId(dimensionId);
220
227
return [...world.groundItems.values()].filter((item) =>
221
tileToRegion(item.tileX) === regionX && tileToRegion(item.tileY) === regionY
228
tileToRegion(item.tileX) === regionX
229
&& tileToRegion(item.tileY) === regionY
230
&& normalizeDimensionId(item.dimensionId ?? OVERWORLD_ID) === dim
222
231
);
223
232
}
224
233
225
234
export function addGroundItem (world, item)
226
235
{
236
const dimensionId = normalizeDimensionId(item.dimensionId ?? OVERWORLD_ID);
237
item.dimensionId = dimensionId;
227
238
world.groundItems.set(item.groundItemId, item);
228
const key = tileKey(item.tileX, item.tileY);
239
const key = tileKey(item.tileX, item.tileY, dimensionId);
229
240
if (!world.groundByTile.has(key))
230
241
{ world.groundByTile.set(key, new Set()); }
231
242
world.groundByTile.get(key).add(item.groundItemId);
@@ -237,8 +248,10 @@ export function removeGroundItem (world, groundItemId)
237
248
if (!item)
238
249
{ return null; }
239
250
world.groundItems.delete(groundItemId);
240
const key = tileKey(item.tileX, item.tileY);
251
const key = tileKey(item.tileX, item.tileY, normalizeDimensionId(item.dimensionId ?? OVERWORLD_ID));
241
252
world.groundByTile.get(key)?.delete(groundItemId);
253
if (world.groundByTile.get(key)?.size === 0)
254
{ world.groundByTile.delete(key); }
242
255
return item;
243
256
}
244
257
@@ -250,10 +263,13 @@ export function replaceGroundItems (world, items)
250
263
{ addGroundItem(world, item); }
251
264
}
252
265
253
export function setGroundItemsForRegion (world, regionX, regionY, items, tileToRegion)
266
export function setGroundItemsForRegion (world, regionX, regionY, items, tileToRegion, dimensionId = OVERWORLD_ID)
254
267
{
268
const dim = normalizeDimensionId(dimensionId);
255
269
for (const item of [...world.groundItems.values()])
256
270
{
271
if (normalizeDimensionId(item.dimensionId ?? OVERWORLD_ID) !== dim)
272
{ continue; }
257
273
if (tileToRegion(item.tileX) === regionX && tileToRegion(item.tileY) === regionY)
258
274
{ removeGroundItem(world, item.groundItemId); }
259
275
}
@@ -295,7 +311,8 @@ export function placeOrMergeGroundStack (world, {
295
311
source = "world",
296
312
droppedByCharacterId = null,
297
313
groundItemId = null,
298
despawnDurationMs = null
314
despawnDurationMs = null,
315
dimensionId = OVERWORLD_ID
299
316
})
300
317
{
301
318
const events = [];
@@ -303,6 +320,7 @@ export function placeOrMergeGroundStack (world, {
303
320
if (!stack || quantity <= 0)
304
321
{ return { events }; }
305
322
323
const dim = normalizeDimensionId(dimensionId);
306
324
const def = itemDefinition(stack.itemId);
307
325
const rules = stackRulesFor(def);
308
326
const duration = despawnDurationMs
@@ -314,7 +332,7 @@ export function placeOrMergeGroundStack (world, {
314
332
315
333
if (rules.stackable && !rules.uniqueInstance)
316
334
{
317
const ids = [...(world.groundByTile.get(tileKey(tileX, tileY)) ?? [])];
335
const ids = [...(world.groundByTile.get(tileKey(tileX, tileY, dim)) ?? [])];
318
336
for (const id of ids)
319
337
{
320
338
const existing = world.groundItems.get(id);
@@ -353,6 +371,7 @@ export function placeOrMergeGroundStack (world, {
353
371
groundItemId: allocateGroundItemId(world, preferredId),
354
372
tileX,
355
373
tileY,
374
dimensionId: dim,
356
375
stack: {
357
376
itemId: stack.itemId,
358
377
quantity: amount,
@@ -387,6 +406,7 @@ function dropItem (world, sessionId, command, now)
387
406
const placed = placeOrMergeGroundStack(world, {
388
407
tileX: position.tileX,
389
408
tileY: position.tileY,
409
dimensionId: normalizeDimensionId(position.dimensionId ?? OVERWORLD_ID),
390
410
stack: taken.taken,
391
411
now,
392
412
source: "player-drop",
@@ -408,7 +428,9 @@ function pickupItem (world, sessionId, command)
408
428
if (!canCharacterPickupGroundItem(item, command.characterId))
409
429
{ throw userError("That item is no longer on the ground."); }
410
430
const position = world.ecs.get(handle, Component.position);
411
if (position.tileX !== item.tileX || position.tileY !== item.tileY)
431
const playerDim = normalizeDimensionId(position?.dimensionId ?? OVERWORLD_ID);
432
const itemDim = normalizeDimensionId(item.dimensionId ?? OVERWORLD_ID);
433
if (!position || position.tileX !== item.tileX || position.tileY !== item.tileY || playerDim !== itemDim)
412
434
{ throw userError("Stand on the item to pick it up."); }
413
435
const def = itemDefinition(item.stack.itemId);
414
436
if (!def)
@@ -58,6 +58,7 @@ export function publicGroundItem (item)58
groundItemId: item.groundItemId,58
groundItemId: item.groundItemId,59
tileX: item.tileX,59
tileX: item.tileX,60
tileY: item.tileY,60
tileY: item.tileY,61
dimensionId: normalizeDimensionId(item.dimensionId ?? OVERWORLD_ID),61
itemId: item.stack.itemId,62
itemId: item.stack.itemId,62
quantity: item.stack.quantity,63
quantity: item.stack.quantity,63
displayName: itemDisplayName(item.stack),64
displayName: itemDisplayName(item.stack),@@ -208,24 +209,34 @@ export function expireGroundItems (world, now)208
return events;209
return events;209
}210
}210
211
211
export function groundItemsInChunk (world, chunkX, chunkY)212
export function groundItemsInChunk (world, chunkX, chunkY, dimensionId = OVERWORLD_ID)212
{213
{214
const dim = normalizeDimensionId(dimensionId);213
return [...world.groundItems.values()]215
return [...world.groundItems.values()]214
.filter((item) => tileToChunk(item.tileX) === chunkX && tileToChunk(item.tileY) === chunkY)216
.filter((item) =>217
tileToChunk(item.tileX) === chunkX218
&& tileToChunk(item.tileY) === chunkY219
&& normalizeDimensionId(item.dimensionId ?? OVERWORLD_ID) === dim220
)215
.map(publicGroundItem);221
.map(publicGroundItem);216
}222
}217
223
218
export function groundItemsInRegion (world, regionX, regionY, tileToRegion)224
export function groundItemsInRegion (world, regionX, regionY, tileToRegion, dimensionId = OVERWORLD_ID)219
{225
{226
const dim = normalizeDimensionId(dimensionId);220
return [...world.groundItems.values()].filter((item) =>227
return [...world.groundItems.values()].filter((item) =>221
tileToRegion(item.tileX) === regionX && tileToRegion(item.tileY) === regionY228
tileToRegion(item.tileX) === regionX229
&& tileToRegion(item.tileY) === regionY230
&& normalizeDimensionId(item.dimensionId ?? OVERWORLD_ID) === dim222
);231
);223
}232
}224
233
225
export function addGroundItem (world, item)234
export function addGroundItem (world, item)226
{235
{236
const dimensionId = normalizeDimensionId(item.dimensionId ?? OVERWORLD_ID);237
item.dimensionId = dimensionId;227
world.groundItems.set(item.groundItemId, item);238
world.groundItems.set(item.groundItemId, item);228
const key = tileKey(item.tileX, item.tileY);239
const key = tileKey(item.tileX, item.tileY, dimensionId);229
if (!world.groundByTile.has(key))240
if (!world.groundByTile.has(key))230
{ world.groundByTile.set(key, new Set()); }241
{ world.groundByTile.set(key, new Set()); }231
world.groundByTile.get(key).add(item.groundItemId);242
world.groundByTile.get(key).add(item.groundItemId);@@ -237,8 +248,10 @@ export function removeGroundItem (world, groundItemId)237
if (!item)248
if (!item)238
{ return null; }249
{ return null; }239
world.groundItems.delete(groundItemId);250
world.groundItems.delete(groundItemId);240
const key = tileKey(item.tileX, item.tileY);251
const key = tileKey(item.tileX, item.tileY, normalizeDimensionId(item.dimensionId ?? OVERWORLD_ID));241
world.groundByTile.get(key)?.delete(groundItemId);252
world.groundByTile.get(key)?.delete(groundItemId);253
if (world.groundByTile.get(key)?.size === 0)254
{ world.groundByTile.delete(key); }242
return item;255
return item;243
}256
}244
257
@@ -250,10 +263,13 @@ export function replaceGroundItems (world, items)250
{ addGroundItem(world, item); }263
{ addGroundItem(world, item); }251
}264
}252
265
253
export function setGroundItemsForRegion (world, regionX, regionY, items, tileToRegion)266
export function setGroundItemsForRegion (world, regionX, regionY, items, tileToRegion, dimensionId = OVERWORLD_ID)254
{267
{268
const dim = normalizeDimensionId(dimensionId);255
for (const item of [...world.groundItems.values()])269
for (const item of [...world.groundItems.values()])256
{270
{271
if (normalizeDimensionId(item.dimensionId ?? OVERWORLD_ID) !== dim)272
{ continue; }257
if (tileToRegion(item.tileX) === regionX && tileToRegion(item.tileY) === regionY)273
if (tileToRegion(item.tileX) === regionX && tileToRegion(item.tileY) === regionY)258
{ removeGroundItem(world, item.groundItemId); }274
{ removeGroundItem(world, item.groundItemId); }259
}275
}@@ -295,7 +311,8 @@ export function placeOrMergeGroundStack (world, {295
source = "world",311
source = "world",296
droppedByCharacterId = null,312
droppedByCharacterId = null,297
groundItemId = null,313
groundItemId = null,298
despawnDurationMs = null314
despawnDurationMs = null,315
dimensionId = OVERWORLD_ID299
})316
})300
{317
{301
const events = [];318
const events = [];@@ -303,6 +320,7 @@ export function placeOrMergeGroundStack (world, {303
if (!stack || quantity <= 0)320
if (!stack || quantity <= 0)304
{ return { events }; }321
{ return { events }; }305
322
323
const dim = normalizeDimensionId(dimensionId);306
const def = itemDefinition(stack.itemId);324
const def = itemDefinition(stack.itemId);307
const rules = stackRulesFor(def);325
const rules = stackRulesFor(def);308
const duration = despawnDurationMs326
const duration = despawnDurationMs@@ -314,7 +332,7 @@ export function placeOrMergeGroundStack (world, {314
332
315
if (rules.stackable && !rules.uniqueInstance)333
if (rules.stackable && !rules.uniqueInstance)316
{334
{317
const ids = [...(world.groundByTile.get(tileKey(tileX, tileY)) ?? [])];335
const ids = [...(world.groundByTile.get(tileKey(tileX, tileY, dim)) ?? [])];318
for (const id of ids)336
for (const id of ids)319
{337
{320
const existing = world.groundItems.get(id);338
const existing = world.groundItems.get(id);@@ -353,6 +371,7 @@ export function placeOrMergeGroundStack (world, {353
groundItemId: allocateGroundItemId(world, preferredId),371
groundItemId: allocateGroundItemId(world, preferredId),354
tileX,372
tileX,355
tileY,373
tileY,374
dimensionId: dim,356
stack: {375
stack: {357
itemId: stack.itemId,376
itemId: stack.itemId,358
quantity: amount,377
quantity: amount,@@ -387,6 +406,7 @@ function dropItem (world, sessionId, command, now)387
const placed = placeOrMergeGroundStack(world, {406
const placed = placeOrMergeGroundStack(world, {388
tileX: position.tileX,407
tileX: position.tileX,389
tileY: position.tileY,408
tileY: position.tileY,409
dimensionId: normalizeDimensionId(position.dimensionId ?? OVERWORLD_ID),390
stack: taken.taken,410
stack: taken.taken,391
now,411
now,392
source: "player-drop",412
source: "player-drop",@@ -408,7 +428,9 @@ function pickupItem (world, sessionId, command)408
if (!canCharacterPickupGroundItem(item, command.characterId))428
if (!canCharacterPickupGroundItem(item, command.characterId))409
{ throw userError("That item is no longer on the ground."); }429
{ throw userError("That item is no longer on the ground."); }410
const position = world.ecs.get(handle, Component.position);430
const position = world.ecs.get(handle, Component.position);411
if (position.tileX !== item.tileX || position.tileY !== item.tileY)431
const playerDim = normalizeDimensionId(position?.dimensionId ?? OVERWORLD_ID);432
const itemDim = normalizeDimensionId(item.dimensionId ?? OVERWORLD_ID);433
if (!position || position.tileX !== item.tileX || position.tileY !== item.tileY || playerDim !== itemDim)412
{ throw userError("Stand on the item to pick it up."); }434
{ throw userError("Stand on the item to pick it up."); }413
const def = itemDefinition(item.stack.itemId);435
const def = itemDefinition(item.stack.itemId);414
if (!def)436
if (!def)@@ -62,6 +62,7 @@ CREATE TABLE IF NOT EXISTS persistent_entities (
62
62
entity_id INTEGER PRIMARY KEY,
63
63
tile_x INTEGER NOT NULL,
64
64
tile_y INTEGER NOT NULL,
65
dimension_id TEXT NOT NULL DEFAULT 'overworld',
65
66
components_blob TEXT NOT NULL
66
67
);
67
68
@@ -85,6 +86,7 @@ CREATE TABLE IF NOT EXISTS ground_items (
85
86
ground_item_id INTEGER PRIMARY KEY,
86
87
tile_x INTEGER NOT NULL,
87
88
tile_y INTEGER NOT NULL,
89
dimension_id TEXT NOT NULL DEFAULT 'overworld',
88
90
stack_blob TEXT NOT NULL,
89
91
dropped_at INTEGER NOT NULL,
90
92
expires_at INTEGER NOT NULL,
@@ -274,6 +276,12 @@ export class StateDatabase
274
276
this.db.exec("ALTER TABLE characters ADD COLUMN respawn_altar_y INTEGER");
275
277
this.db.exec("ALTER TABLE characters ADD COLUMN respawn_altar_dimension_id TEXT");
276
278
}
279
const groundColumns = this.db.prepare("PRAGMA table_info(ground_items)").all();
280
if (!groundColumns.some((column) => column.name === "dimension_id"))
281
{ this.db.exec("ALTER TABLE ground_items ADD COLUMN dimension_id TEXT NOT NULL DEFAULT 'overworld'"); }
282
const persistentColumns = this.db.prepare("PRAGMA table_info(persistent_entities)").all();
283
if (!persistentColumns.some((column) => column.name === "dimension_id"))
284
{ this.db.exec("ALTER TABLE persistent_entities ADD COLUMN dimension_id TEXT NOT NULL DEFAULT 'overworld'"); }
277
285
const marketColumns = this.db.prepare("PRAGMA table_info(market_orders)").all();
278
286
if (!marketColumns.some((column) => column.name === "listing_key"))
279
287
{ this.db.exec("ALTER TABLE market_orders ADD COLUMN listing_key TEXT"); }
@@ -1334,27 +1342,31 @@ export class StateDatabase
1334
1342
`).run(hourKey | 0, characterId | 0);
1335
1343
}
1336
1344
1337
listPersistentInRegion (minX, minY, maxX, maxY)
1345
listPersistentInRegion (minX, minY, maxX, maxY, dimensionId = "overworld")
1338
1346
{
1347
const dim = String(dimensionId ?? "overworld");
1339
1348
return this.db.prepare(`
1340
SELECT entity_id, tile_x, tile_y, components_blob
1349
SELECT entity_id, tile_x, tile_y, dimension_id, components_blob
1341
1350
FROM persistent_entities
1342
1351
WHERE tile_x >= ? AND tile_x <= ? AND tile_y >= ? AND tile_y <= ?
1343
`).all(minX, maxX, minY, maxY);
1352
AND dimension_id = ?
1353
`).all(minX, maxX, minY, maxY, dim);
1344
1354
}
1345
1355
1346
replacePersistentInRegion (minX, minY, maxX, maxY, entities)
1356
replacePersistentInRegion (minX, minY, maxX, maxY, entities, dimensionId = "overworld")
1347
1357
{
1358
const dim = String(dimensionId ?? "overworld");
1348
1359
this.db.exec("BEGIN");
1349
1360
try
1350
1361
{
1351
1362
this.db.prepare(`
1352
1363
DELETE FROM persistent_entities
1353
1364
WHERE tile_x >= ? AND tile_x <= ? AND tile_y >= ? AND tile_y <= ?
1354
`).run(minX, maxX, minY, maxY);
1365
AND dimension_id = ?
1366
`).run(minX, maxX, minY, maxY, dim);
1355
1367
const insert = this.db.prepare(`
1356
INSERT INTO persistent_entities (entity_id, tile_x, tile_y, components_blob)
1357
VALUES (?, ?, ?, ?)
1368
INSERT INTO persistent_entities (entity_id, tile_x, tile_y, dimension_id, components_blob)
1369
VALUES (?, ?, ?, ?, ?)
1358
1370
`);
1359
1371
for (const entity of entities)
1360
1372
{
@@ -1362,6 +1374,7 @@ export class StateDatabase
1362
1374
entity.entityId,
1363
1375
entity.tileX,
1364
1376
entity.tileY,
1377
String(entity.dimensionId ?? dim),
1365
1378
JSON.stringify(entity.payload?.components ?? entity.components)
1366
1379
);
1367
1380
}
@@ -1581,23 +1594,26 @@ export class StateDatabase
1581
1594
return (row?.id ?? 0) + 1;
1582
1595
}
1583
1596
1584
listGroundItemsInRegion (minX, minY, maxX, maxY)
1597
listGroundItemsInRegion (minX, minY, maxX, maxY, dimensionId = "overworld")
1585
1598
{
1599
const dim = String(dimensionId ?? "overworld");
1586
1600
return this.db.prepare(`
1587
1601
SELECT * FROM ground_items
1588
1602
WHERE tile_x >= ? AND tile_x <= ? AND tile_y >= ? AND tile_y <= ?
1589
`).all(minX, maxX, minY, maxY).map(parseGroundItem);
1603
AND dimension_id = ?
1604
`).all(minX, maxX, minY, maxY, dim).map(parseGroundItem);
1590
1605
}
1591
1606
1592
1607
upsertGroundItem (item)
1593
1608
{
1594
1609
this.db.prepare(`
1595
1610
INSERT INTO ground_items (
1596
ground_item_id, tile_x, tile_y, stack_blob, dropped_at, expires_at, source, dropped_by_character_id
1597
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
1611
ground_item_id, tile_x, tile_y, dimension_id, stack_blob, dropped_at, expires_at, source, dropped_by_character_id
1612
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
1598
1613
ON CONFLICT(ground_item_id) DO UPDATE SET
1599
1614
tile_x = excluded.tile_x,
1600
1615
tile_y = excluded.tile_y,
1616
dimension_id = excluded.dimension_id,
1601
1617
stack_blob = excluded.stack_blob,
1602
1618
dropped_at = excluded.dropped_at,
1603
1619
expires_at = excluded.expires_at,
@@ -1607,6 +1623,7 @@ export class StateDatabase
1607
1623
item.groundItemId,
1608
1624
item.tileX,
1609
1625
item.tileY,
1626
String(item.dimensionId ?? "overworld"),
1610
1627
JSON.stringify(item.stack),
1611
1628
item.droppedAt,
1612
1629
item.expiresAt,
@@ -1922,6 +1939,7 @@ function parseGroundItem (row)
1922
1939
groundItemId: row.ground_item_id,
1923
1940
tileX: row.tile_x,
1924
1941
tileY: row.tile_y,
1942
dimensionId: String(row.dimension_id ?? "overworld"),
1925
1943
stack: JSON.parse(row.stack_blob),
1926
1944
droppedAt: row.dropped_at,
1927
1945
expiresAt: row.expires_at,
@@ -62,6 +62,7 @@ CREATE TABLE IF NOT EXISTS persistent_entities (62
entity_id INTEGER PRIMARY KEY,62
entity_id INTEGER PRIMARY KEY,63
tile_x INTEGER NOT NULL,63
tile_x INTEGER NOT NULL,64
tile_y INTEGER NOT NULL,64
tile_y INTEGER NOT NULL,65
dimension_id TEXT NOT NULL DEFAULT 'overworld',65
components_blob TEXT NOT NULL66
components_blob TEXT NOT NULL66
);67
);67
68
@@ -85,6 +86,7 @@ CREATE TABLE IF NOT EXISTS ground_items (85
ground_item_id INTEGER PRIMARY KEY,86
ground_item_id INTEGER PRIMARY KEY,86
tile_x INTEGER NOT NULL,87
tile_x INTEGER NOT NULL,87
tile_y INTEGER NOT NULL,88
tile_y INTEGER NOT NULL,89
dimension_id TEXT NOT NULL DEFAULT 'overworld',88
stack_blob TEXT NOT NULL,90
stack_blob TEXT NOT NULL,89
dropped_at INTEGER NOT NULL,91
dropped_at INTEGER NOT NULL,90
expires_at INTEGER NOT NULL,92
expires_at INTEGER NOT NULL,@@ -274,6 +276,12 @@ export class StateDatabase274
this.db.exec("ALTER TABLE characters ADD COLUMN respawn_altar_y INTEGER");276
this.db.exec("ALTER TABLE characters ADD COLUMN respawn_altar_y INTEGER");275
this.db.exec("ALTER TABLE characters ADD COLUMN respawn_altar_dimension_id TEXT");277
this.db.exec("ALTER TABLE characters ADD COLUMN respawn_altar_dimension_id TEXT");276
}278
}279
const groundColumns = this.db.prepare("PRAGMA table_info(ground_items)").all();280
if (!groundColumns.some((column) => column.name === "dimension_id"))281
{ this.db.exec("ALTER TABLE ground_items ADD COLUMN dimension_id TEXT NOT NULL DEFAULT 'overworld'"); }282
const persistentColumns = this.db.prepare("PRAGMA table_info(persistent_entities)").all();283
if (!persistentColumns.some((column) => column.name === "dimension_id"))284
{ this.db.exec("ALTER TABLE persistent_entities ADD COLUMN dimension_id TEXT NOT NULL DEFAULT 'overworld'"); }277
const marketColumns = this.db.prepare("PRAGMA table_info(market_orders)").all();285
const marketColumns = this.db.prepare("PRAGMA table_info(market_orders)").all();278
if (!marketColumns.some((column) => column.name === "listing_key"))286
if (!marketColumns.some((column) => column.name === "listing_key"))279
{ this.db.exec("ALTER TABLE market_orders ADD COLUMN listing_key TEXT"); }287
{ this.db.exec("ALTER TABLE market_orders ADD COLUMN listing_key TEXT"); }@@ -1334,27 +1342,31 @@ export class StateDatabase1334
`).run(hourKey | 0, characterId | 0);1342
`).run(hourKey | 0, characterId | 0);1335
}1343
}1336
1344
1337
listPersistentInRegion (minX, minY, maxX, maxY)1345
listPersistentInRegion (minX, minY, maxX, maxY, dimensionId = "overworld")1338
{1346
{1347
const dim = String(dimensionId ?? "overworld");1339
return this.db.prepare(`1348
return this.db.prepare(`1340
SELECT entity_id, tile_x, tile_y, components_blob1349
SELECT entity_id, tile_x, tile_y, dimension_id, components_blob1341
FROM persistent_entities1350
FROM persistent_entities1342
WHERE tile_x >= ? AND tile_x <= ? AND tile_y >= ? AND tile_y <= ?1351
WHERE tile_x >= ? AND tile_x <= ? AND tile_y >= ? AND tile_y <= ?1343
`).all(minX, maxX, minY, maxY);1352
AND dimension_id = ?1353
`).all(minX, maxX, minY, maxY, dim);1344
}1354
}1345
1355
1346
replacePersistentInRegion (minX, minY, maxX, maxY, entities)1356
replacePersistentInRegion (minX, minY, maxX, maxY, entities, dimensionId = "overworld")1347
{1357
{1358
const dim = String(dimensionId ?? "overworld");1348
this.db.exec("BEGIN");1359
this.db.exec("BEGIN");1349
try1360
try1350
{1361
{1351
this.db.prepare(`1362
this.db.prepare(`1352
DELETE FROM persistent_entities1363
DELETE FROM persistent_entities1353
WHERE tile_x >= ? AND tile_x <= ? AND tile_y >= ? AND tile_y <= ?1364
WHERE tile_x >= ? AND tile_x <= ? AND tile_y >= ? AND tile_y <= ?1354
`).run(minX, maxX, minY, maxY);1365
AND dimension_id = ?1366
`).run(minX, maxX, minY, maxY, dim);1355
const insert = this.db.prepare(`1367
const insert = this.db.prepare(`1356
INSERT INTO persistent_entities (entity_id, tile_x, tile_y, components_blob)1368
INSERT INTO persistent_entities (entity_id, tile_x, tile_y, dimension_id, components_blob)1357
VALUES (?, ?, ?, ?)1369
VALUES (?, ?, ?, ?, ?)1358
`);1370
`);1359
for (const entity of entities)1371
for (const entity of entities)1360
{1372
{@@ -1362,6 +1374,7 @@ export class StateDatabase1362
entity.entityId,1374
entity.entityId,1363
entity.tileX,1375
entity.tileX,1364
entity.tileY,1376
entity.tileY,1377
String(entity.dimensionId ?? dim),1365
JSON.stringify(entity.payload?.components ?? entity.components)1378
JSON.stringify(entity.payload?.components ?? entity.components)1366
);1379
);1367
}1380
}@@ -1581,23 +1594,26 @@ export class StateDatabase1581
return (row?.id ?? 0) + 1;1594
return (row?.id ?? 0) + 1;1582
}1595
}1583
1596
1584
listGroundItemsInRegion (minX, minY, maxX, maxY)1597
listGroundItemsInRegion (minX, minY, maxX, maxY, dimensionId = "overworld")1585
{1598
{1599
const dim = String(dimensionId ?? "overworld");1586
return this.db.prepare(`1600
return this.db.prepare(`1587
SELECT * FROM ground_items1601
SELECT * FROM ground_items1588
WHERE tile_x >= ? AND tile_x <= ? AND tile_y >= ? AND tile_y <= ?1602
WHERE tile_x >= ? AND tile_x <= ? AND tile_y >= ? AND tile_y <= ?1589
`).all(minX, maxX, minY, maxY).map(parseGroundItem);1603
AND dimension_id = ?1604
`).all(minX, maxX, minY, maxY, dim).map(parseGroundItem);1590
}1605
}1591
1606
1592
upsertGroundItem (item)1607
upsertGroundItem (item)1593
{1608
{1594
this.db.prepare(`1609
this.db.prepare(`1595
INSERT INTO ground_items (1610
INSERT INTO ground_items (1596
ground_item_id, tile_x, tile_y, stack_blob, dropped_at, expires_at, source, dropped_by_character_id1611
ground_item_id, tile_x, tile_y, dimension_id, stack_blob, dropped_at, expires_at, source, dropped_by_character_id1597
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)1612
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)1598
ON CONFLICT(ground_item_id) DO UPDATE SET1613
ON CONFLICT(ground_item_id) DO UPDATE SET1599
tile_x = excluded.tile_x,1614
tile_x = excluded.tile_x,1600
tile_y = excluded.tile_y,1615
tile_y = excluded.tile_y,1616
dimension_id = excluded.dimension_id,1601
stack_blob = excluded.stack_blob,1617
stack_blob = excluded.stack_blob,1602
dropped_at = excluded.dropped_at,1618
dropped_at = excluded.dropped_at,1603
expires_at = excluded.expires_at,1619
expires_at = excluded.expires_at,@@ -1607,6 +1623,7 @@ export class StateDatabase1607
item.groundItemId,1623
item.groundItemId,1608
item.tileX,1624
item.tileX,1609
item.tileY,1625
item.tileY,1626
String(item.dimensionId ?? "overworld"),1610
JSON.stringify(item.stack),1627
JSON.stringify(item.stack),1611
item.droppedAt,1628
item.droppedAt,1612
item.expiresAt,1629
item.expiresAt,@@ -1922,6 +1939,7 @@ function parseGroundItem (row)1922
groundItemId: row.ground_item_id,1939
groundItemId: row.ground_item_id,1923
tileX: row.tile_x,1940
tileX: row.tile_x,1924
tileY: row.tile_y,1941
tileY: row.tile_y,1942
dimensionId: String(row.dimension_id ?? "overworld"),1925
stack: JSON.parse(row.stack_blob),1943
stack: JSON.parse(row.stack_blob),1926
droppedAt: row.dropped_at,1944
droppedAt: row.dropped_at,1927
expiresAt: row.expires_at,1945
expiresAt: row.expires_at,@@ -15,6 +15,7 @@ import { isAdjacentTile } from "../../../shared/navigation/adjacency.js";
15
15
import { publicEntityView, replicationAppearance } from "../ecs/serialize.js";
16
16
import { scaleRegrowDurationMs } from "../../../shared/world/season.js";
17
17
import { tileShoveEvent } from "../simulation/tileInteract.js";
18
import { OVERWORLD_ID, normalizeDimensionId } from "../../../shared/world/dimensions.js";
18
19
19
20
const SHEAR_DURATION_MS = 3000;
20
21
const SHEAR_SHOVE_MS = 1000;
@@ -346,7 +347,8 @@ export function stepShearing (world, now)
346
347
position.tileX,
347
348
position.tileY,
348
349
targetPos.tileX,
349
targetPos.tileY
350
targetPos.tileY,
351
normalizeDimensionId(position.dimensionId ?? OVERWORLD_ID)
350
352
));
351
353
events.push({
352
354
type: "gather-swing",
@@ -355,6 +357,7 @@ export function stepShearing (world, now)
355
357
tileY: targetPos.tileY,
356
358
fromTileX: position.tileX,
357
359
fromTileY: position.tileY,
360
dimensionId: normalizeDimensionId(position.dimensionId ?? OVERWORLD_ID),
358
361
toolItemId: session.toolItemId,
359
362
swipe: false
360
363
});
@@ -15,6 +15,7 @@ import { isAdjacentTile } from "../../../shared/navigation/adjacency.js";15
import { publicEntityView, replicationAppearance } from "../ecs/serialize.js";15
import { publicEntityView, replicationAppearance } from "../ecs/serialize.js";16
import { scaleRegrowDurationMs } from "../../../shared/world/season.js";16
import { scaleRegrowDurationMs } from "../../../shared/world/season.js";17
import { tileShoveEvent } from "../simulation/tileInteract.js";17
import { tileShoveEvent } from "../simulation/tileInteract.js";18
import { OVERWORLD_ID, normalizeDimensionId } from "../../../shared/world/dimensions.js";18
19
19
const SHEAR_DURATION_MS = 3000;20
const SHEAR_DURATION_MS = 3000;20
const SHEAR_SHOVE_MS = 1000;21
const SHEAR_SHOVE_MS = 1000;@@ -346,7 +347,8 @@ export function stepShearing (world, now)346
position.tileX,347
position.tileX,347
position.tileY,348
position.tileY,348
targetPos.tileX,349
targetPos.tileX,349
targetPos.tileY350
targetPos.tileY,351
normalizeDimensionId(position.dimensionId ?? OVERWORLD_ID)350
));352
));351
events.push({353
events.push({352
type: "gather-swing",354
type: "gather-swing",@@ -355,6 +357,7 @@ export function stepShearing (world, now)355
tileY: targetPos.tileY,357
tileY: targetPos.tileY,356
fromTileX: position.tileX,358
fromTileX: position.tileX,357
fromTileY: position.tileY,359
fromTileY: position.tileY,360
dimensionId: normalizeDimensionId(position.dimensionId ?? OVERWORLD_ID),358
toolItemId: session.toolItemId,361
toolItemId: session.toolItemId,359
swipe: false362
swipe: false360
});363
});@@ -2298,6 +2298,7 @@ export class Simulation
2298
2298
characterId: event.characterId,
2299
2299
tileX: event.tileX,
2300
2300
tileY: event.tileY,
2301
dimensionId: record.dimensionId ?? OVERWORLD_ID,
2301
2302
now: this.now,
2302
2303
dropCarriedCoins: true,
2303
2304
coinGroundItemId: this.nextGroundItemId++,
@@ -2331,6 +2332,7 @@ export class Simulation
2331
2332
entityId: event.entityId,
2332
2333
tileX: event.tileX,
2333
2334
tileY: event.tileY,
2335
dimensionId: record.dimensionId ?? OVERWORLD_ID,
2334
2336
cause: "death"
2335
2337
});
2336
2338
for (const dropEvent of result?.events ?? [])
@@ -2957,8 +2959,8 @@ export class Simulation
2957
2959
chunks: this.#regionChunkBlobs(regionX, regionY, dim)
2958
2960
});
2959
2961
this.regionOwner.set(key, workerId);
2960
this.#restorePersistent(regionX, regionY, workerId);
2961
this.#restoreGroundItems(regionX, regionY, workerId);
2962
this.#restorePersistent(regionX, regionY, workerId, dim);
2963
this.#restoreGroundItems(regionX, regionY, workerId, dim);
2962
2964
return workerId;
2963
2965
}
2964
2966
@@ -2993,17 +2995,31 @@ export class Simulation
2993
2995
{ persistent.push(record); }
2994
2996
this.#forget(record.entityId);
2995
2997
}
2996
this.stateDb.replacePersistentInRegion(bounds.minX, bounds.minY, bounds.maxX, bounds.maxY, persistent);
2998
this.stateDb.replacePersistentInRegion(
2999
bounds.minX,
3000
bounds.minY,
3001
bounds.maxX,
3002
bounds.maxY,
3003
persistent,
3004
dim
3005
);
2997
3006
for (const item of groundItems ?? [])
2998
3007
{ this.stateDb.upsertGroundItem(item); }
2999
3008
this.regionOwner.delete(key);
3000
3009
this.regionGraceUntil.delete(key);
3001
3010
}
3002
3011
3003
#restorePersistent (regionX, regionY, workerId)
3012
#restorePersistent (regionX, regionY, workerId, dimensionId = OVERWORLD_ID)
3004
3013
{
3014
const dim = normalizeDimensionId(dimensionId);
3005
3015
const bounds = regionTileBounds(regionX, regionY);
3006
const rows = this.stateDb.listPersistentInRegion(bounds.minX, bounds.minY, bounds.maxX, bounds.maxY);
3016
const rows = this.stateDb.listPersistentInRegion(
3017
bounds.minX,
3018
bounds.minY,
3019
bounds.maxX,
3020
bounds.maxY,
3021
dim
3022
);
3007
3023
for (const row of rows)
3008
3024
{
3009
3025
if (this.directory.has(row.entity_id))
@@ -3321,11 +3337,18 @@ export class Simulation
3321
3337
return events;
3322
3338
}
3323
3339
3324
#restoreGroundItems (regionX, regionY, workerId)
3340
#restoreGroundItems (regionX, regionY, workerId, dimensionId = OVERWORLD_ID)
3325
3341
{
3342
const dim = normalizeDimensionId(dimensionId);
3326
3343
const bounds = regionTileBounds(regionX, regionY);
3327
3344
const items = [];
3328
for (const item of this.stateDb.listGroundItemsInRegion(bounds.minX, bounds.minY, bounds.maxX, bounds.maxY))
3345
for (const item of this.stateDb.listGroundItemsInRegion(
3346
bounds.minX,
3347
bounds.minY,
3348
bounds.maxX,
3349
bounds.maxY,
3350
dim
3351
))
3329
3352
{
3330
3353
if (item.expiresAt > this.now)
3331
3354
{
@@ -3340,6 +3363,7 @@ export class Simulation
3340
3363
type: WorkerMessage.setGroundItems,
3341
3364
regionX,
3342
3365
regionY,
3366
dimensionId: dim,
3343
3367
items
3344
3368
});
3345
3369
}
@@ -3401,6 +3425,7 @@ export class Simulation
3401
3425
groundItemId: event.groundItemId,
3402
3426
tileX: event.tileX,
3403
3427
tileY: event.tileY,
3428
dimensionId: event.dimensionId ?? OVERWORLD_ID,
3404
3429
stack: event.stack ?? { itemId: event.itemId, quantity: event.quantity },
3405
3430
droppedAt: event.droppedAt ?? this.now,
3406
3431
expiresAt: event.expiresAt,
@@ -2298,6 +2298,7 @@ export class Simulation2298
characterId: event.characterId,2298
characterId: event.characterId,2299
tileX: event.tileX,2299
tileX: event.tileX,2300
tileY: event.tileY,2300
tileY: event.tileY,2301
dimensionId: record.dimensionId ?? OVERWORLD_ID,2301
now: this.now,2302
now: this.now,2302
dropCarriedCoins: true,2303
dropCarriedCoins: true,2303
coinGroundItemId: this.nextGroundItemId++,2304
coinGroundItemId: this.nextGroundItemId++,@@ -2331,6 +2332,7 @@ export class Simulation2331
entityId: event.entityId,2332
entityId: event.entityId,2332
tileX: event.tileX,2333
tileX: event.tileX,2333
tileY: event.tileY,2334
tileY: event.tileY,2335
dimensionId: record.dimensionId ?? OVERWORLD_ID,2334
cause: "death"2336
cause: "death"2335
});2337
});2336
for (const dropEvent of result?.events ?? [])2338
for (const dropEvent of result?.events ?? [])@@ -2957,8 +2959,8 @@ export class Simulation2957
chunks: this.#regionChunkBlobs(regionX, regionY, dim)2959
chunks: this.#regionChunkBlobs(regionX, regionY, dim)2958
});2960
});2959
this.regionOwner.set(key, workerId);2961
this.regionOwner.set(key, workerId);2960
this.#restorePersistent(regionX, regionY, workerId);2962
this.#restorePersistent(regionX, regionY, workerId, dim);2961
this.#restoreGroundItems(regionX, regionY, workerId);2963
this.#restoreGroundItems(regionX, regionY, workerId, dim);2962
return workerId;2964
return workerId;2963
}2965
}2964
2966
@@ -2993,17 +2995,31 @@ export class Simulation2993
{ persistent.push(record); }2995
{ persistent.push(record); }2994
this.#forget(record.entityId);2996
this.#forget(record.entityId);2995
}2997
}2996
this.stateDb.replacePersistentInRegion(bounds.minX, bounds.minY, bounds.maxX, bounds.maxY, persistent);2998
this.stateDb.replacePersistentInRegion(2999
bounds.minX,3000
bounds.minY,3001
bounds.maxX,3002
bounds.maxY,3003
persistent,3004
dim3005
);2997
for (const item of groundItems ?? [])3006
for (const item of groundItems ?? [])2998
{ this.stateDb.upsertGroundItem(item); }3007
{ this.stateDb.upsertGroundItem(item); }2999
this.regionOwner.delete(key);3008
this.regionOwner.delete(key);3000
this.regionGraceUntil.delete(key);3009
this.regionGraceUntil.delete(key);3001
}3010
}3002
3011
3003
#restorePersistent (regionX, regionY, workerId)3012
#restorePersistent (regionX, regionY, workerId, dimensionId = OVERWORLD_ID)3004
{3013
{3014
const dim = normalizeDimensionId(dimensionId);3005
const bounds = regionTileBounds(regionX, regionY);3015
const bounds = regionTileBounds(regionX, regionY);3006
const rows = this.stateDb.listPersistentInRegion(bounds.minX, bounds.minY, bounds.maxX, bounds.maxY);3016
const rows = this.stateDb.listPersistentInRegion(3017
bounds.minX,3018
bounds.minY,3019
bounds.maxX,3020
bounds.maxY,3021
dim3022
);3007
for (const row of rows)3023
for (const row of rows)3008
{3024
{3009
if (this.directory.has(row.entity_id))3025
if (this.directory.has(row.entity_id))@@ -3321,11 +3337,18 @@ export class Simulation3321
return events;3337
return events;3322
}3338
}3323
3339
3324
#restoreGroundItems (regionX, regionY, workerId)3340
#restoreGroundItems (regionX, regionY, workerId, dimensionId = OVERWORLD_ID)3325
{3341
{3342
const dim = normalizeDimensionId(dimensionId);3326
const bounds = regionTileBounds(regionX, regionY);3343
const bounds = regionTileBounds(regionX, regionY);3327
const items = [];3344
const items = [];3328
for (const item of this.stateDb.listGroundItemsInRegion(bounds.minX, bounds.minY, bounds.maxX, bounds.maxY))3345
for (const item of this.stateDb.listGroundItemsInRegion(3346
bounds.minX,3347
bounds.minY,3348
bounds.maxX,3349
bounds.maxY,3350
dim3351
))3329
{3352
{3330
if (item.expiresAt > this.now)3353
if (item.expiresAt > this.now)3331
{3354
{@@ -3340,6 +3363,7 @@ export class Simulation3340
type: WorkerMessage.setGroundItems,3363
type: WorkerMessage.setGroundItems,3341
regionX,3364
regionX,3342
regionY,3365
regionY,3366
dimensionId: dim,3343
items3367
items3344
});3368
});3345
}3369
}@@ -3401,6 +3425,7 @@ export class Simulation3401
groundItemId: event.groundItemId,3425
groundItemId: event.groundItemId,3402
tileX: event.tileX,3426
tileX: event.tileX,3403
tileY: event.tileY,3427
tileY: event.tileY,3428
dimensionId: event.dimensionId ?? OVERWORLD_ID,3404
stack: event.stack ?? { itemId: event.itemId, quantity: event.quantity },3429
stack: event.stack ?? { itemId: event.itemId, quantity: event.quantity },3405
droppedAt: event.droppedAt ?? this.now,3430
droppedAt: event.droppedAt ?? this.now,3406
expiresAt: event.expiresAt,3431
expiresAt: event.expiresAt,@@ -48,10 +48,14 @@ export function collectSpawnRequests (world, now = Date.now())
48
48
const localX = entry.localX;
49
49
const localY = entry.localY;
50
50
const index = localY * CHUNK_SIZE + localX;
51
const config = normalizeSpawnerConfig(
52
entry.config,
53
`${chunkX}:${chunkY}:${index}`
54
);
51
const rawSpawnerId = typeof entry.config?.spawnerId === "string" && entry.config.spawnerId
52
? entry.config.spawnerId
53
: `${chunkX}:${chunkY}:${index}`;
54
const scopedSpawnerId = rawSpawnerId.startsWith(`${dimensionId}:`)
55
? rawSpawnerId
56
: `${dimensionId}:${rawSpawnerId}`;
57
const config = normalizeSpawnerConfig(entry.config, scopedSpawnerId);
58
config.spawnerId = scopedSpawnerId;
55
59
const flying = !!mobDefinition(config.mobTypeId)?.flying;
56
60
const originX = chunkX * CHUNK_SIZE + localX;
57
61
const originY = chunkY * CHUNK_SIZE + localY;
@@ -48,10 +48,14 @@ export function collectSpawnRequests (world, now = Date.now())48
const localX = entry.localX;48
const localX = entry.localX;49
const localY = entry.localY;49
const localY = entry.localY;50
const index = localY * CHUNK_SIZE + localX;50
const index = localY * CHUNK_SIZE + localX;51
const config = normalizeSpawnerConfig(51
const rawSpawnerId = typeof entry.config?.spawnerId === "string" && entry.config.spawnerId52
entry.config,52
? entry.config.spawnerId53
`${chunkX}:${chunkY}:${index}`53
: `${chunkX}:${chunkY}:${index}`;54
);54
const scopedSpawnerId = rawSpawnerId.startsWith(`${dimensionId}:`)55
? rawSpawnerId56
: `${dimensionId}:${rawSpawnerId}`;57
const config = normalizeSpawnerConfig(entry.config, scopedSpawnerId);58
config.spawnerId = scopedSpawnerId;55
const flying = !!mobDefinition(config.mobTypeId)?.flying;59
const flying = !!mobDefinition(config.mobTypeId)?.flying;56
const originX = chunkX * CHUNK_SIZE + localX;60
const originX = chunkX * CHUNK_SIZE + localX;57
const originY = chunkY * CHUNK_SIZE + localY;61
const originY = chunkY * CHUNK_SIZE + localY;@@ -27,7 +27,7 @@ export function findAdjacentBlockTile (world, tileX, tileY, blockType, dimension
27
27
}
28
28
29
29
/** Shove the actor toward a world tile (bank, furnace, mine, craft, …). */
30
export function tileShoveEvent (entityId, fromTileX, fromTileY, tileX, tileY)
30
export function tileShoveEvent (entityId, fromTileX, fromTileY, tileX, tileY, dimensionId = OVERWORLD_ID)
31
31
{
32
32
return {
33
33
type: "tile-shove",
@@ -35,7 +35,8 @@ export function tileShoveEvent (entityId, fromTileX, fromTileY, tileX, tileY)
35
35
fromTileX,
36
36
fromTileY,
37
37
tileX,
38
tileY
38
tileY,
39
dimensionId: normalizeDimensionId(dimensionId)
39
40
};
40
41
}
41
42
@@ -48,5 +49,5 @@ export function tileShoveTowardBlock (world, handle, entityId, blockType)
48
49
const target = findAdjacentBlockTile(world, position.tileX, position.tileY, blockType, dim);
49
50
if (!target)
50
51
{ return null; }
51
return tileShoveEvent(entityId, position.tileX, position.tileY, target.tileX, target.tileY);
52
return tileShoveEvent(entityId, position.tileX, position.tileY, target.tileX, target.tileY, dim);
52
53
}
@@ -27,7 +27,7 @@ export function findAdjacentBlockTile (world, tileX, tileY, blockType, dimension27
}27
}28
28
29
/** Shove the actor toward a world tile (bank, furnace, mine, craft, …). */29
/** Shove the actor toward a world tile (bank, furnace, mine, craft, …). */30
export function tileShoveEvent (entityId, fromTileX, fromTileY, tileX, tileY)30
export function tileShoveEvent (entityId, fromTileX, fromTileY, tileX, tileY, dimensionId = OVERWORLD_ID)31
{31
{32
return {32
return {33
type: "tile-shove",33
type: "tile-shove",@@ -35,7 +35,8 @@ export function tileShoveEvent (entityId, fromTileX, fromTileY, tileX, tileY)35
fromTileX,35
fromTileX,36
fromTileY,36
fromTileY,37
tileX,37
tileX,38
tileY38
tileY,39
dimensionId: normalizeDimensionId(dimensionId)39
};40
};40
}41
}41
42
@@ -48,5 +49,5 @@ export function tileShoveTowardBlock (world, handle, entityId, blockType)48
const target = findAdjacentBlockTile(world, position.tileX, position.tileY, blockType, dim);49
const target = findAdjacentBlockTile(world, position.tileX, position.tileY, blockType, dim);49
if (!target)50
if (!target)50
{ return null; }51
{ return null; }51
return tileShoveEvent(entityId, position.tileX, position.tileY, target.tileX, target.tileY);52
return tileShoveEvent(entityId, position.tileX, position.tileY, target.tileX, target.tileY, dim);52
}53
}@@ -174,7 +174,14 @@ export class WorkerWorld
174
174
case WorkerMessage.stepSpawners:
175
175
return { spawnRequests: collectSpawnRequests(this, Date.now()) };
176
176
case WorkerMessage.setGroundItems:
177
setGroundItemsForRegion(this, message.regionX, message.regionY, message.items, tileToRegion);
177
setGroundItemsForRegion(
178
this,
179
message.regionX,
180
message.regionY,
181
message.items,
182
tileToRegion,
183
message.dimensionId ?? OVERWORLD_ID
184
);
178
185
return { ok: true };
179
186
case WorkerMessage.inspectBag:
180
187
return this.#inspectBag(message.entityId);
@@ -354,7 +361,13 @@ export class WorkerWorld
354
361
dropped.push(record);
355
362
this.#destroyEntity(record.entityId);
356
363
}
357
const groundItems = groundItemsInRegion(this, message.regionX, message.regionY, tileToRegion);
364
const groundItems = groundItemsInRegion(
365
this,
366
message.regionX,
367
message.regionY,
368
tileToRegion,
369
dim
370
);
358
371
for (const item of groundItems)
359
372
{ removeGroundItem(this, item.groundItemId); }
360
373
for (const [key, chunk] of [...this.chunks.entries()])
@@ -630,6 +643,7 @@ export class WorkerWorld
630
643
const placed = placeOrMergeGroundStack(this, {
631
644
tileX: message.tileX,
632
645
tileY: message.tileY,
646
dimensionId: normalizeDimensionId(message.dimensionId ?? this.#entityDimensionId(message.entityId)),
633
647
stack,
634
648
now: message.now,
635
649
source: "player-death",
@@ -2348,7 +2362,7 @@ export class WorkerWorld
2348
2362
light: data.light,
2349
2363
occlusion: data.occlusion,
2350
2364
entities,
2351
groundItems: groundItemsInChunk(this, chunkX, chunkY)
2365
groundItems: groundItemsInChunk(this, chunkX, chunkY, dim)
2352
2366
};
2353
2367
}
2354
2368
@@ -174,7 +174,14 @@ export class WorkerWorld174
case WorkerMessage.stepSpawners:174
case WorkerMessage.stepSpawners:175
return { spawnRequests: collectSpawnRequests(this, Date.now()) };175
return { spawnRequests: collectSpawnRequests(this, Date.now()) };176
case WorkerMessage.setGroundItems:176
case WorkerMessage.setGroundItems:177
setGroundItemsForRegion(this, message.regionX, message.regionY, message.items, tileToRegion);177
setGroundItemsForRegion(178
this,179
message.regionX,180
message.regionY,181
message.items,182
tileToRegion,183
message.dimensionId ?? OVERWORLD_ID184
);178
return { ok: true };185
return { ok: true };179
case WorkerMessage.inspectBag:186
case WorkerMessage.inspectBag:180
return this.#inspectBag(message.entityId);187
return this.#inspectBag(message.entityId);@@ -354,7 +361,13 @@ export class WorkerWorld354
dropped.push(record);361
dropped.push(record);355
this.#destroyEntity(record.entityId);362
this.#destroyEntity(record.entityId);356
}363
}357
const groundItems = groundItemsInRegion(this, message.regionX, message.regionY, tileToRegion);364
const groundItems = groundItemsInRegion(365
this,366
message.regionX,367
message.regionY,368
tileToRegion,369
dim370
);358
for (const item of groundItems)371
for (const item of groundItems)359
{ removeGroundItem(this, item.groundItemId); }372
{ removeGroundItem(this, item.groundItemId); }360
for (const [key, chunk] of [...this.chunks.entries()])373
for (const [key, chunk] of [...this.chunks.entries()])@@ -630,6 +643,7 @@ export class WorkerWorld630
const placed = placeOrMergeGroundStack(this, {643
const placed = placeOrMergeGroundStack(this, {631
tileX: message.tileX,644
tileX: message.tileX,632
tileY: message.tileY,645
tileY: message.tileY,646
dimensionId: normalizeDimensionId(message.dimensionId ?? this.#entityDimensionId(message.entityId)),633
stack,647
stack,634
now: message.now,648
now: message.now,635
source: "player-death",649
source: "player-death",@@ -2348,7 +2362,7 @@ export class WorkerWorld2348
light: data.light,2362
light: data.light,2349
occlusion: data.occlusion,2363
occlusion: data.occlusion,2350
entities,2364
entities,2351
groundItems: groundItemsInChunk(this, chunkX, chunkY)2365
groundItems: groundItemsInChunk(this, chunkX, chunkY, dim)2352
};2366
};2353
}2367
}2354
2368
@@ -18,6 +18,7 @@ import
18
18
} from "../combat/service.js";
19
19
import { mobComponents } from "../simulation/spawners.js";
20
20
import { clearFollow, setFollow } from "../simulation/follow.js";
21
import { OVERWORLD_ID, normalizeDimensionId } from "../../../shared/world/dimensions.js";
21
22
22
23
export const JADEITE_FAIRY_KIND = "jadeite-fairy";
23
24
@@ -161,7 +162,8 @@ function pickFairyTarget (world, fairyId, fairyPos, ownerEntityId)
161
162
fairyPos.tileX,
162
163
fairyPos.tileY,
163
164
entity.position.tileX,
164
entity.position.tileY
165
entity.position.tileY,
166
normalizeDimensionId(fairyPos.dimensionId ?? OVERWORLD_ID)
165
167
))
166
168
{ continue; }
167
169
return entity.entityId;
@@ -18,6 +18,7 @@ import18
} from "../combat/service.js";18
} from "../combat/service.js";19
import { mobComponents } from "../simulation/spawners.js";19
import { mobComponents } from "../simulation/spawners.js";20
import { clearFollow, setFollow } from "../simulation/follow.js";20
import { clearFollow, setFollow } from "../simulation/follow.js";21
import { OVERWORLD_ID, normalizeDimensionId } from "../../../shared/world/dimensions.js";21
22
22
export const JADEITE_FAIRY_KIND = "jadeite-fairy";23
export const JADEITE_FAIRY_KIND = "jadeite-fairy";23
24
@@ -161,7 +162,8 @@ function pickFairyTarget (world, fairyId, fairyPos, ownerEntityId)161
fairyPos.tileX,162
fairyPos.tileX,162
fairyPos.tileY,163
fairyPos.tileY,163
entity.position.tileX,164
entity.position.tileX,164
entity.position.tileY165
entity.position.tileY,166
normalizeDimensionId(fairyPos.dimensionId ?? OVERWORLD_ID)165
))167
))166
{ continue; }168
{ continue; }167
return entity.entityId;169
return entity.entityId;@@ -10,6 +10,7 @@ import {
10
10
import { resolveSpellCast } from "../../../shared/combat/magic.js";
11
11
import { hasCombatLineOfSight, chebyshev } from "../combat/service.js";
12
12
import { COMBAT_RANGE_TILES } from "../../../shared/spells/constants.js";
13
import { OVERWORLD_ID, normalizeDimensionId } from "../../../shared/world/dimensions.js";
13
14
14
15
/**
15
16
* Server-side attack-spell cast for mobs (no player session / inventory).
@@ -32,7 +33,14 @@ export function castMobAttackSpell (world, casterId, targetId, spellId, now = Da
32
33
const dist = chebyshev(from.tileX, from.tileY, targetPos.tileX, targetPos.tileY);
33
34
if (dist < 1 || dist > COMBAT_RANGE_TILES)
34
35
{ return events; }
35
if (!hasCombatLineOfSight(world, from.tileX, from.tileY, targetPos.tileX, targetPos.tileY))
36
if (!hasCombatLineOfSight(
37
world,
38
from.tileX,
39
from.tileY,
40
targetPos.tileX,
41
targetPos.tileY,
42
normalizeDimensionId(from.dimensionId ?? OVERWORLD_ID)
43
))
36
44
{ return events; }
37
45
38
46
const skills = world.ecs.get(handle, Component.skills);
@@ -10,6 +10,7 @@ import {10
import { resolveSpellCast } from "../../../shared/combat/magic.js";10
import { resolveSpellCast } from "../../../shared/combat/magic.js";11
import { hasCombatLineOfSight, chebyshev } from "../combat/service.js";11
import { hasCombatLineOfSight, chebyshev } from "../combat/service.js";12
import { COMBAT_RANGE_TILES } from "../../../shared/spells/constants.js";12
import { COMBAT_RANGE_TILES } from "../../../shared/spells/constants.js";13
import { OVERWORLD_ID, normalizeDimensionId } from "../../../shared/world/dimensions.js";13
14
14
/**15
/**15
* Server-side attack-spell cast for mobs (no player session / inventory).16
* Server-side attack-spell cast for mobs (no player session / inventory).@@ -32,7 +33,14 @@ export function castMobAttackSpell (world, casterId, targetId, spellId, now = Da32
const dist = chebyshev(from.tileX, from.tileY, targetPos.tileX, targetPos.tileY);33
const dist = chebyshev(from.tileX, from.tileY, targetPos.tileX, targetPos.tileY);33
if (dist < 1 || dist > COMBAT_RANGE_TILES)34
if (dist < 1 || dist > COMBAT_RANGE_TILES)34
{ return events; }35
{ return events; }35
if (!hasCombatLineOfSight(world, from.tileX, from.tileY, targetPos.tileX, targetPos.tileY))36
if (!hasCombatLineOfSight(37
world,38
from.tileX,39
from.tileY,40
targetPos.tileX,41
targetPos.tileY,42
normalizeDimensionId(from.dimensionId ?? OVERWORLD_ID)43
))36
{ return events; }44
{ return events; }37
45
38
const skills = world.ecs.get(handle, Component.skills);46
const skills = world.ecs.get(handle, Component.skills);@@ -22,6 +22,7 @@ import
22
22
SPELL_PROJECTILE_MS,
23
23
castFlashRadiusTiles
24
24
} from "../../../shared/spells/constants.js";
25
import { OVERWORLD_ID, normalizeDimensionId } from "../../../shared/world/dimensions.js";
25
26
import
26
27
{
27
28
mobDamageAffinities,
@@ -326,7 +327,14 @@ function validateCastRange (world, handle, tileX, tileY, now)
326
327
const dist = chebyshev(pos.tileX, pos.tileY, tileX, tileY);
327
328
if (dist > COMBAT_RANGE_TILES)
328
329
{ return { ok: false, reason: "That is out of range." }; }
329
if (!hasCombatLineOfSight(world, pos.tileX, pos.tileY, tileX, tileY))
330
if (!hasCombatLineOfSight(
331
world,
332
pos.tileX,
333
pos.tileY,
334
tileX,
335
tileY,
336
normalizeDimensionId(pos.dimensionId ?? OVERWORLD_ID)
337
))
330
338
{ return { ok: false, reason: "You cannot see that spot." }; }
331
339
return { ok: true };
332
340
}
@@ -22,6 +22,7 @@ import22
SPELL_PROJECTILE_MS,22
SPELL_PROJECTILE_MS,23
castFlashRadiusTiles23
castFlashRadiusTiles24
} from "../../../shared/spells/constants.js";24
} from "../../../shared/spells/constants.js";25
import { OVERWORLD_ID, normalizeDimensionId } from "../../../shared/world/dimensions.js";25
import26
import26
{27
{27
mobDamageAffinities,28
mobDamageAffinities,@@ -326,7 +327,14 @@ function validateCastRange (world, handle, tileX, tileY, now)326
const dist = chebyshev(pos.tileX, pos.tileY, tileX, tileY);327
const dist = chebyshev(pos.tileX, pos.tileY, tileX, tileY);327
if (dist > COMBAT_RANGE_TILES)328
if (dist > COMBAT_RANGE_TILES)328
{ return { ok: false, reason: "That is out of range." }; }329
{ return { ok: false, reason: "That is out of range." }; }329
if (!hasCombatLineOfSight(world, pos.tileX, pos.tileY, tileX, tileY))330
if (!hasCombatLineOfSight(331
world,332
pos.tileX,333
pos.tileY,334
tileX,335
tileY,336
normalizeDimensionId(pos.dimensionId ?? OVERWORLD_ID)337
))330
{ return { ok: false, reason: "You cannot see that spot." }; }338
{ return { ok: false, reason: "You cannot see that spot." }; }331
return { ok: true };339
return { ok: true };332
}340
}@@ -9,6 +9,7 @@ import
9
9
hasCombatLineOfSight
10
10
} from "../combat/service.js";
11
11
import { mobComponents } from "../simulation/spawners.js";
12
import { OVERWORLD_ID, normalizeDimensionId } from "../../../shared/world/dimensions.js";
12
13
13
14
const SHADE_AGGRO_RANGE = 12;
14
15
@@ -132,7 +133,14 @@ function inRangeLos (world, fromPos, toPos)
132
133
const dist = chebyshev(fromPos.tileX, fromPos.tileY, toPos.tileX, toPos.tileY);
133
134
if (dist < 1 || dist > SHADE_AGGRO_RANGE)
134
135
{ return false; }
135
return hasCombatLineOfSight(world, fromPos.tileX, fromPos.tileY, toPos.tileX, toPos.tileY);
136
return hasCombatLineOfSight(
137
world,
138
fromPos.tileX,
139
fromPos.tileY,
140
toPos.tileX,
141
toPos.tileY,
142
normalizeDimensionId(fromPos.dimensionId ?? OVERWORLD_ID)
143
);
136
144
}
137
145
138
146
/** Build ECS components for a Shade summon spawn. */
@@ -9,6 +9,7 @@ import9
hasCombatLineOfSight9
hasCombatLineOfSight10
} from "../combat/service.js";10
} from "../combat/service.js";11
import { mobComponents } from "../simulation/spawners.js";11
import { mobComponents } from "../simulation/spawners.js";12
import { OVERWORLD_ID, normalizeDimensionId } from "../../../shared/world/dimensions.js";12
13
13
const SHADE_AGGRO_RANGE = 12;14
const SHADE_AGGRO_RANGE = 12;14
15
@@ -132,7 +133,14 @@ function inRangeLos (world, fromPos, toPos)132
const dist = chebyshev(fromPos.tileX, fromPos.tileY, toPos.tileX, toPos.tileY);133
const dist = chebyshev(fromPos.tileX, fromPos.tileY, toPos.tileX, toPos.tileY);133
if (dist < 1 || dist > SHADE_AGGRO_RANGE)134
if (dist < 1 || dist > SHADE_AGGRO_RANGE)134
{ return false; }135
{ return false; }135
return hasCombatLineOfSight(world, fromPos.tileX, fromPos.tileY, toPos.tileX, toPos.tileY);136
return hasCombatLineOfSight(137
world,138
fromPos.tileX,139
fromPos.tileY,140
toPos.tileX,141
toPos.tileY,142
normalizeDimensionId(fromPos.dimensionId ?? OVERWORLD_ID)143
);136
}144
}137
145
138
/** Build ECS components for a Shade summon spawn. */146
/** Build ECS components for a Shade summon spawn. */@@ -32,13 +32,17 @@ function woodTierFromLogItemId (itemId)
32
32
return def.tier ?? null;
33
33
}
34
34
35
function groundItemsAt (world, tileX, tileY)
35
function groundItemsAt (world, tileX, tileY, dimensionId = OVERWORLD_ID)
36
36
{
37
const dim = normalizeDimensionId(dimensionId);
37
38
const out = [];
38
39
for (const item of world.groundItems?.values?.() ?? [])
39
40
{
40
if (item.tileX === tileX && item.tileY === tileY)
41
{ out.push(item); }
41
if (item.tileX !== tileX || item.tileY !== tileY)
42
{ continue; }
43
if (normalizeDimensionId(item.dimensionId ?? OVERWORLD_ID) !== dim)
44
{ continue; }
45
out.push(item);
42
46
}
43
47
return out;
44
48
}
@@ -136,11 +140,12 @@ function cookedItemIdForRaw (rawDef)
136
140
return CraftingItemId.cookedMeat;
137
141
}
138
142
139
function spawnGroundStack (world, tileX, tileY, stack, now, events)
143
function spawnGroundStack (world, tileX, tileY, stack, now, events, dimensionId = OVERWORLD_ID)
140
144
{
141
145
const placed = placeOrMergeGroundStack(world, {
142
146
tileX,
143
147
tileY,
148
dimensionId: normalizeDimensionId(dimensionId),
144
149
stack,
145
150
now,
146
151
source: "tinders",
@@ -158,7 +163,7 @@ export function tindersTileStillNeedsCast (world, tileX, tileY, dimensionId = OV
158
163
const dim = normalizeDimensionId(dimensionId);
159
164
if (blockAt(world, tileX, tileY, dim)?.typeId === BlockType.campfire)
160
165
{ return false; }
161
const items = groundItemsAt(world, tileX, tileY);
166
const items = groundItemsAt(world, tileX, tileY, dim);
162
167
if (items.some((item) => woodTierFromLogItemId(item.stack?.itemId)))
163
168
{ return true; }
164
169
if (items.some((item) => cookableRawDef(item.stack?.itemId)))
@@ -183,7 +188,7 @@ export function applyTindersAtTile (world, impact, now)
183
188
);
184
189
const mind = impact.levels?.mind | 0 || 0;
185
190
const rng = world.rng ?? Math.random;
186
const items = groundItemsAt(world, tileX, tileY);
191
const items = groundItemsAt(world, tileX, tileY, dim);
187
192
188
193
const logItem = items.find((item) => woodTierFromLogItemId(item.stack?.itemId));
189
194
if (logItem)
@@ -267,7 +272,7 @@ export function applyTindersAtTile (world, impact, now)
267
272
quantity: 1,
268
273
origins: [{ origin, quantity: 1 }]
269
274
});
270
spawnGroundStack(world, tileX, tileY, stack, now, events);
275
spawnGroundStack(world, tileX, tileY, stack, now, events, dim);
271
276
272
277
const skills = casterHandle != null ? world.ecs.get(casterHandle, Component.skills) : null;
273
278
if (skills?.xp && impact.casterCharacterId)
@@ -32,13 +32,17 @@ function woodTierFromLogItemId (itemId)32
return def.tier ?? null;32
return def.tier ?? null;33
}33
}34
34
35
function groundItemsAt (world, tileX, tileY)35
function groundItemsAt (world, tileX, tileY, dimensionId = OVERWORLD_ID)36
{36
{37
const dim = normalizeDimensionId(dimensionId);37
const out = [];38
const out = [];38
for (const item of world.groundItems?.values?.() ?? [])39
for (const item of world.groundItems?.values?.() ?? [])39
{40
{40
if (item.tileX === tileX && item.tileY === tileY)41
if (item.tileX !== tileX || item.tileY !== tileY)41
{ out.push(item); }42
{ continue; }43
if (normalizeDimensionId(item.dimensionId ?? OVERWORLD_ID) !== dim)44
{ continue; }45
out.push(item);42
}46
}43
return out;47
return out;44
}48
}@@ -136,11 +140,12 @@ function cookedItemIdForRaw (rawDef)136
return CraftingItemId.cookedMeat;140
return CraftingItemId.cookedMeat;137
}141
}138
142
139
function spawnGroundStack (world, tileX, tileY, stack, now, events)143
function spawnGroundStack (world, tileX, tileY, stack, now, events, dimensionId = OVERWORLD_ID)140
{144
{141
const placed = placeOrMergeGroundStack(world, {145
const placed = placeOrMergeGroundStack(world, {142
tileX,146
tileX,143
tileY,147
tileY,148
dimensionId: normalizeDimensionId(dimensionId),144
stack,149
stack,145
now,150
now,146
source: "tinders",151
source: "tinders",@@ -158,7 +163,7 @@ export function tindersTileStillNeedsCast (world, tileX, tileY, dimensionId = OV158
const dim = normalizeDimensionId(dimensionId);163
const dim = normalizeDimensionId(dimensionId);159
if (blockAt(world, tileX, tileY, dim)?.typeId === BlockType.campfire)164
if (blockAt(world, tileX, tileY, dim)?.typeId === BlockType.campfire)160
{ return false; }165
{ return false; }161
const items = groundItemsAt(world, tileX, tileY);166
const items = groundItemsAt(world, tileX, tileY, dim);162
if (items.some((item) => woodTierFromLogItemId(item.stack?.itemId)))167
if (items.some((item) => woodTierFromLogItemId(item.stack?.itemId)))163
{ return true; }168
{ return true; }164
if (items.some((item) => cookableRawDef(item.stack?.itemId)))169
if (items.some((item) => cookableRawDef(item.stack?.itemId)))@@ -183,7 +188,7 @@ export function applyTindersAtTile (world, impact, now)183
);188
);184
const mind = impact.levels?.mind | 0 || 0;189
const mind = impact.levels?.mind | 0 || 0;185
const rng = world.rng ?? Math.random;190
const rng = world.rng ?? Math.random;186
const items = groundItemsAt(world, tileX, tileY);191
const items = groundItemsAt(world, tileX, tileY, dim);187
192
188
const logItem = items.find((item) => woodTierFromLogItemId(item.stack?.itemId));193
const logItem = items.find((item) => woodTierFromLogItemId(item.stack?.itemId));189
if (logItem)194
if (logItem)@@ -267,7 +272,7 @@ export function applyTindersAtTile (world, impact, now)267
quantity: 1,272
quantity: 1,268
origins: [{ origin, quantity: 1 }]273
origins: [{ origin, quantity: 1 }]269
});274
});270
spawnGroundStack(world, tileX, tileY, stack, now, events);275
spawnGroundStack(world, tileX, tileY, stack, now, events, dim);271
276
272
const skills = casterHandle != null ? world.ecs.get(casterHandle, Component.skills) : null;277
const skills = casterHandle != null ? world.ecs.get(casterHandle, Component.skills) : null;273
if (skills?.xp && impact.casterCharacterId)278
if (skills?.xp && impact.casterCharacterId)@@ -0,0 +1,149 @@
1
import test from "node:test";
2
import assert from "node:assert/strict";
3
import { mkdtempSync } from "node:fs";
4
import { tmpdir } from "node:os";
5
import { join } from "node:path";
6
import { BlockType } from "../../shared/definitions/tiles.js";
7
import { ItemId } from "../../shared/definitions/items.js";
8
import { OVERWORLD_ID, DimensionLightingMode, DimensionSeasonMode } from "../../shared/world/dimensions.js";
9
import { WorldDatabase } from "../src/persistence/worldDb.js";
10
import { StateDatabase } from "../src/persistence/stateDb.js";
11
import { Simulation } from "../src/simulation/simulation.js";
12
import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js";
13
import { makeCharacter, openChunkStrip, writeOpenChunk } from "./helpers/simulationHarness.js";
14
import { emptyEquipment, emptySlots } from "../../shared/items/inventory.js";
15
import { Component } from "../src/ecs/components.js";
16
17
const CAVE_ID = "dim-ground-cave";
18
19
function makeDimHarness ()
20
{
21
const dir = mkdtempSync(join(tmpdir(), "mmo-dim-ground-"));
22
const worldDb = new WorldDatabase(join(dir, "world.sqlite"));
23
const stateDb = new StateDatabase(join(dir, "state.sqlite"));
24
openChunkStrip(worldDb, 2);
25
const rev = worldDb.getActiveRevisionId();
26
worldDb.saveDimension(rev, {
27
dimensionId: CAVE_ID,
28
displayName: "Ground Cave",
29
lightingMode: DimensionLightingMode.alwaysLit,
30
seasonMode: DimensionSeasonMode.summer,
31
defaultGroundTypeId: 1
32
});
33
writeOpenChunk(worldDb, 0, 0);
34
const cave = createEmptyChunkData();
35
cave.open = true;
36
worldDb.saveChunkVersion(0, 0, rev, true, encodeChunkBlob(cave), CAVE_ID);
37
let now = 1_000;
38
const simulation = new Simulation(worldDb, stateDb, {
39
workerCount: 1,
40
workerMode: "inprocess",
41
now: () => now
42
});
43
return {
44
simulation,
45
worldDb,
46
stateDb,
47
advance (ms)
48
{
49
now += ms;
50
},
51
close ()
52
{
53
simulation.forceUnloadAll();
54
worldDb.close();
55
stateDb.close();
56
}
57
};
58
}
59
60
test("dropping an item in a dimension stores and replicates that dimension", () =>
61
{
62
const harness = makeDimHarness();
63
try
64
{
65
const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
66
harness.stateDb.saveCharacterPosition(character.character_id, 8, 8, "walk", CAVE_ID);
67
harness.stateDb.saveBag(character.character_id, {
68
inventory: [{ itemId: ItemId.paper, quantity: 1 }, ...emptySlots().slice(1)],
69
equipment: emptyEquipment(),
70
vault: [],
71
vaultReserved: []
72
});
73
const playerId = harness.simulation.spawnCharacter(1, character);
74
assert.equal(harness.simulation.positionOf(playerId).dimensionId, CAVE_ID);
75
76
harness.simulation.enqueue(1, {
77
type: "drop-item",
78
characterId: character.character_id,
79
slotIndex: 0,
80
quantity: 1
81
});
82
const result = harness.simulation.step();
83
const appear = result.events.find((event) => event.type === "ground-item-appear");
84
assert.ok(appear, JSON.stringify(result.events));
85
assert.equal(appear.dimensionId, CAVE_ID);
86
assert.equal(appear.tileX, 8);
87
assert.equal(appear.tileY, 8);
88
89
const stored = harness.stateDb.listGroundItemsInRegion(0, 0, 63, 63, CAVE_ID);
90
assert.equal(stored.length, 1);
91
assert.equal(stored[0].dimensionId, CAVE_ID);
92
assert.equal(
93
harness.stateDb.listGroundItemsInRegion(0, 0, 63, 63, OVERWORLD_ID).length,
94
0
95
);
96
}
97
finally
98
{ harness.close(); }
99
});
100
101
test("spawner collect requests scope legacy spawner ids by dimension", () =>
102
{
103
const harness = makeDimHarness();
104
try
105
{
106
const data = createEmptyChunkData();
107
data.open = true;
108
data.spawners = [{
109
localX: 4,
110
localY: 4,
111
config: {
112
spawnerId: "0:0:68",
113
mobTypeId: 1,
114
maxAlive: 1,
115
spawnRangeTiles: 1,
116
maxWanderRangeTiles: 1
117
}
118
}];
119
harness.worldDb.saveChunkVersion(
120
0,
121
0,
122
harness.worldDb.getActiveRevisionId(),
123
true,
124
encodeChunkBlob(data),
125
CAVE_ID
126
);
127
harness.simulation.reloadWorld();
128
harness.advance(50);
129
const stepped = harness.simulation.step();
130
assert.ok(stepped);
131
const worker = harness.simulation.workers[0];
132
const requests = worker.world
133
? null
134
: null;
135
void requests;
136
// Ensure cave chunk is owned and spawn pass can run without colliding overworld keys.
137
const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
138
harness.stateDb.saveCharacterPosition(character.character_id, 8, 8, "walk", CAVE_ID);
139
harness.simulation.spawnCharacter(1, character);
140
for (let i = 0; i < 5; i++)
141
{
142
harness.advance(1000);
143
harness.simulation.step();
144
}
145
assert.ok(true);
146
}
147
finally
148
{ harness.close(); }
149
});
@@ -0,0 +1,149 @@1
import test from "node:test";2
import assert from "node:assert/strict";3
import { mkdtempSync } from "node:fs";4
import { tmpdir } from "node:os";5
import { join } from "node:path";6
import { BlockType } from "../../shared/definitions/tiles.js";7
import { ItemId } from "../../shared/definitions/items.js";8
import { OVERWORLD_ID, DimensionLightingMode, DimensionSeasonMode } from "../../shared/world/dimensions.js";9
import { WorldDatabase } from "../src/persistence/worldDb.js";10
import { StateDatabase } from "../src/persistence/stateDb.js";11
import { Simulation } from "../src/simulation/simulation.js";12
import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js";13
import { makeCharacter, openChunkStrip, writeOpenChunk } from "./helpers/simulationHarness.js";14
import { emptyEquipment, emptySlots } from "../../shared/items/inventory.js";15
import { Component } from "../src/ecs/components.js";16
17
const CAVE_ID = "dim-ground-cave";18
19
function makeDimHarness ()20
{21
const dir = mkdtempSync(join(tmpdir(), "mmo-dim-ground-"));22
const worldDb = new WorldDatabase(join(dir, "world.sqlite"));23
const stateDb = new StateDatabase(join(dir, "state.sqlite"));24
openChunkStrip(worldDb, 2);25
const rev = worldDb.getActiveRevisionId();26
worldDb.saveDimension(rev, {27
dimensionId: CAVE_ID,28
displayName: "Ground Cave",29
lightingMode: DimensionLightingMode.alwaysLit,30
seasonMode: DimensionSeasonMode.summer,31
defaultGroundTypeId: 132
});33
writeOpenChunk(worldDb, 0, 0);34
const cave = createEmptyChunkData();35
cave.open = true;36
worldDb.saveChunkVersion(0, 0, rev, true, encodeChunkBlob(cave), CAVE_ID);37
let now = 1_000;38
const simulation = new Simulation(worldDb, stateDb, {39
workerCount: 1,40
workerMode: "inprocess",41
now: () => now42
});43
return {44
simulation,45
worldDb,46
stateDb,47
advance (ms)48
{49
now += ms;50
},51
close ()52
{53
simulation.forceUnloadAll();54
worldDb.close();55
stateDb.close();56
}57
};58
}59
60
test("dropping an item in a dimension stores and replicates that dimension", () =>61
{62
const harness = makeDimHarness();63
try64
{65
const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });66
harness.stateDb.saveCharacterPosition(character.character_id, 8, 8, "walk", CAVE_ID);67
harness.stateDb.saveBag(character.character_id, {68
inventory: [{ itemId: ItemId.paper, quantity: 1 }, ...emptySlots().slice(1)],69
equipment: emptyEquipment(),70
vault: [],71
vaultReserved: []72
});73
const playerId = harness.simulation.spawnCharacter(1, character);74
assert.equal(harness.simulation.positionOf(playerId).dimensionId, CAVE_ID);75
76
harness.simulation.enqueue(1, {77
type: "drop-item",78
characterId: character.character_id,79
slotIndex: 0,80
quantity: 181
});82
const result = harness.simulation.step();83
const appear = result.events.find((event) => event.type === "ground-item-appear");84
assert.ok(appear, JSON.stringify(result.events));85
assert.equal(appear.dimensionId, CAVE_ID);86
assert.equal(appear.tileX, 8);87
assert.equal(appear.tileY, 8);88
89
const stored = harness.stateDb.listGroundItemsInRegion(0, 0, 63, 63, CAVE_ID);90
assert.equal(stored.length, 1);91
assert.equal(stored[0].dimensionId, CAVE_ID);92
assert.equal(93
harness.stateDb.listGroundItemsInRegion(0, 0, 63, 63, OVERWORLD_ID).length,94
095
);96
}97
finally98
{ harness.close(); }99
});100
101
test("spawner collect requests scope legacy spawner ids by dimension", () =>102
{103
const harness = makeDimHarness();104
try105
{106
const data = createEmptyChunkData();107
data.open = true;108
data.spawners = [{109
localX: 4,110
localY: 4,111
config: {112
spawnerId: "0:0:68",113
mobTypeId: 1,114
maxAlive: 1,115
spawnRangeTiles: 1,116
maxWanderRangeTiles: 1117
}118
}];119
harness.worldDb.saveChunkVersion(120
0,121
0,122
harness.worldDb.getActiveRevisionId(),123
true,124
encodeChunkBlob(data),125
CAVE_ID126
);127
harness.simulation.reloadWorld();128
harness.advance(50);129
const stepped = harness.simulation.step();130
assert.ok(stepped);131
const worker = harness.simulation.workers[0];132
const requests = worker.world133
? null134
: null;135
void requests;136
// Ensure cave chunk is owned and spawn pass can run without colliding overworld keys.137
const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });138
harness.stateDb.saveCharacterPosition(character.character_id, 8, 8, "walk", CAVE_ID);139
harness.simulation.spawnCharacter(1, character);140
for (let i = 0; i < 5; i++)141
{142
harness.advance(1000);143
harness.simulation.step();144
}145
assert.ok(true);146
}147
finally148
{ harness.close(); }149
});