Advertisement
theinsekt

New mine1

Apr 1st, 2013
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.43 KB | None | 0 0
  1. --[[
  2. This API will contain the result of
  3. trying to optimize and refactor mine1 and mine2
  4. --]]
  5.  
  6. os.loadAPI("theinsekt/turtle3")
  7.  
  8. -- directions
  9. f="f" -- forward
  10. b="b" -- back
  11. u="u" -- up
  12. d="d" -- down
  13. tr="tr" -- turn right
  14. tl="tl" -- turn left
  15.  
  16. -- can't handle negative length
  17. -- usage: goLoopT[dir](length)
  18. goLoopT={
  19.   f=getGoLoop(f),
  20.   b=getGoLoop(b),
  21.   u=getGoLoop(u),
  22.   d=getGoLoop(d),
  23.   tr=getGoLoop(tr),
  24.   tl=getGoLoop(tl),
  25. }
  26.  
  27. --can handle negative length
  28. function goLoop(dir, length)
  29.   if length<0 then
  30.     return -goLoopT[turtle2.inv(dir)](-length)
  31.   else
  32.     return goLoopT[dir](length)
  33.   end
  34. end
  35.  
  36. -- help function used to get go loop in direction dir
  37. function getGoLoop(dir)
  38.   local go=turtle2.getGo(dir)
  39.  
  40.   return function(length)
  41.     local i=0
  42.     while i<length do
  43.       if not go() then
  44.         return i
  45.       end
  46.       i=i+1
  47.     end
  48.     return i
  49.   end
  50.  
  51. end
  52.  
  53. -- usage: digLoopT[dir](length,tries)
  54. digLoopT={
  55.   f=getDigLoop(f),
  56.   u=getDigLoop(u),
  57.   d=getDigLoop(d),
  58.   b=getDigBackLoop(b),
  59. }
  60.  
  61. function getDigBackLoop()
  62.   local goB=getGoLoop("b")
  63.   local digF=getDigLoop("f")
  64.   local turnR=turtle2.getGo("r")
  65.  
  66.   return function(length, tries)
  67.     local length2=goB(length)
  68.     if length2~=length then
  69.       turnR()
  70.       turnR()
  71.       local length3 = length2+digF(length-length2,tries)
  72.       turnR()
  73.       turnR()
  74.       return length3
  75.     end
  76.     return length
  77.   end
  78. end
  79.  
  80. function getDigLoop(dir)
  81.   local detect=turtle2.getDetect(dir)
  82.   local dig=turtle2.getDig(dir)
  83.   local go=turtle2.getGo(dir)
  84.  
  85.   return function(length,tries)
  86.     local i=0
  87.     while i<length do
  88.       if not clearAndGo(detect,dig,go,tries) then
  89.         return i
  90.       end
  91.     end
  92.     return i
  93.   end
  94. end
  95.  
  96. -- tries to clear the space by using the given
  97. -- functions: detect, dig.
  98. -- Returns true if clear, else false
  99. function clear(detect, dig)
  100.   while detect() do
  101.     if not dig() then
  102.       return false
  103.     end --if
  104.   end --while
  105.   return true
  106. end --function
  107.  
  108. -- tries to clear and then go one step
  109. -- using the given function detect, dig, go.
  110. -- Returns true if managed to use the go function,
  111. -- else false.
  112. function clearAndGo(detect,dig,go,tries)
  113.   local i=1
  114.   while true do
  115.     if not clear(detect,dig) then
  116.       return false
  117.     end --if
  118.     if go() then
  119.       return true
  120.     end --if
  121.     if i>=tries then
  122.       return false
  123.     end --if
  124.     i=i+1
  125.   end --while
  126. end --function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement