Advertisement
theinsekt

mineVein

Feb 22nd, 2013
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.77 KB | None | 0 0
  1. -- API
  2. -- help functions to implement mining of veins.
  3.  
  4. --args={...}
  5. os.loadAPI("theinsekt/turtle2")
  6. os.loadAPI("theinsekt/mine2")
  7. os.loadAPI("theinsekt/position") -- will be used to set a radius
  8.  
  9.  
  10.  
  11. -- check for vein in the given direction and mines it if valuable
  12. -- returns true if has returned to starting position or
  13. -- hasn't moved from starting position
  14. -- this is because wanna avoid runaway turtle.
  15. -- slots are a 1 indexed array that contains
  16. -- the item slots that will be used to compare
  17. -- if the ore in the given direction is a valuable ore.
  18. function mineVein(dir,slots, is_good,limit)
  19.   -- if it's not a block it can't be a vein
  20.   if not turtle2.detect(dir) then
  21.     return true
  22.   end
  23.  
  24.   -- check limit
  25.   if position.whatIfLargest(dir,1)>limit then
  26.     return true
  27.   end
  28.  
  29.   --print("checking ore...")
  30.   if isValuable(dir,slots,is_good) then
  31.     turtle.select(1)
  32.     --print("valuable!")
  33.     if mine2.digLoop(dir,1)~=1 then
  34.       -- could not mine
  35.       -- but is at starting position
  36.       return true
  37.     end
  38.     position.update(dir,1) --test
  39.     --print("got it!")
  40.     if not mineVeinR(slots,is_good,limit) then
  41.       -- other could not go back
  42.       return false
  43.     end
  44.     if mine2.digLoop(dir,-1)~=-1 then
  45.       -- could not go back
  46.       print("can't go back, aborting!")
  47.       return false
  48.     end
  49.     position.update(dir,-1) --test
  50.   else
  51.     --print("crap!")
  52.   end
  53.   -- no problems
  54.   return true  
  55. end
  56.  
  57.  
  58.  
  59. -- check for vein in all directions
  60. function mineVeinR(slots,is_good,limit)
  61.   local i=0
  62.   while i<4 do
  63.     if not mineVein("f",slots,is_good,limit) then
  64.       return false
  65.     end
  66.     mine2.turn("r",1)
  67.     position.update("r",1) --test
  68.     i=i+1
  69.   end
  70.   if not mineVein("u",slots,is_good,limit) then
  71.     return false
  72.   end
  73.   if not mineVein("d",slots,is_good,limit) then
  74.     return false
  75.   end
  76.   return true
  77. end
  78.  
  79.  
  80.  
  81. function isValuable(dir, slots, is_good)
  82.   if same(dir,slots) then
  83.     return is_good
  84.   else
  85.     return not is_good
  86.   end
  87. end
  88.  
  89.  
  90.  
  91. function same(dir, slots)
  92.   local compare=turtle2.getCompare(dir)
  93.   for index, slot in ipairs(slots) do
  94.     turtle.select(slot)
  95.     if compare() then
  96.       return true
  97.     end
  98.   end
  99.   return false
  100. end
  101.  
  102.  
  103.  
  104. function slotsA()
  105.   local slots={}
  106.   local index=1
  107.   local i=1
  108.   while i<=16 do
  109.     if turtle.getItemCount(i)==0 then
  110.       break
  111.     end
  112.     slots[index]=i
  113.     index=index+1
  114.     i=i+1
  115.   end
  116.   return slots
  117. end
  118.    
  119.  
  120. --[[commented out
  121. -- tests
  122. local slots=slotsA()
  123. same("f", slots)
  124.  
  125. --position.update("f",1)
  126. --local x,y,z,rot=position.get()
  127. --print(x.." "..y." "..z.." "..rot)
  128. if #args==1 then
  129.   local dir=args[1]
  130.   print("starting mineVein")
  131.   position.reset()
  132.   print(mineVein(dir,slots,true,20))
  133.   print("finished")
  134. end
  135. ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement