Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---
- --- Generated by EmmyLua(https://github.com/EmmyLua)
- --- Created by cdbab.
- --- DateTime: 11/22/2024 8:41 PM
- ---
- -- Node Script
- -- Variables
- local PROTOCOL = "mobFarm"
- local LOCAL_ID = "MOB_1" -- Change this for each node
- local status = "STOPPED"
- local cID = nil -- Controller ID
- -- Host controller as 'controller' on network
- rednet.host(PROTOCOL, LOCAL_ID)
- -- Redstone Outputs
- local REDSTONE_BACK = "back" -- Lights
- local REDSTONE_RIGHT = "right" -- Fans and grinder
- -- Peripherals
- peripheral.find("modem", rednet.open)
- -- Functions
- local function setRedstoneOutput(side, state)
- redstone.setOutput(side, state)
- end
- local function toggleLights(state)
- setRedstoneOutput(REDSTONE_BACK, state)
- end
- local function toggleFans(state)
- sleep(4) -- Async delay for toggling fans
- setRedstoneOutput(REDSTONE_RIGHT, state)
- end
- local function toggleFarm()
- if status == "RUNNING" then
- print("Stopping farm...")
- toggleLights(true) -- Turn lights ON
- toggleFans(false) -- Turn fans OFF
- status = "STOPPED"
- else
- print("Starting farm...")
- toggleLights(false) -- Turn lights OFF
- toggleFans(true) -- Turn fans ON
- status = "RUNNING"
- end
- end
- -- Updated validateStatus Function
- local function validateStatus()
- local lightsState = redstone.getOutput(REDSTONE_BACK)
- local fansState = redstone.getOutput(REDSTONE_RIGHT)
- -- Check if lights and fans are in the same state, handle by stopping the farm
- if lightsState == fansState then
- print("Error: Lights and fans cannot be in the same state! Turning lights ON and fans OFF.")
- toggleLights(true) -- Turn lights ON
- toggleFans(false) -- Turn fans OFF
- status = "STOPPED" -- Set status to STOPPED instead of ERROR
- else
- status = (fansState and "RUNNING") or "STOPPED"
- end
- end
- local function sendStatus()
- validateStatus()
- local packet = {
- type = "status",
- hostName = LOCAL_ID,
- status = status
- }
- rednet.send(cID, packet, PROTOCOL)
- print("Sent status update:", textutils.serialize(packet))
- end
- -- Helper function to send responses
- local function sendResponse(toID, packet)
- rednet.send(toID, packet, PROTOCOL)
- end
- -- Locate Controller
- while cID == nil do
- cID = rednet.lookup(PROTOCOL, "controller")
- if cID then
- cID = tonumber(cID)
- print("Found controller, host ID is: " .. cID)
- -- Notify controller of initialization
- local packet = {
- type = "responseId",
- CLIENT_ID = LOCAL_ID -- Responding with CLIENT_ID instead of LOCAL_ID
- }
- rednet.send(cID, packet, PROTOCOL)
- print("Initialization message sent to controller.")
- else
- print("Could not locate controller, retrying in 3 seconds...")
- sleep(3)
- end
- end
- -- Main Loop
- while true do
- local senderId, message = rednet.receive(PROTOCOL)
- if senderId == cID then
- if message.type == "toggle" then
- toggleFarm()
- sendStatus()
- elseif message.type == "requestStatus" then
- sendStatus()
- elseif message.type == "requestId" then
- print("Received requestId from controller.") -- Debug log
- local responsePacket = {
- type = "responseId",
- CLIENT_ID = LOCAL_ID -- Responding with CLIENT_ID instead of LOCAL_ID
- }
- sendResponse(senderId, responsePacket)
- print("Sent CLIENT_ID response to controller.") -- Debug log
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement