Guest User

tur

a guest
Apr 26th, 2014
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.20 KB | None | 0 0
  1. -- api
  2.  
  3. os.loadAPI("config")
  4. pos = {}
  5. local conf = config.new(".apidata", "Turtle API Data")
  6.  
  7. -- internal functions
  8.  
  9. -- returns 1 if positive, -1 if negative, or 0 if 0
  10. local function sign(n)
  11.     return n > 0 and 1 or (n < 0 and -1 or 0)
  12. end
  13.  
  14. -- recieves case/command pairs in {}s, optional default value
  15. -- ie switch(dir, {"up", "placeUp"}, {"down", "placeDown"}, "place")
  16. local function switch(exp, ...)
  17.     local args = {...}
  18.     local def = table.remove(args)
  19.     for pair, case in ipairs(args) do
  20.         if exp == case[1] then
  21.             return case[2]
  22.         end
  23.     end
  24.     return def
  25. end
  26.  
  27. local function isint(n)
  28.     return type(n) == "number" and (math.modf(n) == n)
  29. end
  30.  
  31. -- errors msg if any expression in ... fails
  32. local function check(msg, ...)
  33.     for _, exp in pairs({...}) do
  34.         if not exp then
  35.             error(msg, 2)
  36.         end
  37.     end
  38. end
  39.  
  40. local function init()
  41.     conf:load()
  42.     if conf:containsKey("loaded") then
  43.         conf:setBoolean("loaded", true)
  44.         local input = {x = nil, y = nil, z = nil, f = nil}
  45.         for k, _ in ipairs(input) do
  46.             write("Enter current "..k)
  47.             input[k] = read()
  48.             pos[k] = input[k]
  49.             conf:setNumber(k, input[k])
  50.             term.clearLine()
  51.         end
  52.     else
  53.         pos.x = conf:getNumber("x")
  54.         pos.y = conf:getNumber("y")
  55.         pos.z = conf:getNumber("z")
  56.         pos.f = conf:getNumber("f")
  57.     end
  58.     conf:save()
  59. end
  60.  
  61. -- syncs the storage file with the locally stored position data
  62. local function fupdate()
  63.     init()
  64.     conf:load()
  65.     for k, v in ipairs(pos) do
  66.         conf:setNumber(k, v)
  67.     end
  68.     conf:save
  69. end
  70.  
  71. -- turns left or right f times
  72. -- -f = left, +f = right
  73. -- @return new_f
  74. local function fturn(f)
  75.     init()
  76.     for rep = 1, math.abs(f) do
  77.         turtle[f > 0 and "turnRight" or "turnLeft"]()
  78.         pos.f = switch(pos.f + sign(f), {5, 1}, {0, 4}, pos.f + sign(f))
  79.         fupdate()
  80.     end
  81.     return pos.f
  82. end
  83.  
  84. -- moves x, y, z on each axis
  85. -- @return [true + new_coords or false + error]
  86. local function fmove(x, y, z)
  87.     local axes = {
  88.         x = {x, "forward", "back"};
  89.         y = {y, "forward", "back"};
  90.         z = {z, "up", "down"};
  91.     }
  92.     init()
  93.     for axis, data in ipairs(axes) do
  94.         for rep = 1, math.abs(data[1]) do
  95.             if turtle[sign(data[1]) > 0 and data[2] or data[3]]() then
  96.                 pos[axis] = sign(data[1])
  97.                 fupdate()
  98.             else
  99.                 return false, "Failed move "..rep.." of "..math.abs(data[1])
  100.             end
  101.         end
  102.     end
  103.     return true, pos
  104. end
  105.  
  106. -- external functions
  107.  
  108. -- turns left or right
  109. -- @return new_f
  110. function turn(d, r)
  111.     check("Expected 'left' or 'right', got "..d, d == "left" or d == "right")
  112.     check("Expected a positive integer, got "..r, type(r) == "number", r > 0, isint(r))
  113.     init()
  114.     d = d == "left" and -1 or 1
  115.     r = r % 4 == 3 and -1 or r % 4
  116.     fturn(r * d)
  117.     return pos.f
  118. end
  119.  
  120. -- sets direction to f
  121. -- @return new_f
  122. function setf(f)
  123.     check("Expected an integer between 1 and 4, got "..f, f >= 1, f <= 4, isint(f))
  124.     init()
  125.     if f ~= pos.f then
  126.         turn(sign(f - pos.f) == 1 and "right" or "left", math.abs(f - pos.f))
  127.     end
  128.     return pos.f
  129. end
  130.  
  131. -- moves x, y, z on each axis and turns to f
  132. -- @return success + [new_coords or error]
  133. function move(x, y, z, f)
  134.     check("Expected an integer, got "..(isint(x) and (isint(y) and z or y) or x), isint(x), isint(y), isint(z))
  135.     check("Expected an integer between 1 and 4, got "..f, f > 1, f < 4, isint(f))
  136.     init()
  137.     local ok, err = fmove(x, y, z)
  138.     setf(f)
  139.     return ok, ok and pos or err
  140. end
  141.  
  142. -- moves to the specified coords and turns to f
  143. -- @return success + [new_coords or error]
  144. function goto(x, y, z, f)
  145.     check("Expected an integer, got "..(isint(x) and (isint(y) and z or y) or x), isint(x), isint(y), isint(z))
  146.     check("Expected an integer between 1 and 4, got "..f, f > 1, f < 4, isint(f))
  147.     init()
  148.     return move(x - pos.x, y - pos.y, z - pos.z, f)
  149. end
  150.  
  151. -- places the item in slot s in direction d (defaults to front)
  152. -- @return success + remaining_items
  153. function place(s, d)
  154.     check("Expected an integer between 1 and 16, got "..s, type(s) == "number", s >= 1, s <= 16)
  155.     check("Expected 'front', 'up', or 'down', got "..d, d == "front" or d == "up" or d == "down")
  156.     init()
  157.     d = switch(d, {"up", "placeUp"}, {"down", "placeDown"}, "place")
  158.     turtle.select(s)
  159.     return turtle[d](), turtle.getItemCount(s)
  160. end
  161.  
  162. -- resets the coordinates and direction
  163. -- @return old_pos
  164. function reset(x, y, z, f)
  165.     check("Expected an integer, got "..(isint(x) and (isint(y) and z or y) or x), isint(x), isint(y), isint(z))
  166.     check("Expected an integer between 1 and 4, got "..f, f > 1, f < 4, isint(f))
  167.     init()
  168.     local old = pos
  169.     pos.x = x
  170.     pos.y = y
  171.     pos.z = z
  172.     pos.f = f
  173.     fupdate()
  174.     return old
  175. end
  176.  
  177. -- syncs with the position data in the persistance file
  178. function sync()
  179.     init()
  180.     conf:load()
  181.     pos.x = conf:getNumber("x")
  182.     pos.y = conf:getNumber("y")
  183.     pos.z = conf:getNumber("z")
  184.     pos.f = conf:getNumber("f")
  185.     conf:save()
  186. end
Advertisement
Add Comment
Please, Sign In to add comment