Niseg

recursive bore hole

Mar 21st, 2013
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.40 KB | None | 0 0
  1. -- in early development
  2. local tun_number=1
  3. local num_black_list=4;
  4. local num_white_list=4;
  5. local reportArray={}
  6. local depth=0;
  7. local inventory_cur={};
  8. local inventory_prv={};
  9. local reserve_slot=0;
  10. local max_nensting =5
  11. function bullyForward()
  12. while not turtle.forward() do turtle.dig() end
  13. end
  14. function bullyUp()
  15. while not turtle.up() do turtle.digUp() end
  16. end
  17. function checkInventory(dump)
  18. local cnt;
  19. local lim=0;
  20. dump = dump or false
  21. if(dump or turtle.getItemCount(16)==0) then return end
  22. if(turtle.detectDown()) then turtle.digDown() end
  23. for i=reserve_slot+1,num_black_list do
  24. cnt=turtle.getItemCount(i)
  25. if(cnt>30) then turtle.select(i);turtle.drop(cnt-1) end
  26. end
  27. if(reserve_slot==1) then
  28. turtle.select(reserve_slot)
  29. turtle.placeDown()
  30.  
  31. for i=num_black_list+1 ,num_black_list+num_white_list do
  32. --for i=num_black_list+1 ,16 do
  33. cnt=turtle.getItemCount(i)
  34. if(cnt>0) then turtle.select(i);turtle.dropDown(cnt-1) end
  35. end
  36. end
  37.  
  38. for i=(num_black_list+num_white_list+1),16 do
  39. turtle.select(i);turtle.dropDown();
  40. end
  41.  
  42. turtle.select(1)
  43.  
  44. if(reserve_slot==1) then turtle.digDown() end
  45.  
  46. end
  47.  
  48. local function scanInventory(inventory)
  49. local i;
  50.     for i=1,16 do
  51.         inventory[i]=turtle.getItemCount(i);
  52.         if(inventory[i]==nil) then inventory[i]=0 end;
  53.     end
  54. end
  55. local function compareInventory(cur,prv)
  56.     for i=1,16 do
  57.         if cur[i]>prv[i] then
  58.         return i;
  59.         end
  60.     end
  61. return 0;
  62. end
  63. local function copyBackInventory()
  64. for j=1,16 do inventory_prv[j]=inventory_cur[j] end;
  65. end
  66. function reportFind(item)
  67. print(item," ",tun_number,",",depth)
  68. end
  69. function handleBlackList(change)
  70. if change==0 then  return end
  71. if change==reserve_slot then return end
  72. if(inventory_cur[change]>32) then
  73. turtle.select(change)
  74. turtle.drop(inventory_cur[change]-1);
  75. inventory_cur[change]=1
  76. turtle.select(1)
  77. end
  78. end
  79. function boreHoleScan(nesting)
  80. local dag=false;
  81. local lim=num_black_list+num_white_list
  82. local found=false;
  83. local i=0
  84. local nest=nesting
  85. print("n=",nest)
  86. if(nest >max_nensting) then return end
  87. checkInventory();
  88.  
  89. if(nest>0) then
  90. copyBackInventory()
  91. while(turtle.detect()) do dag=turtle.dig(); end
  92.     if(dag) then
  93.         scanInventory(inventory_cur)
  94.         change=compareInventory(inventory_cur,inventory_prv)
  95.  
  96.     if(change>num_black_list) then
  97.     bullyForward() ;boreHoleScan(nest+1); turtle.back();
  98.     else
  99.     handleBlackList(change);
  100.     end
  101. end
  102.  
  103. end
  104. nest=nest+1;
  105. copyBackInventory()
  106. dag=false
  107. while(turtle.detectUp()) do dag=turtle.digUp(); end
  108.  
  109. if(dag) then
  110.     scanInventory(inventory_cur)
  111.     change=compareInventory(inventory_cur,inventory_prv)
  112.  
  113. if(change>num_black_list) then
  114. bullyUp();boreHoleScan(nest); turtle.down();
  115.     else
  116.     handleBlackList(change);
  117. end
  118. end
  119. copyBackInventory()
  120. dag=false
  121. while(turtle.detectDown()) do dag=turtle.digDown() end
  122. if(dag) then
  123.     scanInventory(inventory_cur)
  124.     change=compareInventory(inventory_cur,inventory_prv)
  125.  
  126. if(change>num_black_list ) then
  127. turtle.down(); boreHoleScan(nest); bullyUp();
  128. else
  129.     handleBlackList(change);
  130. end
  131.  
  132. end
  133. turtle.turnLeft()
  134. copyBackInventory()
  135. dag=false
  136. while(turtle.detect()) do dag=turtle.dig() ;  end
  137. if(dag ) then
  138.     scanInventory(inventory_cur)
  139.     change=compareInventory(inventory_cur,inventory_prv)
  140.  
  141. if(change>num_black_list) then
  142. bullyForward() ;boreHoleScan(nest); turtle.back();
  143.     else
  144.     handleBlackList(change);
  145. end
  146. end
  147. turtle.turnRight()
  148. turtle.turnRight()
  149. copyBackInventory()
  150. dag=false
  151. while(turtle.detect()) do dag=turtle.dig() ;  end
  152. if(dag ) then
  153.     scanInventory(inventory_cur)
  154.     change=compareInventory(inventory_cur,inventory_prv)
  155.  
  156. if(change>num_black_list) then
  157. bullyForward() ;boreHoleScan(nest); turtle.back();
  158. else
  159.     handleBlackList(change);
  160. end
  161. end
  162. turtle.turnLeft()
  163.  
  164. end
  165.  
  166.  
  167. function boreHoleMine(length,direction)
  168. local change;
  169. local dag;
  170. local moved;
  171. print( "bhm len=",length ,"dir =", direction);
  172. for i=1,length do
  173. copyBackInventory()
  174. while(turtle.detect()) do dag=turtle.dig() ; sleep(0.5) end
  175. if(dag) then
  176.     scanInventory(inventory_cur)
  177.     change=compareInventory(inventory_cur,inventory_prv)
  178. end
  179. moved=false;
  180. bullyForward()
  181. depth=depth+ direction;
  182. --if(change>num_black_list)  then reportFind( depth) end
  183. boreHoleScan(0)
  184. print("tunnel = ",i)
  185. end
  186. end
  187. local num=0
  188. local len=0
  189. local lwl=0;
  190. print("number of tunnels?")
  191. num=tonumber(read())
  192. print("length of tunnels?")
  193. len=tonumber(read())
  194. print("last black list(BL) item(1-16) ?")
  195. num_black_list=tonumber(read())
  196. num_white_list=0
  197. print("recursion depth - should be minimal like 2-3")
  198. max_nensting=tonumber(read())
  199. print("is there an ender chest in slot 1 (0=no,1=yes)?")
  200.  
  201. reserve_slot=tonumber(read())
  202. turtle.select(1)
  203. if(reserve_slot~=0 and reserve_slot~=1) then reserve_slot=1 end
  204. --print("number of  white list items(after  BL) ?")
  205. --num_white_list=tonumber(read())
  206.  
  207.     scanInventory(inventory_cur);
  208.         scanInventory(inventory_prv);
  209. for i=num_black_list+1,16 do
  210.     if(turtle.getItemCount(i)==0) then break else num_white_list=num_white_list+1;  end
  211. end
  212. turtle.select(1)
  213. local dir=1
  214. num=num+num%2
  215. for i=1,num do
  216. boreHoleMine(len,dir)
  217. checkInventory( true )
  218. if(dir>0) then turtle.turnLeft() else turtle.turnRight(); end
  219. boreHoleMine(3,dir)
  220. if(dir>0) then turtle.turnLeft() else turtle.turnRight(); end
  221. tun_number=tun_number+1
  222. if(dir>0) then bullyUp() else turtle.digDown();turtle.down(); end
  223. dir=dir*-1
  224.  
  225.  
  226. end
  227.  
  228. --for i=1,reportArray.getn() do print (reportArray[i][0]," ", reportArray[i][1],",",reportArray[i][0]) end
Advertisement
Add Comment
Please, Sign In to add comment