Clor

wood

Jul 26th, 2014
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.68 KB | None | 0 0
  1. --Var
  2. local robot = require("robot")
  3. local gps = require("component").navigation
  4. local inv = require("component").inventory_controller
  5. local comp = require("computer")
  6. local s = require("sides")
  7. local tr = require("component").tractor_beam
  8.  
  9. local Xc,Xn,Xt,Yn,Yc,Yt,Zn,Zc,Zt
  10. Xn,Yn,Zn = gps.getPosition()
  11. nSide = gps.getFacing()
  12. --Function
  13.  
  14. function forward()
  15. local n = 0
  16. while true do
  17. if robot.forward() then
  18. while tr.suck() do end return true
  19. else
  20. robot.swing()
  21. n = n+1
  22. end
  23. if n == 20 then print("no run forward, program stop!") os.exit() end
  24. end
  25. end
  26.  
  27. function up()
  28. local n = 0
  29. while true do
  30. if robot.up() then return true else
  31. robot.swingUp()
  32. n = n+1
  33. end
  34. if n == 20 then print("no run up, program stop!") os.exit() end
  35. end
  36. end
  37.  
  38. function down()
  39. local n = 0
  40. while true do
  41. if robot.down() then return true else
  42. robot.swingDown()
  43. n = n+1
  44. end
  45. if n == 20 then print("no run down, program stop!") os.exit() end
  46. end
  47. end
  48.  
  49.  
  50. function sapling()
  51. robot.select(2)
  52. up()
  53. forward()
  54. robot.placeDown()
  55. forward()
  56. robot.placeDown()
  57. robot.turnLeft()
  58. forward()
  59. robot.placeDown()
  60. robot.turnLeft()
  61. forward()
  62. robot.placeDown()
  63. forward()
  64. robot.turnLeft()
  65. forward()
  66. robot.turnLeft()
  67. down()
  68. end
  69.  
  70. function wood()
  71. local n=0
  72. robot.swing()
  73. forward()
  74. while true do
  75.   if robot.detectUp() then
  76.   n=n+1
  77.   robot.swing()
  78.   robot.swingUp()
  79.   up()
  80.   else break end
  81. end
  82. robot.turnLeft()
  83. robot.swing()
  84. forward()
  85. robot.turnRight()
  86. for i=1,n do
  87. robot.swing()
  88. robot.swingDown()
  89. down()
  90. end
  91. robot.swing()
  92. robot.turnRight()
  93. forward()
  94. robot.turnRight()
  95. forward()
  96. robot.turnAround()
  97. end
  98.  
  99. function new_tools()
  100.    if robot.durability() == nil then return false end
  101.    if robot.durability()<150 then
  102.    robot.select(1)
  103.    inv.equip()
  104.        if robot.durability()<150 then
  105.        return false
  106.        else
  107.        return true
  108.     end
  109.     else
  110.     return true
  111.     end
  112. end
  113.  
  114. function detect_wood()
  115.  if robot.detect() then
  116.   up()
  117.    if robot.detect() then
  118.     down()
  119.     return "wood"
  120.    else
  121.     down()
  122.     return "sapling"
  123.    end
  124.  else
  125.   return "clear"
  126.  end
  127. end
  128.  
  129. function run_a()
  130. robot.turnLeft()
  131. for i=1,10 do
  132. forward()
  133. end
  134. robot.turnRight()
  135. end
  136.  
  137. function run_b()
  138. robot.turnRight()
  139. forward()
  140. robot.turnRight()
  141. for i=1,10 do
  142. forward()
  143. end
  144. robot.turnRight()
  145. forward()
  146. robot.turnRight()
  147. end
  148.  
  149. function run_c()
  150. robot.turnRight()
  151. for i=1,10 do
  152. forward()
  153. end
  154. robot.turnLeft()
  155. end
  156.  
  157. function run_d()
  158. robot.turnLeft()
  159. forward()
  160. forward()
  161. robot.turnRight()
  162. for i=1,10 do
  163. forward()
  164. end
  165. robot.turnRight()
  166. forward()
  167. forward()
  168. robot.turnLeft()
  169. end
  170.  
  171. function bksp(area)
  172. if area == "chest" then
  173.   robot.turnLeft()
  174.   forward()
  175.   forward()
  176.   robot.turnRight()
  177.   Xc,Yc,Zc = gps.getPosition()
  178.   Xt,Yt,Zt = gps.getPosition()
  179.     if (nSide == 2) or (nSide == 3) then
  180.       while Xn~=Xt do
  181.       forward()
  182.       Xt,Yt,Zt = gps.getPosition()  end
  183.     robot.turnRight()
  184.     else
  185.       while Zn~=Zt do
  186.       forward()
  187.       Xt,Yt,Zt = gps.getPosition() end
  188.     robot.turnRight()
  189.     end
  190.      if (nSide == 2) or (nSide == 3) then
  191.       while Zn~=Zt do
  192.       forward()  
  193.       Xt,Yt,Zt = gps.getPosition() end
  194.     else
  195.       while Xn~=Xt do
  196.       forward()
  197.       Xt,Yt,Zt = gps.getPosition()   end
  198.     end
  199. elseif area == "wood" then
  200.   Xt,Yt,Zt = gps.getPosition()
  201.   robot.turnAround()
  202.     if (nSide == 2) or (nSide == 3) then
  203.       while Zc~=Zt do
  204.       forward()  
  205.       Xt,Yt,Zt = gps.getPosition() end
  206.    robot.turnLeft()
  207.  else
  208.       while Xc~=Xt do
  209.       forward()
  210.       Xt,Yt,Zt = gps.getPosition() end
  211.    robot.turnLeft()
  212.     end
  213.     if (nSide == 2) or (nSide == 3) then
  214.       while Xc~=Xt do
  215.       forward()
  216.       Xt,Yt,Zt = gps.getPosition() end
  217.  else
  218.       while Zc~=Zt do
  219.       forward()
  220.       Xt,Yt,Zt = gps.getPosition() end
  221.     end  
  222.   robot.turnLeft()
  223.   forward()
  224.   forward()
  225.   robot.turnLeft()
  226. end
  227. end
  228.  
  229. function chest(tools)
  230.     for i=3,16 do
  231.   robot.select(i)
  232.   if (robot.count(i) ~= 0) and (not robot.drop(64)) then robot.turnAround() print("chest full, stop!") os.exit() end
  233.   end
  234.   robot.turnRight()
  235.   robot.select(2)
  236.    for i=1,5 do
  237.    if robot.count(2) ~= 64 then robot.suck(robot.space(2)) else break end
  238.   end
  239.   robot.turnLeft()
  240.   if not count_sapl() then return "sapling" end
  241. if not tools then
  242.   robot.select(1)
  243.   robot.drop()
  244.   inv.equip()
  245.   robot.drop()
  246.   if not robot.suckDown(1) then return "tools" end
  247.   inv.equip()
  248.   if not robot.suckDown(1) then return "tools" end
  249.   end
  250.   return true
  251. end
  252.  
  253. function count_sapl()
  254. if robot.count(2) < 5 then return false else return true end
  255. end
  256.  
  257. function full()
  258. if robot.count(13) == 0  then return true else return false end
  259. end
  260.  
  261. function energy()
  262. if comp.energy() < 1800 then os.sleep(180) end
  263. end
  264.  
  265. -- MultiFunction
  266. function woodAll()
  267.   while true do
  268.        if count_sapl() and full() and new_tools() then
  269.        detect = detect_wood()
  270.            if detect == "clear" then sapling() break
  271.            elseif detect == "wood" then wood() sapling() break  
  272.            elseif detect == "sapling" then break end
  273.    else
  274.     bksp("chest")
  275.  _exit = chest(new_tools())
  276.    if "tools" == _exit then robot.turnAround() print("not tools, stop!") os.exit()
  277.     elseif "sapling" ==_exit then robot.turnAround() print("not sapling, stop!") os.exit()
  278.    end
  279.  bksp("wood")
  280.   end
  281.  end
  282. end
  283.  
  284. --Programs:
  285.  
  286. robot.turnRight()
  287. while true do
  288.   for i=1,3 do
  289.    energy()
  290.    woodAll()
  291.    os.sleep(10)
  292.    energy()
  293.    run_a()
  294.    end
  295. energy()
  296. woodAll()
  297. os.sleep(10)
  298. energy()
  299. run_b()
  300. for i=1,3 do
  301.    energy()
  302.    woodAll()
  303.    os.sleep(10)
  304.    energy()
  305.    run_c()
  306.   end
  307. energy()
  308. woodAll()
  309. os.sleep(10)
  310. energy()
  311. run_d()
  312. end
Advertisement
Add Comment
Please, Sign In to add comment