-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_weird.lua
440 lines (300 loc) · 7.81 KB
/
gen_weird.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
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
--------------------------------------------------
-- EXPERIMENTAL GENERATION OF WEIRD SHAPES --
-- (c) 2014 Andrew Apted, all rights reserved --
--------------------------------------------------
gui =
{
random = math.random
}
require '_util'
SHOW_GHOST = false
SHOW_STAIRCASE = false
ALLOW_CLOSED_SQUARES = true
-- lower this to make larger areas
T_BRANCH_PROB = 65
if arg[1] then
local seed = 0 + arg[1]
print("Seed =", seed)
math.randomseed(seed)
end
-- class POINT
--[[
gx, gy : grid coordinate
neighbor : table[DIR] --> POINT
edges : table[DIR] --> true if an edge to that neighbor
num_edges : number
dead : boolean -- true if cannot make any more edges off here
ghost : table[DIR] --> edges which were killed (dead ends)
limit_edges : number -- do not allow more than this
--]]
GRID = {}
GRID_W = 56
GRID_H = 38
function create_points()
GRID = table.array_2D(GRID_W, GRID_H)
for gx = 1, GRID_W do
for gy = 1, GRID_H do
local P =
{
gx = gx,
gy = gy,
neighbor = {},
edge = {},
ghost = {},
num_edges = 0
}
GRID[gx][gy] = P
P.limit_edges = rand.sel(T_BRANCH_PROB, 3, 2)
end
end
-- link neighbors
for gx = 1, GRID_W do
for gy = 1, GRID_H do
local P = GRID[gx][gy]
for dir = 1, 9 do
if dir ~= 5 then
local nx, ny = geom.nudge(gx, gy, dir)
if table.valid_pos(GRID, nx, ny) then
local N = GRID[nx][ny]
assert(N)
P.neighbor[dir] = N
end
end
end
end
end
end
function add_edge(gx, gy, dir)
local P = GRID[gx][gy]
P.edge[dir] = true
P.num_edges = P.num_edges + 1
local N = P.neighbor[dir]
assert(N)
N.edge[10 - dir] = true
N.num_edges = N.num_edges + 1
end
function remove_edge(gx, gy, dir)
local P = GRID[gx][gy]
local N = P.neighbor[dir]
assert(N)
assert(P.edge[dir])
assert(N.edge[10 - dir])
P.edge[dir] = nil
N.edge[10 - dir] = nil
P.ghost[dir] = true
N.ghost[10 - dir] = true
P.num_edges = P.num_edges - 1
N.num_edges = N.num_edges - 1
end
function is_diagonal_blocked(P, dir)
-- not a diagonal?
if not (dir == 1 or dir == 3 or dir == 7 or dir == 9) then
return false
end
local L_dir = geom.LEFT_45[dir]
local N = P.neighbor[L_dir]
if not N then return true end
return N.edge[geom.RIGHT[dir]]
end
function would_close_a_square(P, dir, N)
if ALLOW_CLOSED_SQUARES then return false end
for pass = 1, 2 do
local dir2 = sel(pass == 1, geom.LEFT[dir], geom.RIGHT[dir])
local P2 = P.neighbor[dir2]
if P2 then
if P.edge[dir2] and N.edge[dir2] and P2.edge[dir] then
return true
end
end
end
return false
end
function eval_edge_at_point(P, dir)
-- returns < 0 if impossible, score > 0 if possible
local N = P.neighbor[dir]
if not N then return -1 end
if N.dead then return -1 end
-- already an edge there?
if P.edge[dir] then return -1 end
-- ensure it does not cross another diagonal
if is_diagonal_blocked(P, dir) then return -1 end
-- rule # 1 : no more than 3 edges at any point
if P.num_edges >= P.limit_edges then return -1 end
if N.num_edges >= N.limit_edges then return -1 end
-- rule # 2 : never make sharp (45-degree) angles
local L_dir = geom. LEFT_45[dir]
local R_dir = geom.RIGHT_45[dir]
if P.edge[L_dir] or P.edge[R_dir] then return -1 end
if N.edge[10 - L_dir] or N.edge[10 - R_dir] then return -1 end
if would_close_a_square(P, dir, N) then return -1 end
if P.no_diagonals or N.no_diagonals then
if dir == 1 or dir == 3 or dir == 7 or dir == 9 then return -1 end
end
if P.no_straights or N.no_straights then
if dir == 2 or dir == 4 or dir == 6 or dir == 8 then return -1 end
end
-- OK --
return 1
end
function try_edge_at_point(P)
assert(not P.dead)
local tab = {}
for dir = 1, 9 do
if dir ~= 5 then
local score = eval_edge_at_point(P, dir)
if score > 0 then
tab[dir] = score
end
end
end
-- nothing was possible
if table.empty(tab) then
P.dead = true
return nil
end
local dir = rand.key_by_probs(tab)
add_edge(P.gx, P.gy, dir)
-- indicate success by returning neighbor
return P.neighbor[dir]
end
function try_add_edge()
local points = {}
for gx = 1, GRID_W do
for gy = 1, GRID_H do
local P = GRID[gx][gy]
if P.num_edges > 0 and P.num_edges < 3 and not P.dead then
table.insert(points, P)
end
end
end
--- print("active_points", #points)
if table.empty(points) then
return
end
local P = rand.pick(points)
-- keep going until hit dead end or shape joins back onto itself
repeat
P = try_edge_at_point(P)
until not (P and P.num_edges == 0)
end
function remove_dud_point(P)
for dir = 1, 9 do
if dir ~= 5 then
if P.edge[dir] then
remove_edge(P.gx, P.gy, dir)
end
end
end
end
function remove_dead_ends()
local found = false
for gx = 1, GRID_W do
for gy = 1, GRID_H do
local P = GRID[gx][gy]
if P.num_edges == 1 then
found = true
remove_dud_point(P)
end
end
end
return found
end
function check_point_is_staircase(P)
-- NOTE: only finds outie corners which can be diagonalized
if P.num_edges ~= 2 then return false end
if P.edge[1] or P.edge[3] or P.edge[7] or P.edge[9] then return false end
if P.edge[4] and P.edge[6] then return false end
if P.edge[2] and P.edge[8] then return false end
--[[
-- skip this point if a connected neighbor point is a staircase
for dir = 1, 9 do
if dir ~= 5 then
if P.edge[dir] and P.neighbor[dir].is_staircase then
return false
end
end
end
--]]
local x_dir = sel(P.edge[4], 4, 6)
local y_dir = sel(P.edge[2], 2, 8)
assert(P.edge[x_dir])
assert(P.edge[y_dir])
local NX = P.neighbor[x_dir]
local NY = P.neighbor[y_dir]
assert(NX and NY)
-- diagonal direction from NX
local corner
if x_dir == 4 then
corner = sel(y_dir == 2, 3, 9)
else
corner = sel(y_dir == 2, 1, 7)
end
-- check for sharp angles (< 90) at these neighbor points
if NX.edge[y_dir] or NY.edge[x_dir] then return false end
return true
end
function find_staircases()
for gx = 1, GRID_W do
for gy = 1, GRID_H do
local P = GRID[gx][gy]
P.is_staircase = check_point_is_staircase(P)
end
end
end
function wr_line(fp, x1, y1, x2, y2, color, width)
fp:write(string.format('<line x1="%d" y1="%d" x2="%d" y2="%d" stroke="%s" stroke-width="%d" />\n',
x1, y1, x2, y2, color, width or 1))
end
function save_as_svg()
local SIZE = 16
local fp = io.open("_weird.svg", "w")
if not fp then error("Cannot create file") end
-- header
fp:write('<svg xmlns="http://www.w3.org/2000/svg" version="1.1">\n')
-- grid
local max_x = GRID_W * SIZE
local max_y = GRID_H * SIZE
for x = 1, GRID_W do
wr_line(fp, x * SIZE, SIZE, x * SIZE, max_y, "#bbb")
end
for y = 1, GRID_H do
wr_line(fp, SIZE, y * SIZE, max_x, y * SIZE, "#bbb")
end
-- points
for x = 1, GRID_W do
for y = 1, GRID_H do
local P = GRID[x][y]
local x1 = x * SIZE
local y1 = (GRID_H - y + 1) * SIZE
for dir = 6,9 do
local N = P.neighbor[dir]
if N then
local x2 = N.gx * SIZE
local y2 = (GRID_H - N.gy + 1) * SIZE
if P.edge[dir] then
wr_line(fp, x1, y1, x2, y2, "#00f", 3)
elseif P.ghost[dir] and SHOW_GHOST then
wr_line(fp, x1, y1, x2, y2, "#f00", 1)
end
end
end -- dir
if P.is_staircase and SHOW_STAIRCASE then
fp:write(string.format('<circle cx="%d" cy="%d" r="5" fill="#f0f" />\n', x1, y1))
end
end -- x, y
end
-- end
fp:write('</svg>\n')
fp:close()
end
-- main --
create_points()
add_edge(GRID_W / 2, GRID_H / 2, 6)
for pass = 1, 4 do
for loop = 1, GRID_W * GRID_H * 2 do
try_add_edge()
end
while remove_dead_ends() do end
end
find_staircases()
save_as_svg()