added cybernetic combinators

This commit is contained in:
Monica Moniot
2022-10-24 22:20:05 -04:00
parent 2991fff171
commit 8b7d8a9560
18 changed files with 456 additions and 303 deletions

View File

@@ -1,38 +1,21 @@
--By Mami
local function set_station_output_empty(station)
--change circuit outputs
station.entity_out.get_control_behavior().parameters = nil
end
local function on_failed_delivery(map_data, train)
--NOTE: must change train status to STATUS_D or remove it from tracked trains after this call
local is_p_delivery_made = train.status ~= STATUS_D_TO_P and train.status ~= STATUS_P
if not is_p_delivery_made then
local station = map_data.stations[train.p_station_id]
for i, item in ipairs(train.manifest) do
station.deliveries[item.name] = station.deliveries[item.name] + item.count
if station.deliveries[item.name] == 0 then
station.deliveries[item.name] = nil
end
end
station.deliveries_total = station.deliveries_total - 1
remove_manifest(map_data, station, train.manifest, 1)
if train.status == STATUS_P then
set_station_output_empty(station)
set_combinator_output(map_data, station.comb1, nil)
end
end
local is_r_delivery_made = train.status == STATUS_R_TO_D
if not is_r_delivery_made then
local station = map_data.stations[train.r_station_id]
for i, item in ipairs(train.manifest) do
station.deliveries[item.name] = station.deliveries[item.name] - item.count
if station.deliveries[item.name] == 0 then
station.deliveries[item.name] = nil
end
end
station.deliveries_total = station.deliveries_total - 1
remove_manifest(map_data, station, train.manifest, -1)
if train.status == STATUS_R then
set_station_output_empty(station)
set_combinator_output(map_data, station.comb1, nil)
end
end
train.r_station_id = 0
@@ -40,91 +23,12 @@ local function on_failed_delivery(map_data, train)
train.manifest = nil
end
local function on_station_built(map_data, stop)
local pos_x = stop.position.x
local pos_y = stop.position.y
local in_pos
local out_pos
local search_area
if stop.direction == 0 then
in_pos = {pos_x, pos_y - 1}
out_pos = {pos_x - 1, pos_y - 1}
search_area = {
{pos_x + DELTA - 1, pos_y + DELTA - 1},
{pos_x - DELTA + 1, pos_y - DELTA}
}
elseif stop.direction == 2 then
in_pos = {pos_x, pos_y}
out_pos = {pos_x, pos_y - 1}
search_area = {
{pos_x + DELTA, pos_y + DELTA - 1},
{pos_x - DELTA + 1, pos_y - DELTA + 1}
}
elseif stop.direction == 4 then
in_pos = {pos_x - 1, pos_y}
out_pos = {pos_x, pos_y}
search_area = {
{pos_x + DELTA - 1, pos_y + DELTA},
{pos_x - DELTA + 1, pos_y - DELTA + 1}
}
elseif stop.direction == 6 then
in_pos = {pos_x - 1, pos_y - 1}
out_pos = {pos_x - 1, pos_y}
search_area = {
{pos_x + DELTA - 1, pos_y + DELTA - 1},
{pos_x - DELTA, pos_y - DELTA + 1}
}
else
assert(false, "cybersyn: invalid direction of train stop")
end
local entity_in = nil
local entity_out = nil
local entities = stop.surface.find_entities(search_area)
for _, cur_entity in pairs(entities) do
if cur_entity.valid then
if cur_entity.name == "entity-ghost" then
if cur_entity.ghost_name == STATION_IN_NAME then
_, entity_in = cur_entity.silent_revive()
elseif cur_entity.ghost_name == STATION_OUT_NAME then
_, entity_out = cur_entity.silent_revive()
end
elseif cur_entity.name == STATION_IN_NAME then
entity_in = cur_entity
elseif cur_entity.name == STATION_OUT_NAME then
entity_out = cur_entity
end
end
end
if entity_in == nil then
entity_in = stop.surface.create_entity({
name = STATION_IN_NAME,
position = in_pos,
force = stop.force
})
end
entity_in.operable = false
entity_in.minable = false
entity_in.destructible = false
if entity_out == nil then
entity_out = stop.surface.create_entity({
name = STATION_OUT_NAME,
position = out_pos,
direction = stop.direction,
force = stop.force
})
end
entity_out.operable = false
entity_out.minable = false
entity_out.destructible = false
local function on_station_built(map_data, stop, comb1, comb2)
local station = {
entity = stop,
entity_in = entity_in,
entity_out = entity_out,
entity_stop = stop,
entity_comb1 = comb1,
entity_comb2 = comb2,
wagon_combs = nil,
deliveries_total = 0,
last_delivery_tick = 0,
priority = 0,
@@ -140,11 +44,9 @@ local function on_station_built(map_data, stop)
update_station_if_auto(map_data, station)
end
local function on_station_broken(map_data, stop)
--search for trains coming to the destroyed station
local station_id = stop.unit_number
local station = map_data.stations[station_id]
local function on_station_broken(map_data, station_id, station)
if station.deliveries_total > 0 then
--search for trains coming to the destroyed station
for train_id, train in pairs(map_data.trains) do
local is_r = train.r_station_id == station_id
local is_p = train.p_station_id == station_id
@@ -161,13 +63,226 @@ local function on_station_broken(map_data, stop)
end
end
end
if station.entity_in.valid then station.entity_in.destroy() end
if station.entity_out.valid then station.entity_out.destroy() end
map_data.stations[station_id] = nil
end
local function search_for_station_combinator(map_data, stop, comb_operation, comb_forbidden)
local pos_x = stop.position.x
local pos_y = stop.position.y
--TODO: fix search area
local search_area = {
{pos_x + DELTA - 1, pos_y + DELTA - 1},
{pos_x - DELTA + 1, pos_y - DELTA + 1}
}
local entities = stop.surface.find_entities(search_area)
for _, entity in pairs(entities) do
if
entity.valid and entity.name == COMBINATOR_NAME and
entity ~= comb_forbidden and map_data.to_stop[entity.unit_number] == stop
then
local control = entity.get_or_create_control_behavior().parameters
if control.operation == comb_operation then
return entity
end
end
end
end
local function on_combinator_built(map_data, comb)
local pos_x = comb.position.x
local pos_y = comb.position.y
--TODO: fix search area
local search_area = {
{pos_x + DELTA - 1, pos_y + DELTA - 1},
{pos_x - DELTA + 1, pos_y - DELTA + 1}
}
local stop = nil
local rail = nil
local entities = stop.surface.find_entities(search_area)
for _, cur_entity in pairs(entities) do
if cur_entity.valid then
if cur_entity.name == "train-stop" then
--NOTE: if there are multiple stops we take the later one
stop = cur_entity
elseif cur_entity.name == "rail-straight" then
rail = cur_entity
end
end
end
local out = comb.surface.create_entity({
name = COMBINATOR_OUT_NAME,
position = comb.position,
force = comb.force
})
comb.connect_neighbour({
target_entity = out,
source_wire_id = defines.circuit_connector_id.combinator_output,
wire = defines.wire_type.green,
})
comb.connect_neighbour({
target_entity = out,
source_wire_id = defines.circuit_connector_id.combinator_output,
wire = defines.wire_type.red,
})
map_data.to_output[comb.unit_number] = out
map_data.to_stop[comb.unit_number] = stop
local control = comb.get_or_create_control_behavior().parameters
if control.operation == OPERATION_WAGON_MANIFEST then
if rail then
update_station_from_rail(map_data, rail)
end
elseif control.operation == OPERATION_DEPOT then
if stop then
local station = map_data.stations[stop.unit_number]
local depot_comb = map_data.depots[stop.unit_number]
if depot_comb or station then
--NOTE: repeated combinators are ignored
else
map_data.depots[stop.unit_number] = comb
end
end
elseif control.operation == OPERATION_SECONDARY_IO then
if stop then
local station = map_data.stations[stop.unit_number]
if station and not station.entity_comb2 then
station.entity_comb2 = comb
end
end
elseif stop then
control.operation = OPERATION_PRIMARY_IO
local station = map_data.stations[stop.unit_number]
local depot_comb = map_data.depots[stop.unit_number]
if station then
--NOTE: repeated combinators are ignored
else
if depot_comb then
--NOTE: this will disrupt deliveries in progress that where dispatched from this station in a minor way
map_data.depots[stop.unit_number] = nil
end
--no station or depot
--add station
local comb2 = search_for_station_combinator(map_data, stop, OPERATION_SECONDARY_IO, nil)
station = {
entity_stop = stop,
entity_comb1 = comb,
entity_comb2 = comb2,
wagon_combs = nil,
deliveries_total = 0,
last_delivery_tick = 0,
priority = 0,
r_threshold = 0,
p_threshold = 0,
locked_slots = 0,
deliveries = {},
train_class = TRAIN_CLASS_AUTO,
accepted_layouts = {},
layout_pattern = nil,
}
map_data.stations[stop.unit_number] = station
update_station_if_auto(map_data, station)
end
end
end
local function on_combinator_broken(map_data, comb)
local out = map_data.to_output[comb.unit_number]
local stop = map_data.to_stop[comb.unit_number]
if stop and stop.valid then
local station = map_data.stations[stop.unit_number]
if station then
if station.comb1 == comb then
local comb1 = search_for_station_combinator(map_data, stop, OPERATION_PRIMARY_IO, comb)
if comb1 then
station.comb1 = comb1
else
on_station_broken(map_data, stop.unit_number, station)
map_data.depots[stop.unit_number] = search_for_station_combinator(map_data, stop, OPERATION_DEPOT, nil)
end
elseif station.comb2 == comb then
station.comb2 = search_for_station_combinator(map_data, stop, OPERATION_SECONDARY_IO, comb)
end
else
local depot_comb = map_data.depots[stop.unit_number]
if depot_comb == comb then
--NOTE: this will disrupt deliveries in progress that where dispatched from this station in a minor way
map_data.depots[stop.unit_number] = search_for_station_combinator(map_data, stop, OPERATION_DEPOT, comb)
end
end
end
if out and out.valid then
out.destroy()
end
map_data.to_output[comb.unit_number] = nil
map_data.to_stop[comb.unit_number] = nil
end
local function on_combinator_updated(map_data, comb)
--NOTE: this is the lazy way to implement updates and is not robust
on_combinator_broken(map_data, comb)
on_combinator_built(map_data, comb)
end
local function on_stop_built(map_data, stop)
local pos_x = stop.position.x
local pos_y = stop.position.y
--TODO: fix search area
local search_area = {
{pos_x + DELTA - 1, pos_y + DELTA - 1},
{pos_x - DELTA + 1, pos_y - DELTA + 1}
}
local comb2 = nil
local comb1 = nil
local depot_comb = nil
local entities = stop.surface.find_entities(search_area)
for _, entity in pairs(entities) do
if entity.valid and entity.name == COMBINATOR_NAME and map_data.to_stop[entity.unit_number] == nil then
map_data.to_stop[entity.unit_number] = stop
local control = entity.get_or_create_control_behavior().parameters
if control.operation == OPERATION_PRIMARY_IO then
comb1 = entity
elseif control.operation == OPERATION_SECONDARY_IO then
comb2 = entity
elseif control.operation == OPERATION_DEPOT then
depot_comb = entity
end
end
end
if comb1 then
on_station_built(map_data, stop, comb1, comb2)
elseif depot_comb then
map_data.depots[stop.unit_number] = depot_comb
end
end
local function on_stop_broken(map_data, stop)
local pos_x = stop.position.x
local pos_y = stop.position.y
--TODO: fix search area
local search_area = {
{pos_x + DELTA - 1, pos_y + DELTA - 1},
{pos_x - DELTA + 1, pos_y - DELTA + 1}
}
local entities = stop.surface.find_entities(search_area)
for _, entity in pairs(entities) do
if map_data.to_stop[entity.unit_number] == stop then
map_data.to_stop[entity.unit_number] = nil
end
end
local station = map_data.stations[stop.unit_number]
if station then
on_station_broken(map_data, stop.unit_number, station)
end
map_data.depots[stop.unit_number] = nil
end
local function on_station_rename(map_data, stop)
--search for trains coming to the renamed station
local station_id = stop.unit_number
@@ -193,24 +308,18 @@ local function on_station_rename(map_data, stop)
end
local function find_and_add_all_stations(map_data)
local function find_and_add_all_stations_from_nothing(map_data)
for _, surface in pairs(game.surfaces) do
local stops = surface.find_entities_filtered({type="train-stop"})
if stops then
for k, stop in pairs(stops) do
if stop.name == BUFFER_STATION_NAME then
local station = map_data.stations[stop.unit_number]
if not station then
on_station_built(map_data, stop)
end
end
local entities = surface.find_entities_filtered({name = COMBINATOR_NAME})
for k, comb in pairs(entities) do
if comb.valid then
on_combinator_built(map_data, comb)
end
end
end
end
local function on_train_arrives_depot(map_data, train_entity)
local train = map_data.trains[train_entity.id]
if train then
@@ -251,15 +360,15 @@ local function on_train_arrives_depot(map_data, train_entity)
r_station_id = 0,
manifest = nil,
}
update_train_layout(global, train)
update_train_layout(map_data, train)
map_data.trains[train_entity.id] = train
map_data.trains_available[train_entity.id] = true
local schedule = create_depot_schedule(train.depot_name)
train_entity.schedule = schedule
end
end
local function on_train_arrives_buffer(map_data, station_id, train)
local function on_train_arrives_buffer(map_data, stop, train)
local station_id = stop.unit_number
if train.manifest then
if train.status == STATUS_D_TO_P then
if train.p_station_id == station_id then
@@ -270,7 +379,7 @@ local function on_train_arrives_buffer(map_data, station_id, train)
for i, item in ipairs(train.manifest) do
signals[i] = {index = i, signal = {type = item.type, name = item.name}, count = item.count}
end
station.entity_out.get_control_behavior().parameters = signals
set_combinator_output(map_data, station.comb1, signals)
end
elseif train.status == STATUS_P_TO_R then
if train.r_station_id == station_id then
@@ -281,7 +390,7 @@ local function on_train_arrives_buffer(map_data, station_id, train)
for i, item in ipairs(train.manifest) do
signals[i] = {index = i, signal = {type = item.type, name = item.name}, count = -1}
end
station.entity_out.get_control_behavior().parameters = signals
set_combinator_output(map_data, station.comb1, signals)
end
else
on_failed_delivery(map_data, train)
@@ -293,31 +402,18 @@ local function on_train_arrives_buffer(map_data, station_id, train)
remove_train(map_data, train, train.entity.id)
end
end
local function on_train_leaves_station(map_data, train)
if train.manifest then
if train.status == STATUS_P then
train.status = STATUS_P_TO_R
local station = map_data.stations[train.p_station_id]
for i, item in ipairs(train.manifest) do
station.deliveries[item.name] = station.deliveries[item.name] + item.count
if station.deliveries[item.name] == 0 then
station.deliveries[item.name] = nil
end
end
station.deliveries_total = station.deliveries_total - 1
set_station_output_empty(station)
remove_manifest(map_data, station, train.manifest, 1)
set_combinator_output(map_data, station.comb1, nil)
elseif train.status == STATUS_R then
train.status = STATUS_R_TO_D
local station = map_data.stations[train.r_station_id]
for i, item in ipairs(train.manifest) do
station.deliveries[item.name] = station.deliveries[item.name] - item.count
if station.deliveries[item.name] == 0 then
station.deliveries[item.name] = nil
end
end
station.deliveries_total = station.deliveries_total - 1
set_station_output_empty(station)
remove_manifest(map_data, station, train.manifest, -1)
set_combinator_output(map_data, station.comb1, nil)
end
end
end
@@ -331,7 +427,6 @@ local function on_train_broken(map_data, train)
end
end
end
local function on_train_modified(map_data, pre_train_id, train_entity)
local train = map_data.trains[pre_train_id]
if train then
@@ -347,8 +442,6 @@ local function on_train_modified(map_data, pre_train_id, train_entity)
end
local function on_tick(event)
tick(global, mod_settings)
global.total_ticks = global.total_ticks + 1
@@ -358,8 +451,10 @@ 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)
if entity.name == "train-stop" then
on_stop_built(global, entity)
elseif entity.name == COMBINATOR_NAME then
on_combinator_built(global, entity)
elseif entity.type == "inserter" then
update_station_from_inserter(global, entity)
elseif entity.type == "pump" then
@@ -377,10 +472,11 @@ local function on_broken(event)
if train then
on_train_broken(global, train)
end
elseif entity.name == BUFFER_STATION_NAME then
on_station_broken(global, entity)
elseif entity.name == "train-stop" then
on_stop_broken(global, entity)
elseif entity.name == COMBINATOR_NAME then
on_combinator_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)
@@ -388,22 +484,9 @@ local function on_broken(event)
update_station_from_rail(global, entity)
end
end
local function on_train_changed(event)
local train_e = event.train
local train = global.trains[train_e.id]
if train_e.state == defines.train_state.wait_station and train_e.station ~= nil then
if train_e.station.name == DEPOT_STATION_NAME then
on_train_arrives_depot(global, train_e)
elseif train_e.station.name == BUFFER_STATION_NAME then
if train then
on_train_arrives_buffer(global, train_e.station.unit_number, train)
end
end
elseif event.old_state == defines.train_state.wait_station then
if train then
on_train_leaves_station(global, train)
end
local function on_rename(event)
if event.entity.name == "train-stop" then
on_station_rename(global, event.entity)
end
end
@@ -416,33 +499,48 @@ local function on_train_built(event)
on_train_modified(global, event.old_train_id_2, train_e)
end
end
local function on_train_changed(event)
local train_e = event.train
local train = global.trains[train_e.id]
if train_e.state == defines.train_state.wait_station then
local stop = train_e.station
if stop and stop.name == "train-stop" then
if global.stations[stop.unit_number] then
on_train_arrives_buffer(global, stop, train)
elseif global.depots[stop.unit_number] then
on_train_arrives_depot(global, train_e)
end
end
elseif event.old_state == defines.train_state.wait_station then
if train then
on_train_leaves_station(global, train)
end
end
end
local function on_surface_removed(event)
local surface = game.surfaces[event.surface_index]
if surface then
local train_stops = surface.find_entities_filtered({type = "train-stop"})
for _, entity in pairs(train_stops) do
if entity.name == BUFFER_STATION_NAME then
if entity.name == "train-stop" then
on_station_broken(global, entity)
end
end
end
end
local function on_rename(event)
if event.entity.name == BUFFER_STATION_NAME then
on_station_rename(global, event.entity)
end
end
local filter_built = {
{filter = "type", type = "train-stop"},
{filter = "type", type = "arithmetic-combinator"},
{filter = "type", type = "inserter"},
{filter = "type", type = "pump"},
{filter = "type", type = "straight-rail"},
}
local filter_broken = {
{filter = "type", type = "train-stop"},
{filter = "type", type = "arithmetic-combinator"},
{filter = "type", type = "inserter"},
{filter = "type", type = "pump"},
{filter = "type", type = "straight-rail"},
@@ -477,12 +575,12 @@ end)
script.on_init(function()
--TODO: we are not checking changed cargo capacities
find_and_add_all_stations(global)
--find_and_add_all_stations(global)
register_events()
end)
script.on_configuration_changed(function(data)
--TODO: we are not checking changed cargo capacities
find_and_add_all_stations(global)
--find_and_add_all_stations(global)
register_events()
end)