forked from PrismarineJS/prismarine-web-client
-
Notifications
You must be signed in to change notification settings - Fork 216
Expand file tree
/
Copy pathworld.ts
More file actions
287 lines (250 loc) · 10.1 KB
/
Copy pathworld.ts
File metadata and controls
287 lines (250 loc) · 10.1 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
import { WorldLightHolder } from 'minecraft-lighting/dist/worldLightHolder'
import Chunks from 'prismarine-chunk'
import mcData from 'minecraft-data'
import { Block } from 'prismarine-block'
import { Vec3 } from 'vec3'
import { WorldBlockProvider } from 'mc-assets/dist/worldBlockProvider'
import moreBlockDataGeneratedJson from '../moreBlockDataGenerated.json'
import legacyJson from '../../../../src/preflatMap.json'
import { defaultMesherConfig, CustomBlockModels, BlockStateModelInfo, getBlockAssetsCacheKey } from './shared'
import { INVISIBLE_BLOCKS } from './worldConstants'
const ignoreAoBlocks = Object.keys(moreBlockDataGeneratedJson.noOcclusions)
function columnKey (x, z) {
return `${x},${z}`
}
function isCube (shapes) {
if (!shapes || shapes.length !== 1) return false
const shape = shapes[0]
return shape[0] === 0 && shape[1] === 0 && shape[2] === 0 && shape[3] === 1 && shape[4] === 1 && shape[5] === 1
}
export type BlockModelPartsResolved = ReturnType<WorldBlockProvider['getAllResolvedModels0_1']>
export type WorldBlock = Omit<Block, 'position'> & {
// todo
isCube: boolean
/** cache */
models?: BlockModelPartsResolved | null
_originalProperties?: Record<string, any>
_properties?: Record<string, any>
}
export class World {
lightHolder = new WorldLightHolder(0, 0)
config = defaultMesherConfig
Chunk: typeof import('prismarine-chunk/types/index').PCChunk
columns = {} as { [key: string]: import('prismarine-chunk/types/index').PCChunk }
blockCache = {}
biomeCache: { [id: number]: mcData.Biome }
preflat: boolean
erroredBlockModel?: BlockModelPartsResolved
customBlockModels = new Map<string, CustomBlockModels>() // chunkKey -> blockModels
sentBlockStateModels = new Set<string>()
blockStateModelInfo = new Map<string, BlockStateModelInfo>()
constructor (version) {
this.Chunk = Chunks(version) as any
this.biomeCache = mcData(version).biomes
this.preflat = !mcData(version).supportFeature('blockStateId')
this.config.version = version
}
getLight (pos: Vec3, isNeighbor = false, skipMoreChecks = false, curBlockName = '') {
const IS_USING_SERVER_LIGHTING = false
// for easier testing
if (!(pos instanceof Vec3)) pos = new Vec3(...pos as [number, number, number])
const { enableLighting, skyLight } = this.config
if (!enableLighting) return 15
// const key = `${pos.x},${pos.y},${pos.z}`
// if (lightsCache.has(key)) return lightsCache.get(key)
const column = this.getColumnByPos(pos)
if (!column || !hasChunkSection(column, pos)) return 15
let result = Math.min(
15,
Math.max(
this.getBlockLight(pos),
Math.min(skyLight, this.getSkyLight(pos))
)
)
const MIN_LIGHT_LEVEL = 2
result = Math.max(result, MIN_LIGHT_LEVEL)
// lightsCache.set(key, result)
if (result === 2 && IS_USING_SERVER_LIGHTING) {
if ([this.getBlock(pos)?.name ?? '', curBlockName].some(x => /_stairs|slab|glass_pane/.exec(x)) && !skipMoreChecks) { // todo this is obviously wrong
const lights = [
this.getLight(pos.offset(0, 1, 0), undefined, true),
this.getLight(pos.offset(0, -1, 0), undefined, true),
this.getLight(pos.offset(0, 0, 1), undefined, true),
this.getLight(pos.offset(0, 0, -1), undefined, true),
this.getLight(pos.offset(1, 0, 0), undefined, true),
this.getLight(pos.offset(-1, 0, 0), undefined, true)
].filter(x => x !== 2)
if (lights.length) {
const min = Math.min(...lights)
result = min
}
}
if (isNeighbor) result = 15 // TODO
}
return result
}
getBlockLight (pos: Vec3) {
return this.lightHolder.getBlockLight(pos.x, pos.y, pos.z)
// return column.getBlockLight(posInChunk(pos))
}
getSkyLight (pos: Vec3) {
return this.lightHolder.getSkyLight(pos.x, pos.y, pos.z)
// return column.getSkyLight(posInChunk(pos))
}
addColumn (x, z, json) {
const chunk = this.Chunk.fromJson(json)
this.columns[columnKey(x, z)] = chunk as any
return chunk
}
removeColumn (x, z) {
delete this.columns[columnKey(x, z)]
}
getColumn (x, z) {
return this.columns[columnKey(x, z)]
}
setBlockStateId (pos: Vec3, stateId) {
if (stateId === undefined) throw new Error('stateId is undefined')
const key = columnKey(Math.floor(pos.x / 16) * 16, Math.floor(pos.z / 16) * 16)
const column = this.columns[key]
// null column means chunk not loaded
if (!column) return false
column.setBlockStateId(posInChunk(pos.floored()), stateId)
return true
}
getColumnByPos (pos: Vec3) {
return this.getColumn(Math.floor(pos.x / 16) * 16, Math.floor(pos.z / 16) * 16)
}
getBlock (pos: Vec3, blockProvider?: WorldBlockProvider, attr?: { hadErrors?: boolean }): WorldBlock | null {
// for easier testing
if (!(pos instanceof Vec3)) pos = new Vec3(...pos as [number, number, number])
const key = columnKey(Math.floor(pos.x / 16) * 16, Math.floor(pos.z / 16) * 16)
const blockPosKey = `${pos.x},${pos.y},${pos.z}`
const modelOverride = this.customBlockModels.get(key)?.[blockPosKey]
const column = this.columns[key]
// null column means chunk not loaded
if (!column) return null
const loc = pos.floored()
const locInChunk = posInChunk(loc)
const stateId = column.getBlockStateId(locInChunk)
const cacheKey = getBlockAssetsCacheKey(stateId, modelOverride)
if (!this.blockCache[cacheKey]) {
const b = column.getBlock(locInChunk) as unknown as WorldBlock
if (modelOverride) {
b.name = modelOverride
}
b.isCube = isCube(b.shapes)
this.blockCache[cacheKey] = b
Object.defineProperty(b, 'position', {
get () {
throw new Error('position is not reliable, use pos parameter instead of block.position')
}
})
if (this.preflat) {
b._properties = {}
const namePropsStr = legacyJson.blocks[b.type + ':' + b.metadata] || findClosestLegacyBlockFallback(b.type, b.metadata, pos)
if (namePropsStr) {
b.name = namePropsStr.split('[')[0]
const propsStr = namePropsStr.split('[')?.[1]?.split(']')
if (propsStr) {
const newProperties = Object.fromEntries(propsStr.join('').split(',').map(x => {
let [key, val] = x.split('=')
if (!isNaN(val)) val = parseInt(val, 10)
return [key, val]
}))
b._properties = newProperties
}
}
}
}
const block: WorldBlock = this.blockCache[cacheKey]
if (block.models === undefined && blockProvider) {
if (!attr) throw new Error('attr is required')
const props = block.getProperties()
try {
// fixme
if (this.preflat) {
if (block.name === 'cobblestone_wall') {
props.up = 'true'
for (const key of ['north', 'south', 'east', 'west']) {
const val = props[key]
if (val === 'false' || val === 'true') {
props[key] = val === 'true' ? 'low' : 'none'
}
}
}
}
const useFallbackModel = !!(this.preflat || modelOverride)
const issues = [] as string[]
const resolvedModelNames = [] as string[]
const resolvedConditions = [] as string[]
block.models = blockProvider.getAllResolvedModels0_1(
{
name: block.name,
properties: props,
},
useFallbackModel,
issues,
resolvedModelNames,
resolvedConditions
)!
// Track block state model info
if (!this.sentBlockStateModels.has(cacheKey)) {
this.blockStateModelInfo.set(cacheKey, {
cacheKey,
issues,
modelNames: resolvedModelNames,
conditions: resolvedConditions
})
}
if (!block.models.length) {
if (block.name !== 'water' && block.name !== 'lava' && !INVISIBLE_BLOCKS.has(block.name)) {
console.debug('[mesher] block to render not found', block.name, props)
}
block.models = null
}
if (block.models && modelOverride) {
const model = block.models[0]
block.transparent = model[0]?.['transparent'] ?? block.transparent
}
} catch (err) {
this.erroredBlockModel ??= blockProvider.getAllResolvedModels0_1({ name: 'errored', properties: {} })
block.models ??= this.erroredBlockModel
console.error(`Critical assets error. Unable to get block model for ${block.name}[${JSON.stringify(props)}]: ` + err.message, err.stack)
attr.hadErrors = true
}
}
if (block.name === 'flowing_water') block.name = 'water'
if (block.name === 'flowing_lava') block.name = 'lava'
if (block.name === 'bubble_column') block.name = 'water' // TODO need to distinguish between water and bubble column
// block.position = loc // it overrides position of all currently loaded blocks
//@ts-expect-error
block.biome = this.biomeCache[column.getBiome(locInChunk)] ?? this.biomeCache[1] ?? this.biomeCache[0]
if (block.name === 'redstone_ore') block.transparent = false
return block
}
shouldMakeAo (block: WorldBlock | null) {
return block?.isCube && !ignoreAoBlocks.includes(block.name)
}
}
const findClosestLegacyBlockFallback = (id, metadata, pos) => {
console.warn(`[mesher] Unknown block with ${id}:${metadata} at ${pos}, falling back`) // todo has known issues
for (const [key, value] of Object.entries(legacyJson.blocks)) {
const [idKey, meta] = key.split(':')
if (idKey === id) return value
}
return null
}
// todo export in chunk instead
const hasChunkSection = (column, pos) => {
if (column._getSection) return column._getSection(pos)
if (column.skyLightSections) {
return column.skyLightSections[getLightSectionIndex(pos, column.minY)] || column.blockLightSections[getLightSectionIndex(pos, column.minY)]
}
if (column.sections) return column.sections[pos.y >> 4]
}
function posInChunk (pos) {
return new Vec3(Math.floor(pos.x) & 15, Math.floor(pos.y), Math.floor(pos.z) & 15)
}
function getLightSectionIndex (pos, minY = 0) {
return Math.floor((pos.y - minY) / 16) + 1
}