Advertisement
April_The_Sergal

node script

Nov 23rd, 2024 (edited)
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.60 KB | Gaming | 0 0
  1. ---
  2. --- Generated by EmmyLua(https://github.com/EmmyLua)
  3. --- Created by cdbab.
  4. --- DateTime: 11/22/2024 8:41 PM
  5. ---
  6. -- Node Script
  7.  
  8. -- Variables
  9. local PROTOCOL = "mobFarm"
  10. local LOCAL_ID = "MOB_1" -- Change this for each node
  11. local status = "STOPPED"
  12. local cID = nil -- Controller ID
  13.  
  14. -- Host controller as 'controller' on network
  15. rednet.host(PROTOCOL, LOCAL_ID)
  16.  
  17. -- Redstone Outputs
  18. local REDSTONE_BACK = "back" -- Lights
  19. local REDSTONE_RIGHT = "right" -- Fans and grinder
  20.  
  21. -- Peripherals
  22. peripheral.find("modem", rednet.open)
  23.  
  24. -- Functions
  25. local function setRedstoneOutput(side, state)
  26.     redstone.setOutput(side, state)
  27. end
  28.  
  29. local function toggleLights(state)
  30.     setRedstoneOutput(REDSTONE_BACK, state)
  31. end
  32.  
  33. local function toggleFans(state)
  34.     sleep(4) -- Async delay for toggling fans
  35.     setRedstoneOutput(REDSTONE_RIGHT, state)
  36. end
  37.  
  38. local function toggleFarm()
  39.     if status == "RUNNING" then
  40.         print("Stopping farm...")
  41.         toggleLights(true) -- Turn lights ON
  42.         toggleFans(false) -- Turn fans OFF
  43.         status = "STOPPED"
  44.     else
  45.         print("Starting farm...")
  46.         toggleLights(false) -- Turn lights OFF
  47.         toggleFans(true) -- Turn fans ON
  48.         status = "RUNNING"
  49.     end
  50. end
  51.  
  52. -- Updated validateStatus Function
  53. local function validateStatus()
  54.     local lightsState = redstone.getOutput(REDSTONE_BACK)
  55.     local fansState = redstone.getOutput(REDSTONE_RIGHT)
  56.  
  57.     -- Check if lights and fans are in the same state, handle by stopping the farm
  58.     if lightsState == fansState then
  59.         print("Error: Lights and fans cannot be in the same state! Turning lights ON and fans OFF.")
  60.         toggleLights(true) -- Turn lights ON
  61.         toggleFans(false) -- Turn fans OFF
  62.         status = "STOPPED" -- Set status to STOPPED instead of ERROR
  63.     else
  64.         status = (fansState and "RUNNING") or "STOPPED"
  65.     end
  66. end
  67.  
  68. local function sendStatus()
  69.     validateStatus()
  70.     local packet = {
  71.         type = "status",
  72.         hostName = LOCAL_ID,
  73.         status = status
  74.     }
  75.     rednet.send(cID, packet, PROTOCOL)
  76.     print("Sent status update:", textutils.serialize(packet))
  77. end
  78.  
  79. -- Helper function to send responses
  80. local function sendResponse(toID, packet)
  81.     rednet.send(toID, packet, PROTOCOL)
  82. end
  83.  
  84. -- Locate Controller
  85. while cID == nil do
  86.     cID = rednet.lookup(PROTOCOL, "controller")
  87.     if cID then
  88.         cID = tonumber(cID)
  89.         print("Found controller, host ID is: " .. cID)
  90.         -- Notify controller of initialization
  91.         local packet = {
  92.             type = "responseId",
  93.             CLIENT_ID = LOCAL_ID -- Responding with CLIENT_ID instead of LOCAL_ID
  94.         }
  95.         rednet.send(cID, packet, PROTOCOL)
  96.         print("Initialization message sent to controller.")
  97.     else
  98.         print("Could not locate controller, retrying in 3 seconds...")
  99.         sleep(3)
  100.     end
  101. end
  102.  
  103. -- Main Loop
  104. while true do
  105.     local senderId, message = rednet.receive(PROTOCOL)
  106.  
  107.     if senderId == cID then
  108.         if message.type == "toggle" then
  109.             toggleFarm()
  110.             sendStatus()
  111.         elseif message.type == "requestStatus" then
  112.             sendStatus()
  113.         elseif message.type == "requestId" then
  114.             print("Received requestId from controller.") -- Debug log
  115.             local responsePacket = {
  116.                 type = "responseId",
  117.                 CLIENT_ID = LOCAL_ID -- Responding with CLIENT_ID instead of LOCAL_ID
  118.             }
  119.             sendResponse(senderId, responsePacket)
  120.             print("Sent CLIENT_ID response to controller.") -- Debug log
  121.         end
  122.     end
  123. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement