Refactor dropper controller to remove rednet dependency and streamline item dispensing logic
This commit is contained in:
@@ -1,27 +1,15 @@
|
|||||||
-- Dropper Controller: Runs on Computer 1 (next to dropper_0)
|
-- Dropper Controller: Runs on Computer 1 (next to dropper_0)
|
||||||
-- Listens for rednet "dispense" messages and fires redstone to the back.
|
-- Watches the dropper and pulses redstone until it's empty.
|
||||||
|
|
||||||
local REDSTONE_SIDE = "back" -- side facing dropper_0
|
local REDSTONE_SIDE = "back" -- side facing dropper_0
|
||||||
local PULSE_TIME = 0.3 -- redstone pulse duration in seconds
|
local PULSE_TIME = 0.3 -- redstone pulse duration in seconds
|
||||||
local PROTOCOL = "inventory"
|
local POLL_INTERVAL = 0.5 -- how often to check the dropper
|
||||||
|
local DROPPER_SIDE = "back" -- side where the dropper is (as peripheral)
|
||||||
|
|
||||||
-------------------------------------------------
|
-------------------------------------------------
|
||||||
-- Setup
|
-- Helpers
|
||||||
-------------------------------------------------
|
-------------------------------------------------
|
||||||
|
|
||||||
-- Open any available modem
|
|
||||||
local function setupModem()
|
|
||||||
for _, side in ipairs({"top","bottom","left","right","front","back"}) do
|
|
||||||
if peripheral.getType(side) == "modem" then
|
|
||||||
rednet.open(side)
|
|
||||||
print("[OK] Modem opened on: " .. side)
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
print("[ERR] No modem found!")
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Send a redstone pulse to trigger the dropper
|
-- Send a redstone pulse to trigger the dropper
|
||||||
local function pulseRedstone()
|
local function pulseRedstone()
|
||||||
redstone.setOutput(REDSTONE_SIDE, true)
|
redstone.setOutput(REDSTONE_SIDE, true)
|
||||||
@@ -29,6 +17,15 @@ local function pulseRedstone()
|
|||||||
redstone.setOutput(REDSTONE_SIDE, false)
|
redstone.setOutput(REDSTONE_SIDE, false)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Check if the dropper has any items
|
||||||
|
local function dropperHasItems()
|
||||||
|
local dropper = peripheral.wrap(DROPPER_SIDE)
|
||||||
|
if not dropper then return false end
|
||||||
|
local contents = dropper.list()
|
||||||
|
if not contents then return false end
|
||||||
|
return next(contents) ~= nil
|
||||||
|
end
|
||||||
|
|
||||||
-------------------------------------------------
|
-------------------------------------------------
|
||||||
-- Main
|
-- Main
|
||||||
-------------------------------------------------
|
-------------------------------------------------
|
||||||
@@ -37,28 +34,22 @@ print("=================================")
|
|||||||
print(" Dropper Controller (ID " .. os.getComputerID() .. ")")
|
print(" Dropper Controller (ID " .. os.getComputerID() .. ")")
|
||||||
print("=================================")
|
print("=================================")
|
||||||
print("")
|
print("")
|
||||||
print("Dropper side: " .. REDSTONE_SIDE)
|
print("Redstone side: " .. REDSTONE_SIDE)
|
||||||
print("Listening for dispense commands...")
|
print("Watching dropper for items...")
|
||||||
print("")
|
print("")
|
||||||
|
|
||||||
if not setupModem() then
|
|
||||||
print("Cannot continue without a modem.")
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
while true do
|
while true do
|
||||||
local senderID, message, protocol = rednet.receive(PROTOCOL)
|
if dropperHasItems() then
|
||||||
|
print("[DISPENSE] Items detected, firing dropper...")
|
||||||
|
|
||||||
if message == "dispense" then
|
-- Keep pulsing until the dropper is empty
|
||||||
print(string.format("[RX] Dispense from computer %d", senderID))
|
while dropperHasItems() do
|
||||||
|
|
||||||
-- Pulse once per item slot in the dropper to eject everything
|
|
||||||
-- A dropper has 9 slots; pulse enough times to empty it
|
|
||||||
for i = 1, 9 do
|
|
||||||
pulseRedstone()
|
pulseRedstone()
|
||||||
sleep(0.1)
|
sleep(0.1)
|
||||||
end
|
end
|
||||||
|
|
||||||
print("[OK] Dropper fired (9 pulses)")
|
print("[OK] Dropper empty.")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
sleep(POLL_INTERVAL)
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user