-
Notifications
You must be signed in to change notification settings - Fork 21
/
lib_control.lua
67 lines (58 loc) · 1.76 KB
/
lib_control.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
local Position = require '__FARL__/stdlib/area/position'
local M = {}
function M.saveVar(var, name)
var = var or global
local n = name or ""
game.write_file("farl/farl"..n..".lua", serpent.block(var, {name="glob"}))
end
function M.debugDump(var, force)
if false or force then
local msg
if type(var) == "string" then
msg = var
else
msg = serpent.dump(var, {name="var", comment=false, sparse=false, sortkeys=true})
end
for _,player in pairs(game.players) do
player.print(msg)
end
local tick = game and game.tick or 0
log(tick .. " " .. msg)
end
end
function M.debugLog(var, prepend)
if not global.debug_log then return end
local str = prepend or ""
for _,player in pairs(game.players) do
local msg
if type(var) == "string" then
msg = var
else
msg = serpent.dump(var, {name="var", comment=false, sparse=false, sortkeys=true})
end
player.print(str..msg)
log(str..msg)
end
end
function M.startsWith(haystack,needle)
return string.sub(haystack,1,string.len(needle))==needle
end
function M.endsWith(haystack,needle)
return needle=='' or string.sub(haystack,-string.len(needle))==needle
end
M.diagonal_data = {
[1] = { x = 0.5, y = -0.5 },
[3] = { x = 0.5, y = 0.5 },
[5] = { x = -0.5, y = 0.5 },
[7] = { x = -0.5, y = -0.5 }
}
function M.diagonal_to_real_pos(rail)
local data = M.diagonal_data
if rail.type and rail.type == "straight-rail" then
local off = data[rail.direction] and data[rail.direction] or { x = 0, y = 0 } --fix for diagonal rails??!
return Position.add(off, rail.position)
else
return rail.position
end
end
return M