Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local y = fs.open("startup","w")
- y.write("shell.run(\"adex\")")
- y.close()
- function ask(napis,oneKey)
- term.clear()
- term.setCursorPos(1,1)
- term.write(napis)
- term.setCursorPos(1,2)
- local answer = ""
- local x,answerPos = term.getCursorPos()
- local nEvent,nKey = os.pullEvent()
- while nKey == keys.enter do
- nEvent,nKey = os.pullEvent()
- end
- if oneKey then
- return nKey,nEvent
- else
- answer = read()
- return answer
- end
- end
- if fs.exists("TEMP") then
- shell.run("TEMP")
- os.loadAPI("TEMP")
- else
- depth = 0
- xPos,zPos = 0,0
- xDir,zDir = 1,0
- a = {
- size = tonumber(ask("Podaj wielkosc wykopu:")),
- proX = 0,
- proY = 0,
- proZ = 0,
- ores = 0,
- bedrock = false,
- moveX = tonumber(ask("Podaj przesuniecie x:")),
- moveY = tonumber(ask("Podaj przesuniecie y:")),
- moveZ = tonumber(ask("Podaj przesuniecie z:")),
- }
- a.proX = a.moveX
- a.proZ = a.moveZ
- end
- local goTo -- Filled in further down
- local refuel -- Filled in further down
- function save()
- local temp = fs.open("TEMP","w")
- temp.write("\nxPos="..tostring(xPos))
- temp.write("\nzPos="..tostring(zPos))
- temp.write("\ndepth="..tostring(depth))
- temp.write("\nxDir,zDir="..tostring(xDir)..","..tostring(zDir).."\n")
- temp.write("a="..textutils.serialize(a))
- temp.close()
- end
- local function unload( _bKeepOneFuelStack )
- print( "Unloading items..." )
- for n=1,16 do
- local nCount = turtle.getItemCount(n)
- if nCount > 0 then
- turtle.select(n)
- local bDrop = true
- if _bKeepOneFuelStack and turtle.refuel(0) then
- bDrop = false
- _bKeepOneFuelStack = false
- end
- if bDrop then
- turtle.drop()
- end
- end
- end
- collected = 0
- turtle.select(1)
- refuel()
- end
- local function returnSupplies()
- local x,y,z,xd,zd = xPos,depth,zPos,xDir,zDir
- print( "Returning to surface..." )
- goTo( 0,0,0,-1,0 )
- local fuelNeeded = 12*(x+y+z) + 1
- if not refuel( fuelNeeded ) then
- unload( true )
- print( "Waiting for fuel" )
- while not refuel( fuelNeeded ) do
- os.pullEvent( "turtle_inventory" )
- end
- else
- unload( true )
- end
- print( "Resuming mining..." )
- end
- local function collect()
- local bFull = true
- local nTotalItems = 0
- for n=1,16 do
- local nCount = turtle.getItemCount(n)
- if nCount == 0 then
- bFull = false
- end
- nTotalItems = nTotalItems + nCount
- end
- if nTotalItems > collected then
- collected = nTotalItems
- if math.fmod(collected + unloaded, 50) == 0 then
- print( "Mined "..(collected + unloaded).." items." )
- end
- end
- if bFull then
- print( "No empty slots left." )
- return false
- end
- return true
- end
- function refuel( ammount )
- local fuelLevel = turtle.getFuelLevel()
- if fuelLevel == "unlimited" then
- return true
- end
- local needed = 19000
- if turtle.getFuelLevel() < needed then
- local fueled = false
- turtle.select(1)
- while turtle.refuel(1) do
- if turtle.placeUp() then
- if turtle.getFuelLevel() >= needed then
- return true
- end
- else
- return false
- end
- end
- return false
- end
- return true
- end
- local function checkBed()
- local bUp,nUp = turtle.inspectUp()
- local bDown,nDown = turtle.inspectDown()
- local bFor,nFor = turtle.inspect()
- if bFor then
- if string.find(string.lower(nFor.name),"bedrock") then
- a.bedrock = true
- return true
- else
- a.proY = a.proY-1
- end
- end
- return false
- end
- local function tryUp()
- while not turtle.up() do
- if not turtle.digUp() then
- if turtle.detect() then
- if checkBed() then
- return false
- end
- else
- sleep(0.5)
- end
- end
- end
- depth = depth + 1
- save()
- return true
- end
- local function tryForwards()
- while not turtle.forward() do
- if not turtle.dig() then
- if turtle.detect() then
- if checkBed() then
- bedrock = true
- a.proY = a.proY + 1
- end
- if not tryUp() then
- return false
- end
- end
- if turtle.attack() or turtle.attackUp() then
- turtle.suck()
- sleep( 0.5 )
- end
- end
- end
- xPos = xPos + xDir
- zPos = zPos + zDir
- save()
- return true
- end
- local function tryDown()
- while not turtle.down() do
- if not turtle.digDown() then
- if turtle.detectDown() then
- if not a.bedrock then
- print("Found bedbrock!Finishing...")
- a.bedrock=true
- end
- a.proY=a.proY+1
- save()
- return false
- end
- end
- end
- depth = depth - 1
- save()
- return true
- end
- local function turnLeft()
- turtle.turnLeft()
- xDir, zDir = -zDir, xDir
- save()
- end
- local function turnRight()
- turtle.turnRight()
- xDir, zDir = zDir, -xDir
- save()
- end
- function goTo( x, y, z, xd, zd)
- while x~=xPos or y~=depth or z~=zPos or bedrock do
- if zPos > z then
- while zDir ~= -1 do
- turnLeft()
- end
- while zPos > z do
- tryForwards()
- end
- elseif zPos < z then
- while zDir ~= 1 do
- turnLeft()
- end
- while zPos < z do
- tryForwards()
- end
- end
- if xPos > x then
- while xDir ~= -1 do
- turnLeft()
- end
- while xPos > x do
- tryForwards()
- end
- elseif xPos < x then
- while xDir ~= 1 do
- turnLeft()
- end
- while xPos < x do
- tryForwards()
- end
- end
- while depth ~=y do
- if depth > y then
- if not tryDown() then
- tryForwards()
- end
- else
- if not tryUp() then
- tryForwards()
- end
- end
- end
- end
- while zDir ~= zd or xDir ~= xd do
- if xDir==zd and zDir==-xd then
- turnLeft()
- else
- turnRight()
- end
- end
- end
- function checkFull()
- if turtle.getItemCount(16)~=0 then
- returnSupplies()
- goTo(0,a.proY+a.moveY,0,1,0)
- end
- term.clear()
- term.setCursorPos(1,1)
- print("exp XYZ: ",a.proX," ",a.proY," ",a.proZ)
- print("moje XYZ: ",xPos," ",depth," ",zPos)
- end
- if not turtle.inspect then
- turtle.inspectUp = function()
- turtle.digUp()
- return false
- end
- turtle.inspectDown = function()
- turtle.digDown()
- return false
- end
- end
- function checkOre()
- local function inspect()
- local bUp,Up = turtle.inspectUp()
- local bDown,Down = turtle.inspectDown()
- if bUp then
- if string.find(string.lower(Up.name),"ore") then
- turtle.digUp()
- a.ores=a.ores+1
- end
- end
- if bDown then
- if string.find(string.lower(Down.name),"ore") then
- turtle.digDown()
- a.ores=a.ores+1
- end
- end
- end
- local function mustdig()
- while true do
- if not turtle.dig() then
- sleep(1)
- end
- end
- end
- parallel.waitForAny(inspect,mustdig)
- end
- print("excavating...")
- if xPos == 0 and zPos == 0 and depth ==0 then
- refuel(1)
- end
- checkFull()
- if a.proY+a.moveY~=depth then
- goTo(xPos,a.proY+a.moveY,zPos,xDir,zDir)
- end
- while true do
- while a.proZ-a.moveZ < a.size do
- while a.proX-a.moveX < a.size do
- if math.fmod(a.proY,6)==0 then
- if math.fmod(a.proZ,2)==0 then
- goTo(a.proX,a.proY+a.moveY,a.proZ,1,0)
- else
- goTo(a.size-a.proX-1,a.proY+a.moveY,a.proZ,-1,0)
- end
- else
- if math.fmod(a.proZ,2)==0 then
- goTo(a.size-a.proX-1,a.proY+a.moveY,a.size-a.proZ-1,-1,0)
- else
- goTo(a.proX,a.proY+a.moveY,a.size-a.proZ-1,1,0)
- end
- end
- checkOre()
- checkFull()
- a.proX = a.proX + 1
- save()
- end
- a.proX = a.moveX
- a.proZ = a.proZ+1
- save()
- end
- a.proZ = a.moveZ
- a.proY = a.proY-3
- if a.bedrock then
- break
- end
- save()
- end
- goTo(0,0,0,-1,0)
- unload(false)
- print("Found "..a.ores.." ores total")
- fs.delete("startup")
- fs.delete("TEMP")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement