@@ -14,8 +14,6 @@ factorissimo.on_event(factorissimo.events.on_init(), function()
1414 storage .factories_by_entity = storage .factories_by_entity or {}
1515 -- Map: Surface index -> list of factories on it
1616 storage .surface_factories = storage .surface_factories or {}
17- -- Scalar
18- storage .next_factory_surface = storage .next_factory_surface or 0
1917end )
2018
2119-- RECURSION TECHNOLOGY --
@@ -45,17 +43,32 @@ local function was_this_placed_on_a_space_exploration_spaceship(layout, building
4543 }
4644end
4745
48- --- @param surface LuaSurface | LuaPlanet
46+ local function surface_localised_name (surface )
47+ if surface .localised_name then
48+ return surface .localised_name
49+ elseif surface .planet and surface .planet .prototype .localised_name then
50+ return {" " , " [img=space-location." , surface .planet .name , " ] " , surface .planet .prototype .localised_name }
51+ else
52+ return {" ?" , {" space-location-name." .. surface .name }, surface .name }
53+ end
54+ end
55+
4956--- @return string
50- local function true_name (surface )
51- if surface .name :find (" %-factory%-floor$" ) then
52- return surface .name :gsub (" %-factory%-floor$" , " " )
53- elseif (surface .object_name or type (surface )) == " LuaSurface" then
54- if surface .planet or surface .platform then
55- return surface .planet .name
56- end
57+ local function which_surface_should_this_new_factory_be_placed_on (layout , building )
58+ if was_this_placed_on_a_space_exploration_spaceship (layout , building ) then
59+ return " se-spaceship-factory-floor"
60+ end
61+
62+ local surface = building .surface
63+ if layout .surface_override then
64+ return layout .surface_override
65+ elseif surface .platform then
66+ return " space-factory-floor"
67+ elseif surface .planet then
68+ return surface .planet .name :gsub (" %-factory%-floor" , " " ) .. " -factory-floor"
69+ else
70+ return surface .name :gsub (" %-factory%-floor" , " " ) .. " -factory-floor"
5771 end
58- return surface .name :gsub (" %-%d+$" , " " )
5972end
6073
6174local function set_factory_active_or_inactive (factory )
@@ -68,30 +81,10 @@ local function set_factory_active_or_inactive(factory)
6881 local position = building .position
6982
7083 local function can_place_factory_here ()
71- -- Check if a player is trying to cheat by moving factories to diffrent planets.
72- local original_planet = factory .original_planet
73- local inside_surface = factory .inside_surface
74-
75- if original_planet and original_planet .valid then
76- if inside_surface and inside_surface .valid and inside_surface .name ~= " se-spaceship-factory-floor" then
77- local original_planet_name = true_name (original_planet )
78- local surface_name = true_name (surface )
79- if original_planet_name ~= surface_name then
80- local original_planet_prototype = (game .planets [original_planet_name ] or original_planet ).prototype
81- local flying_text = {" factory-connection-text.invalid-placement-planet" , original_planet_name , original_planet_prototype .localised_name }
82- return false , flying_text , true
83- end
84- end
85- end
86-
87- -- In space exploration, we differentiate between space factories and spaceship factories.
88- if script .active_mods [" space-exploration" ] then
89- if inside_surface and inside_surface .valid then
90- local spaceship = was_this_placed_on_a_space_exploration_spaceship (factory .layout , building )
91- if inside_surface .name == " space-factory-floor" and spaceship then
92- return false , {" factory-connection-text.se-must-not-build-factory-building-on-a-spaceship" }, true
93- end
94- end
84+ -- Check if a player is trying to cheat by moving factories between surfaces.
85+ if factory .inside_surface .name ~= which_surface_should_this_new_factory_be_placed_on (factory .layout , building ) then
86+ flying_text = {" factory-connection-text.invalid-placement-surface" , surface_localised_name (factory .inside_surface ), surface_localised_name (surface )}
87+ return false , flying_text , true
9588 end
9689
9790 if settings .global [" Factorissimo2-free-recursion" ].value then
@@ -211,21 +204,6 @@ end)
211204
212205-- FACTORY GENERATION --
213206
214- local function which_void_surface_should_this_new_factory_be_placed_on (layout , building )
215- if was_this_placed_on_a_space_exploration_spaceship (layout , building ) then
216- return " se-spaceship-factory-floor"
217- end
218- if layout .surface_override then return layout .surface_override end
219-
220- local surface = building .surface
221- if surface .planet then
222- return (surface .planet .name .. " -factory-floor" ):gsub (" %-factory%-floor%-factory%-floor" , " -factory-floor" )
223- end
224-
225- storage .next_factory_surface = storage .next_factory_surface + 1
226- return storage .next_factory_surface .. " -factory-floor"
227- end
228-
229207factorissimo .on_event (defines .events .on_surface_created , function (event )
230208 local surface = game .get_surface (event .surface_index )
231209 if not surface .name :find (" %-factory%-floor$" ) then return end
@@ -254,12 +232,23 @@ local function find_first_unused_position(surface)
254232 return # used_indexes + 1
255233end
256234
257- local function surface_sanity_checks (surface )
235+ local function surface_sanity_checks (surface , building )
236+ if remote .interfaces [" RSO" ] then -- RSO compatibility
237+ pcall (remote .call , " RSO" , " ignoreSurface" , surface .name )
238+ end
239+
258240 if surface .name == " space-factory-floor" then
259241 surface .localised_name = {" space-location-name.space-factory-floor" }
260242 surface .set_property (" gravity" , 0 )
261243 surface .set_property (" pressure" , 0 )
262244 surface .set_property (" magnetic-field" , 0 )
245+ elseif surface .name == " se-spaceship-factory-floor" then
246+ surface .localised_name = {" space-location-name.se-spaceship-factory-floor" }
247+ surface .set_property (" gravity" , 0 )
248+ surface .set_property (" pressure" , 0 )
249+ surface .set_property (" magnetic-field" , 0 )
250+ else
251+ surface .localised_name = surface_localised_name (surface )
263252 end
264253
265254 surface .daytime = 0.5
@@ -269,30 +258,35 @@ local function surface_sanity_checks(surface)
269258 surface .map_gen_settings = {width = 2 , height = 2 }
270259end
271260
272- local function create_factory_position (layout , building )
273- local surface_name = which_void_surface_should_this_new_factory_be_placed_on (layout , building )
261+ local function create_factory_surface (surface_name )
262+ assert (_G .surface == nil )
263+
264+ if remote .interfaces [" RSO" ] then -- RSO compatibility
265+ pcall (remote .call , " RSO" , " ignoreSurface" , surface_name )
266+ end
267+
274268 local surface = game .get_surface (surface_name )
269+ if surface then
270+ return surface
271+ end
275272
276- if not surface then
277- if remote . interfaces [ " RSO " ] then -- RSO compatibility
278- pcall ( remote . call , " RSO " , " ignoreSurface " , surface_name )
279- end
273+ local planet = game . planets [ surface_name ]
274+ if planet then
275+ return planet . create_surface ( )
276+ end
280277
281- local planet = game .planets [surface_name ]
282- if planet then
283- surface = planet .surface or planet .create_surface ()
284- end
278+ return game .create_surface (surface_name , {width = 2 , height = 2 })
279+ end
285280
286- if not surface then
287- surface = game .create_surface (surface_name , {width = 2 , height = 2 })
288- surface .localised_name = {" space-location-name.factory-floor" , storage .next_factory_surface }
289- end
281+ local function create_factory_position (layout , building )
282+ local surface_name = which_surface_should_this_new_factory_be_placed_on (layout , building )
283+ local surface = game .get_surface (surface_name )
290284
291- assert ( surface )
292- assert ( surface . name == surface_name )
285+ if not surface then
286+ surface = create_factory_surface ( surface_name )
293287 end
294288
295- surface_sanity_checks (surface )
289+ surface_sanity_checks (surface , building )
296290
297291 local n = find_first_unused_position (surface ) - 1
298292 local FACTORISSIMO_CHUNK_SPACING = 16
345339
346340local function add_hidden_tile_rect (factory )
347341 local surface = factory .inside_surface
348- local layout = factory .layout
349342 local xmin = factory .inside_x - 64
350343 local ymin = factory .inside_y - 64
351344 local xmax = factory .inside_x + 64
@@ -487,6 +480,8 @@ local sprite_path_translation = {
487480 virtual = " virtual-signal" ,
488481}
489482local function generate_factory_item_description (factory )
483+ local bound_to = {" item-description.bound-to" , surface_localised_name (factory .inside_surface )}
484+
490485 local overlay = factory .inside_overlay_controller
491486 local params = {}
492487 if overlay and overlay .valid then
@@ -500,7 +495,11 @@ local function generate_factory_item_description(factory)
500495 end
501496 end
502497 local params = table.concat (params , " \n " )
503- if params ~= " " then return " [font=heading-2]" .. params .. " [/font]" end
498+ if params == " " then
499+ return bound_to
500+ else
501+ return {" " , bound_to , " \n [font=heading-2]" .. params .. " [/font]" }
502+ end
504503end
505504
506505local function is_completely_empty (factory )
@@ -547,7 +546,7 @@ local function cleanup_factory_interior(factory)
547546 factory .inside_surface .set_tiles (out_of_map_tiles )
548547
549548 local factory_lists = {storage .factories , storage .saved_factories , storage .factories_by_entity }
550- for surface_index , factory_list in pairs (storage .surface_factories ) do
549+ for _ , factory_list in pairs (storage .surface_factories ) do
551550 factory_lists [# factory_lists + 1 ] = factory_list
552551 end
553552
@@ -734,7 +733,6 @@ local function create_fresh_factory(entity)
734733 local layout = remote_api .create_layout (entity .name , entity .quality )
735734 local factory = create_factory_interior (layout , entity )
736735 create_factory_exterior (factory , entity )
737- factory .original_planet = entity .surface .planet
738736 set_factory_active_or_inactive (factory )
739737 return factory
740738end
0 commit comments