Advertisement
Guest User

farmer.lua

a guest
Nov 20th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.13 KB | None | 0 0
  1. local component = require("component")
  2. local robot = require("robot")
  3. local event = require("event")
  4. local computer = require("computer")
  5. local isRunning = true
  6. local isHarvestRunning = false
  7. local harvestTimer = nil
  8.  
  9. local waitTimeMinutes = 1
  10. local minChargePercent = 98
  11.  
  12. function strafeRow()
  13.   for i=0,6,1 do
  14.     robot.use()
  15.     robot.forward()
  16.   end
  17. end
  18.  
  19. function strafeField()
  20.   strafeRow()
  21.   robot.turnLeft()
  22.   robot.forward()
  23.   robot.turnLeft()
  24.   strafeRow()
  25.   robot.turnRight()
  26.   robot.forward()
  27.   robot.turnRight()
  28.   strafeRow()
  29. end
  30.  
  31. function harvestField()
  32.   strafeField()
  33.   robot.turnAround()
  34.   strafeField()
  35. end
  36.  
  37. function drainToChest()
  38.   for i=1,robot.inventorySize(),1 do
  39.     robot.select(i)
  40.     if (robot.count() > 0) then
  41.       robot.dropDown()
  42.     end
  43.   end
  44.   robot.select(1)
  45. end
  46.  
  47. function needsPower()
  48.   return (computer.energy()/computer.maxEnergy()*100 < minChargePercent)
  49. end
  50.  
  51. function BeginHarvest()
  52.       if (isHarvestRunning) then
  53.         return
  54.       end
  55.  
  56.       isHarvestRunning = true
  57.       print("Collecting crops")
  58.       harvestField()
  59.       robot.turnAround()
  60.       print("Draining to chest")
  61.       drainToChest()
  62.       if (needsPower()) then
  63.         print("Needs Charging")
  64.         robot.turnLeft()
  65.         robot.forward()
  66.         repeat
  67.           print("Charging...")
  68.           os.sleep(1)
  69.         until (computer.energy()/computer.maxEnergy()*100 >= 99)
  70.         robot.turnAround()
  71.         robot.forward()
  72.         robot.turnLeft()
  73.         print("Chargine finished")
  74.       end
  75.       isHarvestRunning = false
  76.       print("Waiting " .. waitTimeMinutes .. " minutes")
  77.       print("Press X to stop program")
  78. end
  79.  
  80. --BeginHarvest()
  81. function ListenTest()
  82.   print("Starting Harvest")
  83.   print("Waiting " .. waitTimeMinutes .. " minutes")
  84.   print("Press X to stop program")
  85.   harvestTimer = event.timer(waitTimeMinutes * 60, BeginHarvest, math.huge)
  86.  
  87.   while isRunning do
  88.     local test, _, keyval = event.pull(math.huge, "key_up")
  89.     if (test ~= nil and keyval == 120) then
  90.       event.cancel(harvestTimer)
  91.       isRunning = false
  92.     end
  93.   end
  94. end
  95.  
  96. ListenTest()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement