yaim904 wrote: Mon Mar 14, 2022 11:48 pm
LuaForce I feel that the guide lacks more information and/or details.
When I tried to see the name of the forces it showed me this.
Code: Select all
for _, Force in pairs( game.forces ) do
log( "'" .. Force.name .. "'" )
end
'player'
'enemy'
'neutral'
'conquest'
'ignore'
'capture'
'friendly'
Each game will always have the default forces: 'player', 'enemy', and 'neutral'. Mods can create additional forces, so eventually, there may be a maximum of 64 forces in a game. You have a mod that added 'conquest', 'ignore', capture', and 'friendly'.
What would it look like in a team multiplayer?
It depends. In the most simple case, there could be just the three default forces, and all players belong to force 'player'. But you could do this:
Code: Select all
script.on_init(function()
game.create_force("all_players_are_here")
end)
script.on_event(defines.events.on_player_created, function(event)
local player = game.players[event.player_index]
player.force = game.forces["all_players_are_here"]
end)
In that case, all players would belong to 'all_players_are_here', and if a tech is researched, all players would benefit from it. However, there would be nobody on force 'player'.
Consider this:
Code: Select all
script.on_init(function()
local new_force = game.create_force("second_player_force")
new_force.set_friend('player', true)
end)
script.on_event(defines.events.on_player_created, function(event)
if event.player_index % 2 == 0 then
local player = game.players[event.player_index]
player.force = game.forces["second_player_force"]
end
end)
Players 1, 3, 5, … would belong to 'player', players 2, 4, 6, … to 'second_player_force'. These forces would be allies, so turrets from one force wouldn't attack players from the other force, and players from 'player' could open buildings from 'second_player_force' as if they were their own. However, both forces would have to do their own research.
You can set the relations of forces:
Code: Select all
local force = game.player.force
force.set_friend(other_force, Boolean)
force.set_ceasefire(other_force, Boolean)
and read them:
Code: Select all
local is_friend = function(force, other_force)
return force.get_friend(other_force)
end
local is_neutral = function(force, other_force)
if force.get_ceasefire(other_force) and not force.get_friend(other_force) then
return true
end
end
local is_enemy = function(force, other_force)
if force.get_friend(other_force) or force.get_ceasefire(other_force) then
return false
end
return true
end
I want a way to identify the forces that contain the players.
All forces have the property force.players (a table). In unmodded games, game.forces['enemy'].players and game.forces['neutral'].players will be empty, and game.forces['player'].players will be a list of all players in the game. So you could do this:
Code: Select all
for f, force in pairs(game.forces) do
local p = next(force.players) and true or false
game.print("Force "..f.." has players: "..tostring(p))
end
next(table) will return the first element of a table, or nil if the table is empty.