mirror of
https://github.com/Kiritow/OpenComputerScripts.git
synced 2024-03-22 13:10:46 +08:00
Update and fix bugs in Cross 3
This commit is contained in:
parent
93332cd6c5
commit
217a27012e
317
cross_3.lua
317
cross_3.lua
|
@ -8,37 +8,39 @@ require("queue")
|
||||||
local sides = require("sides")
|
local sides = require("sides")
|
||||||
|
|
||||||
-- Config your update functions here (Do not change variable name)
|
-- Config your update functions here (Do not change variable name)
|
||||||
local redin1 = proxy("redstone", "")
|
local redin1 = proxy("redstone", "02")
|
||||||
local redin2 = proxy("redstone", "")
|
local redin2 = proxy("redstone", "a9")
|
||||||
local redin3 = proxy("redstone", "")
|
local redin3 = proxy("redstone", "ab")
|
||||||
local redout1 = proxy("redstone", "")
|
local redout1 = proxy("redstone", "ca")
|
||||||
|
local redout2 = proxy("redstone", "f3")
|
||||||
|
|
||||||
-- Redirect Table
|
-- Redirect Table
|
||||||
local redirect_tb = {
|
local redirect_tb = {
|
||||||
["ab_st"]={redin1,sides.north},
|
-- Inputs
|
||||||
["ab_sr"]={redin1,sides.east},
|
["ab_st"] = {redin1, sides.north},
|
||||||
["ab_lout"]={redin1,sides.south},
|
["ab_sr"] = {redin1, sides.east},
|
||||||
["ab_left"]={redin1,sides.west},
|
["ab_lout"] = {redin1, sides.south},
|
||||||
|
["ab_left"] = {redin1, sides.west},
|
||||||
|
|
||||||
["ba_st"]={redin2,sides.north},
|
["ba_st"] = {redin2, sides.north},
|
||||||
["ba_sr"]={redin2,sides.east},
|
["ba_sr"] = {redin2, sides.east},
|
||||||
["ba_lout"]={redin2,sides.south},
|
["ba_lout"] = {redin2, sides.south},
|
||||||
["ba_left"]={redin2,sides.west},
|
["ba_left"] = {redin2, sides.west},
|
||||||
|
|
||||||
["c_st"]={redin3,sides.north},
|
["c_st"] = {redin3, sides.north},
|
||||||
["c_sr"]={redin3,sides.east},
|
["c_sr"] = {redin3, sides.east},
|
||||||
["c_lout"]={redin3,sides.south},
|
["c_lout"] = {redin3, sides.south},
|
||||||
["c_left"]={redin3,sides.west},
|
["c_left"] = {redin3, sides.west},
|
||||||
|
|
||||||
["ab_ks"]={redout1,sides.north},
|
-- Outputs
|
||||||
["ab_m"]={redout1,sides.east},
|
["ab_ks"] = {redout1, sides.north},
|
||||||
["ba_ks"]={redout1,sides.south},
|
["ab_m"] = {redout1, sides.east},
|
||||||
["ba_m"]={redout1,sides.west},
|
["ba_ks"] = {redout1, sides.south},
|
||||||
|
["ba_m"] = {redout1, sides.west},
|
||||||
|
["c_ks"] = {redout2, sides.north},
|
||||||
|
["c_m"] = {redout2, sides.east},
|
||||||
|
|
||||||
["c_ks"]={redout2,sides.north},
|
["last_unused"] = {"unused", sides.north}
|
||||||
["c_m"]={redout2,sides.east},
|
|
||||||
|
|
||||||
["last_unused"]={redin1,sides.north}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
local function getNameFromRaw(Device, Side)
|
local function getNameFromRaw(Device, Side)
|
||||||
|
@ -87,12 +89,14 @@ end
|
||||||
|
|
||||||
local evl = Queue.new()
|
local evl = Queue.new()
|
||||||
|
|
||||||
local function restone_delegator(Name, callback_func)
|
local function redstone_delegator(Name, callback_func)
|
||||||
|
print("Add Redstone Delegator ",Name,callback_func)
|
||||||
evl:push(
|
evl:push(
|
||||||
AddEventListener(
|
AddEventListener(
|
||||||
"redstone_changed",
|
"redstone_changed",
|
||||||
function(ev, dev, sd, from, to)
|
function(ev, dev, sd, from, to)
|
||||||
if (getNameFromRaw(dev, sd) == Name) then
|
if (getNameFromRaw(dev, sd) == Name) then
|
||||||
|
print("Calling callback:",callback_func,from,to)
|
||||||
callback_func(from, to)
|
callback_func(from, to)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -100,37 +104,25 @@ local function restone_delegator(Name, callback_func)
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
local bus=Queue.new();
|
local bus = Queue.new()
|
||||||
|
|
||||||
local function redstone_event(DeviceName,OnEvent,OffEvent)
|
local function redstone_event(DeviceName, OnEvent, OffEvent)
|
||||||
if(OnEvent~=nil and OffEvent~=nil) then
|
checkstring(OnEvent)
|
||||||
redstone_delegator(DeviceName,
|
checkstring(OffEvent)
|
||||||
function(from,to)
|
redstone_delegator(
|
||||||
if(from<to) then
|
DeviceName,
|
||||||
bus:push(OnEvent)
|
function(from, to)
|
||||||
else
|
if (from < to) then
|
||||||
bus:push(OffEvent)
|
bus:push(OnEvent)
|
||||||
end
|
else
|
||||||
end)
|
bus:push(OffEvent)
|
||||||
elseif(OnEvent~=nil) then
|
end
|
||||||
redstone_delegator(DeviceName,
|
end
|
||||||
function(from,to)
|
)
|
||||||
if(from<to) then
|
|
||||||
bus:push(OnEvent)
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
elseif(OffEvent~=nil) then
|
|
||||||
redstone_delegator(DeviceName,
|
|
||||||
function(from,to)
|
|
||||||
if(from>to) then
|
|
||||||
bus:push(OffEvent)
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function doInit()
|
local function doInit()
|
||||||
evl:push(
|
evl:push(
|
||||||
AddEventListener(
|
AddEventListener(
|
||||||
"interrupted",
|
"interrupted",
|
||||||
function()
|
function()
|
||||||
|
@ -139,21 +131,27 @@ local function doInit()
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
redstone_event("ab_st","a_new_train","a_train_left")
|
redstone_event("ab_st", "a_new_train", "a_train_left")
|
||||||
redstone_event("ba_st","b_new_train","b_train_left")
|
redstone_event("ba_st", "b_new_train", "b_train_left")
|
||||||
redstone_event("c_st","c_new_train","c_train_left")
|
redstone_event("c_st", "c_new_train", "c_train_left")
|
||||||
redstone_event("ab_left","b_detect_start","b_detect_end")
|
redstone_event("ab_left", "b_detect_start", "b_detect_end")
|
||||||
redstone_event("ba_left","a_detect_start","a_detect_end")
|
redstone_event("ba_left", "a_detect_start", "a_detect_end")
|
||||||
redstone_event("c_left","c_detect_start","c_detect_end")
|
redstone_event("c_left", "c_detect_start", "c_detect_end")
|
||||||
|
end
|
||||||
|
|
||||||
|
local function doCleanUp()
|
||||||
|
while (evl:top() ~= nil) do
|
||||||
|
RemoveEventListener(evl:pop())
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Status
|
-- Status
|
||||||
-- Out busy flag
|
-- Out busy flag
|
||||||
local a_busy,b_busy,c_busy=false,false,false
|
local a_busy, b_busy, c_busy = false, false, false
|
||||||
local crosslocked=false
|
local crosslocked = false
|
||||||
|
|
||||||
local function doCheck()
|
local function doCheck()
|
||||||
for k, vt in pairs(redirect_tb) do
|
for k, vt in pairs(redirect_tb) do
|
||||||
if (vt[1] == nil) then
|
if (vt[1] == nil) then
|
||||||
error("Check Failed. Please review your redstone configure")
|
error("Check Failed. Please review your redstone configure")
|
||||||
end
|
end
|
||||||
|
@ -163,114 +161,123 @@ local function doCheck()
|
||||||
end
|
end
|
||||||
|
|
||||||
local function doClearOutput()
|
local function doClearOutput()
|
||||||
local smt={"ab_ks","ab_m","ba_ks","ba_m","c_ks","c_m"}
|
local smt = {"ab_ks", "ab_m", "ba_ks", "ba_m", "c_ks", "c_m"}
|
||||||
|
|
||||||
for k, v in pairs(smt) do
|
for k, v in pairs(smt) do
|
||||||
disabledevice(v)
|
disabledevice(v)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function CCSMain()
|
local function CCSMain()
|
||||||
doCheck()
|
doCheck()
|
||||||
doClearOutput()
|
doClearOutput()
|
||||||
doInit()
|
doInit()
|
||||||
|
|
||||||
print("CCS Started. Press Ctrl+C to stop.")
|
print("CCS Started. Press Ctrl+C to stop.")
|
||||||
local running=true
|
local running = true
|
||||||
while(running) do
|
while (running) do
|
||||||
os.sleep(0.25)
|
os.sleep(0.25)
|
||||||
local ev="no_evnet"
|
local ev = "no_event"
|
||||||
if(bus:top()~=nil) then
|
if (bus:top() ~= nil) then
|
||||||
ev=bus:pop();
|
ev = bus:pop()
|
||||||
end
|
end
|
||||||
|
|
||||||
if (ev ~= "no_event") then
|
if (ev ~= "no_event") then
|
||||||
print(ev)
|
print(ev)
|
||||||
end
|
end
|
||||||
|
|
||||||
if (ev == "no_event") then
|
if (ev == "no_event") then
|
||||||
-- No event, no action.
|
-- No event, no action.
|
||||||
elseif (ev == "stop") then
|
elseif (ev == "stop") then
|
||||||
running = false
|
running = false
|
||||||
elseif (ev=="delay") then
|
elseif (ev == "delay") then
|
||||||
os.sleep(0.25)
|
os.sleep(0.25)
|
||||||
elseif (ev=="a_new_train") then -- New Train From A
|
elseif (ev == "a_new_train") then -- New Train From A
|
||||||
local done=false
|
local done = false
|
||||||
if(readdevice("ab_sr")>0) then -- Turn to C
|
if (readdevice("ab_sr") > 0) then -- Turn to C
|
||||||
if( (not c_busy) and (not crosslocked) and readdevice("c_lout")>0) then
|
if ((not c_busy) and (not crosslocked) and readdevice("c_lout") > 0) then
|
||||||
c_busy=true
|
c_busy = true
|
||||||
crosslocked=true
|
crosslocked = true
|
||||||
enabledevice("ab_m")
|
enabledevice("ab_m")
|
||||||
trigger("ab_ks")
|
trigger("ab_ks")
|
||||||
done=true
|
done = true
|
||||||
end
|
end
|
||||||
else -- Go directly to B
|
else -- Go directly to B
|
||||||
if( (not b_busy) and (not crosslocked) and readdevice("ab_lout")>0) then
|
if ((not b_busy) and (not crosslocked) and readdevice("ab_lout") > 0) then
|
||||||
b_busy=true
|
b_busy = true
|
||||||
crosslocked=true
|
crosslocked = true
|
||||||
disabledevice("ab_m")
|
disabledevice("ab_m")
|
||||||
trigger("ab_ks")
|
trigger("ab_ks")
|
||||||
done=true
|
done = true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if(not done) then
|
if (not done) then
|
||||||
bus:push(ev)
|
bus:push(ev)
|
||||||
end
|
end
|
||||||
|
elseif (ev == "b_new_train") then -- New Train From B
|
||||||
|
local done = false
|
||||||
|
if (readdevice("ba_sr") > 0) then -- Turn to C
|
||||||
|
if ((not c_busy) and readdevice("c_lout") > 0) then
|
||||||
|
c_busy = true
|
||||||
|
enabledevice("ba_m")
|
||||||
|
trigger("ba_ks")
|
||||||
|
done = true
|
||||||
|
end
|
||||||
|
else -- Go directly to A
|
||||||
|
if ((not a_busy) and (not crosslocked) and readdevice("ba_lout") > 0) then
|
||||||
|
a_busy = true
|
||||||
|
crosslocked = true
|
||||||
|
disabledevice("ba_m")
|
||||||
|
trigger("ba_ks")
|
||||||
|
done = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
elseif (ev=="b_new_train") then -- New Train From B
|
if (not done) then
|
||||||
local done=false
|
bus:push(ev)
|
||||||
if(readdevice("ba_sr")>0) then -- Turn to C
|
end
|
||||||
if( (not c_busy) and readdevice("c_lout")>0) then
|
elseif (ev == "c_new_train") then -- New Train From C
|
||||||
c_busy=true
|
local done = false
|
||||||
enabledevice("ba_m")
|
if (readdevice("c_sr") > 0) then -- Turn to A
|
||||||
trigger("ba_ks")
|
if ((not a_busy) and readdevice("ba_lout") > 0) then
|
||||||
done=true
|
a_busy = true
|
||||||
end
|
enabledevice("c_m")
|
||||||
else -- Go directly to A
|
trigger("c_ks")
|
||||||
if( (not a_busy) and (not crosslocked) and readdevice("ba_lout")>0) then
|
done = true
|
||||||
a_busy=true
|
end
|
||||||
crosslocked=true
|
else -- Go directly to B
|
||||||
disabledevice("ba_m")
|
if ((not b_busy) and (not crosslocked) and readdevice("ab_lout") > 0) then
|
||||||
trigger("ba_ks")
|
b_busy = true
|
||||||
done=true
|
crosslocked = true
|
||||||
end
|
disabledevice("c_m")
|
||||||
end
|
disabledevice("ab_m") -- Must Asure this motor is disabled (So we can move to B)
|
||||||
|
trigger("c_ks")
|
||||||
|
done = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
if(not done) then
|
if (not done) then
|
||||||
bus:push(ev)
|
bus:push(ev)
|
||||||
end
|
end
|
||||||
elseif (ev=="c_new_train") then -- New Train From C
|
elseif (ev == "a_detect_end") then
|
||||||
local done=false
|
a_busy = false
|
||||||
if(readdevice("ba_sr")>0) then -- Turn to A
|
crosslocked = false
|
||||||
if( (not a_busy) and readdevice("ba_lout")>0) then
|
elseif (ev == "b_detect_end") then
|
||||||
a_busy=true
|
b_busy = false
|
||||||
enabledevice("c_m")
|
crosslocked = false
|
||||||
trigger("c_ks")
|
elseif (ev == "c_detect_end") then
|
||||||
done=true
|
c_busy = false
|
||||||
end
|
crosslocked = false
|
||||||
else -- Go directly to B
|
else
|
||||||
if( (not b_busy) and (not crosslocked) and readdevice("ab_lout")>0) then
|
print("Ignoring:", ev)
|
||||||
b_busy=true
|
end
|
||||||
crosslocked=true
|
|
||||||
disabledevice("c_m")
|
|
||||||
trigger("c_ks")
|
|
||||||
done=true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if(not done) then
|
|
||||||
bus:push(ev)
|
|
||||||
end
|
|
||||||
elseif(ev=="a_detect_end") then
|
|
||||||
a_busy=false
|
|
||||||
crosslocked=true
|
|
||||||
elseif(ev=="b_detect_end") then
|
|
||||||
b_busy=false
|
|
||||||
crosslocked=true
|
|
||||||
elseif(ev=="c_detect_end") then
|
|
||||||
c_busy=false
|
|
||||||
crosslocked=true
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
doCleanUp()
|
||||||
|
doClearOutput();
|
||||||
end
|
end
|
||||||
|
|
||||||
|
print("Program Start")
|
||||||
|
CCSMain()
|
||||||
|
print("Program Stop")
|
Loading…
Reference in New Issue
Block a user