Advertisement
ambiguities

util.lua

Apr 25th, 2024 (edited)
872
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.41 KB | None | 0 0
  1. local function turn(direction, count)
  2.   if direction ~= "left" and direction ~= "right" then
  3.     error("Invalid direction: " .. direction)
  4.   end
  5.  
  6.   local count = count or 1
  7.   local success, msg = nil, nil
  8.  
  9.   for i = 1, count do
  10.     if direction == "left" then
  11.       success, msg = turtle.turnLeft()
  12.     else
  13.       success, msg = turtle.turnRight()
  14.     end
  15.     if not success then
  16.       error(msg)
  17.     end
  18.   end
  19. end
  20.  
  21. local function checkFull()
  22.   for i = 1, 16 do
  23.     if turtle.getItemSpace(i) > 0 then
  24.       return false
  25.     end
  26.   end
  27.   return true
  28. end
  29.  
  30. local function dig(direction, returnFacing)
  31.   while checkFull() do
  32.     print("Inventory full...")
  33.     local event, key, is_held = os.pullEvent("key")
  34.   end
  35.  
  36.   if returnFacing == nil then
  37.     returnFacing = true
  38.   end
  39.  
  40.   local success, msg = nil, nil
  41.  
  42.   if direction == "up" then
  43.     if turtle.detectUp() then
  44.       success, msg = turtle.digUp()
  45.     end
  46.   elseif direction == "down" then
  47.     if turtle.detectDown() then
  48.       success, msg = turtle.digDown()
  49.     end
  50.   elseif direction == "left" then
  51.     turtle.turnLeft()
  52.     if turtle.detect() then
  53.       success, msg = turtle.dig()
  54.     end
  55.     if returnFacing then
  56.       turtle.turnRight()
  57.     end
  58.   elseif direction == "right" then
  59.     turtle.turnRight()
  60.     if turtle.detect() then
  61.       success, msg = turtle.dig()
  62.     end
  63.     if returnFacing then
  64.       turtle.turnLeft()
  65.     end
  66.   elseif turtle.detect() then
  67.     success, msg = turtle.dig()
  68.   end
  69.  
  70.   if not success and not msg == "Nothing to dig here" then
  71.     error(msg)
  72.   end
  73. end
  74.  
  75. local function go(direction)
  76.   local success, msg = nil, nil
  77.  
  78.   if direction == "up" then
  79.     dig("up")
  80.     success, msg = turtle.up()
  81.   elseif direction == "down" then
  82.     dig("down")
  83.     success, msg = turtle.down()
  84.   elseif direction == "forward" then
  85.     dig()
  86.     success, msg = turtle.forward()
  87.   elseif direction == "back" then
  88.     turn("right", 2)
  89.     dig()
  90.     success, msg = turtle.forward()
  91.     turn("right", 2)
  92.   elseif direction == "left" then
  93.     turn("left")
  94.     dig()
  95.     success, msg = turtle.forward()
  96.     turn("right")
  97.   elseif direction == "right" then
  98.     turn("right")
  99.     dig()
  100.     success, msg = turtle.forward()
  101.     turn("left")
  102.   else
  103.     error("Invalid direction: " .. direction)
  104.   end
  105.  
  106.   if not success then
  107.     error(msg)
  108.   end
  109. end
  110.  
  111. return {
  112.   turn = turn,
  113.   dig = dig,
  114.   go = go
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement