Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tArgs = {...}
- local x, y, z, f
- local xCur, yCur, zCur, fCur
- local xDif, yDif, zDif
- local posIn, posOut, pos
- local function syncFile()
- posOut = io.open("data/pos.txt", "w")
- posOut:write(textutils.serialize({xCur, yCur, zCur, fCur}))
- posOut:close()
- end
- local function abort(msg)
- syncFile()
- print("ERROR: "..msg)
- do return end
- end
- local function up(n)
- for i = 1, n do
- while not turtle.up() do
- turtle.digUp()
- end
- yCur = yCur + 1
- syncFile()
- end
- end
- local function down(n)
- for i = 1, n do
- while not turtle.down() do
- turtle.digDown()
- end
- yCur = yCur - 1
- syncFile()
- end
- end
- local function moveF(dir, n)
- while fCur ~= dir do
- turtle.turnLeft()
- if dir < 0 or dir > 3 then
- do return end
- end
- if fCur ~= 0 then
- fCur = fCur - 1
- else
- fCur = 3
- end
- syncFile()
- end
- for i = 1, n do
- while not turtle.forward() do
- up()
- end
- if fCur == 0 then
- zCur = zCur + 1
- elseif fCur == 1 then
- xCur = xCur - 1
- elseif fCur == 2 then
- zCur = zCur - 1
- elseif fCur == 3 then
- xCur = xCur + 1
- else
- abort("invalid state for fCur")
- end
- syncFile()
- end
- end
- if not tArgs[1] or not tArgs[2] or not tArgs[3] then
- print("Usage: goto <x> <y> <z> [<f>]")
- do return end
- end
- x = tonumber(tArgs[1])
- y = tonumber(tArgs[2])
- z = tonumber(tArgs[3])
- if tArgs[4] then
- f = tonumber(tArgs[4])
- end
- posIn = io.open("data/pos.txt", "r")
- pos = posIn:read()
- posIn:close()
- pos = textutils.unserialize(pos)
- xCur = tonumber(pos[1])
- yCur = tonumber(pos[2])
- zCur = tonumber(pos[3])
- fCur = tonumber(pos[4])
- xDif = x - xCur
- zDif = z - zCur
- if xDif < 0 then
- xDif = xDif * -1
- moveF(1, xDif)
- else
- moveF(3, xDif)
- end
- if zDif < 0 then
- zDif = zDif * -1
- moveF(2, zDif)
- else
- moveF(0, zDif)
- end
- yDif = y - yCur
- if yDif < 0 then
- yDif = yDif * -1
- down(yDif)
- else
- up(yDif)
- end
- if f then
- moveF(f, 0)
- end
- syncFile()
Advertisement
Add Comment
Please, Sign In to add comment