-
Notifications
You must be signed in to change notification settings - Fork 210
feat: Client Side Light Engine! #304
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: next
Are you sure you want to change the base?
Changes from 12 commits
245300f
d5c61d8
48ead54
037e297
ace45a9
ec6b249
9f505f8
f18b3a1
e10f610
0fa66e2
b1ba2cd
3cd1ac3
1918c68
b4c72db
1f5b682
5a57d29
2f6191a
f4eab39
79f0fdd
27c55b1
f4f5edd
c97c7e0
c4b9c33
7d224fb
d6f394f
ddf0810
5720cfa
7dba526
e95f84e
90de0d0
f185df9
6be3c5c
56aee16
b8c8f8a
6a8d15b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,35 @@ | ||||||||||||||||||||||||||
| import { LightWorld, createLightEngineForSyncWorld, convertPrismarineBlockToWorldBlock, fillColumnWithZeroLight } from 'minecraft-lighting' | ||||||||||||||||||||||||||
| import { world } from 'prismarine-world' | ||||||||||||||||||||||||||
| import { WorldRendererCommon } from './worldrendererCommon' | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| let lightEngine: LightWorld | null = null | ||||||||||||||||||||||||||
| export const getLightEngine = () => { | ||||||||||||||||||||||||||
| if (!lightEngine) throw new Error('Light engine not initialized') | ||||||||||||||||||||||||||
| return lightEngine | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| export const getLightEngineSafe = () => { | ||||||||||||||||||||||||||
| return lightEngine | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| export const createLightEngine = (world: WorldRendererCommon) => { | ||||||||||||||||||||||||||
| lightEngine = createLightEngineForSyncWorld(world.displayOptions.worldView.world as unknown as world.WorldSync, loadedData, { | ||||||||||||||||||||||||||
| minY: world.worldSizeParams.minY, | ||||||||||||||||||||||||||
| height: world.worldSizeParams.worldHeight, | ||||||||||||||||||||||||||
| enableSkyLight: false, | ||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||
| lightEngine.PARALLEL_CHUNK_PROCESSING = false | ||||||||||||||||||||||||||
| globalThis.lightEngine = lightEngine | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| export const processLightChunk = async (x: number, z: number) => { | ||||||||||||||||||||||||||
| const chunkX = Math.floor(x / 16) | ||||||||||||||||||||||||||
| const chunkZ = Math.floor(z / 16) | ||||||||||||||||||||||||||
| const engine = getLightEngine() | ||||||||||||||||||||||||||
| // fillColumnWithZeroLight(engine.externalWorld, chunkX, chunkZ) | ||||||||||||||||||||||||||
| return engine.receiveUpdateColumn(chunkX, chunkZ) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| export const updateBlockLight = (x: number, y: number, z: number, stateId: number) => { | ||||||||||||||||||||||||||
| const engine = getLightEngine() | ||||||||||||||||||||||||||
| engine.setBlock(x, y, z, convertPrismarineBlockToWorldBlock(mcData.blocks[stateId], loadedData)) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
zardoy marked this conversation as resolved.
Outdated
Comment on lines
+90
to
+93
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incomplete light engine cleanup The Update the function to clean up both engine types: export const destroyLightEngine = () => {
+ if (lightEngineNew) {
+ lightEngineNew.destroy?.()
+ lightEngineNew = null
+ }
lightEngine = null
globalThis.lightEngine = null
}📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -7,10 +7,9 @@ import { Vec3 } from 'vec3' | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { BotEvents } from 'mineflayer' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { proxy } from 'valtio' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import TypedEmitter from 'typed-emitter' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { getItemFromBlock } from '../../../src/chatUtils' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { delayedIterator } from '../../playground/shared' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { playerState } from '../../../src/mineflayer/playerState' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { chunkPos } from './simpleUtils' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { processLightChunk, updateBlockLight } from './lightEngine' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export type ChunkPosKey = string // like '16,16' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type ChunkPos = { x: number, z: number } // like { x: 16, z: 16 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -81,6 +80,7 @@ export class WorldDataEmitter extends (EventEmitter as new () => TypedEmitter<Wo | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| updateBlockLight(position.x, position.y, position.z, stateId) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.emit('blockUpdate', { pos: position, stateId }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -252,13 +252,23 @@ export class WorldDataEmitter extends (EventEmitter as new () => TypedEmitter<Wo | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| async loadChunk (pos: ChunkPos, isLightUpdate = false, reason = 'spiral') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const [botX, botZ] = chunkPos(this.lastPos) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const chunkX = Math.floor(pos.x / 16) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const chunkZ = Math.floor(pos.z / 16) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const dx = Math.abs(botX - Math.floor(pos.x / 16)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const dz = Math.abs(botZ - Math.floor(pos.z / 16)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const dx = Math.abs(botX - chunkX) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const dz = Math.abs(botZ - chunkZ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (dx <= this.viewDistance && dz <= this.viewDistance) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // eslint-disable-next-line @typescript-eslint/await-thenable -- todo allow to use async world provider but not sure if needed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const column = await this.world.getColumnAt(pos['y'] ? pos as Vec3 : new Vec3(pos.x, 0, pos.z)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (column) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const result = await processLightChunk(pos.x, pos.z) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!result) return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potential logic issue with null/empty array check The check If you're trying to check for an empty array, use -if (!result) return
+if (!result || !result.length) return📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (const affectedChunk of result) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (affectedChunk.x === chunkX && affectedChunk.z === chunkZ) continue | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const loadedChunk = this.loadedChunks[`${affectedChunk.x},${affectedChunk.z}`] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!loadedChunk) continue | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void this.loadChunk(new Vec3(affectedChunk.x * 16, 0, affectedChunk.z * 16), true) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Verification agent 🧩 Analysis chainConsider adding safeguards for recursive light propagation The implementation correctly processes light updates and propagates them to affected neighboring chunks. However, if chunks affect each other in a circular pattern, this could potentially lead to excessive recursion. Consider adding a recursion depth counter or tracking processed chunks to prevent potential infinite loops: async loadChunk (pos: ChunkPos, isLightUpdate = false, recursionDepth = 0) {
// existing code...
const column = await this.world.getColumnAt(pos['y'] ? pos as Vec3 : new Vec3(pos.x, 0, pos.z))
if (column) {
const result = await processLightChunk(pos.x, pos.z)
if (!result) return
+ // Prevent excessive recursion
+ if (recursionDepth >= 5) return
for (const affectedChunk of result) {
if (affectedChunk.x === chunkX && affectedChunk.z === chunkZ) continue
const loadedChunk = this.loadedChunks[`${affectedChunk.x},${affectedChunk.z}`]
if (!loadedChunk) continue
- void this.loadChunk(new Vec3(affectedChunk.x * 16, 0, affectedChunk.z * 16), true)
+ void this.loadChunk(new Vec3(affectedChunk.x * 16, 0, affectedChunk.z * 16), true, recursionDepth + 1)
}
// rest of the existing code...
}Let's verify if there are any similar safeguards in the current implementation: 🏁 Script executed: #!/bin/bash
# Check if there's any existing recursion prevention in light processing
# Look for depth tracking or visited chunks in light-related functions
rg -A 3 -B 3 "recursion|depth|visited|processed" --glob "*.ts"
# Look for any maps/sets used to track processed chunks in light engine
ast-grep --pattern $'const $_ = new (Set|Map)<$_>'Length of output: 41150 Action Required: Add Safeguards for Recursive Light Propagation Our verification did not uncover any existing safeguard for recursion in light processing within Please consider introducing a recursion depth counter (or a mechanism to track already processed chunks) to cap recursive calls. For example, a diff similar to the one suggested below would add a guard that stops further recursion after a fixed depth (e.g., 5), making the propagation robust against potential infinite loops: async loadChunk (pos: ChunkPos, isLightUpdate = false, recursionDepth = 0) {
// existing code...
const column = await this.world.getColumnAt(pos['y'] ? pos as Vec3 : new Vec3(pos.x, 0, pos.z))
if (column) {
const result = await processLightChunk(pos.x, pos.z)
if (!result) return
+ // Prevent excessive recursion by capping the recursion depth
+ if (recursionDepth >= 5) return
for (const affectedChunk of result) {
if (affectedChunk.x === chunkX && affectedChunk.z === chunkZ) continue
const loadedChunk = this.loadedChunks[`${affectedChunk.x},${affectedChunk.z}`]
if (!loadedChunk) continue
- void this.loadChunk(new Vec3(affectedChunk.x * 16, 0, affectedChunk.z * 16), true)
+ void this.loadChunk(new Vec3(affectedChunk.x * 16, 0, affectedChunk.z * 16), true, recursionDepth + 1)
}
// rest of the existing code...
}
}Implementing this safeguard should mitigate the risk of infinite recursion during light propagation. 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // const latency = Math.floor(performance.now() - this.lastTime) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // this.debugGotChunkLatency.push(latency) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // this.lastTime = performance.now() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.