Advertisement
Guest User

farm

a guest
Jul 21st, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.19 KB | None | 0 0
  1. -- Program for Farming Turtle from ComputerCraft mod.
  2. -- AutoFarm Ver 1.0
  3. -- Copyright  (c)  2015  Alexander Andreyev <aravsar@ya.ru>
  4. --   Farming 9x9 field
  5. -- I S x x x x x x x x
  6. --   x x x x x x x x x
  7. --   x x x x x x x x x
  8. --   x x x x x x x x x
  9. --   x x x x W x x x x
  10. --   x x x x x x x x x
  11. --   x x x x x x x x x
  12. --   x x x x x x x x x
  13. --   x x x x x x x x x
  14. -- S - start position
  15. -- I - possible inventory with which interacts turtle in the end (do_end_stuff)
  16. -- W - water block in center
  17. -- The turtle moves clockwise from S to W by spiral,
  18. -- moves back to the S, drop stuff to I and stops.
  19. -- The turtle must be in one block above the field.
  20.  
  21. -- MAIN PROGRAM
  22. args      = {...}
  23. chest_name_part = 'Chest' -- a part of name of the I chest
  24. rings     = {9,7,5}
  25. fuel_slot = 16
  26. plant_slots = {1,2,3,4,5,6,7,8}
  27. harvest_slots = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}
  28. magic = {name = 'magicalcrops:', metadata = 7} -- growed up magic crops
  29.  
  30. function _refuel(fuel)
  31.   turtle.select(fuel_slot)
  32.   turtle.refuel(fuel)
  33. end
  34.  
  35. -- MOVEMENT
  36. function process_field(do_stuff, do_end_stuff)
  37.   for i = 1, #rings do
  38.     local n = rings[i]
  39.     for j = 1, 4 do
  40.       if j == 4 then n = n - 1 end
  41.       for k = 1, n-1 do
  42.         do_stuff()
  43.         turtle.forward()
  44.       end
  45.       turtle.turnRight()
  46.     end
  47.     do_stuff()
  48.     turtle.forward()
  49.   end
  50.   -- moving back on start position
  51.   turtle.turnLeft()
  52.   for i = 1, 3 do turtle.forward() end
  53.   turtle.turnLeft()
  54.   for i = 1, 3 do turtle.forward() end
  55.  
  56.   do_end_stuff()
  57.  
  58.   turtle.turnLeft()
  59.   turtle.turnLeft()
  60. end
  61.  
  62. -- PLANTING
  63. function before_plant()
  64.   turtle.turnLeft()
  65.   turtle.turnLeft()
  66.   for i = 1, #plant_slots do
  67.     turtle.select(plant_slots[i])
  68.     turtle.suck()
  69.     local data = turtle.getItemDetail()
  70.     if not string.match(data.name,magic.name) then turtle.drop() end
  71.     local count = turtle.getItemCount()
  72.     if count > 9 then turtle.dropUp(count - 9) end
  73.   end
  74.   turtle.turnLeft()
  75.   turtle.turnLeft()
  76. end
  77. function plant()
  78.   for i = 1, #plant_slots do
  79.     turtle.select(plant_slots[i])
  80.     if turtle.getItemCount(plant_slots[i]) > 0 then
  81.       turtle.placeDown()
  82.       return
  83.     end
  84.   end
  85. end
  86. function planting()
  87.   local s,_ = turtle.inspectDown()
  88.   if s == false then
  89.     turtle.digDown()
  90.     plant()
  91.   end
  92. end
  93. function on_end_planting()
  94.   for i = 1, #plant_slots do
  95.     turtle.select(plant_slots[i])
  96.     turtle.drop()
  97.   end
  98. end
  99.  
  100. -- HARVESTING
  101. function harvest(object)
  102.   local s,m = turtle.inspectDown()
  103.   if not s or not string.match(m.name,object.name) then return end
  104.  
  105.   for i = 1, #harvest_slots do
  106.     turtle.select(harvest_slots[i])
  107.     if turtle.getItemSpace(harvest_slots[i]) > 0 then
  108.       turtle.digDown()
  109.       return
  110.     end
  111.   end
  112. end
  113. function harvesting()
  114.   harvest(magic)
  115. end
  116. function on_end_harvesting()
  117.   for i = 1, #harvest_slots do
  118.     turtle.select(harvest_slots[i])
  119.     turtle.drop()
  120.   end
  121. end
  122.  
  123. -- Controlling program
  124. if args[1] == 'p' then
  125.   _refuel(2)
  126.   before_plant()
  127.   process_field(planting, on_end_planting)
  128. elseif args[1] == 'h' then
  129.   _refuel(2)
  130.   process_field(harvesting, on_end_harvesting)
  131. else
  132.   print('p  - for planting')
  133.   print('h  - for harvest')
  134. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement