Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local pos = {x = nil, y = nil, z = nil}
- local orientation = nil
- --possible states are "home", "movingToQuarry",
- -- "inQuarry", "mining", "exitingQuarry", "movingHome"
- local state = nil
- local settings = {yTravel = 2, fuelMin = 1000,
- refuelAmount = 1500, managerID = nil, distressID = nil}
- -- MAKE SURE SLOTS COME IN ORDER (i.e fuel then enderFuel then enderStorage...)
- local slots = {fuel = {1,2,3}, noDig = nil, trash = {4,5,6}, enderFuel = nil,
- enderStorage = nil}
- -- Storage chest in front, fuel chest below
- local home = {x = nil, y = nil, z = nil, orientation = nil}
- local job = {x = nil, y = nil, z = nil, orientation = nil, size = nil}
- --north, east, south, west
- local xDiff = {0, 1, 0, -1}
- local zDiff = {-1, 0, 1, 0}
- local selected = nil
- --**** utility ****--
- local function member(table, value)
- if table == nil then
- return false
- end
- for k,v in ipairs(table) do
- if table[k] == value then
- return true
- end
- end
- return false
- end
- --**** inventory stuff ****--
- local function select(slot)
- if selected ~= slot then
- turtle.select(slot)
- selected = slot
- end
- end
- local function isTrash() --checks current slot for trash
- if slots.noDig then
- for i = 1,#slots.noDig do
- if turtle.compareTo(slots.noDig[i]) then
- return true
- end
- end
- end
- if slots.trash then
- for i =1,#slots.trash do
- if turtle.compareTo(slots.trash[i]) then
- return true
- end
- end
- end
- return false
- end
- local function isSpecialSlot(slot)
- for k,v in pairs(slots) do
- if type(v) == "table" then
- for i = 1,#slots[k] do
- if slot == slots[k][i] then
- return true
- end
- end
- elseif slot == v then
- return true
- end
- end
- return false
- end
- local function removeTrash()
- for i = 1,16 do
- if turtle.getItemCount(i) > 0 then
- select(i)
- if isTrash() then
- if member(slots.noDig, i) or member(slots.trash, i) then
- turtle.dropUp(math.max(0, turtle.getItemCount(i) - 1))
- else
- turtle.dropUp()
- end
- end
- end
- end
- select(1)
- end
- local function organizeInventory()
- for i = 16,2,-1 do
- if not isSpecialSlot(i) and turtle.getItemCount(i) > 0 then
- select(i)
- for j = 1, i-1 do
- turtle.transferTo(j)
- end
- end
- end
- end
- local function dig()
- if slots.noDig then
- local detectedUp = false
- local detectedDown = false
- for i = 1,#slots.noDig do
- select(slots.noDig[i])
- if not detectedUp and turtle.compareUp() then
- detectedUp = true
- end
- if not detectedDown and turtle.compareDown() then
- detectedDown = true
- end
- if detectedUp and detectedDown then
- break
- end
- end
- select(slots.noDig[1])
- if not detectedUp then
- turtle.digUp()
- end
- if not detectedDown then
- turtle.digDown()
- end
- else
- turtle.digUp()
- turtle.digDown()
- end
- end
- --**** file handling ****--
- local paths = {state = "quarryData/state", home = "quarryData/home",
- settings = "quarryData/settings", job = "quarryData/job"}
- local function save(file, data)
- local h = fs.open(file,"w")
- h.write(textutils.serialize(data))
- h.close(h)
- return true
- end
- local function load(file)
- if not fs.exists(file) then
- print("tried to load an invalid file: "..file)
- return nil
- end
- local h = fs.open(file, "r")
- local rv = textutils.unserialize(h.readAll())
- h.close()
- return rv
- end
- --**** basic movement ****--
- local function moveForward(safe)
- while not turtle.forward() do
- if not safe then
- turtle.dig()
- end
- end
- pos.x = pos.x + xDiff[orientation]
- pos.z = pos.z + zDiff[orientation]
- end
- local function moveUp(safe)
- while not turtle.up() do
- if not safe then
- turtle.digUp()
- end
- end
- pos.y = pos.y + 1
- end
- local function moveDown(safe)
- while not turtle.down() do
- if not safe then
- turtle.digDown()
- end
- end
- pos.y = pos.y - 1
- end
- local function right()
- turtle.turnRight()
- orientation = orientation%4 + 1
- end
- local function left()
- turtle.turnLeft()
- orientation = (orientation - 2)%4 + 1
- end
- local function look(direction)
- while orientation ~= direction do
- if (orientation - direction)%4 >= 2 then
- right()
- else
- left()
- end
- end
- end
- local function moveTo(xTarget, yTarget, zTarget, safeMode)
- while yTarget > pos.y do
- moveUp(safeMode)
- end
- while yTarget < pos.y do
- moveDown(safeMode)
- end
- if xTarget < pos.x then
- look(4)
- elseif xTarget > pos.x then
- look(2)
- end
- while xTarget ~= pos.x do
- moveForward(safeMode)
- end
- if zTarget < pos.z then
- look(1)
- elseif zTarget > pos.z then
- look(3)
- end
- while zTarget ~= pos.z do
- moveForward(safeMode)
- end
- end
- --**** advanced movement ****--
- local function moveHome() -- can only be called from states "mining", "exitingQuarry", "movingHome"
- if state == "mining" then
- moveTo(job.x, pos.y, job.y)
- state = "exitingQuarry"
- save(paths.state, state)
- end
- if state == "exitingQuarry" then
- moveTo(job.x, settings.yTravel, job.z)
- state = "movingHome"
- save(paths.state, state)
- end
- if state == "movingHome" then
- moveTo(home.x, settings.yTravel, home.z)
- moveTo(home.x, home.y, home.z, true) --may get stuck bc safeMode
- look(home.orientation)
- state = "home"
- save(paths.state, state)
- end
- end
- local function moveToQuarry() -- gets stuck by safe mode sometimes
- if state == "home" then
- moveTo(home.x, settings.yTravel, home.z, true)
- state = "movingToQuarry"
- save(paths.state, state)
- end
- if state == "movingToQuarry" then
- moveTo(job.x, settings.yTravel, job.z)
- state = "enteringQuarry"
- save(paths.state, state)
- end
- if state == "enteringQuarry" then
- moveTo(job.x, job.y, job.z)
- look(job.orientation)
- state = "mining"
- save(paths.state, state)
- end
- end
- local function locate() -- loops until success
- pos.z = nil
- while pos.z == nil do
- pos.x, pos.y, pos.z = gps.locate(1)
- end
- end
- local function orient(safe)
- locate()
- local x1 = pos.x
- local z1 = pos.z
- local rightTurns = 0
- local ups = 0
- while true do
- if not safe then
- turtle.dig()
- end
- turtle.attack()
- if not turtle.forward() then
- turtle.turnRight()
- rightTurns = rightTurns + 1
- if rightTurns % 4 == 0 then
- if not safe then
- turtle.digUp()
- end
- if turtle.up() then
- ups = ups + 1
- else
- print("stuck in 'function: orient'")
- end
- end
- else
- break
- end
- end
- locate()
- --north, east, south, west
- --local xDiff = {0, 1, 0, -1}
- --local zDiff = {-1, 0, 1, 0}
- if pos.x > x1 then
- orientation = 2
- elseif pos.x < x1 then
- orientation = 4
- elseif pos.z > z1 then
- orientation = 3
- elseif pos.z < z1 then
- orientation = 1
- else
- print("Orientation was not decided properly")
- end
- right()
- rightTurns = rightTurns + 1
- right()
- rightTurns = rightTurns + 1
- moveForward(safe)
- for i = 1,ups do
- moveDown(safe)
- end
- for i = 1,rightTurns%4 do
- left()
- end
- end
- --**** refueling and dropping ****--
- local function getNextEmpty(i) --returns the next empty slot after i
- for j = i,16 do
- if turtle.getItemCount(j) == 0 then
- return j
- end
- end
- return nil
- end
- local function normalRefuel()
- if not slots.fuel then
- return false
- end
- for i = 1,#slots.fuel do
- if turtle.getItemCount(slots.fuel[i]) > 0 then
- select(i)
- while turtle.getFuelLevel() < settings.fuelMin + settings.refuelAmount and turtle.refuel(1) do
- end
- if turtle.getFuelLevel() > settings.fuelMin + settings.refuelAmount then
- break
- end
- end
- end
- select(1)
- end
- local function normalDrop()
- for i = 1,16 do
- select(i)
- if member(slots.noDig, i) or member(slots.trash, i) then
- turtle.drop(math.max(0, turtle.getItemCount(i) - 1))
- elseif not isSpecialSlot(i) then -- inefficient iterations, negligable
- turtle.drop()
- end
- end
- select(1)
- end
- local function restockFuel() --Assumes 16 always is an insignificant slot
- select(slots.fuel[1])
- while turtle.getFuelLevel() < settings.fuelMin + settings.refuelAmount do --refuelling loop
- if not turtle.refuel(1) then
- turtle.drop()
- if not turtle.suckDown() then
- return
- end
- end
- end
- if not getNextEmpty(slots.fuel[#slots.fuel]+1) then --makes sure there is an empty slot
- select(16)
- turtle.drop()
- end
- for i=1,#slots.fuel do
- select(slots.fuel[i])
- if not turtle.refuel(0) then
- turtle.drop()
- end
- local nextEmpty = 16
- while turtle.getItemCount(selected) < 64 do
- local count = turtle.getItemCount(selected)
- nextEmpty = getNextEmpty(selected+1) --fails if slot 16 is set for fuel
- if not turtle.suckDown() then
- break
- elseif turtle.getItemCount(selected) == count then --sucked but itemcount did not change
- select(nextEmpty)
- turtle.drop()
- select(slots.fuel[i])
- else
- if not turtle.refuel(0) then
- turtle.drop()
- end
- end
- end
- select(nextEmpty)
- turtle.dropDown()
- end
- select(1)
- end
- local function enderRefuel(safe) -- loops until success
- select(slots.enderFuel)
- local rights = 0
- local ups = 0
- while true do --chest placing loop
- if not safe then
- turtle.dig()
- end
- turtle.attack()
- if turtle.place() then
- break --
- end
- turtle.turnRight()
- rights = rights + 1
- if rights%4==0 then
- if not safe then
- turtle.digUp()
- end
- turtle.attackUp()
- if turtle.up() then
- ups = ups + 1
- end
- end
- end
- while turtle.getFuelLevel() < settings.fuelMin + settings.refuelAmount do
- if turtle.getItemCount(selected) == 0 then
- turtle.suck()
- end
- if not turtle.refuel(1) then
- turtle.dropUp()
- end
- end
- turtle.drop()
- turtle.dig()
- select(1)
- for i = 1,rights%4 do
- turtle.turnLeft()
- end
- for i=1,ups do
- while not turtle.down() do
- if not safe then
- turtle.digDown()
- end
- turtle.attackDown()
- end
- end
- end
- local function enderDrop(safe) -- loops until success
- select(slots.enderStorage)
- local rights = 0
- local ups = 0
- while true do --chest placing loop
- if not safe then
- turtle.dig()
- end
- turtle.attack()
- if turtle.place() then
- break --
- end
- turtle.turnRight()
- rights = rights + 1
- if rights%4==0 then
- if not safe then
- turtle.digUp()
- end
- turtle.attackUp()
- if turtle.up() then
- ups = ups + 1
- end
- end
- end
- normalDrop()
- select(slots.enderStorage)
- turtle.drop()
- turtle.dig()
- select(1)
- for i = 1,rights%4 do
- turtle.turnLeft()
- end
- for i=1,ups do
- while not turtle.down() do
- if not safe then
- turtle.digDown()
- end
- turtle.attackDown()
- end
- end
- end
- local function goHomeDropReturn()
- local xTemp, yTemp, zTemp, oTemp = pos.x, pos.y, pos.z, orientation
- moveHome()
- normalDrop()
- if not slots.enderFuel then
- restockFuel()
- end
- moveToQuarry()
- moveTo(xTemp, yTemp, zTemp)
- look(oTemp)
- end
- --**** digging and quarry ****--
- local function digLayer(length, width) -- consecutive calls with a non-quadratic size results in varying results
- for i = 1,width do
- for j=1,length-1 do
- -------------------------------------
- if turtle.getItemCount(16) > 0 then
- removeTrash()
- organizeInventory()
- if turtle.getItemCount(16) > 0 then
- if slots.enderStorage then
- enderdrop()
- else
- goHomeDropReturn()
- end
- end
- end
- -------------------------------------
- dig()
- moveForward()
- end
- dig()
- if i < width then
- if i%2==1 then
- right()
- moveForward()
- right()
- else
- left()
- moveForward()
- left()
- end
- end
- end
- -------------------------------------------------------
- if turtle.getFuelLevel() < settings.fuelMin then
- if slots.enderFuel then
- enderRefuel()
- else
- normalRefuel()
- if turtle.getFuelLevel() < settings.fuelMin then
- goHomeDropReturn()
- end
- end
- end
- -------------------------------------------------------
- end
- ----[[
- home = load(paths.home)
- job = load(paths.job)
- orient(true)
- moveTo(job.x, home.y + settings.yTravel, job.z, true)
- moveTo(job.x, job.y, job.z)
- look(job.orientation)
- digLayer(job.size, job.size)
- removeTrash()
- moveTo(home.x, home.y + settings.yTravel, home.z, true)
- moveTo(home.x, home.y, home.z, true)
- look(home.orientation)
- normalDrop()
- restockFuel()
- --]]
- --[[
- orient(true)
- home = {x = pos.x, y = pos.y, z = pos.z, orientation = orientation}
- save(paths.home, home)
- job = {x = home.x + xDiff[orientation]*5, y = home.y - 2, z = home.z + zDiff[orientation]*5, orientation = orientation}
- save(paths.job, job)--]]
- --rednet.open("right")
- --MAKE SURE THE TURTLE CHECKS FUEL IMMIDIATELY, AS TO NOT GET STUCK
- --Tested functions:
- -- member, select, isTrash, isSpecialSlot, removeTrash, organizeInventory,
- -- dig, save, load, moveForward, moveUp, moveDown, right, left, moveTo, locate, orient,
- -- getNextEmpty, normalRefuel, normalDrop, enderDrop, restockFuel
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement