Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- API --------------------------------------------------
- function hasValue (tab, val)
- for index, value in ipairs(tab) do
- if value == val then
- return true
- end
- end
- return false
- end
- function printKeys(tab)
- -- Source - https://stackoverflow.com/a/12674376
- -- Posted by lhf
- -- Retrieved 2026-06-11, License - CC BY-SA 3.0
- local keyset={}
- local n=0
- for k,v in pairs(tab) do
- n=n+1
- keyset[n]=k
- end
- print(textutils.serialise(keyset))
- end
- ---------------------------------------------------------
- EXPECTED_CROP_MACHINES = 4
- TIME_BETWEEN_HARVEST_SECONDS = 900
- local function main()
- peripheral.find("modem", rednet.open)
- rednet.broadcast("crop_controller_ping")
- local timer_id = os.startTimer(1)
- -- Connect to crop workers
- local connected_crop_machines = {}
- repeat
- local event, p1, p2 = os.pullEvent()
- if event == "rednet_message" then
- local id = p1
- local message = p2
- if message == "crop_worker_pong" then
- connected_crop_machines[#connected_crop_machines + 1] = id
- end
- elseif event == "timer" and p1 == timer_id then
- rednet.broadcast("crop_controller_ping")
- timer_id = os.startTimer(1)
- end
- until #connected_crop_machines == EXPECTED_CROP_MACHINES
- os.cancelTimer(timer_id)
- -- Identify crop workers
- local name_response_count = 0
- local crop_machine_names = {}
- for i,v in ipairs(connected_crop_machines) do
- rednet.send(v, "crop_controller_identify")
- end
- repeat
- local id, message = rednet.receive()
- if hasValue(connected_crop_machines, id) then
- crop_machine_names[id] = message
- name_response_count = name_response_count + 1
- end
- until name_response_count == EXPECTED_CROP_MACHINES
- for i, v in pairs(crop_machine_names) do
- print(tostring(v) .. " has connected")
- end
- while true do
- -- Set off crop workers
- for i,v in ipairs(connected_crop_machines) do
- rednet.send(v, "crop_controller_harvest")
- end
- local finished_crop_machines = 0
- repeat
- local id, message = rednet.receive()
- if hasValue(connected_crop_machines, id) then
- if message == "crop_worker_finished" then
- finished_crop_machines = finished_crop_machines + 1
- elseif message == "crop_worker_no_fuel" then
- print(tostring(crop_machine_names[id]) .. " (" .. tostring(id) .. ") is out of fuel")
- elseif message == "crop_worker_no_seeds" then
- print(tostring(crop_machine_names[id]) .. " (" .. tostring(id) .. ") doesn't have enough seeds")
- end
- end
- until finished_crop_machines == EXPECTED_CROP_MACHINES
- os.sleep(TIME_BETWEEN_HARVEST_SECONDS)
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment