Blackhome

GravelQuarry

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