Advertisement
Blackhome

GravelQuarry

Jan 26th, 2025 (edited)
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.66 KB | Gaming | 0 0
  1. local areaX, areaY, targetBlock = ...
  2.  
  3. local move = require("move1")
  4. local inspect = require("inspect1")
  5. local dropAll = require("dropAll1")
  6.  
  7.  
  8. local saveFilename = "gravel/data.txt"
  9.  
  10. local turtlePos = {0, 0, 0}
  11. local turtleLastPos = {turtlePos[1], turtlePos[2], turtlePos[3]}
  12.  
  13. local turtleVec = {1, 0}
  14. local turtleLastVec = {turtleVec[1], turtleVec[2]}
  15.  
  16. --[[
  17.     workModes:
  18.         1:  normalMode
  19.         2:  on way to start
  20.         3:  on way to last pos
  21.     targetBlock:
  22.         1:  gravel + sand
  23.         2:  gravel
  24.         3:  sand
  25. ]]
  26. local workMode = 1
  27. local targetBlock = 1
  28.  
  29. local arrTargetBlocks = {"minecraft:gravel", "minecraft:sand"}
  30.  
  31. local function saveData(fileName, data)
  32.     local file = fs.open(fileName, "w") -- Datei im Schreibmodus öffnen
  33.     if file then
  34.         file.write(textutils.serialize(data)) -- Tabelle in die Datei schreiben
  35.         file.close()
  36.         print("Daten gespeichert in:", fileName)
  37.     else
  38.         print("Fehler beim Speichern der Daten.")
  39.     end
  40. end
  41.  
  42. local function loadData(fileName)
  43.     if fs.exists(fileName) then
  44.         local file = fs.open(fileName, "r") -- Datei im Lesemodus öffnen
  45.         if file then
  46.             local content = file.readAll()
  47.             file.close()
  48.             return textutils.unserialize(content) -- String in Tabelle umwandeln
  49.         end
  50.     else
  51.         print("Datei nicht gefunden:", fileName)
  52.     end
  53.     return nil -- Standardwert, wenn Datei nicht existiert
  54. end
  55.  
  56. local function updateSaveData()
  57.     local arrArea = {areaX, areaY}
  58.     local dataToSave = {area = arrArea, pos = turtlePos, lastPos = turtleLastPos, vector = turtleVec, lastVector = turtleLastVec, mode = workMode, block = targetBlock}
  59.     saveData(saveFilename, dataToSave)
  60. end
  61.  
  62.  
  63. local function forward()
  64.     move.Forward()
  65.     turtlePos[1] = turtlePos[1] + turtleVec[1]
  66.     turtlePos[2] = turtlePos[2] + turtleVec[2]
  67.     updateSaveData()
  68. end
  69. local function upward()
  70.     move.Up()
  71.     turtlePos[3] = turtlePos[3] + 1
  72.     updateSaveData()
  73. end
  74. local function downward()
  75.     move.Down()
  76.     turtlePos[3] = turtlePos[3] - 1
  77.     updateSaveData()
  78. end
  79. local function turnLeft()
  80.     turtle.turnLeft()
  81.     local buffer = turtleVec[1]
  82.     turtleVec[1] = turtleVec[2]
  83.     turtleVec[2] = -buffer
  84.     updateSaveData()
  85. end
  86. local function turnRight()
  87.     turtle.turnRight()
  88.     local buffer = turtleVec[1]
  89.     turtleVec[1] = -turtleVec[2]
  90.     turtleVec[2] = buffer
  91.     updateSaveData()
  92. end
  93.  
  94. local function digGravel()
  95.     while (not turtle.detectDown()) or inspect.Down(arrTargetBlocks) do
  96.         downward()
  97.     end
  98. end
  99. local function goForward()
  100.     while (not inspect.Forward(arrTargetBlocks)) and turtle.detect() do
  101.         upward()
  102.     end
  103.     forward()
  104. end
  105.  
  106. local function varInArr(var, arr)
  107.     for i = 1, #arr, 1 do
  108.         if var == arr[i] then
  109.             return true
  110.         end
  111.     end
  112.     return false
  113. end
  114.  
  115. local function selectItemFromList(itemList)
  116.     local itemA = turtle.getItemDetail()
  117.     if itemA then
  118.         if varInArr(itemA.name, itemList) then
  119.             return true
  120.         end
  121.     end
  122.  
  123.     for i=1, 16, 1 do
  124.         if turtle.getItemCount(i) > 0 then
  125.             turtle.select(i)
  126.             local itemB = turtle.getItemDetail()
  127.             if itemB then
  128.                 if varInArr(itemB.name, itemList) then
  129.                     return true
  130.                 end
  131.             end
  132.         end
  133.     end
  134.     return false
  135. end
  136.  
  137. local function offload()
  138.     local chestList = { "minecraft:chest" }
  139.     local bChest = inspect.Forward(chestList)
  140.  
  141.     if not bChest then
  142.         if selectItemFromList(chestList) then
  143.             bChest = true
  144.             while not turtle.place() do
  145.                 if not turtle.dig() then
  146.                     print("Can't place Block")
  147.                     bChest = false
  148.                     break
  149.                 end
  150.             end
  151.         end
  152.     end
  153.  
  154.     if bChest then
  155.         return dropAll.Forward()
  156.     end
  157.     return false
  158. end
  159.  
  160. local function goToStart()
  161.     if workMode == 1 then
  162.         turtleLastPos = {turtlePos[1], turtlePos[2], turtlePos[3]}
  163.         turtleLastVec = {turtleVec[1], turtleVec[2]}
  164.         workMode = 2
  165.     end
  166.  
  167.     while turtleVec[1] > -1 do
  168.         turnRight()
  169.     end
  170.     if (turtlePos[1] + turtlePos[2] + math.abs(turtlePos[3])) > 0 then
  171.         while turtlePos[1] > 0 do
  172.             forward()
  173.         end
  174.         turnRight()
  175.         while turtlePos[2] > 0 do
  176.             forward()
  177.         end
  178.         turnLeft()
  179.         while turtlePos[3] > 0 do
  180.             downward()
  181.         end
  182.         while turtlePos[3] < 0 do
  183.             upward()
  184.         end
  185.     end
  186.     offload()
  187. end
  188.  
  189. local function goToLastPosition()
  190.     workMode = 3
  191.  
  192.     while turtlePos[3] < turtleLastPos[3] do
  193.         upward()
  194.     end
  195.     while turtlePos[3] > turtleLastPos[3] do
  196.         downward()
  197.     end
  198.     turnLeft()
  199.     while turtlePos[2] < turtleLastPos[2] do
  200.         forward()
  201.     end
  202.     turnLeft()
  203.     while turtlePos[1] < turtleLastPos[1] do
  204.         forward()
  205.     end
  206.  
  207.     while not (turtleVec[1] == turtleLastVec[1] and turtleVec[2] == turtleLastVec[2]) do
  208.         turnLeft()
  209.     end
  210.  
  211.     workMode = 1
  212. end
  213.  
  214. -- checks if turtle has enough fuel and refuels if not
  215. local function checkAndRefuel()
  216.     local distance = turtlePos[1] + turtlePos[2] + math.abs(turtlePos[3])
  217.     local multiplier = 1
  218.     if distance == 0 then
  219.         distance = turtleLastPos[1] + turtleLastPos[2] + math.abs(turtleLastPos[3])
  220.         multiplier = 2
  221.     end
  222.     local fuelLevel = turtle.getFuelLevel()
  223.  
  224.     local fuelBuffer = 100
  225.  
  226.     if fuelLevel > multiplier * distance + fuelBuffer then
  227.         return
  228.     end
  229.     goToStart()
  230.     offload()
  231.     multiplier = 2
  232.  
  233.     print("Pls add fuel to turtle")
  234.  
  235.     while true do
  236.         io.read()
  237.         move.refuelProcess()
  238.         fuelLevel = turtle.getFuelLevel()
  239.         if fuelLevel > 2 * distance + fuelBuffer then
  240.             break
  241.         else
  242.             print("Not enough fuel was added, pls try again")
  243.         end
  244.     end
  245.     goToLastPosition()
  246.  
  247. end
  248.  
  249. --checks if turtle has an empty slot and unloads if not
  250. local function checkAndUnload()
  251.     for i=16, 1, -1 do
  252.         if turtle.getItemCount(i) == 0 then
  253.             return
  254.         end
  255.     end
  256.     goToStart()
  257.     print("Trying to offload")
  258.     while not offload() do end
  259.     checkAndRefuel()
  260.     goToLastPosition()
  261.  
  262. end
  263.  
  264.  
  265. if not areaX then
  266.     areaX = 4
  267. end
  268. if not areaY then
  269.     areaY = 5
  270. end
  271.  
  272. local data = loadData(saveFilename)
  273. if data then
  274.     areaX, areaY = tonumber(data.area[1]), tonumber(data.area[2])
  275.     turtlePos = { tonumber(data.pos[1]), tonumber(data.pos[2]), tonumber(data.pos[3]) }
  276.     turtleLastPos = { tonumber(data.lastPos[1]), tonumber(data.lastPos[2]), tonumber(data.lastPos[3]) }
  277.     turtleVec = { tonumber(data.vector[1]), tonumber(data.vector[2]), tonumber(data.vector[3]) }
  278.     turtleLastVec = { tonumber(data.lastVector[1]), tonumber(data.lastVector[2]), tonumber(data.lastVector[3]) }
  279.     workMode = tonumber(data.mode)
  280.     targetBlock = tonumber(data.block)
  281. end
  282. if targetBlock == 2 then
  283.     arrTargetBlocks = {"minecraft:gravel"}
  284. elseif targetBlock == 3 then
  285.     arrTargetBlocks = {"minecraft:sand"}
  286. end
  287.  
  288. areaX = tonumber(areaX)
  289. areaY = tonumber(areaY)
  290. targetBlock = tonumber(targetBlock)
  291.  
  292. updateSaveData()
  293. print("areaX: ", areaX, "   areaY: ", areaY)
  294. print("pos: ", textutils.serialize(turtlePos))
  295.  
  296. if workMode == 2 then
  297.     goToStart()
  298.     print("Trying to offload")
  299.     while not offload() do end
  300.     checkAndRefuel()
  301.     goToLastPosition()
  302. end
  303. if workMode == 3 then
  304.     goToLastPosition()
  305. end
  306.  
  307. digGravel()
  308. while turtlePos[2] < areaY do
  309.     if (turtlePos[2] % 2) == 0 then
  310.         while turtlePos[1] < areaX - 1 do
  311.             goForward()
  312.             digGravel()
  313.             checkAndRefuel()
  314.             checkAndUnload()
  315.         end
  316.         if not (turtlePos[2] == areaY - 1) then
  317.             turnRight()
  318.             goForward()
  319.             digGravel()
  320.             turnRight()
  321.             checkAndRefuel()
  322.             checkAndUnload()
  323.         else
  324.             break
  325.         end
  326.     else
  327.         while turtlePos[1] > 0 do
  328.             goForward()
  329.             digGravel()
  330.             checkAndRefuel()
  331.             checkAndUnload()
  332.         end
  333.         if not (turtlePos[2] == areaY - 1) then
  334.             turnLeft()
  335.             goForward()
  336.             digGravel()
  337.             turnLeft()
  338.             checkAndRefuel()
  339.             checkAndUnload()
  340.         else
  341.             break
  342.         end
  343.     end
  344. end
  345.  
  346. goToStart()
  347. offload()
  348.  
  349. -- delete file
  350. local  command = "delete " .. saveFilename
  351. local success, message = shell.run(command)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement