Don't like ads? PRO users don't see any ads ;-)
Guest

MiningT

By: a guest on Aug 8th, 2012  |  syntax: None  |  size: 16.94 KB  |  hits: 8  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. -- Min coords:
  2. minX = 0
  3. minY = 0 -- Must be a number divisible by 2!
  4. minZ = 0
  5. --Max coords:
  6. maxX = 5
  7. maxY = 4 -- Must be a number divisible by 2!
  8. maxZ = 5
  9.  
  10. -- Start / Cargo drop:
  11. homeX=0
  12. homeY=0
  13. homeZ=-2
  14. -- Slots config
  15. sums = {}
  16. sums[9]=1
  17. sums[8]=1
  18. sums[7]=1
  19. sums[6]=2
  20. sums[5]=1 --
  21. sums[4]=2
  22. sums[3]=1 --
  23. sums[2]=2
  24. sums[1]=1 --
  25.  
  26. -- Do not change anything below this comment or something could go wrong.
  27.  
  28. local tArgs = { ... }
  29. term.clear()
  30. term.setCursorPos(1,1)
  31. print("   ---  Mining program  ---")
  32.  
  33. sums[0]=0
  34. finaliza = false
  35. help = false
  36.  
  37. function setInicial()
  38.                 -- MiddlePass coords
  39.                 partX = homeX
  40.                 partY = homeY
  41.                 partZ = homeZ + 2
  42.                 if minZ>homeZ then
  43.                                 partZ = minZ
  44.                 end
  45.                 -- Current coords:
  46.                 x = homeX
  47.                 y = homeY
  48.                 z = homeZ
  49.                 lado = 1 -- Starting direction. 0=l 1=f 2=r 3=b
  50.                 -- last mining coords / mining taring coords:
  51.                 xw = minX
  52.                 zw = minZ
  53.                 yw = minY
  54.                 if math.fmod(minY,4) ~= 0 then
  55.                                 xw = maxX
  56.                                 zw = maxZ
  57.                 end
  58.                 ladow = 1
  59. end
  60.  
  61. function readAndSetArg(num)
  62.                 if #tArgs >=num+3 then
  63.                                 if tonumber(tArgs[num+1]) ~= nil and tonumber(tArgs[num+2]) ~= nil and tonumber(tArgs[num+3]) ~= nil then
  64.                                                 if tArgs[num] == "h" then
  65.                                                                 homeX=tonumber(tArgs[num+1])
  66.                                                                 homeY=tonumber(tArgs[num+2])
  67.                                                                 homeZ=tonumber(tArgs[num+3])
  68.                                                                 print("Read home position:  x"..homeX.." y"..homeY.." z"..homeZ)
  69.                                                 elseif tArgs[num] == "s" then
  70.                                                                 minX=tonumber(tArgs[num+1])
  71.                                                                 minY=tonumber(tArgs[num+2])
  72.                                                                 minZ=tonumber(tArgs[num+3])
  73.                                                                 print("Read start position: x"..minX.." y"..minY.." z"..minZ)
  74.                                                 elseif tArgs[num] == "f" then
  75.                                                                 maxX=tonumber(tArgs[num+1])
  76.                                                                 maxY=tonumber(tArgs[num+2])
  77.                                                                 maxZ=tonumber(tArgs[num+3])
  78.                                                                 print("Read final position: x"..maxX.." y"..maxY.." z"..maxZ)
  79.                                                 end
  80.                                 end
  81.                 end
  82. end
  83. function readArgs()
  84. -- Reads the arguments
  85.                 if #tArgs > 0 then
  86.                                 if tArgs[1] == "help" or tArgs[1] == "Help" then
  87.                                                 print("Usage: miner [help]")
  88.                                                 print("Usage: miner [h X Y Z] [s X Y Z] [f X Y Z]")
  89.                                                 print()
  90.                                                 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.")
  91.                                                 print()
  92.                                                 print("Example:")
  93.                                                 print(" miner h 0 0 -1 s -2 0 0 f 2 4 2")
  94.                                                 help = true
  95.                                 elseif #tArgs >= 4 then
  96.                                                 readAndSetArg(1)
  97.                                                 readAndSetArg(5)
  98.                                                 readAndSetArg(9)
  99.                                 end
  100.                 end
  101.                                                 setInicial()
  102. end
  103.  
  104. function askStarting()
  105.                 print()
  106.                 print("Home position:   x"..homeX.." y"..homeY.." z"..homeZ)
  107.                 print("Start position:  x"..minX.." y"..minY.." z"..minZ)
  108.                 print("Finish position: x"..maxX.." y"..maxY.." z"..maxZ)
  109.                 print()
  110.                 print("Are these numbrers correct? [S/Y/N]")
  111.                 while true do
  112.                                 event, param1 = os.pullEvent()
  113.                                 if event == "char" then
  114.                                                 if param1 == "S" or param1 == "s" or param1 == "Y" or param1 == "y"  then
  115.                                                                 print("Ok! Turtle starting...")
  116.                                                                 return true
  117.                                                 elseif param1 == "N" or param1 == "n" then
  118.                                                                 print("Try again!")
  119.                                                                 return false
  120.                                                 end
  121.                                 elseif event == "key" and param1==28 then
  122.                                                 print("Ok! Turtle starting...")
  123.                                                 return true
  124.                                 end
  125.                 end
  126.                 return false
  127. end
  128. function gira(l)
  129. -- Receive an absolute directon and does the propiate turns
  130.                 local resta = math.abs(lado -l)
  131.                 while lado ~= l do
  132.                                 if (resta > 2 and lado < l) or (resta <= 2 and lado > l) then
  133.                                                 turtle.turnLeft()
  134.                                                 lado = lado-1
  135.                                 else  
  136.                                                 turtle.turnRight()
  137.                                                 lado = lado+1
  138.                                 end
  139.                                 if lado>3 then
  140.                                                 lado=0
  141.                                 elseif lado<0 then
  142.                                                 lado=3
  143.                                 end
  144.                 end
  145. end
  146. function movX(tox)
  147.                 -- Moves the turle to tox in X axix
  148.                 if x > tox then
  149.                                 gira(0)
  150.                 elseif x < tox then
  151.                                 gira(2)
  152.                 end
  153.                 while x ~= tox do
  154.                                 while turtle.detect() do
  155.                                                 turtle.dig()
  156.                                 end
  157.                                 turtle.forward()
  158.                                 if lado == 0 then
  159.                                                 x=x-1
  160.                                 else
  161.                                                 x=x+1
  162.                                 end
  163.                 end
  164. end
  165. function movY(toy)
  166.                 -- Moves the turle to toy in Y axix
  167.                 while y < toy do
  168.                                 if turtle.detectDown() then
  169.                                                 turtle.digDown()
  170.                                 end
  171.                                                 turtle.down()
  172.                                                 y=y+1;
  173.                 end
  174.                 while y > toy do
  175.                                 while turtle.detectUp() do
  176.                                                 turtle.digUp()
  177.                                 end
  178.                                 if turtle.up() then
  179.                                                 y=y-1;
  180.                                 else
  181.                                                 finaliza=true
  182.                                                 break;
  183.                                 end
  184.                 end
  185. end
  186. function movZ(toz)
  187.                 -- Moves the turle to toz in Z axix
  188.                 if z > toz then
  189.                                 gira(3)
  190.                 elseif z < toz then
  191.                                 gira(1)
  192.                 end
  193.                 while z ~= toz do
  194.                                 while turtle.detect() do
  195.                                                 turtle.dig()
  196.                                 end
  197.                                 turtle.forward()
  198.                                 if lado == 1 then
  199.                                                 z=z+1
  200.                                 else
  201.                                                 z=z-1
  202.                                 end
  203.                 end
  204. end
  205. function mueveCords(tox,toy,toz,dir)
  206.                 -- Moves the turtle to the spcified coords mining everithing in its way.
  207.                 modY = 0
  208.                 if dir == "home" then
  209.                                 if y-2 >= toy and y-2 >= minY then
  210.                                                 modY=2
  211.                                                 movY(y-modY)
  212.                                 end
  213.                                 -- x:
  214.                                 movX(tox)
  215.                                 -- z:
  216.                                 movZ(toz)
  217.                                 -- y:
  218.                                 movY(toy)
  219.                 else
  220.                                 if y <= toy-2 and toy-2 >= minY then
  221.                                                 modY=2;
  222.                                 end
  223.                                 movY(toy-modY)
  224.                                 -- z:
  225.                                 movZ(toz)
  226.                                 -- x:
  227.                                 movX(tox)
  228.                                 -- y:
  229.                                 movY(toy)
  230.                 end
  231. end
  232. function checkInventario()
  233. -- Checks the slots. If some slot is full, it will go home to drop everihing
  234.                 local tmp=9
  235.                 local lleno=false
  236.                 while tmp >0 do
  237.                                                 if turtle.getItemSpace(tmp) == 0 then
  238.                                                                 turtle.select(tmp)
  239.                                                                 --if turtle.compare() or turtle.compare() then
  240.                                                                                 lleno=true
  241.                                                                                 tmp=0
  242.                                                                 --end
  243.                                                 end
  244.                                                 tmp=tmp-sums[tmp]
  245.                 end
  246.                 if lleno then
  247.                 --rednet.open("Right")
  248.                                 xw=x
  249.                                 yw=y
  250.                                 zw=z
  251.                                 ladow=lado
  252.                                 goHome()
  253.                                 descarga()
  254.                                 goWork()
  255.                 end
  256. end
  257. function cava()
  258. -- mine forward and up
  259.                 while (turtle.detect() or turtle.detectUp()) and not finaliza do
  260.                                 cavado=true
  261.                                 if turtle.detect() then
  262.                                                 if not turtle.dig() then
  263.                                                                 finaliza=true
  264.                                                 end
  265.                                 end
  266.                                 if turtle.detectUp() then
  267.                                                 if not turtle.digUp() then
  268.                                                                 finaliza=true
  269.                                                 end
  270.                                 end
  271.                 end
  272. end
  273. function workX(num)
  274.                 -- mine in X axis
  275.                 gira(num+1)
  276.                 local fin = false
  277.                 while not fin and not finaliza do
  278.                                 if (x > minX and num==-1) or (x < maxX and num==1) then
  279.                                                 checkInventario()
  280.                                                 cava()
  281.                                                 if turtle.forward() then
  282.                                                                 x=x+num
  283.                                                 else
  284.                                                                 finaliza=true
  285.                                                 end
  286.                                 else
  287.                                                 fin=true
  288.                                 end
  289.                 end
  290. end
  291. function workZ(num)
  292.                 -- mine a entire level
  293.                 local fin = false
  294.                 while not fin and not finaliza do
  295.                                 if math.fmod(z,2) == 0 then
  296.                                                 workX(1*num)
  297.                                 else
  298.                                                 workX(-1*num)
  299.                                 end
  300.                                 if (z > minZ and num==-1) or (z < maxZ and num == 1) then
  301.                                                 gira(2-num)
  302.                                                 cava()
  303.                                                 if turtle.forward() then
  304.                                                                 z = z+num
  305.                                                 else
  306.                                                                 finaliza=true
  307.                                                 end
  308.                                 else
  309.                                                 fin = true
  310.                                 end
  311.                 end
  312. end
  313. function goHome()
  314. -- Go to home coords
  315.                 mueveCords(partX,partY,partZ,"home")
  316.                 mueveCords(homeX,homeY,homeZ,"home")
  317. end
  318. function descarga()
  319.                 -- Drops everithing but 1 block
  320.                 print("Dropping items...")
  321.                 local tmp2=9
  322.                 while tmp2 > 0 do
  323.   turtle.select(tmp2)
  324.                 if turtle.getItemCount(tmp2) > 0 then
  325.                                 turtle.drop(turtle.getItemCount(tmp2)-1)
  326.                                 end
  327.                                 tmp2=tmp2-1
  328.                 end
  329. end
  330. function goWork()
  331.                 -- Moves the turtle to the last mining coordinates
  332.                 mueveCords(partX,partY,partZ,"work")
  333.                 mueveCords(xw,yw,zw,"work")
  334.                 gira(ladow)
  335. end
  336. function mineria()
  337. -- Starts everithing.
  338.                 print("Starting from x:"..minX.." y:"..minY.." z:"..minZ)
  339.                 print("To x:"..maxX.." y:"..maxY.." z:"..maxZ)
  340.                 goWork()
  341.                 checkInventario()
  342.                 while y <= maxY and not finaliza do
  343.                                 if math.fmod(y/2,2) == 0 then
  344.                                                 workZ(1)
  345.                                 else
  346.                                                 workZ(-1)
  347.                                 end
  348.                                 if y < maxY and not finaliza then
  349.                                                 turtle.digUp()
  350.                                                 turtle.digDown()
  351.                                                 if  turtle.down() then
  352.                                                                 y=y+1
  353.                                                 else
  354.                                                                 finaliza=true
  355.                                                 end
  356.                                                 turtle.digUp()
  357.                                                 turtle.digDown()
  358.                                                 if turtle.down() and not finaliza then
  359.                                                                 y=y+1
  360.                                                 else
  361.                                                                 turtle.digUp()
  362.                                                                 finaliza=true
  363.                                                 end
  364.                                 else
  365.                                                 maxY=maxY-1
  366.                                 end
  367.                 end
  368. end
  369.  
  370. readArgs()
  371. if not help then
  372.                 if askStarting() then
  373.                 mineria()
  374.                 if finaliza then
  375.                                 print("Something went wrong...(bedrock?player?mob?)")
  376.                 else
  377.                                 print("Finish!!")
  378.                 end
  379.                 goHome()
  380.                 descarga()
  381.                 gira(1)
  382.                 end
  383. end