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,
"flib": true,
"cybersyn": true,
"creative-mod": true,
},
"disableExtraMods": true
}
]
}

View File

@@ -188,12 +188,15 @@ local function send_train_between(map_data, r_station_id, p_station_id, depot, p
local item_network_name = network_name..":"..item.name
local r_stations = economy.all_r_stations[item_network_name]
local p_stations = economy.all_p_stations[item_network_name]
if r_stations then
for j, id in ipairs(r_stations) do
if id == r_station_id then
table_remove(r_stations, j)
break
end
end
end
if p_stations then
for j, id in ipairs(p_stations) do
if id == p_station_id then
table_remove(p_stations, j)
@@ -202,6 +205,7 @@ local function send_train_between(map_data, r_station_id, p_station_id, depot, p
end
end
end
end
remove_available_train(map_data, depot)
train.status = STATUS_D_TO_P
@@ -407,25 +411,32 @@ local function tick_dispatch(map_data, mod_settings)
table_sort(r_stations, function(a_id, b_id)
local a = stations[a_id]
local b = stations[b_id]
if a and b then
if a.priority ~= b.priority then
return a.priority < b.priority
else
return a.last_delivery_tick > b.last_delivery_tick
end
else
return a == nil
end
end)
break
else
for i, id in ipairs(r_stations) do
local station = stations[id]
if station then
station.display_failed_request = true
station.display_update = true
end
end
end
end
end
local r_station_id = table_remove(r_stations--[[@as uint[] ]])
local r_station = stations[r_station_id]
if r_station then
local item_name = tick_data.item_name
local item_type = tick_data.item_type
local r_threshold = r_station.p_count_or_r_threshold_per_item[item_name]
@@ -437,7 +448,7 @@ local function tick_dispatch(map_data, mod_settings)
local could_have_been_serviced = false
for j, p_station_id in ipairs(p_stations) do
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 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)
@@ -463,6 +474,7 @@ local function tick_dispatch(map_data, mod_settings)
r_station.display_failed_request = true
r_station.display_update = true
end
end
return false
end
---@param map_data MapData

View File

@@ -64,6 +64,7 @@
---@alias cybersyn.global MapData
---@class Economy
---could contain invalid stations
---@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_names {[string]: uint[]} --{[network_name:item_name]: count}

View File

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