Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make on_combinator_built search for straight rails by type #138

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions cybersyn/scripts/factorio-api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,7 @@ function se_get_space_elevator_name(cache, surface)

if not entity or not entity.valid then
--Caching failed, default to expensive lookup
entity = surface.find_entities_filtered({
name = SE_ELEVATOR_STOP_PROTO_NAME,
type = "train-stop",
limit = 1,
})[1]
entity = surface.find_entities_filtered({name = SE_ELEVATOR_STOP_PROTO_NAME, limit = 1})[1]

if entity then
cache.se_get_space_elevator_name[cache_idx] = entity
Expand Down
93 changes: 47 additions & 46 deletions cybersyn/scripts/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -239,19 +239,6 @@ local function on_combinator_built(map_data, comb)
{pos_x + 2, pos_y + 1.5}
}
end
local stop = nil
local rail = nil
local entities = comb.surface.find_entities_filtered({area = search_area, name = {"train-stop", "straight-rail"}})
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.type == "straight-rail" then
rail = cur_entity
end
end
end

local out = comb.surface.create_entity({
name = COMBINATOR_OUT_NAME,
Expand Down Expand Up @@ -289,42 +276,57 @@ local function on_combinator_built(map_data, comb)
map_data.to_comb[unit_number] = comb
map_data.to_comb_params[unit_number] = params
map_data.to_output[unit_number] = out
map_data.to_stop[unit_number] = stop

if op == MODE_WAGON then
local rail = nil
for _, entity in pairs(comb.surface.find_entities_filtered({area = search_area, type = "straight-rail"})) do
if entity.valid then
rail = entity
end
end
map_data.to_stop[unit_number] = nil
if rail then
update_stop_from_rail(map_data, rail, nil, true)
end
elseif stop then
local id = stop.unit_number--[[@as uint]]
local station = map_data.stations[id]
local depot = map_data.depots[id]
local refueler = map_data.refuelers[id]
if op == MODE_DEPOT then
if refueler then
on_refueler_broken(map_data, id, refueler)
end
if not station and not depot then
on_depot_built(map_data, stop, comb)
end
elseif op == MODE_REFUELER then
if not station and not depot and not refueler then
on_refueler_built(map_data, stop, comb)
end
elseif op == MODE_SECONDARY_IO then
if station and not station.entity_comb2 then
station.entity_comb2 = comb
end
elseif op == MODE_PRIMARY_IO then
if refueler then
on_refueler_broken(map_data, id, refueler)
end
if depot then
on_depot_broken(map_data, id, depot)
else
local stop = nil
for _, entity in pairs(comb.surface.find_entities_filtered({area = search_area, name = "train-stop"})) do
if entity.valid then
stop = entity
end
if not station then
local comb2 = search_for_station_combinator(map_data, stop, MODE_SECONDARY_IO, comb)
on_station_built(map_data, stop, comb, comb2)
end
map_data.to_stop[unit_number] = stop
if stop then
local id = stop.unit_number--[[@as uint]]
local station = map_data.stations[id]
local depot = map_data.depots[id]
local refueler = map_data.refuelers[id]
if op == MODE_DEPOT then
if refueler then
on_refueler_broken(map_data, id, refueler)
end
if not station and not depot then
on_depot_built(map_data, stop, comb)
end
elseif op == MODE_REFUELER then
if not station and not depot and not refueler then
on_refueler_built(map_data, stop, comb)
end
elseif op == MODE_SECONDARY_IO then
if station and not station.entity_comb2 then
station.entity_comb2 = comb
end
elseif op == MODE_PRIMARY_IO then
if refueler then
on_refueler_broken(map_data, id, refueler)
end
if depot then
on_depot_broken(map_data, id, depot)
end
if not station then
local comb2 = search_for_station_combinator(map_data, stop, MODE_SECONDARY_IO, comb)
on_station_built(map_data, stop, comb, comb2)
end
end
end
end
Expand Down Expand Up @@ -691,9 +693,8 @@ 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.valid and entity.name == "train-stop" then
for _, entity in pairs(surface.find_entities_filtered({name = "train-stop"})) do
if entity.valid then
on_stop_broken(global, entity)
end
end
Expand Down