Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- --
- -- There are config variables below.
- --
- -- Modified to use a file to
- -- 'remember' what action it was doing
- -- when the world stopped existing.
- --
- -- Fair warning, I have not tested
- -- this code.
- --
- -- Also, you should really make things
- -- local, it keeps the computer global
- -- memory cleaner.
- --
- -- 39 characters are not enough width.
- -- http://computercraft.info/wiki/Resolution#Turtle_resolution
- --
- -- I've practically rewritten this
- -- entirely, feel free to not use it
- -- because of this.
- --
- -- If you need me to explain the code
- -- then feel free to ask me to do so.
- --
- -- Lupus590
- --
- -- P.S. I'm part way through this
- -- rewrite and I'm realising that I
- -- may be doing a lot of tricks which
- -- you may not know about, I've tried
- -- my best to comment regularly.
- --
- -- P.P.S. You have some odd logic.
- --
- -- ]]
- --[[ multi line comment
- I've done it oddly above as I believe
- ComputerCraft reacts funny.
- It might only be the syntax highlighter
- that goes funny, which this would
- demonstrate if on an advanced system. ]]
- ------------
- -- Config --
- ------------
- local targetFuelLevel = 1000
- -- turtle will try to keep at this fuel level by using lava
- -- increase if turtle runs out of fuel and has no lava
- -- decrease if it keeps drinking it all
- local stateFileName = "lavaElelvatorState"
- ----------
- -- Code --
- ----------
- local currentState
- local state = {}
- state.descend = "descend"
- state.goToLava = "goToLava"
- state.getLava = "getLava"
- state.goToShaftBottom = "goToShaftBottom"
- state.ascend = "ascend"
- state.giveLava = "giveLava"
- local action = {} -- corresponds to the states above
- action.descend
- action.goToLava
- action.getLava
- action.goToShaftBottom
- action.ascend
- action.giveLava
- local function findResumeActions() -- figures out what the turtle was doing before chunk unload so that the turtle can continue efficiently
- -- print("Running findResumeActions...")
- -- resume sequence { action.descend, action.goToLava, action.getLava, action.goToShaftBottom, action.ascend, action.giveLava}
- if fs.exists(stateFileName) then
- local stateFile = fs.open(stateFileName, "r")
- currentState = stateFile.readLine()
- stateFile.close()
- if currentState == state.descend then
- return "continue" -- default actions are fine
- elseif currentState == state.goToLava then
- return {action.goToLava, action.getLava, action.goToShaftBottom, action.ascend, action.giveLava}
- elseif currentState == state.getLava then
- return {action.getLava, action.goToShaftBottom, action.ascend, action.giveLava}
- elseif currentState == state.goToShaftBottom then
- return {action.goToShaftBottom, action.ascend, action.giveLava}
- elseif currentState == state.ascend then
- return {action.ascend, action.giveLava}
- elseif currentState == state.givLava then
- return {action.giveLava}
- else
- error("Unexpected value in file: "..stateFileName)
- end
- end
- print("Could not find file: "..stateFileName)
- print("Assistance required to start")
- print("Please place turtle to the shaft bottom and then restart this program")
- print("If the turtle is already there then type in 'Y', any other input will quit this program")
- if string.lower(read()) == "y" then
- return "continue"
- else
- return "quit"
- end
- end
- local function saveState(state)
- local stateFile = fs.open(stateFileName, "w")
- stateFile.writeLine(state) -- could error here if for some reason the file can't be opened
- stateFile.close()
- --print("state saved")
- end
- local function selectEmptySlot()
- for i = 1, 16 do
- turtle.select(i)
- if turtle.getItemCount() == 0 then
- return true
- end
- end
- return false -- could not find an empty slot
- end
- local function checkFuel() -- true = lava used
- local currentSlot = 1
- local drank = false
- while turtle.getFuelLevel < targetFuelLevel do
- turtle.select(currentSlot)
- if not turtle.refuel(1) then -- if we can't refuel from it
- if currentSlot ~= 16 then
- currentSlot = currentSlot + 1 -- select the next slot
- else
- break -- exit the while loop as we have ran out of slots to try to refuel from
- end
- else
- drank = true -- we refuelled from something
- end
- end
- return drank
- end
- function action.descend()
- --print("Running action.descend...")
- saveState(state.descending)
- while turtle.down() do -- if we can go down then we do, otherwise we must be at the bottom
- -- would check fuel but no lava on board so we are stuck anyway
- end
- end
- function action.goToLava()
- --print("Running action.goToLava...")
- saveState(state.goToLava)
- while turtle.forward() do
- -- would check fuel but no lava on board so we are stuck anyway
- end
- end
- function action.getLava()
- --print("Running action.getLava...")
- saveState(state.getLava)
- -- drop off empty buckets
- for i = 1,16 do
- turtle.select(i)
- local _, item = turtle.getItemDetail()
- if item.name = "minecraft:bucket" then
- turtle.drop()
- end
- end
- -- pick up full buckets
- while selectEmptySlot() do
- turtle.suck()
- -- check that the item we got is not an empty bucket
- local _, item = turtle.getItemDetail()
- if item.name = "minecraft:bucket" then
- turtle.drop() -- if it is an empty bucket, put it back
- sleep(10) -- sleep a bit so the other turtle has time to fill the bucket
- end
- -- let's check our fuel while we have lava near us
- if checkFuel() then
- -- we just refuelled, may as well replace the empty bucket/s while we are here
- return action.getLava() -- trick: by returning we don't use another 'slot' on the function stack
- end
- end
- end
- function action.goToShaftBottom()
- --print("Running action.goToShaftBottom...")
- saveState(state.goToShaftBottom)
- checkFuel()
- while turtle.back() do
- checkFuel() -- have lava, don't want to get stuck
- end
- end
- function action.ascend()
- --print("Running action.ascend...")
- saveState(state.ascend)
- local _, blockInFront = turtle.inspect()
- local foundChest = blockInFront == "minecraft:chest" -- this should work
- while not foundChest do
- checkFuel()
- turtle.up()
- _, blockInFront = turtle.inspect() -- reusing the old variables
- foundChest = blockInFront == "minecraft:chest"
- end
- end
- function action.giveLava()
- --print("Running action.giveLava...")
- saveState(state.giveLava)
- checkFuel() -- just before we do, lets be a selfish turtle
- -- drop off full buckets
- for i = 1,16 do
- turtle.select(i)
- local _, item = turtle.getItemDetail()
- if item.name = "minecraft:lava_bucket" then
- turtle.drop()
- end
- end
- -- pick up empty buckets
- while selectEmptySlot() do
- turtle.suck()
- -- check that the item we got is not a full bucket
- local _, item = turtle.getItemDetail()
- if item.name = "minecraft:lava_bucket" then
- turtle.drop() -- if it is a full bucket, put it back
- sleep(10) -- sleep a bit so the quarry has time to drink
- end
- end
- end
- ------------------
- -- Main Program --
- ------------------
- local resumeActions = findResumeActions() -- returns a table (usually)
- if type(resumeActions) == "table" and #resumeActions > 0 then -- is it a table and does it have stuff in
- for _,v in ipairs(resumeActions) do -- go through each key in the table and put the value in v
- v() -- the value should be a function, run it. If it ends up not being a function the script will error here
- end
- elseif type(resumeActions) == "string"
- if resumeActions == "quit" then
- return
- elseif resumeActions == "continue" then
- -- continue to while true loop
- else
- error("Unexpected string from findResumeActions")
- end
- else
- error("Unexpected return value from findResumeActions")
- end
- while true do
- action.descend()
- action.goToLava()
- action.getLava()
- action.goToShaftBottom()
- action.ascend()
- action.goToQuarry()
- action.giveLava()
- action.goToShaftTop()
- end
Add Comment
Please, Sign In to add comment