implemented locked slots

This commit is contained in:
Monica Moniot
2022-10-16 18:48:11 -04:00
parent 503c847bcd
commit 482b04efb0
9 changed files with 45 additions and 35 deletions

3
.vscode/launch.json vendored
View File

@@ -14,6 +14,7 @@
"debugadapter": true, "debugadapter": true,
"flib": true, "flib": true,
"cybersyn": true, "cybersyn": true,
"creative-mod": true,
}, },
"disableExtraMods": true "disableExtraMods": true
}, },
@@ -27,6 +28,7 @@
"debugadapter": true, "debugadapter": true,
"flib": true, "flib": true,
"cybersyn": true, "cybersyn": true,
"creative-mod": true,
}, },
"disableExtraMods": true "disableExtraMods": true
}, },
@@ -39,6 +41,7 @@
"debugadapter": true, "debugadapter": true,
"flib": true, "flib": true,
"cybersyn": true, "cybersyn": true,
"creative-mod": true,
}, },
"disableExtraMods": true "disableExtraMods": true
} }

View File

@@ -21,4 +21,5 @@ data:extend({
cybersyn_priority, cybersyn_priority,
cybersyn_p_threshold, cybersyn_p_threshold,
cybersyn_r_threshold, cybersyn_r_threshold,
cybersyn_locked_slots,
}) })

View File

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.5 KiB

View File

@@ -38,3 +38,4 @@ cybersyn-train-network=Cybernetic train network
cybersyn-priority=Station priority cybersyn-priority=Station priority
cybersyn-p_threshold=Provide threshold cybersyn-p_threshold=Provide threshold
cybersyn-r_threshold=Request threshold cybersyn-r_threshold=Request threshold
cybersyn-locked-slots=Locked slots per cargo wagon

View File

@@ -27,5 +27,13 @@ cybersyn_r_threshold = {
icon = "__cybersyn__/graphics/icons/r_threshold.png", icon = "__cybersyn__/graphics/icons/r_threshold.png",
icon_size = 64, icon_size = 64,
subgroup = "cybersyn-signal", subgroup = "cybersyn-signal",
order = "a-b" order = "a-c"
}
cybersyn_locked_slots = {
type = "virtual-signal",
name = LOCKED_SLOTS,
icon = "__cybersyn__/graphics/icons/locked_slots.png",
icon_size = 64,
subgroup = "cybersyn-signal",
order = "a-d"
} }

View File

@@ -3,6 +3,7 @@
SIGNAL_PRIORITY = "cybersyn-priority" SIGNAL_PRIORITY = "cybersyn-priority"
REQUEST_THRESHOLD = "cybersyn-r_threshold" REQUEST_THRESHOLD = "cybersyn-r_threshold"
PROVIDE_THRESHOLD = "cybersyn-p_threshold" PROVIDE_THRESHOLD = "cybersyn-p_threshold"
LOCKED_SLOTS = "cybersyn-locked-slots"
STATION_IN_NAME = "cybersyn-station-in" STATION_IN_NAME = "cybersyn-station-in"
STATION_OUT_NAME = "cybersyn-station-out" STATION_OUT_NAME = "cybersyn-station-out"

View File

@@ -3,25 +3,6 @@ local get_distance = require("__flib__.misc").get_distance
local math = math local math = math
local INF = math.huge local INF = math.huge
local function icpairs(a, start_i)
if #a == 0 then
return function() end
end
start_i = start_i%#a + 1
local i = start_i - 1
local flag = true
return function()
i = i%#a + 1
if i ~= start_i or flag then
flag = false
local v = a[i]
if v then
return i, v
end
end
end
end
local create_loading_order_condition = {type = "inactivity", compare_type = "and", ticks = 120} local create_loading_order_condition = {type = "inactivity", compare_type = "and", ticks = 120}
function create_loading_order(stop, manifest) function create_loading_order(stop, manifest)
local condition = {} local condition = {}
@@ -55,7 +36,7 @@ end
local create_direct_to_station_order_condition = {{type = "time", compare_type = "and", ticks = 0}} local create_direct_to_station_order_condition = {{type = "time", compare_type = "and", ticks = 0}}
local function create_direct_to_station_order(stop) local function create_direct_to_station_order(stop)
return {wait_conditions = create_direct_to_station_order_condition, rail = stop.connected_rail, rail_direction = stop.connected_rail_direction} return {rail = stop.connected_rail, rail_direction = stop.connected_rail_direction}
end end
function create_depot_schedule(depot_name) function create_depot_schedule(depot_name)
@@ -178,7 +159,11 @@ local function send_train_between(map_data, r_station_id, p_station_id, train, p
end end
end end
local locked_slots = math.max(p_station.locked_slots, r_station.locked_slots)
local total_slots_left = train.item_slot_capacity local total_slots_left = train.item_slot_capacity
if locked_slots > 0 then
total_slots_left = math.max(total_slots_left - #train.entity.cargo_wagons*locked_slots, math.min(total_slots_left, #train.entity.cargo_wagons))
end
local total_liquid_left = train.fluid_capacity local total_liquid_left = train.fluid_capacity
local i = 1 local i = 1
@@ -266,6 +251,7 @@ function tick(map_data, mod_settings)
station.r_threshold = mod_settings.r_threshold station.r_threshold = mod_settings.r_threshold
station.p_threshold = mod_settings.p_threshold station.p_threshold = mod_settings.p_threshold
station.priority = 0 station.priority = 0
station.locked_slots = 0
local signals = get_signals(station) local signals = get_signals(station)
if signals then if signals then
for k, v in pairs(signals) do for k, v in pairs(signals) do
@@ -280,6 +266,8 @@ function tick(map_data, mod_settings)
station.r_threshold = math.abs(item_count) station.r_threshold = math.abs(item_count)
elseif item_name == PROVIDE_THRESHOLD then elseif item_name == PROVIDE_THRESHOLD then
station.p_threshold = math.abs(item_count) station.p_threshold = math.abs(item_count)
elseif item_name == LOCKED_SLOTS then
station.locked_slots = math.max(item_count, 0)
end end
signals[k] = nil signals[k] = nil
end end
@@ -297,6 +285,7 @@ function tick(map_data, mod_settings)
r_stations_all[item_name] = {} r_stations_all[item_name] = {}
p_stations_all[item_name] = {} p_stations_all[item_name] = {}
all_items[#all_items + 1] = item_name all_items[#all_items + 1] = item_name
all_items[#all_items + 1] = v.signal.type
end end
table.insert(r_stations_all[item_name], station_id) table.insert(r_stations_all[item_name], station_id)
elseif effective_item_count >= station.p_threshold then elseif effective_item_count >= station.p_threshold then
@@ -304,6 +293,7 @@ function tick(map_data, mod_settings)
r_stations_all[item_name] = {} r_stations_all[item_name] = {}
p_stations_all[item_name] = {} p_stations_all[item_name] = {}
all_items[#all_items + 1] = item_name all_items[#all_items + 1] = item_name
all_items[#all_items + 1] = v.signal.type
end end
table.insert(p_stations_all[item_name], station_id) table.insert(p_stations_all[item_name], station_id)
end end
@@ -315,7 +305,10 @@ function tick(map_data, mod_settings)
local failed_because_missing_trains_total = 0 local failed_because_missing_trains_total = 0
--we do not dispatch more than one train per station per tick --we do not dispatch more than one train per station per tick
--psuedo-randomize what item (and what station) to check first so if trains available is low they choose orders psuedo-randomly --psuedo-randomize what item (and what station) to check first so if trains available is low they choose orders psuedo-randomly
for _, item_name in icpairs(all_items, total_ticks) do local start_i = 2*(total_ticks%(#all_items/2)) + 1
for item_i = 0, #all_items - 1, 2 do
local item_name = all_items[(start_i + item_i - 1)%#all_items + 1]
local item_type = all_items[(start_i + item_i)%#all_items + 1]
local r_stations = r_stations_all[item_name] local r_stations = r_stations_all[item_name]
local p_stations = p_stations_all[item_name] local p_stations = p_stations_all[item_name]
@@ -333,7 +326,7 @@ function tick(map_data, mod_settings)
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 train, d = get_valid_train(map_data, r_station_id, p_station_id) local train, d = get_valid_train(map_data, r_station_id, p_station_id, item_type)
local prior = stations[p_station_id].priority local prior = stations[p_station_id].priority
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 train then if train then
@@ -367,7 +360,7 @@ function tick(map_data, mod_settings)
local r_station = stations[r_station_id] local r_station = stations[r_station_id]
local prior = r_station.priority local prior = r_station.priority
if prior > highest_prior or (prior == highest_prior and r_station.last_delivery_tick < lowest_tick) then if prior > highest_prior or (prior == highest_prior and r_station.last_delivery_tick < lowest_tick) then
local train, d = get_valid_train(map_data, r_station_id, p_station_id) local train, d = get_valid_train(map_data, r_station_id, p_station_id, item_type)
if train then if train then
best = i best = i
best_train = train best_train = train
@@ -387,4 +380,5 @@ function tick(map_data, mod_settings)
end end
end end
end end
--TODO: add alert for missing trains
end end

View File

@@ -16,6 +16,7 @@ Station: {
last_delivery_tick: int last_delivery_tick: int
r_threshold: int >= 0 r_threshold: int >= 0
p_threshold: int >= 0 p_threshold: int >= 0
locked_slots: int >= 0
entity: LuaEntity entity: LuaEntity
entity_in: LuaEntity entity_in: LuaEntity
entity_out: LuaEntity entity_out: LuaEntity

View File

@@ -147,10 +147,11 @@ local function on_station_built(map_data, stop)
entity_in = entity_in, entity_in = entity_in,
entity_out = entity_out, entity_out = entity_out,
deliveries_total = 0, deliveries_total = 0,
priority = 0,
last_delivery_tick = 0, last_delivery_tick = 0,
priority = 0,
r_threshold = 0, r_threshold = 0,
p_threshold = 0, p_threshold = 0,
locked_slots = 0,
deliveries = {}, deliveries = {},
accepted_layouts = {} accepted_layouts = {}
} }
@@ -238,7 +239,7 @@ local function update_train_layout(map_data, train)
item_slot_capacity = item_slot_capacity + #inv item_slot_capacity = item_slot_capacity + #inv
elseif carriage.type == "fluid-wagon" then elseif carriage.type == "fluid-wagon" then
layout = layout.."F" layout = layout.."F"
fluid_capacity = fluid_capacity + carriage.prototype.capacity fluid_capacity = fluid_capacity + carriage.prototype.fluid_capacity
else else
layout = layout.."?" layout = layout.."?"
end end
@@ -315,6 +316,7 @@ local function on_train_arrives_depot(map_data, train_entity)
if next(contents) == nil then if next(contents) == nil then
train.depot_name = train_entity.station.backer_name train.depot_name = train_entity.station.backer_name
train.status = STATUS_D train.status = STATUS_D
train.entity.schedule = create_depot_schedule(train.depot_name)
map_data.trains_available[train_entity.id] = true map_data.trains_available[train_entity.id] = true
else--train still has cargo else--train still has cargo
train.entity.schedule = nil train.entity.schedule = nil
@@ -370,6 +372,7 @@ local function on_train_arrives_buffer(map_data, station_id, train)
else else
on_failed_delivery(map_data, train) on_failed_delivery(map_data, train)
remove_train(map_data, train, train.entity.id) remove_train(map_data, train, train.entity.id)
train.entity.schedule = nil
end end
else else
--train is lost somehow, probably from player intervention --train is lost somehow, probably from player intervention
@@ -409,6 +412,9 @@ local function on_train_broken(map_data, train)
if train.manifest then if train.manifest then
on_failed_delivery(map_data, train) on_failed_delivery(map_data, train)
remove_train(map_data, train, train.entity.id) remove_train(map_data, train, train.entity.id)
if train.entity.valid then
train.entity.schedule = nil
end
end end
end end
@@ -418,15 +424,10 @@ local function on_train_modified(map_data, pre_train_id, train_entity)
if train.manifest then if train.manifest then
on_failed_delivery(map_data, train) on_failed_delivery(map_data, train)
end
remove_train(map_data, train, pre_train_id) remove_train(map_data, train, pre_train_id)
else--train is in depot if train.entity.valid then
remove_train(map_data, train, pre_train_id) train.entity.schedule = nil
train.entity = train_entity
update_train_layout(map_data, train)
--TODO: update train stats
map_data.trains[train_entity.id] = train
map_data.trains_available[train_entity.id] = true
end end
end end
end end