Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. -- api
  2.  
  3. t = turtle
  4.  
  5. pos = {
  6. x = 1;
  7. y = 1;
  8. z = 29;
  9. f = 1;
  10. }
  11.  
  12. -- internal functions
  13.  
  14. -- returns 1 if positive, -1 if negative, or 0 if 0
  15. local function sign(n)
  16. return n > 0 and 1 or (n < 0 and -1 or 0)
  17. end
  18.  
  19. -- recieves case/command pairs in {}s, optional default value
  20. -- ie switch(dir, {"up", "placeUp"}, {"down", "placeDown"}, "place")
  21. local function switch(exp, ...)
  22. local args = {...}
  23. local def = table.remove(args)
  24. for pair, case in ipairs(args) do
  25. if exp == case[1] then
  26. return case[2]
  27. end
  28. end
  29. return def
  30. end
  31.  
  32. local function isint(n)
  33. return type(n) == "number" and (math.modf(n) == n)
  34. end
  35.  
  36. -- errors msg if any expression in ... fails
  37. local function check(msg, ...)
  38. for _, exp in pairs({...}) do
  39. if not exp then
  40. error(msg, 2)
  41. end
  42. end
  43. end
  44.  
  45. -- turns left or right f times
  46. -- -f = left, +f = right
  47. -- @return new_f
  48. local function fturn(f)
  49. for rep = 1, math.abs(f) do
  50. t[f > 0 and "turnRight" or "turnLeft"]()
  51. pos.f = switch(pos.f + sign(f), {5, 1}, {0, 4}, pos.f + sign(f))
  52. end
  53. return pos.f
  54. end
  55.  
  56. -- moves x, y, z on each axis
  57. -- @return [true + new_coords or false + error]
  58. local function fmove(x, y, z)
  59. local axes = {
  60. x = {x, "forward", "back"};
  61. y = {y, "forward", "back"};
  62. z = {z, "up", "down"};
  63. }
  64. for axis, data in ipairs(axes) do
  65. for rep = 1, math.abs(data[1]) do
  66. if t[sign(data[1]) > 0 and data[2] or data[3]]() then
  67. pos[axis] = sign(data[1])
  68. else
  69. return false, "Failed move "..rep.." of "..math.abs(data[1])
  70. end
  71. end
  72. end
  73. return true, pos
  74. end
  75.  
  76. -- external functions
  77.  
  78. -- turns left or right
  79. -- @return new_f
  80. function turn(d, r)
  81. check("Expected 'left' or 'right', got "..d, d == "left" or d == "right")
  82. check("Expected a positive integer, got "..r, type(r) == "number", r > 0, isint(r))
  83. d = d == "left" and -1 or 1
  84. r = r % 4 == 3 and -1 or r % 4
  85. fturn(r * d)
  86. return pos.f
  87. end
  88.  
  89. -- sets direction to f
  90. -- @return new_f
  91. function setf(f)
  92. check("Expected an integer between 1 and 4, got "..f, f > 1, f < 4, isint(f))
  93. turn(sign(f - pos.f) == 1 and "right" or "left", math.abs(f - pos.f))
  94. return pos.f
  95. end
  96.  
  97. -- moves x, y, z on each axis and turns to f
  98. -- @return success + [new_coords or error]
  99. function move(x, y, z, f)
  100. check("Expected an integer, got "..(isint(x) and x or (isint(y) and y or z), isint(x), isint(y), isint(z)))
  101. check("Expected an integer between 1 and 4, got "..f, f > 1, f < 4, isint(f))
  102. local ok, err = fmove(x, y, z)
  103. setf(f)
  104. return ok, ok and pos or err
  105. end
  106.  
  107. -- moves to the specified coords and turns to f
  108. -- @return success + [new_coords or error]
  109. function goto(x, y, z, f)
  110. check("Expected an integer, got "..(isint(x) and x or (isint(y) and y or z), isint(x), isint(y), isint(z)))
  111. check("Expected an integer between 1 and 4, got "..f, f > 1, f < 4, isint(f))
  112. return move(x - pos.x, y - pos.y, z - pos.z, f)
  113. end
  114.  
  115. -- places the item in slot s in direction d (defaults to front)
  116. -- @return success + remaining_items
  117. function place(s, d)
  118. check("Expected an integer between 1 and 16, got "..s, type(s) == "number", s >= 1, s <= 16)
  119. check("Expected 'front', 'up', or 'down', got "..d, d == "front" or d == "up" or d == "down")
  120. d = switch(d, {"up", "placeUp"}, {"down", "placeDown"}, "place")
  121. t.select(s)
  122. return t[d](), t.getItemCount(s)
  123. end
  124.  
  125. -- resets the coordinates and direction
  126. -- @return old_pos
  127. function reset(x, y, z, f)
  128. check("Expected an integer, got "..(isint(x) and x or (isint(y) and y or z), isint(x), isint(y), isint(z)))
  129. check("Expected an integer between 1 and 4, got "..f, f > 1, f < 4, isint(f))
  130. local old = pos
  131. pos.x = x
  132. pos.y = y
  133. pos.z = z
  134. pos.f = f
  135. return old
  136. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement