Advertisement
Sam54123

CP Wheat Farm

Aug 22nd, 2019
1,348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.42 KB | None | 0 0
  1. -- Simple wheat 9x9 farm program designed to work with CP storage system
  2. -- REQUIRES farmStorage and turtleUtils v1.0.0
  3.  
  4. os.loadAPI('farmStorage')
  5. os.loadAPI('turtleUtils')
  6. rednet.open('left')
  7.  
  8. requiredFuel = 100
  9. -- requiredFuel = 19950
  10.  
  11. -- 5 day cycles
  12. delayTime = 6000
  13. --delayTime = 60
  14.  
  15. fuelItem = {name="minecraft:log",damage=1}
  16.  
  17. -- Given an item object, find and select it in the inventory
  18. function selectItem(item)
  19.     if turtle.getItemDetail() ~= nil and turtle.getItemDetail().name == item.name and turtle.getItemDetail().damage == item.damage then
  20.         return
  21.     end
  22.    
  23.     for i=1,16,1 do
  24.         if turtle.getItemDetail(i) ~= nil and turtle.getItemDetail(i).name == item.name and turtle.getItemDetail(i).damage == item.damage then
  25.             slot = i
  26.         end
  27.     end
  28.    
  29.  
  30.    
  31.     if slot == nil then
  32.         print("Unable to select "..item.name)
  33.     else
  34.         turtle.select(slot)
  35.     end
  36. end
  37.  
  38. -- Get how many of an item the turtle has
  39. function tally(item)
  40.     local count = 0
  41.     for i = 1,16,1 do
  42.         if turtle.getItemDetail(i) ~= nil
  43.         and turtle.getItemDetail(i).name == item.name
  44.         and turtle.getItemDetail(i).damage == item.damage then
  45.             count = count + turtle.getItemCount(i)
  46.         end
  47.     end
  48.     return count
  49. end
  50.  
  51. -- Drop all of an item <item> [amount to keep in inventory]
  52. function dropItem(item, keepAmount)
  53.     if keepAmount == nil then keepAmount = 0 end
  54.    
  55.     -- Keep track of selected slot
  56.     local selectedSlot = turtle.getSelectedSlot()
  57.    
  58.     -- Make array of all matching slots
  59.     local slots = {}
  60.     for i=1,16,1 do
  61.         if turtle.getItemDetail(i) ~= nil and turtle.getItemDetail(i).name == item.name and turtle.getItemDetail(i).damage == item.damage then
  62.             slots[#slots+1] = i
  63.         end
  64.     end
  65.    
  66.     -- Drop all items
  67.     for k,v in pairs(slots) do
  68.         -- Make sure we have keepAmount left
  69.         if tally(item) <= keepAmount then
  70.             turtle.select(selectedSlot)
  71.             return
  72.         end
  73.        
  74.         local currentAmount = tally(item)
  75.         turtle.select(v)
  76.         if currentAmount - turtle.getItemCount() < keepAmount then
  77.             turtle.drop(currentAmount - keepAmount)
  78.         else
  79.             turtle.drop()
  80.         end
  81.        
  82.     end
  83.    
  84.     turtle.select(selectedSlot)
  85. end
  86.  
  87. -- Harvest and replant the current block, regardless of age
  88. function replant()
  89.     turtle.digDown()
  90.     selectItem({name="minecraft:wheat_seeds",damage=0})
  91.     turtle.placeDown()
  92. end
  93.  
  94. -- Harvest and replant the whole farm, regardless of age
  95. function harvest()
  96.     print("Harvesting crops...")
  97.     turtle.turnLeft()
  98.    
  99.     replant()
  100.     for i = 1,4,1 do
  101.         for j = 1,8,1 do
  102.             turtle.forward()
  103.             replant()
  104.         end
  105.         turtle.turnLeft()
  106.         turtle.forward()
  107.         replant()
  108.         turtle.turnLeft()
  109.         for j = 1,8,1 do
  110.             turtle.forward()
  111.             replant()
  112.         end
  113.         turtle.turnRight()
  114.         turtle.forward()
  115.         replant()
  116.         turtle.turnRight()
  117.     end
  118.    
  119.     -- Final stretch
  120.     for i = 1,8,1 do
  121.         turtle.forward()
  122.         replant()
  123.     end
  124.    
  125.     -- Return to base
  126.     for i = 1,2,1 do
  127.         turtle.turnRight()
  128.         for j = 1,8,1 do
  129.             turtle.forward()
  130.         end
  131.     end
  132.     turtle.turnLeft()
  133.    
  134. end
  135.  
  136. -- Get a shipment from the storage system
  137. function requestShipment(items)
  138.     success = farmStorage.requestShipment(items)
  139.    
  140.     if not success then
  141.         return false
  142.     end
  143.    
  144.     -- Put system into hold mode
  145.     redstone.setOutput('top', true)
  146.    
  147.     -- Manually dispense minecart
  148.     redstone.setOutput('right', true)
  149.     os.sleep(.1)
  150.     redstone.setOutput('right', false)
  151.    
  152.     -- Put system into shipment mode
  153.     os.sleep(1)
  154.     redstone.setOutput('right',true)
  155.    
  156.     return true
  157. end
  158.  
  159. -- Return a shipment's minecart chest
  160. function returnShipment()
  161.     redstone.setOutput('right', false)
  162.     os.sleep(.05)
  163.     redstone.setOutput('top', false)
  164.    
  165. end
  166.  
  167. -- Suck as much as possible ou of inventory
  168. function suckAll()
  169.     local canSuck = true
  170.     while canSuck do
  171.         canSuck = turtle.suck()
  172.     end
  173. end
  174.  
  175. -- Search for all refuelable items and refuel
  176. function attemptRefuel()
  177.     for i = 1,16,1 do
  178.         turtle.select(i)
  179.         turtle.refuel()
  180.     end
  181.    
  182.     return turtle.getFuelLevel() >= requiredFuel
  183. end
  184.  
  185. -- Refuel the turtle by any means nessicary (storage request)
  186. function refuel()
  187.     print("Refueling...")
  188.     if not attemptRefuel() then
  189.         print("Retrieving fuel from storage system")
  190.         if farmStorage.getItemPercentage(fuelItem) == 0 then
  191.             print("Storage system doesn't have fuel! Please supply turtle with fuel, or restart to check storage again.")
  192.             local success = false
  193.             while not success do
  194.                 os.queueEvent("randomEvent")
  195.                 os.pullEvent()
  196.                 success = attemptRefuel()
  197.             end
  198.             return true
  199.         end
  200.         local hasShipment = requestShipment({farmStorage.itemRequest(fuelItem,1)})
  201.         if hasShipment then
  202.             local success = false
  203.             while not success do
  204.                 os.queueEvent("randomEvent")
  205.                 os.pullEvent()
  206.                 turtle.suck()
  207.                 success = attemptRefuel()
  208.             end
  209.             suckAll()
  210.             returnShipment()
  211.             return true
  212.         else
  213.             print("Shipment failed. Please manually supply turtle with fuel.")
  214.             local success = false
  215.             while not success do
  216.                 os.queueEvent("randomEvent")
  217.                 os.pullEvent()
  218.                 success = attemptRefuel()
  219.             end
  220.             return true
  221.         end
  222.     end
  223.     return true
  224. end
  225.  
  226. -- Complete harvest cycle
  227. function harvestCycle()
  228.     print("Starting harvest cycle.")
  229.     if turtle.getFuelLevel() < requiredFuel then
  230.         refuel()
  231.     end
  232.     harvest()
  233.     print("Depositing Items")
  234.     dropItem({name="minecraft:wheat",damage=0})
  235.     dropItem({name="minecraft:wheat_seeds",damage=0},64)
  236. end
  237.  
  238. print("Starting wheat farm")
  239. while true do
  240.     turtleUtils.itemTimer(1,2,delayTime)
  241.     harvestCycle()
  242.     turtleUtils.setupItemTimer(1,2,{name="minecraft:wheat_seeds",damage=0})
  243. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement