-
-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathDecorator.ts
344 lines (329 loc) · 10.9 KB
/
Decorator.ts
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
import { DataModel } from '@mcschema/core'
import type { BlockPos, ChunkPos, PerlinNoise, Random } from 'deepslate/worldgen'
import type { Color } from '../../Utils.js'
import { clamp, stringToColor } from '../../Utils.js'
import type { VersionId } from '../../services/index.js'
import { checkVersion } from '../../services/index.js'
import { normalizeId, sampleHeight, sampleInt } from './WorldgenUtils.jsx'
export type Placement = [BlockPos, number]
export type PlacementContext = {
placements: Placement[],
features: string[],
random: Random,
biomeInfoNoise: PerlinNoise,
seaLevel: number,
version: VersionId,
nextFloat(): number,
nextInt(max: number): number,
nextGaussian(): number,
}
export type PlacedFeature = {
pos: BlockPos,
feature: string,
color: Color,
}
const terrain = [50, 50, 51, 51, 52, 52, 53, 54, 56, 57, 57, 58, 58, 59, 60, 60, 60, 59, 59, 59, 60, 61, 61, 62, 63, 63, 64, 64, 64, 65, 65, 66, 66, 65, 65, 66, 66, 67, 67, 67, 68, 69, 71, 73, 74, 76, 79, 80, 81, 81, 82, 83, 83, 82, 82, 81, 81, 80, 80, 80, 81, 81, 82, 82]
export const featureColors: Color[] = [
[255, 77, 54], // red
[59, 118, 255], // blue
[91, 207, 25], // green
[217, 32, 245], // magenta
[255, 209, 41], // yellow
[52, 204, 209], // cyan
]
export function decorateChunk(pos: ChunkPos, state: any, ctx: PlacementContext): PlacedFeature[] {
if (checkVersion(ctx.version, undefined, '1.17')) {
getPlacements([pos[0] * 16, 0, pos[1] * 16], DataModel.unwrapLists(state), ctx)
} else {
modifyPlacement([pos[0] * 16, 0, pos[1] * 16], DataModel.unwrapLists(state.placement), ctx)
}
return ctx.placements.map(([pos, i]) => {
const feature = ctx.features[i]
let color = i < featureColors.length ? featureColors[i] : stringToColor(feature)
color = [clamp(color[0], 50, 205), clamp(color[1], 50, 205), clamp(color[2], 50, 205)]
if ((Math.floor(pos[0] / 16) + Math.floor(pos[2] / 16)) % 2 === 0) {
color = [0.85 * color[0], 0.85 * color[1], 0.85 * color[2]]
}
return { pos, feature, color }
})
}
function decorateY(pos: BlockPos, y: number): BlockPos[] {
return [[ pos[0], y, pos[2] ]]
}
// 1.17 and before
function useFeature(s: string, ctx: PlacementContext) {
const i = ctx.features.indexOf(s)
if (i != -1) return i
ctx.features.push(s)
return ctx.features.length - 1
}
function getPlacements(pos: BlockPos, feature: any, ctx: PlacementContext): void {
if (typeof feature === 'string') {
ctx.placements.push([pos, useFeature(feature, ctx)])
return
}
const type = normalizeId(feature?.type ?? 'no_op')
const featureFn = Features[type]
if (featureFn) {
featureFn(feature.config, pos, ctx)
} else {
ctx.placements.push([pos, useFeature(JSON.stringify(feature), ctx)])
}
}
function getPositions(pos: BlockPos, decorator: any, ctx: PlacementContext): BlockPos[] {
const type = normalizeId(decorator?.type ?? 'nope')
const decoratorFn = Decorators[type]
if (!decoratorFn) {
return [pos]
}
return decoratorFn(decorator?.config, pos, ctx)
}
const Features: {
[key: string]: (config: any, pos: BlockPos, ctx: PlacementContext) => void,
} = {
decorated: (config, pos, ctx) => {
const positions = getPositions(pos, config?.decorator, ctx)
positions.forEach(p => getPlacements(p, config?.feature, ctx))
},
random_boolean_selector: (config, pos, ctx) => {
const feature = ctx.nextFloat() < 0.5 ? config?.feature_true : config?.feature_false
getPlacements(pos, feature, ctx)
},
random_selector: (config, pos, ctx) => {
for (const f of config?.features ?? []) {
if (ctx.nextFloat() < (f?.chance ?? 0)) {
getPlacements(pos, f.feature, ctx)
return
}
}
getPlacements(pos, config?.default, ctx)
},
simple_random_selector: (config, pos, ctx) => {
const feature = config?.features?.[ctx.nextInt(config?.features?.length ?? 0)]
getPlacements(pos, feature, ctx)
},
}
const Decorators: {
[key: string]: (config: any, pos: BlockPos, ctx: PlacementContext) => BlockPos[],
} = {
chance: (config, pos, ctx) => {
return ctx.nextFloat() < 1 / (config?.chance ?? 1) ? [pos] : []
},
count: (config, pos, ctx) => {
return new Array(sampleInt(config?.count ?? 1, ctx)).fill(pos)
},
count_extra: (config, pos, ctx) => {
let count = config?.count ?? 1
if (ctx.nextFloat() < config.extra_chance ?? 0){
count += config.extra_count ?? 0
}
return new Array(count).fill(pos)
},
count_multilayer: (config, pos, ctx) => {
return new Array(sampleInt(config?.count ?? 1, ctx)).fill(pos)
.map(p => [
p[0] + ctx.nextInt(16),
p[1],
p[2] + ctx.nextInt(16),
])
},
count_noise: (config, pos, ctx) => {
const noise = ctx.biomeInfoNoise.sample(pos[0] / 200, 0, pos[2] / 200)
const count = noise < config.noise_level ? config.below_noise : config.above_noise
return new Array(count).fill(pos)
},
count_noise_biased: (config, pos, ctx) => {
const factor = Math.max(1, config.noise_factor)
const noise = ctx.biomeInfoNoise.sample(pos[0] / factor, 0, pos[2] / factor)
const count = Math.max(0, Math.ceil((noise + (config.noise_offset ?? 0)) * config.noise_to_count_ratio))
return new Array(count).fill(pos)
},
dark_oak_tree: (_config, pos, ctx) => {
return [...new Array(16)].map((_, i) => {
const x = Math.floor(i / 4) * 4 + 1 + ctx.nextInt(3) + pos[0]
const y = Math.max(ctx.seaLevel, terrain[clamp(0, 63, x)])
const z = Math.floor(i % 4) * 4 + 1 + ctx.nextInt(3) + pos[2]
return [x, y, z]
})
},
decorated: (config, pos, ctx) => {
return getPositions(pos, config?.outer, ctx).flatMap(p => {
return getPositions(p, config?.inner, ctx)
})
},
depth_average: (config, pos, ctx) => {
const y = ctx.nextInt(config?.spread ?? 0) + ctx.nextInt(config?.spread ?? 0) - (config.spread ?? 0) + (config?.baseline ?? 0)
return decorateY(pos, y)
},
emerald_ore: (_config, pos, ctx) => {
const count = 3 + ctx.nextInt(6)
return [...new Array(count)].map(() => [
pos[0] + ctx.nextInt(16),
4 + ctx.nextInt(28),
pos[2] + ctx.nextInt(16),
])
},
fire: (config, pos, ctx) => {
const count = 1 + ctx.nextInt(ctx.nextInt(sampleInt(config?.count, ctx)))
return [...new Array(count)].map(() => [
pos[0] + ctx.nextInt(16),
ctx.nextInt(128),
pos[2] + ctx.nextInt(16),
])
},
glowstone: (config, pos, ctx) => {
const count = ctx.nextInt(1 + ctx.nextInt(sampleInt(config?.count, ctx)))
return [...new Array(count)].map(() => [
pos[0] + ctx.nextInt(16),
ctx.nextInt(128),
pos[2] + ctx.nextInt(16),
])
},
heightmap: (_config, pos, ctx) => {
const y = Math.max(ctx.seaLevel, terrain[clamp(0, 63, pos[0])])
return decorateY(pos, y)
},
heightmap_spread_double: (_config, pos, ctx) => {
const y = Math.max(ctx.seaLevel, terrain[clamp(0, 63, pos[0])])
return decorateY(pos, ctx.nextInt(y * 2))
},
heightmap_world_surface: (_config, pos, ctx) => {
const y = Math.max(ctx.seaLevel, terrain[clamp(0, 63, pos[0])])
return decorateY(pos, y)
},
iceberg: (_config, pos, ctx) => {
return [[
pos[0] + 4 + ctx.nextInt(8),
pos[1],
pos[2] + 4 + ctx.nextInt(8),
]]
},
lava_lake: (config, pos, ctx) => {
if (ctx.nextInt((config.chance ?? 1) / 10) === 0) {
const y = ctx.nextInt(ctx.nextInt(256 - 8) + 8)
if (y < ctx.seaLevel || ctx.nextInt((config?.chance ?? 1) / 8) == 0) {
const x = ctx.nextInt(16) + pos[0]
const z = ctx.nextInt(16) + pos[2]
return [[x, y, z]]
}
}
return []
},
nope: (_config, pos) => {
return [pos]
},
range: (config, pos, ctx) => {
const y = ctx.nextInt((config?.maximum ?? 1) - (config?.top_offset ?? 0)) + (config?.bottom_offset ?? 0)
return decorateY(pos, y)
},
range_biased: (config, pos, ctx) => {
const y = ctx.nextInt(ctx.nextInt((config?.maximum ?? 1) - (config?.top_offset ?? 0)) + (config?.bottom_offset ?? 0))
return decorateY(pos, y)
},
range_very_biased: (config, pos, ctx) => {
const y = ctx.nextInt(ctx.nextInt(ctx.nextInt((config?.maximum ?? 1) - (config?.top_offset ?? 0)) + (config?.bottom_offset ?? 0)) + (config?.bottom_offset ?? 0))
return decorateY(pos, y)
},
spread_32_above: (_config, pos, ctx) => {
const y = ctx.nextInt(pos[1] + 32)
return decorateY(pos, y)
},
top_solid_heightmap: (_config, pos) => {
const y = terrain[clamp(0, 63, pos[0])]
return decorateY(pos, y)
},
magma: (_config, pos, ctx) => {
const y = ctx.nextInt(pos[1] + 32)
return decorateY(pos, y)
},
square: (_config, pos, ctx) => {
return [[
pos[0] + ctx.nextInt(16),
pos[1],
pos[2] + ctx.nextInt(16),
]]
},
surface_relative_threshold: (config, pos) => {
const height = terrain[clamp(0, 63, pos[0])]
const min = height + (config?.min_inclusive ?? -Infinity)
const max = height + (config?.max_inclusive ?? Infinity)
return (pos[1] < min || pos[1] > max) ? [pos] : []
},
water_lake: (config, pos, ctx) => {
if (ctx.nextInt(config.chance ?? 1) === 0) {
return [[
pos[0] + ctx.nextInt(16),
ctx.nextInt(256),
pos[2] + ctx.nextInt(16),
]]
}
return []
},
}
// 1.18 and after
function modifyPlacement(pos: BlockPos, placement: any[], ctx: PlacementContext) {
let positions = [pos]
for (const modifier of placement) {
const modifierFn = PlacementModifiers[normalizeId(modifier?.type ?? 'nope')]
if (!modifierFn) continue
positions = positions.flatMap(pos =>
PlacementModifiers[normalizeId(modifier.type)](modifier, pos, ctx)
)
}
for (const pos of positions) {
ctx.placements.push([pos, 0])
}
}
const PlacementModifiers: {
[key: string]: (config: any, pos: BlockPos, ctx: PlacementContext) => BlockPos[],
} = {
count: ({ count }, pos, ctx) => {
return new Array(sampleInt(count ?? 1, ctx)).fill(pos)
},
count_on_every_layer: ({ count }, pos, ctx) => {
return new Array(sampleInt(count ?? 1, ctx)).fill(pos)
.map(p => [
p[0] + ctx.nextInt(16),
p[1],
p[2] + ctx.nextInt(16),
])
},
environment_scan: ({}, pos) => {
return [pos]
},
height_range: ({ height }, pos, ctx) => {
return decorateY(pos, sampleHeight(height, ctx))
},
heightmap: ({}, pos, ctx) => {
const y = Math.max(ctx.seaLevel, terrain[clamp(0, 63, pos[0])])
return decorateY(pos, y)
},
in_square: ({}, pos, ctx) => {
return [[
pos[0] + ctx.nextInt(16),
pos[1],
pos[2] + ctx.nextInt(16),
]]
},
noise_based_count: ({ noise_to_count_ratio, noise_factor, noise_offset }, pos, ctx) => {
const factor = Math.max(1, noise_factor)
const noise = ctx.biomeInfoNoise.sample(pos[0] / factor, 0, pos[2] / factor)
const count = Math.max(0, Math.ceil((noise + (noise_offset ?? 0)) * noise_to_count_ratio))
return new Array(count).fill(pos)
},
noise_threshold_count: ({ noise_level, below_noise, above_noise }, pos, ctx) => {
const noise = ctx.biomeInfoNoise.sample(pos[0] / 200, 0, pos[2] / 200)
const count = noise < noise_level ? below_noise : above_noise
return new Array(count).fill(pos)
},
random_offset: ({ xz_spread, y_spread }, pos, ctx) => {
return [[
pos[0] + sampleInt(xz_spread, ctx),
pos[1] + sampleInt(y_spread, ctx),
pos[2] + sampleInt(xz_spread, ctx),
]]
},
rarity_filter: ({ chance }, pos, ctx) => {
return ctx.nextFloat() < 1 / (chance ?? 1) ? [pos] : []
},
}