Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- init
- num = 1
- step = 1
- curnum = 1
- fuelenderchest = 1
- stuffenderchest = 2
- minfuel = 1024
- -- A little change to standard assert function
- _G.assert = function(condition, errMsg, level)
- if not condition then
- error(errMsg, (tonumber(level) or 1) + 1)
- end
- return condition
- end
- -- explode(seperator, string)
- function explode(d,p)
- local t, ll
- t={}
- ll=0
- if(#p == 1) then return {p} end
- while true do
- l=string.find(p,d,ll,true) -- find the next d in the string
- if l~=nil then -- if "not not" found then..
- table.insert(t, string.sub(p,ll,l-1)) -- Save it in our array.
- ll=l+1 -- save just after where we found it for searching next time.
- else
- table.insert(t, string.sub(p,ll)) -- Save what's left in our array.
- break -- Break at end, as it should be, according to the lua manual.
- end
- end
- return t
- end
- function exportResume(t, r)
- r = r or "resume"
- f = io.open(r, "w")
- for k,v in pairs(t) do
- f:write(k .. "=" .. tostring(v) .. "\n")
- end
- f:close()
- end
- -- resume function. Needs to be called after variable init.
- function loadResume(r)
- r = r or "resume"
- t = io.open(r)
- if t then
- for lines in t:lines(t) do
- c = explode("=", lines)
- if #c == 2 then
- if #c[1] > 2 then
- print("setting "..tostring(c[1]).." to "..tostring(c[2]))
- if c[1] == "num" then num = tonumber(c[2]) end
- if c[1] == "step" then step = tonumber(c[2]) end
- if c[1] == "curnum" then curnum = tonumber(c[2]) end
- _G[c[1]] = tonumber(c[2])
- end
- end
- end
- t:close()
- end
- end
- local function forcePlaceUp ()
- while not turtle.placeUp() do
- if turtle.detectUp() then
- if peripheral.isPresent ("top") and peripheral.getType ("top") == "turtle" then
- -- Other turtle detected. Waiting...
- while peripheral.isPresent ("top") do
- sleep (2)
- end
- end
- turtle.digUp()
- elseif turtle.attackUp() then
- -- Attacking obstructing entity
- end
- end
- end
- function checkFuel (amount)
- assert (type(amount) == "number", "Bad fuel amount. Number required, got "..type(amount), 2)
- local fuel = turtle.getFuelLevel()
- if fuel == "unlimited" or fuel > amount then
- return true
- else
- turtle.select (fuelenderchest)
- forcePlaceUp ()
- turtle.suckUp ()
- while true do
- if not turtle.refuel (1) then
- if turtle.getItemCount (fuelenderchest) then
- turtle.dropUp ()
- end
- sleep (1)
- turtle.suckUp ()
- end
- if turtle.getFuelLevel () >= amount then
- if turtle.getItemCount (fuelenderchest) > 0 then
- turtle.dropUp ()
- end
- -- If it can't drop all the fuel into chest, just consume it all
- if turtle.getItemCount (fuelenderchest) > 0 then
- turtle.refuel (turtle.getItemCount(fuelenderchest))
- end
- turtle.digUp ()
- turtle.select (1)
- return true
- end
- sleep (1)
- end
- end
- end
- function dumpStuff ()
- if stuffenderchest ~= nil then
- turtle.select (stuffenderchest)
- forcePlaceUp ()
- for i = 1,16 do
- if i ~= fuelenderchest and i ~= stuffenderchest then
- while turtle.getItemCount (i) > 0 do
- turtle.select (i)
- turtle.dropUp ()
- end
- end
- end
- turtle.select (stuffenderchest)
- turtle.digUp ()
- turtle.select (1)
- end
- end
- function checkSpace ()
- if turtle.getItemCount (16) > 0 then
- dumpStuff ()
- end
- end
- -- resume
- loadResume()
- -- mainloop
- while true do
- -- pre check fuel
- checkFuel(minfuel)
- -- pre check inventory
- checkSpace()
- -- dig + check inventory
- turtle.digUp()
- checkSpace()
- while turtle.suckUp() do
- checkSpace()
- sleep(0.01)
- end
- turtle.digDown()
- checkSpace()
- while turtle.suckDown() do
- checkSpace()
- sleep(0.01)
- end
- turtle.dig()
- checkSpace()
- while turtle.suck() do
- checkSpace()
- sleep(0.01)
- end
- uptries = 0
- while turtle.detectUp() do
- uptries = uptries + 1
- if uptries > 10 then break end
- turtle.digUp()
- checkSpace()
- sleep(0.01)
- end
- -- move
- while not turtle.forward() do
- -- try fixing the reason why we can't move and retry
- turtle.dig()
- turtle.attack()
- checkFuel(minfuel)
- checkSpace()
- sleep(0.01)
- end
- curnum = curnum + 1
- if curnum > num then
- curnum = 1
- turtle.turnLeft()
- step = step + 1
- if step > 2 then
- step = 1
- num = num + 1
- end
- end
- -- write resume
- exportResume({num = num, step = step, curnum = curnum})
- -- pause
- sleep(0.01)
- end
Advertisement
Add Comment
Please, Sign In to add comment