2017-10-17 15:25:53 +08:00
|
|
|
--[[
|
|
|
|
Station 2/3 Schedule Program
|
|
|
|
]]
|
|
|
|
require("util")
|
2017-10-19 12:46:37 +08:00
|
|
|
local sides = require("sides")
|
|
|
|
local event = require("event")
|
2017-10-17 15:25:53 +08:00
|
|
|
--[[
|
|
|
|
Redstone Signals
|
2017-10-19 11:32:37 +08:00
|
|
|
Direction_[Type][Info]: number
|
2017-10-17 15:25:53 +08:00
|
|
|
]]
|
2017-10-19 12:46:37 +08:00
|
|
|
local ab_st, ab_sr, ab_lin, ab_lout
|
|
|
|
local ba_st, ba_sr, ba_lin, ba_lout
|
2017-10-17 15:25:53 +08:00
|
|
|
|
2017-10-19 12:46:37 +08:00
|
|
|
local ab_ko, ab_m, ab_ks = 0, 0, 0
|
|
|
|
local ba_ko, ba_m, ba_ks = 0, 0, 0
|
|
|
|
local mid_ka, mid_kb = 0, 0
|
2017-10-17 15:25:53 +08:00
|
|
|
|
|
|
|
-- Config your update functions here (Do not change function name)
|
2017-10-19 12:46:37 +08:00
|
|
|
local redin1 = proxy("redstone", "")
|
|
|
|
local redin2 = proxy("redstone", "")
|
|
|
|
local redout1 = proxy("redstone", "")
|
|
|
|
local redout2 = proxy("redstone", "")
|
2017-10-17 15:25:53 +08:00
|
|
|
|
|
|
|
local function updateRedstoneInput()
|
2017-10-19 12:46:37 +08:00
|
|
|
ab_st = redin1.getInput(sides.north)
|
|
|
|
ab_sr = redin1.getInput(sides.east)
|
|
|
|
ba_st = redin1.getInput(sides.south)
|
|
|
|
ba_sr = redin1.getInput(sides.west)
|
|
|
|
ab_lin = redin2.getInput(sides.north)
|
|
|
|
ab_lout = redin2.getInput(sides.east)
|
|
|
|
ba_lin = redin2.getInput(sides.south)
|
|
|
|
ba_lout = redin2.getInput(sides.west)
|
2017-10-17 15:25:53 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
local function updateRedstoneOutput()
|
2017-10-19 12:46:37 +08:00
|
|
|
redout1.setOutput(sides.north, ab_ko)
|
|
|
|
redout1.setOutput(sides.east, ab_m)
|
|
|
|
redout1.setOutput(sides.south, ab_ks)
|
|
|
|
redout1.setOutput(sides.west, mid_ka)
|
|
|
|
redout2.setOutput(sides.north, ba_ko)
|
|
|
|
redout2.setOutput(sides.east, ba_m)
|
|
|
|
redout2.setOutput(sides.south, ba_ks)
|
|
|
|
redout2.setOutput(sides.west, mid_kb)
|
2017-10-17 15:25:53 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Internal Schedule Status (Notice: Program must start without any trains in station)
|
2017-10-19 11:32:37 +08:00
|
|
|
local mid_direction
|
2017-10-19 12:46:37 +08:00
|
|
|
local ab_station_time = 0
|
|
|
|
local ba_station_time = 0
|
|
|
|
local mid_time = 0
|
|
|
|
local ab_exit_time = 0
|
|
|
|
local ba_exit_time = 0
|
2017-10-17 15:25:53 +08:00
|
|
|
|
2017-10-21 17:46:24 +08:00
|
|
|
-- Debug Output
|
|
|
|
local dprint = print
|
|
|
|
|
|
|
|
local function debugClearOutput()
|
|
|
|
if(dprint ~= nil) then
|
|
|
|
os.execute("clear")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function debugOutputInfo()
|
|
|
|
dprint("ab_st",ab_st,"ab_sr",ab_sr,"ba_st",ba_st,"ba_sr",ba_sr,"ab_lin",ab_lin,"ab_lout",ab_lout,"ba_lin",ba_lin,"ba_lout",ba_lout)
|
|
|
|
dprint("mid_direction",mid_direction,"ab_station_time",ab_station_time,"ba_station_time",ba_station_time,"mid_time",mid_time,"ab_exit_time",ab_exit_time,"ba_exit_time",ba_exit_time)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function debugValueInfo()
|
|
|
|
dprint("ab_ko",ab_ko,"ab_m",ab_m,"ab_ks",ab_ks,"mid_ka",mid_ka,"ba_ko",ba_ko,"ba_m",ba_m,"ba_ks",ba_ks,"mid_kb",mid_kb)
|
|
|
|
end
|
|
|
|
|
2017-10-17 22:12:08 +08:00
|
|
|
--[[
|
|
|
|
Internal Functions
|
|
|
|
]]
|
2017-10-17 15:25:53 +08:00
|
|
|
local function doInit()
|
|
|
|
-- Flash output to zero.
|
|
|
|
updateRedstoneOutput()
|
|
|
|
end
|
|
|
|
|
2017-10-21 17:46:24 +08:00
|
|
|
local function doCheck()
|
|
|
|
if(redin1==nil or redin2==nil or redout1==nil or redout2==nil) then
|
|
|
|
print("Redstone Configure")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-17 15:25:53 +08:00
|
|
|
-- Main Program
|
|
|
|
local function main()
|
2017-10-21 17:46:24 +08:00
|
|
|
doCheck()
|
2017-10-17 15:25:53 +08:00
|
|
|
doInit()
|
2017-10-17 22:12:08 +08:00
|
|
|
while true do
|
2017-10-21 17:46:24 +08:00
|
|
|
debugClearOutput()
|
2017-10-17 22:12:08 +08:00
|
|
|
-- Flush input
|
|
|
|
updateRedstoneInput()
|
2017-10-21 17:46:24 +08:00
|
|
|
debugOutputInfo()
|
2017-10-17 22:12:08 +08:00
|
|
|
-- Update status
|
2017-10-19 12:46:37 +08:00
|
|
|
if (ab_station_time > 0) then
|
|
|
|
ab_station_time = ab_station_time + 1
|
2017-10-19 11:32:37 +08:00
|
|
|
end
|
2017-10-19 12:46:37 +08:00
|
|
|
if (ba_station_time > 0) then
|
|
|
|
ba_station_time = ba_station_time + 1
|
2017-10-18 08:57:35 +08:00
|
|
|
end
|
2017-10-19 12:46:37 +08:00
|
|
|
if (mid_time > 0) then
|
|
|
|
mid_time = mid_time + 1
|
2017-10-18 08:57:35 +08:00
|
|
|
end
|
2017-10-19 12:46:37 +08:00
|
|
|
if (ab_exit_time > 0) then
|
|
|
|
ab_exit_time = ab_exit_time + 1
|
|
|
|
if (ab_exit_time > 10) then -- Exit will reset in 10 loops (5seconds)
|
|
|
|
ab_exit_time = 0
|
|
|
|
end
|
2017-10-18 08:57:35 +08:00
|
|
|
end
|
2017-10-19 12:46:37 +08:00
|
|
|
if (ba_exit_time > 0) then
|
|
|
|
ba_exit_time = ba_exit_time + 1
|
|
|
|
if (ba_exit_time > 10) then -- Exit will reset in 10 loops (5seconds)
|
|
|
|
ba_exit_time = 0
|
|
|
|
end
|
2017-10-18 08:57:35 +08:00
|
|
|
end
|
2017-10-17 22:12:08 +08:00
|
|
|
|
|
|
|
-- Judge Incoming bus.
|
2017-10-19 12:46:37 +08:00
|
|
|
if (ab_st > 0) then -- New incoming bus from A to B
|
|
|
|
if (ab_sr > 0) then -- This bus want to stop
|
|
|
|
if (ab_station_time == 0) then -- If AB Station is free
|
2017-10-21 17:46:24 +08:00
|
|
|
dprint("A-->B Train In")
|
2017-10-19 12:46:37 +08:00
|
|
|
ab_ko = 15 -- enable to allow incoming
|
|
|
|
ab_ks = 0 -- disable to let it stop
|
|
|
|
ab_m = 0 -- disable to allow incoming to station
|
|
|
|
ab_station_time = 1 -- Start Time Counter
|
2017-10-17 22:12:08 +08:00
|
|
|
else -- AB Station is not free
|
|
|
|
-- This train should wait outside the station
|
2017-10-21 17:46:24 +08:00
|
|
|
dprint("A-->B Train Pending")
|
2017-10-17 22:12:08 +08:00
|
|
|
end
|
|
|
|
else -- This bus want to pass by
|
2017-10-19 12:46:37 +08:00
|
|
|
if (mid_time == 0) then -- Mid is free
|
2017-10-21 17:46:24 +08:00
|
|
|
dprint("A-->Mid Train In")
|
2017-10-19 12:46:37 +08:00
|
|
|
ab_ko = 15 -- enable to allow incoming
|
|
|
|
ab_m = 15 -- enable motor to let it pass.
|
|
|
|
mid_ka = 15 -- enable switch from A
|
|
|
|
mid_kb = 0 -- disable switch to B
|
|
|
|
mid_direction = "ab"
|
|
|
|
mid_time = 1 -- Start time counter
|
2017-10-17 22:12:08 +08:00
|
|
|
else -- Mid is busy
|
2017-10-21 17:46:24 +08:00
|
|
|
dprint("A-->Mid Train Pending")
|
2017-10-17 22:12:08 +08:00
|
|
|
-- This train should wait outside the station
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-19 12:46:37 +08:00
|
|
|
if (ba_st > 0) then -- New incoming bus from B to A
|
|
|
|
if (ba_sr > 0) then -- This bus want to stop
|
|
|
|
if (ba_station_time == 0) then -- If BA Station is free
|
2017-10-21 17:46:24 +08:00
|
|
|
dprint("B-->A Train In")
|
2017-10-19 12:46:37 +08:00
|
|
|
ba_ko = 15 -- enable to allow incoming
|
|
|
|
ba_ks = 0 -- disable to let it stop
|
|
|
|
ba_m = 0 -- disable to allow incoming to station
|
|
|
|
ba_station_time = 1 -- Start Time Counter
|
2017-10-17 22:12:08 +08:00
|
|
|
else -- BA Station is not free
|
|
|
|
-- This train should wait outside the station
|
2017-10-21 17:46:24 +08:00
|
|
|
dprint("B-->A Train Pending")
|
2017-10-17 22:12:08 +08:00
|
|
|
end
|
|
|
|
else -- This bus want to pass by
|
2017-10-19 12:46:37 +08:00
|
|
|
if (mid_time == 0) then -- Mid is free
|
2017-10-21 17:46:24 +08:00
|
|
|
dprint("B-->Mid Train In")
|
2017-10-19 12:46:37 +08:00
|
|
|
ba_ko = 15 -- enable to allow incoming
|
|
|
|
ba_m = 15 -- enable motor to let it pass.
|
|
|
|
mid_ka = 0
|
|
|
|
mid_kb = 15
|
|
|
|
mid_direction = "ba"
|
|
|
|
mid_time = 1
|
2017-10-17 22:12:08 +08:00
|
|
|
else -- Mid is busy
|
2017-10-21 17:46:24 +08:00
|
|
|
dprint("B-->Mid Train Pending")
|
2017-10-17 22:12:08 +08:00
|
|
|
-- This train should wait outside the station
|
|
|
|
end
|
2017-10-19 12:46:37 +08:00
|
|
|
end
|
2017-10-18 08:57:35 +08:00
|
|
|
end
|
2017-10-19 12:46:37 +08:00
|
|
|
|
|
|
|
if (ab_lout > 0 and ab_exit_time == 0) then -- AB next free
|
|
|
|
-- Judge which train should pass.
|
|
|
|
if (ab_station_time > 16 and (mid_time > 0 and mid_direction == "ab")) then -- Two Trains
|
|
|
|
if (ab_station_time > mid_time) then -- StationTrain wait longer.
|
2017-10-21 17:46:24 +08:00
|
|
|
dprint("A-->B Train Out")
|
2017-10-19 12:46:37 +08:00
|
|
|
ab_station_time = 0 -- Stop counter
|
|
|
|
ab_ks = 15 -- enable swith to let it go
|
|
|
|
ab_exit_time = 1
|
|
|
|
else -- MidTrain wait longer
|
2017-10-21 17:46:24 +08:00
|
|
|
dprint("Mid-->B Train out")
|
2017-10-19 12:46:37 +08:00
|
|
|
mid_time = 0 -- Stop Counter
|
|
|
|
mid_kb = 15
|
|
|
|
ab_exit_time = 1
|
|
|
|
end
|
|
|
|
elseif (ab_station_time > 16) then --Only Station Train
|
2017-10-21 17:46:24 +08:00
|
|
|
dprint("A-->B Train Out")
|
2017-10-19 12:46:37 +08:00
|
|
|
ab_station_time = 0
|
|
|
|
ab_ks = 15
|
|
|
|
ab_exit_time = 1
|
|
|
|
elseif (mid_time > 0 and mid_direction == "ab") then -- Only Mid Train
|
2017-10-21 17:46:24 +08:00
|
|
|
dprint("Mid-->B Train Out")
|
2017-10-19 12:46:37 +08:00
|
|
|
mid_time = 0
|
|
|
|
mid_kb = 15
|
|
|
|
ab_exit_time = 1
|
|
|
|
end -- No train
|
2017-10-19 11:32:37 +08:00
|
|
|
end -- End of AB judge
|
2017-10-19 12:46:37 +08:00
|
|
|
|
|
|
|
if (ba_lout > 0 and ba_exit_time == 0) then -- BA next free
|
|
|
|
-- Judge which train should pass.
|
|
|
|
if (ba_station_time > 16 and (mid_time > 0 and mid_direction == "ba")) then -- Two Trains
|
|
|
|
if (ba_station_time > mid_time) then -- StationTrain wait longer.
|
2017-10-21 17:46:24 +08:00
|
|
|
dprint("B-->A Train Out")
|
2017-10-19 12:46:37 +08:00
|
|
|
ba_station_time = 0 -- Stop counter
|
|
|
|
ba_ks = 15 -- enable swith to let it go
|
|
|
|
ba_exit_time = 1
|
|
|
|
else -- MidTrain wait longer
|
2017-10-21 17:46:24 +08:00
|
|
|
dprint("Mid-->A Train Out")
|
2017-10-19 12:46:37 +08:00
|
|
|
mid_time = 0 -- Stop Counter
|
|
|
|
mid_ka = 15
|
|
|
|
ba_exit_time = 1
|
|
|
|
end
|
|
|
|
elseif (ba_station_time > 16) then --Only Station Train
|
2017-10-21 17:46:24 +08:00
|
|
|
dprint("B-->A Train Out")
|
2017-10-19 12:46:37 +08:00
|
|
|
ba_station_time = 0
|
|
|
|
ba_ks = 15
|
|
|
|
ba_exit_time = 1
|
|
|
|
elseif (mid_time > 0 and mid_direction == "ba") then -- Only Mid Train
|
2017-10-21 17:46:24 +08:00
|
|
|
dprint("Mid-->A Train Out")
|
2017-10-19 12:46:37 +08:00
|
|
|
mid_time = 0
|
|
|
|
mid_ka = 15
|
|
|
|
ba_exit_time = 1
|
|
|
|
end -- No train
|
2017-10-17 22:12:08 +08:00
|
|
|
end
|
|
|
|
|
2017-10-21 17:46:24 +08:00
|
|
|
debugValueInfo()
|
|
|
|
|
2017-10-17 22:12:08 +08:00
|
|
|
-- Set output
|
|
|
|
updateRedstoneOutput()
|
|
|
|
|
|
|
|
-- Sleep for next loop
|
2017-10-21 17:46:24 +08:00
|
|
|
dprint("==========")
|
2017-10-17 22:12:08 +08:00
|
|
|
os.sleep(0.5)
|
|
|
|
end
|
2017-10-19 12:46:37 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
print("Start!")
|
|
|
|
main()
|
|
|
|
print("Stop!")
|