fixed potential crash

This commit is contained in:
Monica Moniot
2022-11-10 23:49:22 -05:00
parent 0e9df22034
commit baf373035a
4 changed files with 63 additions and 52 deletions

2
.vscode/launch.json vendored
View File

@@ -53,9 +53,7 @@
"debugadapter": true, "debugadapter": true,
"flib": true, "flib": true,
"cybersyn": true, "cybersyn": true,
"creative-mod": true,
}, },
"disableExtraMods": true
} }
] ]
} }

View File

@@ -188,16 +188,20 @@ local function send_train_between(map_data, r_station_id, p_station_id, depot, p
local item_network_name = network_name..":"..item.name local item_network_name = network_name..":"..item.name
local r_stations = economy.all_r_stations[item_network_name] local r_stations = economy.all_r_stations[item_network_name]
local p_stations = economy.all_p_stations[item_network_name] local p_stations = economy.all_p_stations[item_network_name]
for j, id in ipairs(r_stations) do if r_stations then
if id == r_station_id then for j, id in ipairs(r_stations) do
table_remove(r_stations, j) if id == r_station_id then
break table_remove(r_stations, j)
break
end
end end
end end
for j, id in ipairs(p_stations) do if p_stations then
if id == p_station_id then for j, id in ipairs(p_stations) do
table_remove(p_stations, j) if id == p_station_id then
break table_remove(p_stations, j)
break
end
end end
end end
end end
@@ -407,18 +411,24 @@ local function tick_dispatch(map_data, mod_settings)
table_sort(r_stations, function(a_id, b_id) table_sort(r_stations, function(a_id, b_id)
local a = stations[a_id] local a = stations[a_id]
local b = stations[b_id] local b = stations[b_id]
if a.priority ~= b.priority then if a and b then
return a.priority < b.priority if a.priority ~= b.priority then
return a.priority < b.priority
else
return a.last_delivery_tick > b.last_delivery_tick
end
else else
return a.last_delivery_tick > b.last_delivery_tick return a == nil
end end
end) end)
break break
else else
for i, id in ipairs(r_stations) do for i, id in ipairs(r_stations) do
local station = stations[id] local station = stations[id]
station.display_failed_request = true if station then
station.display_update = true station.display_failed_request = true
station.display_update = true
end
end end
end end
end end
@@ -426,42 +436,44 @@ local function tick_dispatch(map_data, mod_settings)
local r_station_id = table_remove(r_stations--[[@as uint[] ]]) local r_station_id = table_remove(r_stations--[[@as uint[] ]])
local r_station = stations[r_station_id] local r_station = stations[r_station_id]
local item_name = tick_data.item_name if r_station then
local item_type = tick_data.item_type local item_name = tick_data.item_name
local r_threshold = r_station.p_count_or_r_threshold_per_item[item_name] local item_type = tick_data.item_type
local r_threshold = r_station.p_count_or_r_threshold_per_item[item_name]
local best = 0 local best = 0
local best_depot = nil local best_depot = nil
local best_dist = INF local best_dist = INF
local highest_prior = -INF local highest_prior = -INF
local could_have_been_serviced = false local could_have_been_serviced = false
for j, p_station_id in ipairs(p_stations) do for j, p_station_id in ipairs(p_stations) do
local p_station = stations[p_station_id] local p_station = stations[p_station_id]
if p_station.p_count_or_r_threshold_per_item[item_name] >= r_threshold then if p_station and p_station.p_count_or_r_threshold_per_item[item_name] >= r_threshold then
local prior = p_station.priority local prior = p_station.priority
local slot_threshold = item_type == "fluid" and r_threshold or ceil(r_threshold/get_stack_size(map_data, item_name)) local slot_threshold = item_type == "fluid" and r_threshold or ceil(r_threshold/get_stack_size(map_data, item_name))
local depot, d = get_valid_train(map_data, r_station_id, p_station_id, item_type, slot_threshold) local depot, d = get_valid_train(map_data, r_station_id, p_station_id, item_type, slot_threshold)
if prior > highest_prior or (prior == highest_prior and d < best_dist) then if prior > highest_prior or (prior == highest_prior and d < best_dist) then
if depot then if depot then
best = j best = j
best_dist = d best_dist = d
best_depot = depot best_depot = depot
highest_prior = prior highest_prior = prior
elseif d < INF then elseif d < INF then
could_have_been_serviced = true could_have_been_serviced = true
best = j best = j
end
end end
end end
end end
end if best_depot then
if best_depot then send_train_between(map_data, r_station_id, table_remove(p_stations, best), best_depot, item_name)
send_train_between(map_data, r_station_id, table_remove(p_stations, best), best_depot, item_name) else
else if could_have_been_serviced then
if could_have_been_serviced then send_missing_train_alert_for_stops(r_station.entity_stop, stations[p_stations[best]].entity_stop)
send_missing_train_alert_for_stops(r_station.entity_stop, stations[p_stations[best]].entity_stop) end
r_station.display_failed_request = true
r_station.display_update = true
end end
r_station.display_failed_request = true
r_station.display_update = true
end end
return false return false
end end

View File

@@ -64,6 +64,7 @@
---@alias cybersyn.global MapData ---@alias cybersyn.global MapData
---@class Economy ---@class Economy
---could contain invalid stations
---@field public all_r_stations {[string]: uint[]} --{[network_name:item_name]: count} ---@field public all_r_stations {[string]: uint[]} --{[network_name:item_name]: count}
---@field public all_p_stations {[string]: uint[]} --{[network_name:item_name]: count} ---@field public all_p_stations {[string]: uint[]} --{[network_name:item_name]: count}
---@field public all_names {[string]: uint[]} --{[network_name:item_name]: count} ---@field public all_names {[string]: uint[]} --{[network_name:item_name]: count}

View File

@@ -661,12 +661,7 @@ local function on_broken(event)
local entity = event.entity local entity = event.entity
if not entity or not entity.valid then return end if not entity or not entity.valid then return end
if entity.train then if entity.name == "train-stop" then
local train = global.trains[entity.train.id]
if train then
on_train_broken(global, train)
end
elseif entity.name == "train-stop" then
on_stop_broken(global, entity) on_stop_broken(global, entity)
elseif entity.name == COMBINATOR_NAME then elseif entity.name == COMBINATOR_NAME then
on_combinator_broken(global, entity) on_combinator_broken(global, entity)
@@ -676,6 +671,11 @@ local function on_broken(event)
update_station_from_pump(global, entity, entity) update_station_from_pump(global, entity, entity)
elseif entity.type == "straight-rail" then elseif entity.type == "straight-rail" then
update_station_from_rail(global, entity, nil) update_station_from_rail(global, entity, nil)
elseif entity.train then
local train = global.trains[entity.train.id]
if train then
on_train_broken(global, train)
end
end end
end end
local function on_rename(event) local function on_rename(event)