Advertisement
Aixler

Earth woodfarm

Feb 5th, 2013
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.90 KB | None | 0 0
  1. --DONE: Check Fuel level
  2. --DONE: Pickup Fuel
  3. --DONE: Refuel
  4. --DONE: Pickup Saplings
  5. --DONE: Pickup Bonemeal
  6. --DONE: Left or Right orientation
  7. --TODO: Plant Saplings
  8. --TODO: Use Bonemeal
  9. --TODO: Chop bottom blocks
  10. --TODO: Chop till top
  11. --TODO: Return to start
  12. --DONE: Drop-off everything
  13. --DONE: Wait x minutes
  14. --TODO: Restart after server down
  15.  
  16. function printUsage()
  17.     print("Usage:")
  18.     print("WoodFarm")
  19.     print("Everyting else will be done automatically..")
  20.     print("If there is a 10x10 fenced area, and all chests are sitting where they should.")
  21.     print("Drop-off chest, Fuel chest, Saplings chest, Bonemeal chest.")
  22. end
  23.  
  24. function turnRight()
  25.     turtle.turnRight()
  26.     o = o + 1
  27.    
  28.     -- we want to stay between 0 and 4
  29.     if o > 3 then
  30.         o = o % 4
  31.     end
  32. end
  33.  
  34. function turnLeft()
  35.     turtle.turnLeft()
  36.     o = o - 1
  37.    
  38.     if o < 0 then
  39.         o = o + 4
  40.     end
  41. end
  42.  
  43. function forward()
  44.     turtle.forward()
  45.    
  46.     o = o % 4
  47.     if o == 0 then
  48.         x = x + 1
  49.     elseif o == 1 then
  50.         y = y + 1
  51.     elseif o == 2 then
  52.         x = x - 1
  53.     elseif o == 3 then
  54.         y = y - 1
  55.     else
  56.         -- Not good
  57.         error()
  58.     end
  59. end
  60.  
  61. function goToCenter()
  62.    
  63. end
  64.  
  65. function returnHome()
  66.     print("Returning home...")
  67.     print("x = " .. x)
  68.     print("y = " .. y)
  69.     print("z = " .. z)
  70.     print("o = " .. o)
  71.    
  72.     if x > 0 then
  73.     print("1")
  74.         while o ~= 2 do
  75.             turnRight()
  76.         end
  77.     elseif x < 0 then
  78.     print("2")
  79.         while o ~= 0 do
  80.             turnRight()
  81.         end
  82.     else
  83.     print("3")
  84.         --nothing?
  85.     end
  86.    
  87.     while x ~= 0 do
  88.     print("4")
  89.         forward()
  90.     end
  91.    
  92.     if y > 0 then
  93.     print("5")
  94.         print("Go left")
  95.         while o ~= 3 do
  96.     print("6")
  97. turnRight()
  98.         end
  99.     elseif y < 0 then
  100.     print("7")
  101.         print("Go right")
  102.         while o ~= 1 do
  103.     print("8")
  104. turnRight()
  105.         end
  106.     else
  107.     print("9")
  108.         --nothing?
  109.     end
  110.    
  111.     while y ~= 0 do
  112.     print("10")
  113.         forward()
  114.     end
  115.    
  116.     while o ~= 0 do
  117.     print("11")
  118.         turnRight()
  119.     end
  120. end
  121.  
  122. function moveToSide()
  123.     if orientation == "right" then
  124.         turnRight()
  125.         forward()
  126.         turnLeft()
  127.         return
  128.     elseif orientation == "left" then
  129.         turnLeft()
  130.         forward()
  131.         turnRight()
  132.         return
  133.     else
  134.         print("Shouldn't be here!")
  135.     end
  136.     error()
  137. end
  138.  
  139. -- check if fuel is below 10000, and add fuel if so
  140. function checkAndReFuel()
  141.     -- go to fuel chest
  142.     moveToSide()
  143.    
  144.     if turtle.getFuelLevel() >= 10000 then
  145.         return
  146.     end
  147.    
  148.     -- we should be by the fuel chesst
  149.     if not turtle.detect() then
  150.         print("Nothing to get fuel from")
  151.         error()
  152.     end
  153.    
  154.     while turtle.getFuelLevel() < 20000 do
  155.         pickup = false
  156.         turtle.select(1)
  157.         while not pickup do -- let's try untill we get something
  158.             pickup = turtle.suck()
  159.         end
  160.        
  161.         turtle.select(1)
  162.         if not turtle.refuel() then
  163.             print("No fuel in the fuel chest!")
  164.             error()
  165.         end
  166.     end
  167. end
  168.  
  169. -- find out if we go left or right
  170. function checkOrientation()
  171.     if not orientation == nil then
  172.         return
  173.     end
  174.    
  175.     turnRight()
  176.     if turtle.detect() then
  177.         print("There is something to the right.. Let's try left.")
  178.     else
  179.         print("The right is free! We found our orientation!")
  180.         orientation = "right"
  181.         turnLeft()
  182.         return
  183.     end
  184.    
  185.     turnLeft()
  186.     turnLeft()
  187.     if turtle.detect() then
  188.         print("There is something to the left aswell.. WTF!")
  189.     else
  190.         print("The left is free! We found our orientation!")
  191.         orientation = "left"
  192.         turnRight()
  193.         return
  194.     end
  195.    
  196.     -- if we get here we are lost
  197.     error()    
  198. end
  199.  
  200. -- place items in chest
  201. function dropOff()
  202.     if not turtle.detect() then
  203.         print("There is nothing in front..! Halp!")
  204.         error()
  205.     end
  206.    
  207.     for slot = 1, 16 do
  208.         dropped = false
  209.         turtle.select(slot)
  210.         if turtle.getItemCount(slot) > 0 then
  211.             while not dropped do -- lets try this untill success
  212.                 dropped = turtle.drop()
  213.             end
  214.         end
  215.     end
  216. end
  217.  
  218. function pickup(something, slot, needed)
  219.     -- go to next chest
  220.     moveToSide()
  221.    
  222.     -- we should be by a chesst
  223.     if not turtle.detect() then
  224.         print("Nothing to get " .. something .. "from")
  225.         error()
  226.     end
  227.    
  228.     while turtle.getItemCount(slot) < needed do
  229.         pickuped = false
  230.         turtle.select(slot)
  231.         while not pickuped do -- let's try untill we get something
  232.             pickuped = turtle.suck()
  233.         end
  234.     end
  235. end
  236.  
  237. function pickupSaplings()
  238.     pickup("Sapling", 1, 4)
  239. end
  240.  
  241. function pickupBonemeal()
  242.     pickup("Bonemeal", 2, 1)
  243. end
  244.  
  245. -- main fuction that stands for one cycle
  246. function woodFarm()
  247.     dropOff() -- nice clean start
  248.     checkOrientation() -- where do we want to go today
  249.     checkAndReFuel() -- check fuel level and refill
  250.     pickupSaplings() -- get some saplings
  251.     pickupBonemeal() -- get some bonemeal
  252. --  goToCenter() -- walk to center
  253.     returnHome() -- go to the start
  254. end
  255.  
  256. -- ---------- Running programm ----------
  257. -- Check argument usage
  258. tArgs = { ... }
  259. if #tArgs ~= 0 then
  260.     printUsage()
  261.     return
  262. end
  263.  
  264. -- Set params
  265. orientation = nil
  266.  
  267. x = 0
  268. y = 0
  269. z = 0
  270. o = 0
  271.  
  272. repeat
  273.     if pcall(woodFarm) then
  274.         -- nothing to report, sleep and start over.
  275.         os.sleep(900)
  276.     else
  277.         print("Horrible nasty stuff happened.. Stopping program.")
  278.         return
  279.     end
  280. until false
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement