2018-01-04 13:44:40 +08:00
|
|
|
local component=require("component")
|
2018-01-04 23:28:53 +08:00
|
|
|
local sides=require("sides")
|
2018-01-05 23:32:58 +08:00
|
|
|
local thread=require("thread")
|
2018-01-04 23:12:30 +08:00
|
|
|
require("libevent")
|
2018-01-07 22:32:08 +08:00
|
|
|
require("libnetbox")
|
2018-01-04 13:44:40 +08:00
|
|
|
require("util")
|
|
|
|
|
|
|
|
--- Auto Configure
|
|
|
|
local digital_controller = proxy("digital_controller_box")
|
|
|
|
local digital_receiver = proxy("digital_receiver_box")
|
2018-01-04 23:12:30 +08:00
|
|
|
local out_ticket = proxy("routing_track")
|
2018-01-04 13:44:40 +08:00
|
|
|
|
|
|
|
--- Manually Configure
|
2018-01-07 22:32:08 +08:00
|
|
|
local load_detector = proxy("digital_detector","8")
|
|
|
|
local unload_detector = proxy("digital_detector","1")
|
2018-01-04 23:12:30 +08:00
|
|
|
|
2018-01-07 22:32:08 +08:00
|
|
|
local load_transposer = proxy("transposer","c")
|
|
|
|
local unload_transposer = proxy("transposer","5")
|
2018-01-04 23:12:30 +08:00
|
|
|
|
2018-01-07 22:32:08 +08:00
|
|
|
local route_ab_load = proxy("routing_switch","c")
|
|
|
|
local route_ba_load = proxy("routing_switch","6")
|
|
|
|
local route_ab_unload = proxy("routing_switch","8")
|
|
|
|
local route_ba_unload = proxy("routing_switch","2")
|
2018-01-04 23:12:30 +08:00
|
|
|
|
2018-01-04 23:28:53 +08:00
|
|
|
--- Internal Variables
|
|
|
|
local load_box_side
|
|
|
|
local unload_box_side
|
2018-01-05 08:39:49 +08:00
|
|
|
local loading=0 -- 0 Free 1 Ready 2 Processing
|
|
|
|
local unloading=0
|
2018-01-05 23:32:58 +08:00
|
|
|
local lockway=0 -- 0 Free 1 Loading 2 Unloading
|
2018-01-04 23:28:53 +08:00
|
|
|
|
2018-01-05 23:32:58 +08:00
|
|
|
-- Value: 1 Green 2 Blinking Yellow 3 Yellow 4 Blinking Red 5 Red
|
2018-01-04 13:44:40 +08:00
|
|
|
local green=1
|
2018-01-05 23:32:58 +08:00
|
|
|
local byellow=2
|
|
|
|
local yellow=3
|
2018-01-04 13:44:40 +08:00
|
|
|
local bred=4
|
|
|
|
local red=5
|
|
|
|
|
|
|
|
local function setSignal(name,value)
|
|
|
|
digital_controller.setAspect(name,value)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function checkDevice()
|
2018-01-04 23:12:30 +08:00
|
|
|
print("Checking Devices...")
|
|
|
|
|
2018-01-04 13:44:40 +08:00
|
|
|
local function doCheckDevice(device)
|
|
|
|
if(device==nil) then
|
|
|
|
error("Some device is nil. Please double check your configure.")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
doCheckDevice(digital_controller)
|
|
|
|
doCheckDevice(digital_receiver)
|
2018-01-04 23:12:30 +08:00
|
|
|
doCheckDevice(out_ticket)
|
|
|
|
|
|
|
|
doCheckDevice(load_detector)
|
|
|
|
doCheckDevice(unload_detector)
|
|
|
|
doCheckDevice(route_ab_load)
|
|
|
|
doCheckDevice(route_ba_load)
|
|
|
|
doCheckDevice(route_ab_unload)
|
|
|
|
doCheckDevice(route_ba_unload)
|
2018-01-04 13:44:40 +08:00
|
|
|
|
|
|
|
local t=digital_controller.getSignalNames()
|
|
|
|
|
|
|
|
local function checkSigName(name)
|
|
|
|
local found=false
|
|
|
|
for k,v in pairs(t) do
|
|
|
|
if(v==name) then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
error("CheckSigName: Failed to check signal: " .. name)
|
|
|
|
end
|
|
|
|
|
2018-01-04 23:12:30 +08:00
|
|
|
checkSigName("AInCtrl")
|
|
|
|
checkSigName("BInCtrl")
|
|
|
|
checkSigName("LoadCartCtrl")
|
|
|
|
checkSigName("LoadBoxCtrl")
|
2018-01-05 23:32:58 +08:00
|
|
|
checkSigName("LoadLamp")
|
2018-01-04 23:12:30 +08:00
|
|
|
checkSigName("UnloadCartCtrl")
|
|
|
|
checkSigName("UnloadBoxCtrl")
|
2018-01-05 23:32:58 +08:00
|
|
|
checkSigName("UnloadLamp")
|
2018-01-04 23:12:30 +08:00
|
|
|
checkSigName("OutCtrl")
|
2018-01-05 08:39:49 +08:00
|
|
|
checkSigName("OutSwitchCtrl")
|
2018-01-04 13:44:40 +08:00
|
|
|
|
|
|
|
t=digital_receiver.getSignalNames()
|
2018-01-05 23:32:58 +08:00
|
|
|
local function checkSigNameX(name)
|
|
|
|
checkSigName(name)
|
|
|
|
if(digital_receiver.getAspect(name)~=red) then
|
|
|
|
error("CheckSigNameX: Failed to check cart signals. Value must be red while initializing.")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
checkSigNameX("LoadCartSig")
|
|
|
|
checkSigNameX("UnloadCartSig")
|
2018-01-04 23:12:30 +08:00
|
|
|
|
|
|
|
local function checkRoutingTable(device)
|
|
|
|
if(device.getRoutingTableTitle()==false) then
|
|
|
|
error("CheckRoutingTable: Failed to check routing table. Please insert a routing table in it.")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
checkRoutingTable(route_ab_load)
|
|
|
|
checkRoutingTable(route_ba_load)
|
|
|
|
checkRoutingTable(route_ab_unload)
|
|
|
|
checkRoutingTable(route_ba_unload)
|
|
|
|
|
|
|
|
local function checkRoutingTicket(device)
|
|
|
|
if(device.getDestination()==false) then
|
|
|
|
error("CheckRoutingTicket: Failed to check routing track. Please insert a golden ticket in it.")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
checkRoutingTicket(out_ticket)
|
2018-01-04 13:44:40 +08:00
|
|
|
|
2018-01-04 23:28:53 +08:00
|
|
|
local function checkChest(device)
|
|
|
|
if(device.getInventorySize(sides.down)==nil) then
|
|
|
|
error("CheckChest: Failed to check chest. Cache Chest must exists.")
|
|
|
|
end
|
|
|
|
|
|
|
|
for i=1,device.getInventorySize(sides.down),1 do
|
|
|
|
if(device.getStackInSlot(sides.down,i)~=nil) then
|
|
|
|
error("CheckChest: Failed to check chest. Cache Chest not empty.")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local tsd
|
|
|
|
|
|
|
|
local dr={sides.north,sides.south,sides.east,sides.west}
|
|
|
|
for k,v in pairs(dr) do
|
|
|
|
if(device.getInventorySize(v)~=nil) then
|
|
|
|
tsd=v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if(tsd==nil) then
|
|
|
|
error("CheckChest: Failed to check chest. Normal Chest must exists.")
|
|
|
|
end
|
|
|
|
|
|
|
|
for i=1,device.getInventorySize(tsd),1 do
|
|
|
|
if(device.getStackInSlot(tsd,i)~=nil) then
|
|
|
|
error("CheckChest: Failed to check chest. Normal Chest not empty.")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return tsd
|
|
|
|
end
|
|
|
|
|
|
|
|
load_box_side=checkChest(load_transposer)
|
|
|
|
unload_box_side=checkChest(unload_transposer)
|
|
|
|
|
2018-01-04 13:44:40 +08:00
|
|
|
print("Check device pass.")
|
|
|
|
end
|
|
|
|
|
|
|
|
local function resetDevice()
|
2018-01-04 23:12:30 +08:00
|
|
|
print("Reseting Devices...")
|
2018-01-04 23:28:53 +08:00
|
|
|
|
2018-01-04 13:44:40 +08:00
|
|
|
digital_controller.setEveryAspect(red)
|
2018-01-05 23:32:58 +08:00
|
|
|
setSignal("UnloadBoxCtrl",green) --- Lock unload box.
|
|
|
|
setSignal("LoadLamp",green)
|
2018-01-04 13:44:40 +08:00
|
|
|
|
2018-01-04 23:12:30 +08:00
|
|
|
route_ab_load.setRoutingTable({})
|
|
|
|
route_ba_load.setRoutingTable({})
|
|
|
|
route_ab_unload.setRoutingTable({})
|
|
|
|
route_ba_unload.setRoutingTable({})
|
2018-01-04 13:44:40 +08:00
|
|
|
|
|
|
|
print("Device reset done.")
|
|
|
|
end
|
|
|
|
|
2018-01-05 23:32:58 +08:00
|
|
|
local function setLoadLamp(sigcolor)
|
|
|
|
setSignal("LoadLamp",sigcolor)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function setUnloadLamp(sigcolor)
|
|
|
|
setSignal("UnloadLamp",sigcolor)
|
|
|
|
end
|
2018-01-04 23:42:38 +08:00
|
|
|
|
|
|
|
local function lockLoadChest()
|
|
|
|
setSignal("LoadBoxCtrl",green)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function unlockLoadChest()
|
|
|
|
setSignal("LoadBoxCtrl",red)
|
|
|
|
end
|
|
|
|
|
2018-01-05 23:32:58 +08:00
|
|
|
local function lockUnloadChest()
|
|
|
|
setSignal("UnloadBoxCtrl",green)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function unlockUnloadChest()
|
|
|
|
setSignal("UnloadBoxCtrl",red)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local function getNewTransID(cnt)
|
|
|
|
print("Getting new transfer id...")
|
2018-01-07 22:32:08 +08:00
|
|
|
OpenPort(10011)
|
|
|
|
BroadcastData(10010,"TSCM","req","store",cnt)
|
|
|
|
e=WaitEvent("net_message",10)
|
2018-01-05 23:32:58 +08:00
|
|
|
local ret
|
|
|
|
if(e~=nil and e.data[1]=="TSCM" and e.data[2]=="ack" and e.data[3]=="pass") then
|
|
|
|
ret=e.data[4]
|
|
|
|
else
|
|
|
|
ret=nil
|
|
|
|
end
|
2018-01-07 22:32:08 +08:00
|
|
|
ClosePort(10011)
|
2018-01-05 23:32:58 +08:00
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
|
|
|
local function SetTicket(Dest)
|
2018-01-08 12:01:33 +08:00
|
|
|
out_ticket.setDestination(Dest)
|
2018-01-05 23:32:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
local function doLoadWork(item_cnt)
|
|
|
|
print("LoadWork: Ready.")
|
|
|
|
local id=getNewTransID(item_cnt)
|
|
|
|
if(id==nil) then
|
|
|
|
print("LoadWork: Failed to get new transID.")
|
|
|
|
print("LoadWork: item rollback started.")
|
|
|
|
|
|
|
|
setLoadLamp(byellow)
|
|
|
|
local sz=load_transposer.getInventorySize(sides.down)
|
|
|
|
local cnt=1
|
|
|
|
for i=1,sz,1 do
|
|
|
|
if(load_transposer.getStackInSlot(sides.down,i)~=nil) then
|
|
|
|
load_transposer.transferItem(sides.down,load_box_side)
|
|
|
|
cnt=cnt+1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
print("LoadWork: item rollback finished.")
|
|
|
|
loading=0
|
|
|
|
setLoadLamp(green)
|
|
|
|
unlockLoadChest()
|
|
|
|
else
|
|
|
|
id=math.ceil(id)
|
|
|
|
print("LoadWork: Transfer id got.")
|
|
|
|
print("LoadWork: Setting routing table...")
|
|
|
|
local trainid="TC_" .. tostring(id)
|
|
|
|
local routestr="Dest=" .. trainid
|
|
|
|
print("LoadWork: ",routestr)
|
|
|
|
local routetb={[1]=routestr}
|
|
|
|
route_ab_load.setRoutingTable(routetb)
|
|
|
|
route_ba_load.setRoutingTable(routetb)
|
|
|
|
|
|
|
|
local backtrainid="TR_" .. tostring(id)
|
|
|
|
|
|
|
|
print("LoadWork: Routing table set.")
|
|
|
|
|
|
|
|
local bus=CreateEventBus()
|
|
|
|
EventBusListen(bus,"minecart")
|
|
|
|
|
|
|
|
local function trigger()
|
|
|
|
setSignal("LoadCartCtrl",green)
|
|
|
|
os.sleep(0.5)
|
|
|
|
setSignal("LoadCartCtrl",red)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function lockOutWay()
|
|
|
|
while(lockway~=0) do
|
|
|
|
os.sleep(1)
|
|
|
|
end
|
|
|
|
lockway=1
|
|
|
|
end
|
|
|
|
|
|
|
|
local function unlockOutWay()
|
|
|
|
lockway=0
|
|
|
|
end
|
|
|
|
|
2018-01-08 12:01:33 +08:00
|
|
|
local function startOutWay()
|
|
|
|
setSignal("OutCtrl",green)
|
|
|
|
os.sleep(0.5)
|
|
|
|
setSignal("OutCtrl",red)
|
|
|
|
|
|
|
|
unlockOutWay()
|
|
|
|
end
|
|
|
|
|
2018-01-05 23:32:58 +08:00
|
|
|
while true do
|
|
|
|
local e=GetNextEvent(bus)
|
|
|
|
if(e.event=="minecart") then
|
|
|
|
print("LoadWork: Minecart arrived. Start Loading...")
|
|
|
|
if(e.minecartType=="locomotive_creative") then
|
|
|
|
print("LoadWork: Skipping locomotive.")
|
|
|
|
print("LoadWork: Try locking outway...")
|
|
|
|
lockOutWay()
|
|
|
|
print("LoadWork: Outway locked.")
|
|
|
|
SetTicket(backtrainid)
|
|
|
|
setLoadLamp(red)
|
|
|
|
elseif(e.minecartType=="cart_chest") then
|
|
|
|
print("LoadWork: Filling chest cart...")
|
|
|
|
while(digital_receiver.getAspect("LoadCartSig")==red) do
|
|
|
|
os.sleep(1)
|
|
|
|
end
|
|
|
|
print("LoadWork: Chest cart filled.")
|
|
|
|
trigger()
|
|
|
|
elseif(e.minecartType=="cart_worldspike_admin") then
|
|
|
|
print("LoadWork: Found world spike. Finish.")
|
|
|
|
trigger()
|
2018-01-08 12:01:33 +08:00
|
|
|
trigger()
|
|
|
|
|
|
|
|
startOutWay()
|
2018-01-05 23:32:58 +08:00
|
|
|
break
|
|
|
|
else
|
|
|
|
print("LoadWork: Skipping unknown cart_type: " .. e.minecartType)
|
|
|
|
trigger()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
DestroyEventBus(bus)
|
|
|
|
|
|
|
|
--- Clean Up
|
|
|
|
setLoadLamp(green)
|
|
|
|
unlockLoadChest()
|
|
|
|
end
|
|
|
|
end
|
2018-01-04 23:42:38 +08:00
|
|
|
|
|
|
|
local function startLoad()
|
2018-01-05 08:39:49 +08:00
|
|
|
if(loading>0) then
|
|
|
|
return false,"Loading status not free"
|
|
|
|
end
|
|
|
|
|
2018-01-04 23:42:38 +08:00
|
|
|
lockLoadChest()
|
2018-01-05 08:39:49 +08:00
|
|
|
|
2018-01-04 23:42:38 +08:00
|
|
|
local sz=load_transposer.getInventorySize(load_box_side)
|
|
|
|
local cnt=1
|
|
|
|
for i=1,sz,1 do
|
|
|
|
if(load_transposer.getStackInSlot(load_box_side,i)~=nil) then
|
|
|
|
load_transposer.transferItem(load_box_side,sides.down)
|
|
|
|
cnt=cnt+1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-05 23:32:58 +08:00
|
|
|
print("startLoad: " .. cnt-1 .. " item transferred to internal chest")
|
|
|
|
loading=1
|
|
|
|
thread.create(doLoadWork,cnt);
|
|
|
|
|
|
|
|
setLoadLamp(yellow)
|
|
|
|
print("Info: You items have been submitted.")
|
|
|
|
end
|
|
|
|
|
|
|
|
local function storeMain()
|
|
|
|
setLoadLamp(byellow)
|
|
|
|
print("Please put your items to inbox.")
|
|
|
|
print("Once you finished it, press enter")
|
|
|
|
io.read()
|
|
|
|
print("Start loading...")
|
|
|
|
startLoad()
|
2018-01-04 23:42:38 +08:00
|
|
|
end
|
|
|
|
|
2018-01-04 13:44:40 +08:00
|
|
|
local function main()
|
|
|
|
checkDevice()
|
|
|
|
resetDevice()
|
2018-01-05 23:32:58 +08:00
|
|
|
|
2018-01-08 11:20:04 +08:00
|
|
|
NetBoxInit()
|
2018-01-07 22:32:08 +08:00
|
|
|
|
2018-01-05 23:32:58 +08:00
|
|
|
while true do
|
|
|
|
print(
|
|
|
|
"-------------\n" ..
|
|
|
|
"Action List\n" ..
|
|
|
|
"1 Store items\n" ..
|
|
|
|
"2 Get items\n" ..
|
|
|
|
"3 Exit\n" ..
|
|
|
|
"-------------"
|
|
|
|
)
|
|
|
|
local id=io.read("*num")
|
|
|
|
io.read()
|
|
|
|
if(id==1) then
|
|
|
|
storeMain()
|
|
|
|
elseif(id==2) then
|
|
|
|
getMain()
|
|
|
|
elseif(id==3) then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
resetDevice()
|
|
|
|
unlockUnloadChest()
|
2018-01-07 22:32:08 +08:00
|
|
|
|
2018-01-08 11:20:04 +08:00
|
|
|
NetBoxCleanUp()
|
2018-01-04 13:44:40 +08:00
|
|
|
end
|
|
|
|
|
2018-01-04 23:12:30 +08:00
|
|
|
print("Transport System Client Started.")
|
|
|
|
print("Author: Kiritow")
|
|
|
|
main()
|
|
|
|
print("Transport System Client Stopped.")
|