mirror of
https://github.com/ComfyFactory/ComfyFactorio.git
synced 2025-01-04 00:15:45 +02:00
Fixed power issue between surfaces
Changes: - Fixed an issue that would sometimes cause poles between surfaces not connect due to connection limit. Current solution is not ideal as power loss can still happen (since some poles will be forcefully disconnected), but poles between surfaces are now guaranteed to be connected.
This commit is contained in:
parent
0f50d2d96c
commit
de5620b7eb
@ -1605,4 +1605,69 @@ function Public.get_random_dictionary_entry(t, key)
|
||||
end
|
||||
end
|
||||
|
||||
-- mainly used to connect multi-surface poles
|
||||
function Public.force_connect_poles(pole1, pole2)
|
||||
if not pole1 then return end
|
||||
if not pole1.valid then return end
|
||||
if not pole2 then return end
|
||||
if not pole2.valid then return end
|
||||
|
||||
-- force connections for testing (by placing many poles around the substations)
|
||||
-- for _, e in pairs(pole1.surface.find_entities_filtered{type="electric-pole", position = pole1.position, radius = 10}) do
|
||||
-- pole1.connect_neighbour(e)
|
||||
-- end
|
||||
|
||||
-- for _, e in pairs(pole2.surface.find_entities_filtered{type="electric-pole", position = pole2.position, radius = 10}) do
|
||||
-- pole2.connect_neighbour(e)
|
||||
-- end
|
||||
|
||||
local success = pole1.connect_neighbour(pole2)
|
||||
if success then return end
|
||||
|
||||
local pole1_neighbours = pole1.neighbours['copper']
|
||||
local pole2_neighbours = pole2.neighbours['copper']
|
||||
|
||||
-- try avoiding disconnecting more poles than needed
|
||||
local disconnect_from_pole1 = false
|
||||
local disconnect_from_pole2 = false
|
||||
|
||||
if #pole1_neighbours >= #pole2_neighbours then
|
||||
disconnect_from_pole1 = true
|
||||
end
|
||||
|
||||
if #pole2_neighbours >= #pole1_neighbours then
|
||||
disconnect_from_pole2 = true
|
||||
end
|
||||
|
||||
if disconnect_from_pole1 then
|
||||
-- Prioritise disconnecting last connections as those are most likely redundant (at least for holds, although even then it's not always the case)
|
||||
for i = #pole1_neighbours, 1, -1 do
|
||||
local e = pole1_neighbours[i]
|
||||
-- only disconnect poles from same surface
|
||||
if e and e.valid and e.surface.name == pole1.surface.name then
|
||||
pole1.disconnect_neighbour(e)
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if disconnect_from_pole2 then
|
||||
-- Prioritise disconnecting last connections as those are most likely redundant (at least for holds, although even then it's not always the case)
|
||||
for i = #pole2_neighbours, 1, -1 do
|
||||
local e = pole2_neighbours[i]
|
||||
-- only disconnect poles from same surface
|
||||
if e and e.valid and e.surface.name == pole2.surface.name then
|
||||
pole2.disconnect_neighbour(e)
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local success2 = pole1.connect_neighbour(pole2)
|
||||
if not success2 then
|
||||
-- This can happen if in future pole reach connection limit(5) with poles from other surfaces
|
||||
log("Error: power fix didn't work")
|
||||
end
|
||||
end
|
||||
|
||||
return Public
|
@ -330,7 +330,9 @@ function Public.place_boat(boat, floor_tile, place_entities_bool, correct_tiles,
|
||||
e.rotatable = false
|
||||
if i == 1 then
|
||||
boat.upstairs_pole = e
|
||||
Public.try_connect_upstairs_and_downstairs_poles(boat)
|
||||
if boat.downstairs_poles and boat.downstairs_poles[1] then
|
||||
Common.force_connect_poles(boat.upstairs_pole, boat.downstairs_poles[1][1])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -729,17 +731,6 @@ end
|
||||
-- end
|
||||
-- end
|
||||
|
||||
|
||||
function Public.try_connect_upstairs_and_downstairs_poles(boat)
|
||||
-- local memory = Memory.get_crew_memory()
|
||||
|
||||
if not (boat and boat.upstairs_pole and boat.upstairs_pole.valid and boat.downstairs_poles and boat.downstairs_poles[1] and boat.downstairs_poles[1][1] and boat.downstairs_poles[1][1].valid) then return end
|
||||
|
||||
boat.upstairs_pole.connect_neighbour(boat.downstairs_poles[1][1])
|
||||
end
|
||||
|
||||
|
||||
|
||||
local function process_entity_on_boat_unteleportable(memory, boat, newsurface, vector, players_just_offside, oldsurface_name, newsurface_name, e, name)
|
||||
|
||||
local un = e.unit_number
|
||||
@ -913,9 +904,11 @@ local function process_entity_on_boat_teleportable(memory, boat, newsurface, new
|
||||
ee = e.clone{position = p2, surface = newsurface, create_build_effect_smoke = false}
|
||||
end
|
||||
|
||||
if boat.upstairs_pole and e == boat.upstairs_pole then
|
||||
if e == boat.upstairs_pole then
|
||||
boat.upstairs_pole = ee
|
||||
Public.try_connect_upstairs_and_downstairs_poles(boat)
|
||||
if boat.downstairs_poles and boat.downstairs_poles[1] then
|
||||
Common.force_connect_poles(boat.upstairs_pole, boat.downstairs_poles[1][1])
|
||||
end
|
||||
end
|
||||
|
||||
if not (e.name == 'car' or e.name == 'tank' or e.name == 'spidertron') then
|
||||
|
@ -210,9 +210,7 @@ function Public.create_hold_surface(nth)
|
||||
end
|
||||
end
|
||||
if nth >= 2 then
|
||||
if boat.downstairs_poles[nth][1] and boat.downstairs_poles[nth][1].valid and boat.downstairs_poles[nth-1][2] and boat.downstairs_poles[nth-1][2].valid then
|
||||
boat.downstairs_poles[nth][1].connect_neighbour(boat.downstairs_poles[nth-1][2])
|
||||
end
|
||||
Common.force_connect_poles(boat.downstairs_poles[nth][1], boat.downstairs_poles[nth-1][2])
|
||||
end
|
||||
|
||||
if not boat.downstairs_fluid_storages then boat.downstairs_fluid_storages = {} end
|
||||
|
Loading…
Reference in New Issue
Block a user