Advertisement
theinsekt

mine1

Feb 20th, 2013
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.12 KB | None | 0 0
  1. -- help functions for mining
  2.  
  3.  
  4. -- uses the functions negative and positive
  5. -- to turn in a negative or positive direction.
  6. -- negative could be turtle.turnLeft
  7. -- positive could be turtle.turnRight
  8. -- length is an integer
  9. function turn(positive, negative,length)
  10.   local tmp=length%4
  11.   if tmp==1 then
  12.     positive()
  13.   elseif tmp==3 then
  14.     negative()
  15.   elseif tmp==2 then
  16.     -- bonus, turns in the given direction
  17.     if length<0 then
  18.       negative()
  19.       negative()
  20.     else
  21.       positive()
  22.       positive()
  23.     end --else
  24.   end --else
  25.   return length
  26. end --function      
  27.  
  28.  
  29. function goLoop(go, length)
  30.   local i=0
  31.   while i<length do
  32.     if not go() then
  33.       break
  34.     end --if
  35.     i=i+1
  36.   end --while
  37.   return i
  38. end --function
  39.  
  40.  
  41.  
  42. --[[
  43. example use:
  44. digLoop(turtle.detect, turtle.dig,turtle.forward, 3, 20)
  45. where the turtle functions are all in the same direction
  46. -- Returns the distance traveled, which
  47. -- is equal to length if it was completely successful
  48. -- If length was 0 then only tries to dig.
  49. ]]
  50. function digLoop(detect,dig,go,length,tries)
  51. -- special case: only dig if length==0
  52.   if length==0 then
  53.     if clear(detect,dig) then
  54.       return 0
  55.     else
  56.       return -1
  57.     end --else
  58.   end --if
  59. -- dig and go
  60.   local i=0
  61.   while i<length do
  62.     if not clearAndGo(detect,dig,go,tries) then
  63.       return i
  64.     end --if
  65.     i=i+1
  66.   end --while
  67.   return i
  68. end --function
  69.  
  70. -- tries to clear the space by using the given
  71. -- function detect, dig.
  72. -- Returns true if clear, else false
  73. function clear(detect, dig)
  74.   while detect() do
  75.     if not dig() then
  76.       return false
  77.     end --if
  78.   end --while
  79.   return true
  80. end --function
  81.  
  82. -- tries to clear and then go one step
  83. -- using the given function detect, dig, go.
  84. -- Returns true if managed to use the go function,
  85. -- else false.
  86. function clearAndGo(detect,dig,go,tries)
  87.   local i=1
  88.   while true do
  89.     if not clear(detect,dig) then
  90.       return false
  91.     end --if
  92.     if go() then
  93.       return true
  94.     end --if
  95.     if i>=tries then
  96.       return false
  97.     end --if
  98.     i=i+1
  99.   end --while
  100. end --function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement