Update and fix bugs in Cross 3

This commit is contained in:
Kirigaya Kazuto 2017-11-04 13:04:10 +08:00
parent 93332cd6c5
commit 217a27012e

View File

@ -8,13 +8,15 @@ require("queue")
local sides = require("sides")
-- Config your update functions here (Do not change variable name)
local redin1 = proxy("redstone", "")
local redin2 = proxy("redstone", "")
local redin3 = proxy("redstone", "")
local redout1 = proxy("redstone", "")
local redin1 = proxy("redstone", "02")
local redin2 = proxy("redstone", "a9")
local redin3 = proxy("redstone", "ab")
local redout1 = proxy("redstone", "ca")
local redout2 = proxy("redstone", "f3")
-- Redirect Table
local redirect_tb = {
-- Inputs
["ab_st"] = {redin1, sides.north},
["ab_sr"] = {redin1, sides.east},
["ab_lout"] = {redin1, sides.south},
@ -30,15 +32,15 @@ local redirect_tb = {
["c_lout"] = {redin3, sides.south},
["c_left"] = {redin3, sides.west},
-- Outputs
["ab_ks"] = {redout1, sides.north},
["ab_m"] = {redout1, sides.east},
["ba_ks"] = {redout1, sides.south},
["ba_m"] = {redout1, sides.west},
["c_ks"] = {redout2, sides.north},
["c_m"] = {redout2, sides.east},
["last_unused"]={redin1,sides.north}
["last_unused"] = {"unused", sides.north}
}
local function getNameFromRaw(Device, Side)
@ -87,12 +89,14 @@ end
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(
AddEventListener(
"redstone_changed",
function(ev, dev, sd, from, to)
if (getNameFromRaw(dev, sd) == Name) then
print("Calling callback:",callback_func,from,to)
callback_func(from, to)
end
end
@ -100,33 +104,21 @@ local function restone_delegator(Name, callback_func)
)
end
local bus=Queue.new();
local bus = Queue.new()
local function redstone_event(DeviceName, OnEvent, OffEvent)
if(OnEvent~=nil and OffEvent~=nil) then
redstone_delegator(DeviceName,
checkstring(OnEvent)
checkstring(OffEvent)
redstone_delegator(
DeviceName,
function(from, to)
if (from < to) then
bus:push(OnEvent)
else
bus:push(OffEvent)
end
end)
elseif(OnEvent~=nil) then
redstone_delegator(DeviceName,
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
local function doInit()
@ -147,6 +139,12 @@ local function doInit()
redstone_event("c_left", "c_detect_start", "c_detect_end")
end
local function doCleanUp()
while (evl:top() ~= nil) do
RemoveEventListener(evl:pop())
end
end
-- Status
-- Out busy flag
local a_busy, b_busy, c_busy = false, false, false
@ -179,9 +177,9 @@ local function CCSMain()
local running = true
while (running) do
os.sleep(0.25)
local ev="no_evnet"
local ev = "no_event"
if (bus:top() ~= nil) then
ev=bus:pop();
ev = bus:pop()
end
if (ev ~= "no_event") then
@ -217,7 +215,6 @@ local function CCSMain()
if (not done) then
bus:push(ev)
end
elseif (ev == "b_new_train") then -- New Train From B
local done = false
if (readdevice("ba_sr") > 0) then -- Turn to C
@ -242,7 +239,7 @@ local function CCSMain()
end
elseif (ev == "c_new_train") then -- New Train From C
local done = false
if(readdevice("ba_sr")>0) then -- Turn to A
if (readdevice("c_sr") > 0) then -- Turn to A
if ((not a_busy) and readdevice("ba_lout") > 0) then
a_busy = true
enabledevice("c_m")
@ -254,6 +251,7 @@ local function CCSMain()
b_busy = true
crosslocked = true
disabledevice("c_m")
disabledevice("ab_m") -- Must Asure this motor is disabled (So we can move to B)
trigger("c_ks")
done = true
end
@ -264,13 +262,22 @@ local function CCSMain()
end
elseif (ev == "a_detect_end") then
a_busy = false
crosslocked=true
crosslocked = false
elseif (ev == "b_detect_end") then
b_busy = false
crosslocked=true
crosslocked = false
elseif (ev == "c_detect_end") then
c_busy = false
crosslocked=true
crosslocked = false
else
print("Ignoring:", ev)
end
end
doCleanUp()
doClearOutput();
end
print("Program Start")
CCSMain()
print("Program Stop")