-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.lua
287 lines (263 loc) · 8.51 KB
/
map.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
ROT=require 'rotLove/src/rot'
tiles = {
floor = 1,
wall = 2,
escape = 3,
}
tile_images = {
love.graphics.newImage("assets/floor.png"),
love.graphics.newImage("assets/wall.png"),
love.graphics.newImage("assets/escape.png"),
}
Map = {
width = 20,
height = 20,
grid = nil,
num_guards = 3,
guard_spawns = {},
spawn = nil,
}
function Map:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end
tile_w = 48
tile_h = 48
function line_intersects_wall(x1, y1, x2, y2)
-- returns the wall intersection closest to (x1, y1) if one exists on the
-- line. else, returns {x2, y2}
local points = grid_line_intersection(x1 / tile_w, y1 / tile_h,
x2 / tile_w, y2 / tile_h)
for i, p in ipairs(points) do
local map_x = math.floor(p.x)
local map_y = math.floor(p.y)
if map_x >= 0 and map_x < map.width and map_y >= 0 and map_y < map.height and map.grid[map_x][map_y] == tiles.wall then
-- return intersection point
local mx = map_x * tile_w
local my = map_y * tile_h
local left = mx - 1
local right = mx + tile_w + 1
local top = my - 1
local bottom = my + tile_h + 1
local px, py = p.x * tile_w, p.y * tile_h
if x1 <= px then
local x, y = findIntersect(x1, y1, px, py, left, top, left, bottom, true, true)
if x ~= 0 then
return x, y
end
else
local x, y = findIntersect(x1, y1, px, py, right, top, right, bottom, true, true)
if x ~= 0 then
return x, y
end
end
if y1 <= py then
local x, y = findIntersect(x1, y1, px, py, left, top, right, top, true, true)
if x ~= 0 then
return x, y
end
else
local x, y = findIntersect(x1, y1, px, py, left, bottom, right, bottom, true, true)
if x ~= 0 then
return x, y
end
end
end
end
return x2, y2
end
function grid_line_intersection(x1, y1, x2, y2)
-- returns the map points that are intersected by a given line
-- via https://github.com/Yonaba/Algorithm-Implementations/blob/master/Bresenham_Based_Supercover_Line/Lua/Yonaba/bresenham_based_supercover.lua
local points = {}
local xstep, ystep, err, errprev, ddx, ddy
local x, y = x1, y1
local dx, dy = x2 - x1, y2 - y1
points[#points + 1] = {x = x1, y = y1}
if dy < 0 then
ystep = - 1
dy = -dy
else
ystep = 1
end
if dx < 0 then
xstep = - 1
dx = -dx
else
xstep = 1
end
ddx, ddy = dx * 2, dy * 2
if ddx >= ddy then
errprev, err = dx, dx
for i = 1, dx do
x = x + xstep
err = err + ddy
if err > ddx then
y = y + ystep
err = err - ddx
if err + errprev < ddx then
points[#points + 1] = {x = x, y = y - ystep}
elseif err + errprev > ddx then
points[#points + 1] = {x = x - xstep, y = y}
else
points[#points + 1] = {x = x, y = y - ystep}
points[#points + 1] = {x = x - xstep, y = y}
end
end
points[#points + 1] = {x = x, y = y}
errprev = err
end
else
errprev, err = dy, dy
for i = 1, dy do
y = y + ystep
err = err + ddx
if err > ddy then
x = x + xstep
err = err - ddy
if err + errprev < ddy then
points[#points + 1] = {x = x - xstep, y = y}
elseif err + errprev > ddy then
points[#points + 1] = {x = x, y = y - ystep}
else
points[#points + 1] = {x = x, y = y - ystep}
points[#points + 1] = {x = x - xstep, y = y}
end
end
points[#points + 1] = {x = x, y = y}
errprev = err
end
end
return points
end
-- from https://love2d.org/wiki/General_math
-- Returns 1 if number is positive, -1 if it's negative, or 0 if it's 0.
function sign(n) return n>0 and 1 or n<0 and -1 or 0 end
-- from https://love2d.org/wiki/General_math
function checkIntersect(l1p1, l1p2, l2p1, l2p2)
local function checkDir(pt1, pt2, pt3) return sign(((pt2.x-pt1.x)*(pt3.y-pt1.y)) - ((pt3.x-pt1.x)*(pt2.y-pt1.y))) end
return (checkDir(l1p1,l1p2,l2p1) ~= checkDir(l1p1,l1p2,l2p2)) and (checkDir(l2p1,l2p2,l1p1) ~= checkDir(l2p1,l2p2,l1p2))
end
-- from https://love2d.org/wiki/PointWithinShape
function findIntersect(g1,h1,g2,h2,i1,j1,i2,j2 )
local xk = 0
local yk = 0
if checkIntersect({x=g1, y=h1}, {x=g2, y=h2}, {x=i1, y=j1}, {x=i2, y=j2}) then
local a = h2-h1
local b = (g2-g1)
local v = ((h2-h1)*g1) - ((g2-g1)*h1)
local d = i2-i1
local c = (j2-j1)
local w = ((j2-j1)*i1) - ((i2-i1)*j1)
xk = (1/((a*d)-(b*c))) * ((d*v)-(b*w))
yk = (-1/((a*d)-(b*c))) * ((a*w)-(c*v))
else
xk,yk = 0,0
end
return xk, yk
end
function pixel_to_map_coords(x, y)
return math.floor(x / tile_w), math.floor(y / tile_h)
end
function point_is_in_wall(x, y)
local x = math.floor(x / tile_w)
local y = math.floor(y / tile_h)
return map.grid[x][y] == tiles.wall
end
function get_fov(x, y, angle1, angle2, dist)
-- ideal algorithm described at http://www.redblobgames.com/articles/visibility/
local triangles = {}
local step = (angle2 - angle1) / 64
local i = 0
local last_x, last_y = x, y
for i = angle1, angle2, step do
local x1 = x + math.cos(i) * dist
local y1 = y + math.sin(i) * dist
x1, y1 = line_intersects_wall(x, y, x1, y1)
table.insert(triangles, {x, y, last_x, last_y, x1, y1})
last_x = x1
last_y = y1
end
return triangles
end
function generate_map(level)
map = Map:new()
map.width = map.width + 1 * (level - 1)
map.height = map.height + 10 * (level - 1)
map.num_guards = map.num_guards * math.pow(2, level - 1)
map.grid = {}
map.guard_spawns = {}
-- fill the map with floor tiles
for i = 0, map.width-1 do
map.grid[i] = {}
for j = 0, map.height-1 do
map.grid[i][j] = 1
end
end
mapgen = ROT.Map.Digger(map.width, map.height,
{dugPercentage = 0.90,
roomWidth = {1, 5},
roomHeight = {1, 5},
timeLimit = 50000})
function callback(x, y, val)
if val == 2 or val == 0 then
map.grid[x-1][y-1] = tiles.floor
else
map.grid[x-1][y-1] = tiles.wall
end
end
mapgen:create(callback)
spawn_section_height = map.height / map.num_guards
for g = 1, map.num_guards do
y = spawn_section_height * g
x = random:random(map.width - 1)
while map.grid[x][y] ~= tiles.floor do
y = y + 1
for i = 1, 10 do
x = random:random(map.width - 1)
if map.grid[x][y] == tiles.floor then
break
end
end
if y >= map.height then
y = 1
end
end
table.insert(map.guard_spawns, {x = x * tile_w, y = y * tile_h})
end
-- find a spawn for the player
local y = map.height - 2
local x = math.floor(map.width / 2)
while map.grid[x][y] == tiles.wall do
y = y - 1
for i = 1, 10 do
x = random:random(map.width - 1)
if map.grid[x][y] == tiles.floor then
break
end
end
end
map.grid[x][y] = tiles.escape
map.spawn = {x, y}
end
function draw_map(camera)
x1, y1, x2, y2 = get_camera_edges()
map_display_w = math.ceil((x2 - x1) / tile_w)
map_display_h = math.ceil((y2 - y1) / tile_h)
startx = math.floor(x1 / tile_w)
starty = math.floor(y1 / tile_h)
startx = math.max(0, startx)
starty = math.max(0, starty)
map_display_w = math.min(map_display_w, map.width - startx - 1)
map_display_h = math.min(map_display_h, map.height - starty - 1)
for x = startx, startx + map_display_w do
for y = starty, starty + map_display_h do
love.graphics.draw(
tile_images[map.grid[x][y]],
x * tile_w,
y * tile_h)
end
end
end