Guest User

A Computercraft program

a guest
Dec 13th, 2013
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.78 KB | None | 0 0
  1. local dist = 0 --unused
  2. local x = 0
  3. local y = 0
  4. local height = 0
  5. local inc_x = 1
  6. local inc_y = 1
  7. local thickness = 1
  8. local direction = 0
  9. local inc = 1
  10. local ignores = 0
  11.  
  12. while tonumber(ignores)<=0 do --ignore value definition
  13.  print("please indicate how many slots should be ignore markers, cannot be less than 1")
  14.  ignores = read()
  15. end
  16. local ignore = tonumber(ignores)
  17. term.clear()
  18.  
  19. function mineOre() --modified vein miner by GOPHER ATL
  20. local function doMine(digFunc, moveFunc, backFunc, compareFunc, detectFunc)
  21.   if not smartcompare(compareFunc,detectFunc) then
  22.     return
  23.   end
  24.   digFunc()    
  25.   moveFunc()
  26.  
  27.   for i=1,4 do
  28.     goLeft()
  29.     if turtle.detect() then
  30.       doMine(turtle.dig,digforw,turtle.back,turtle.compare, turtle.detect)
  31.     end
  32.   end    
  33.  
  34.   if turtle.detectUp() then
  35.     doMine(digup, turtle.up, turtle.down, turtle.compareUp, turtle.detectUp)                              
  36.   end
  37.  
  38.   if turtle.detectDown() then
  39.     doMine(digdown, turtle.down, turtle.up, turtle.compareDown, turtle.detectDown)
  40.   end
  41.  
  42.   backFunc()
  43.   return  
  44. end
  45.  doMine(turtle.dig,digforw,turtle.back, turtle.compare, turtle.detect)
  46. end
  47.  
  48. function bridge() --unused
  49.  if turtle.detectDown()~=true then
  50.   turtle.select(2)
  51.   turtle.placeDown()
  52.  end
  53. end
  54.  
  55. function dump(pos) --dump items to ender chest in slot 1
  56. if pos==Up then
  57.  if turtle.getItemCount(16)>0 then
  58.   turtle.digUp()
  59.   turtle.select(1)
  60.   turtle.placeUp()
  61.   for slotn = 4, ignore do
  62.   for slot = 3+ignore,16,1 do
  63.    turtle.select(slot)
  64.    if turtle.compareTo(slotn) then
  65.     turtle.dropDown()
  66.    end
  67.    turtle.dropUp()
  68.   end
  69.   end
  70.   turtle.select(1)
  71.   turtle.digUp()
  72.  end
  73. elseif pos==Down then
  74.  if turtle.getItemCount(16)>0 then
  75.   turtle.digDown()
  76.   turtle.select(1)
  77.   turtle.placeDown()
  78.   for slotn = 4, ignore do
  79.   for slot = 3+ignore,16 do
  80.    turtle.select(slot)
  81.    if turtle.compareTo(slotn) then
  82.     turtle.dropUp()
  83.    turtle.dropDown()
  84.    end
  85.   end
  86.   turtle.select(1)
  87.   turtle.digDown()
  88.   end
  89.   end
  90.  end
  91. end
  92.  
  93. function digforw() --anti-gravel and mob forward digging
  94.  
  95.   while turtle.detect() do
  96.     turtle.dig()
  97.   end
  98.   while not turtle.forward() do
  99.    turtle.attack()
  100.   end
  101.   dist = dist + 1
  102.   record()
  103.   display()
  104. end
  105.  
  106. function goback() --anti-mob backwards digging
  107.  while not turtle.back() do
  108.   attackBack()
  109.   end
  110.   record(backrecorder)
  111.   display()
  112. end
  113.  
  114. function attackBack() --for goback()
  115.  local d = true
  116.   turtle.turnLeft()
  117.   turtle.turnLeft()
  118.   while d == true do
  119.    d = turtle.attack()
  120.    sleep(0.5)
  121.   end
  122.   turtle.turnLeft()
  123.   turtle.turnLeft()
  124. end
  125.  
  126. function smartcompare(compareFunc, detectFunc) --compare block to items in slot 4-ignore value
  127. local memory = 0
  128.  for cslot = 4, 3 + ignore do
  129.   turtle.select(cslot)
  130.   if (not compareFunc(cslot))and detectFunc() then
  131.    memory = memory + 1
  132.   else
  133.    print("false")
  134.    return false
  135.   end
  136.   if memory >= ignore then
  137.    print(ignore)
  138.    print("true")
  139.    return true
  140.   end
  141.  end
  142. end
  143.  
  144. function check() --analyze the block, if not recognized then dig the vein
  145.   if smartcompare(turtle.compare, turtle.detect) then
  146.   print("mining vein...")
  147.    mineOre()
  148.    sleep(1)
  149.    return true
  150.   else
  151.    return false
  152.   end
  153. end
  154.  
  155. function checkOre(digDirFunc) --turn around checking for ores
  156.  digDirFunc()
  157.  local count = 0
  158.  for dir = 1, 4 do
  159.    if check() then
  160.     count = count + 1
  161.    end
  162.    goLeft()
  163.  end
  164.  if count > 0 then
  165.   return true
  166.  else
  167.   return false
  168.  end
  169. end
  170.  
  171. function checkforw() --check and go forward at the same time
  172.  digforw()
  173.   for dir = 1, 4 do
  174.    check()
  175.    goLeft()
  176.   end
  177.   local recoverDown = 0
  178.   while smartcompare(turtle.compareDown, turtle.detectDown) do
  179.    checkOre(digdown)
  180.    recoverDown = recoverDown + 1
  181.   end
  182.  for l = 1, recoverDown do
  183.   digup()
  184.  end
  185.   digup()
  186.   for dir = 1, 4 do
  187.    check()
  188.    goLeft()
  189.   end
  190.   while smartcompare(turtle.compareUp, turtle.detectUp) do
  191.    checkOre(digup)
  192.   end
  193.   digdown()
  194. end
  195.  
  196. function goRight() --turn right while registering the direction
  197.  turtle.turnRight()
  198.  if direction < 3 then
  199.   direction = direction + 1
  200.  else
  201.   direction = 0
  202.  end
  203. end
  204.  
  205. function goLeft() --turn left while reg. the direction
  206.  turtle.turnLeft()
  207.  if direction > 0 then
  208.   direction = direction - 1
  209.  else
  210.   direction = 3
  211.  end
  212. end
  213.  
  214. function digup() --anti-gravel and mob upward dig
  215.  while turtle.detectUp() do
  216.   turtle.digUp()
  217.   sleep(0.4)
  218.  end
  219.  while not turtle.up() do
  220.   turtle.attackUp()
  221.  end
  222.  dump("Down")
  223. end
  224.  
  225. function digdown() --anti-gravel and mob downward dig
  226.  while turtle.detectDown() do
  227.   turtle.digDown()
  228.  end
  229.  while not turtle.down() do
  230.   turtle.attackDown()
  231.  end
  232.  dump("Up")
  233. end
  234.  
  235. function torch() --unused
  236.  if dist >= 12 then
  237.   turtle.select(2)
  238.    if turtle.detectUp() then
  239.     turtle.digUp()
  240.    end
  241.   turtle.placeUp()
  242.   dist = 0
  243.  end
  244. end
  245.  
  246. function refuel() --auto refuel on slot 3
  247.  if turtle.getFuelLevel() <= 512 then
  248.   turtle.select(3)
  249.   turtle.refuel(4)
  250.  end
  251. end
  252.  
  253. function record(recordFunc) --records the coordinates
  254.  if recordFunc == nil then
  255.   recordFunc = recorder
  256.  end
  257.  if direction == 0 then
  258.   inc_y = 1
  259.   inc_x = 0
  260.  elseif direction == 1 then
  261.   inc_y = 0
  262.   inc_x = 1
  263.  elseif direction == 2 then
  264.   inc_y = -1
  265.   inc_x = 0
  266.  elseif direction == 3 then
  267.   inc_y = 0
  268.   inc_x = -1
  269.  end
  270.  recordFunc()
  271. end
  272.  
  273. function recorder() --two different recorders for forwards and backwards
  274.  x = x+inc_x
  275.  y = y+inc_y
  276. end
  277.  
  278. function backrecorder()
  279.  x = x-inc_x
  280.  y = y-inc_y
  281. end
  282.  
  283. function straighten() --faces direction 0
  284. local _direction = direction
  285.  for turn = 1, _direction do
  286.   goLeft()
  287.  end
  288.  direction = 0
  289. end
  290.  
  291. function homeX(target_x) --moves to the targeted x and y
  292.  print("adjusting X to home in")
  293.  straighten()
  294.  if target_x < x then
  295.   goLeft()
  296.  elseif target_x > x then
  297.   goRight()
  298.  end
  299.  for nudgeX = 1, math.abs(target_x - x) do
  300.   digforw()
  301.  end
  302. end
  303.  
  304. function homeY(target_y)
  305.  print("adjusting Y to home in")
  306.  straighten()
  307.  if target_y < y then
  308.   goLeft()
  309.   goLeft()
  310.  end
  311.  for nudgeY = 1, math.abs(target_y - y) do
  312.   digforw()
  313.  end
  314. end
  315.  
  316. --debug
  317. function display() --displays positional data to terminal
  318.  term.clear()
  319.  term.setCursorPos(1,1)
  320.  print("x relative to the starting point is: "..x)
  321.  print("z relative to the starting point is: "..y)
  322.  print("the direction is: "..direction)
  323. end
  324.  
  325. term.clear() --start of the program
  326. term.setCursorPos(2,2)
  327. print("please enter the thickness")
  328. inc = read()
  329. term.clear()
  330. term.setCursorPos(2,2)
  331. print("please enter the radius (the number of circles)")
  332. local diam = read()
  333.  
  334.  
  335.  
  336. local I = tonumber(diam)/2
  337. local i = 0
  338. repeat
  339.  for length = 1,thickness do
  340.   dump("Up")
  341.   checkforw()
  342.  end
  343.  refuel()
  344.  thickness = thickness + math.floor(inc)
  345.  goRight()
  346.  display()
  347.  i = i + 0.25
  348. until i == I
  349. homeX(0)
  350. homeY(0)
  351. dump("Up")
Advertisement
Add Comment
Please, Sign In to add comment