mirror of
https://github.com/Xevion/project-cybersyn.git
synced 2025-12-10 20:08:09 -06:00
started implementing train classes
This commit is contained in:
@@ -30,3 +30,6 @@ STATION_LAYOUT_FLUID = "F"
|
|||||||
STATION_LAYOUT_BOTH = "."
|
STATION_LAYOUT_BOTH = "."
|
||||||
|
|
||||||
LONGEST_INSERTER_REACH = 2
|
LONGEST_INSERTER_REACH = 2
|
||||||
|
|
||||||
|
TRAIN_CLASS_ALL = "all"
|
||||||
|
TRAIN_CLASS_AUTO = "auto"
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ global: {
|
|||||||
trains_available: {[train_id]: bool}
|
trains_available: {[train_id]: bool}
|
||||||
layouts: {[layout_id]: Layout}
|
layouts: {[layout_id]: Layout}
|
||||||
layout_train_count: {[layout_id]: int}
|
layout_train_count: {[layout_id]: int}
|
||||||
|
train_classes: {[string]: TrainClass}
|
||||||
}
|
}
|
||||||
Station: {
|
Station: {
|
||||||
deliveries_total: int
|
deliveries_total: int
|
||||||
@@ -23,10 +24,9 @@ Station: {
|
|||||||
deliveries: {
|
deliveries: {
|
||||||
[item_name]: int
|
[item_name]: int
|
||||||
}
|
}
|
||||||
--train_layout: [char]
|
train_class: string
|
||||||
accepted_layouts: {
|
accepted_layouts: TrainClass
|
||||||
[layout_id]: bool
|
layout_pattern: string|nil
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Train: {
|
Train: {
|
||||||
entity: LuaEntity
|
entity: LuaEntity
|
||||||
@@ -43,6 +43,9 @@ Train: {
|
|||||||
count: int
|
count: int
|
||||||
}]
|
}]
|
||||||
}
|
}
|
||||||
|
TrainClass: {
|
||||||
|
[layout_id]: bool
|
||||||
|
}
|
||||||
Layout: string
|
Layout: string
|
||||||
]]
|
]]
|
||||||
--TODO: only init once
|
--TODO: only init once
|
||||||
@@ -58,3 +61,6 @@ global.trains_available = {}
|
|||||||
global.layouts = {}
|
global.layouts = {}
|
||||||
global.layout_train_count = {}
|
global.layout_train_count = {}
|
||||||
global.layout_top_id = 1
|
global.layout_top_id = 1
|
||||||
|
global.train_classes = {
|
||||||
|
[TRAIN_CLASS_ALL] = {},
|
||||||
|
}
|
||||||
|
|||||||
@@ -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)
|
function update_train_layout(map_data, train)
|
||||||
local carriages = train.entity.carriages
|
local carriages = train.entity.carriages
|
||||||
@@ -15,7 +32,7 @@ function update_train_layout(map_data, train)
|
|||||||
elseif carriage.type == "fluid-wagon" then
|
elseif carriage.type == "fluid-wagon" then
|
||||||
layout = layout..TRAIN_LAYOUT_FLUID
|
layout = layout..TRAIN_LAYOUT_FLUID
|
||||||
fluid_capacity = fluid_capacity + carriage.prototype.fluid_capacity
|
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
|
--layout = layout..TRAIN_LAYOUT_ARTILLERY
|
||||||
else
|
else
|
||||||
layout = layout..TRAIN_LAYOUT_NA
|
layout = layout..TRAIN_LAYOUT_NA
|
||||||
@@ -38,10 +55,11 @@ function update_train_layout(map_data, train)
|
|||||||
map_data.layouts[layout_id] = layout
|
map_data.layouts[layout_id] = layout
|
||||||
map_data.layout_train_count[layout_id] = 1
|
map_data.layout_train_count[layout_id] = 1
|
||||||
for _, station in pairs(map_data.stations) do
|
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
|
station.accepted_layouts[layout_id] = true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
map_data.train_classes[TRAIN_CLASS_ALL][layout_id] = true
|
||||||
else
|
else
|
||||||
map_data.layout_train_count[layout_id] = map_data.layout_train_count[layout_id] + 1
|
map_data.layout_train_count[layout_id] = map_data.layout_train_count[layout_id] + 1
|
||||||
end
|
end
|
||||||
@@ -50,8 +68,7 @@ function update_train_layout(map_data, train)
|
|||||||
train.fluid_capacity = fluid_capacity
|
train.fluid_capacity = fluid_capacity
|
||||||
end
|
end
|
||||||
|
|
||||||
local area = require("__flib__.area")
|
local function reset_station_layout(map_data, station)
|
||||||
function reset_station_layout(map_data, station)
|
|
||||||
--station.entity
|
--station.entity
|
||||||
local station_rail = station.entity.connected_rail
|
local station_rail = station.entity.connected_rail
|
||||||
local rail_direction_from_station
|
local rail_direction_from_station
|
||||||
@@ -149,3 +166,53 @@ function reset_station_layout(map_data, station)
|
|||||||
end
|
end
|
||||||
end
|
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
|
||||||
|
|||||||
@@ -40,22 +40,6 @@ local function on_failed_delivery(map_data, train)
|
|||||||
train.manifest = nil
|
train.manifest = nil
|
||||||
end
|
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 function on_station_built(map_data, stop)
|
||||||
local pos_x = stop.position.x
|
local pos_x = stop.position.x
|
||||||
local pos_y = stop.position.y
|
local pos_y = stop.position.y
|
||||||
@@ -148,11 +132,13 @@ local function on_station_built(map_data, stop)
|
|||||||
p_threshold = 0,
|
p_threshold = 0,
|
||||||
locked_slots = 0,
|
locked_slots = 0,
|
||||||
deliveries = {},
|
deliveries = {},
|
||||||
|
train_class = TRAIN_CLASS_AUTO,
|
||||||
accepted_layouts = {},
|
accepted_layouts = {},
|
||||||
layout_pattern = ".*",--TODO: change this
|
layout_pattern = nil,
|
||||||
}
|
}
|
||||||
|
|
||||||
map_data.stations[stop.unit_number] = station
|
map_data.stations[stop.unit_number] = station
|
||||||
|
|
||||||
|
update_station_if_auto(map_data, station)
|
||||||
end
|
end
|
||||||
local function on_station_broken(map_data, stop)
|
local function on_station_broken(map_data, stop)
|
||||||
--search for trains coming to the destroyed station
|
--search for trains coming to the destroyed station
|
||||||
@@ -371,13 +357,15 @@ end
|
|||||||
local function on_built(event)
|
local function on_built(event)
|
||||||
local entity = event.entity or event.created_entity or event.destination
|
local entity = event.entity or event.created_entity or event.destination
|
||||||
if not entity or not entity.valid then return end
|
if not entity or not entity.valid then return end
|
||||||
|
|
||||||
if entity.name == BUFFER_STATION_NAME then
|
if entity.name == BUFFER_STATION_NAME then
|
||||||
on_station_built(global, entity)
|
on_station_built(global, entity)
|
||||||
elseif entity.type == "inserter" then
|
elseif entity.type == "inserter" then
|
||||||
|
update_station_from_inserter(global, entity)
|
||||||
elseif entity.type == "pump" then
|
elseif entity.type == "pump" then
|
||||||
if entity.pump_rail_target then
|
update_station_from_pump(global, entity)
|
||||||
|
elseif entity.type == "straight-rail" then
|
||||||
end
|
update_station_from_rail(global, entity)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
local function on_broken(event)
|
local function on_broken(event)
|
||||||
@@ -392,7 +380,12 @@ local function on_broken(event)
|
|||||||
elseif entity.name == BUFFER_STATION_NAME then
|
elseif entity.name == BUFFER_STATION_NAME then
|
||||||
on_station_broken(global, entity)
|
on_station_broken(global, entity)
|
||||||
elseif entity.type == "inserter" then
|
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
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -446,11 +439,13 @@ local filter_built = {
|
|||||||
{filter = "type", type = "train-stop"},
|
{filter = "type", type = "train-stop"},
|
||||||
{filter = "type", type = "inserter"},
|
{filter = "type", type = "inserter"},
|
||||||
{filter = "type", type = "pump"},
|
{filter = "type", type = "pump"},
|
||||||
|
{filter = "type", type = "straight-rail"},
|
||||||
}
|
}
|
||||||
local filter_broken = {
|
local filter_broken = {
|
||||||
{filter = "type", type = "train-stop"},
|
{filter = "type", type = "train-stop"},
|
||||||
{filter = "type", type = "inserter"},
|
{filter = "type", type = "inserter"},
|
||||||
{filter = "type", type = "pump"},
|
{filter = "type", type = "pump"},
|
||||||
|
{filter = "type", type = "straight-rail"},
|
||||||
{filter = "rolling-stock"},
|
{filter = "rolling-stock"},
|
||||||
}
|
}
|
||||||
local function register_events()
|
local function register_events()
|
||||||
|
|||||||
Reference in New Issue
Block a user