Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local initfn ="initwell"
- local initar={}
- local start_fuel=0
- local target_fuel=0
- local miningwell=1
- local chest=2
- local conduit=3
- local chargestation=4
- local args={...}
- local arg={}
- for i=1,#args do arg[i]=args[i] end
- local slp = 10
- local lx=30
- local ly=30
- local slot=17
- local pipeItems=false
- local hasChargeStation=false;
- local reverseDir=false
- local dumbyMode=false
- local fuelidx=0;
- local ftbl= {
- {tpd, mdd, turtle.digDown, turtle.detectDown, turtle.compareDown },
- {tpf, mfd, turtle.dig , turtle.detect , turtle.compare },
- {tpu, mud, turtle.digUp, turtle.detectUp, turtle.compareUp }
- }
- local cf=turtle.getFuelLevel()
- target_fuel=cf
- local function selt(sl)
- if dumbyMode and fuelidx>target_fuel then return end
- if slot==sl then return end
- slot=sl
- turtle.select(sl)
- end
- local function saveFuelFile()
- arg[5]=turtle.getFuelLevel()
- local fout =fs.open(initfn,"w")
- fout.writeLine(textutils.serialize(arg))
- print(textutils.serialize(arg))
- fout.close()
- end
- local function loadFuelFile()
- local fin =fs.open(initfn,"r")
- local str=fin.readLine()
- print(str)
- arg=textutils.unserialize(str)
- start_fuel=arg[5]
- fuelidx=start_fuel
- fin.close()
- end
- local function dumbyMove()
- fuelidx= fuelidx-1
- if fuelidx== target_fuel then dumbyMode=false end
- end
- local function dumbyPlace()
- end
- local function dumbyTurn()
- end
- local function digAndThrow(dir)
- if dir== nil then dir=0 end
- local dir2=dir+2
- local full=false;
- local orgslot=slot
- for i=1,16 do
- if turtle.getItemCount(i)==0 then
- selt(i)
- break
- end if(i==16) then
- --print("failed to place block - not critical")
- -- return false
- full = true
- end
- end
- -- if all slots are
- if not ftbl[dir2][3]() then selt(orgslot);return false end
- if not full then
- turtle.drop()
- selt(orgslot)
- end
- return true
- end
- local function trt()
- if dumbyMode then dumbyTurn();return end
- turtle.turnRight()
- end
- local function tlt()
- if dumbyMode then dumbyTurn();return end
- turtle.turnLeft()
- end
- local function mfd()
- if dumbyMode then dumbyMove();return end
- while not turtle.forward() do
- if turtle.getFuelLevel()==0 then return false end
- digAndThrow(0)
- end
- end
- local function mbd()
- if dumbyMode then dumbyMove();return end
- while not turtle.back() do
- if turtle.getFuelLevel()==0 then return false end
- tlt()
- tlt()
- digAndThrow(0)
- tlt()
- tlt()
- end
- return true
- end
- local function mdd()
- if dumbyMode then dumbyMove();return end
- while not turtle.down() do
- if turtle.getFuelLevel()==0 then return false end
- digAndThrow(-1)
- end
- end
- local function mud()
- if dumbyMode then dumbyMove();return end
- while not turtle.up() do
- if turtle.getFuelLevel()==0 then return false end
- digAndThrow(1)
- end
- end
- local function tpf()
- if dumbyMode then dumbyPlace();return end
- while not turtle.place() do
- if fuelidx~= nil and fuelidx==target_fuel then break else digAndThrow(0) end
- end
- end
- local function tpu()
- if dumbyMode then dumbyPlace();return end
- while not turtle.placeUp() do
- if fuelidx~= nil and fuelidx==target_fuel then break else digAndThrow(1) end
- end
- return true
- end
- local function tpd()
- if dumbyMode then dumbyPlace();return end
- while not turtle.placeDown() do
- if fuelidx~= nil and fuelidx==target_fuel then break else digAndThrow(-1) end
- end
- return true
- end
- local function lookForMore(slotn)
- selt(slotn)
- --print("entry slotn= "..slotn)
- for i=1,16 do
- if i~=slotn and turtle.compareTo(i) then
- --print ("found more in slot "..i.."with "..turtle.getItemCount(i).." items")
- --print("got space for "..turtle.getItemSpace(slotn).." more")
- selt(i)
- turtle.transferTo(slotn,math.min(turtle.getItemCount(i),turtle.getItemSpace(slotn)))
- -- print("slotn="..slotn)
- selt(slotn)
- return true
- end
- end
- return false
- end
- --[[
- local function cleanSingleWellState()
- local retval =1
- if turtle.getItemCount(4)==0 and hasChargeStation then
- then selt(4); turtle.dig()
- return
- end
- if turtle.detect() then
- selt(miningwell)
- if (turtle.compare() then
- turtle.dig();turtle.up()
- if turtle.detect() then
- turtle.select(conduit)
- turtle.dig()
- mfd()
- return
- end
- end
- else
- if turtle.getItemCount(conduit)==1 and not lookForMore(conduit) then mud(); return end
- selt(conduit)
- turtle.place()
- end
- selt(conduit)
- if turtle.compare() then
- sleep(slp)
- if not pipeItems and turtle.getItemCount(chest)==0 then
- selt(chest); turtle.digDown();
- end
- mdd();
- selt(miningwell)
- turtle.dig()
- if pipeItems and i<lx then
- if turtle.getItemCount(chest)==1 and not lookForMore(chest) then mud(); return end
- selt(chest)
- tpf()
- end
- mud()
- return
- end
- end
- --]]
- print("Niseg's Mining well script")
- if #args~=4 then
- if #args==0 and fs.exists(initfn) then
- loadFuelFile()
- dumbyMode=true
- print("starting recovery")
- else
- print(shell.getRunningProgram().." <delay> <row length> <rows> <0-1> ")
- print("1 is use transport pipes 0 enderchest" )
- error()
- end
- end
- if arg[1] ~= nil then slp = tonumber(arg[1]) end
- if arg[2] ~= nil then lx = tonumber(arg[2]) end
- if arg[3] ~= nil then ly = tonumber(arg[3]) end
- if arg[4] ~=nil then
- local sel =tonumber(arg[4])
- if sel== 1 or sel==3 then pipeItems=true end
- if sel==3 or sel== 2 then reverseDir=true end
- end
- local wellcount=turtle.getItemCount(miningwell)
- if wellcount>=3 then wellcount =3 else wellcount =1 end
- --if not pipeItems then wellcount=1 end
- if not dumbyMode and turtle.getItemCount(chargestation)>0 then
- print("checking if I have a charge station")
- if not mbd() then print("no fuel");error() end
- selt(chargestation)
- tpf(); sleep(5);
- if(turtle.getFuelLevel()>cf) then hasChargeStation = true end
- turtle.dig()
- mfd()
- end
- if not dumbyMode and wellcount == 1 then
- saveFuelFile()
- local fout=fs.open("startup","w")
- fout.writeLine("shell.run(\""..shell.getRunningProgram().."\")")
- fout.close()
- end
- for j=1,ly do
- print("starting row " ..j)
- local nf=6*lx +wellcount+3;
- cf=turtle.getFuelLevel()
- print("fuel is at "..cf.. " / "..nf)
- while nf>cf do
- if hasChargeStation then
- mbd()
- selt(chargestation)
- tpf()
- while nf>cf do cf=turtle.getFuelLevel(); sleep(10) end
- turtle.dig()
- mfd()
- else
- print(" I need more fuel")
- for k=1,4 do tlt() end
- shell.run("refuel all")
- sleep(5);
- end
- cf=turtle.getFuelLevel()
- end
- if(pipeItems) then
- selt(chest); tpd()
- mbd();selt(conduit); tpf()
- end
- for i=1,lx do
- if wellcount==1 then -- single well
- selt(miningwell)
- tpd()
- mbd()
- if not pipeItems then
- selt(chest)
- tpd()
- end
- if turtle.getItemCount(conduit)==1 and not lookForMore(conduit) then break end
- selt(conduit)
- tpf()
- if not dumbyMode then sleep(slp) end
- if not pipeItems then
- selt(chest)
- turtle.digDown()
- end
- mdd()
- selt(miningwell)
- if not dumbyMode then turtle.dig() end
- if pipeItems and i<lx then
- if turtle.getItemCount(chest)==1 and not lookForMore(chest) then mud();break end
- selt(chest)
- tpf()
- end
- mud()
- else
- if i==1 then
- --mbd()
- mdd()
- end
- selt(miningwell)
- for k=1,3 do
- tlt()
- tpf()
- end
- mud()
- if pipeItems and turtle.getItemCount(chest)==1 and not lookForMore(chest) then print("not enough pipes");error() end
- selt(chest)
- tpd()
- for k=1,3 do
- if turtle.getItemCount(conduit)==1 and not lookForMore(conduit) then print("not enough energy pipes");error() end
- selt(conduit)
- tpf()
- trt()
- end
- mud()
- if turtle.getItemCount(conduit)==1 and not lookForMore(conduit) then print("not enough energy pipes");error() end
- selt(conduit)
- tpd()
- sleep(slp) --don't remember how long
- -- time to clean up
- turtle.digDown()
- mdd()
- for k=1,3 do
- tlt()
- turtle.dig()
- end
- selt(chest)
- turtle.digDown()
- mdd()
- selt(miningwell)
- for k=1,3 do
- turtle.dig()
- trt()
- end
- --trt()
- selt(conduit)-- she'll be right
- tpu()
- mbd()
- if pipeItems then
- selt(chest)
- tpf()
- end
- if i==lx then mud() end
- end
- end
- selt(conduit)
- --- retract the conduits
- local llx=lx+1
- if pipeItems then llx=llx+1 end
- -- while turtle.detect() do
- for i=1,llx do
- if not dumbyMode then turtle.dig() end
- --turtle.dig();
- mfd();
- if not dumbyMode then
- if pipeItems and turtle.detect()then
- selt(chest)
- turtle.digDown()
- -- turtle.digDown()
- selt(conduit)
- end
- end
- end
- if(reverseDir) then trt() else tlt() end
- for k=1,wellcount do
- mbd()
- --tpd()
- while redstone.getInput("top") do
- selt(conduit)
- sleep(1)
- turtle.suckUp()
- while turtle.detectUp() do sleep(0.5) end
- end
- if turtle.getItemCount(conduit)==1 and not lookForMore(conduit) then break end
- if not turtle.compareUp() then tpu() end
- if pipeItems then
- if turtle.getItemCount(chest)==1 and not lookForMore(chest) then break end
- selt(chest)
- tpd()
- selt(conduit)
- end
- end
- if(reverseDir) then tlt() else trt() end
- mbd()
- if turtle.getItemCount(conduit)==1 and not lookForMore(conduit) then break end
- tpf()
- --[[if pipeItems then
- selt(chest)
- tpd()
- selt(conduit)
- end
- --]]
- --[[
- mbd()
- tpf()
- if pipeItems then
- selt(chest)
- tpd()
- selt(conduit)
- end
- --]]
- end
- fs.delete(initfn)
- fs.delete("startup")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement