-
Notifications
You must be signed in to change notification settings - Fork 2
/
chunk.lua
255 lines (203 loc) · 6.8 KB
/
chunk.lua
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
local Chunk = Object:extend()
local blockTypes = require "blocks"
local Vector = require "vector"
local Matrix = require "matrix"
local Light = require "light"
local getVertex = require "mesh"
local format = {
{ "VertexPosition", "float", 3 },
{ "VertexTexCoord", "float", 2 },
{ "VertexNormal", "float", 3 },
{ "VertexColor", "byte", 4 },
}
local normals = {
{ 0, 0, 1 },
{ 0, 1, 0 },
{ 0, 0, -1 },
{ 0, -1, 0 },
{ 1, 0, 0 },
{ -1, 0, 0 },
}
local maxVertices = CHUNK_SIZE * CHUNK_HEIGHT * CHUNK_SIZE * 6 * 6
function Chunk:new(x, y, z, world)
self.position = Vector(x * CHUNK_SIZE, y * CHUNK_HEIGHT, z * CHUNK_SIZE)
self.world = world
self.blocks = {}
self.lightMap = {}
self.toUpdate = {}
self.loaded = false
for i = 1, CHUNK_SIZE do
self.blocks[i] = {}
self.lightMap[i] = {}
for j = 1, CHUNK_HEIGHT do
self.blocks[i][j] = {}
self.lightMap[i][j] = {}
for k = 1, CHUNK_SIZE do
local x, y, z = self.position.x + i, self.position.y + j, self.position.z + k
local h = CHUNK_HEIGHT - math.floor(love.math.noise(x/20, z/20, 0) * 16) - 1
local c = math.floor(love.math.noise(x/8, y/4, z/8, 1)*2)
if j == h then
self.blocks[i][j][k] = 2
elseif j <= 4 then
self.blocks[i][j][k] = 1
elseif j < h and j > 16 then
self.blocks[i][j][k] = c * 3
elseif j < h and j <= 16 then
self.blocks[i][j][k] = c
else
self.blocks[i][j][k] = 0
end
self.lightMap[i][j][k] = 0
end
end
end
self.mesh = love.graphics.newMesh(format, maxVertices, "triangles")
self.mesh:setTexture(tileset)
self.model = Matrix()
self.channel = "chunk"..x..y..z
self.thread = love.thread.newThread("load_mesh.lua")
self:updateLight()
end
function Chunk:__tostring()
local nx = math.floor((self.position.x-1) / CHUNK_SIZE)
local nz = math.floor((self.position.z-1) / CHUNK_SIZE)
return "Chunk: "..nx..", "..nz
end
function Chunk.encodeIndex(i, j, k)
return i + (j-1) * CHUNK_SIZE + (k-1) * CHUNK_SIZE * CHUNK_HEIGHT
end
function Chunk.decodeIndex(index)
local i = (index - 1) % CHUNK_SIZE + 1
local j = math.floor((index - 1) / CHUNK_SIZE) % CHUNK_HEIGHT + 1
local k = math.floor((index - 1) / (CHUNK_SIZE * CHUNK_HEIGHT)) % CHUNK_SIZE + 1
return i, j, k
end
function Chunk:load()
self.done = true
local blocks = {}
for i = 0, CHUNK_SIZE + 1 do
blocks[i] = {}
for j = 0, CHUNK_HEIGHT + 1 do
blocks[i][j] = {}
for k = 0, CHUNK_SIZE + 1 do
local x, y, z = self.position.x + i, self.position.y + j, self.position.z + k
blocks[i][j][k] = self.world:getBlock(x, y, z)
end
end
end
self.thread:start(self.position:table(), blocks, self.channel, blockTypes)
end
function Chunk:updateLight()
for i = 1, CHUNK_SIZE do
for j = 1, CHUNK_HEIGHT do
for k = 1, CHUNK_SIZE do
local block = self.blocks[i][j][k]
if block <= 0 then
self.blocks[i][j][k] = self.lightMap[i][j][k] - 8
end
end
end
end
Light.updateSunLight(self)
end
function Chunk:setFace(index, mesh, x, y, z, value)
local function getBlock(i, j, k)
local x, y, z = self.position.x + i, self.position.y + j, self.position.z + k
return self.world:getBlock(x, y, z)
end
for i = 1, 6 do
local vi, vertexData = getVertex(index, i, mesh, x, y, z, value, self.position:table(), getBlock)
self.mesh:setVertex(vi, vertexData)
end
end
function Chunk:updateMesh(start, stop)
local vertices = {}
local v = 1
local cx, cy, cz = self.position:unpack()
for f = start, stop do
local i = math.floor((f-1) / CHUNK_SIZE) + 1
local k = (f-1) % CHUNK_SIZE + 1
for j = 1, CHUNK_HEIGHT do
local block = self.blocks[i][j][k]
local mesh = blockTypes[block]
local x, y, z = i + cx, j + cy, k + cz
self:setFace(0, mesh, i, j, k, self.world:getBlock(x, y, z + 1))
self:setFace(1, mesh, i, j, k, self.world:getBlock(x, y + 1, z))
self:setFace(2, mesh, i, j, k, self.world:getBlock(x, y, z - 1))
self:setFace(3, mesh, i, j, k, self.world:getBlock(x, y - 1, z))
self:setFace(4, mesh, i, j, k, self.world:getBlock(x + 1, y, z))
self:setFace(5, mesh, i, j, k, self.world:getBlock(x - 1, y, z))
end
end
self.mesh:setVertices(vertices)
end
function Chunk:updateBlockMesh(x, y, z)
if y < 1 or y > CHUNK_HEIGHT then return end
-- translate to local coordinates
local i, j, k = x - self.position.x, y - self.position.y, z - self.position.z
local block = self.blocks[i][j][k]
local mesh = blockTypes[block]
self:setFace(0, mesh, i, j, k, self.world:getBlock(x, y, z + 1))
self:setFace(1, mesh, i, j, k, self.world:getBlock(x, y + 1, z))
self:setFace(2, mesh, i, j, k, self.world:getBlock(x, y, z - 1))
self:setFace(3, mesh, i, j, k, self.world:getBlock(x, y - 1, z))
self:setFace(4, mesh, i, j, k, self.world:getBlock(x + 1, y, z))
self:setFace(5, mesh, i, j, k, self.world:getBlock(x - 1, y, z))
end
function Chunk:getBlock(x, y, z)
if y < 1 or y > CHUNK_HEIGHT then return 0 end
-- translate to local coordinates
x = x - self.position.x
y = y - self.position.y
z = z - self.position.z
return self.blocks[x][y][z]
end
function Chunk:setBlock(x, y, z, block)
if y < 1 or y > CHUNK_HEIGHT then return end
-- translate to local coordinates
local i, j, k = x - self.position.x, y - self.position.y, z - self.position.z
if self.blocks[i][j][k] == block then return end
self.blocks[i][j][k] = block
if block > 0 then
Light.removeLight(self, i, j, k, block)
else
Light.addLight(self, i, j, k)
end
self.scheduleUpdate(self, i, j, k)
end
function Chunk:scheduleUpdate(i, j, k)
for dx = -1, 1 do
for dy = -1, 1 do
for dz = -1, 1 do
local x, y, z = i + dx + self.position.x, j + dy + self.position.y, k + dz + self.position.z
local chunk = self.world:getChunk(x, z)
if chunk then
local ci, cj, ck = x - chunk.position.x, y - chunk.position.y, z - chunk.position.z
local index = self.encodeIndex(ci, cj, ck)
chunk.toUpdate[index] = true
end
end
end
end
end
local maxMeshUpdates = 32
function Chunk:update()
local message = love.thread.getChannel(self.channel):pop()
if message then
local vertices, start, count = unpack(message)
self.mesh:setVertices(vertices, start, count)
end
local meshUpdates = 0
for index, v in pairs(self.toUpdate) do
if meshUpdates >= maxMeshUpdates then break end
local i, j, k = self.decodeIndex(index)
self:updateBlockMesh(i + self.position.x, j + self.position.y, k + self.position.z)
self.toUpdate[index] = nil
meshUpdates = meshUpdates + 1
end
end
function Chunk:draw()
love.graphics.getShader():send("modelMatrix", self.model)
love.graphics.draw(self.mesh)
end
return Chunk