SHOW:
|
|
- or go back to the newest paste.
| 1 | local tArgs = {...}
| |
| 2 | local x, y, z, f | |
| 3 | local xCur, yCur, zCur, fCur | |
| 4 | local xDif, yDif, zDif | |
| 5 | local posIn, posOut, pos | |
| 6 | ||
| 7 | local function syncFile() | |
| 8 | posOut = io.open("data/pos.txt", "w")
| |
| 9 | posOut:write(textutils.serialize({xCur, yCur, zCur, fCur}))
| |
| 10 | posOut:close() | |
| 11 | end | |
| 12 | ||
| 13 | local function abort(msg) | |
| 14 | syncFile() | |
| 15 | print("ERROR: "..msg)
| |
| 16 | do return end | |
| 17 | end | |
| 18 | ||
| 19 | local function up(n) | |
| 20 | for i = 1, n do | |
| 21 | while not turtle.up() do | |
| 22 | turtle.digUp() | |
| 23 | end | |
| 24 | yCur = yCur + 1 | |
| 25 | syncFile() | |
| 26 | end | |
| 27 | end | |
| 28 | ||
| 29 | local function down(n) | |
| 30 | for i = 1, n do | |
| 31 | while not turtle.down() do | |
| 32 | turtle.digDown() | |
| 33 | end | |
| 34 | yCur = yCur - 1 | |
| 35 | syncFile() | |
| 36 | end | |
| 37 | end | |
| 38 | ||
| 39 | local function moveF(dir, n) | |
| 40 | while fCur ~= dir do | |
| 41 | turtle.turnLeft() | |
| 42 | if dir < 0 or dir > 3 then | |
| 43 | do return end | |
| 44 | end | |
| 45 | if fCur ~= 0 then | |
| 46 | fCur = fCur - 1 | |
| 47 | else | |
| 48 | fCur = 3 | |
| 49 | end | |
| 50 | syncFile() | |
| 51 | end | |
| 52 | for i = 1, n do | |
| 53 | while not turtle.forward() do | |
| 54 | - | up() |
| 54 | + | up(1) |
| 55 | end | |
| 56 | if fCur == 0 then | |
| 57 | zCur = zCur + 1 | |
| 58 | elseif fCur == 1 then | |
| 59 | xCur = xCur - 1 | |
| 60 | elseif fCur == 2 then | |
| 61 | zCur = zCur - 1 | |
| 62 | elseif fCur == 3 then | |
| 63 | xCur = xCur + 1 | |
| 64 | else | |
| 65 | abort("invalid state for fCur")
| |
| 66 | end | |
| 67 | syncFile() | |
| 68 | end | |
| 69 | end | |
| 70 | ||
| 71 | if not tArgs[1] or not tArgs[2] or not tArgs[3] then | |
| 72 | print("Usage: goto <x> <y> <z> [<f>]")
| |
| 73 | do return end | |
| 74 | end | |
| 75 | ||
| 76 | x = tonumber(tArgs[1]) | |
| 77 | y = tonumber(tArgs[2]) | |
| 78 | z = tonumber(tArgs[3]) | |
| 79 | ||
| 80 | if tArgs[4] then | |
| 81 | f = tonumber(tArgs[4]) | |
| 82 | end | |
| 83 | ||
| 84 | posIn = io.open("data/pos.txt", "r")
| |
| 85 | pos = posIn:read() | |
| 86 | posIn:close() | |
| 87 | ||
| 88 | pos = textutils.unserialize(pos) | |
| 89 | ||
| 90 | xCur = tonumber(pos[1]) | |
| 91 | yCur = tonumber(pos[2]) | |
| 92 | zCur = tonumber(pos[3]) | |
| 93 | fCur = tonumber(pos[4]) | |
| 94 | ||
| 95 | xDif = x - xCur | |
| 96 | zDif = z - zCur | |
| 97 | ||
| 98 | if xDif < 0 then | |
| 99 | xDif = xDif * -1 | |
| 100 | moveF(1, xDif) | |
| 101 | else | |
| 102 | moveF(3, xDif) | |
| 103 | end | |
| 104 | ||
| 105 | if zDif < 0 then | |
| 106 | zDif = zDif * -1 | |
| 107 | moveF(2, zDif) | |
| 108 | else | |
| 109 | moveF(0, zDif) | |
| 110 | end | |
| 111 | ||
| 112 | yDif = y - yCur | |
| 113 | ||
| 114 | if yDif < 0 then | |
| 115 | yDif = yDif * -1 | |
| 116 | down(yDif) | |
| 117 | else | |
| 118 | up(yDif) | |
| 119 | end | |
| 120 | ||
| 121 | if f then | |
| 122 | moveF(f, 0) | |
| 123 | end | |
| 124 | ||
| 125 | syncFile() |