xOCAx

Spot farm

Jan 16th, 2022
507
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.67 KB | None | 0 0
  1. -- Spot farm, please don't ask why
  2. local robot_api = require("robot")
  3. local computer = require("computer")
  4. local event = require("event")
  5.  
  6. fsm = {}
  7.  
  8. -- flags
  9. inventory_used = false
  10. inventory_overflowed = false
  11.  
  12. function checkInventory()
  13.     if inventory_used then
  14.         for i=1,16 do
  15.             if robot_api.space(i) > 0 then
  16.                 return true
  17.             end
  18.         end
  19.         return false
  20.     end
  21.     return nil
  22. end
  23.  
  24. function checkInstrument()
  25.     if robot_api.durability() > 0 then
  26.         return true
  27.     end
  28.     return false
  29. end
  30.  
  31. function configureTest()
  32.     inv_size = robot_api.inventorySize()
  33.     if inv_size > 0 then
  34.         inventory_used = true
  35.         print("inventory founded...\n")
  36.     end
  37. end
  38.  
  39.  
  40. function fsm.storeItemsState()
  41.     robot_api.turnLeft()
  42.     for i=1,16 do
  43.         robot_api.select(i)
  44.         robot_api.drop()
  45.     end
  46.     robot_api.turnRight()
  47.     print("store all items from inventory")
  48.     inventory_overflowed = false
  49. end
  50.  
  51. function fsm:instrumentIssueEvent()
  52.     while not checkInstrument() do
  53.         computer.beep()
  54.         os.sleep(1)
  55.     end
  56. end
  57.  
  58. function killEntity()
  59.     robot_api.swing()  
  60.     if not checkInstrument() then
  61.         print("instrument broken")
  62.         fsm:instrumentIssueEvent()
  63.     end
  64. end
  65.  
  66. -- function fsm.chargeState() end
  67.  
  68. -- function checkEnergy() end
  69.  
  70. timerID = nil
  71.  
  72. function fsm.farmState()
  73.     if inventory_overflowed then
  74.         return
  75.     end
  76.     if checkInventory() ~= nil then
  77.         if not checkInventory() then
  78.             print("inventory overflowed...")
  79.             inventory_overflowed = true
  80.             fsm.storeItemsState()
  81.         end
  82.     end
  83.     _, name = robot_api.detect()
  84.     while name == "entity" do
  85.         killEntity()
  86.     end
  87. end
  88.  
  89. function start()
  90.     print("initialize...\n")
  91.     configureTest()
  92.     timerID = event.timer(1, fsm.farmState, math.huge)
  93. end
  94.  
  95. start()
Advertisement
Add Comment
Please, Sign In to add comment