Advertisement
Vrill

Computercraft: light

Dec 14th, 2013
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.85 KB | None | 0 0
  1. --------------------------------
  2. -- Vril's lighting program v 1.1 --
  3. --------------------------------
  4.  
  5. function isnumber(myvar)
  6.  local num
  7.  
  8.  if type(myvar)=="number" then return true end
  9.  if type(myvar)~="string" then return false end
  10.  num=tonumber(myvar)
  11.  if num~=nil then return true end
  12.  return false
  13. end
  14.  
  15. function unloadAll()
  16.  local i
  17.  turtle.turnRight()
  18.  turtle.turnRight()
  19.  for i=16,1,-1 do
  20.   turtle.select(i)
  21.   turtle.drop()
  22.  end
  23.  turtle.turnLeft()
  24.  turtle.turnLeft()
  25. end
  26.  
  27. function isFull()
  28.  local i
  29.  local num
  30.  for i=1,16 do
  31.   num=turtle.getItemCount(i)
  32.   if num==0 then
  33.    return false
  34.   end
  35.  end
  36.  return true
  37. end
  38.  
  39. function left()
  40.  if tD[1]==1 and tD[2]==0 then
  41.   tD[1]=0
  42.   tD[2]=-1
  43.  elseif tD[1]==0 and tD[2]==-1 then
  44.   tD[1]=-1
  45.   tD[2]=0
  46.  elseif tD[1]==-1 and tD[2]==0 then
  47.   tD[1]=0
  48.   tD[2]=1
  49.  elseif tD[1]==0 and tD[2]==1 then
  50.   tD[1]=1
  51.   tD[2]=0
  52.  end
  53.  turtle.turnLeft()
  54. end
  55.  
  56. function right()
  57.  if tD[1]==1 and tD[2]==0 then
  58.   tD[1]=0
  59.   tD[2]=1
  60.  elseif tD[1]==0 and tD[2]==1 then
  61.   tD[1]=-1
  62.   tD[2]=0
  63.  elseif tD[1]==-1 and tD[2]==0 then
  64.   tD[1]=0
  65.   tD[2]=-1
  66.  elseif tD[1]==0 and tD[2]==-1 then
  67.   tD[1]=1
  68.   tD[2]=0
  69.  end
  70.  turtle.turnRight()
  71. end
  72.  
  73. function turnL(num1,num2)
  74.  while tD[1]~=num1 or tD[2]~=num2 do
  75.   left()
  76.  end
  77. end
  78.  
  79. function turnR(num1,num2)
  80.  while tD[1]~=num1 or tD[2]~=num2 do
  81.   right()
  82.  end
  83. end
  84.  
  85. function toBase()
  86.  local i
  87.  local x=tX
  88.  local y=tY
  89.  local can_move=true
  90.  
  91.  for i=tZ,0 do
  92.   tZ=tZ+1
  93.   turtle.digUp()
  94.   can_move=turtle.up()
  95.    while not can_move do
  96.     print("There is a something on my way!")
  97.     sleep(2)
  98.     turtle.attackUp()
  99.     can_move=turtle.up()
  100.    end
  101.  end
  102.  
  103.  if tX>0 then
  104.   turnL(-1,0)
  105.   for i=1,x do
  106.    turtle.dig()
  107.    can_move=turtle.forward()
  108.    while not can_move do
  109.     print("There is a something on my way!")
  110.     sleep(2)
  111.     turtle.attack()
  112.     can_move=turtle.forward()
  113.    end
  114.    tX=tX-1
  115.   end
  116.  elseif tX<0 then
  117.   turnR(1,0)
  118.   x=-x
  119.   for i=1,x do
  120.    turtle.dig()
  121.    can_move=turtle.forward()
  122.    while not can_move do
  123.     print("There is a something on my way!")
  124.     sleep(2)
  125.     turtle.attack()
  126.     can_move=turtle.forward()
  127.    end
  128.    tX=tX+1
  129.   end
  130.  end
  131.  
  132.  if tY>0 then
  133.   turnL(0,-1)
  134.   for i=1,y do
  135.    turtle.dig()
  136.    can_move=turtle.forward()
  137.    while not can_move do
  138.     print("There is a something on my way!")
  139.     sleep(2)
  140.     turtle.attack()
  141.     can_move=turtle.forward()
  142.    end
  143.    tY=tY-1
  144.   end
  145.  elseif tY<0 then
  146.   turnR(0,1)
  147.   y=-y
  148.   for i=1,y do
  149.    turtle.dig()
  150.    can_move=turtle.forward()
  151.    while not can_move do
  152.     print("There is a something on my way!")
  153.     sleep(2)
  154.     turtle.attack()
  155.     can_move=turtle.forward()
  156.    end
  157.    tY=tY+1
  158.   end
  159.  end
  160.  
  161.  turnR(1,0)
  162.  turtle.digDown()
  163.  can_move=turtle.down()
  164.    while not can_move do
  165.     print("There is a something on my way!")
  166.     sleep(2)
  167.     turtle.attackUp()
  168.     can_move=turtle.down()
  169.    end
  170.  tZ=tZ-1
  171. end
  172.  
  173. function toWork(x,y,z,d)
  174.  local i,steepX,steepY
  175.  local can_move=true
  176.  
  177.  turtle.digUp()
  178.  can_move=turtle.up()
  179.    while not can_move do
  180.     print("There is a something on my way!")
  181.     sleep(2)
  182.     turtle.attackUp()
  183.     can_move=turtle.up()
  184.    end
  185.  tZ=tZ+1
  186.  
  187.  if x>tX then
  188.   steepX=1
  189.  else
  190.   steepX=-1
  191.  end
  192.  if y>tY then
  193.   steepY=1
  194.  else
  195.   steepY=-1
  196.  end
  197.  
  198.  turnR(steepX,0)
  199.  while tX~=x do
  200.   turtle.dig()
  201.   can_move=turtle.forward()
  202.    while not can_move do
  203.     print("There is a something on my way!")
  204.     sleep(2)
  205.     turtle.attack()
  206.     can_move=turtle.forward()
  207.    end
  208.   tX=tX+steepX
  209.  end
  210.  turnL(0,steepY)
  211.  while tY~=y do
  212.   turtle.dig()
  213.   can_move=turtle.forward()
  214.    while not can_move do
  215.     print("There is a something on my way!")
  216.     sleep(2)
  217.     turtle.attack()
  218.     can_move=turtle.forward()
  219.    end
  220.   tY=tY+steepY
  221.  end
  222.  
  223.  turnR(d[1],d[2])
  224.  
  225.  for i=tZ-1,z,-1 do
  226.   turtle.digDown()
  227.   can_move=turtle.down()
  228.    while not can_move do
  229.     print("There is a something on my way!")
  230.     sleep(2)
  231.     turtle.attackDown()
  232.     can_move=turtle.down()
  233.    end
  234.   tZ=tZ-1
  235.  end
  236. end
  237.  
  238. function moveDig(n)
  239.  local i
  240.  local x,y,z,d
  241.  local can_move=true
  242.  d={}
  243.  
  244.  if n<0 then
  245.   n=-n
  246.   turtle.turnLeft()
  247.   turtle.turnLeft()
  248.   for i=1,n do
  249.    -----------work break start
  250.    if isFull() then
  251.     x,y,z,d[1],d[2]=tX,tY,tZ,tD[1],tD[2]
  252.     toBase()   
  253.     unloadAll()
  254.     toWork(x,y,z,d)
  255.    end
  256.    -----------work break end
  257.    turtle.dig()
  258.    turtle.suck()
  259.    can_move=turtle.forward()
  260.    while not can_move do
  261.     print("There is a something on my way!")
  262.     sleep(2)
  263.     turtle.attack()
  264.     can_move=turtle.forward()
  265.    end
  266.    tX=tX-tD[1]
  267.    tY=tY-tD[2]
  268.   end
  269.   turtle.turnLeft()
  270.   turtle.turnLeft()
  271.  else
  272.   for i=1,n do
  273.    -----------work break start
  274.    if isFull() then
  275.     x,y,z,d[1],d[2]=tX,tY,tZ,tD[1],tD[2]
  276.     toBase()   
  277.     unloadAll()
  278.     toWork(x,y,z,d)
  279.    end
  280.    -----------work break end
  281.    turtle.dig()
  282.    turtle.suck()
  283.    can_move=turtle.forward()
  284.    while not can_move do
  285.     print("There is a something on my way!")
  286.     sleep(2)
  287.     turtle.attack()
  288.     can_move=turtle.forward()
  289.    end
  290.    tX=tX+tD[1]
  291.    tY=tY+tD[2]
  292.   end
  293.  end
  294. end
  295.  
  296. function moveDigUp(n)
  297.  local i
  298.  local can_move=true
  299.  
  300.  if n<0 then
  301.   n=-n
  302.   for i=1,n do
  303.    turtle.digDown()
  304.    can_move=turtle.down()
  305.    while not can_move do
  306.     print("There is a something on my way!")
  307.     sleep(2)
  308.     turtle.attackDown()
  309.     can_move=turtle.down()
  310.    end
  311.    tZ=tZ-1
  312.   end
  313.  else
  314.   for i=1,n do
  315.    turtle.digUp()
  316.    can_move=turtle.up()
  317.    while not can_move do
  318.     print("There is a something on my way!")
  319.     sleep(2)
  320.     turtle.attackUp()
  321.     can_move=turtle.up()
  322.    end
  323.    tZ=tZ+1
  324.   end
  325.  end
  326. end
  327.  
  328. --------------------------------
  329.  
  330. --global variables:
  331. tX,tY,tZ=0,0,0
  332. tD={1,0}
  333.  
  334. --Locals:
  335. local i
  336. local l_cnt=0
  337. local light=1
  338. local args={...}
  339. local input
  340.  
  341. if #args <1 or not isnumber(args[1]) then
  342.  print("Usage: light <depth> [<interval>]")
  343.  print("  <depth> - the depth.")
  344.  print("  [<interval>] - (optional)Set the light block every <interval> blocks.")
  345.  print("  slots 1,2 - main block/light block.")
  346.  print("  Example: 'light 20 4' - digging")
  347.  print("20 blocks down,")
  348.  print("setting light block every 4 blocks")
  349.  print("Attention! Beware of bedrock!")
  350.  return
  351. end
  352.  
  353. input = tonumber(args[1])
  354. light = input+1
  355. print("*'light' prog. is active!")
  356. print("  *Label: ",os.getComputerLabel())
  357. print("  *Fuel Level:",turtle.getFuelLevel())
  358.  
  359. if args[2]~=nil and isnumber(args[2]) then
  360.  light = tonumber(args[2])
  361.  if light<1 then light=1 end
  362. end
  363.  
  364. print("Start digging ",input," blocks down.")
  365.  
  366. moveDig(1)
  367. right()
  368. right()
  369.  
  370. for i=1,input do
  371.  moveDigUp(-1)
  372.  turtle.dig()
  373. end
  374.  
  375. for i=1,input do
  376.  l_cnt=l_cnt+1
  377.  if l_cnt >= light then
  378.   l_cnt=0
  379.    turtle.select(2)
  380.    turtle.place()
  381.  else
  382.   turtle.select(1)
  383.   turtle.place()
  384.  end
  385.  
  386.  moveDigUp(1)
  387. end
  388.  
  389. moveDig(1)
  390. right()
  391. right()
  392.  
  393. print("Light application quits!")
  394. print("  *Fuel Level:",turtle.getFuelLevel())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement