Advertisement
SimLeek

fell

Apr 21st, 2015
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.82 KB | None | 0 0
  1. -- computercraft surface felling
  2.  
  3. args={...}
  4.  
  5. x=0
  6. y=0
  7. x1= tonumber(args[1])
  8. y1= tonumber(args[2])
  9. x2= tonumber(args[3])
  10. y2= tonumber(args[4])
  11.  
  12. if x1==nil or y1==nil then
  13.   print("Usage: fell [x1] [y1] {[x2] [y2]}")
  14.   return
  15. end
  16. if x2==nil or y2==nil then
  17.   x2=0
  18.   y2=0
  19. end
  20.  
  21. w=math.abs(x1-x2)
  22. h=math.abs(y1-y2)
  23.  
  24. print("Requested felling a "..w.."x"..h.." area.")
  25. print("Checking fuel...")
  26. reqFuel=w*h + (w + h)*3
  27.  
  28. if turtle.getFuelLevel() < reqFuel then
  29.   print("I do not have enough fuel.")
  30.   print("I have "..turtle.getFuelLevel().." fuel. But I need "..reqFuel.." fuel.")
  31.   if turtle.getFuelLimit() < reqFuel then
  32.     print("And I can only carry "..turtle.getFuelLimit().." fuel.")
  33.   end
  34.   return
  35. end
  36.  
  37. print("Finding start...")
  38.  
  39. function fellDown()
  40.   while turtle.down() do
  41.   end
  42. end
  43.    
  44. function fell()
  45.   while true do
  46.     r,t = turtle.inspectUp()
  47.     if r and (string.find(t.name, "Wood") or string.find(t.name, "log") or string.find(t.name, "leaves")) then
  48.       turtle.digUp()
  49.       turtle.up()
  50.     else
  51.       fellDown()
  52.       return
  53.     end
  54.   end
  55. end
  56.  
  57. function selectItem(t)
  58.   for i=16,1,-1 do
  59.     turtle.select(i)
  60.     item=turtle.getItemDetail()
  61.     if item~=nil and (item.name == t.name and item.damage == t.metadata) then
  62.       return true
  63.     end
  64.   end
  65.   return false
  66. end
  67.  
  68. function tryForward()
  69.   r,t = turtle.inspect()
  70.   if r then
  71.     if string.find(t.name, "Wood") or string.find(t.name, "log") then
  72.       turtle.dig()
  73.       turtle.forward()
  74.       fell()
  75.     else
  76.       if not turtle.up() then
  77.         print("Error: ceiling detected!")
  78.         fellDown()
  79.         print("So now I'mma break yo shit!")
  80.         turtle.dig()
  81.         turtle.forward()
  82.         if selectItem(t) then turtle.drop() end
  83.       else
  84.         tryForward()
  85.       end
  86.     end
  87.   else
  88.     turtle.forward()
  89.     fellDown()
  90.   end
  91.   return true
  92. end
  93.  
  94.  
  95. function findLoc(a,b)
  96.   if a<x then
  97.     turtle.turnRight()
  98.     turtle.turnRight()
  99.     for i=x-1,a,-1 do
  100.       tryForward()
  101.       x=x-1
  102.     end
  103.     turtle.turnRight()
  104.     turtle.turnRight()
  105.   elseif a>x then
  106.     for i=x+1,a do
  107.       tryForward()
  108.       x=x+1
  109.     end
  110.   end
  111.  
  112.   if b<y then
  113.     turtle.turnLeft()
  114.     for i=y-1,b,-1 do
  115.       tryForward()
  116.       y=y-1
  117.     end
  118.     turtle.turnRight()
  119.   elseif b>y then
  120.     turtle.turnRight()
  121.     for i=y+1,b do
  122.       tryForward()
  123.       y=y+1
  124.     end
  125.     turtle.turnLeft()
  126.   end
  127.  
  128. end
  129.  
  130. print(" "..x2.." "..y2)
  131. findLoc(x2,y2)
  132. print(":"..x.." "..y)
  133.  
  134. it=1
  135. if y2-y1>0 then it=-2 else it=2 end
  136.  
  137. for yi=y2,y1-it,it do
  138.   print(" "..x1.." "..yi)
  139.   findLoc(x1,yi)
  140.   print(":"..x.." "..y)
  141.   findLoc(x1,yi+it/2)
  142.   print(" "..x2.." "..(yi+it))
  143.   findLoc(x2,yi+it/2)
  144.   print(":"..x.." "..y)
  145.   findLoc(x2,yi+it)
  146. end
  147. print(" ".."0".." ".."0")
  148. findLoc(0,0)
  149. print(":"..x.." "..y)
  150. print("Done!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement