Compare commits
No commits in common. "4afa3e68d1cf00b2b5fcb8cfd4479ee3fc6a6ae1" and "c2a8dfedaf8c367f4f008527a814ddf8851d841d" have entirely different histories.
4afa3e68d1
...
c2a8dfedaf
76
kai/lib.lua
76
kai/lib.lua
@ -1,76 +0,0 @@
|
|||||||
-- Do not lose state when reset (chunkloading etc)
|
|
||||||
state = (function ()
|
|
||||||
local path = ".state"
|
|
||||||
local state_table = {}
|
|
||||||
|
|
||||||
if fs.exists(path) then
|
|
||||||
local f = fs.open(path, "r")
|
|
||||||
state_table = textutils.unserialize(f.readAll())
|
|
||||||
f.close()
|
|
||||||
end
|
|
||||||
|
|
||||||
return setmetatable({}, {
|
|
||||||
__index = state_table,
|
|
||||||
|
|
||||||
__newindex = function(self, k, v)
|
|
||||||
rawset(state_table, k, v)
|
|
||||||
local f = fs.open(path, "w")
|
|
||||||
f.write(textutils.serialize(state_table))
|
|
||||||
f.close()
|
|
||||||
end,
|
|
||||||
})
|
|
||||||
end)()
|
|
||||||
|
|
||||||
--[[
|
|
||||||
orientation:
|
|
||||||
0 = South (+z)
|
|
||||||
1 = West (-x)
|
|
||||||
2 = North (-z)
|
|
||||||
3 = East (+x)
|
|
||||||
]]
|
|
||||||
|
|
||||||
function move(raw_move, position_update)
|
|
||||||
return function()
|
|
||||||
success, err = raw_move()
|
|
||||||
if not success then
|
|
||||||
return false, err
|
|
||||||
end
|
|
||||||
position_update()
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
return {
|
|
||||||
state = state,
|
|
||||||
|
|
||||||
fwd = move(turtle.forward, function()
|
|
||||||
if state.o == 1 or state.o == 3 then
|
|
||||||
state.x = state.x + ((state.o == 1) and -1 or 1)
|
|
||||||
else
|
|
||||||
state.z = state.z + ((state.o == 2) and -1 or 1)
|
|
||||||
end
|
|
||||||
end),
|
|
||||||
|
|
||||||
back = move(turtle.back, function()
|
|
||||||
if state.o == 1 or state.o == 3 then
|
|
||||||
state.x = state.x + ((state.o == 1 and 1 or -1))
|
|
||||||
else
|
|
||||||
state.z = state.z + ((state.o == 2) and 1 or -1)
|
|
||||||
end
|
|
||||||
end),
|
|
||||||
|
|
||||||
up = move(turtle.up, function()
|
|
||||||
state.y = state.y + 1
|
|
||||||
end),
|
|
||||||
|
|
||||||
down = move(turtle.down, function()
|
|
||||||
state.y = state.y - 1
|
|
||||||
end),
|
|
||||||
|
|
||||||
left = move(turtle.turnLeft, function()
|
|
||||||
state.o = (state.o - 1) % 4
|
|
||||||
end),
|
|
||||||
|
|
||||||
right = move(turtle.turnRight, function()
|
|
||||||
state.o = (state.o + 1) % 4
|
|
||||||
end),
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user