Skip to content

Commit 2912a03

Browse files
committed
ChunkSectionDecoded
1 parent f027c31 commit 2912a03

File tree

12 files changed

+250
-42
lines changed

12 files changed

+250
-42
lines changed

coffee/client/index.coffee

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {AnimatedTextureAtlas} from './module/AnimatedTextureAtlas.js'
1212
import {Players} from './module/Players.js'
1313
import {RandomNick} from './module/RandomNick.js'
1414

15-
1615
class TerrainWorker
1716
constructor: (options)->
1817
@worker=new Worker "module/TerrainWorker.js", {type:'module'}
@@ -47,9 +46,12 @@ class TerrainWorker
4746
}
4847

4948
init = ()->
49+
5050
#Terrain worker
5151
worker=new TerrainWorker
5252

53+
chunkWorker=new Worker "module/ChunkWorker.js", {type:'module'}
54+
5355
#canvas,renderer,camera,lights
5456
canvas=document.querySelector '#c'
5557
renderer=new THREE.WebGLRenderer {
@@ -112,8 +114,14 @@ init = ()->
112114
socket.on "blockUpdate",(block)->
113115
terrain.setVoxel block...
114116
return
115-
socket.on "mapChunk", (chunk)->
116-
# console.log chunk
117+
socket.on "mapChunk", (sections,x,z)->
118+
# console.log("Recieved mapChunk "+x+" "+z)
119+
chunkWorker.postMessage {
120+
type:"computeSections"
121+
data:{
122+
sections,x,z
123+
}
124+
}
117125
players=new Players {socket,scene,al}
118126
socket.on "playerUpdate",(data)->
119127
players.update data
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
class BitArray
2+
constructor: (options)->
3+
if options is null
4+
return
5+
if not options.bitsPerValue > 0
6+
console.error 'bits per value must at least 1'
7+
if not (options.bitsPerValue <= 32)
8+
console.error 'bits per value exceeds 32'
9+
valuesPerLong = Math.floor 64 / options.bitsPerValue
10+
length = Math.ceil(options.capacity / valuesPerLong)
11+
if not options.data
12+
options.data = Array(length * 2).fill(0)
13+
valueMask = (1 << options.bitsPerValue) - 1
14+
15+
@data = options.data
16+
@capacity = options.capacity
17+
@bitsPerValue = options.bitsPerValue
18+
@valuesPerLong = valuesPerLong
19+
@valueMask = valueMask
20+
return
21+
get:(index)->
22+
if not (index >= 0 && index < @capacity)
23+
console.error 'index is out of bounds'
24+
startLongIndex = Math.floor index / @valuesPerLong
25+
indexInLong = (index - startLongIndex * @valuesPerLong) * @bitsPerValue
26+
if indexInLong >= 32
27+
indexInStartLong = indexInLong - 32
28+
startLong = @data[startLongIndex * 2 + 1]
29+
return (startLong >>> indexInStartLong) & @valueMask
30+
startLong = @data[startLongIndex * 2]
31+
indexInStartLong = indexInLong
32+
result = startLong >>> indexInStartLong
33+
endBitOffset = indexInStartLong + @bitsPerValue
34+
if endBitOffset > 32
35+
endLong = @data[startLongIndex * 2 + 1]
36+
result |= endLong << (32 - indexInStartLong)
37+
return result & @valueMask
38+
length: () ->
39+
return @data.length / 2
40+
getBitsPerValue: ()->
41+
return @bitsPerValue
42+
43+
class ChunkDecoder
44+
constructor: (options)->
45+
#NOTHING
46+
getBlockIndex: (pos)->
47+
return (pos.y << 8) | (pos.z << 4) | pos.x
48+
computeSections: (packet)->
49+
sections=packet.sections
50+
num=0
51+
for i in sections
52+
num+=1
53+
if i isnt null
54+
solidBlockCount=i.solidBlockCount
55+
palette=i.palette
56+
data=new BitArray i.data
57+
pos={
58+
x:0
59+
y:0
60+
z:0
61+
}
62+
base=[]
63+
for x in [0..15]
64+
for y in [0..15]
65+
for z in [0..255]
66+
base.push(data.get(@getBlockIndex({x,y,z})))
67+
console.log "Computed chunk section "+packet.x+" "+packet.z+" "+num
68+
69+
70+
addEventListener "message", (e)->
71+
fn = handlers[e.data.type]
72+
if not fn
73+
throw new Error('no handler for type: ' + e.data.type)
74+
fn(e.data.data)
75+
return
76+
77+
cd=new ChunkDecoder
78+
79+
handlers={
80+
computeSections:(data)->
81+
cd.computeSections data
82+
}

coffee/index.coffee

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11

2-
el=require "electron"
2+
opn=require "opn"
33
fs=require "fs"
4-
54
config=JSON.parse fs.readFileSync(__dirname+"/config.json")
65
require("./server")(config)
76

8-
el.app.on 'ready', ()->
9-
size=el.screen.getPrimaryDisplay().size
10-
win = new el.BrowserWindow({ width:size.width, height:size.height,icon:__dirname+"/client/assets/images/icon.png"})
11-
win.setMenuBarVisibility(false)
12-
win.setAutoHideMenuBar(true)
13-
win.loadURL("http://#{config.host}:#{config['express-port']}")
7+
opn("http://#{config.host}:#{config['express-port']}")

coffee/server.coffee

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,9 @@ module.exports=(config)->
5454
#On recieve real Map Chunk
5555
socketInfo[socket.id].bot._client.on "map_chunk",(packet)->
5656

57-
# cell=new Chunk()
58-
# cell.load packet.chunkData,packet.bitMap,false,true
59-
# console.log cell.fromJson
60-
io.to(socket.id).emit "mapChunk", packet
57+
cell=new Chunk()
58+
cell.load packet.chunkData,packet.bitMap,false,true
59+
io.to(socket.id).emit "mapChunk", cell.sections,packet.x,packet.z
6160

6261
# console.log packet
6362
return

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
"version": "1.0.0",
44
"description": "Render Minecrafta w przegladarce",
55
"scripts": {
6-
"start": "electron .",
6+
"start": "node src/index.js",
77
"coffee": "coffee -o src/ -cw coffee/",
88
"atlas": "node src/atlas.js"
99
},
10-
"main": "src/index.js",
1110
"repository": {
1211
"type": "git",
1312
"url": "git+https://github.com/michaljaz/web-minecraft.git"
@@ -21,6 +20,7 @@
2120
"dependencies": {
2221
"canvas": "^2.6.1",
2322
"child_process": "^1.0.2",
23+
"custom-electron-titlebar": "^3.2.5",
2424
"express": "^4.17.1",
2525
"flying-squid": "^1.3.2",
2626
"http": "0.0.1-security",
@@ -32,7 +32,6 @@
3232
"terminal-kit": "^1.44.0"
3333
},
3434
"devDependencies": {
35-
"coffeescript": "^2.5.1",
36-
"electron": "^10.1.5"
35+
"coffeescript": "^2.5.1"
3736
}
3837
}

src/client/index.js

Lines changed: 11 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/client/module/ChunkWorker.js

Lines changed: 124 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"express-port":8080,
44
"host":"localhost",
55
"realServer":{
6-
"ip":"mc.grok.eu",
6+
"ip":"localhost",
77
"port":25565
88
}
99
}

src/index.js

Lines changed: 3 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)