omr__

Diggy turtle

Sep 11th, 2019
249
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. K_VERSION = 0.299
  2.  
  3. -- TODO:
  4. -- Handle startup script
  5. -- Unload chest when full
  6. -- Waypoints
  7. -- Work with negative
  8. -- DONE:
  9. -- Verify fuel and refuel
  10.  
  11. local args = {...}
  12. local home = {0, 0, 0, "south"}
  13. local isContinue = args[4]
  14. local size = {x = tonumber(args[1]),  y = tonumber(args[2]), z = tonumber(args[3])}
  15. local x, y, z, facing
  16.  
  17. function init()
  18.     if not lama then
  19.         os.loadAPI("lama")
  20.         lama.overwrite()
  21.     end
  22.  
  23.     if (isContinue) then
  24.         x, y, z, facing = lama.getPosition()
  25.     else
  26.         x = 0
  27.         y = 0
  28.         z = 0
  29.         facing = "south"
  30.         lama.setPosition(x, y, z, facing)
  31.     end
  32. end
  33.  
  34. function checkFuel()
  35.     let res = true
  36.     if (turtle.getFuelLevel() < 10) then
  37.         print("Fuel level is low, put fuel in #16 slot")
  38.         local selectedSlot = turtle.getSelectedSlot()
  39.         turtle.select(16)
  40.         res = lama.refuel(10)
  41.         turtle.select(selectedSlot)
  42.     end
  43.     return res
  44. end
  45.  
  46. function addWaypoint(name)
  47.     local file = fs.open("waypoints", "w")
  48.     file.write(textutils.serialize(table))
  49.     file.close()
  50.     return true
  51. end
  52.  
  53. function getWaypoint(name)
  54.     if (fs.exists("waypoints")) then
  55.         local file = fs.open("resumeParams", "r")
  56.         local data = file.readAll()
  57.         file.close()
  58.         local params = textutils.unserialize(data)
  59.         fs.delete("waypoints")
  60.         return params
  61.     end
  62. end
  63.  
  64. function unloadInChest()
  65.     if (not lama.detectUp()) then print("Can't find a chest below!") return end
  66.     addWaypoint("unload")
  67.     goTo(home)
  68.     for slot = 1, 15 do
  69.         turtle.select(slot)
  70.         if turtle.detectUp() then turtle.dropUp() end
  71.     end
  72.     turtle.select(1)
  73.     getWaypoint("unload")
  74. end
  75.  
  76. function getPos()
  77.     local _x, _y, _z, _facing = lama.getPosition()
  78.     return {x = tonumber(_x), y = tonumber(_y), z = tonumber(_z), f = _facing}
  79. end
  80.  
  81. function setForward()
  82.     local _facing = getPos().f
  83.     if (_facing == "east") then lama.turnRight()
  84.     elseif (_facing == "north") then lama.turnLeft() lama.turnLeft()
  85.     elseif (_facing == "west") then lama.turnLeft() end
  86. end
  87.  
  88. function setBack()
  89.     local _facing = getPos().f
  90.     if (_facing == "south") then lama.turnLeft() lama.turnLeft()
  91.     elseif (_facing == "east") then lama.turnLeft()
  92.     elseif (_facing == "west") then lama.turnRight() end
  93. end
  94.  
  95. function setLeft()
  96.     local _facing = getPos().f;
  97.     if (_facing == "south") then lama.turnLeft()
  98.     elseif (_facing == "north") then lama.turnRight()
  99.     elseif (_facing== "west") then lama.turnLeft() lama.turnLeft() end
  100. end
  101.  
  102. function setRight()
  103.     local _facing = getPos().f;
  104.     if (_facing == "south") then
  105.         lama.turnRight()
  106.     elseif (_facing == "north") then
  107.         lama.turnLeft()
  108.     elseif (_facing == "east") then
  109.         lama.turnRight()
  110.         lama.turnRight()
  111.     end
  112. end
  113.  
  114. function dig(detectFn, digFn)
  115.     detectFn = detectFn or turtle.detect
  116.     digFn = digFn or turtle.dig
  117.  
  118.     while detectFn() do
  119.         digFn()
  120.         os.sleep(0.5)
  121.     end
  122. end
  123.  
  124. function digUp() dig(turtle.detectUp, turtle.digUp) end
  125.  
  126. function move(moveFn)
  127.     if (turtle.getItemCount(15) > 0)  then unloadInChest() end
  128.     if (checkFuel()) then moveFn() end
  129. end
  130.  
  131. function fForward(n)
  132.     n = n or 1
  133.     for i = 1, n do
  134.         dig()
  135.         move(lama.forward)
  136.     end
  137. end
  138. function fBack() move(lama.back) end
  139. function fUp() digUp() move(lama.up) end
  140. function fDown() turtle.digDown() move(lama.down) end
  141. function fRight() setRight() fForward() setForward() end
  142. function fLeft(n) setLeft() fForward(n) setForward() end
  143.  
  144. function utils()
  145.     local isRowStart = getPos().x == 0
  146.     local isRowEnd =  getPos().x == size.x - 1
  147.     local isColStart = getPos().y == 0
  148.     local isColEnd = getPos().y == size.y - 1
  149.     local isLenStart = getPos().z == 0
  150.     local isLenEnd = getPos().z == size.z
  151.  
  152.     function isUp() return turtle.detectUp() end
  153.     function isDown() return turtle.detectDown() end
  154.     function isForward() return turtle.detect() end
  155.     function isLeft()
  156.         setLeft()
  157.         local res = turtle.detect()
  158.         setForward()
  159.         return res
  160.     end
  161.     function isRight()
  162.         setRight()
  163.         local res = turtle.detect()
  164.         setForward()
  165.         return res
  166.     end
  167.     function isNextLeft()
  168.         if (isLeft()) then return true end
  169.         local res = false
  170.         if (getPos().x == size.x - 2) then return res end
  171.         fLeft()
  172.         res = isLeft()
  173.         fRight()
  174.         return res
  175.     end
  176.     function isNextRight()
  177.         if (isRight()) then return true end
  178.         local res = false
  179.         if (getPos().x == 1) then return res end
  180.         fRight()
  181.         res = isRight()
  182.         fLeft()
  183.         return res
  184.     end
  185.     function isBlockAround()
  186.         local res = false
  187.         if (isRowStart) then res = res or isNextLeft() end
  188.         if (isRowEnd) then res = res or isNextRight() end
  189.         if (isColStart) then res = res or isUp() end
  190.         if (isColEnd) then res = res or isDown() end
  191.         if (not isRowStart and not isRowEnd) then res = res or isLeft() or isRight() end
  192.         if (not isColStart and not isColEnd) then res = res or isUp() or isDown() end
  193.         return res
  194.     end
  195.     return {
  196.         isLeft = isLeft, isRight = isRight, isUp = isUp, isDown = isDown, isForward = isForward,
  197.         isNextLeft = isNextLeft, isNextRight = isNextRight,
  198.         isBlockAround = isBlockAround,
  199.         isRowStart = isRowStart, isRowEnd = isRowEnd,
  200.         isColStart = isColStart, isColEnd = isColEnd,
  201.         isLenStart = isLenStart, isLenEnd = isLenEnd
  202.     }
  203. end
  204.  
  205. function goTo(waypoint)
  206.     local x, y, z, facing = unpack(waypoint)
  207.    
  208.     while (getPos().x ~= x) do
  209.         if (getPos().x < x) then fRight()
  210.         else fLeft() end
  211.     end
  212.  
  213.     while (getPos().y ~= y) do
  214.         if (getPos().y < y) then fUp()
  215.         else fDown() end
  216.     end
  217.  
  218.     while (getPos().z ~= z) do
  219.         if (getPos().z < z) then fForward()
  220.         else fBack() end
  221.     end
  222. end
  223.  
  224. function mine()
  225.     function moveToNextRow()
  226.         repeat
  227.             fForward()
  228.         until(utils().isBlockAround())
  229.     end
  230.  
  231.     function digRow()
  232.         function _dig() if (not utils().isLenEnd) then dig() end end
  233.  
  234.         local cond, moveFn
  235.         if (utils().isRowStart) then
  236.             cond = size.x - 1
  237.             moveFn = fLeft
  238.         elseif (utils().isRowEnd) then
  239.             cond = 0
  240.             moveFn = fRight
  241.         elseif (utils().isNextLeft()) then
  242.             cond = size.x - 1
  243.             moveFn = fLeft
  244.         elseif (utils().isNextRight()) then
  245.             cond = 0
  246.             moveFn = fRight
  247.         end
  248.    
  249.         while (getPos().x ~= cond) do
  250.             _dig()
  251.             moveFn()
  252.         end
  253.  
  254.         _dig()
  255.     end
  256.  
  257.     function digSquare()
  258.         local cond, moveFn
  259.         if (utils().isColStart) then
  260.             cond = size.y - 1
  261.             moveFn = fUp
  262.         elseif (utils().isColEnd) then
  263.             cond = 0
  264.             moveFn = fDown
  265.         elseif (utils().isUp()) then
  266.             cond = size.y - 1
  267.             moveFn = fUp
  268.         elseif (utils().isDown()) then
  269.             cond = 0
  270.             moveFn = fDown
  271.         end
  272.  
  273.         while (getPos().y ~= cond or utils().isBlockAround()) do
  274.             digRow()
  275.             if (getPos().y ~= cond) then moveFn() end
  276.         end
  277.     end
  278.  
  279.     function digArea()
  280.         while (getPos().z ~= size.z or utils().isBlockAround()) do
  281.             digSquare()
  282.             if (getPos().z ~= size.z) then moveToNextRow() end
  283.         end
  284.     end
  285.    
  286.     if (isContinue) then setForward() end
  287.     if (utils().isLenStart) then fForward() end
  288.  
  289.     digArea()
  290. end
  291.  
  292. init()
  293. mine()
  294. goTo(home)
  295.  
  296. return true
RAW Paste Data