-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdiamondsquare.lua
More file actions
500 lines (247 loc) · 7.59 KB
/
Copy pathdiamondsquare.lua
File metadata and controls
500 lines (247 loc) · 7.59 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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = pcall(require, 'compat53.module'); if p then _tl_compat = m end end; local assert = _tl_compat and _tl_compat.assert or assert; local coroutine = _tl_compat and _tl_compat.coroutine or coroutine; local io = _tl_compat and _tl_compat.io or io; local ipairs = _tl_compat and _tl_compat.ipairs or ipairs; local load = _tl_compat and _tl_compat.load or load; local math = _tl_compat and _tl_compat.math or math; local pcall = _tl_compat and _tl_compat.pcall or pcall; local string = _tl_compat and _tl_compat.string or string
require('love')
local Pipeline = require('pipeline')
local format = string.format
local inspect = require("inspect")
require('diamondsquare_common')
local DiamonAndSquare = {State = {}, }
local DiamonAndSquare_mt = {
__index = DiamonAndSquare,
}
local serpent = require('serpent')
function DiamonAndSquare:setPosition(x, y)
self.pipeline:openPushAndClose(self.renderobj_name, 'set_position', x, y)
end
function DiamonAndSquare:render()
self.pipeline:openPushAndClose(self.renderobj_name, 'flush')
end
function DiamonAndSquare:send2render()
local uncompressed = serpent.dump(self.map)
local compress = love.data.compress
local compressed = compress('string', 'gzip', uncompressed, 9)
print('#compressed', #compressed)
self.pipeline:openPushAndClose(
self.renderobj_name,
'map', self.mapSize, compressed)
self.pipeline:openPushAndClose(
self.renderobj_name,
'set_rez', self.rez)
end
function DiamonAndSquare:setRez(rez)
self.rez = rez
self.pipeline:openPushAndClose(
self.renderobj_name,
'set_rez', self.rez)
end
function DiamonAndSquare:load(fname)
local data, size = love.filesystem.read(fname)
if data then
local func
local ok, errmsg = pcall(function()
func = load(data)()
end)
if not ok then
local msg_part = fname .. ': ' .. errmsg
print('Could not load DiamonAndSquare from ' .. msg_part)
end
self.mapSize = func.mapSize
self.map = func.map
else
local msg_part = fname .. ': ' .. tostring(size)
error('Could not load DiamonAndSquare from ' .. msg_part)
end
end
function DiamonAndSquare:save(_)
end
function DiamonAndSquare:newCoroutine()
return coroutine.create(function()
local stop = false
repeat
self:square()
coroutine.yield()
stop = self:diamond()
coroutine.yield()
until stop
end)
end
function DiamonAndSquare:eval()
local filenum = 0
print('------------------------------------------------------------')
self:printMap2File(filenum)
filenum = filenum + 1
print('------------------------------------------------------------')
local coro = coroutine.create(function()
local stop = false
repeat
self:square()
self:printMap2File(filenum)
filenum = filenum + 1
coroutine.yield()
stop = self:diamond()
self:printMap2File(filenum)
filenum = filenum + 1
until stop
self:normalizeInplace()
self:printMap2File(filenum)
filenum = filenum + 1
end)
local ok
ok = coroutine.resume(coro)
while ok do
ok = coroutine.resume(coro)
end
return self
end
function DiamonAndSquare:normalizeInplace()
for i = 1, self.mapSize do
for j = 1, self.mapSize do
local c = self.map[i] and self.map[i][j] or nil
if c then
if c > 1 then
self.map[i][j] = 1
elseif c < 0 then
self.map[i][j] = 0
end
end
end
end
end
function DiamonAndSquare.new(
mapn,
rng,
pl)
if type(mapn) ~= 'number' then
error('No mapn parameter in constructor.')
end
local self
self = setmetatable({}, DiamonAndSquare_mt)
assert(pl, "pipeline is nil")
self.pipeline = pl
self.renderobj_name = "diamondsquare" .. renderobj_counter
renderobj_counter = renderobj_counter + 1
self.pipeline:pushCodeFromFileRoot(
self.renderobj_name, 'rdr_diamondsquare.lua')
self.rng = rng
self.mapn = mapn
self:reset()
self.rez = 8
return self
end
function DiamonAndSquare:reset()
self.map = {}
self.mapSize = math.ceil(2 ^ self.mapn) + 1
self.chunkSize = self.mapSize - 1
local corners = {
{
i = 1,
j = 1,
},
{
i = self.mapSize,
j = 1,
},
{
i = self.mapSize,
j = self.mapSize,
},
{
i = 1,
j = self.mapSize,
},
}
for _, corner in ipairs(corners) do
local i, j = corner.i, corner.j
local value = self.rng()
value = 0.5 - 0.5 * math.cos(value * math.pi)
self.map[i] = self.map[i] or {}
self.map[i][j] = value
end
end
local floor = math.floor
function DiamonAndSquare:value(i, j)
if (i - floor(i) > 0.) then
end
if (j - floor(j) > 0.) then
end
if self.map[floor(i)] and self.map[floor(i)][floor(j)] then
return self.map[floor(i)][floor(j)]
else
print(format("value is NULL for [%d, %d]", i, j));
end
end
function DiamonAndSquare:random(min, max)
return min + self.rng() * (max - min)
end
function DiamonAndSquare:squareValue(i, j)
local min, max
local corners = {
{ i = i, j = j },
{ i = i + self.chunkSize, j = j },
{ i = i, j = j + self.chunkSize },
{ i = i + self.chunkSize, j = j + self.chunkSize },
}
for _, corner in ipairs(corners) do
local v = self:value(corner.i, corner.j)
if v then
min = min and math.min(min, v) or v
max = max and math.max(max, v) or v
end
end
return min, max
end
function DiamonAndSquare:square()
print('square')
local half = math.floor(self.chunkSize / 2)
for i = 1, self.mapSize - 1, self.chunkSize do
for j = 1, self.mapSize - 1, self.chunkSize do
local min, max = self:squareValue(i, j)
self.map[i + half] = self.map[i + half] or {}
self.map[i + half][j + half] = self:random(min, max)
end
end
end
function DiamonAndSquare:diamondValue(i, j, half)
local min, max = 1000., -1000.
local corners = {
{ i = i, j = j - half },
{ i = i + half, j = j },
{ i = i, j = j + half },
{ i = i - half, j = j },
}
for _, corner in ipairs(corners) do
local v = self:value(corner.i, corner.j)
if v then
min = math.min(min, v)
max = math.max(max, v)
end
end
return min, max
end
function DiamonAndSquare:diamond()
print('--------------------- diamond ---------------------')
local half = self.chunkSize / 2
print('half', half)
local ceil = math.ceil
for i = 1, self.mapSize, half do
for j = (i + half) % self.chunkSize, self.mapSize, self.chunkSize do
local min, max = self:diamondValue(i, j, half)
self.map[ceil(i)] = self.map[ceil(i)] or {}
self.map[ceil(i)][ceil(j)] = self:random(min, max)
end
end
self.chunkSize = ceil(self.chunkSize / 2)
return self.chunkSize <= 1
end
function DiamonAndSquare:getFieldSize()
return self.rez * self.mapSize
end
function DiamonAndSquare:printMap2File(filenum)
assert(type(filenum) == 'number' and filenum >= 0)
local file = io.open(string.format('map.lua.%d.txt', filenum), "w+")
for _, v in ipairs(self.map) do
local str = inspect(v)
file:write(str .. '\n')
end
file:close()
end
return DiamonAndSquare