Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- pastebin get bSBgdCDF StorageTurtle
- local TASK_FILE = "task_list.txt"
- local STATUS_FILE = "status.txt"
- TaskStatus = {
- getTask = 0,
- accepted = 1,
- inProgress = 2,
- finished = 3,
- completing = 4,
- joiningQueue = 5,
- waiting = 6,
- refuelNeeded = 7,
- refueling = 8,
- noTask = 9
- }
- local DISK_NAMES = {
- incoming = "Incoming Task",
- user = "User Task",
- outgoing = "Outgoing Task",
- finished = "Finished Task",
- settings = "Settings",
- storage = "Data Storage",
- init = "Turtle Initializing"
- }
- -- === Initializing Variables ===
- local orientation, direction, modulo, yModulo
- local finishCoord, outputCoord, refuelCoord
- local directionVectors
- local leftBeltCoord, rightBeltCoord
- local facingVec
- local turtlePos
- -- === Utility ===
- local function readInitData()
- local localPath = "init.txt"
- local initFile
- -- Prüfe, ob bereits eine lokale Init-Datei existiert
- if fs.exists(localPath) then
- initFile = fs.open(localPath, "r")
- else
- -- Wenn nicht vorhanden, von Init-Disk lesen
- local initDiskPath
- for _, side in ipairs(peripheral.getNames()) do
- if peripheral.getType(side) == "drive" then
- local disk = peripheral.wrap(side)
- if disk.getDiskLabel() == "Turtle Initializing" then
- initDiskPath = disk.getMountPath()
- break
- end
- end
- end
- if not initDiskPath then error("Init disk not found") end
- local diskFilePath = fs.combine(initDiskPath, "init.txt")
- if not fs.exists(diskFilePath) then
- error("init.txt not found on disk")
- end
- -- Lese Daten von Disk
- initFile = fs.open(diskFilePath, "r")
- -- Speichere eine Kopie lokal
- local diskData = initFile.readAll()
- initFile.close()
- initFile = fs.open(localPath, "w")
- initFile.write(diskData)
- initFile.close()
- -- Öffne neu zum Parsen
- initFile = fs.open(localPath, "r")
- end
- -- Parse Inhalte
- local orientation = initFile.readLine():match("orientation=(%a+)")
- local direction = tonumber(initFile.readLine():match("direction=(%-?%d+)"))
- local modulo = tonumber(initFile.readLine():match("modulo=(%d+)"))
- local yModulo = tonumber(initFile.readLine():match("yModulo=(%d+)"))
- local x1 = tonumber(initFile.readLine():match("finish_x=(%-?%d+)"))
- local y1 = tonumber(initFile.readLine():match("finish_y=(%-?%d+)"))
- local z1 = tonumber(initFile.readLine():match("finish_z=(%-?%d+)"))
- local x2 = tonumber(initFile.readLine():match("output_x=(%-?%d+)"))
- local y2 = tonumber(initFile.readLine():match("output_y=(%-?%d+)"))
- local z2 = tonumber(initFile.readLine():match("output_z=(%-?%d+)"))
- local x3 = tonumber(initFile.readLine():match("refuel_x=(%-?%d+)"))
- local y3 = tonumber(initFile.readLine():match("refuel_y=(%-?%d+)"))
- local z3 = tonumber(initFile.readLine():match("refuel_z=(%-?%d+)"))
- local finishPos = {x = x1, y = y1, z = z1}
- local outputPos = {x = x2, y = y2, z = z2}
- local refuelPos = {x = x3, y = y3, z = z3}
- initFile.close()
- return orientation, direction, modulo, yModulo, finishPos, outputPos, refuelPos
- end
- local function getDiskByLabel(label)
- for _, side in ipairs(peripheral.getNames()) do
- if peripheral.getType(side) == "drive" then
- local disk = peripheral.wrap(side)
- if disk.getDiskLabel() == label then
- return disk, disk.getMountPath()
- end
- end
- end
- return nil, nil
- end
- local function isDiskOnSide(label, turtleSide)
- for _, side in ipairs(peripheral.getNames()) do
- if side == turtleSide then
- if peripheral.getType(side) == "drive" then
- local disk = peripheral.wrap(side)
- if disk.getDiskLabel() == label then
- return true
- end
- end
- end
- end
- return false
- end
- local function getDirectionVectors(orientation, direction)
- if orientation == "x" then
- return {
- forward = {x = direction, y = 0, z = 0},
- backward = {x = -direction, y = 0, z = 0},
- right = {x = 0, y = 0, z = direction},
- left = {x = 0, y = 0, z = -direction},
- up = {x = 0, y = 1, z = 0},
- down = {x = 0, y = -1, z = 0}
- }
- elseif orientation == "z" then
- return {
- forward = {x = 0, y = 0, z = direction},
- backward = {x = 0, y = 0, z = -direction},
- right = {x = -direction, y = 0, z = 0},
- left = {x = direction, y = 0, z = 0},
- up = {x = 0, y = 1, z = 0},
- down = {x = 0, y = -1, z = 0}
- }
- else
- error("Invalid orientation")
- end
- end
- local function get_GPS_Position()
- local x, y, z = gps.locate()
- if not x then print("Error: GPS not available") return nil end
- return {x = math.floor(x), y = math.floor(y), z = math.floor(z)}
- end
- local function add(pos, delta)
- return {
- x = pos.x + delta.x,
- y = pos.y + delta.y,
- z = pos.z + delta.z
- }
- end
- local function equalCoords(coord1, coord2)
- return coord1.x == coord2.x and coord1.y == coord2.y and coord1.z == coord2.z
- end
- local function substract(pos, delta)
- return {
- x = pos.x - delta.x,
- y = pos.y - delta.y,
- z = pos.z - delta.z
- }
- end
- local function getLeftBeltCoordinate(endBlockCoord, dir, orient)
- local leftBeltCoord = add(endBlockCoord, dir)
- if orient == "x" then
- leftBeltCoord.z = 0
- else
- leftBeltCoord.x = 0
- end
- return leftBeltCoord
- end
- local function isTurtleBlock(data)
- return data and type(data.name) == "string" and data.name:match("computercraft:turtle")
- end
- -- === Task Readings + Writings ===
- local function getTaskList(path)
- if not fs.exists(path) then return {} end
- local file = fs.open(path, "r")
- local data = textutils.unserialize(file.readAll())
- file.close()
- return data or {}
- end
- local function saveTaskList(path, taskList)
- local file = fs.open(path, "w")
- file.write(textutils.serialize(taskList))
- file.close()
- end
- local function addTaskToList(path, task)
- local tasks = getTaskList(path)
- table.insert(tasks, task)
- saveTaskList(path, tasks)
- end
- local function load_status()
- local path = STATUS_FILE
- local file
- if fs.exists(path) then
- file = fs.open(path, "r")
- else
- file = fs.open(path, "w")
- file.writeLine("status=" .. tostring(TaskStatus.noTask))
- return TaskStatus.noTask
- end
- local status = tonumber(file.readLine():match("status=(%-?%d+)"))
- file.close()
- return status
- end
- local function save_status(status)
- local path = STATUS_FILE
- local file = fs.open(path, "w")
- file.writeLine("status=" .. tostring(status))
- file.close()
- end
- -- === Facing and Rotation Management ===
- local function turnInDirection(dir)
- if not facingVec then return end
- if dir == "left" then
- turtle.turnLeft()
- facingVec = {x = facingVec.z, y = 0, z = -facingVec.x}
- elseif dir == "right" then
- turtle.turnRight()
- facingVec = {x = -facingVec.z, y = 0, z = facingVec.x}
- elseif dir == "around" then
- turtle.turnLeft()
- turtle.turnLeft()
- facingVec = {x = -facingVec.x, y = 0, z = -facingVec.z}
- end
- end
- local function getTurnDirection(from, to)
- local function vectorsEqual(a, b)
- return a.x == b.x and a.z == b.z
- end
- if vectorsEqual(from, to) then return nil end
- local left = {x = from.z, y = 0, z = -from.x}
- local right = {x = -from.z, y = 0, z = from.x}
- local back = {x = -from.x, y = 0, z = -from.z}
- if vectorsEqual(to, left) then
- return "left"
- elseif vectorsEqual(to, right) then
- return "right"
- elseif vectorsEqual(to, back) then
- return "around"
- else
- error("Invalid target direction")
- end
- end
- local function turnAbsolutDirection(dir)
- if not facingVec then return end
- local turnDirection = getTurnDirection(facingVec, dir)
- if turnDirection then
- turnInDirection(turnDirection)
- end
- end
- -- === Basic Movement ===
- local function moveUp()
- local bMove = turtle.up()
- turtlePos = get_GPS_Position()
- return bMove
- end
- local function moveDown()
- local bMove = turtle.down()
- turtlePos = get_GPS_Position()
- return bMove
- end
- local function moveForward()
- if not turtlePos then
- turtlePos = get_GPS_Position()
- end
- local bMove = turtle.forward()
- local pos = get_GPS_Position()
- facingVec = substract(pos, turtlePos)
- turtlePos = pos
- return bMove
- end
- --- === Coords Utillity ===
- -- gets value on axis relativ to orientation (LR = left right, FB = forward backward)
- local function getOrientatedAxisValues(coord)
- local axis_LR = 0
- local axis_FB = 0
- if orientation == "x" then
- axis_LR = coord.z
- axis_FB = coord.x
- else
- axis_LR = coord.x
- axis_FB = coord.z
- end
- return axis_LR, axis_FB
- end
- -- === check Bounds ===
- -- checked if turtle is in bounds of storage system or infront of it
- local function isCoordInBounds(coord)
- --print("Coord: (" .. tostring(coord.x) .. ", " .. tostring(coord.y) .. ", " .. tostring(coord.z) .. ")")
- if orientation == "x" then
- local beltDistance = leftBeltCoord.x - coord.x
- print("target_x: " .. tostring(coord.x) .. " belt_x: " .. tostring(leftBeltCoord.x))
- print(beltDistance)
- if beltDistance == 0 then return true end
- if beltDistance == math.abs(beltDistance) * directionVectors.forward.x then
- return false
- end
- else
- local beltDistance = leftBeltCoord.z - coord.z
- print("target_z: " .. tostring(coord.z) .. " belt_x: " .. tostring(leftBeltCoord.z))
- print(beltDistance)
- if beltDistance == 0 then return true end
- if beltDistance == math.abs(beltDistance) * directionVectors.forward.z then
- return false
- end
- end
- return true
- end
- -- check if turtle is on belt (either left or right)
- local function isCoordOnBelt(coord)
- local coord_LR, coord_FB = getOrientatedAxisValues(coord)
- if not (coord_FB == leftBeltCoord.x + leftBeltCoord.z) then
- print("LR: " .. tostring(coord_FB))
- print("Add: " .. tostring(leftBeltCoord.x + leftBeltCoord.z))
- return false
- end
- if not (coord.y == rightBeltCoord.y or coord.y == leftBeltCoord.y) then
- print("LR_Y: " .. tostring(coord_FB))
- print("BeltHeight: " .. tostring(rightBeltCoord.y) .. ", " .. tostring(leftBeltCoord.y))
- return false
- end
- return true
- end
- -- check if coord is in lift up or lift down
- local function isCoordOnLift(coord)
- local added = add(leftBeltCoord, directionVectors.forward)
- local add_LR, add_FB = getOrientatedAxisValues(added)
- local c_LR, c_FB = getOrientatedAxisValues(coord)
- if not (c_FB == add_FB) then
- return false
- end
- if ((c_LR + 1) % 8 == modulo) or ((c_LR - 1) % 8 == modulo) then
- return true
- end
- return false
- end
- -- check if coord is on a section
- local function isCoordOnSection(coord)
- local coord_LR, coord_FB = getOrientatedAxisValues(coord)
- if (coord_LR % 8 == modulo and coord.y % 7 == yModulo and isCoordInBounds(coord)) then
- return true
- end
- return false
- end
- -- check if coord is on the side of a vertical chest row
- local function isCoordOnChestRow(coord)
- print("Checking for row")
- local c_LR, c_FB = getOrientatedAxisValues(coord)
- local belt_LR, belt_FB = getOrientatedAxisValues(leftBeltCoord)
- if not (((c_LR + 1) % 8 == modulo) or ((c_LR - 1) % 8 == modulo)) then
- return false
- end
- if isCoordInBounds(coord) and (math.abs(c_FB - belt_FB) > 1) then
- return true
- end
- return false
- end
- -- check if coord is on an exit section
- local function isCoordOnExitSection(coord)
- local coord_LR, coord_FB = getOrientatedAxisValues(coord)
- local exitModulo = (yModulo - 1 > 0) and (yModulo - 1) or (yModulo - 1 + 7)
- if (coord_LR % 8 == modulo and coord.y % 7 == exitModulo and isCoordInBounds(coord)) then
- return true
- end
- return false
- end
- -- check if coord can be reached in the storage system
- local function isConnected(coord)
- if not isCoordInBounds(coord) then return true end
- if isCoordOnBelt(coord) then return true end
- if isCoordOnLift(coord) then return true end
- if isCoordOnSection(coord) then return true end
- if isCoordOnChestRow(coord) then return true end
- if isCoordOnExitSection(coord) then return true end
- return false
- end
- -- determines direction of turtle and moves one block if necessary
- local function getFacingValue(turtleState)
- if not facingVec then
- local downSuccess, downData = turtle.inspectDown()
- -- turn till facing input task disk drive (on backward side of block where turtle receives task)
- if downSuccess and downData.name == "computercraft:disk_drive" then
- local cnt = 0
- while cnt < 4 do
- local frontSuccess, frontData = turtle.inspect()
- if frontSuccess and frontData.name == "computercraft:disk_drive" then
- facingVec = directionVectors.backward
- return true
- end
- turtle.turnRight()
- cnt = cnt + 1
- end
- end
- if not moveForward() then
- -- get correct Facing from joining queue
- local level = 0
- if turtleState == TaskStatus.joiningQueue then
- if turtlePos.x == finishCoord.x and turtlePos.z == finishCoord.z then
- level = turtlePos.y - finishCoord.y
- while true do
- local success, data = turtle.inspect()
- if not (success and isTurtleBlock(data)) then
- while not moveForward() do sleep(0.5) end
- local addedCoord = add(finishCoord, directionVectors.backward)
- if turtlePos.x == addedCoord.x and turtlePos.z == addedCoord.z then
- print("Set waiting status")
- read()
- turtleState = TaskStatus.waiting
- save_status(TaskStatus.waiting)
- break
- else
- print("Set no Task status")
- save_status(TaskStatus.noTask)
- return true
- end
- else
- while not moveUp() do sleep(0.5) end
- level = level + 1
- end
- end
- else
- turtleState = TaskStatus.noTask
- save_status(TaskStatus.noTask)
- end
- end
- -- get correct position in queue (only when queue is not walled)
- if turtleState == TaskStatus.waiting then
- local posQueueEntry = add(finishCoord, directionVectors.backward)
- local cnt = 0
- while cnt < 4 do
- local success, data = turtle.inspect()
- if success and (not data.name:match("computercraft:")) then
- local level = turtlePos.y - finishCoord.y
- if turtlePos.x == posQueueEntry.x and turtlePos.z == posQueueEntry.z then
- if level % 2 == 0 then
- turtle.turnRight()
- turtle.turnRight()
- end
- else
- if level % 2 == 1 then
- turtle.turnRight()
- turtle.turnRight()
- end
- end
- return true
- end
- turtle.turnRight()
- cnt = cnt + 1
- end
- return true
- end
- local turtle_LR_coord, turtle_FB_coord = getOrientatedAxisValues(turtlePos)
- local LR_axis, FB_axis = getOrientatedAxisValues(directionVectors.right)
- -- check if turtle is infront of storage chest
- if (turtle_LR_coord + LR_axis) % 8 == modulo then
- local success, data = turtle.inspect()
- if success and data.name == "minecraft:chest" then
- facingVec = directionVectors.left
- return true
- end
- elseif (turtle_LR_coord - LR_axis) % 8 == modulo then
- local success, data = turtle.inspect()
- if success and data.name == "minecraft:chest" then
- facingVec = directionVectors.right
- return true
- end
- end
- -- turns 360° and trys to move to get directional vector
- local cnt = 0
- local bForward = false
- while not bForward and cnt < 4 do
- turtle.turnLeft()
- bForward = moveForward()
- cnt = cnt + 1
- end
- if not bForward then
- return false
- else
- if not isConnected(turtlePos) then
- turnInDirection("around")
- while not moveForward() do sleep(0.5) end
- end
- end
- else
- if turtleState == TaskStatus.joiningQueue then
- local addedCoord = add(finishCoord, directionVectors.backward)
- if not (turtlePos.x == addedCoord.x and turtlePos.z == addedCoord.z) then
- turtleState = TaskStatus.noTask
- save_status(TaskStatus.noTask)
- end
- end
- if not isConnected(turtlePos) then
- turnInDirection("around")
- while not moveForward() do sleep(0.5) end
- end
- end
- end
- return true
- end
- -- === Inventory Management ===
- -- gets specified item in given amount from chest
- local function suckSpecificItem(itemName, count, strDir)
- local chest = peripheral.wrap(strDir)
- if not chest or not chest.list then
- return false
- end
- local chestItems = chest.list()
- local foundSlots = {}
- local totalAvailable = 0
- -- 1. Scanne Kiste nach dem gewünschten Item
- for slot, item in pairs(chestItems) do
- if item.name == itemName then
- table.insert(foundSlots, {slot = slot, count = item.count})
- totalAvailable = totalAvailable + item.count
- end
- end
- if totalAvailable < count then
- return false
- end
- -- 2. Finde freien Slot in der Turtle für gecachtes Item
- local cachedItemSlot = nil
- for i = 1, 16 do
- if turtle.getItemCount(i) == 0 then
- cachedItemSlot = i
- break
- end
- end
- if not cachedItemSlot then
- print("Kein freier Slot zum Zwischenspeichern des Kisten-Items.")
- return false
- end
- turtle.select(cachedItemSlot)
- local cachedItem = nil
- local list = chest.list()
- if list[1] and list[1].name ~= itemName then
- if strDir == "front" then
- if turtle.suck(64) then
- cachedItem = turtle.getItemDetail(cachedItemSlot)
- end
- elseif strDir == "top" then
- if turtle.suckUp(64) then
- cachedItem = turtle.getItemDetail(cachedItemSlot)
- end
- elseif strDir == "bottom" then
- if turtle.suckDown(64) then
- cachedItem = turtle.getItemDetail(cachedItemSlot)
- end
- end
- end
- -- 3. Erstes gewünschtes Item in Slot 1 schieben
- local nextSlotIndex = 1
- if not chest.getItemDetail(1) then
- local nextSource = foundSlots[nextSlotIndex]
- chest.pushItems(strDir, nextSource.slot, nextSource.count, 1)
- nextSlotIndex = nextSlotIndex + 1
- elseif (chest.getItemDetail(1) and chest.getItemDetail(1).name == itemName) then
- nextSlotIndex = nextSlotIndex + 1
- end
- -- 4. Gecachtes Item zurückgeben
- if cachedItem then
- turtle.select(cachedItemSlot)
- if strDir == "front" then
- turtle.drop() -- Kiste verteilt es automatisch auf einen freien Slot ≠ 1
- elseif strDir == "top" then
- turtle.dropUp() -- Kiste verteilt es automatisch auf einen freien Slot ≠ 1
- elseif strDir == "bottom" then
- turtle.dropDown() -- Kiste verteilt es automatisch auf einen freien Slot ≠ 1
- end
- end
- -- 5. Zielitems absaugen
- local collected = 0
- turtle.select(cachedItemSlot) -- Wiederverwenden für Einsammeln
- while collected < count do
- local remaining = count - collected
- local slot1Item = chest.getItemDetail(1)
- local pullCount = math.min(slot1Item.count, remaining)
- if strDir == "front" then
- if turtle.suck(pullCount) then
- collected = collected + pullCount
- end
- elseif strDir == "top" then
- if turtle.suckUp(pullCount) then
- collected = collected + pullCount
- end
- elseif strDir == "bottom" then
- if turtle.suckDown(pullCount) then
- collected = collected + pullCount
- end
- end
- if collected < count and nextSlotIndex <= #foundSlots then
- local nextSource = foundSlots[nextSlotIndex]
- chest.pushItems(strDir, nextSource.slot, nextSource.count, 1)
- nextSlotIndex = nextSlotIndex + 1
- end
- end
- return true
- end
- -- selects slot with item in turtle inventory
- local function selectItem(itemName)
- for i = 1, 16 do
- if turtle.getItemCount(i) > 0 then
- local data = turtle.getItemDetail(i)
- if data and data.name == itemName then
- turtle.select(i)
- return true
- end
- end
- end
- return false
- end
- -- drops of inventory, if specified only items with itemName
- local function dropOffInventory(itemName)
- for i = 1, 16 do
- if turtle.getItemCount(i) > 0 then
- if itemName then
- local data = turtle.getItemDetail(i)
- if data.name == itemName then
- turtle.select(i)
- turtle.drop()
- end
- else
- turtle.select(i)
- turtle.drop()
- end
- end
- end
- end
- -- === System Movement ===
- -- returns LR Position of Section in which the target is located
- local function findSectionPos_LR(targetPos)
- local target_LR_coord, target_FB_coord = getOrientatedAxisValues(targetPos)
- local targetModulo = target_LR_coord % 8
- local distanceToSection = targetModulo - modulo
- if distanceToSection > 4 then
- distanceToSection = distanceToSection - 8
- elseif distanceToSection < -4 then
- distanceToSection = distanceToSection + 8
- elseif math.abs(distanceToSection) == 4 then
- print("Error: Targetposition out of bounds")
- return nil
- end
- local try_LR_coord1 = math.abs(distanceToSection) + target_LR_coord
- local try_LR_coord2 = -math.abs(distanceToSection) + target_LR_coord
- if try_LR_coord1 % 8 == modulo then
- return try_LR_coord1
- elseif try_LR_coord2 % 8 == modulo then
- return try_LR_coord2
- else
- print("Error: Can't find correct Section")
- return nil
- end
- end
- local function getSectionHeight(targetPos)
- local target_yModulo = targetPos.y % 7
- local difference = target_yModulo - yModulo
- if difference < 0 then
- difference = difference + 7
- end
- return targetPos.y - difference
- end
- -- returns LR Position of belt exit + direction it needs to face there
- local function getBeltExitLocation(targetPos)
- local target_LR_coord, target_FB_coord = getOrientatedAxisValues(targetPos)
- if isCoordInBounds(targetPos) then
- local exitCoord = findSectionPos_LR(targetPos)
- if exitCoord then
- return exitCoord, directionVectors.forward
- else
- print("Error: No exit Coords found")
- return nil, nil
- end
- else
- return target_LR_coord, directionVectors.backward
- end
- end
- -- moves from left belt to right belt or the other way around
- local function switchBelts()
- local turtle_LR_coord, turtle_FB_coord = getOrientatedAxisValues(turtlePos)
- local dir_Right, otherDir = getOrientatedAxisValues(directionVectors.right)
- local dir_Left = -dir_Right
- if turtlePos.y == rightBeltCoord.y then
- -- moves to elevator left of section way to go down
- if not ((turtle_LR_coord + dir_Right) % 8 == modulo) then
- turnAbsolutDirection(directionVectors.right)
- end
- while not ((turtle_LR_coord + dir_Right) % 8 == modulo) do
- while not moveForward() do sleep(0.5) end
- turtle_LR_coord, turtle_FB_coord = getOrientatedAxisValues(turtlePos)
- end
- turnAbsolutDirection(directionVectors.forward)
- while not moveForward() do sleep(0.5) end
- while not moveDown() do sleep(0.5) end
- turnAbsolutDirection(directionVectors.backward)
- while not moveForward() do sleep(0.5) end
- turnAbsolutDirection(directionVectors.left)
- else
- -- moves to elevator right of section way to go up
- if not ((turtle_LR_coord + dir_Left) % 8 == modulo) then
- turnAbsolutDirection(directionVectors.left)
- end
- while not ((turtle_LR_coord + dir_Left) % 8 == modulo) do
- while not moveForward() do sleep(0.5) end
- turtle_LR_coord, turtle_FB_coord = getOrientatedAxisValues(turtlePos)
- end
- turnAbsolutDirection(directionVectors.forward)
- while not moveForward() do sleep(0.5) end
- while not moveUp() do sleep(0.5) end
- turnAbsolutDirection(directionVectors.backward)
- while not moveForward() do sleep(0.5) end
- turnAbsolutDirection(directionVectors.right)
- end
- end
- -- moves from lift down to lift up or the other way around
- local function switchLifts()
- print("Switch lift")
- local bLiftDown = false
- local pos_section_LR, pos_section_FB = getOrientatedAxisValues(add(turtlePos, directionVectors.right))
- local taboo1 = yModulo - 1
- local taboo2 = yModulo - 1 + 7
- local tabooCondition = (turtlePos.y % 7 == yModulo) or (turtlePos.y % 7 == taboo1) or (turtlePos.y % 7 == taboo2)
- local condition2 = (turtlePos.y % 2 == 0)
- if pos_section_LR % 8 == modulo then
- bLiftDown = true
- end
- if bLiftDown then
- while condition2 or tabooCondition do
- while not moveDown() do sleep(0.5) end
- condition2 = (turtlePos.y % 2 == 0)
- tabooCondition = (turtlePos.y % 7 == yModulo) or (turtlePos.y % 7 == taboo1) or (turtlePos.y % 7 == taboo2)
- end
- turnAbsolutDirection(directionVectors.right)
- while not moveForward() do sleep(0.5) end
- while not moveForward() do sleep(0.5) end
- else
- while (not condition2) or tabooCondition do
- while not moveUp() do sleep(0.5) end
- condition2 = (turtlePos.y % 2 == 0)
- tabooCondition = (turtlePos.y % 7 == yModulo) or (turtlePos.y % 7 == taboo1) or (turtlePos.y % 7 == taboo2)
- end
- turnAbsolutDirection(directionVectors.left)
- while not moveForward() do sleep(0.5) end
- while not moveForward() do sleep(0.5) end
- end
- end
- -- adapts position to join closest belt, switches to other one if necessary
- local function joinBeltFromOutside(targetPos)
- if isCoordInBounds(turtlePos) then
- print("Error: Turtle is already in bounds")
- return false
- end
- local turtle_LR_coord, turtle_FB_coord = getOrientatedAxisValues(turtlePos)
- local target_LR_coord, target_FB_coord = getOrientatedAxisValues(targetPos)
- local beltHeight = leftBeltCoord.y
- local bRightBelt = false
- if orientation == "x" then
- if (target_LR_coord - turtle_LR_coord) / directionVectors.right.z > 0 then
- beltHeight = rightBeltCoord.y
- bRightBelt = true
- end
- elseif orientation == "z" then
- if (target_LR_coord - turtle_LR_coord) / directionVectors.right.x > 0 then
- beltHeight = rightBeltCoord.y
- bRightBelt = true
- end
- end
- turnAbsolutDirection(directionVectors.forward)
- if turtlePos.y > leftBeltCoord.y then
- while turtlePos.y > beltHeight do
- if not moveDown() then sleep(0.5) end
- end
- else
- while turtlePos.y < beltHeight do
- if not moveUp() then sleep(0.5) end
- end
- end
- while not isCoordInBounds(turtlePos) do
- if not moveForward() then sleep(0.5) end
- end
- if bRightBelt then
- turnAbsolutDirection(directionVectors.right)
- else
- turnAbsolutDirection(directionVectors.left)
- end
- return true
- end
- -- moves along the belt til exit + exiting it
- local function useAndExitBelt(targetPos)
- local turtle_LR_coord, turtle_FB_coord = getOrientatedAxisValues(turtlePos)
- local target_LR_coord, target_FB_coord = getOrientatedAxisValues(targetPos)
- if not isCoordOnBelt(turtlePos) then
- print("Error: Turtle is not on Belt.")
- return false
- end
- local beltHeight = leftBeltCoord.y
- local bGoingRight = false
- if orientation == "x" then
- if (target_LR_coord - turtle_LR_coord) / directionVectors.right.z > 0 then
- beltHeight = rightBeltCoord.y
- bGoingRight = true
- end
- elseif orientation == "z" then
- if (target_LR_coord - turtle_LR_coord) / directionVectors.right.x > 0 then
- beltHeight = rightBeltCoord.y
- bGoingRight = true
- end
- end
- if not (turtlePos.y == beltHeight) then
- switchBelts()
- end
- if bGoingRight then
- turnAbsolutDirection(directionVectors.right)
- else
- turnAbsolutDirection(directionVectors.left)
- end
- local exit_LR, exitVec = getBeltExitLocation(targetPos)
- if not exit_LR then
- print("Error: Can't find exit.")
- return false
- end
- while not (turtle_LR_coord == exit_LR) do
- while not moveForward() do sleep(0.5) end
- turtle_LR_coord, turtle_FB_coord = getOrientatedAxisValues(turtlePos)
- end
- if equalCoords(targetPos, finishCoord) then
- if turtlePos.y == leftBeltCoord.y + 1 then
- switchBelts()
- useAndExitBelt(targetPos)
- return true
- end
- end
- turnAbsolutDirection(exitVec)
- while not moveForward() do sleep(0.5) end
- return true
- end
- -- moves along lift and exits it. Turns in right direction afterward
- local function useAndExitLift(targetPos)
- local bExitToBelt = false
- local bTargetInBounds = isCoordInBounds(targetPos)
- local bLiftDown = false
- local pos_section_LR, otherValue = getOrientatedAxisValues(add(turtlePos, directionVectors.right))
- local section_LR, otherValue1 = getOrientatedAxisValues(add(turtlePos, directionVectors.left))
- if pos_section_LR % 8 == modulo then
- section_LR = pos_section_LR
- bLiftDown = true
- end
- if not isCoordOnLift(turtlePos) then
- print("Error: Turtle is not on Lift.")
- return false
- end
- if not (findSectionPos_LR(targetPos) == section_LR) then
- print("Exit to belt")
- bExitToBelt = true
- end
- if not bTargetInBounds then
- bExitToBelt = true
- end
- if not bExitToBelt then
- -- exit to storage section
- local sectionHeight = getSectionHeight(targetPos)
- while sectionHeight > turtlePos.y do
- if bLiftDown then
- switchLifts()
- bLiftDown = false
- else
- while not moveUp() do sleep(0.5) end
- end
- end
- while sectionHeight < turtlePos.y do
- if not bLiftDown then
- switchLifts()
- bLiftDown = true
- else
- while not moveDown() do sleep(0.5) end
- end
- end
- if bLiftDown then
- turnAbsolutDirection(directionVectors.right)
- else
- turnAbsolutDirection(directionVectors.left)
- end
- while not moveForward() do sleep(0.5) end
- turnAbsolutDirection(directionVectors.forward)
- else
- -- exit to belt
- local exitHeight = leftBeltCoord.y
- local turtle_LR_coord, turtle_FB_coord = getOrientatedAxisValues(turtlePos)
- local target_LR_coord, target_FB_coord = getOrientatedAxisValues(targetPos)
- local step_right_LR, step_right_FB = getOrientatedAxisValues(directionVectors.right)
- local distance = target_LR_coord - turtle_LR_coord
- if distance == step_right_LR * math.abs(distance) then
- exitHeight = leftBeltCoord.y + 1
- end
- while not (exitHeight == turtlePos.y) do
- while exitHeight > turtlePos.y do
- if bLiftDown then
- switchLifts()
- bLiftDown = false
- -- calculates new direction and targetBelt again in case the direction changed
- distance = target_LR_coord - turtle_LR_coord
- if distance == step_right_LR * math.abs(distance) then
- exitHeight = leftBeltCoord.y + 1
- end
- else
- while not moveUp() do sleep(0.5) end
- end
- end
- while exitHeight < turtlePos.y do
- if not bLiftDown then
- switchLifts()
- bLiftDown = true
- -- calculates new direction and targetBelt again in case the direction changed
- distance = target_LR_coord - turtle_LR_coord
- if distance == step_right_LR * math.abs(distance) then
- exitHeight = leftBeltCoord.y + 1
- end
- else
- while not moveDown() do sleep(0.5) end
- end
- end
- end
- turnAbsolutDirection(directionVectors.backward)
- while not moveForward() do sleep(0.5) end
- if turtlePos.y == beltHeight1 then
- turnAbsolutDirection(directionVectors.left)
- elseif turtlePos.y == beltHeight2 then
- turnAbsolutDirection(directionVectors.right)
- end
- end
- end
- -- moves along section and exits at targetPos if possible otherwise exits directly.
- local function useAndExitSection(targetPos)
- local turtle_LR_coord, turtle_FB_coord = getOrientatedAxisValues(turtlePos)
- local target_LR_coord, target_FB_coord = getOrientatedAxisValues(targetPos)
- local step_forward_LR, step_forward_FB = getOrientatedAxisValues(directionVectors.forward)
- local step_right_LR, step_right_FB = getOrientatedAxisValues(directionVectors.right)
- local belt_LR, belt_FB = getOrientatedAxisValues(leftBeltCoord)
- if not isCoordOnSection(turtlePos) then
- print("Error: Turtle is not on Section.")
- return false
- end
- turnAbsolutDirection(directionVectors.forward)
- local difference = target_FB_coord - turtle_FB_coord
- -- exits belt at closest point to the left
- local function takeNextLeft()
- while math.abs(turtle_FB_coord - belt_FB) < 2 do
- while not moveForward() do sleep(0.5) end
- turtle_LR_coord, turtle_FB_coord = getOrientatedAxisValues(turtlePos)
- end
- turnAbsolutDirection(directionVectors.left)
- while not moveForward() do sleep(0.5) end
- end
- if not (difference == step_forward_FB * math.abs(difference)) then
- takeNextLeft()
- return true
- elseif not(getSectionHeight(targetPos) == turtlePos.y) then
- takeNextLeft()
- return true
- elseif math.abs(target_LR_coord - turtle_LR_coord) > 1 then
- takeNextLeft()
- return true
- end
- while not (target_FB_coord == turtle_FB_coord) do
- while not moveForward() do sleep(0.5) end
- turtle_LR_coord, turtle_FB_coord = getOrientatedAxisValues(turtlePos)
- end
- if turtle_LR_coord + step_right_LR == target_LR_coord then
- turnAbsolutDirection(directionVectors.right)
- elseif turtle_LR_coord - step_right_LR == target_LR_coord then
- turnAbsolutDirection(directionVectors.left)
- end
- while not moveForward() do sleep(0.5) end
- end
- -- climbs chest row up till target or exit
- local function useAndExitChestRow(targetPos)
- local turtle_LR_coord, turtle_FB_coord = getOrientatedAxisValues(turtlePos)
- local step_right_LR, step_right_FB = getOrientatedAxisValues(directionVectors.right)
- local exitModulo = (yModulo - 1 > 0) and (yModulo - 1) or (yModulo - 1 + 7)
- if not isCoordOnChestRow(turtlePos) then
- print("Error: Turtle is not on chest row.")
- return false
- end
- while not (turtlePos.y % 7 == exitModulo) do
- while not moveUp() do sleep(0.5) end
- if equalCoords(turtlePos, targetPos) then
- return true
- end
- end
- if (turtle_LR_coord + step_right_LR) % 8 == modulo then
- turnAbsolutDirection(directionVectors.right)
- elseif (turtle_LR_coord - step_right_LR) % 8 == modulo then
- turnAbsolutDirection(directionVectors.left)
- else
- print("Error: Turtle on wrong Position")
- return false
- end
- while not moveForward() do sleep(0.5) end
- turnAbsolutDirection(directionVectors.backward)
- return true
- end
- -- Moves till the end of the exit section and enters lift
- local function useAndExitExitSection(targetPos)
- local turtle_LR_coord, turtle_FB_coord = getOrientatedAxisValues(turtlePos)
- local target_LR_coord, target_FB_coord = getOrientatedAxisValues(targetPos)
- local step_right_LR, step_right_FB = getOrientatedAxisValues(directionVectors.right)
- local belt_LR_coord, belt_FB_coord = getOrientatedAxisValues(leftBeltCoord)
- if not isCoordOnExitSection(turtlePos) then
- print("Error: Turtle is not on exit section.")
- return false
- end
- turnAbsolutDirection(directionVectors.backward)
- while math.abs(turtle_FB_coord - belt_FB_coord) > 1 do
- while not moveForward() do sleep(0.5) end
- turtle_LR_coord, turtle_FB_coord = getOrientatedAxisValues(turtlePos)
- end
- local target_section_LR = findSectionPos_LR(targetPos)
- local exitLiftHeight = leftBeltCoord.y
- if target_section_LR == turtle_LR_coord then
- exitLiftHeight = getSectionHeight(targetPos)
- end
- local distance = target_LR_coord - turtle_LR_coord
- if distance == step_right_LR * math.abs(distance) then
- exitLiftHeight = leftBeltCoord.y + 1
- elseif distance == -step_right_LR * math.abs(distance) then
- exitLiftHeight = leftBeltCoord.y
- elseif distance == 0 then
- if turtlePos.y > leftBeltCoord.y + 1 then
- exitLiftHeight = leftBeltCoord.y + 1
- elseif turtlePos.y < leftBeltCoord.y then
- exitLiftHeight = leftBeltCoord.y
- else
- while not moveForward() do sleep(0.5) end
- if turtlePos.y == leftBeltCoord.y then
- turnAbsolutDirection(directionVectors.left)
- else
- turnAbsolutDirection(directionVectors.right)
- end
- return true
- end
- end
- if turtlePos.y >= exitLiftHeight then
- turnAbsolutDirection(directionVectors.left)
- while not moveForward() do sleep(0.5) end
- return true
- else
- turnAbsolutDirection(directionVectors.right)
- while not moveForward() do sleep(0.5) end
- return true
- end
- end
- -- reaches target from beltexit if it is out of bounds
- local function reachOutsideTarget(targetPos)
- local turtle_LR_coord, turtle_FB_coord = getOrientatedAxisValues(turtlePos)
- local target_LR_coord, target_FB_coord = getOrientatedAxisValues(targetPos)
- while not (turtle_FB_coord == target_FB_coord) do
- while not moveForward() do sleep(0.5) end
- turtle_LR_coord, turtle_FB_coord = getOrientatedAxisValues(turtlePos)
- end
- while turtlePos.y > targetPos.y do
- while not moveDown() do sleep(0.5) end
- end
- while turtlePos.y < targetPos.y do
- while not moveUp() do sleep(0.5) end
- end
- return true
- end
- -- === Turtle States ===
- -- Returns the coords where task starts
- local function getTaskStartCoords()
- local tasks = getTaskList("task_list.txt")
- if #tasks == 0 then
- return nil
- end
- local task = table.remove(tasks, 1)
- if task.taskType == "create_chests" then
- local taskPos = {x = task.x, y = task.y, z = task.z}
- local taskPos_LR, taskPos_FB = getOrientatedAxisValues(taskPos)
- local right_LR, right_FB = getOrientatedAxisValues(directionVectors.right)
- if (taskPos_LR + 2 * right_LR) % 8 == modulo then
- return add(taskPos, directionVectors.right)
- elseif (taskPos_LR - 2 * right_LR) % 8 == modulo then
- return add(taskPos, directionVectors.left)
- else
- return nil
- end
- elseif task.taskType == "store_items" or task.taskType == "get_items" then
- local taskPos = task.chestCoord
- local taskPos_LR, taskPos_FB = getOrientatedAxisValues(taskPos)
- local right_LR, right_FB = getOrientatedAxisValues(directionVectors.right)
- if (taskPos_LR + 2 * right_LR) % 8 == modulo then
- return add(taskPos, directionVectors.right)
- elseif (taskPos_LR - 2 * right_LR) % 8 == modulo then
- return add(taskPos, directionVectors.left)
- else
- return nil
- end
- end
- return nil
- end
- -- accept task from outgoing tasks saves status to accepted or goToPrep when finished
- local function acceptTask()
- print("\nWait for task")
- local disk, outgoingPath = getDiskByLabel(DISK_NAMES.outgoing)
- local saveState = TaskStatus.noTask
- local task
- if not disk then return nil end
- while true do
- local fullPath = fs.combine(outgoingPath, "task_list.txt")
- local tasks = getTaskList("task_list.txt")
- local bLocal = true
- if #tasks == 0 then
- tasks = getTaskList(fullPath)
- bLocal = false
- end
- if #tasks == 0 then
- sleep(0.5)
- else
- task = table.remove(tasks, 1)
- print("Got task " .. task.taskType)
- if not bLocal then
- -- Save remaining tasks back
- local file = fs.open(fullPath, "w")
- file.write(textutils.serialize(tasks))
- file.close()
- addTaskToList("task_list.txt", task)
- end
- if task.taskType == "create_chests" then
- print("Looking for chests in chest")
- turnAbsolutDirection(directionVectors.right)
- local first1, first2 = true, true
- while true do
- local success, data = turtle.inspect()
- if not (success and data.name == "minecraft:chest") then
- if first1 then
- print("No chest found, waiting for placed chest.")
- first1 = false
- end
- sleep(0.5)
- else
- if not suckSpecificItem("minecraft:chest", 100, "front") then
- if first2 then
- print("Not enough chests found, waiting for 100 chests in chest inventory")
- first2 = false
- end
- sleep(0.5)
- else
- saveState = TaskStatus.accepted
- break
- end
- end
- end
- elseif task.taskType == "store_items" then
- if task.itemCount > 1 then
- print("Looking for " .. tostring(task.itemCount) .. task.itemDisplayName .. "s in Chest above")
- else
- print("Looking for ".. tostring(task.itemCount) .. task.itemDisplayName .. " in Chest above")
- end
- local first1, first2 = true, true
- while true do
- local success, data = turtle.inspectUp()
- if not (success and data.name == "minecraft:chest") then
- if first1 then
- print("No chest found above, waiting for placed chest.")
- first1 = false
- end
- sleep(0.5)
- end
- if not suckSpecificItem(task.itemName, task.itemCount, "top") then
- if first2 then
- local str1 = "Not enough " .. task.itemDisplayName .. "s found, waiting for " .. tostring(task.itemCount)
- local str2 = " " .. task.itemDisplayName .. "s in chest inventory."
- print(str1 .. str2)
- first2 = false
- end
- sleep(0.5)
- else
- saveState = TaskStatus.accepted
- break
- end
- end
- elseif task.taskType == "get_items" then
- saveState = TaskStatus.accepted
- elseif task.taskType == "check_inventory" then
- print("Tasks of type " .. task.taskType .. " can not be accepted at the moment.")
- return false
- else
- print("Wrong task type.")
- return false
- end
- break
- end
- end
- turnAbsolutDirection(directionVectors.forward)
- while not moveForward() do sleep(0.5) end
- if saveState == TaskStatus.accepted then
- print("Accepted Task " .. task.taskType)
- end
- save_status(saveState)
- return true
- end
- -- moves turtle along systemways to given target
- local function goToCoord(targetPos)
- if not targetPos then
- print(textutils.serialize(targetPos))
- print("\nNo coreect target position given.")
- return false
- end
- print("\nGo to Coord (" .. tostring(targetPos.x) .. ", " .. tostring(targetPos.y) .. ", " .. tostring(targetPos.z) .. ")")
- if equalCoords(targetPos, turtlePos) then
- print("target reached")
- return true
- end
- local startPos = turtlePos
- local beltDistance = 0
- local bTurtleInBounds = isCoordInBounds(turtlePos)
- local bTargetInBounds = isCoordInBounds(targetPos)
- local beltPos
- local turtle_LR_coord, turtle_FB_coord = getOrientatedAxisValues(turtlePos)
- local target_LR_coord, target_FB_coord = getOrientatedAxisValues(targetPos)
- if not bTurtleInBounds then
- -- turtle outside Storage System
- print("try joining belt")
- joinBeltFromOutside(targetPos)
- print("belt joined")
- end
- if isCoordOnBelt(turtlePos) then
- -- turtle on Belt
- print("use belt")
- useAndExitBelt(targetPos)
- print("exited belt")
- if not bTargetInBounds then
- reachOutsideTarget(targetPos)
- if equalCoords(targetPos, turtlePos) then
- print("target reached")
- return true
- end
- end
- local sectionHeight = getSectionHeight(targetPos)
- print("Reaching Section at: " .. tostring(sectionHeight))
- if not (turtlePos.y == sectionHeight) then
- if turtlePos.y > sectionHeight then
- turnAbsolutDirection(directionVectors.left)
- while not moveForward() do sleep(0.5) end
- end
- if turtlePos.y < sectionHeight then
- turnAbsolutDirection(directionVectors.right)
- while not moveForward() do sleep(0.5) end
- end
- end
- end
- if isCoordOnLift(turtlePos) then
- print("Using Lift")
- useAndExitLift(targetPos)
- end
- if isCoordOnSection(turtlePos) then
- print("Using Section")
- useAndExitSection(targetPos)
- end
- if isCoordOnChestRow(turtlePos) then
- print("Using chest row")
- useAndExitChestRow(targetPos)
- end
- if isCoordOnExitSection(turtlePos) then
- print("Using exit section")
- useAndExitExitSection(targetPos)
- end
- if equalCoords(startPos, turtlePos) then
- return false
- end
- return goToCoord(targetPos)
- end
- -- completes the task and saves status to finished or goToAfterWork
- local function doTaskWork()
- print("\n Starting with task")
- local tasks = getTaskList("task_list.txt")
- if #tasks == 0 then
- return nil
- end
- local task = table.remove(tasks, 1)
- if task.taskType == "create_chests" then
- -- places a row of chests (y=5, delta_x(z) = 10) and goes back to top of first five chests
- local function placeChestRow()
- local cnt = 0
- while cnt < 10 do
- local cnt1 = 0
- while cnt1 < 5 do
- while not moveUp() do sleep(0.5) end
- if not selectItem("minecraft:chest") then return false end
- if not turtle.placeDown() then return false end
- cnt1 = cnt1 + 1
- end
- if not (cnt == 9) then
- while not moveForward() do sleep(0.5) end
- while cnt1 > 0 do
- while not moveDown() do sleep(0.5) end
- cnt1 = cnt1 - 1
- end
- end
- cnt = cnt + 1
- end
- turnInDirection("around")
- while cnt > 1 do
- while not moveForward() do sleep(0.5) end
- cnt = cnt - 1
- end
- return true
- end
- local pos_LR, pos_FB = getOrientatedAxisValues(add(turtlePos, directionVectors.right))
- local endTurnDirection = directionVectors.left
- if pos_LR % 8 == modulo then
- endTurnDirection = directionVectors.right
- end
- while not moveForward() do sleep(0.5) end
- while not moveForward() do sleep(0.5) end
- turnAbsolutDirection(directionVectors.forward)
- if not placeChestRow() then return false end
- turnAbsolutDirection(endTurnDirection)
- while not moveForward() do sleep(0.5) end
- local cnt = 5
- while cnt > 0 do
- while not moveDown() do sleep(0.5) end
- cnt = cnt - 1
- end
- turnAbsolutDirection(directionVectors.forward)
- if not placeChestRow() then return false end
- turnAbsolutDirection(endTurnDirection)
- while not moveForward() do sleep(0.5) end
- elseif task.taskType == "store_items" then
- local success, data = turtle.inspect()
- if success and data.name == "minecraft:chest" then
- dropOffInventory(task.itemName)
- else
- print("Error: No Chest found")
- return false
- end
- elseif task.taskType == "get_items" then
- local success, data = turtle.inspect()
- if success and data.name == "minecraft:chest" then
- if not suckSpecificItem(task.itemName, task.itemCount, "front") then
- print("Error: Not enough items found")
- end
- else
- print("Error: No Chest found")
- end
- if not goToCoord(outputCoord) then
- error("Can't reach output chest", 0)
- end
- dropOffInventory(task.itemName)
- turnAbsolutDirection(directionVectors.left)
- while not moveForward() do sleep(0.5) end
- turnAbsolutDirection(directionVectors.forward)
- end
- save_status(TaskStatus.finished)
- return true
- end
- -- Copys task to finished disk and deletes local one
- local function completeTask()
- local localTaskList = getTaskList("task_list.txt")
- local task = table.remove(localTaskList, 1)
- local index = 1
- print("Finish task " .. task.taskType .. " with ID " .. tostring(task.taskId))
- -- add finished Task
- local disk, path = getDiskByLabel(DISK_NAMES.finished)
- local finishedPath = fs.combine(path, "task_list.txt")
- addTaskToList(finishedPath, task)
- -- delete local (override)
- saveTaskList("task_list.txt", localTaskList)
- save_status(TaskStatus.joiningQueue)
- return true
- end
- -- === Queue Handling ===
- -- joins in queue
- local function joinQueue()
- print("\nEntering queue...")
- local level = 0
- if turtlePos.x == finishCoord.x and turtlePos.z == finishCoord.z then
- level = turtlePos.y - finishCoord.y
- else
- return false
- end
- turnAbsolutDirection(directionVectors.backward)
- while true do
- local success, data = turtle.inspect()
- if not (success and isTurtleBlock(data)) then
- while not moveForward() do sleep(0.5) end
- if level % 2 == 0 then
- turnAbsolutDirection(directionVectors.right)
- else
- turnAbsolutDirection(directionVectors.left)
- end
- break
- else
- while not moveUp() do sleep(0.5) end
- level = level + 1
- end
- end
- save_status(TaskStatus.waiting)
- end
- -- moves along queue till reaching outgoing task disk drive
- local function queueForTask()
- print("\nQueue for task")
- local bFirst = true
- while true do
- local success, data = turtle.inspectDown()
- if success and data.name == "computercraft:disk_drive" then
- print("Exiting Queue")
- break
- end
- if bFirst then
- success, data = turtle.inspect()
- if success and (not isTurtleBlock(data)) then
- while not moveDown() do sleep(0.5) end
- turnInDirection("around")
- end
- bFirst = false
- end
- if not moveForward() then
- sleep(0.5)
- else
- success, data = turtle.inspectDown()
- if success and data.name == "computercraft:disk_drive" then
- print("Exiting Queue")
- break
- end
- success, data = turtle.inspect()
- if success and (not isTurtleBlock(data)) then
- while not moveDown() do sleep(0.5) end
- turnInDirection("around")
- end
- end
- end
- save_status(TaskStatus.getTask)
- return true
- end
- local function turtleNeedsFuel()
- local fuelLevel = turtle.getFuelLevel()
- if fuelLevel < 2000 then
- print("Turtle needs more fuel")
- return true
- end
- return false
- end
- local function main()
- if load_status() == TaskStatus.refueling then
- if turtleNeedsFuel() then
- return
- end
- if isDiskOnSide(DISK_NAMES.init, "bottom") then
- save_status(TaskStatus.noTask)
- else
- return
- end
- end
- if not getFacingValue(load_status()) then
- return
- end
- while true do
- local status = load_status()
- if status == TaskStatus.refueling then
- if turtleNeedsFuel() then
- return
- end
- if isDiskOnSide(DISK_NAMES.init, "bottom") then
- save_status(TaskStatus.noTask)
- else
- return
- end
- end
- if status == TaskStatus.getTask then
- if turtleNeedsFuel() then
- save_status(TaskStatus.refuelNeeded)
- elseif not acceptTask() then
- return
- end
- elseif status == TaskStatus.accepted then
- if goToCoord(getTaskStartCoords())then
- save_status(TaskStatus.inProgress)
- end
- elseif status == TaskStatus.inProgress then
- if not doTaskWork() then return end
- elseif status == TaskStatus.finished then
- if goToCoord(finishCoord) then -- done
- save_status(TaskStatus.completing)
- end
- elseif status == TaskStatus.completing then
- completeTask()
- elseif status == TaskStatus.joiningQueue then
- joinQueue() -- done
- elseif status == TaskStatus.waiting then
- queueForTask() -- done
- elseif status == TaskStatus.refuelNeeded then
- if goToCoord(refuelCoord) then -- done
- save_status(TaskStatus.refueling)
- end
- else
- if goToCoord(finishCoord) then -- done
- save_status(TaskStatus.joiningQueue)
- end
- end
- end
- end
- -- === Initializing System ===
- orientation, direction, modulo, yModulo, finishCoord, outputCoord, refuelCoord = readInitData()
- directionVectors = getDirectionVectors(orientation, direction)
- finishCoord = add(finishCoord, directionVectors.up)
- outputCoord = add(outputCoord, directionVectors.forward)
- refuelCoord = add(add(refuelCoord, directionVectors.forward), directionVectors.forward)
- leftBeltCoord = getLeftBeltCoordinate(finishCoord, directionVectors.forward, orientation)
- rightBeltCoord = add(leftBeltCoord, directionVectors.up)
- facingVec = nil
- turtlePos = get_GPS_Position()
- main()
Advertisement
Add Comment
Please, Sign In to add comment