Page 1 of 1

How does research work in PVP?

Posted: Mon Mar 14, 2022 3:13 pm
by yaim904
Context
I'm creating a MOD that interacts with technologies and research, but I don't know how this part of the game works in multiplayer.
Questions
  1. Each team investigates the technologies separately??
  2. When researching a technology, does it affect both teams?
  3. Each team has a different technology queue??
Want
How do I access the research of both teams?

Re: How does research work in PVP?

Posted: Mon Mar 14, 2022 3:37 pm
by FuryoftheStars
I think, though don't quote me on this, that each team has the same list of technologies (ie, you can't have Tech A available only to Team A), but they research them separately. I believe it's tracked through the Forces object.

Re: How does research work in PVP?

Posted: Mon Mar 14, 2022 4:03 pm
by Pi-C
yaim904 wrote: Mon Mar 14, 2022 3:13 pm How do I access the research of both teams?
Like FuryoftheStars commented, you can access force.technologies["tech-name"].researched (read and write). You definitely should listen to these events
This one could be useful as well: on_technology_effects_reset will give you event.force in the event data, the other events have event.research.force.

Re: How does research work in PVP?

Posted: Mon Mar 14, 2022 4:35 pm
by yaim904
FuryoftheStars wrote: Mon Mar 14, 2022 3:37 pm
So, not exist difference queues for each team.

And i can use the events to do any i want (or can).
Pi-C wrote: Mon Mar 14, 2022 4:03 pm
I am right??

Re: How does research work in PVP?

Posted: Mon Mar 14, 2022 4:42 pm
by FuryoftheStars
yaim904 wrote: Mon Mar 14, 2022 4:35 pm So, not exist difference queues for each team.
I'm sorry, I'm reading the word "queue" differently then how I intended my post to be interpreted.

Let me try this over. :)
yaim904 wrote: Mon Mar 14, 2022 3:13 pm
  1. Each team investigates the technologies separately??
  2. When researching a technology, does it affect both teams?
  3. Each team has a different technology queue??
  1. Yes
  2. No
  3. Yes
  4. (What I meant in my post) Can each team have a different technology tree? No

Re: How does research work in PVP?

Posted: Mon Mar 14, 2022 4:52 pm
by Pi-C
yaim904 wrote: Mon Mar 14, 2022 4:35 pm So, not exist difference queues for each team.
Yes: All technologies in the game can be researched by all forces (even the biters have force.technologies although they'll never research anything -- at least in vanilla). But each force must research individually, so if force A has researched cars (for example), force B must also research it before they can build cars.
And i can use the events to do any i want (or can).
Yes. For example, if a force has researched the rocket silo, you could reset all their research so they must start over again. That wouldn't make sense, but you could do it. :-)

Re: How does research work in PVP?

Posted: Mon Mar 14, 2022 6:45 pm
by yaim904
LuaForce
I thought foce.player has all the players.
Pi-C wrote: Mon Mar 14, 2022 4:52 pm
So, force has force.team1 and force.team2??

Ok, I'm learning a lot.

Re: How does research work in PVP?

Posted: Mon Mar 14, 2022 7:45 pm
by robot256
In this context, "force" means any LuaForce object reference. You can get the LuaForce reference from an event, a player or entity object, or from the game.forces list.

game.forces is the list of all forces (teams).

For example, `game.forces["team1"].players` is the list of all players on team1.

Re: How does research work in PVP?

Posted: Mon Mar 14, 2022 11:48 pm
by yaim904
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'
What would it look like in a team multiplayer?

I want a way to identify the forces that contain the players.

Re: How does research work in PVP?

Posted: Tue Mar 15, 2022 3:13 am
by Pi-C
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.

Re: How does research work in PVP?

Posted: Tue Mar 15, 2022 8:20 pm
by yaim904
I understood everything!! :D

And my question changes to: Why is all this not in the API guide?
Thanks for the explanation.
I have to reconstruct a part of the code, but now that I understand it, it excites me.