Skip to content
Snippets Groups Projects
Commit bcb57325 authored by Peter Nerlich's avatar Peter Nerlich
Browse files

core: more info returned by find_route(), stop if trains appear in the wrong place

parent 5aeb2bed
No related branches found
No related tags found
No related merge requests found
Pipeline #58342 passed
...@@ -343,8 +343,12 @@ F.core.train_enter = function(place, dir, train) ...@@ -343,8 +343,12 @@ F.core.train_enter = function(place, dir, train)
train.place = S.places[place] train.place = S.places[place]
train.dir = dir train.dir = dir
if train.desired_state == nil or if not is_entry then
(train.desired_state == "routed" and train.desired_place == place and train.desired_dir == dir) then F.core.expect_train(train)
end
if train.desired == nil or
(train.desired.state == "routed" and train.desired.place == place and train.desired.dir == dir) then
local dest = F.core.train_find_next_destination(train) local dest = F.core.train_find_next_destination(train)
if dest == nil then if dest == nil then
if is_entry then if is_entry then
...@@ -353,25 +357,29 @@ F.core.train_enter = function(place, dir, train) ...@@ -353,25 +357,29 @@ F.core.train_enter = function(place, dir, train)
table.insert(flags, "rejecting") table.insert(flags, "rejecting")
else else
table.insert(flags, "parking") table.insert(flags, "parking")
train.desired_state = "parked" train.desired = {
train.desired_place = place ["state"] = "parked",
train.desired_dir = dir ["place"] = place,
["dir"] = dir,
}
local msg = "Parking train "..F.util.dump(train.id).." at "..F.util.dump(place).."→"..F.util.dump(dir).."." local msg = "Parking train "..F.util.dump(train.id).." at "..F.util.dump(place).."→"..F.util.dump(dir).."."
F.util.debug(msg) F.util.debug(msg)
print(msg) print(msg)
end end
else else
train.desired_state = "routed" train.desired = {
train.desired_place = dest.place.fullname ["state"] = "routed",
train.desired_dir = dest.dir ["place"] = dest.place.fullname,
["dir"] = dest.dir,
}
table.insert(S.tasks, {["type"] = "route", ["train"] = train, ["dest"] = dest}) table.insert(S.tasks, {["type"] = "route", ["train"] = train, ["dest"] = dest})
payload.new_task = "route" payload.new_task = "route"
end end
elseif (train.desired_state == "routed" or train.desired_state == "parked") and (train.desired_place ~= place or train.desired_dir ~= dir) then elseif (train.desired.state == "routed" or train.desired.state == "parked") and (train.desired.place ~= place or train.desired.dir ~= dir) then
F.core.validate_place_dir_combo(train.desired_place, train.desired_dir) F.core.validate_place_dir_combo(train.desired.place, train.desired.dir)
table.insert(S.tasks, {["type"] = "route", ["train"] = train, ["dest"] = { table.insert(S.tasks, {["type"] = "route", ["train"] = train, ["dest"] = {
["place"] = S.places[train.desired_place], ["place"] = S.places[train.desired.place],
["dir"] = train.desired_dir, ["dir"] = train.desired.dir,
}}) }})
payload.new_task = "route" payload.new_task = "route"
end end
...@@ -406,6 +414,14 @@ F.core.train_exit = function(place, dir, train) ...@@ -406,6 +414,14 @@ F.core.train_exit = function(place, dir, train)
F.util.debug(msg) F.util.debug(msg)
assert(false, msg) assert(false, msg)
end end
-- use the original object
train = S.trains[train.id]
-- save current place and dir
train.place = S.places[place]
train.dir = dir
F.core.expect_train(train)
F.util.debug("Train "..F.util.dump(train.id).." exited via "..F.util.dump(place).."→"..F.util.dump(dir)) F.util.debug("Train "..F.util.dump(train.id).." exited via "..F.util.dump(place).."→"..F.util.dump(dir))
train.place = nil train.place = nil
train.dir = nil train.dir = nil
...@@ -430,9 +446,11 @@ F.core.reject_train = function(train) ...@@ -430,9 +446,11 @@ F.core.reject_train = function(train)
if S.caches.interfaces[train.place.fullname] ~= nil and S.caches.interfaces[train.place.fullname][train.dir] ~= nil then if S.caches.interfaces[train.place.fullname] ~= nil and S.caches.interfaces[train.place.fullname][train.dir] ~= nil then
local interface = S.caches.interfaces[train.place.fullname][train.dir].interface local interface = S.caches.interfaces[train.place.fullname][train.dir].interface
if interface.exit ~= nil then if interface.exit ~= nil then
train.desired_state = "routed" train.desired = {
train.desired_place = interface.exit.place.fullname ["state"] = "routed",
train.desired_dir = interface.exit.dir ["place"] = interface.exit.place.fullname,
["dir"] = interface.exit.dir,
}
table.insert(S.tasks, {["type"] = "route", ["train"] = train, ["dest"] = interface.exit}) table.insert(S.tasks, {["type"] = "route", ["train"] = train, ["dest"] = interface.exit})
return return
end end
...@@ -443,6 +461,24 @@ F.core.reject_train = function(train) ...@@ -443,6 +461,24 @@ F.core.reject_train = function(train)
assert(false, msg) assert(false, msg)
end end
F.core.expect_train = function(train, dest)
if dest == nil then
dest = train.expected
end
local place
if type(dest.place) == "string" then
place = train.place.fullname == dest.place
else
place = train.place.fullname == dest.place.fullname
end
if not (place and train.dir == dest.dir) then
atc_send("BB")
local msg = "Train "..F.util.dump(train.id).." wrongly appeared at "..F.util.dump(train.place.name).."→"..F.util.dump(train.dir).."!"
F.util.debug(msg)
assert(false, msg)
end
end
F.core.train_find_next_destination = function(train) F.core.train_find_next_destination = function(train)
-- TODO: lookup where we expect the train to go next -- TODO: lookup where we expect the train to go next
return nil return nil
...@@ -554,7 +590,13 @@ print("NEW PHASE - TODO: "..F.util.dump(a))]]-- ...@@ -554,7 +590,13 @@ print("NEW PHASE - TODO: "..F.util.dump(a))]]--
end end
-- merge with already found options -- merge with already found options
for _,v in ipairs(routes) do for _,v in ipairs(routes) do
result_found.routes[v[1]] = costs[min[2]] + v[2] result_found.routes[v[1]] = {
["name"] = v[1],
["cost"] = costs[min[2]] + v[2],
["individual_cost"] = v[2],
["place"] = dest.place,
["dir"] = dest.dir,
}
end end
print("find_route() intermediary result: "..F.util.dump(result_found)) print("find_route() intermediary result: "..F.util.dump(result_found))
else else
...@@ -592,6 +634,9 @@ print("find_route() intermediary result: "..F.util.dump(result_found)) ...@@ -592,6 +634,9 @@ print("find_route() intermediary result: "..F.util.dump(result_found))
end end
F.core.do_route = function(dest, train) F.core.do_route = function(dest, train)
if train ~= S.trains[train.id] then
train = S.trains[train.id]
end
local flags = {"do_route"} local flags = {"do_route"}
local payload = {["dest"] = dest, ["train"] = train} local payload = {["dest"] = dest, ["train"] = train}
...@@ -611,17 +656,25 @@ F.core.do_route = function(dest, train) ...@@ -611,17 +656,25 @@ F.core.do_route = function(dest, train)
end end
if can_set_route(pos, result.best_route) then if can_set_route(pos, result.best_route) then
set_route(pos, result.best_route) set_route(pos, result.best_route)
train.expected = {
["place"] = result.routes[result.best_route].place.fullname,
["dir"] = result.routes[result.best_route].dir,
}
else else
local options = {} local options = {}
for k,v in pairs(result.routes) do for name,v in pairs(result.routes) do
table.insert(options, {k,v}) table.insert(options, v)
end end
table.sort(options, function(a,b) return a[2] < b[2] end) table.sort(options, function(a,b) return a.cost < b.cost end)
local success = false local success = false
for _,v in ipairs(options) do for _,v in ipairs(options) do
if v[1] ~= result.best_route and can_set_route(pos, v[1]) then if v.name ~= result.best_route and can_set_route(pos, v.name) then
set_route(pos, v[1]) set_route(pos, v.name)
success = true success = true
train.expected = {
["place"] = v.place.fullname,
["dir"] = v.dir,
}
end end
end end
if not success then if not success then
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment