Advertisement
sneakybeakylike

ComputerCraft Quarry WIP

May 16th, 2024 (edited)
748
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.19 KB | None | 0 0
  1. -- function to check fuel and refuel if empty
  2. function checkAndRefuel()
  3.     if turtle.getFuelLevel() == 0 then
  4.         -- Select the slot with coal
  5.         for slot = 1, 16 do
  6.             if turtle.getItemCount(slot) > 0 then
  7.                 turtle.select(slot)
  8.                 if turtle.refuel(1) then
  9.                     local file = io.open("output.txt", "a")
  10.                     file:write("Refueled!\n")
  11.                     file:close()
  12.                     return true
  13.                 end
  14.             end
  15.         end
  16.         local file = io.open("output.txt", "a")
  17.         file:write("No coal found!\n")
  18.         file:close()
  19.         return false
  20.     end
  21.     return true
  22. end
  23.  
  24. -- function to check if inventory is full
  25. function isInventoryFull()
  26.     for i = 1, 16 do
  27.         if turtle.getItemCount(i) == 0 then
  28.             return false
  29.         end
  30.     end
  31.     return true
  32. end
  33.  
  34. -- fixes inventory scattering
  35. function stackItems()
  36.     -- remember seen items
  37.     m = {}
  38.  
  39.     for i = 1, 16 do
  40.         local this = turtle.getItemDetail(i)
  41.  
  42.         if this ~= nil then
  43.             -- slot is not empty
  44.             local saved = m[this.name .. (this.damage or "")]
  45.            
  46.             if saved ~= nil then
  47.                 -- we've seen this item before in the inventory
  48.                 local amount = this.count
  49.  
  50.                 turtle.select(i)
  51.                 turtle.transferTo(saved.slot)
  52.                 if amount > saved.space then
  53.                     -- we have leftovers, and now the saved slot is full, so we replace it by the current one
  54.                     saved.slot = i
  55.                     saved.count = amount - saved.space
  56.                     -- update on table
  57.                     m[this.name .. (this.damage or "")] = saved
  58.                 elseif amount == saved.space then
  59.                     -- just delete the entry
  60.                     m[this.name .. (this.damage or "")] = nil
  61.                 end
  62.             else
  63.                 -- there isn't another slot with this item so far, so sign this one up
  64.                 this.slot = i
  65.                 this.space = turtle.getItemSpace(i)
  66.                 m[this.name .. (this.damage or "")] = this
  67.             end
  68.         end
  69.     end
  70. end
  71.  
  72. -- selects an item if its in the inventory, otherwise returns false
  73. function selectItem(name)
  74.     for i = 1, 16 do
  75.         local data = turtle.getItemDetail(i)
  76.         if data and data.name == name then
  77.             turtle.select(i)
  78.             return true
  79.         end
  80.     end
  81.     return false
  82. end
  83.  
  84. -- returns item count given item name
  85. function getItemCount(name)
  86.     local count = 0
  87.     for i = 1, 16 do
  88.         local data = turtle.getItemDetail(i)
  89.         if data and data.name == name then
  90.             count = count + data.count
  91.         end
  92.     end
  93.     return count
  94. end
  95.  
  96. -- gets direction
  97. -- 0 North, 1 East, 2 South, 3 West
  98. function getDirection()
  99.     local old_x, old_y, old_z = gps.locate()
  100.     -- determine direction we are facing
  101.     if not checkAndRefuel() then
  102.         return false
  103.     end
  104.     turtle.back()
  105.     local new_x, new_y, new_z = gps.locate()
  106.     if not checkAndRefuel() then
  107.         return false
  108.     end
  109.     turtle.forward()
  110.     if new_x > old_x then
  111.         -- facing west
  112.         return true, 3
  113.     elseif new_x < old_x then
  114.         -- facing east
  115.         return true, 1
  116.     elseif new_z > old_z then
  117.         -- facing north
  118.         return true, 0
  119.     end
  120.     return true, 2
  121. end
  122.  
  123. -- moves to position
  124. function moveTo(x, y, z)
  125.     local cur_x, cur_y, cur_z = gps.locate()
  126.     local dx = math.abs(x - cur_x)
  127.     local dy = math.abs(y - cur_y)
  128.     local dz = math.abs(z - cur_z)    
  129.    
  130.     -- rotate towards south
  131.     local direction_success, direction = getDirection()
  132.     if not direction_success then
  133.         return false
  134.     end
  135.     if direction == 3 then
  136.         turtle.turnLeft()
  137.     elseif direction == 1 then
  138.         turtle.turnRight()
  139.     elseif direction == 0 then
  140.         turtle.turnLeft()
  141.         turtle.turnLeft()
  142.     end
  143.     -- now we are oriented towards south
  144.     local file = io.open("output.txt", "a")
  145.     file:write("Facing South Now\n")
  146.     file:write("----------------\n")
  147.     file:write("Current pos: (", cur_x, ", ", cur_y, ", ", cur_z, ")\n")
  148.     file:write("Moving towards: (", x, ", ", y, ", ", z, ")\n")
  149.     file:write("----------------\n")
  150.     file:close()
  151.     -- north -Z direction
  152.     -- south +Z direction
  153.     -- east +X direction
  154.     -- west -X direction
  155.     -- correct y first, then x and z
  156.     local align_y_last = false
  157.     if cur_y > y then
  158.         align_y_last = true
  159.     end
  160.     if dy == 0 and not align_y_last then
  161.         local file = io.open("output.txt", "a")
  162.         file:write("Y aligned\n")
  163.         file:close()
  164.     elseif not align_y_last then
  165.         local file = io.open("output.txt", "a")
  166.         file:write("Correcting Y...\n")
  167.         file:close()
  168.         if cur_y < y then
  169.             -- we need to head up
  170.             for i = 1, dy do
  171.                 if not checkAndRefuel() then
  172.                     return false
  173.                 end
  174.                 turtle.up()
  175.             end
  176.             local file = io.open("output.txt", "a")
  177.             file:write("Y aligned\n")
  178.             file:close()
  179.         else
  180.             -- we need to head down
  181.             for i = 1, dy do
  182.                 if not checkAndRefuel() then
  183.                     return false
  184.                 end
  185.                 turtle.down()
  186.             end
  187.         end
  188.     end
  189.     -- correct x
  190.     if dx == 0 then
  191.         local file = io.open("output.txt", "a")
  192.         file:write("X aligned\n")
  193.         file:close()
  194.     else
  195.         local file = io.open("output.txt", "a")
  196.         file:write("Correcting X...\n")
  197.         file:close()
  198.         if cur_x < x then
  199.             -- we need to head EAST
  200.             local file = io.open("output.txt", "a")
  201.             file:write("cur_x < x | rotating east\n")
  202.             file:close()
  203.             turtle.turnLeft()
  204.             for i = 1, dx do
  205.                 if not checkAndRefuel() then
  206.                     return false
  207.                 end
  208.                 turtle.forward()
  209.             end
  210.             local file = io.open("output.txt", "a")
  211.             file:write("rotating south\n")
  212.             file:close()
  213.             turtle.turnRight()
  214.         else
  215.             -- we need to head WEST
  216.             local file = io.open("output.txt", "a")
  217.             file:write("cur_x > x | rotating west\n")
  218.             file:close()
  219.             turtle.turnRight()
  220.             for i = 1, dx do
  221.                 if not checkAndRefuel() then
  222.                     return false
  223.                 end
  224.                 turtle.forward()
  225.             end
  226.             local file = io.open("output.txt", "a")
  227.             file:write("rotating south\n")
  228.             file:close()
  229.             turtle.turnLeft()
  230.         end
  231.         local file = io.open("output.txt", "a")
  232.         file:write("X aligned\n")
  233.         file:close()
  234.     end
  235.     -- correct z
  236.     if dz == 0 then
  237.         local file = io.open("output.txt", "a")
  238.         file:write("Z aligned\n")
  239.         file:close()
  240.     else
  241.         local file = io.open("output.txt", "a")
  242.         file:write("Correcting Z...\n")
  243.         file:close()
  244.         if cur_z < z then
  245.             -- we need to head south
  246.             for i = 1, dz do
  247.                 if not checkAndRefuel() then
  248.                     return false
  249.                 end
  250.                 turtle.forward()
  251.             end
  252.         else
  253.             local file = io.open("output.txt", "a")
  254.             file:write("cur_z > z | rotating north\n")
  255.             file:close()
  256.             turtle.turnLeft()
  257.             turtle.turnLeft()
  258.             for i = 1, dz do
  259.                 if not checkAndRefuel() then
  260.                     return false
  261.                 end
  262.                 turtle.forward()
  263.             end
  264.             local file = io.open("output.txt", "a")
  265.             file:write("rotating south\n")
  266.             file:close()
  267.             turtle.turnLeft()
  268.             turtle.turnLeft()
  269.         end
  270.         local file = io.open("output.txt", "a")
  271.         file:write("Z aligned\n")
  272.         file:close()
  273.     end
  274.     -- check if we align y last
  275.     if dy == 0 and align_y_last then
  276.         local file = io.open("output.txt", "a")
  277.         file:write("Y aligned\n")
  278.         file:close()
  279.     elseif align_y_last then
  280.         local file = io.open("output.txt", "a")
  281.         file:write("Correcting Y...\n")
  282.         file:close()
  283.         if cur_y < y then
  284.             -- we need to head up
  285.             for i = 1, dy do
  286.                 if not checkAndRefuel() then
  287.                     return false
  288.                 end
  289.                 turtle.up()
  290.             end
  291.             local file = io.open("output.txt", "a")
  292.             file:write("Y aligned\n")
  293.             file:close()
  294.         else
  295.             -- we need to head down
  296.             for i = 1, dy do
  297.                 if not checkAndRefuel() then
  298.                     return false
  299.                 end
  300.                 turtle.down()
  301.             end
  302.         end
  303.     end
  304.     cur_x, cur_y, cur_z = gps.locate()
  305.     if cur_x ~= x or cur_y ~= y or cur_z ~= z then
  306.         local file = io.open("output.txt", "a")
  307.         file:write("Failed to move to (", x, ", ", y, ", ", z, ")\n")
  308.         file:close()
  309.         return false
  310.     end
  311.     return true
  312. end
  313.  
  314. -- goes to storage chest, dumps inventory, returns back
  315. -- x y and z are the starting location of the quarry
  316. function storeItems(x, y, z)
  317.     -- store current position and direction
  318.     local cur_x, cur_y, cur_z = gps.locate()
  319.     local direction_success, direction = getDirection()
  320.     if not direction_success then
  321.         return false
  322.     end
  323.     local file = io.open("output.txt", "a")
  324.     file:write("Moving to Origin location!!!\n")
  325.     file:close()
  326.     -- move to origin of quarry
  327.     if not moveTo(x, y, z) then
  328.         return false
  329.     end
  330.     local file = io.open("output.txt", "a")
  331.     file:write("Finished moving to Origin location!!!\n")
  332.     file:close()
  333.     -- rotate towards north
  334.     turtle.turnLeft()
  335.     turtle.turnLeft()
  336.     local file = io.open("output.txt", "a")
  337.     file:write("Checking for chest\n")
  338.     file:close()
  339.     -- now we are facing north towards our storage chest
  340.     local inspect_data_success, inspect_data = turtle.inspect()
  341.     if not inspect_data_success then
  342.         local file = io.open("output.txt", "a")
  343.         file:write("No block detected at starting point\n")
  344.         file:close()
  345.         return false
  346.     end
  347.     local file = io.open("output.txt", "a")
  348.     file:write("CHEST DATA: {name=", inspect_data.name, "\n")
  349.     file:close()
  350.     if inspect_data.tags and inspect_data.tags["forge:chests"] then
  351.         local file = io.open("output.txt", "a")
  352.         file:write("Forge Chest detected. Storing items\n")
  353.         file:close()
  354.         stackItems()
  355.         -- assume coal is in first item slot. transfer all other items to chest
  356.         for i = 2, 16 do
  357.             local item_data = turtle.getItemDetail(i)
  358.             if item_data ~= nil then
  359.                 -- item slot is not empty, so we will select it and drop
  360.                 turtle.select(i)
  361.                 turtle.drop()
  362.             end
  363.         end
  364.     else
  365.         local file = io.open("output.txt", "a")
  366.         file:write("Not a forge chest in front of the turtle.\n")
  367.         file:close()
  368.         return false
  369.     end
  370.     local file = io.open("output.txt", "a")
  371.     file:write("Finished storing items\n")
  372.     file:write("Moving to previous location\n")
  373.     file:close()
  374.     -- now we have emptied our inventory. now we need to return to (old_x, old_y, old_z) facing the direction we were previously facing
  375.     if not moveTo(cur_x, cur_y, cur_z) then
  376.         return false
  377.     end
  378.     local file = io.open("output.txt", "a")
  379.     file:write("Re-orienting to old direction\n")
  380.     file:close()
  381.     -- facing south, rotate towards direction
  382.     -- 0 North, 1 East, 2 South, 3 West
  383.     if direction == 3 then
  384.         turtle.turnRight()
  385.     elseif direction == 1 then
  386.         turtle.turnLeft()
  387.     elseif direction == 0 then
  388.         turtle.turnLeft()
  389.         turtle.turnLeft()
  390.     end
  391.     local file = io.open("output.txt", "a")
  392.     file:write("Finished with storeItems()\n")
  393.     file:close()
  394.     return true
  395. end
  396.  
  397. -- mines a layer of the quarry
  398. -- x y and z are the starting location of the quarry
  399. function mine_layer(start_x, start_y, start_z)
  400.     -- stackItems once per layer
  401.     -- check if inventory is full after every block mined
  402.     -- if inventory full then call storeItems
  403.     stackItems()
  404.     for x = 1, 16 do
  405.         for y = 1, 15 do
  406.             if isInventoryFull() then
  407.                 if not storeItems(start_x, start_y, start_z) then
  408.                     return false
  409.                 end
  410.             end
  411.             turtle.dig()
  412.             if isInventoryFull() then
  413.                 if not storeItems(start_x, start_y, start_z) then
  414.                     return false
  415.                 end
  416.             end
  417.             turtle.digDown()
  418.             if not checkAndRefuel() then
  419.                 return false
  420.             end
  421.             turtle.forward()
  422.         end
  423.         -- line finished so now we gotta dig down
  424.         if isInventoryFull() then
  425.             if not storeItems(start_x, start_y, start_z) then
  426.                 return false
  427.             end
  428.         end
  429.         turtle.digDown()
  430.         if x % 2 == 0 and x ~= 16 then
  431.             turtle.turnLeft()
  432.             if isInventoryFull() then
  433.                 if not storeItems(start_x, start_y, start_z) then
  434.                     return false
  435.                 end
  436.             end
  437.             turtle.dig()
  438.             if not checkAndRefuel() then
  439.                 return false
  440.             end
  441.             turtle.forward()
  442.             turtle.turnLeft()
  443.         else
  444.             turtle.turnRight()
  445.             if isInventoryFull() then
  446.                 if not storeItems(start_x, start_y, start_z) then
  447.                     return false
  448.                 end
  449.             end
  450.             turtle.dig()
  451.             if not checkAndRefuel() then
  452.                 return false
  453.             end
  454.             turtle.forward()
  455.             turtle.turnRight()
  456.         end
  457.         -- ready to start new line
  458.     end
  459.     -- finished layer
  460.     return true
  461. end
  462.  
  463. -- main loop of quarry
  464. local start_x, start_y, start_z = gps.locate()
  465. local file = io.open("output.txt", "a")
  466. file:write("start_x = ", start_x, ", start_y = ", start_y, ", start_z = ", start_z, "\n")
  467. file:close()
  468. if start_x == nil or start_y == nil or start_z == nil then
  469.     local file = io.open("output.txt", "a")
  470.     file:write("GPS Offline\n")
  471.     file:close()
  472.     return
  473. end
  474. while 1 == 1 do
  475.     if not mine_layer(start_x, start_y, start_z) then
  476.         local file = io.open("output.txt", "a")
  477.         file:write("Failed to mine layer. Either out of storage or fuel\n")
  478.         file:close()
  479.         return
  480.     else
  481.         local file = io.open("output.txt", "a")
  482.         file:write("Mined layer\n")
  483.         file:close()
  484.         -- get ready for the next layer
  485.         -- end layer facing south
  486.         turtle.turnLeft()
  487.         for i = 1, 15 do
  488.             if not checkAndRefuel() then
  489.                 return false
  490.             end
  491.             turtle.forward()
  492.         end
  493.         turtle.turnRight()
  494.         for i = 1, 2 do
  495.             if not checkAndRefuel() then
  496.                 return false
  497.             end
  498.             turtle.down()
  499.             turtle.digDown()
  500.         end
  501.         -- now we are ready to start the next layer
  502.     end
  503. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement