Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- stack = {}--save
- stacksize=0--save
- function push(x)
- stacksize=stacksize+1
- stack[stacksize]=x
- fsave({stack=stack,stacksize=stacksize})
- end
- function pop()
- a=get()
- stack[stacksize]=nil
- stacksize=stacksize-1
- fsave({stack=stack,stacksize=stacksize})
- return a
- end
- function get()
- return stack[stacksize]
- end
- function tablelength(T)
- local count = 0
- for _ in pairs(T) do count = count + 1 end
- return count
- end
- goingback=false--saved
- vein=false--saved
- x=0--FORWARDS+ saved
- y=0--RIGHT+ saved
- z=0--UP+ saved
- sx=0--saved
- sy=0--saved
- sz=0--saved
- delay=0.05
- facing=0--saved
- FORWARD=0
- RIGHT=1
- BACK=2
- LEFT=3
- DOWN=4
- UP=5
- junk={}
- worth={}
- clearfrom=2
- function clearfroms(slot)
- clearfrom=slot
- end
- function addtoworth(filter)
- if type(filter)=="table" then
- for index, value in ipairs(filter) do
- worth[#worth+1]=value
- end
- else
- worth[#worth+1]=filter
- end
- end
- function addtojunk(filter)
- if type(filter)=="table" then
- for index, value in ipairs(filter) do
- junk[#junk+1]=value
- end
- else
- junk[#junk+1]=filter
- end
- end
- function split(inputstr, sep)
- if sep == nil then
- sep = "%s"
- end
- local t={}
- for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
- table.insert(t, str)
- end
- return t
- end
- function toItem(id)--minecraft:cobblestone
- if id==nil then
- return "None"
- end
- local ls=split(id,":")
- if ls[2]~=nil then
- return string.lower(split(id,":")[2])
- end
- return id
- end
- function contains(tab, val)
- for index, value in ipairs(tab) do
- if value == val then
- return true
- end
- end
- return false
- end
- function containsc(tab, val)
- for index, value in ipairs(tab) do
- print(value.." "..val)
- if string.find(val,value) then
- return true
- end
- end
- return false
- end
- function rotate(dir)
- while not (dir==facing) do
- if dir<facing then
- turtle.turnLeft()
- facing=facing-1
- else
- turtle.turnRight()
- facing=facing+1
- end
- end
- fsave({facing=facing})
- end
- function checkOreslite()
- local blocku=getFBlocku()
- local blockd=getFBlockd()
- local block=getFBlock()
- if turtle.getFuelLevel()<turtle.getFuelLimit() then
- if block=="lava" and getFBlockdata().state.level==0 then
- setItem("bucket",1)
- turtle.select(1)
- turtle.place()
- turtle.refuel(1)
- end
- if blocku=="lava" and getFBlockdatau().state.level==0 then
- setItem("bucket",1)
- turtle.select(1)
- turtle.placeUp()
- turtle.refuel(1)
- end
- if blockd=="lava" and getFBlockdatad().state.level==0 then
- setItem("bucket",1)
- turtle.select(1)
- turtle.placeDown()
- turtle.refuel(1)
- end
- end
- if not vein then
- if isWorth(blocku) then
- veinmine(UP)
- end
- if isWorth(blockd) then
- veinmine(DOWN)
- end
- if isWorth(block) then
- veinmine(facing)
- end
- end
- if isChest(blocku) then
- lootChest(UP)
- end
- if isChest(blockd) then
- lootChest(DOWN)
- end
- if isChest(block) then
- lootChest(FORWARD)
- end
- end
- function movef()
- checkOreslite()
- if(turtle.detect()) then
- while not turtle.forward() do
- turtle.dig()
- turtle.attack()
- sleep(delay)
- end
- else
- turtle.forward()
- end
- if(facing==FORWARD) then
- x=x+1
- elseif(facing==BACK) then
- x=x-1
- elseif(facing==RIGHT) then
- y=y+1
- elseif(facing==LEFT) then
- y=y-1
- end
- fsave({y=y,x=x})
- checkOreslite()
- end
- function movev(dir)
- if dir==DOWN then
- while not turtle.down() do
- turtle.digDown()
- sleep(delay)
- end
- z=z-1;
- else
- while not turtle.up() do
- turtle.digUp()
- sleep(delay)
- end
- z=z+1;
- end
- fsave({z=z})
- end
- function move(mx,my,mz)
- local dir=0;
- if not (mx==0) then
- rotate(getMoveDir(mx,0))
- mx=math.abs(mx);
- while not (mx==0) do
- movef()
- mx=mx-1;
- end
- end
- if not (my==0) then
- rotate(getMoveDir(0,my))
- my=math.abs(my);
- while not (my==0) do
- movef()
- my=my-1;
- end
- end
- while not (mz==0) do
- checkOreslite()
- if mz<0 then
- movev(DOWN)
- mz=mz+1;
- else
- movev(UP)
- mz=mz-1;
- end
- checkOreslite()
- end
- checkOreslite()
- end
- function getMoveDir(mx,my)
- if mx==0 and my==0 then
- return facing;
- end
- if mx>0 and my==0 then
- return FORWARD;
- end
- if mx<0 and my==0 then
- return BACK;
- end
- if mx==0 and my<0 then
- return LEFT;
- end
- if mx==0 and my>0 then
- return RIGHT;
- end
- end
- function getFBlockd()
- local success, ydata = turtle.inspectDown()
- if success then
- return toItem(ydata.name)
- else
- return nil
- end
- end
- function getFBlocku()
- local success, ydata = turtle.inspectUp()
- if success then
- return toItem(ydata.name)
- else
- return nil
- end
- end
- function getRedstone()
- local success, ydata = turtle.inspect()
- if success and string.find(getFBlock(), "redstone") then
- return tonumber(ydata.state.power)
- else
- return 0
- end
- end
- function getFBlock()
- local success, ydata = turtle.inspect()
- if success then
- return toItem(ydata.name)
- else
- return nil
- end
- end
- function getFBlockdata()
- local success, ydata = turtle.inspect()
- if success then
- return ydata
- else
- return nil
- end
- end
- function getFBlockdatau()
- local success, ydata = turtle.inspectUp()
- if success then
- return ydata
- else
- return nil
- end
- end
- function getFBlockdatad()
- local success, ydata = turtle.inspectDown()
- if success then
- return ydata
- else
- return nil
- end
- end
- function isWorth(block)
- if block==nil then
- return false
- end
- return containsc(worth, block)
- end
- function isChest(block)
- if block==nil then
- return false
- end
- return string.find(block, "chest")
- end
- function shouldret()
- return turtle.getFuelLevel()-(math.abs(x)+math.abs(y)+math.abs(z))<200;
- end
- function shouldretc(lfuel)
- return turtle.getFuelLevel()-(lfuel+math.abs(x)+math.abs(y)+math.abs(z))<200;
- end
- function findOre()
- if isWorth(getFBlocku()) then
- return UP
- end
- if isWorth(getFBlockd()) then
- return DOWN
- end
- if isChest(getFBlocku()) then
- lootChest(UP)
- end
- if isChest(getFBlockd()) then
- lootChest(DOWN)
- end
- rotate(FORWARD)
- if isChest(getFBlock()) then
- lootChest(FORWARD)
- end
- if isWorth(getFBlock()) then
- return FORWARD
- end
- rotate(RIGHT)
- if isChest(getFBlock()) then
- lootChest(FORWARD)
- end
- if isWorth(getFBlock()) then
- return RIGHT
- end
- rotate(BACK)
- if isChest(getFBlock()) then
- lootChest(FORWARD)
- end
- if isWorth(getFBlock()) then
- return BACK
- end
- rotate(LEFT)
- if isChest(getFBlock()) then
- lootChest(FORWARD)
- end
- if isWorth(getFBlock()) then
- return LEFT
- end
- return -1
- end
- function veinmine(dir)
- if goingback then
- return
- end
- vein=true
- fsave({vein=vein})
- print("Mining Vein")
- sx=x
- sy=y
- sz=z
- fsave({sx=sx,sy=sy,sz=sz})
- local nx=0
- local ny=0
- local nz=0
- push(nz)
- push(ny)
- push(nx)
- startdir=facing
- while dir>=0 or stacksize>1 do
- if dir>=0 then
- -- move to next and look for ores
- if dir<4 then
- rotate(dir)
- movef()
- else
- if dir==DOWN then
- move(0,0,-1)
- else
- move(0,0,1)
- end
- end
- nx=x-sx
- ny=y-sy
- nz=z-sz
- push(nz)
- push(ny)
- push(nx)
- dir=findOre()
- else
- local vx=pop()-nx
- local vy=pop()-ny
- local vz=pop()-nz
- move(vx,vy,vz)
- nx=x-sx
- ny=y-sy
- nz=z-sz
- dir=findOre()
- end
- end
- rotate(startdir)
- print("Mined Vein")
- vein=false
- fsave({vein=vein})
- end
- function goback()
- goingback=true
- fsave({goingback=goingback})
- print("Going Back")
- move(-x,-y,-z)
- rotate(FORWARD)
- goingback=false
- fsave({goingback=goingback})
- end
- function checkOres()
- if findOre()>0 then
- veinmine(facing)
- end
- end
- function deposit()
- if not isChest(getFBlock()) then
- return false
- end
- for i=clearfrom,16 do
- turtle.select(i)
- while turtle.drop() == false and turtle.getItemCount() ~= 0 do -- Waits until turtle can deposit item
- sleep(1)
- end
- turtle.drop()
- end
- turtle.select(1)
- return true
- end
- function check()
- if turtle.getItemCount(15) == 0 then
- return false
- else
- return true
- end
- end
- function suck(dir)
- if dir==UP then
- return turtle.suckUp()
- elseif dir==DOWN then
- return turtle.suckDown()
- else
- rotate(dir)
- return turtle.suck()
- end
- end
- function lootChest(dir)
- if not (x==0 and y==0 and z==0) then
- if check() then
- clearInv()
- end
- while suck(dir) do
- if check() then
- clearInv()
- end
- end
- end
- end
- function clearInv()
- print("Clearing Inv")
- for i=clearfrom,16 do
- if turtle.getItemCount(i) > 0 then
- print(toItem(turtle.getItemDetail(i).name))
- if containsc(junk,toItem(turtle.getItemDetail(i)["name"])) then
- turtle.select(i)
- turtle.drop()
- end
- end
- end
- turtle.select(1)
- if check() then
- print("Going back to clear inventory")
- local cdir=facing
- if stacksize>1 then
- for i=stacksize,2,-3 do
- local vx=stack[i]-(x-sx)
- local vy=stack[i-1]-(y-sy)
- local vz=stack[i-2]-(z-sz)
- move(vx,vy,vz)
- end
- end
- local hx=x
- local hy=y
- local hz=z
- goback()
- rotate(BACK)
- if not deposit() then
- print("Couldnt find Chest")
- exit()
- end
- if shouldretc((hx+hy+hz)*2) then
- print("Got no fuel left")
- exit()
- end
- move(hx,hy,hz)
- if stacksize>1 then
- for i=3,stacksize,3 do
- local vx=stack[i]-(x-sx)
- local vy=stack[i-1]-(y-sy)
- local vz=stack[i-2]-(z-sz)
- move(vx,vy,vz)
- end
- end
- rotate(cdir)
- end
- end
- function getItem(slot)
- local ydata = turtle.getItemDetail(slot)
- if ydata ~= nil then
- return toItem(ydata.name)
- else
- return nil
- end
- end
- function freespace(slot)
- turtle.select(slot)
- for i=1,16 do
- if i~=slot then
- if turtle.getItemCount(i)==0 then
- turtle.transferTo(i)
- return
- end
- end
- end
- clearInv()
- for i=1,16 do
- if i~=slot then
- if turtle.getItemCount(i)==0 then
- turtle.transferTo(i)
- return
- end
- end
- end
- end
- function setItem(item,slot)
- if string.find(toItem(getItem(slot)), item) then
- return
- end
- if turtle.getItemCount(slot)>0 then
- freespace(slot)
- end
- for i=1,16 do
- if string.find(toItem(getItem(i)), item) then
- turtle.select(i)
- turtle.transferTo(slot)
- end
- end
- end
- function atSpawn()
- return x==0 and y==0 and z==0
- end
- save={}
- --shell.getRunningProgram()
- function registerRunning(path)
- local r=fs.open("resume","w")
- r.write(path)
- r.close()
- local s=fs.open("save","w")
- s.close()
- end
- function unregister()
- fs.delete("resume")
- fs.delete("save")
- end
- function fstd(tab)
- for k, v in pairs(tab) do
- if save[k]==nil then
- save[k]=v
- end
- end
- end
- function fsave(tab)
- for k, v in pairs(tab) do
- save[k]=v
- print("Saving",k)
- end
- local f=fs.open("save","w")
- f.write(textutils.serialize(save))
- f.flush()
- f.close()
- end
- function fload(name)
- return save["name"]
- end
- function isResumed()
- return fs.exists("resume")
- end
- function tobool(str)
- return str=="true"
- end
- function loadstatus()
- if isResumed() then
- if fs.exists("save") then
- local savefile=fs.open("save","r")
- save=textutils.unserialize(savefile.readAll())
- savefile.close()
- end
- fstd({
- x=x,
- y=y,
- z=z,
- facing=facing,
- vein=vein,
- goingback=goingback,
- stack=stack,
- stacksize=stacksize,
- sx=sx,
- sy=sy,
- sz=sz
- })
- print("Loading last coords")
- print(textutils.tabulate(save))
- x=tonumber(fload("x"))
- y=tonumber(fload("y"))
- z=tonumber(fload("z"))
- facing=tonumber(fload("facing"))
- vein=tobool(fload("vein"))
- if vein then
- --resume
- --TODO return to veinmine and handle clear Inventory
- end
- goingback=tobool(fload("goingback"))
- if goingback then
- goback()
- end
- print("table type: "..type(fload("stack")))
- stack =fload("stack")
- stacksize =tonumber(fload("stacksize"))
- sx =tonumber(fload("sx"))
- sy =tonumber(fload("sy"))
- sz =tonumber(fload("sz"))
- end
- end
- loadstatus()
Advertisement
Add Comment
Please, Sign In to add comment