BlueGit

timfa questward

Add ground and block chunk data for default grass tiles.

Co-authored-by: Cursor <cursoragent@cursor.com>

timfa Tim Falken · pushed by timfa

Authored 2026-08-24 · 2 months ago

Hash
87e2f6b70a4e023b1334749b4a793e6251ff1e47
Parent
d70631cb
2 files changed +117 −3
docs/FEATURES.md +3 −3
@@ -15,9 +15,9 @@ Each entry is a short note on behaviour and where the code lives. Update this fi
15 15
16 16 ## Ground and block tile layers
17 17
18 **Status:** not implemented
19 **Spec:** Every tile has a visual ground type. An optional block supplies collision and behaviour. Unedited chunks are default grass with no blocks.
20 **Code:** —
18 **Status:** full
19 **Spec:** Every tile has a visual ground type. An optional block supplies collision and behaviour. Unedited chunks are default grass with no blocks. First-version types are grass, wood, stone wall, and torch.
20 **Code:** `server/src/world/chunkData.js`, `shared/definitions/tiles.js`
21 21
22 22 ## Open and closed chunks
23 23
@@ -15,9 +15,9 @@ Each entry is a short note on behaviour and where the code lives. Update this fi
15
15
16## Ground and block tile layers
16## Ground and block tile layers
17
17
18**Status:** not implemented
18**Status:** full
19**Spec:** Every tile has a visual ground type. An optional block supplies collision and behaviour. Unedited chunks are default grass with no blocks.
19**Spec:** Every tile has a visual ground type. An optional block supplies collision and behaviour. Unedited chunks are default grass with no blocks. First-version types are grass, wood, stone wall, and torch.
20**Code:** —
20**Code:** `server/src/world/chunkData.js`, `shared/definitions/tiles.js`
21
21
22## Open and closed chunks
22## Open and closed chunks
23
23
server/src/world/chunkData.js added +114 −0
@@ -0,0 +1,114 @@
1 import { CHUNK_SIZE, DEFAULT_WORLD_SETTINGS } from "../../../shared/protocol/constants.js";
2 import { GroundType, isBlockPassable, blockLightRange } from "../../../shared/definitions/tiles.js";
3 import { tileIndex } from "./coordinates.js";
4
5 export function createEmptyChunkData (groundTypeId = DEFAULT_WORLD_SETTINGS.defaultGroundTypeId)
6 {
7 const tileCount = CHUNK_SIZE * CHUNK_SIZE;
8 const grounds = new Array(tileCount).fill(groundTypeId);
9 const blocks = new Array(tileCount).fill(null);
10 const light = new Array(tileCount).fill(0);
11 return {
12 open: false,
13 grounds,
14 blocks,
15 light
16 };
17 }
18
19 export function cloneChunkData (data)
20 {
21 return {
22 open: data.open,
23 grounds: data.grounds.slice(),
24 blocks: data.blocks.map((block) => (block ? { typeId: block.typeId, config: block.config ?? null } : null)),
25 light: data.light.slice()
26 };
27 }
28
29 export function encodeChunkBlob (data)
30 {
31 return JSON.stringify({
32 open: !!data.open,
33 grounds: data.grounds,
34 blocks: data.blocks,
35 light: data.light
36 });
37 }
38
39 export function decodeChunkBlob (blob, groundTypeId = DEFAULT_WORLD_SETTINGS.defaultGroundTypeId)
40 {
41 if (!blob)
42 { return createEmptyChunkData(groundTypeId); }
43
44 const parsed = typeof blob === "string" ? JSON.parse(blob) : blob;
45 const fallback = createEmptyChunkData(groundTypeId);
46 const tileCount = CHUNK_SIZE * CHUNK_SIZE;
47 const grounds = Array.isArray(parsed.grounds) ? parsed.grounds.slice(0, tileCount) : fallback.grounds;
48 const blocks = Array.isArray(parsed.blocks) ? parsed.blocks.slice(0, tileCount) : fallback.blocks;
49 const light = Array.isArray(parsed.light) ? parsed.light.slice(0, tileCount) : fallback.light;
50
51 while (grounds.length < tileCount)
52 { grounds.push(groundTypeId); }
53 while (blocks.length < tileCount)
54 { blocks.push(null); }
55 while (light.length < tileCount)
56 { light.push(0); }
57
58 return {
59 open: !!parsed.open,
60 grounds,
61 blocks,
62 light
63 };
64 }
65
66 export function getTile (data, localX, localY)
67 {
68 const index = tileIndex(localX, localY);
69 return {
70 groundTypeId: data.grounds[index],
71 block: data.blocks[index],
72 light: data.light[index]
73 };
74 }
75
76 export function setGround (data, localX, localY, groundTypeId)
77 {
78 data.grounds[tileIndex(localX, localY)] = groundTypeId;
79 }
80
81 export function setBlock (data, localX, localY, block)
82 {
83 data.blocks[tileIndex(localX, localY)] = block;
84 }
85
86 export function isChunkTilePassable (data, localX, localY)
87 {
88 const block = data.blocks[tileIndex(localX, localY)];
89 return isBlockPassable(block?.typeId ?? null);
90 }
91
92 export function collectLightSources (chunkX, chunkY, data)
93 {
94 const sources = [];
95 for (let localY = 0; localY < CHUNK_SIZE; localY++)
96 {
97 for (let localX = 0; localX < CHUNK_SIZE; localX++)
98 {
99 const block = data.blocks[tileIndex(localX, localY)];
100 const range = blockLightRange(block?.typeId ?? null);
101 if (range > 0)
102 {
103 sources.push({
104 tileX: chunkX * CHUNK_SIZE + localX,
105 tileY: chunkY * CHUNK_SIZE + localY,
106 range
107 });
108 }
109 }
110 }
111 return sources;
112 }
113
114 export { GroundType };
@@ -0,0 +1,114 @@
1import { CHUNK_SIZE, DEFAULT_WORLD_SETTINGS } from "../../../shared/protocol/constants.js";
2import { GroundType, isBlockPassable, blockLightRange } from "../../../shared/definitions/tiles.js";
3import { tileIndex } from "./coordinates.js";
4
5export function createEmptyChunkData (groundTypeId = DEFAULT_WORLD_SETTINGS.defaultGroundTypeId)
6{
7 const tileCount = CHUNK_SIZE * CHUNK_SIZE;
8 const grounds = new Array(tileCount).fill(groundTypeId);
9 const blocks = new Array(tileCount).fill(null);
10 const light = new Array(tileCount).fill(0);
11 return {
12 open: false,
13 grounds,
14 blocks,
15 light
16 };
17}
18
19export function cloneChunkData (data)
20{
21 return {
22 open: data.open,
23 grounds: data.grounds.slice(),
24 blocks: data.blocks.map((block) => (block ? { typeId: block.typeId, config: block.config ?? null } : null)),
25 light: data.light.slice()
26 };
27}
28
29export function encodeChunkBlob (data)
30{
31 return JSON.stringify({
32 open: !!data.open,
33 grounds: data.grounds,
34 blocks: data.blocks,
35 light: data.light
36 });
37}
38
39export function decodeChunkBlob (blob, groundTypeId = DEFAULT_WORLD_SETTINGS.defaultGroundTypeId)
40{
41 if (!blob)
42 { return createEmptyChunkData(groundTypeId); }
43
44 const parsed = typeof blob === "string" ? JSON.parse(blob) : blob;
45 const fallback = createEmptyChunkData(groundTypeId);
46 const tileCount = CHUNK_SIZE * CHUNK_SIZE;
47 const grounds = Array.isArray(parsed.grounds) ? parsed.grounds.slice(0, tileCount) : fallback.grounds;
48 const blocks = Array.isArray(parsed.blocks) ? parsed.blocks.slice(0, tileCount) : fallback.blocks;
49 const light = Array.isArray(parsed.light) ? parsed.light.slice(0, tileCount) : fallback.light;
50
51 while (grounds.length < tileCount)
52 { grounds.push(groundTypeId); }
53 while (blocks.length < tileCount)
54 { blocks.push(null); }
55 while (light.length < tileCount)
56 { light.push(0); }
57
58 return {
59 open: !!parsed.open,
60 grounds,
61 blocks,
62 light
63 };
64}
65
66export function getTile (data, localX, localY)
67{
68 const index = tileIndex(localX, localY);
69 return {
70 groundTypeId: data.grounds[index],
71 block: data.blocks[index],
72 light: data.light[index]
73 };
74}
75
76export function setGround (data, localX, localY, groundTypeId)
77{
78 data.grounds[tileIndex(localX, localY)] = groundTypeId;
79}
80
81export function setBlock (data, localX, localY, block)
82{
83 data.blocks[tileIndex(localX, localY)] = block;
84}
85
86export function isChunkTilePassable (data, localX, localY)
87{
88 const block = data.blocks[tileIndex(localX, localY)];
89 return isBlockPassable(block?.typeId ?? null);
90}
91
92export function collectLightSources (chunkX, chunkY, data)
93{
94 const sources = [];
95 for (let localY = 0; localY < CHUNK_SIZE; localY++)
96 {
97 for (let localX = 0; localX < CHUNK_SIZE; localX++)
98 {
99 const block = data.blocks[tileIndex(localX, localY)];
100 const range = blockLightRange(block?.typeId ?? null);
101 if (range > 0)
102 {
103 sources.push({
104 tileX: chunkX * CHUNK_SIZE + localX,
105 tileY: chunkY * CHUNK_SIZE + localY,
106 range
107 });
108 }
109 }
110 }
111 return sources;
112}
113
114export { GroundType };