Repository navigation
Expand file tree
/
Copy pathchunkWorker.ts
More file actions
362 lines (317 loc) · 10.7 KB
/
Copy pathchunkWorker.ts
File metadata and controls
362 lines (317 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/// <reference lib="webworker" />
import * as THREE from "three";
import { SimplexNoise } from "three/examples/jsm/math/SimplexNoise";
import { BlockID, oreConfig } from "./Block";
import { RNG } from "./RNG";
import { WorldParams, WorldSize } from "./WorldChunk";
declare const self: DedicatedWorkerGlobalScope;
export const generateChunk = async (
chunkSize: WorldSize,
params: WorldParams,
x: number,
z: number
) => {
const chunkPos = new THREE.Vector3(x, 0, z);
let data = initEmptyChunk(chunkSize);
const rng = new RNG(params.seed);
data = generateResources(rng, data, chunkSize, chunkPos);
data = generateTerrain(rng, data, chunkSize, params, chunkPos);
data = generateTrees(rng, data, chunkSize, params, chunkPos);
data = generateTallGrass(rng, data, chunkSize, params);
data = generateFlowers(rng, data, chunkSize, params);
return data;
};
const initEmptyChunk = (chunkSize: WorldSize) => {
const data = new Array(chunkSize.width);
for (let x = 0; x < chunkSize.width; x++) {
data[x] = new Array(chunkSize.height);
for (let y = 0; y < chunkSize.height; y++) {
data[x][y] = new Array(chunkSize.width);
for (let z = 0; z < chunkSize.width; z++) {
data[x][y][z] = BlockID.Air;
}
}
}
return data;
};
/**
* Generates the resources (coal, stone, etc.) for the world
*/
export const generateResources = (
rng: RNG,
input: BlockID[][][],
size: WorldSize,
chunkPos: THREE.Vector3
): BlockID[][][] => {
const simplex = new SimplexNoise(rng);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
for (const [_, config] of Object.entries(oreConfig)) {
for (let x = 0; x < size.width; x++) {
for (let y = 0; y < size.height; y++) {
for (let z = 0; z < size.width; z++) {
const value = simplex.noise3d(
(chunkPos.x + x) / config.scale.x,
(chunkPos.y + y) / config.scale.y,
(chunkPos.z + z) / config.scale.z
);
if (value > config.scarcity) {
input[x][y][z] = config.id;
}
}
}
}
}
return input;
};
/**
* Generates the terrain data
*/
export const generateTerrain = (
rng: RNG,
input: BlockID[][][],
size: WorldSize,
params: WorldParams,
chunkPos: THREE.Vector3
): BlockID[][][] => {
const simplex = new SimplexNoise(rng);
for (let x = 0; x < size.width; x++) {
for (let z = 0; z < size.width; z++) {
const value = simplex.noise(
(chunkPos.x + x) / param
10BC8
s.terrain.scale,
(chunkPos.z + z) / params.terrain.scale
);
const scaledNoise =
params.terrain.offset + params.terrain.magnitude * value;
let height = Math.floor(size.height * scaledNoise);
height = Math.max(0, Math.min(height, size.height - 1));
const numSurfaceBlocks =
params.surface.offset +
Math.abs(simplex.noise(x, z) * params.surface.magnitude);
const numBedrockBlocks =
params.bedrock.offset +
Math.abs(simplex.noise(x, z) * params.bedrock.magnitude);
for (let y = 0; y < size.height; y++) {
if (y < height) {
if (y < numBedrockBlocks) {
input[x][y][z] = BlockID.Bedrock;
} else if (y < height - numSurfaceBlocks) {
if (input[x][y][z] === BlockID.Air) {
input[x][y][z] = BlockID.Stone;
}
} else {
input[x][y][z] = BlockID.Dirt;
}
} else if (y === height) {
input[x][y][z] = BlockID.Grass;
} else if (y > height) {
input[x][y][z] = BlockID.Air;
}
}
}
}
return input;
};
/**
* Generates trees
*/
export const generateTrees = (
rng: RNG,
input: BlockID[][][],
size: WorldSize,
params: WorldParams,
chunkPos: THREE.Vector3
): BlockID[][][] => {
const simplex = new SimplexNoise(rng);
const canopySize = params.trees.canopy.size.max;
for (let baseX = canopySize; baseX < size.width - canopySize; baseX++) {
for (let baseZ = canopySize; baseZ < size.width - canopySize; baseZ++) {
const n =
simplex.noise(chunkPos.x + baseX, chunkPos.z + baseZ) * 0.5 + 0.5;
if (n < 1 - params.trees.frequency) {
continue;
}
// Find the grass tile
for (let y = size.height - 1; y >= 0; y--) {
if (input[baseX][y][baseZ] !== BlockID.Grass) {
continue;
}
// Found grass, move one time up
const baseY = y + 1;
const minH = params.trees.trunkHeight.min;
const maxH = params.trees.trunkHeight.max;
const trunkHeight = Math.round(rng.random() * (maxH - minH)) + minH;
const topY = baseY + trunkHeight;
// Fill in blocks for the trunk
for (let i = baseY; i < topY; i++) {
input[baseX][i][baseZ] = BlockID.OakLog;
}
// Generate the canopy
// generate layer by layer, 4 layers in total
for (let i = 0; i < 4; i++) {
if (i === 0) {
// first layer above the height of tree and has 5 leaves in a + shape
input[baseX][topY][baseZ] = BlockID.Leaves;
input[baseX + 1][topY][baseZ] = BlockID.Leaves;
input[baseX - 1][topY][baseZ] = BlockID.Leaves;
input[baseX][topY][baseZ + 1] = BlockID.Leaves;
input[baseX][topY][baseZ - 1] = BlockID.Leaves;
} else if (i === 1) {
// base layer
input[baseX][topY - i][baseZ] = BlockID.Leaves;
input[baseX + 1][topY - i][baseZ] = BlockID.Leaves;
input[baseX - 1][topY - i][baseZ] = BlockID.Leaves;
input[baseX][topY - i][baseZ + 1] = BlockID.Leaves;
input[baseX][topY - i][baseZ - 1] = BlockID.Leaves;
// diagonal leaf blocks grow min of 1 and max of 3 blocks away from the trunk
const minR = params.trees.canopy.size.min;
const maxR = params.trees.canopy.size.max;
const R = Math.round(rng.random() * (maxR - minR)) + minR;
// grow leaves in a diagonal shape
for (let x = -R; x <= R; x++) {
for (let z = -R; z <= R; z++) {
if (x * x + z * z > R * R) {
continue;
}
if (input[baseX + x][topY - i][baseZ + z] !== BlockID.Air) {
continue;
}
if (rng.random() > 0.5) {
input[baseX + x][topY - i][baseZ + z] = BlockID.Leaves;
}
}
}
} else if (i === 2 || i == 3) {
for (let x = -2; x <= 2; x++) {
for (let z = -2; z <= 2; z++) {
if (input[baseX + x][topY - i][baseZ + z] !== BlockID.Air) {
continue;
}
input[baseX + x][topY - i][baseZ + z] = BlockID.Leaves;
}
}
// remove 4 corners randomly
for (const x of [-2, 2]) {
for (const z of [-2, 2]) {
if (rng.random() > 0.5) {
input[baseX + x][topY - i][baseZ + z] = BlockID.Air;
}
}
}
}
}
}
}
}
return input;
};
/**
* Generate random patches of tall grass across the top surface
*/
export const generateTallGrass = (
rng: RNG,
input: BlockID[][][],
size: WorldSize,
params: WorldParams
): BlockID[][][] => {
for (let x = 0; x < size.width; x++) {
for (let z = 0; z < size.width; z++) {
// starting from the top of the chunk, find the first grass block
// if come in contact with leaves, stop since grass doesn't grow under trees
for (let y = size.height - 1; y >= 0; y--) {
if (input[x][y][z] === BlockID.Leaves) {
break;
}
if (input[x][y][z] === BlockID.Grass) {
// found grass, move one time up
const baseY = y + 1;
if (input[x][baseY][z] !== BlockID.Air) {
continue;
}
if (rng.random() < params.grass.frequency) {
input[x][baseY][z] = BlockID.TallGrass;
// Define the maximum distance from the center
const maxDistance = params.grass.patchSize;
// Random walk algorithm
let currentX = x;
let currentZ = z;
for (let i = 0; i < maxDistance; i++) {
const direction = rng.random() * 2 * Math.PI; // Random direction
currentX += Math.round(Math.cos(direction));
currentZ += Math.round(Math.sin(direction));
// Check if the new position is within the chunk boundaries and is air
if (
currentX >= 0 &&
currentX < size.width &&
currentZ >= 0 &&
currentZ < size.width &&
input[currentX][baseY][currentZ] === BlockID.Air &&
input[currentX][y][currentZ] === BlockID.Grass
) {
input[currentX][baseY][currentZ] = BlockID.TallGrass;
}
}
}
}
}
}
}
return input;
};
/**
* Generate random patches of flowers across the top surface
*/
export const generateFlowers = (
rng: RNG,
input: BlockID[][][],
size: WorldSize,
params: WorldParams
): BlockID[][][] => {
for (let x = 0; x < size.width; x++) {
for (let z = 0; z < size.width; z++) {
// starting from the top of the chunk, find the first grass block
// if come in contact with leaves, stop since flowers doesn't grow under trees
for (let y = size.height - 1; y >= 0; y--) {
if (input[x][y][z] === BlockID.Leaves) {
break;
}
if (input[x][y][z] === BlockID.Grass) {
// found grass, move one time up
const baseY = y + 1;
if (input[x][baseY][z] !== BlockID.Air) {
continue;
}
// Check if there's a tallgrass block within 3 blocks
let isTallGrassNearby = false;
for (let dx = -3; dx <= 3; dx++) {
for (let dz = -3; dz <= 3; dz++) {
const nx = x + dx;
const nz = z + dz;
if (
nx >= 0 &&
nx < size.width &&
nz >= 0 &&
nz < size.width &&
input[nx][baseY][nz] === BlockID.TallGrass
) {
isTallGrassNearby = true;
break;
}
}
if (isTallGrassNearby) {
break;
}
}
if (isTallGrassNearby) {
continue;
}
const flowerId =
rng.random() < 0.5 ? BlockID.FlowerDandelion : BlockID.FlowerRose;
if (rng.random() < params.flowers.frequency) {
input[x][baseY][z] = flowerId;
}
}
}
}
}
return input;
};