From eef1ac57a1cb48cdad2a505d29629a82e3e83514 Mon Sep 17 00:00:00 2001 From: Luke Granger-Brown Date: Sat, 5 Feb 2022 17:51:29 +0000 Subject: [PATCH] ops/factorio/multiworld: actually add debugrenameworld --- ops/factorio/multiworld/multiworld.lua | 94 ++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/ops/factorio/multiworld/multiworld.lua b/ops/factorio/multiworld/multiworld.lua index e00229d0ee..9b1aaeea60 100644 --- a/ops/factorio/multiworld/multiworld.lua +++ b/ops/factorio/multiworld/multiworld.lua @@ -457,6 +457,93 @@ local function debugeditor_command(cmd) player.toggle_map_editor() end +local function debugrenameworld_command(cmd) + local p + local player = game.player + + if player == nil then + print('Sorry, need to be in game') + return + end + p = player.print + + if player.admin ~= true then + p('Need to be an admin', c) + return + end + + if cmd.parameter == nil then + p('/debugrenameworld ', c) + return + end + + local bits = split_whitespace(cmd.parameter) + if #bits ~= 2 then + p('/debugrenameworld ', c) + return + end + + local old_name = bits[1] + local new_name = bits[2] + + if old_name == 'nauvis' or old_name == 'spawn' then + p('Sorry, renaming the spawn world is not permitted', c) + return + end + + local surface = get_surface_by_name(old_name) + if surface == nil then + p('No such world ' .. old_name, c) + return + end + + -- We need to get some stuff before we rename the surface, since it's name dependent. + local force = force_for_surface(surface) + + -- Rename the surface. + surface.name = new_name + p('Renamed surface ' .. old_name .. ' to ' .. new_name, c) + + -- Check if we need to rename the force. + local new_force_name = force_name_for_surface(surface) + if force == nil then + p('Creating new force ' .. new_force_name) + force = game.create_force(new_force_name) + local player_owned_entities = surface.find_entities_filtered { + force = "player" + } + p('Moving everything (' .. #player_owned_entities .. + ' entities) from the player force to the ' .. force.name .. + ' force') + for _, entity in pairs(player_owned_entities) do + entity.force = force + end + elseif force.name ~= new_force_name then + p('Renaming force ' .. force.name .. ' to ' .. new_force_name) + local new_force = game.create_force(new_force_name) + game.merge_forces(force.name, new_force.name) + force = new_force + end + + -- Move inventories and locations, if any, from the old name to the new name. + if global.inventories == nil then + global.inventories = {} + end + if global.locations == nil then + global.locations = {} + end + local old_surface_inventories = global.inventories[old_name] + if old_surface_inventories == nil then + old_surface_inventories = {} + end + local old_surface_locations = global.locations[old_name] + if old_surface_locations == nil then + old_surface_locations = {} + end + global.inventories[new_name] = old_surface_inventories + global.locations[new_name] = old_surface_locations +end + local function debugresetworldstate_command(cmd) local p local player = game.player @@ -549,6 +636,13 @@ multiworld.add_commands = function() error_handler("/debugeditor", game.player)) end) + commands.add_command('debugrenameworld', + 'Renames a surface and remaps corresponding state', + function(cmd) + xpcall(function() debugrenameworld_command(cmd) end, + error_handler("/debugrenameworld", game.player)) + end) + commands.add_command('debugresetworldstate', 'Resets the multiworld state for a surface without touching the surface itself', function(cmd)