-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLineOfSight.lua
382 lines (300 loc) · 9.29 KB
/
LineOfSight.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
-- LineOfSight
LineOfSight = Sprite:extend
{
class = "LineOfSight",
x = 0,
y = 0,
width = 1,
height = 1,
cell = config.cellSize,
visibility = {},
alreadySeen = {},
collision = {},
rebuildCollision = true,
dirty = true,
scanline = 0,
sourceOids = {},
lastCalculatedCellSource = {x = -1000, y = -1000},
updateEachNFrames = config.cellUpdateEachNFrames,
framesUntilUpdate = 0,
allVisible = true,
onNew = function (self)
self.width = config.map_width
self.height = config.map_height
self.scanline = math.floor(config.map_width / self.cell) + 200
the.app.view.timer:every(0.5, function()
--~ print("tick")
if self.dirty == true then
--~ print("dirty")
self.dirty = false
self.rebuildCollision = true
end
end)
end,
cell2px = function (self, cx, cy)
return cx * self.cell, cy * self.cell
end,
px2cell = function (self, x, y)
return math.floor(x / self.cell), math.floor(y / self.cell)
end,
calculateCollision = function (self)
self.rebuildCollision = false
-- clear
for k,v in pairs(self.collision) do self.collision[k] = nil end
local c = self.collision
local c0x, c0y = self:px2cell(0,0)
local c1x, c1y = self:px2cell(config.map_width, config.map_height)
local cell = self.cell
if c0x < -math.huge or c0x > math.huge then return end
local b = 2
c0x, c0y = vector.add(c0x, c0y, -b,-b)
c1x, c1y = vector.add(c1x, c1y, b, b)
--~ print("build collision cache")
local count = 0
for cx = c0x, c1x do
for cy = c0y, c1y do
if self:isCollisionOnCell(cx,cy) then
count = count + 1
c[self:cellKey(cx,cy)] = true
end
end
end
--~ print("found", count, "collision cells")
end,
isCollisionOnCell = function (self, cx,cy)
-- profile.start("los collide")
local collision = false
local px,py = self:cell2px(cx,cy)
-- profile.start("los sprite")
local s = {
x = px,
y = py,
width = self.cell,
height = self.cell,
solid = true,
}
-- profile.stop()
-- profile.start("los collide landscape")
if collision == false and the.view.landscape:subcollide(s) then collision = true end
-- profile.stop()
-- profile.start("los collide collision")
if collision == false and the.view.collision:collide(s) then collision = true end
-- profile.stop()
-- resources
for obj,_ in pairs(the.ressourceObjects) do
if collision == false and obj:collide(s) then collision = true end
end
-- blockers
for obj,_ in pairs(the.blockers) do
if collision == false and obj:collide(s) then collision = true end
end
-- profile.stop()
return collision
end,
rasterResult = {},
calculateVisibilityAddSource = function (self, px,py, range, angle, rotation, feelRange)
local self_cellKey = self.cellKey
local vector_sqLenFromTo = vector.sqLenFromTo
local self_isCollisionOnCellUseCache = self.isCollisionOnCellUseCache
local cellsUntilDarkMax = config.cellsUntilDark
local self_cell2px = self.cell2px
local self_collision = self.collision
local colFun = nil
if self.rebuildCollision then
profile.start("onupdate.los.calculate.rebuild")
self:calculateCollision()
profile.stop()
colFun = function(k,cx,cy) return self:isCollisionOnCell(cx,cy) end
else
colFun = function(k,cx,cy) return self_collision[k] end
end
local pcx, pcy = self:px2cell(px,py)
--~ if pcx == self.lastCalculatedCellSource.x and pcy == self.lastCalculatedCellSource.y then return
--~ else
--~ self.lastCalculatedCellSource.x = pcx
--~ self.lastCalculatedCellSource.y = pcy
--~ end
-- profile.start("calculateVisibility")
local v = self.visibility
local as = self.alreadySeen
-- feel range
local f0x, f0y = self:px2cell(px-feelRange,py-feelRange)
local f1x, f1y = self:px2cell(px+feelRange,py+feelRange)
for fx = f0x, f1x do
for fy = f0y, f1y do
local k = self_cellKey(self, fx,fy)
v[k] = 1
as[k] = 1
end
end
local sx,sy = -the.view.translate.x, -the.view.translate.y
local c0x, c0y = self:px2cell(sx,sy)
local c1x, c1y = self:px2cell(sx + the.app.width, sy + the.app.height)
local cell = self.cell
if
c0x > -math.huge and c0x < math.huge
then
local b = 2
c0x, c0y = vector.add(c0x, c0y, -b,-b)
c1x, c1y = vector.add(c1x, c1y, b, b)
-- early out
if pcx < c0x or c1x < pcx or
pcy < c0y or c1y < pcy then return end
local range2 = range*range
local angleHRad = (angle / 2) / 180 * math.pi
local srcViewX, srcViewY = vector.fromVisualRotation(rotation, 1)
local rasterResult = self.rasterResult
for cx = c0x, c1x do
for cy = c0y, c1y do
if cx == c0x or cx == c1x or cy == c0y or cy == c1y then
--~ print("line from", pcx,pcy,"to",cx,cy)
local free = true
local cellsUntilDark = cellsUntilDarkMax
--profile.start("los 1 step")
-- in fov?
local a = vector.angleFromTo(srcViewX, srcViewY, vector.fromTo(px,py, self_cell2px(self,cx,cy)))
--~ print(a, angleHRad)
if a <= angleHRad then
--profile.start("los 1 raster")
-- clear raster result
for k,v in pairs(rasterResult) do rasterResult[k] = nil end
-- and run a new raster run
geometry.raster_line_it(pcx,pcy,cx,cy,rasterResult)
for i=1,#rasterResult,3 do
local x,y,cellNumInLine = rasterResult[i], rasterResult[i+1], rasterResult[i+2]
--profile.start("los 1 sub step")
local k = self_cellKey(self, x,y)
if (v[k] or 0) <= 1 then
if cellNumInLine > 0 then
-- view range
local d = vector_sqLenFromTo(px,py, self_cell2px(self,x,y))
--~ print(d,range,px,py,x,y)
if d > range2 then free = false
-- collision
elseif colFun(k, x,y) then free = false end
end
if free or cellsUntilDark > 0 then
if free then
as[k] = 1
v[k] = 1
elseif cellsUntilDark > 0 then
as[k] = 1
v[k] = 1
cellsUntilDark = cellsUntilDark - 1
else
--profile.stop()
break
end
end
end
--~ print("free", free, x,y)
--profile.stop()
end
--profile.stop()
end
--profile.stop()
end
end
end
end
-- profile.stop()
end,
calculateVisibility = function (self)
-- clear visibility
for k,v in pairs(self.visibility) do self.visibility[k] = nil end
for _,oid in pairs(self.sourceOids) do
local o = object_manager.get(oid)
if o then
local ox, oy = tools.object_center(o)
profile.start("onupdate.los.calculate")
self:calculateVisibilityAddSource(ox,oy, o.viewRange or 0, o.viewAngle or 0, o.rotation, o.feelRange or 0)
profile.stop()
end
end
end,
cellKey = function (self, cx,cy)
-- "int" for faster array table access
return self.scanline + cx + self.scanline * cy
end,
isObjectVisible = function (self, o)
local c0x, c0y = self:px2cell(o.x,o.y)
local c1x, c1y = self:px2cell(o.x + o.width, o.y + o.height)
if
c0x > -math.huge and c0x < math.huge
then
for cx = c0x, c1x do
for cy = c0y, c1y do
local k = self:cellKey(cx,cy)
local f = self.visibility[k] or 0
if f > 0 then return true end
end
end
end
return false
end,
getVisibility = function (self, px, py, cx, cy)
return self.visibility[self:cellKey(cx,cy)] or 0
--~ local dx,dy = vector.fromTo(the.player.x, the.player.y, px, py)
--~ local l = vector.len(dx, dy)
--~ local vx,vy = vector.fromVisualRotation(the.player.rotation, 1)
--~ local v = vector.dot(vx,vy, vector.normalize(dx,dy))
--~ if v < 0.25 then v = 0 end
--~ v = 1 - v
--~ return v --math.min(utils.mapIntoRange (l, 100, 300, 0, 1), 1-v)
end,
onUpdate = function (self, elapsed)
profile.start("onupdate.los")
if self.framesUntilUpdate <= 0 then
--~ print("UPDATE")
self:calculateVisibility()
self.framesUntilUpdate = self.updateEachNFrames
else
--~ print("SKIP")
self.framesUntilUpdate = self.framesUntilUpdate - 1
end
profile.stop()
end,
reset = function (self)
self.visibility = {}
self.alreadySeen = {}
self.collision = nil
end,
screenCellMap = {},
onDraw = function (self, x, y)
if self.allVisible then return end
-- profile.start("fos draw")
love.graphics.setBlendMode( "alpha" )
local sx,sy = -the.view.translate.x, -the.view.translate.y
local c0x, c0y = self:px2cell(sx,sy)
local c1x, c1y = self:px2cell(sx + the.app.width, sy + the.app.height)
local cell = self.cell
--~ print(sx,sy, c0x, c0y, c1x, c1y, cell)
local m = self.screenCellMap
local mv = self.visibility
local mas = self.alreadySeen
local r,g,b = unpack(config.lineOfSightColor)
if
c0x > -math.huge and c0x < math.huge
then
for cx = c0x, c1x do
for cy = c0y, c1y do
local k = self:cellKey(cx,cy)
local px,py = self:cell2px(cx,cy)
local vb = mv[k] or 0
local as = mas[k] or 0
local f = math.max(vb, as / 2)
if vb > 0 then f = config.lineOfSightInSight
elseif as > 0 then f = config.lineOfSightOutOfSight
else f = config.lineOfSightUnknown end
local of = m[k] or 0
f = 0.4 * f + 0.6 * of
m[k] = f
--~ print(cx, cy, px, py)
love.graphics.setColor(r,g,b, math.floor(255 * (1 - f)))
love.graphics.rectangle("fill", px-sx,py-sy, cell,cell)
end
end
end
-- profile.stop()
end,
}