minecraft-cc-tools/light-server.lua

151 lines
3.3 KiB
Lua

local rs = require('libs/rs')
local MAIN_DELAY_ON = 0.25
local MAIN_DELAY_OFF = 0.10
local SECONDARY_DELAY_ON = 1.25
local SECONDARY_DELAY_OFF = 0.5
local MAIN_INPUT_SIDE = 'top'
local MAIN_OUTPUT_SIDE = 'left'
local SECONDARY_INPUT_SIDE = 'back'
local SECONDARY_OUTPUT_SIDE = 'front'
local VERSION = '1.1.0'
local function getMainColorsOrder()
return {
colors.white,
colors.orange,
colors.magenta,
colors.lightBlue,
colors.yellow,
colors.lime,
colors.pink,
colors.gray,
colors.lightGray,
colors.cyan,
colors.purple,
colors.blue,
colors.brown,
colors.green,
colors.red,
colors.black
}
end
local function getSecondaryColorsOrder()
return {
colors.white,
colors.orange,
colors.magenta
}
end
local function switchOnLight(bundledOutput, colorsOrder, sleepTime)
if sleepTime <= 0 then
bundledOutput.setColorState(colors.combine(table.unpack(colorsOrder)))
return
end
for _, color in pairs(colorsOrder) do
bundledOutput.setOn(color)
os.sleep(sleepTime)
end
end
local function switchOffLight(bundledOutput, colorsOrder, sleepTime)
if sleepTime <= 0 then
bundledOutput.setColorState(0)
return
end
for _, color in pairs(colorsOrder) do
bundledOutput.setOff(color)
os.sleep(sleepTime)
end
end
local mainLoopQueue = {}
local secondaryLoopQueue = {}
local function applyMainCommand(bundledOutput)
local nextState = table.remove(mainLoopQueue, 1)
if nextState == nil then
return
end
local mainColorsOrder = getMainColorsOrder()
if nextState then
switchOnLight(bundledOutput, mainColorsOrder, MAIN_DELAY_ON)
else
switchOffLight(bundledOutput, mainColorsOrder, MAIN_DELAY_OFF)
end
return applyMainCommand(bundledOutput)
end
local function applySecondaryCommand(bundledOutput)
local nextState = table.remove(secondaryLoopQueue, 1)
if nextState == nil then
return
end
local secondaryColorsOrder = getSecondaryColorsOrder()
if nextState then
switchOnLight(bundledOutput, secondaryColorsOrder, SECONDARY_DELAY_ON)
else
switchOffLight(bundledOutput, secondaryColorsOrder, SECONDARY_DELAY_OFF)
end
return applySecondaryCommand(bundledOutput)
end
local function mainLoop()
local bundledOutput = rs.createBundledOutput(MAIN_OUTPUT_SIDE)
while true do
os.pullEvent('light_command_main')
applyMainCommand(bundledOutput)
end
end
local function secondaryLoop()
local bundledOutput = rs.createBundledOutput(SECONDARY_OUTPUT_SIDE)
while true do
os.pullEvent('light_command_secondary')
applySecondaryCommand(bundledOutput)
end
end
local function redstoneLoop()
local mainState = nil
local secondaryState = nil
while true do
local newMainState = redstone.getInput(MAIN_INPUT_SIDE)
local newSecondaryState = redstone.getInput(SECONDARY_INPUT_SIDE)
if mainState ~= newMainState then
mainState = newMainState
table.insert(mainLoopQueue, newMainState)
os.queueEvent('light_command_main', newMainState)
end
if secondaryState ~= newSecondaryState then
secondaryState = newSecondaryState
table.insert(secondaryLoopQueue, newSecondaryState)
os.queueEvent('light_command_secondary', newSecondaryState)
end
os.pullEvent('redstone')
end
end
print('> Starting light server v' .. VERSION)
parallel.waitForAny(mainLoop, secondaryLoop, redstoneLoop)