- -- Min coords:
- minX = 0
- minY = 0 -- Must be a number divisible by 2!
- minZ = 0
- --Max coords:
- maxX = 5
- maxY = 4 -- Must be a number divisible by 2!
- maxZ = 5
- -- Start / Cargo drop:
- homeX=0
- homeY=0
- homeZ=-2
- -- Slots config
- sums = {}
- sums[9]=1
- sums[8]=1
- sums[7]=1
- sums[6]=2
- sums[5]=1 --
- sums[4]=2
- sums[3]=1 --
- sums[2]=2
- sums[1]=1 --
- -- Do not change anything below this comment or something could go wrong.
- local tArgs = { ... }
- term.clear()
- term.setCursorPos(1,1)
- print(" --- Mining program ---")
- sums[0]=0
- finaliza = false
- help = false
- function setInicial()
- -- MiddlePass coords
- partX = homeX
- partY = homeY
- partZ = homeZ + 2
- if minZ>homeZ then
- partZ = minZ
- end
- -- Current coords:
- x = homeX
- y = homeY
- z = homeZ
- lado = 1 -- Starting direction. 0=l 1=f 2=r 3=b
- -- last mining coords / mining taring coords:
- xw = minX
- zw = minZ
- yw = minY
- if math.fmod(minY,4) ~= 0 then
- xw = maxX
- zw = maxZ
- end
- ladow = 1
- end
- function readAndSetArg(num)
- if #tArgs >=num+3 then
- if tonumber(tArgs[num+1]) ~= nil and tonumber(tArgs[num+2]) ~= nil and tonumber(tArgs[num+3]) ~= nil then
- if tArgs[num] == "h" then
- homeX=tonumber(tArgs[num+1])
- homeY=tonumber(tArgs[num+2])
- homeZ=tonumber(tArgs[num+3])
- print("Read home position: x"..homeX.." y"..homeY.." z"..homeZ)
- elseif tArgs[num] == "s" then
- minX=tonumber(tArgs[num+1])
- minY=tonumber(tArgs[num+2])
- minZ=tonumber(tArgs[num+3])
- print("Read start position: x"..minX.." y"..minY.." z"..minZ)
- elseif tArgs[num] == "f" then
- maxX=tonumber(tArgs[num+1])
- maxY=tonumber(tArgs[num+2])
- maxZ=tonumber(tArgs[num+3])
- print("Read final position: x"..maxX.." y"..maxY.." z"..maxZ)
- end
- end
- end
- end
- function readArgs()
- -- Reads the arguments
- if #tArgs > 0 then
- if tArgs[1] == "help" or tArgs[1] == "Help" then
- print("Usage: miner [help]")
- print("Usage: miner [h X Y Z] [s X Y Z] [f X Y Z]")
- print()
- print("Where 'h' is the home position, 's' is the start position and 'f' is the finish position. And X Y Z are the coordinates.")
- print()
- print("Example:")
- print(" miner h 0 0 -1 s -2 0 0 f 2 4 2")
- help = true
- elseif #tArgs >= 4 then
- readAndSetArg(1)
- readAndSetArg(5)
- readAndSetArg(9)
- end
- end
- setInicial()
- end
- function askStarting()
- print()
- print("Home position: x"..homeX.." y"..homeY.." z"..homeZ)
- print("Start position: x"..minX.." y"..minY.." z"..minZ)
- print("Finish position: x"..maxX.." y"..maxY.." z"..maxZ)
- print()
- print("Are these numbrers correct? [S/Y/N]")
- while true do
- event, param1 = os.pullEvent()
- if event == "char" then
- if param1 == "S" or param1 == "s" or param1 == "Y" or param1 == "y" then
- print("Ok! Turtle starting...")
- return true
- elseif param1 == "N" or param1 == "n" then
- print("Try again!")
- return false
- end
- elseif event == "key" and param1==28 then
- print("Ok! Turtle starting...")
- return true
- end
- end
- return false
- end
- function gira(l)
- -- Receive an absolute directon and does the propiate turns
- local resta = math.abs(lado -l)
- while lado ~= l do
- if (resta > 2 and lado < l) or (resta <= 2 and lado > l) then
- turtle.turnLeft()
- lado = lado-1
- else
- turtle.turnRight()
- lado = lado+1
- end
- if lado>3 then
- lado=0
- elseif lado<0 then
- lado=3
- end
- end
- end
- function movX(tox)
- -- Moves the turle to tox in X axix
- if x > tox then
- gira(0)
- elseif x < tox then
- gira(2)
- end
- while x ~= tox do
- while turtle.detect() do
- turtle.dig()
- end
- turtle.forward()
- if lado == 0 then
- x=x-1
- else
- x=x+1
- end
- end
- end
- function movY(toy)
- -- Moves the turle to toy in Y axix
- while y < toy do
- if turtle.detectDown() then
- turtle.digDown()
- end
- turtle.down()
- y=y+1;
- end
- while y > toy do
- while turtle.detectUp() do
- turtle.digUp()
- end
- if turtle.up() then
- y=y-1;
- else
- finaliza=true
- break;
- end
- end
- end
- function movZ(toz)
- -- Moves the turle to toz in Z axix
- if z > toz then
- gira(3)
- elseif z < toz then
- gira(1)
- end
- while z ~= toz do
- while turtle.detect() do
- turtle.dig()
- end
- turtle.forward()
- if lado == 1 then
- z=z+1
- else
- z=z-1
- end
- end
- end
- function mueveCords(tox,toy,toz,dir)
- -- Moves the turtle to the spcified coords mining everithing in its way.
- modY = 0
- if dir == "home" then
- if y-2 >= toy and y-2 >= minY then
- modY=2
- movY(y-modY)
- end
- -- x:
- movX(tox)
- -- z:
- movZ(toz)
- -- y:
- movY(toy)
- else
- if y <= toy-2 and toy-2 >= minY then
- modY=2;
- end
- movY(toy-modY)
- -- z:
- movZ(toz)
- -- x:
- movX(tox)
- -- y:
- movY(toy)
- end
- end
- function checkInventario()
- -- Checks the slots. If some slot is full, it will go home to drop everihing
- local tmp=9
- local lleno=false
- while tmp >0 do
- if turtle.getItemSpace(tmp) == 0 then
- turtle.select(tmp)
- --if turtle.compare() or turtle.compare() then
- lleno=true
- tmp=0
- --end
- end
- tmp=tmp-sums[tmp]
- end
- if lleno then
- --rednet.open("Right")
- xw=x
- yw=y
- zw=z
- ladow=lado
- goHome()
- descarga()
- goWork()
- end
- end
- function cava()
- -- mine forward and up
- while (turtle.detect() or turtle.detectUp()) and not finaliza do
- cavado=true
- if turtle.detect() then
- if not turtle.dig() then
- finaliza=true
- end
- end
- if turtle.detectUp() then
- if not turtle.digUp() then
- finaliza=true
- end
- end
- end
- end
- function workX(num)
- -- mine in X axis
- gira(num+1)
- local fin = false
- while not fin and not finaliza do
- if (x > minX and num==-1) or (x < maxX and num==1) then
- checkInventario()
- cava()
- if turtle.forward() then
- x=x+num
- else
- finaliza=true
- end
- else
- fin=true
- end
- end
- end
- function workZ(num)
- -- mine a entire level
- local fin = false
- while not fin and not finaliza do
- if math.fmod(z,2) == 0 then
- workX(1*num)
- else
- workX(-1*num)
- end
- if (z > minZ and num==-1) or (z < maxZ and num == 1) then
- gira(2-num)
- cava()
- if turtle.forward() then
- z = z+num
- else
- finaliza=true
- end
- else
- fin = true
- end
- end
- end
- function goHome()
- -- Go to home coords
- mueveCords(partX,partY,partZ,"home")
- mueveCords(homeX,homeY,homeZ,"home")
- end
- function descarga()
- -- Drops everithing but 1 block
- print("Dropping items...")
- local tmp2=9
- while tmp2 > 0 do
- turtle.select(tmp2)
- if turtle.getItemCount(tmp2) > 0 then
- turtle.drop(turtle.getItemCount(tmp2)-1)
- end
- tmp2=tmp2-1
- end
- end
- function goWork()
- -- Moves the turtle to the last mining coordinates
- mueveCords(partX,partY,partZ,"work")
- mueveCords(xw,yw,zw,"work")
- gira(ladow)
- end
- function mineria()
- -- Starts everithing.
- print("Starting from x:"..minX.." y:"..minY.." z:"..minZ)
- print("To x:"..maxX.." y:"..maxY.." z:"..maxZ)
- goWork()
- checkInventario()
- while y <= maxY and not finaliza do
- if math.fmod(y/2,2) == 0 then
- workZ(1)
- else
- workZ(-1)
- end
- if y < maxY and not finaliza then
- turtle.digUp()
- turtle.digDown()
- if turtle.down() then
- y=y+1
- else
- finaliza=true
- end
- turtle.digUp()
- turtle.digDown()
- if turtle.down() and not finaliza then
- y=y+1
- else
- turtle.digUp()
- finaliza=true
- end
- else
- maxY=maxY-1
- end
- end
- end
- readArgs()
- if not help then
- if askStarting() then
- mineria()
- if finaliza then
- print("Something went wrong...(bedrock?player?mob?)")
- else
- print("Finish!!")
- end
- goHome()
- descarga()
- gira(1)
- end
- end