local function create_force(player) if not (player and player.valid) then return end -- Protect against players having the same name as an existing force local name = game.forces[player.name] and "" or player.name while name == "" or game.forces[name] do name = script.mod_name.."-"..player.name.."-"..math.floor(math.random(100)) end -- Create and assign force game.create_force(name) player.force = name -- Share chart player.force.share_chart = true end script.on_event(defines.events.on_player_joined_game, function(event) local player = game.players[event.player_index] local f_name = player.force.name if player.name ~= f_name and not f_name:match("^"..script.mod_name.."%-"..player.name.."%-%d*$") then create_force(player) policy(player.force) end end) local function policy(player_force) if not (player_force and player_force.valid) then return end for f, force in pairs(game.forces) do -- Ignore player_force and forces without players if player_force ~= force and force.name == "player" or next(force.players) then player_force.set_cease_fire(force, true) player_force.set_friend(force, true) force.set_cease_fire(player_force, true) force.set_friend(player_force, true) end end end -- Make sure all players are on their own force local function init() local f_name for p, player in pairs(game.players) do f_name = player.force.name if player.name ~= f_name and not f_name:match("^"..script.mod_name.."%-"..player.name.."%-%d*$") then create_force(player) policy(player.force) end end end script.on_event(defines.events.on_player_joined_game, function(event) local player = game.players[event.player_index] create_force(player) policy(player.force) end) -- We can't remove the force before the player has been removed, so we store the -- force name with the player for now. script.on_event(defines.events.on_pre_player_removed, function(event) local player = game.players[event.player_index] global.remove_forces = global.remove_forces or {} global.remove_forces[player.index] = player.force.name end) -- We only get the index of the removed player, but we can remove the force because -- we've stored its name. script.on_event(defines.events.on_player_removed, function(event) local player = game.players[event.player_index] local force = global.remove_forces[player.index] -- Merge removed player's force with force "player" (all entities will be moved to -- force "player" and the other force will be deleted) game.merge_forces(force, "player") global.remove_forces[player.index] = nil if not next(global.remove_forces) then global.remove_forces = nil end end) script.on_init(init) script.on_configuration_changed(init)