Advertisement
Vrill

Computercraft: tunnel

Dec 14th, 2013
800
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.69 KB | None | 0 0
  1. --[[
  2. ####################################
  3. ------------------------------------
  4. -- Vril's tunneling program v 1.6 --
  5. ------------------------------------
  6. ####################################
  7. --]]
  8. function isnumber(myvar)
  9.  local num
  10.  
  11.  if type(myvar)=="number" then return true end
  12.  if type(myvar)~="string" then return false end
  13.  num=tonumber(myvar)
  14.  if num~=nil then return true end
  15.  return false
  16. end
  17.  
  18. function forceDigFront()
  19.  while turtle.detect() do
  20.   turtle.dig()
  21.   sleep(0.2)
  22.  end
  23. end
  24.  
  25. function forceDigUp()
  26.  while turtle.detectUp() do
  27.   turtle.digUp()
  28.   sleep(0.5)
  29.  end
  30. end
  31.  
  32. function forceMoveForward()
  33.  local can_move=true
  34.  can_move=turtle.forward()
  35.  while not can_move do
  36.   print("There is a something on my way!")
  37.   sleep(2)
  38.   --
  39.   turtle.dig()
  40.   turtle.attack()
  41.   --
  42.   can_move=turtle.forward()
  43.  end
  44. end
  45.  
  46. function forceMoveBack()
  47.  local can_move=true
  48.  can_move=turtle.back()
  49.  while not can_move do
  50.   print("There is a something on my way!")
  51.   sleep(2)
  52.   can_move=turtle.back()
  53.  end
  54. end
  55.  
  56. function forceMoveUp()
  57.  local can_move=true
  58.  can_move=turtle.up()
  59.  while not can_move do
  60.   print("There is a something on my way!")
  61.   sleep(2)
  62.   --
  63.   turtle.digUp()
  64.   turtle.attackUp()
  65.   --
  66.   can_move=turtle.up()
  67.  end
  68. end
  69.  
  70. function forceMoveDown()
  71.  local can_move=true
  72.  can_move=turtle.down()
  73.  while not can_move do
  74.   print("There is a something on my way!")
  75.   sleep(2)
  76.   --
  77.   turtle.digDown()
  78.   turtle.attackDown()
  79.   --
  80.   can_move=turtle.down()
  81.  end
  82. end
  83.  
  84. function unloadAll()
  85.  local i
  86.  local dropcnt=0
  87.  
  88.  turtle.turnRight()
  89.  turtle.turnRight()
  90.  for i=1,16 do
  91.   dropcnt=turtle.getItemCount(i)
  92.   if i>9 then
  93.    if dropcnt > 0 then
  94.     turtle.select(i)
  95.     turtle.drop()
  96.    end
  97.   elseif not slots[i] then
  98.    if dropcnt > 0 then
  99.     turtle.select(i)
  100.     turtle.drop()
  101.    end
  102.   end
  103.  end
  104.  turtle.turnLeft()
  105.  turtle.turnLeft()
  106. end
  107.  
  108. function isFull()
  109.  local i
  110.  local num
  111.  for i=1,16 do
  112.   num=turtle.getItemCount(i)
  113.   if num==0 then
  114.    return false
  115.   end
  116.  end
  117.  return true
  118. end
  119.  
  120. function left()
  121.  if tD[1]==1 and tD[2]==0 then
  122.   tD[1]=0
  123.   tD[2]=-1
  124.  elseif tD[1]==0 and tD[2]==-1 then
  125.   tD[1]=-1
  126.   tD[2]=0
  127.  elseif tD[1]==-1 and tD[2]==0 then
  128.   tD[1]=0
  129.   tD[2]=1
  130.  elseif tD[1]==0 and tD[2]==1 then
  131.   tD[1]=1
  132.   tD[2]=0
  133.  end
  134.  turtle.turnLeft()
  135. end
  136.  
  137. function right()
  138.  if tD[1]==1 and tD[2]==0 then
  139.   tD[1]=0
  140.   tD[2]=1
  141.  elseif tD[1]==0 and tD[2]==1 then
  142.   tD[1]=-1
  143.   tD[2]=0
  144.  elseif tD[1]==-1 and tD[2]==0 then
  145.   tD[1]=0
  146.   tD[2]=-1
  147.  elseif tD[1]==0 and tD[2]==-1 then
  148.   tD[1]=1
  149.   tD[2]=0
  150.  end
  151.  turtle.turnRight()
  152. end
  153.  
  154. function turnL(num1,num2)
  155.  while tD[1]~=num1 or tD[2]~=num2 do
  156.   left()
  157.  end
  158. end
  159.  
  160. function turnR(num1,num2)
  161.  while tD[1]~=num1 or tD[2]~=num2 do
  162.   right()
  163.  end
  164. end
  165.  
  166. function toBase()
  167.  local i
  168.  local x=tX
  169.  local y=tY
  170.  local z=tZ
  171.  
  172.  if y<0 then
  173.   turnL(0,1)
  174.   for i=1,-y,1 do
  175.    forceMoveForward()
  176.   end
  177.  elseif y>0 then
  178.   turnL(0,-1)
  179.   for i=1,y,1 do
  180.    forceMoveForward()
  181.   end
  182.  end
  183.  
  184.  turnR(-1,0)
  185.  if stairs == -1 and turtleFloor == 1 then
  186.   forceMoveUp()
  187.  end
  188.  for i=1,x,1 do
  189.   forceMoveForward()
  190.   if stairs == 1 then
  191.    forceMoveDown()
  192.    z=z-1
  193.   elseif stairs == -1 then
  194.    forceMoveUp()
  195.    z=z+1
  196.   end
  197.  end
  198.  if stairs == -1 and turtleFloor == 1 then
  199.   forceMoveDown()
  200.  end
  201.  
  202.  if z<0 then
  203.   for i=1,-z,1 do
  204.    forceMoveUp()
  205.   end
  206.  elseif z>0 then
  207.   for i=1,z,1 do
  208.    forceMoveDown()
  209.   end
  210.  end
  211.  
  212.  turnR(1,0)
  213. end
  214.  
  215. function toWork(x,y,z,d)
  216.  local i
  217.  
  218.  if stairs == 0 then
  219.   forceMoveUp()
  220.   forceMoveUp()
  221.   z=z-2
  222.  end
  223.  
  224.  turnR(1,0)
  225.  for i=1,x,1 do
  226.   forceMoveForward()
  227.   if stairs == -1 and z ~= 0 then
  228.    forceMoveDown()
  229.    z=z+1
  230.   elseif stairs == 1 then
  231.    forceMoveUp()
  232.    z=z-1
  233.   end
  234.  end
  235.  
  236.  if y<0 then
  237.   turnR(0,-1)
  238.   for i=1,-y,1 do
  239.    forceMoveForward()
  240.   end
  241.  elseif y>0 then
  242.   turnR(0,1)
  243.   for i=1,y,1 do
  244.    forceMoveForward()
  245.   end
  246.  end
  247.  
  248.  if z<0 then
  249.   for i=1,-z,1 do
  250.    forceMoveDown()
  251.   end
  252.  elseif z>0 then
  253.   for i=1,z,1 do
  254.    forceMoveUp()
  255.   end
  256.  end
  257.  
  258.  turnL(d[1],d[2])
  259. end
  260.  
  261. function moveDig(n)
  262.  local i
  263.  local x,y,z,d
  264.  local d={}
  265.  
  266.  if n<0 then
  267.   n=-n
  268.   turtle.turnLeft()
  269.   turtle.turnLeft()
  270.   for i=1,n do
  271.    -----------work break start
  272.    if isFull() then
  273.    
  274.     x,y,z,d[1],d[2]=tX,tY,tZ,tD[1],tD[2]
  275.    
  276.     if stairs == 1 and turtleFloor == 1 then
  277.      forceMoveUp()
  278.     end
  279.     toBase()   
  280.     if stairs == 1 and turtleFloor == 1 then
  281.      forceMoveDown()
  282.     end
  283.    
  284.     unloadAll()
  285.     if stairs == -1 and turtleFloor ~= 3 then
  286.      forceMoveUp()
  287.     end
  288.     toWork(x,y,z,d)
  289.     if stairs == -1 and turtleFloor ~= 3 then
  290.      forceMoveDown()
  291.     end
  292.    
  293.    end
  294.    -----------work break end
  295.    forceDigFront()
  296.    turtle.suck()
  297.    forceMoveForward()
  298.    tX=tX-tD[1]
  299.    tY=tY-tD[2]
  300.   end
  301.   turtle.turnLeft()
  302.   turtle.turnLeft()
  303.  else
  304.   for i=1,n do
  305.    -----------work break start
  306.    if isFull() then
  307.    
  308.     x,y,z,d[1],d[2]=tX,tY,tZ,tD[1],tD[2]
  309.    
  310.     if stairs == 1 and turtleFloor == 1 then
  311.      forceMoveUp()
  312.     end
  313.     toBase()   
  314.     if stairs == 1 and turtleFloor == 1 then
  315.      forceMoveDown()
  316.     end
  317.    
  318.     unloadAll()
  319.     if stairs == -1 and turtleFloor ~= 3 then
  320.      forceMoveUp()
  321.     end
  322.     toWork(x,y,z,d)
  323.     if stairs == -1 and turtleFloor ~= 3 then
  324.      forceMoveDown()
  325.     end
  326.    
  327.    end
  328.    -----------work break end
  329.    forceDigFront()
  330.    turtle.suck()
  331.    forceMoveForward()
  332.    tX=tX+tD[1]
  333.    tY=tY+tD[2]
  334.   end
  335.  end
  336. end
  337.  
  338. function moveDigUp(n)
  339.  local i
  340.  
  341.  if n<0 then
  342.   n=-n
  343.   for i=1,n do
  344.    turtle.digDown()
  345.    turtle.suckDown()
  346.    forceMoveDown()
  347.    tZ=tZ-1
  348.   end
  349.  else
  350.   for i=1,n do
  351.    forceDigUp()
  352.    turtle.suckUp()
  353.    forceMoveUp()
  354.    tZ=tZ+1
  355.   end
  356.  end
  357. end
  358.  
  359. function makeBung()
  360.  local i
  361.  turtle.select(9)
  362.  
  363. --s v1.6
  364.  if stairs == 1 then
  365.   moveDigUp(-1)
  366.   left()
  367.   left()
  368.   moveDig(1)
  369.   right()
  370.   right()
  371.  end
  372. --s v1.6
  373.  
  374.  forceDigFront()
  375.  turtle.place()
  376.  left()
  377.  moveDig(1)
  378.  right()
  379.  
  380.  forceDigFront()
  381.  turtle.place()
  382.  left()
  383.  moveDig(1)
  384.  right()
  385.  
  386.  forceDigFront()
  387.  turtle.place()
  388.  
  389.  moveDigUp(-1)
  390.  
  391.  forceDigFront()
  392.  turtle.place()
  393.  right()
  394.  moveDig(1)
  395.  left()
  396.  
  397.  forceDigFront()
  398.  turtle.place()
  399.  right()
  400.  moveDig(1)
  401.  left()
  402.  
  403.  forceDigFront()
  404.  turtle.place()
  405.  
  406.  moveDigUp(-1)
  407.  
  408.  forceDigFront()
  409.  turtle.place()
  410.  left()
  411.  moveDig(1)
  412.  right()
  413.  
  414.  forceDigFront()
  415.  turtle.place()
  416.  left()
  417.  moveDig(1)
  418.  right()
  419.  
  420.  forceDigFront()
  421.  turtle.place()
  422.  
  423.  right()
  424.  moveDig(2)
  425.  moveDigUp(2)
  426.  left()
  427. end
  428. ------------------------------------
  429.  
  430. --global variables:
  431. tX,tY,tZ=0,0,0
  432. tD={1,0}
  433. slots={false,false,false,false,false,false,false,false,false}
  434. stairs = 0
  435. turtleFloor = 3
  436.  
  437. --Locals:
  438. local dist = 0
  439. local light = 0
  440. local i
  441. local l_cnt = 0
  442. local args={...}
  443.  
  444. if #args <1 or not isnumber(args[1]) then
  445.  print("Usage: tunnel <distance> [<interval>]")
  446.  print("  <distance> - length of the tunnel.")
  447.  print("  [<interval>] - (optional)Set the light block every <interval> blocks.")
  448.  print("  slots 1,2,3,4 - blocks for left wall/right wall/ceiling/floor.")
  449.  print("  slots 5,6,7,8 - light blocks(left/right/ceiling/floor).")
  450.  print("  Example1: 'tunnel 20 4' - turtle will")
  451.  print("dig tunnel with length 20")
  452.  print("setting light every 4 blocks.")
  453.  print("  Example2: 'tunnel 20 4 st' - stairs up")
  454.  print("  Example3: 'tunnel 20 4 -st' - stairs down")
  455.  return
  456. end
  457.  
  458. print("*'tunneling' prog. is active!")
  459. print("  *Label: ",os.getComputerLabel())
  460. print("  *Fuel Level:",turtle.getFuelLevel())
  461.  
  462. for i=1,9 do
  463.  if turtle.getItemCount(i) > 0 then
  464.   slots[i]=true
  465.  else
  466.   slots[i]=false
  467.  end
  468. end
  469.  
  470. dist = tonumber(args[1])
  471. if dist<1 then dist=1 end
  472. light=dist+1
  473. if args[2]~=nil and isnumber(args[2]) then
  474.  light = tonumber(args[2])
  475.  if light<1 then light=1 end
  476. else
  477.  for i=5,8 do slots[i]=false end
  478. end
  479.  
  480. print("Start digging ",dist," blocks front. ")
  481. if args[3]~=nil then
  482.  if args[3]=="st" then
  483.   stairs=1
  484.   print("Stairs up.")
  485.  end
  486.  if args[3]=="-st" then
  487.   stairs=-1
  488.   print("Stairs down.")
  489.  end
  490. end
  491. if args[2]~=nil then
  492.  if args[2]=="st" then
  493.   stairs=1
  494.   print("Stairs up.")
  495.  end
  496.  if args[2]=="-st" then
  497.   stairs=-1
  498.   print("Stairs down.")
  499.  end
  500. end
  501.  
  502. moveDigUp(2)
  503. turtleFloor = 3 -- <== oO
  504. right()
  505. moveDig(1)
  506. left()
  507.  
  508. l_cnt = light-1
  509. for i=1,dist do
  510.  l_cnt=l_cnt+1
  511.  
  512.  if i==1 then moveDig(1)
  513.  else
  514.   if stairs~=1 then
  515.    moveDig(1)
  516.   end
  517.  end
  518.  
  519.  right()
  520.  forceDigUp()
  521.  forceDigFront()
  522.  if slots[2] then
  523.   turtle.select(2)
  524.   turtle.place()
  525.  end
  526.  if slots[3] then
  527.   turtle.select(3)
  528.   turtle.placeUp()  
  529.  end
  530.  left()
  531.  left()
  532.  moveDig(1)
  533.  forceDigUp()
  534.  if l_cnt >= light then
  535.   if slots[7] then
  536.    turtle.select(7)
  537.    turtle.placeUp()
  538.   elseif slots[3] then
  539.    turtle.select(3)
  540.    turtle.placeUp()
  541.   end
  542.  else
  543.   if slots[3] then
  544.    turtle.select(3)
  545.    turtle.placeUp()  
  546.   end
  547.  end
  548.  moveDig(1)
  549.  forceDigUp()
  550.  forceDigFront()
  551.  if slots[1] then
  552.   turtle.select(1)
  553.   turtle.place()
  554.  end
  555.  if slots[3] then
  556.   turtle.select(3)
  557.   turtle.placeUp()  
  558.  end
  559.  moveDigUp(-1)
  560.  turtleFloor = 2 -- <== oO
  561.  forceDigFront()
  562.  if l_cnt >= light then
  563.   if slots[5] then
  564.    turtle.select(5)
  565.    turtle.place()
  566.   elseif slots[1] then
  567.    turtle.select(1)
  568.    turtle.place()  
  569.   end
  570.  else
  571.   if slots[1] then
  572.    turtle.select(1)
  573.    turtle.place()  
  574.   end
  575.  end
  576.  right()
  577.  right()
  578.  moveDig(2)
  579.  forceDigFront()
  580.  if l_cnt >= light then
  581.   if slots[6] then
  582.    turtle.select(6)
  583.    turtle.place()
  584.   elseif slots[2] then
  585.    turtle.select(2)
  586.    turtle.place()  
  587.   end
  588.  else
  589.   if slots[2] then
  590.    turtle.select(2)
  591.    turtle.place()  
  592.   end
  593.  end
  594.  moveDigUp(-1)
  595.  turtleFloor = 1 -- <== oO
  596.  forceDigFront()
  597.  turtle.digDown()
  598.  if slots[2] then
  599.   turtle.select(2)
  600.   turtle.place()  
  601.  end
  602.  if slots[4] then
  603.   turtle.select(4)
  604.   turtle.placeDown()
  605.  end
  606.  left()
  607.  left()
  608.  moveDig(1)
  609.  turtle.digDown()
  610.  if l_cnt >= light then
  611.   l_cnt=0
  612.   if slots[8] then
  613.    turtle.select(8)
  614.    turtle.placeDown()
  615.   elseif slots[4] then
  616.    turtle.select(4)
  617.    turtle.placeDown()  
  618.   end
  619.  else
  620.   if slots[4] then
  621.    turtle.select(4)
  622.    turtle.placeDown()
  623.   end
  624.  end
  625.  moveDig(1)
  626.  forceDigFront()
  627.  turtle.digDown()
  628.  if slots[4] then
  629.   turtle.select(4)
  630.   turtle.placeDown()
  631.  end
  632.  if slots[1] then
  633.   turtle.select(1)
  634.   turtle.place()  
  635.  end
  636.  right()
  637.  right()
  638.  moveDig(2)
  639.  left()
  640.  if stairs~=1 then
  641.   moveDigUp(2+stairs)
  642.  else
  643.   moveDigUp(2)
  644.   moveDig(1)
  645.   moveDigUp(1)
  646.  end
  647.  turtleFloor = 3 -- <== oO
  648. end
  649.  
  650. if stairs == -1 then
  651.  moveDigUp(1)
  652. end
  653.  
  654. --s v1.3
  655. if slots[9] then
  656.  makeBung()
  657. end
  658. --e v1.3
  659.  
  660. if stairs == 1 then
  661.  moveDigUp(-1)
  662. end
  663.  
  664. toBase()
  665. for i=1,9 do
  666.  slots[i]=false
  667. end
  668. unloadAll()
  669. print("  *Tunneling application quits!")
  670. print("  *Fuel level=",turtle.getFuelLevel())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement