DabDaddy6223

crop_controller.lua

Jun 11th, 2026 (edited)
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.02 KB | None | 0 0
  1. -- API --------------------------------------------------
  2.  
  3. function hasValue (tab, val)
  4.     for index, value in ipairs(tab) do
  5.         if value == val then
  6.             return true
  7.         end
  8.     end
  9.  
  10.     return false
  11. end
  12.  
  13. function printKeys(tab)
  14.     -- Source - https://stackoverflow.com/a/12674376
  15.     -- Posted by lhf
  16.     -- Retrieved 2026-06-11, License - CC BY-SA 3.0
  17.  
  18.     local keyset={}
  19.     local n=0
  20.  
  21.     for k,v in pairs(tab) do
  22.         n=n+1
  23.         keyset[n]=k
  24.     end
  25.  
  26.     print(textutils.serialise(keyset))
  27. end
  28.  
  29. ---------------------------------------------------------
  30.  
  31. EXPECTED_CROP_MACHINES = 4
  32. TIME_BETWEEN_HARVEST_SECONDS = 900
  33.  
  34. local function main()
  35.     peripheral.find("modem", rednet.open)
  36.     rednet.broadcast("crop_controller_ping")
  37.  
  38.     local timer_id = os.startTimer(1)
  39.  
  40.     -- Connect to crop workers
  41.     local connected_crop_machines = {}
  42.     repeat
  43.         local event, p1, p2 = os.pullEvent()
  44.  
  45.         if event == "rednet_message" then
  46.             local id = p1
  47.             local message = p2
  48.  
  49.             if message == "crop_worker_pong" then
  50.                 connected_crop_machines[#connected_crop_machines + 1] = id
  51.             end
  52.  
  53.         elseif event == "timer" and p1 == timer_id then
  54.             rednet.broadcast("crop_controller_ping")
  55.             timer_id = os.startTimer(1)
  56.         end
  57.  
  58.     until #connected_crop_machines == EXPECTED_CROP_MACHINES
  59.  
  60.     os.cancelTimer(timer_id)
  61.  
  62.     -- Identify crop workers
  63.     local name_response_count = 0
  64.     local crop_machine_names = {}
  65.     for i,v in ipairs(connected_crop_machines) do
  66.         rednet.send(v, "crop_controller_identify")
  67.     end
  68.  
  69.     repeat
  70.         local id, message = rednet.receive()
  71.         if hasValue(connected_crop_machines, id) then
  72.             crop_machine_names[id] = message
  73.             name_response_count = name_response_count + 1
  74.         end
  75.     until name_response_count == EXPECTED_CROP_MACHINES
  76.  
  77.     for i, v in pairs(crop_machine_names) do
  78.         print(tostring(v) .. " has connected")
  79.     end
  80.  
  81.     while true do
  82.         -- Set off crop workers
  83.         for i,v in ipairs(connected_crop_machines) do
  84.             rednet.send(v, "crop_controller_harvest")
  85.         end
  86.  
  87.         local finished_crop_machines = 0
  88.         repeat
  89.             local id, message = rednet.receive()
  90.             if hasValue(connected_crop_machines, id) then
  91.                 if message == "crop_worker_finished" then
  92.                     finished_crop_machines = finished_crop_machines + 1
  93.                 elseif message == "crop_worker_no_fuel" then
  94.                     print(tostring(crop_machine_names[id]) .. " (" .. tostring(id) .. ") is out of fuel")
  95.                 elseif message == "crop_worker_no_seeds" then
  96.                     print(tostring(crop_machine_names[id]) .. " (" .. tostring(id) .. ") doesn't have enough seeds")
  97.                 end
  98.             end
  99.         until finished_crop_machines == EXPECTED_CROP_MACHINES
  100.  
  101.         os.sleep(TIME_BETWEEN_HARVEST_SECONDS)
  102.     end
  103. end
  104.  
  105. main()
Advertisement
Add Comment
Please, Sign In to add comment