Advertisement
Larvix

impTurtle

Jan 17th, 2024 (edited)
1,135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.33 KB | None | 0 0
  1. if not turtle then
  2.     error("impTurtle API requires device type 'turtle'.")
  3. end
  4.  
  5. local moved = {
  6.     north = vector.new( 0, 0,-1),
  7.     east  = vector.new( 1, 0, 0),
  8.     south = vector.new( 0, 0, 1),
  9.     west  = vector.new(-1, 0, 0),
  10. }
  11.  
  12. local faceName = {
  13.     [0] = "north",
  14.     [1] = "east",
  15.     [2] = "south",
  16.     [3] = "west",
  17. }
  18.  
  19. local status = {
  20.     bearing = nil,
  21.     position = nil,
  22.     fuel = turtle.getFuelLevel(),
  23. }
  24.  
  25. local setup = function(bearing, posX, posY, posZ)
  26.     bearing = string.lower(bearing or "")
  27.     if bearing == "north" or bearing == "n" then
  28.         status.bearing = 0
  29.     elseif bearing == "east" or bearing == "e" then
  30.         status.bearing = 1
  31.     elseif bearing == "south" or bearing == "s" then
  32.         status.bearing = 2
  33.     elseif bearing == "west" or bearing == "w" then
  34.         status.bearing = 3
  35.     else
  36.         error("Bad argument #1 setting up impTurtle. Expects string direction, e.g. 'north' or 'n'.")
  37.     end
  38.     if type(posX) == "number" and type(posY) == "number" and type(posZ) == "number" then
  39.         status.position = vector.new(posX,posY,posZ)
  40.     else
  41.         status.bearing = nil
  42.         error("Bad arguments #2 - #4 setting up impTurtle. Expects number for start co-ordinates, e.g. 0, 0, 0.")
  43.     end
  44. end
  45.  
  46. local getStatus = function(specified)
  47.     local tmp = status.bearing
  48.     status.bearing = faceName[tmp]
  49.     gotStatus = textutils.serialise(status)
  50.     status.bearing = tmp
  51.     return textutils.unserialise(gotStatus)
  52. end
  53.  
  54. local move = function(direction,times)
  55.     local times = tonumber(times)
  56.     local completed = 0
  57.     if not times then times = 1 end
  58.     for i = 1,times do
  59.         if status.fuel == 0 then turtle.refuel() end
  60.         if direction == "left" then
  61.             turtle.turnLeft()
  62.             if status.bearing then
  63.                 status.bearing = (status.bearing-1)%4
  64.             end
  65.         elseif direction == "right" then
  66.             turtle.turnRight()
  67.             if status.bearing then
  68.                 status.bearing = (status.bearing+1)%4
  69.             end
  70.         elseif direction == "up" then
  71.             if turtle.up() then
  72.                 completed = completed + 1
  73.                 if status.position then
  74.                     status.position.y = status.position.y + 1
  75.                 end
  76.             else
  77.                 break
  78.             end
  79.         elseif direction == "down" then
  80.             if turtle.down() then
  81.                 completed = completed + 1
  82.                 if status.position then
  83.                     status.position.y = status.position.y - 1
  84.                 end
  85.             else
  86.                 break
  87.             end
  88.         elseif direction == "forward" then
  89.             if turtle.forward() then
  90.                 completed = completed + 1
  91.                 if status.position and status.bearing then
  92.                     status.position = status.position + moved[faceName[status.bearing]]
  93.                 end
  94.             else
  95.                 break
  96.             end
  97.         elseif direction == "back" then
  98.             if turtle.back() then
  99.                 completed = completed + 1
  100.                 if status.position and status.bearing then
  101.                     status.position = status.position - moved[faceName[status.bearing]]
  102.                 end
  103.             else
  104.                 break
  105.             end
  106.         else
  107.             return "Invalid Direction"
  108.         end
  109.         status.fuel = turtle.getFuelLevel()
  110.     end
  111.     return completed
  112. end
  113.  
  114. local go = {
  115. fd = function(times)
  116.     return move("forward",times)
  117. end,
  118. bk = function(times)
  119.     return move("back",times)
  120. end,
  121. lt = function(times)
  122.     return move("left",times)
  123. end,
  124. rt = function(times)
  125.     return move("right",times)
  126. end,
  127. up = function(times)
  128.     return move("up",times)
  129. end,
  130. dn = function(times)
  131.     return move("down",times)
  132. end,
  133. }
  134.  
  135. return {
  136. left = go.lt,
  137. right = go.rt,
  138. up = go.up,
  139. down = go.dn,
  140. forward = go.fd,
  141. back = go.bk,
  142. status = getStatus,
  143. setup = setup,
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement