-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze.lua
283 lines (246 loc) · 9.29 KB
/
maze.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
Maze = {height, width, exitX = 0, exitY = 0, roomCount = 0,
wall = 0, chestUsed = 1, exit = 2, chest = 3, -- collision
pass = 4, room = 5, key = 6, decoKey = 7, -- no collision
__call = function(self, x, y, val)
if val then
self[y*Maze.width + x] = val
else
return self[y*Maze.width + x]
end
end,
visibility = {}, content = setmetatable({}, Maze), ways = {},
ended = function ()
for i = 2, Maze.height - 1, 2 do
for j = 2, Maze.width - 1, 2 do
if Maze.content(i, j) == Maze.wall then return false end
end
end
return true
end,
deadend = function (x,y)
return (x == Maze.width - 1 or Maze.content(x + 2, y) ~= Maze.wall) and
(y == Maze.height - 1 or Maze.content(x, y + 2) ~= Maze.wall) and
(x == 2 or Maze.content(x - 2, y) ~= Maze.wall) and
(y == 2 or Maze.content(x, y - 2) ~= Maze.wall)
end,
generate = function (width, height)
Maze.width = width
Maze.height = height
Maze.exitX = 0
Maze.exitY = 0
Maze.roomCount = 0
for i=1, Maze.height do
for j=1, Maze.width do
Maze.content(i, j, Maze.wall)
Maze.visibility(i, j, false)
Maze.ways(i, j, 0)
end
end
local x,y
local roomCount = math.floor(math.sqrt(Maze.width * Maze.height))
local check = 0
for l = 1, roomCount do -- Room pre-generation routine
repeat -- Basically - they must generate before Maze
local rHeight, rWidth = 2 * love.math.random(1, 2), 2 * love.math.random(1, 2)
local b = true
repeat -- Choose the center point of room
x = 2*love.math.random(1, (Maze.width - 1)/2) + 2
x = rWidth == 4 and x or x + 1
y = 2*love.math.random(1, (Maze.height - 1)/2) + 2
y = rHeight == 4 and y or y + 1
check = check + 1
until not (x < rWidth + 2 or x > Maze.width - rWidth - 2 or y < rHeight + 2 or y > Maze.height - rHeight - 2) or check > 10000
check = check + 1
if check > 10000 then break end -- In case shit happens
for i = y - rHeight/2 - 2, y + rHeight/2 + 2 do -- Check for touching another rooms
for j = x - rWidth/2 - 2, x + rWidth/2 + 2 do
if Maze.content(j, i) == Maze.room then
i = y + rHeight/2
j = x + rWidth/2
b = false
end
end
end
if b then -- If we didn`t touch room - this happens
for i = y - rHeight/2, y + rHeight/2 do
for j = x - rWidth/2, x + rWidth/2 do
Maze.content(j, i, Maze.room)
end
end
Maze.content(x, y, Maze.chest)
b = love.math.random(0, 3) -- Exit position
if b == 0 then
Maze.content(x - rWidth/2 + 2*(love.math.random(1, rWidth/2)), y + rHeight/2 + 1, Maze.room)
elseif b == 1 then
Maze.content(x - rWidth/2 + 2*(love.math.random(1, rWidth/2)), y - rHeight/2 - 1, Maze.room)
elseif b == 2 then
Maze.content(x + rWidth/2 + 1, y - rHeight/2 + 2*(love.math.random(1, rHeight/2)), Maze.room)
elseif b == 3 then
Maze.content(x - rWidth/2 - 1, y - rHeight/2 + 2*(love.math.random(1, rHeight/2)), Maze.room)
end
Maze.roomCount = Maze.roomCount + 1
end
until b
end
x, y, check = 2, 2, 0
local direction = 0
Maze.content(x, y, Maze.pass) -- Droppin`
repeat -- Main cycle
direction = love.math.random(0,3)
-- Jumpin`
if direction == 0 and x ~= Maze.width - 1 and Maze.content(x+2, y) ~= Maze.room and Maze.content(x+2, y) ~= Maze.pass then
Maze.content(x+1, y, Maze.pass)
x = x + 2
elseif direction == 1 and x ~= 2 and Maze.content(x-2, y) ~= Maze.room and Maze.content(x-2, y) ~= Maze.pass then
Maze.content(x-1, y, Maze.pass)
x = x - 2
elseif direction == 2 and y ~= Maze.height - 1 and Maze.content(x, y+2) ~= Maze.room and Maze.content(x, y+2) ~= Maze.pass then
Maze.content(x, y+1, Maze.pass)
y = y + 2
elseif direction == 3 and y ~= 2 and Maze.content(x, y-2) ~= Maze.room and Maze.content(x, y-2) ~= Maze.pass then
Maze.content(x, y-1, Maze.pass)
y = y - 2
end
Maze.content(x, y, Maze.pass) -- Diggin`
if Maze.deadend(x,y) then -- Gettin` dafuq outta here
repeat
x = 2 * love.math.random(1, (Maze.width - 1) / 2)
y = 2 * love.math.random(1, (Maze.height - 1) / 2)
until Maze.content(x, y) == Maze.pass
end
check = check + 1
until check%1000 == 0 and Maze.ended()
-- Here is end
if direction == 0 then
Maze.exitX = Maze.width
Maze.exitY = 2 * love.math.random(1, (Maze.height - 1) / 2)
elseif direction == 1 then
Maze.exitX = 1
Maze.exitY = 2 * love.math.random(1, (Maze.height - 1) / 2)
elseif direction == 2 then
Maze.exitX = 2 * love.math.random(1, (Maze.width - 1) / 2)
Maze.exitY = Maze.height
elseif direction == 3 then
Maze.exitX = 2 * love.math.random(1, (Maze.width - 1) / 2)
Maze.exitY = 1
end
Maze.content(Maze.exitX, Maze.exitY, Maze.exit)
end,
mapWays = function ()
for i = 1, Maze.height do
for j = 1, Maze.width do
if Maze.content(j, i) == Maze.pass or Maze.content(j, i) == Maze.room then
Maze.ways(j, i, 0)
else
Maze.ways(j, i, -1)
end
end
end
local W = {}
W[1] = {}
if Maze.exitX == Maze.width then
W[1].x = Maze.exitX - 1
elseif Maze.exitX == 1 then
W[1].x = 2
else
W[1].x = Maze.exitX
end
if Maze.exitY == Maze.height then
W[1].y = Maze.exitY - 1
elseif Maze.exitY == 1 then
W[1].y = 2
else
W[1].y = Maze.exitY
end
W[1].c = 1
repeat
for key, val in pairs(W) do
Maze.ways(val.x, val.y, val.c)
if Maze.ways(val.x+1, val.y) == 0 then table.insert(W, {y = val.y, x = val.x+1, c = val.c+1}) end
if Maze.ways(val.x-1, val.y) == 0 then table.insert(W, {y = val.y, x = val.x-1, c = val.c+1}) end
if Maze.ways(val.x, val.y+1) == 0 then table.insert(W, {y = val.y+1, x = val.x, c = val.c+1}) end
if Maze.ways(val.x, val.y-1) == 0 then table.insert(W, {y = val.y-1, x = val.x, c = val.c+1}) end
Maze.ways.max = val.c
Maze.ways.x = val.x
Maze.ways.y = val.y
table.remove(W, key)
end
until #W == 0
end,
findAbsolute = function (x1, y1, x2, y2)
x1 = math.modf(x1)
y1 = math.modf(y1)
x2 = math.modf(x2)
y2 = math.modf(y2)
if (x1 == x2 and y1 == y2) or Maze.content(x2, y2) == Maze.wall then return 0, 'inside' end
local steps, count = 0, 0
local A, B, dir = {}, {}, ''
B[1] = {x = x1, y = y1, c = 0}
repeat
for _, val in pairs(B) do
if count > 10000 then return 0, 'inside' end
x1, y1 = val.x, val.y
if x1 == x2 and y1 == y2 then break end
if x1+1 == x2 and y1 == y2 then dir = 'left' end
if x1-1 == x2 and y1 == y2 then dir = 'right' end
if x1 == x2 and y1+1 == y2 then dir = 'up' end
if x1 == x2 and y1-1 == y2 then dir = 'down' end
A[y1*Maze.width + x1] = val.c
steps = val.c + 1
count = count + 1
if not A[y1*Maze.width + x1+1] and Maze.ways[y1*Maze.width + x1+1] ~= -1 then
table.insert(B, {x = x1+1, y = y1, c = val.c+1})
end
if not A[y1*Maze.width + x1-1] and Maze.ways[y1*Maze.width + x1-1] ~= -1 then
table.insert(B, {x = x1-1, y = y1, c = val.c+1})
end
if not A[(y1+1)*Maze.width + x1] and Maze.ways[(y1+1)*Maze.width + x1] ~= -1 then
table.insert(B, {x = x1, y = y1+1, c = val.c+1})
end
if not A[(y1-1)*Maze.width + x1] and Maze.ways[(y1-1)*Maze.width + x1] ~= -1 then
table.insert(B, {x = x1, y = y1-1, c = val.c+1})
end
end
until x1 == x2 and y1 == y2
return steps, dir
end,
update = function ()
for i=1, Maze.height do
for j=1, Maze.width do
Maze.visibility[i*Maze.width + j] = false
end
end
local x,y = math.floor(hero.x),math.floor(hero.y)
Maze.visibility[y*Maze.width + x] = true
repeat
y = y + 1
Maze.visibility[y*Maze.width + x - 1] = true
Maze.visibility[y*Maze.width + x] = true
Maze.visibility[y*Maze.width + x + 1] = true
until Maze.content[y*Maze.width + x] == Maze.wall
x,y = math.floor(hero.x),math.floor(hero.y)
repeat
y = y - 1
Maze.visibility[y*Maze.width + x - 1] = true
Maze.visibility[y*Maze.width + x] = true
Maze.visibility[y*Maze.width + x + 1] = true
until Maze.content[y*Maze.width + x] == Maze.wall
x,y = math.floor(hero.x),math.floor(hero.y)
repeat
x = x + 1
Maze.visibility[(y-1)*Maze.width + x] = true
Maze.visibility[y*Maze.width + x] = true
Maze.visibility[(y+1)*Maze.width + x] = true
until Maze.content[y*Maze.width + x] == Maze.wall
x,y = math.floor(hero.x),math.floor(hero.y)
repeat
x = x - 1
Maze.visibility[(y-1)*Maze.width + x] = true
Maze.visibility[y*Maze.width + x] = true
Maze.visibility[(y+1)*Maze.width + x] = true
until Maze.content[y*Maze.width + x] == Maze.wall
end
}
setmetatable(Maze.content, Maze)
setmetatable(Maze.ways, Maze)
setmetatable(Maze.visibility, Maze)