Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- api
- t = turtle
- pos = {
- x = 1;
- y = 1;
- z = 29;
- f = 1;
- }
- -- internal functions
- -- returns 1 if positive, -1 if negative, or 0 if 0
- local function sign(n)
- return n > 0 and 1 or (n < 0 and -1 or 0)
- end
- -- recieves case/command pairs in {}s, optional default value
- -- ie switch(dir, {"up", "placeUp"}, {"down", "placeDown"}, "place")
- local function switch(exp, ...)
- local args = {...}
- local def = table.remove(args)
- for pair, case in ipairs(args) do
- if exp == case[1] then
- return case[2]
- end
- end
- return def
- end
- local function isint(n)
- return type(n) == "number" and (math.modf(n) == n)
- end
- -- errors msg if any expression in ... fails
- local function check(msg, ...)
- for _, exp in pairs({...}) do
- if not exp then
- error(msg, 2)
- end
- end
- end
- -- turns left or right f times
- -- -f = left, +f = right
- -- @return new_f
- local function fturn(f)
- for rep = 1, math.abs(f) do
- t[f > 0 and "turnRight" or "turnLeft"]()
- pos.f = switch(pos.f + sign(f), {5, 1}, {0, 4}, pos.f + sign(f))
- end
- return pos.f
- end
- -- moves x, y, z on each axis
- -- @return [true + new_coords or false + error]
- local function fmove(x, y, z)
- local axes = {
- x = {x, "forward", "back"};
- y = {y, "forward", "back"};
- z = {z, "up", "down"};
- }
- for axis, data in ipairs(axes) do
- for rep = 1, math.abs(data[1]) do
- if t[sign(data[1]) > 0 and data[2] or data[3]]() then
- pos[axis] = sign(data[1])
- else
- return false, "Failed move "..rep.." of "..math.abs(data[1])
- end
- end
- end
- return true, pos
- end
- -- external functions
- -- turns left or right
- -- @return new_f
- function turn(d, r)
- check("Expected 'left' or 'right', got "..d, d == "left" or d == "right")
- check("Expected a positive integer, got "..r, type(r) == "number", r > 0, isint(r))
- d = d == "left" and -1 or 1
- r = r % 4 == 3 and -1 or r % 4
- fturn(r * d)
- return pos.f
- end
- -- sets direction to f
- -- @return new_f
- function setf(f)
- check("Expected an integer between 1 and 4, got "..f, f > 1, f < 4, isint(f))
- turn(sign(f - pos.f) == 1 and "right" or "left", math.abs(f - pos.f))
- return pos.f
- end
- -- moves x, y, z on each axis and turns to f
- -- @return success + [new_coords or error]
- function move(x, y, z, f)
- check("Expected an integer, got "..(isint(x) and x or (isint(y) and y or z), isint(x), isint(y), isint(z)))
- check("Expected an integer between 1 and 4, got "..f, f > 1, f < 4, isint(f))
- local ok, err = fmove(x, y, z)
- setf(f)
- return ok, ok and pos or err
- end
- -- moves to the specified coords and turns to f
- -- @return success + [new_coords or error]
- function goto(x, y, z, f)
- check("Expected an integer, got "..(isint(x) and x or (isint(y) and y or z), isint(x), isint(y), isint(z)))
- check("Expected an integer between 1 and 4, got "..f, f > 1, f < 4, isint(f))
- return move(x - pos.x, y - pos.y, z - pos.z, f)
- end
- -- places the item in slot s in direction d (defaults to front)
- -- @return success + remaining_items
- function place(s, d)
- check("Expected an integer between 1 and 16, got "..s, type(s) == "number", s >= 1, s <= 16)
- check("Expected 'front', 'up', or 'down', got "..d, d == "front" or d == "up" or d == "down")
- d = switch(d, {"up", "placeUp"}, {"down", "placeDown"}, "place")
- t.select(s)
- return t[d](), t.getItemCount(s)
- end
- -- resets the coordinates and direction
- -- @return old_pos
- function reset(x, y, z, f)
- check("Expected an integer, got "..(isint(x) and x or (isint(y) and y or z), isint(x), isint(y), isint(z)))
- check("Expected an integer between 1 and 4, got "..f, f > 1, f < 4, isint(f))
- local old = pos
- pos.x = x
- pos.y = y
- pos.z = z
- pos.f = f
- return old
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement