Niseg

bore hole miner experiment 2

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