Advertisement
Guest User

quarry

a guest
Jan 19th, 2020
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.69 KB | None | 0 0
  1. local pos = {x = nil, y = nil, z = nil}
  2. local orientation = nil
  3.  
  4. --possible states are "home", "movingToQuarry",
  5. -- "inQuarry", "mining", "exitingQuarry", "movingHome"
  6. local state = nil
  7.  
  8. local settings = {yTravel = 2, fuelMin = 1000,
  9.                     refuelAmount = 1500, managerID = nil, distressID = nil}
  10.  
  11. -- MAKE SURE SLOTS COME IN ORDER (i.e fuel then enderFuel then enderStorage...)
  12. local slots = {fuel = {1,2,3}, noDig = nil, trash = {4,5,6}, enderFuel = nil,
  13.                 enderStorage = nil}
  14. -- Storage chest in front, fuel chest below
  15.  
  16. local home = {x = nil, y = nil, z = nil, orientation = nil}
  17.  
  18. local job = {x = nil, y = nil, z = nil, orientation = nil, size = nil}
  19.  
  20. --north, east, south, west
  21. local xDiff = {0, 1, 0, -1}
  22. local zDiff = {-1, 0, 1, 0}
  23.  
  24. local selected = nil
  25.  
  26. --**** utility ****--
  27. local function member(table, value)
  28.     if table == nil then
  29.         return false
  30.     end
  31.     for k,v in ipairs(table) do
  32.         if table[k] == value then
  33.             return true
  34.         end
  35.     end
  36.     return false
  37. end
  38. --**** inventory stuff ****--
  39. local function select(slot)
  40.     if selected ~= slot then
  41.         turtle.select(slot)
  42.         selected = slot
  43.     end
  44. end
  45. local function isTrash() --checks current slot for trash
  46.     if slots.noDig then
  47.         for i = 1,#slots.noDig do
  48.             if turtle.compareTo(slots.noDig[i]) then
  49.                 return true
  50.             end
  51.         end
  52.     end
  53.     if slots.trash then
  54.         for i =1,#slots.trash do
  55.             if turtle.compareTo(slots.trash[i]) then
  56.                 return true
  57.             end
  58.         end
  59.     end
  60.     return false
  61. end
  62. local function isSpecialSlot(slot)
  63.     for k,v in pairs(slots) do
  64.         if type(v) == "table" then
  65.             for i = 1,#slots[k] do
  66.                 if slot == slots[k][i] then
  67.                     return true
  68.                 end
  69.             end
  70.         elseif slot == v then
  71.             return true
  72.         end
  73.     end
  74.     return false
  75. end
  76. local function removeTrash()
  77.     for i = 1,16 do
  78.         if turtle.getItemCount(i) > 0 then
  79.             select(i)
  80.             if isTrash() then
  81.  
  82.                 if member(slots.noDig, i) or member(slots.trash, i) then
  83.                     turtle.dropUp(math.max(0, turtle.getItemCount(i) - 1))
  84.                 else
  85.                     turtle.dropUp()
  86.                 end
  87.  
  88.             end
  89.         end
  90.     end
  91.     select(1)
  92. end
  93. local function organizeInventory()
  94.     for i = 16,2,-1 do
  95.         if not isSpecialSlot(i) and turtle.getItemCount(i) > 0 then
  96.             select(i)
  97.             for j = 1, i-1 do
  98.                 turtle.transferTo(j)
  99.             end
  100.         end
  101.     end
  102. end
  103. local function dig()
  104.     if slots.noDig then
  105.         local detectedUp = false
  106.         local detectedDown = false
  107.         for i = 1,#slots.noDig do
  108.             select(slots.noDig[i])
  109.             if not detectedUp and turtle.compareUp() then
  110.                 detectedUp = true
  111.             end
  112.             if not detectedDown and turtle.compareDown() then
  113.                 detectedDown = true
  114.             end
  115.             if detectedUp and detectedDown then
  116.                 break
  117.             end
  118.         end
  119.         select(slots.noDig[1])
  120.         if not detectedUp then
  121.             turtle.digUp()
  122.         end
  123.         if not detectedDown then
  124.             turtle.digDown()
  125.         end
  126.     else
  127.         turtle.digUp()
  128.         turtle.digDown()
  129.     end
  130. end
  131. --**** file handling ****--
  132. local paths = {state = "quarryData/state", home = "quarryData/home",
  133.                settings = "quarryData/settings", job = "quarryData/job"}
  134. local function save(file, data)
  135.     local h = fs.open(file,"w")
  136.     h.write(textutils.serialize(data))
  137.     h.close(h)
  138.     return true
  139. end
  140. local function load(file)
  141.     if not fs.exists(file) then
  142.         print("tried to load an invalid file: "..file)
  143.         return nil
  144.     end
  145.     local h = fs.open(file, "r")
  146.     local rv = textutils.unserialize(h.readAll())
  147.     h.close()
  148.     return rv
  149. end
  150. --**** basic movement ****--
  151. local function moveForward(safe)
  152.     while not turtle.forward() do
  153.         if not safe then
  154.             turtle.dig()
  155.         end
  156.     end
  157.     pos.x = pos.x + xDiff[orientation]
  158.     pos.z = pos.z + zDiff[orientation]
  159. end
  160. local function moveUp(safe)
  161.     while not turtle.up() do
  162.         if not safe then
  163.             turtle.digUp()
  164.         end
  165.     end
  166.     pos.y = pos.y + 1
  167. end
  168. local function moveDown(safe)
  169.     while not turtle.down() do
  170.         if not safe then
  171.             turtle.digDown()
  172.         end
  173.     end
  174.     pos.y = pos.y - 1
  175. end
  176. local function right()
  177.     turtle.turnRight()
  178.     orientation = orientation%4 + 1
  179. end
  180. local function left()
  181.     turtle.turnLeft()
  182.     orientation = (orientation - 2)%4 + 1
  183. end
  184. local function look(direction)
  185.     while orientation ~= direction do
  186.         if (orientation - direction)%4 >= 2 then
  187.             right()
  188.         else
  189.             left()
  190.         end
  191.     end
  192. end
  193. local function moveTo(xTarget, yTarget, zTarget, safeMode)
  194.     while yTarget > pos.y do
  195.         moveUp(safeMode)
  196.     end
  197.     while yTarget < pos.y do
  198.         moveDown(safeMode)
  199.     end
  200.     if xTarget < pos.x then
  201.         look(4)
  202.     elseif xTarget > pos.x then
  203.         look(2)
  204.     end
  205.     while xTarget ~= pos.x do
  206.         moveForward(safeMode)
  207.     end
  208.     if zTarget < pos.z then
  209.         look(1)
  210.     elseif zTarget > pos.z then
  211.         look(3)
  212.     end
  213.     while zTarget ~= pos.z do
  214.         moveForward(safeMode)
  215.     end
  216. end
  217. --**** advanced movement ****--
  218. local function moveHome() -- can only be called from states "mining", "exitingQuarry", "movingHome"
  219.     if state == "mining" then
  220.         moveTo(job.x, pos.y, job.y)
  221.         state = "exitingQuarry"
  222.         save(paths.state, state)
  223.     end
  224.     if state == "exitingQuarry" then
  225.         moveTo(job.x, settings.yTravel, job.z)
  226.         state = "movingHome"
  227.         save(paths.state, state)
  228.     end
  229.     if state == "movingHome" then
  230.         moveTo(home.x, settings.yTravel, home.z)
  231.         moveTo(home.x, home.y, home.z, true) --may get stuck bc safeMode
  232.         look(home.orientation)
  233.         state = "home"
  234.         save(paths.state, state)
  235.     end
  236. end
  237. local function moveToQuarry() -- gets stuck by safe mode sometimes
  238.     if state == "home" then
  239.         moveTo(home.x, settings.yTravel, home.z, true)
  240.         state = "movingToQuarry"
  241.         save(paths.state, state)
  242.     end
  243.     if state == "movingToQuarry" then
  244.         moveTo(job.x, settings.yTravel, job.z)
  245.         state = "enteringQuarry"
  246.         save(paths.state, state)
  247.     end
  248.     if state == "enteringQuarry" then
  249.         moveTo(job.x, job.y, job.z)
  250.         look(job.orientation)
  251.         state = "mining"
  252.         save(paths.state, state)
  253.     end
  254. end
  255. local function locate() -- loops until success
  256.     pos.z = nil
  257.     while pos.z == nil do
  258.         pos.x, pos.y, pos.z = gps.locate(1)
  259.     end
  260. end
  261. local function orient(safe)
  262.     locate()
  263.     local x1 = pos.x
  264.     local z1 = pos.z
  265.     local rightTurns = 0
  266.     local ups = 0
  267.     while true do
  268.         if not safe then
  269.             turtle.dig()
  270.         end
  271.         turtle.attack()
  272.         if not turtle.forward() then
  273.             turtle.turnRight()
  274.             rightTurns = rightTurns + 1
  275.             if rightTurns % 4 == 0 then
  276.                 if not safe then
  277.                     turtle.digUp()
  278.                 end
  279.                 if turtle.up() then
  280.                     ups = ups + 1
  281.                 else
  282.                     print("stuck in 'function: orient'")
  283.                 end
  284.             end
  285.         else
  286.             break
  287.         end
  288.     end
  289.     locate()
  290.     --north, east, south, west
  291.     --local xDiff = {0, 1, 0, -1}
  292.     --local zDiff = {-1, 0, 1, 0}
  293.     if pos.x > x1 then
  294.         orientation = 2
  295.     elseif pos.x < x1 then
  296.         orientation = 4
  297.     elseif pos.z > z1 then
  298.         orientation = 3
  299.     elseif pos.z < z1 then
  300.         orientation = 1
  301.     else
  302.         print("Orientation was not decided properly")
  303.     end
  304.  
  305.     right()
  306.     rightTurns = rightTurns + 1
  307.     right()
  308.     rightTurns = rightTurns + 1
  309.  
  310.     moveForward(safe)
  311.  
  312.     for i = 1,ups do
  313.         moveDown(safe)
  314.     end
  315.     for i = 1,rightTurns%4 do
  316.         left()
  317.     end
  318. end
  319. --**** refueling and dropping ****--
  320. local function getNextEmpty(i) --returns the next empty slot after i
  321.     for j = i,16 do
  322.         if turtle.getItemCount(j) == 0 then
  323.             return j
  324.         end
  325.     end
  326.     return nil
  327. end
  328.  
  329. local function normalRefuel()
  330.     if not slots.fuel then
  331.         return false
  332.     end
  333.     for i = 1,#slots.fuel do
  334.         if turtle.getItemCount(slots.fuel[i]) > 0 then
  335.             select(i)
  336.             while turtle.getFuelLevel() < settings.fuelMin + settings.refuelAmount and turtle.refuel(1) do
  337.             end
  338.             if turtle.getFuelLevel() > settings.fuelMin + settings.refuelAmount then
  339.                 break
  340.             end
  341.         end
  342.     end
  343.     select(1)
  344. end
  345. local function normalDrop()
  346.     for i = 1,16 do
  347.         select(i)
  348.         if member(slots.noDig, i) or member(slots.trash, i) then
  349.             turtle.drop(math.max(0, turtle.getItemCount(i) - 1))
  350.         elseif not isSpecialSlot(i) then -- inefficient iterations, negligable
  351.             turtle.drop()
  352.         end
  353.     end
  354.     select(1)
  355. end
  356. local function restockFuel() --Assumes 16 always is an insignificant slot
  357.     select(slots.fuel[1])
  358.     while turtle.getFuelLevel() < settings.fuelMin + settings.refuelAmount do --refuelling loop
  359.         if not turtle.refuel(1) then
  360.             turtle.drop()
  361.             if not turtle.suckDown() then
  362.                 return
  363.             end
  364.         end
  365.     end
  366.     if not getNextEmpty(slots.fuel[#slots.fuel]+1) then --makes sure there is an empty slot
  367.         select(16)
  368.         turtle.drop()
  369.     end
  370.  
  371.     for i=1,#slots.fuel do
  372.         select(slots.fuel[i])
  373.         if not turtle.refuel(0) then
  374.             turtle.drop()
  375.         end
  376.         local nextEmpty = 16
  377.         while turtle.getItemCount(selected) < 64 do
  378.             local count = turtle.getItemCount(selected)
  379.             nextEmpty = getNextEmpty(selected+1) --fails if slot 16 is set for fuel
  380.             if not turtle.suckDown() then
  381.                 break
  382.             elseif turtle.getItemCount(selected) == count then --sucked but itemcount did not change
  383.                 select(nextEmpty)
  384.                 turtle.drop()
  385.                 select(slots.fuel[i])
  386.             else
  387.                 if not turtle.refuel(0) then
  388.                     turtle.drop()
  389.                 end
  390.             end
  391.         end
  392.         select(nextEmpty)
  393.         turtle.dropDown()
  394.     end
  395.     select(1)
  396. end
  397.  
  398. local function enderRefuel(safe) -- loops until success
  399.     select(slots.enderFuel)
  400.     local rights = 0
  401.     local ups = 0
  402.     while true do --chest placing loop
  403.         if not safe then
  404.             turtle.dig()
  405.         end
  406.         turtle.attack()
  407.         if turtle.place() then
  408.             break --
  409.         end
  410.  
  411.         turtle.turnRight()
  412.         rights = rights + 1
  413.         if rights%4==0 then
  414.             if not safe then
  415.                 turtle.digUp()
  416.             end
  417.             turtle.attackUp()
  418.             if turtle.up() then
  419.                 ups = ups + 1
  420.             end
  421.         end
  422.     end
  423.  
  424.     while turtle.getFuelLevel() < settings.fuelMin + settings.refuelAmount do
  425.         if turtle.getItemCount(selected) == 0 then
  426.             turtle.suck()
  427.         end
  428.         if not turtle.refuel(1) then
  429.             turtle.dropUp()
  430.         end
  431.     end
  432.     turtle.drop()
  433.     turtle.dig()
  434.     select(1)
  435.  
  436.     for i = 1,rights%4 do
  437.         turtle.turnLeft()
  438.     end
  439.     for i=1,ups do
  440.         while not turtle.down() do
  441.             if not safe then
  442.                 turtle.digDown()
  443.             end
  444.             turtle.attackDown()
  445.         end
  446.     end
  447. end
  448. local function enderDrop(safe) -- loops until success
  449.     select(slots.enderStorage)
  450.     local rights = 0
  451.     local ups = 0
  452.     while true do --chest placing loop
  453.         if not safe then
  454.             turtle.dig()
  455.         end
  456.         turtle.attack()
  457.         if turtle.place() then
  458.             break --
  459.         end
  460.  
  461.         turtle.turnRight()
  462.         rights = rights + 1
  463.         if rights%4==0 then
  464.             if not safe then
  465.                 turtle.digUp()
  466.             end
  467.             turtle.attackUp()
  468.             if turtle.up() then
  469.                 ups = ups + 1
  470.             end
  471.         end
  472.     end
  473.  
  474.     normalDrop()
  475.  
  476.     select(slots.enderStorage)
  477.     turtle.drop()
  478.     turtle.dig()
  479.     select(1)
  480.  
  481.     for i = 1,rights%4 do
  482.         turtle.turnLeft()
  483.     end
  484.     for i=1,ups do
  485.         while not turtle.down() do
  486.             if not safe then
  487.                 turtle.digDown()
  488.             end
  489.             turtle.attackDown()
  490.         end
  491.     end
  492. end
  493. local function goHomeDropReturn()
  494.     local xTemp, yTemp, zTemp, oTemp = pos.x, pos.y, pos.z, orientation
  495.     moveHome()
  496.     normalDrop()
  497.     if not slots.enderFuel then
  498.         restockFuel()
  499.     end
  500.     moveToQuarry()
  501.     moveTo(xTemp, yTemp, zTemp)
  502.     look(oTemp)
  503. end
  504. --**** digging and quarry ****--
  505. local function digLayer(length, width) -- consecutive calls with a non-quadratic size results in varying results
  506.     for i = 1,width do
  507.         for j=1,length-1 do
  508.  
  509.             -------------------------------------
  510.             if turtle.getItemCount(16) > 0 then
  511.                 removeTrash()
  512.                 organizeInventory()
  513.                 if turtle.getItemCount(16) > 0 then
  514.                     if slots.enderStorage then
  515.                         enderdrop()
  516.                     else
  517.                         goHomeDropReturn()
  518.                     end
  519.                 end
  520.             end
  521.             -------------------------------------
  522.  
  523.             dig()
  524.             moveForward()
  525.         end
  526.         dig()
  527.        
  528.         if i < width then
  529.             if i%2==1 then
  530.                 right()
  531.                 moveForward()
  532.                 right()
  533.             else
  534.                 left()
  535.                 moveForward()
  536.                 left()
  537.             end
  538.         end
  539.     end
  540.     -------------------------------------------------------
  541.     if turtle.getFuelLevel() < settings.fuelMin then
  542.         if slots.enderFuel then
  543.             enderRefuel()
  544.         else
  545.             normalRefuel()
  546.             if turtle.getFuelLevel() < settings.fuelMin then
  547.                 goHomeDropReturn()
  548.             end
  549.         end
  550.     end
  551.     -------------------------------------------------------
  552. end
  553.  
  554. ----[[
  555. home = load(paths.home)
  556. job = load(paths.job)
  557. orient(true)
  558. moveTo(job.x, home.y + settings.yTravel, job.z, true)
  559. moveTo(job.x, job.y, job.z)
  560. look(job.orientation)
  561. digLayer(job.size, job.size)
  562. removeTrash()
  563. moveTo(home.x, home.y + settings.yTravel, home.z, true)
  564. moveTo(home.x, home.y, home.z, true)
  565. look(home.orientation)
  566. normalDrop()
  567. restockFuel()
  568. --]]
  569.  
  570. --[[
  571. orient(true)
  572. home = {x = pos.x, y = pos.y, z = pos.z, orientation = orientation}
  573. save(paths.home, home)
  574. job = {x = home.x + xDiff[orientation]*5, y = home.y - 2, z = home.z + zDiff[orientation]*5, orientation = orientation}
  575. save(paths.job, job)--]]
  576.  
  577. --rednet.open("right")
  578.  
  579. --MAKE SURE THE TURTLE CHECKS FUEL IMMIDIATELY, AS TO NOT GET STUCK
  580.  
  581. --Tested functions:
  582. -- member, select, isTrash, isSpecialSlot, removeTrash, organizeInventory,
  583. -- dig, save, load, moveForward, moveUp, moveDown, right, left, moveTo, locate, orient,
  584. -- getNextEmpty, normalRefuel, normalDrop, enderDrop, restockFuel
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement