started implementing train classes

This commit is contained in:
Monica Moniot
2022-10-23 19:10:56 -04:00
parent 1219a211f4
commit 2991fff171
4 changed files with 101 additions and 30 deletions

View File

@@ -30,3 +30,6 @@ STATION_LAYOUT_FLUID = "F"
STATION_LAYOUT_BOTH = "."
LONGEST_INSERTER_REACH = 2
TRAIN_CLASS_ALL = "all"
TRAIN_CLASS_AUTO = "auto"

View File

@@ -9,6 +9,7 @@ global: {
trains_available: {[train_id]: bool}
layouts: {[layout_id]: Layout}
layout_train_count: {[layout_id]: int}
train_classes: {[string]: TrainClass}
}
Station: {
deliveries_total: int
@@ -23,10 +24,9 @@ Station: {
deliveries: {
[item_name]: int
}
--train_layout: [char]
accepted_layouts: {
[layout_id]: bool
}
train_class: string
accepted_layouts: TrainClass
layout_pattern: string|nil
}
Train: {
entity: LuaEntity
@@ -43,6 +43,9 @@ Train: {
count: int
}]
}
TrainClass: {
[layout_id]: bool
}
Layout: string
]]
--TODO: only init once
@@ -58,3 +61,6 @@ global.trains_available = {}
global.layouts = {}
global.layout_train_count = {}
global.layout_top_id = 1
global.train_classes = {
[TRAIN_CLASS_ALL] = {},
}

View File

@@ -1,5 +1,22 @@
--By Mami
local area = require("__flib__.area")
function remove_train(map_data, train, train_id)
map_data.trains[train_id] = nil
map_data.trains_available[train_id] = nil
local layout_id = train.layout_id
local count = map_data.layout_train_count[layout_id]
if count <= 1 then
map_data.layout_train_count[layout_id] = nil
map_data.layouts[layout_id] = nil
for station_id, station in pairs(map_data.stations) do
station.accepted_layouts[layout_id] = nil
end
map_data.train_classes[TRAIN_CLASS_ALL][layout_id] = nil
else
map_data.layout_train_count[layout_id] = count - 1
end
end
function update_train_layout(map_data, train)
local carriages = train.entity.carriages
@@ -15,7 +32,7 @@ function update_train_layout(map_data, train)
elseif carriage.type == "fluid-wagon" then
layout = layout..TRAIN_LAYOUT_FLUID
fluid_capacity = fluid_capacity + carriage.prototype.fluid_capacity
--elseif carriage.type == "artillery-wagon" then
--elseif carriage.type == "artillery-wagon" then
--layout = layout..TRAIN_LAYOUT_ARTILLERY
else
layout = layout..TRAIN_LAYOUT_NA
@@ -38,10 +55,11 @@ function update_train_layout(map_data, train)
map_data.layouts[layout_id] = layout
map_data.layout_train_count[layout_id] = 1
for _, station in pairs(map_data.stations) do
if string.find(layout, station.layout_pattern) ~= nil then
if station.layout_pattern and string.find(layout, station.layout_pattern) ~= nil then
station.accepted_layouts[layout_id] = true
end
end
map_data.train_classes[TRAIN_CLASS_ALL][layout_id] = true
else
map_data.layout_train_count[layout_id] = map_data.layout_train_count[layout_id] + 1
end
@@ -50,8 +68,7 @@ function update_train_layout(map_data, train)
train.fluid_capacity = fluid_capacity
end
local area = require("__flib__.area")
function reset_station_layout(map_data, station)
local function reset_station_layout(map_data, station)
--station.entity
local station_rail = station.entity.connected_rail
local rail_direction_from_station
@@ -149,3 +166,53 @@ function reset_station_layout(map_data, station)
end
end
end
function set_station_train_class(map_data, station, train_class_name)
if train_class_name == TRAIN_CLASS_AUTO then
if station.train_class ~= TRAIN_CLASS_AUTO then
station.train_class = TRAIN_CLASS_AUTO
station.accepted_layouts = {}
end
reset_station_layout(map_data, station)
else
station.train_class = train_class_name
station.accepted_layouts = map_data.train_classes[train_class_name]
assert(station.accepted_layouts ~= nil)
station.layout_pattern = nil
end
end
function update_station_if_auto(map_data, station)
if station.train_class == TRAIN_CLASS_AUTO then
reset_station_layout(map_data, station)
end
end
function update_station_from_rail(map_data, rail)
--TODO: search further?
local entity = rail.get_rail_segment_entity(nil, false)
if entity.name == BUFFER_STATION_NAME then
update_station_if_auto(map_data, map_data.stations[entity.unit_number])
end
end
function update_station_from_pump(map_data, pump)
if pump.pump_rail_target then
update_station_from_rail(map_data, pump.pump_rail_target)
end
end
function update_station_from_inserter(map_data, inserter)
--TODO: check if correct
local surface = inserter.surface
local pos = inserter.position
local pickup_pos = inserter.prototype.inserter_pickup_position
local drop_pos = inserter.prototype.inserter_drop_position
local rail = surface.find_entity("straight-rail", {pos.x + pickup_pos.x, pos.y + pickup_pos.y})
if rail then
update_station_from_rail(map_data, rail)
end
rail = surface.find_entity("straight-rail", {pos.x + drop_pos.x, pos.y + drop_pos.y})
if rail then
update_station_from_rail(map_data, rail)
end
end

View File

@@ -40,22 +40,6 @@ local function on_failed_delivery(map_data, train)
train.manifest = nil
end
local function remove_train(map_data, train, train_id)
map_data.trains[train_id] = nil
map_data.trains_available[train_id] = nil
local layout_id = train.layout_id
local count = map_data.layout_train_count[layout_id]
if count <= 1 then
map_data.layout_train_count[layout_id] = nil
map_data.layouts[layout_id] = nil
for station_id, station in pairs(map_data.stations) do
station.accepted_layouts[layout_id] = nil
end
else
map_data.layout_train_count[layout_id] = count - 1
end
end
local function on_station_built(map_data, stop)
local pos_x = stop.position.x
local pos_y = stop.position.y
@@ -148,11 +132,13 @@ local function on_station_built(map_data, stop)
p_threshold = 0,
locked_slots = 0,
deliveries = {},
train_class = TRAIN_CLASS_AUTO,
accepted_layouts = {},
layout_pattern = ".*",--TODO: change this
layout_pattern = nil,
}
map_data.stations[stop.unit_number] = station
update_station_if_auto(map_data, station)
end
local function on_station_broken(map_data, stop)
--search for trains coming to the destroyed station
@@ -371,13 +357,15 @@ end
local function on_built(event)
local entity = event.entity or event.created_entity or event.destination
if not entity or not entity.valid then return end
if entity.name == BUFFER_STATION_NAME then
on_station_built(global, entity)
elseif entity.type == "inserter" then
update_station_from_inserter(global, entity)
elseif entity.type == "pump" then
if entity.pump_rail_target then
end
update_station_from_pump(global, entity)
elseif entity.type == "straight-rail" then
update_station_from_rail(global, entity)
end
end
local function on_broken(event)
@@ -392,7 +380,12 @@ local function on_broken(event)
elseif entity.name == BUFFER_STATION_NAME then
on_station_broken(global, entity)
elseif entity.type == "inserter" then
--NOTE: check if this works or if it needs to be delayed
update_station_from_inserter(global, entity)
elseif entity.type == "pump" then
update_station_from_pump(global, entity)
elseif entity.type == "straight-rail" then
update_station_from_rail(global, entity)
end
end
@@ -446,11 +439,13 @@ local filter_built = {
{filter = "type", type = "train-stop"},
{filter = "type", type = "inserter"},
{filter = "type", type = "pump"},
{filter = "type", type = "straight-rail"},
}
local filter_broken = {
{filter = "type", type = "train-stop"},
{filter = "type", type = "inserter"},
{filter = "type", type = "pump"},
{filter = "type", type = "straight-rail"},
{filter = "rolling-stock"},
}
local function register_events()