Sv443

ComputerCraft Concrete Converter

Nov 28th, 2023 (edited)
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.05 KB | Gaming | 0 0
  1. -- How many times the turtle has to turn clockwise
  2. -- until it faces the input inventory
  3. -- Use negative numbers for counterclockwise turns
  4. INPUT_TURNS = -1
  5. -- How many times the turtle has to turn clockwise
  6. -- until it faces the output inventory
  7. -- Use negative numbers for counterclockwise turns
  8. OUTPUT_TURNS = 1
  9.  
  10. -- Delay to wait after placing a block - nil for no delay
  11. PLACE_DELAY = nil
  12. -- Delay to wait after breaking a block - nil for no delay
  13. BREAK_DELAY = nil
  14.  
  15.  
  16.  
  17. ORIENTATION_PATH = "/orientation.txt"
  18. curOrient = 0
  19.  
  20. function run()
  21.     print("\n| ConcreteConverter by Sv443\n| https://github.com/Sv443/ComputerCraft-Projects\n")
  22.  
  23.     correctOrientation()
  24.  
  25.     local itemDet = turtle.getItemDetail(1)
  26.     if itemDet ~= nil and itemDet.count > 0 then
  27.         print("! Started with items in inventory!")
  28.         print("! Automatically ejecting them...\n")
  29.         depositItems()
  30.     end
  31.  
  32.     while true do
  33.         local itemDet = turtle.getItemDetail(1)
  34.         if itemDet == nil then
  35.             grabItems()
  36.         end
  37.         itemDet = turtle.getItemDetail(1)
  38.         turtle.select(1)
  39.         if itemDet ~= nil and itemDet.count > 0 then
  40.             print("> Converting "..itemDet.count.." block"..(itemDet.count > 1 and "s" or ""))
  41.         end
  42.         for i = 1, itemDet.count do
  43.             turtle.place()
  44.             if PLACE_DELAY ~= nil then
  45.                 os.sleep(PLACE_DELAY)
  46.             end
  47.             turtle.dig()
  48.             if BREAK_DELAY ~= nil then
  49.                 os.sleep(BREAK_DELAY)
  50.             end
  51.         end
  52.         depositItems()
  53.     end
  54. end
  55.  
  56. -- Turns right or left
  57. function turn(direction)
  58.     local file = fs.open(ORIENTATION_PATH, "w")
  59.     if direction == "right" then
  60.         curOrient = curOrient + 1
  61.         file.write(tostring(curOrient).."\n")
  62.         file.close()
  63.         turtle.turnRight()
  64.     elseif direction == "left" then
  65.         curOrient = curOrient - 1
  66.         file.write(tostring(curOrient).."\n")
  67.         file.close()
  68.         turtle.turnLeft()
  69.     end
  70. end
  71.  
  72. -- Corrects the orientation on reboot
  73. function correctOrientation()
  74.     if not fs.exists(ORIENTATION_PATH) then
  75.         local file = fs.open(ORIENTATION_PATH, "w")
  76.         file.write("0\n")
  77.         file.close()
  78.     end
  79.     local file = fs.open(ORIENTATION_PATH, "r")
  80.     local correctOrient = tonumber(file.readLine())
  81.     if correctOrient ~= 0 then
  82.         if correctOrient > 0 then
  83.             for t = 1, correctOrient do
  84.                 turtle.turnLeft()
  85.             end
  86.         else
  87.             for t = 1, math.abs(correctOrient) do
  88.                 turtle.turnRight()
  89.             end
  90.         end
  91.     end
  92.     file.close()
  93. end
  94.  
  95. -- Grabs new items from the first occupied slot in the input inventory
  96. -- Returns true if items could be grabbed, false otherwise
  97. function grabItems()
  98.     local waiting = false
  99.     turtle.select(1)
  100.     for t = 1, math.abs(INPUT_TURNS) do
  101.         if INPUT_TURNS > 0 then
  102.             turn("right")
  103.         else
  104.             turn("left")
  105.         end
  106.     end
  107.     while not turtle.suck() do
  108.         if not waiting then
  109.             print("* Waiting for blocks...")
  110.             waiting = true
  111.         end
  112.         os.sleep(0.5)
  113.     end
  114.     for t = 1, math.abs(INPUT_TURNS) do
  115.         if INPUT_TURNS > 0 then
  116.             turn("left")
  117.         else
  118.             turn("right")
  119.         end
  120.     end
  121.     return result
  122. end
  123.  
  124. -- Iterates over all slots, depositing items into the output inventory
  125. function depositItems()
  126.     turtle.select(1)
  127.     for t = 1, math.abs(OUTPUT_TURNS) do
  128.         if OUTPUT_TURNS > 0 then
  129.             turn("right")
  130.         else
  131.             turn("left")
  132.         end
  133.     end
  134.     for i = 1, 16 do
  135.         local itemDet = turtle.getItemDetail(i)
  136.         if itemDet == nil or itemDet.count == 0 then
  137.             break
  138.         end
  139.         turtle.select(i)
  140.         turtle.drop()
  141.     end
  142.     for t = 1, math.abs(OUTPUT_TURNS) do
  143.         if OUTPUT_TURNS > 0 then
  144.             turn("left")
  145.         else
  146.             turn("right")
  147.         end
  148.     end
  149. end
  150.  
  151. run()
  152.  
Add Comment
Please, Sign In to add comment