Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local areaX, areaY, targetBlock = ...
- local move = require("move1")
- local inspect = require("inspect1")
- local dropAll = require("dropAll1")
- local saveFilename = "gravel/data.txt"
- local turtlePos = {0, 0, 0}
- local turtleLastPos = {turtlePos[1], turtlePos[2], turtlePos[3]}
- local turtleVec = {1, 0}
- local turtleLastVec = {turtleVec[1], turtleVec[2]}
- --[[
- workModes:
- 1: normalMode
- 2: on way to start
- 3: on way to last pos
- targetBlock:
- 1: gravel + sand
- 2: gravel
- 3: sand
- ]]
- local workMode = 1
- local targetBlock = 1
- local arrTargetBlocks = {"minecraft:gravel", "minecraft:sand"}
- local function saveData(fileName, data)
- local file = fs.open(fileName, "w") -- Datei im Schreibmodus öffnen
- if file then
- file.write(textutils.serialize(data)) -- Tabelle in die Datei schreiben
- file.close()
- print("Daten gespeichert in:", fileName)
- else
- print("Fehler beim Speichern der Daten.")
- end
- end
- local function loadData(fileName)
- if fs.exists(fileName) then
- local file = fs.open(fileName, "r") -- Datei im Lesemodus öffnen
- if file then
- local content = file.readAll()
- file.close()
- return textutils.unserialize(content) -- String in Tabelle umwandeln
- end
- else
- print("Datei nicht gefunden:", fileName)
- end
- return nil -- Standardwert, wenn Datei nicht existiert
- end
- local function updateSaveData()
- local arrArea = {areaX, areaY}
- local dataToSave = {area = arrArea, pos = turtlePos, lastPos = turtleLastPos, vector = turtleVec, lastVector = turtleLastVec, mode = workMode, block = targetBlock}
- saveData(saveFilename, dataToSave)
- end
- local function forward()
- move.Forward()
- turtlePos[1] = turtlePos[1] + turtleVec[1]
- turtlePos[2] = turtlePos[2] + turtleVec[2]
- updateSaveData()
- end
- local function upward()
- move.Up()
- turtlePos[3] = turtlePos[3] + 1
- updateSaveData()
- end
- local function downward()
- move.Down()
- turtlePos[3] = turtlePos[3] - 1
- updateSaveData()
- end
- local function turnLeft()
- turtle.turnLeft()
- local buffer = turtleVec[1]
- turtleVec[1] = turtleVec[2]
- turtleVec[2] = -buffer
- updateSaveData()
- end
- local function turnRight()
- turtle.turnRight()
- local buffer = turtleVec[1]
- turtleVec[1] = -turtleVec[2]
- turtleVec[2] = buffer
- updateSaveData()
- end
- local function digGravel()
- while (not turtle.detectDown()) or inspect.Down(arrTargetBlocks) do
- downward()
- end
- end
- local function goForward()
- while (not inspect.Forward(arrTargetBlocks)) and turtle.detect() do
- upward()
- end
- forward()
- end
- local function varInArr(var, arr)
- for i = 1, #arr, 1 do
- if var == arr[i] then
- return true
- end
- end
- return false
- end
- local function selectItemFromList(itemList)
- local itemA = turtle.getItemDetail()
- if itemA then
- if varInArr(itemA.name, itemList) then
- return true
- end
- end
- for i=1, 16, 1 do
- if turtle.getItemCount(i) > 0 then
- turtle.select(i)
- local itemB = turtle.getItemDetail()
- if itemB then
- if varInArr(itemB.name, itemList) then
- return true
- end
- end
- end
- end
- return false
- end
- local function offload()
- local chestList = { "minecraft:chest" }
- local bChest = inspect.Forward(chestList)
- if not bChest then
- if selectItemFromList(chestList) then
- bChest = true
- while not turtle.place() do
- if not turtle.dig() then
- print("Can't place Block")
- bChest = false
- break
- end
- end
- end
- end
- if bChest then
- return dropAll.Forward()
- end
- return false
- end
- local function goToStart()
- if workMode == 1 then
- turtleLastPos = {turtlePos[1], turtlePos[2], turtlePos[3]}
- turtleLastVec = {turtleVec[1], turtleVec[2]}
- workMode = 2
- end
- while turtleVec[1] > -1 do
- turnRight()
- end
- if (turtlePos[1] + turtlePos[2] + math.abs(turtlePos[3])) > 0 then
- while turtlePos[1] > 0 do
- forward()
- end
- turnRight()
- while turtlePos[2] > 0 do
- forward()
- end
- turnLeft()
- while turtlePos[3] > 0 do
- downward()
- end
- while turtlePos[3] < 0 do
- upward()
- end
- end
- offload()
- end
- local function goToLastPosition()
- workMode = 3
- while turtlePos[3] < turtleLastPos[3] do
- upward()
- end
- while turtlePos[3] > turtleLastPos[3] do
- downward()
- end
- turnLeft()
- while turtlePos[2] < turtleLastPos[2] do
- forward()
- end
- turnLeft()
- while turtlePos[1] < turtleLastPos[1] do
- forward()
- end
- while not (turtleVec[1] == turtleLastVec[1] and turtleVec[2] == turtleLastVec[2]) do
- turnLeft()
- end
- workMode = 1
- end
- -- checks if turtle has enough fuel and refuels if not
- local function checkAndRefuel()
- local distance = turtlePos[1] + turtlePos[2] + math.abs(turtlePos[3])
- local multiplier = 1
- if distance == 0 then
- distance = turtleLastPos[1] + turtleLastPos[2] + math.abs(turtleLastPos[3])
- multiplier = 2
- end
- local fuelLevel = turtle.getFuelLevel()
- local fuelBuffer = 100
- if fuelLevel > multiplier * distance + fuelBuffer then
- return
- end
- goToStart()
- offload()
- multiplier = 2
- print("Pls add fuel to turtle")
- while true do
- io.read()
- move.refuelProcess()
- fuelLevel = turtle.getFuelLevel()
- if fuelLevel > 2 * distance + fuelBuffer then
- break
- else
- print("Not enough fuel was added, pls try again")
- end
- end
- goToLastPosition()
- end
- --checks if turtle has an empty slot and unloads if not
- local function checkAndUnload()
- for i=16, 1, -1 do
- if turtle.getItemCount(i) == 0 then
- return
- end
- end
- goToStart()
- print("Trying to offload")
- while not offload() do end
- checkAndRefuel()
- goToLastPosition()
- end
- if not areaX then
- areaX = 4
- end
- if not areaY then
- areaY = 5
- end
- local data = loadData(saveFilename)
- if data then
- areaX, areaY = tonumber(data.area[1]), tonumber(data.area[2])
- turtlePos = { tonumber(data.pos[1]), tonumber(data.pos[2]), tonumber(data.pos[3]) }
- turtleLastPos = { tonumber(data.lastPos[1]), tonumber(data.lastPos[2]), tonumber(data.lastPos[3]) }
- turtleVec = { tonumber(data.vector[1]), tonumber(data.vector[2]), tonumber(data.vector[3]) }
- turtleLastVec = { tonumber(data.lastVector[1]), tonumber(data.lastVector[2]), tonumber(data.lastVector[3]) }
- workMode = tonumber(data.mode)
- targetBlock = tonumber(data.block)
- end
- if targetBlock == 2 then
- arrTargetBlocks = {"minecraft:gravel"}
- elseif targetBlock == 3 then
- arrTargetBlocks = {"minecraft:sand"}
- end
- areaX = tonumber(areaX)
- areaY = tonumber(areaY)
- targetBlock = tonumber(targetBlock)
- updateSaveData()
- print("areaX: ", areaX, " areaY: ", areaY)
- print("pos: ", textutils.serialize(turtlePos))
- if workMode == 2 then
- goToStart()
- print("Trying to offload")
- while not offload() do end
- checkAndRefuel()
- goToLastPosition()
- end
- if workMode == 3 then
- goToLastPosition()
- end
- digGravel()
- while turtlePos[2] < areaY do
- if (turtlePos[2] % 2) == 0 then
- while turtlePos[1] < areaX - 1 do
- goForward()
- digGravel()
- checkAndRefuel()
- checkAndUnload()
- end
- if not (turtlePos[2] == areaY - 1) then
- turnRight()
- goForward()
- digGravel()
- turnRight()
- checkAndRefuel()
- checkAndUnload()
- else
- break
- end
- else
- while turtlePos[1] > 0 do
- goForward()
- digGravel()
- checkAndRefuel()
- checkAndUnload()
- end
- if not (turtlePos[2] == areaY - 1) then
- turnLeft()
- goForward()
- digGravel()
- turnLeft()
- checkAndRefuel()
- checkAndUnload()
- else
- break
- end
- end
- end
- goToStart()
- offload()
- -- delete file
- local command = "delete " .. saveFilename
- local success, message = shell.run(command)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement