forked from Middlerun/lovecraft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chunk.lua
325 lines (295 loc) · 9.92 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
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
Chunk = {}
function Chunk:new()
local o = {}
setmetatable(o, self)
self.__index = self
o.generated = false
o.treesGenerated = false
o.r = nil
o.c = nil
o.terrain = nil
o.block = {}
o.perlin = {}
o.coalNoise = {}
for r = 1, 32 do
o.block[r] = {}
o.perlin[r] = {}
o.coalNoise[r] = {}
for c = 1, 32 do
o.block[r][c] = UNGENERATED
o.perlin[r][c] = 0
end
end
o.hasDirt = false
o.changed = true
return o
end
function Chunk:generate(seed, chunkR, chunkC)
if chunkR < -3 then
for r = 1, 32 do
self.block[r] = {}
self.perlin[r] = {}
for c = 1, 32 do
self.block[r][c] = AIR
self.perlin[r][c] = 0
end
end
else
self.perlin = self:generatePerlin(seed, chunkR, chunkC)
local absR
local absC
self.block = {}
for r = 1, 32 do
absR = chunkR * 32 + r
self.block[r] = {}
dirtMargin = (128 - absR) * 0.01
for c = 1, 32 do
absC = chunkC * 32 + c
value = self.perlin[r][c]
if absR < 0 then
value = value - absR * 0.02
end
if value > 0.5 then self.block[r][c] = AIR
elseif value > 1.4 - dirtMargin or value < -0.6 then
self.block[r][c] = DIRT
self.hasDirt = true
else self.block[r][c] = STONE
end
if self.block[r][c] == STONE and self.coalNoise[r][c] > 0.08 then self.block[r][c] = COAL_ORE end
end
end
end
self.perlin = {}
self.generated = true
self.changed = true
self.r = chunkR
self.c = chunkC
end
function Chunk:generatePerlin(seed, chunkR, chunkC)
local persistence = 0.55
local N = 6
local amplitude = 1
local compInterp
local compAmplitude
local comp
local data = {}
for r = 1, 32 do
data[r] = {}
for c = 1, 32 do
data[r][c] = 0
end
end
for i = N, 1, -1 do
compInterp = 2^(i-1)
compAmplitude = amplitude * persistence^(N-i)
comp = self:perlinComponent2D(seed+i*571, chunkR, chunkC, compInterp, compAmplitude)
for r = 1, 32 do
for c = 1, 32 do
data[r][c] = data[r][c] + comp[r][c]
end
end
if i == 3 then self.coalNoise = comp end
end
return data
end
function Chunk:perlinComponent2D(seed, chunkR, chunkC, N, amplitude)
local topEdge1 = chunkR * 32 + 1
local bottomEdge1 = (chunkR + 1) * 32 + 1
local leftEdge1 = chunkC * 32 + 1
local rightEdge1 = (chunkC + 1) * 32 + 1
local topEdge2 = math.floor((topEdge1 - 1) / N) - 1
local bottomEdge2 = math.ceil ((bottomEdge1 - 1) / N) + 1
local leftEdge2 = math.floor((leftEdge1 - 1) / N) - 1
local rightEdge2 = math.ceil ((rightEdge1 - 1) / N) + 1
local rawData = {}
local finalData = {}
for r = topEdge2, bottomEdge2 do
rawData[r - topEdge2 + 1] = {}
for c = leftEdge2, rightEdge2 do
rawData[r - topEdge2 + 1][c - leftEdge2 + 1] = amplitude * rand:get(seed + r, c)
end
end
local interpData = self:interpolate2D(rawData, chunkR, chunkC, N)
for r = 1, 32 do
finalData[r] = {}
for c = 1, 32 do
finalData[r][c] = interpData[r][c]
end
end
return finalData
end
function Chunk:interpolate2D(values, chunkR, chunkC, N)
local topEdge1 = chunkR * 32 + 1
local bottomEdge1 = (chunkR + 1) * 32 + 1
local leftEdge1 = chunkC * 32 + 1
local rightEdge1 = (chunkC + 1) * 32 + 1
local topEdge2 = math.floor((topEdge1 - 1) / N) - 1
local bottomEdge2 = math.ceil ((bottomEdge1 - 1) / N) + 1
local leftEdge2 = math.floor((leftEdge1 - 1) / N) - 1
local rightEdge2 = math.ceil ((rightEdge1 - 1) / N) + 1
local newData1 = {}
local min
local max
for r = 1, #values do
newData1[r] = {}
for c = 1, #values[r] - 3 do
P = (values[r][c+3] - values[r][c+2]) - (values[r][c] - values[r][c+1])
Q = (values[r][c] - values[r][c+1]) - P
R = (values[r][c+2] - values[r][c])
S = values[r][c+1]
min = math.max(leftEdge1 - 1, (leftEdge2 + c) * N) % N + 1
max = math.min(rightEdge1 - 1, (leftEdge2 + c + 1) * N) % N + N + 1
for j = min, max-1 do
x = j/N
table.insert(newData1[r], P*x^3 + Q*x^2 + R*x + S)
end
end
end
assert(#newData1[1] == 32, "wrong length. chunkR="..chunkR.." chunkC="..chunkC.." N="..N)
local newData2 = {}
for r = 1, 32 do
newData2[r] = {}
end
for c = 1, 32 do
rowCount = 0
for r = 1, #newData1 - 3 do
P = (newData1[r+3][c] - newData1[r+2][c]) - (newData1[r][c] - newData1[r+1][c])
Q = (newData1[r][c] - newData1[r+1][c]) - P
R = (newData1[r+2][c] - newData1[r][c])
S = newData1[r+1][c]
min = math.max(topEdge1 - 1, (topEdge2 + r) * N) % N + 1
max = math.min(bottomEdge1 - 1, (bottomEdge2 + r + 1) * N) % N + N + 1
for j = min, max-1 do
x = j/N
rowCount = rowCount + 1
newData2[rowCount][c] = P*x^3 + Q*x^2 + R*x + S
end
end
end
for r = 1, 32 do
for c = 1, 32 do
assert(newData2[r][c] ~= nil, "nil value, r="..r.." c="..c.." N="..N)
end
end
return newData2
end
function Chunk:getBlock(r, c)
if not self.generated then return UNGENERATED end
if r < 1 or r > 32 or c < 1 or c > 32 then
return self.terrain:getBlock(self.r * 32 + r, self.c * 32 + c)
end
return self.block[r][c]
end
function Chunk:setBlock(r, c, block)
if not self.generated then return end
if r < 1 or r > 32 or c < 1 or c > 32 then
self.terrain:setBlock(self.r * 32 + r, self.c * 32 + c, block)
return
elseif block ~= AIR and block ~= LEAVES and self:getBlock(r+1, c) == GRASS then self:setBlock(r+1, c, DIRT)
end
self.block[r][c] = block
self.changed = true
if r == 1 and self.terrain:hasChunk(self.r-1, self.c) then self.terrain:getChunk(self.r-1, self.c).changed = true end
if r == 32 and self.terrain:hasChunk(self.r+1, self.c) then self.terrain:getChunk(self.r+1, self.c).changed = true end
if c == 1 and self.terrain:hasChunk(self.r, self.c-1) then self.terrain:getChunk(self.r, self.c-1).changed = true end
if c == 32 and self.terrain:hasChunk(self.r, self.c+1) then self.terrain:getChunk(self.r, self.c+1).changed = true end
end
function Chunk:isGenerated()
return self.generated
end
function Chunk:render()
if self.framebuffer == nil then
self.framebuffer = love.graphics.newFramebuffer(512, 512)
self.framebuffer:setFilter("linear", "nearest")
end
if not self.generated then return end
love.graphics.setRenderTarget(self.framebuffer)
love.graphics.setColor(255, 255, 255, 255)
local num, base
for r = 1, 32 do
for c = 1, 32 do
if self.block[r][c] ~= AIR and self.block[r][c] ~= UNGENERATED then
num = 1
if joinsTo(self.block[r][c], self:getBlock(r-1, c), UP) then num = num + 1 end
if joinsTo(self.block[r][c], self:getBlock(r, c+1), RIGHT) then num = num + 2 end
if joinsTo(self.block[r][c], self:getBlock(r+1, c), DOWN) then num = num + 4 end
if joinsTo(self.block[r][c], self:getBlock(r, c-1), LEFT) then num = num + 8 end
base = tileBase(self.block[r][c])
if base ~= nil then love.graphics.draw(images[base][num], (c-1)*16, (r-1)*16) end
love.graphics.draw(images[self.block[r][c]][num], (c-1)*16, (r-1)*16)
end
end
end
love.graphics.setRenderTarget()
self.changed = false
end
function Chunk:renderPerlin()
if self.framebufferPerlin == nil then
self.framebufferPerlin = love.graphics.newFramebuffer(512, 512)
self.framebufferPerlin:setFilter("linear", "nearest")
end
if not self.generated then return end
love.graphics.setRenderTarget(self.framebufferPerlin)
love.graphics.setColor(255, 255, 255, 255)
for r = 1, 32 do
for c = 1, 32 do
if self.block[r][c] ~= AIR then
love.graphics.setColor(128 + 80 * self.perlin[r][c], 128 + 80 * self.perlin[r][c], 128 + 80 * self.perlin[r][c], 255)
love.graphics.rectangle("fill", (c-1)*16, (r-1)*16, 16, 16)
end
end
end
love.graphics.setRenderTarget()
end
function Chunk:generateTrees()
if self.treesGenerated or not self.hasDirt or self.r > 1 then return end
local canGenerate = true
for r = -1, 0 do
for c = -1, 1 do
if not self.terrain:hasChunk(self.r + r, self.c + c) or not self.terrain:getChunk(self.r + r, self.c + c).generated then
canGenerate = false
end
end
end
if canGenerate then
local canPlantTree
local height, maxHeight
local radius
math.randomseed(self.terrain:getSeed() + 13669 * self.r + self.c)
for r = 1, 32 do
for c = 1, 32 do
if self:getBlock(r, c) == DIRT then
if self:getBlock(r-1, c) == AIR or self:getBlock(r-1, c) == LEAVES then
if self.r * 32 + r < math.random() * 32 then self:setBlock(r, c, GRASS) end
end
if math.random() < 0.1 then
canPlantTree = true
for i = 1, 5 do
if self:getBlock(r-i, c) ~= AIR and self:getBlock(r-i, c) ~= LEAVES then canPlantTree = false end
end
if canPlantTree then
-- Plant tree
maxHeight = 5 + math.floor(6 * math.random())
for i = 1, maxHeight do
if self:getBlock(r-i-1, c) ~= AIR and self:getBlock(r-i-1, c) ~= LEAVES then break end
self:setBlock(r-i, c, WOOD)
height = i
end
radius = math.floor(height/3) + 1
for r2 = -radius, radius do
for c2 = -radius, radius do
if pythag(r2, c2) <= radius + 0.5 and self:getBlock(r-height+r2, c+c2) == AIR then self:setBlock(r-height+r2, c+c2, LEAVES) end
end
end
end
end
end
end
end
self.treesGenerated = true
end
end
function Chunk:draw(view)
if self.framebuffer == nil or self.changed then self:render() end
love.graphics.draw(self.framebuffer, (32*self.c-view.x)*view.zoom + love.graphics.getWidth()/2, (32*self.r-view.y)*view.zoom+love.graphics.getHeight()/2, 0, view.zoom/16, view.zoom/16)
end