Niseg

new borehole test 3

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