Resolve random players and spawn points in server replays.

This commit is contained in:
Paul Chote
2020-10-19 11:33:20 +01:00
committed by abcdefg30
parent 7b75a78e38
commit 6b6b1e56e6
8 changed files with 120 additions and 35 deletions

View File

@@ -17,6 +17,7 @@ using Eluant.ObjectBinding;
using OpenRA.Network;
using OpenRA.Primitives;
using OpenRA.Scripting;
using OpenRA.Support;
using OpenRA.Traits;
using OpenRA.Widgets;
@@ -100,19 +101,19 @@ namespace OpenRA
readonly StanceColors stanceColors;
static FactionInfo ChooseFaction(World world, string name, bool requireSelectable = true)
public static FactionInfo ResolveFaction(string factionName, IEnumerable<FactionInfo> factionInfos, MersenneTwister playerRandom, bool requireSelectable = true)
{
var selectableFactions = world.Map.Rules.Actors["world"].TraitInfos<FactionInfo>()
var selectableFactions = factionInfos
.Where(f => !requireSelectable || f.Selectable)
.ToList();
var selected = selectableFactions.FirstOrDefault(f => f.InternalName == name)
?? selectableFactions.Random(world.SharedRandom);
var selected = selectableFactions.FirstOrDefault(f => f.InternalName == factionName)
?? selectableFactions.Random(playerRandom);
// Don't loop infinite
for (var i = 0; i <= 10 && selected.RandomFactionMembers.Any(); i++)
{
var faction = selected.RandomFactionMembers.Random(world.SharedRandom);
var faction = selected.RandomFactionMembers.Random(playerRandom);
selected = selectableFactions.FirstOrDefault(f => f.InternalName == faction);
if (selected == null)
@@ -122,7 +123,13 @@ namespace OpenRA
return selected;
}
static FactionInfo ChooseDisplayFaction(World world, string factionName)
static FactionInfo ResolveFaction(World world, string factionName, MersenneTwister playerRandom, bool requireSelectable)
{
var factionInfos = world.Map.Rules.Actors["world"].TraitInfos<FactionInfo>();
return ResolveFaction(factionName, factionInfos, playerRandom, requireSelectable);
}
static FactionInfo ResolveDisplayFaction(World world, string factionName)
{
var factions = world.Map.Rules.Actors["world"].TraitInfos<FactionInfo>().ToArray();
@@ -141,7 +148,7 @@ namespace OpenRA
return client.Name;
}
public Player(World world, Session.Client client, PlayerReference pr)
public Player(World world, Session.Client client, PlayerReference pr, MersenneTwister playerRandom)
{
World = world;
InternalName = pr.Name;
@@ -157,11 +164,11 @@ namespace OpenRA
PlayerName = ResolvePlayerName(client, world.LobbyInfo.Clients, world.Map.Rules.Actors["player"].TraitInfos<IBotInfo>());
BotType = client.Bot;
Faction = ChooseFaction(world, client.Faction, !pr.LockFaction);
DisplayFaction = ChooseDisplayFaction(world, client.Faction);
Faction = ResolveFaction(world, client.Faction, playerRandom, !pr.LockFaction);
DisplayFaction = ResolveDisplayFaction(world, client.Faction);
var assignSpawnPoints = world.WorldActor.TraitOrDefault<IAssignSpawnPoints>();
HomeLocation = assignSpawnPoints?.AssignHomeLocation(world, client) ?? pr.HomeLocation;
HomeLocation = assignSpawnPoints?.AssignHomeLocation(world, client, playerRandom) ?? pr.HomeLocation;
SpawnPoint = assignSpawnPoints?.SpawnPointForPlayer(this) ?? client.SpawnPoint;
DisplaySpawnPoint = client.SpawnPoint;
}
@@ -175,8 +182,8 @@ namespace OpenRA
Playable = pr.Playable;
Spectating = pr.Spectating;
BotType = pr.Bot;
Faction = ChooseFaction(world, pr.Faction, false);
DisplayFaction = ChooseDisplayFaction(world, pr.Faction);
Faction = ResolveFaction(world, pr.Faction, playerRandom, false);
DisplayFaction = ResolveDisplayFaction(world, pr.Faction);
HomeLocation = pr.HomeLocation;
SpawnPoint = DisplaySpawnPoint = 0;
}

View File

@@ -1203,8 +1203,9 @@ namespace OpenRA.Server
// HACK: NonCombatant and non-Playable players are set to null to simplify replay tracking
// The null padding is needed to keep the player indexes in sync with world.Players on the clients
// This will need to change if future code wants to use worldPlayers for other purposes
var playerRandom = new MersenneTwister(LobbyInfo.GlobalSettings.RandomSeed);
foreach (var cmpi in Map.Rules.Actors["world"].TraitInfos<ICreatePlayersInfo>())
cmpi.CreateServerPlayers(Map, LobbyInfo, worldPlayers);
cmpi.CreateServerPlayers(Map, LobbyInfo, worldPlayers, playerRandom);
if (recorder != null)
{

View File

@@ -18,6 +18,7 @@ using OpenRA.GameRules;
using OpenRA.Graphics;
using OpenRA.Network;
using OpenRA.Primitives;
using OpenRA.Support;
namespace OpenRA.Traits
{
@@ -365,21 +366,28 @@ namespace OpenRA.Traits
}
[RequireExplicitImplementation]
public interface ICreatePlayers { void CreatePlayers(World w); }
public interface ICreatePlayers { void CreatePlayers(World w, MersenneTwister playerRandom); }
[RequireExplicitImplementation]
public interface ICreatePlayersInfo : ITraitInfoInterface
{
void CreateServerPlayers(MapPreview map, Session lobbyInfo, List<GameInformation.Player> players);
void CreateServerPlayers(MapPreview map, Session lobbyInfo, List<GameInformation.Player> players, MersenneTwister playerRandom);
}
[RequireExplicitImplementation]
public interface IAssignSpawnPoints
{
CPos AssignHomeLocation(World world, Session.Client client);
CPos AssignHomeLocation(World world, Session.Client client, MersenneTwister playerRandom);
int SpawnPointForPlayer(Player player);
}
[RequireExplicitImplementation]
public interface IAssignSpawnPointsInfo : ITraitInfoInterface
{
object InitializeState(MapPreview map, Session lobbyInfo);
int AssignSpawnPoint(object state, Session lobbyInfo, Session.Client client, MersenneTwister playerRandom);
}
public interface IBotInfo : ITraitInfoInterface
{
string Type { get; }

View File

@@ -211,8 +211,10 @@ namespace OpenRA
LongBitSet<PlayerBitMask>.Reset();
// Add players
// Create an isolated RNG to simplify synchronization between client and server player faction/spawn assignments
var playerRandom = new MersenneTwister(orderManager.LobbyInfo.GlobalSettings.RandomSeed);
foreach (var cmp in WorldActor.TraitsImplementing<ICreatePlayers>())
cmp.CreatePlayers(this);
cmp.CreatePlayers(this, playerRandom);
// Set defaults for any unset stances
foreach (var p in Players)