-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath_finder.lua
More file actions
56 lines (42 loc) · 1.7 KB
/
path_finder.lua
File metadata and controls
56 lines (42 loc) · 1.7 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
local maf = require("lib.maf")
local math_helper = require("lib.math_helper")
local path_finder = {
find_neighbors_position = function (self, field, center, color)
if type(center) ~= "table" then return end
color = (field.slot:get_content(center) or {}).color or color
if not color then return end
local neighbor = {
list = {}
}
local start, stop = maf.vector(), maf.vector()
start = center + maf.vector(-1,-1)
stop = center + maf.vector(1,1)
for i = start.x, stop.x do
for j = start.y, stop.y do
neighbor.position = maf.vector(i,j)
neighbor.instance = field.slot:get_content(neighbor.position)
if neighbor.instance then
if neighbor.instance.color == color then
table.insert(neighbor.list, maf.vector(neighbor.position:unpack()))
end
end
end
end
return neighbor.list, center
end,
-- также можно искать пустые последовательности
find_directional_sequence = function(self, field, last, bias, path)
if (type(last) ~= "table") or ( type(bias) ~= "table") then return false end
local color = (field.slot:get_content(last) or {}).color
path = ((type(path) ~= "table") and {last}) or path
local next_position = last + bias
if field.slot:is_in_bounds(next_position) then
if (field.slot:get_content(next_position) or {}).color == color then
table.insert(path, next_position)
return self:find_directional_sequence(field, next_position, bias, path)
end
end
return path
end
}
return path_finder