65 lines
1.9 KiB
Lua
65 lines
1.9 KiB
Lua
-- Dropper Controller: Runs on Computer 1 (next to dropper_0)
|
|
-- Listens for rednet "dispense" messages and fires redstone to the back.
|
|
|
|
local REDSTONE_SIDE = "back" -- side facing dropper_0
|
|
local PULSE_TIME = 0.3 -- redstone pulse duration in seconds
|
|
local PROTOCOL = "inventory"
|
|
|
|
-------------------------------------------------
|
|
-- Setup
|
|
-------------------------------------------------
|
|
|
|
-- 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
|
|
local function pulseRedstone()
|
|
redstone.setOutput(REDSTONE_SIDE, true)
|
|
sleep(PULSE_TIME)
|
|
redstone.setOutput(REDSTONE_SIDE, false)
|
|
end
|
|
|
|
-------------------------------------------------
|
|
-- Main
|
|
-------------------------------------------------
|
|
|
|
print("=================================")
|
|
print(" Dropper Controller (ID " .. os.getComputerID() .. ")")
|
|
print("=================================")
|
|
print("")
|
|
print("Dropper side: " .. REDSTONE_SIDE)
|
|
print("Listening for dispense commands...")
|
|
print("")
|
|
|
|
if not setupModem() then
|
|
print("Cannot continue without a modem.")
|
|
return
|
|
end
|
|
|
|
while true do
|
|
local senderID, message, protocol = rednet.receive(PROTOCOL)
|
|
|
|
if message == "dispense" then
|
|
print(string.format("[RX] Dispense from computer %d", senderID))
|
|
|
|
-- 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()
|
|
sleep(0.1)
|
|
end
|
|
|
|
print("[OK] Dropper fired (9 pulses)")
|
|
end
|
|
end
|