-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdir.lua
More file actions
executable file
·53 lines (48 loc) · 1.21 KB
/
dir.lua
File metadata and controls
executable file
·53 lines (48 loc) · 1.21 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
-- Direction-related helpers.
local Dir = {}
Dir.N = defines.direction.north
Dir.S = defines.direction.south
Dir.E = defines.direction.east
Dir.W = defines.direction.west
-- Convert a direction to a Position representing a 1 tile offset in that dir.
Dir.toOffset = {
[Dir.N] = {x= 0, y=-1},
[Dir.S] = {x= 0, y= 1},
[Dir.E] = {x= 1, y= 0},
[Dir.W] = {x=-1, y= 0},
}
-- Convert a direction to an orientation (e.g. a percentage clockwise rotation from north).
Dir.toOrientation = {
[Dir.N] = 0,
[Dir.E] = .25,
[Dir.S] = .50,
[Dir.W] = .75,
}
-- Convert a direction to the positive X/Y axis: S and E.
Dir.abs = {
[Dir.N] = Dir.S,
[Dir.S] = Dir.S,
[Dir.E] = Dir.E,
[Dir.W] = Dir.E,
}
-- Single 90 degree clockwise rotation.
Dir.R = {
[Dir.N] = Dir.E,
[Dir.E] = Dir.S,
[Dir.S] = Dir.W,
[Dir.W] = Dir.N,
}
function Dir.isParallel(a, b)
return Dir.abs[a] == Dir.abs[b]
end
-- Returns the primary direction of `to` relative to `from`. If it's directly 45 degrees, prefer N or S.
function Dir.getPrimary(from, to)
local dx = to.x - from.x
local dy = to.y - from.y
if math.abs(dx) > math.abs(dy) then
return dx > 0 and Dir.E or Dir.W
else
return dy > 0 and Dir.S or Dir.N
end
end
return Dir