Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow disabling specific sides for human or AI players only #542

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 59 additions & 22 deletions DXMainClient/DXGUI/Multiplayer/GameLobby/GameLobbyBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1040,11 +1040,12 @@ protected GameModeMapFilter GetDefaultGameModeMapFilter()

/// <summary>
/// Applies disallowed side indexes to the side option drop-downs
/// and player options.
/// and player options for human or computer players.
/// </summary>
protected void CheckDisallowedSides()
protected void CheckDisallowedSidesForGroup(bool forHumanPlayers)
{
var disallowedSideArray = GetDisallowedSides();
var disallowedSideArray = GetDisallowedSidesForGroup(forHumanPlayers);
var playerInfos = forHumanPlayers ? Players : AIPlayers;
int defaultSide = 0;
int allowedSideCount = disallowedSideArray.Count(b => b == false);

Expand All @@ -1058,28 +1059,25 @@ protected void CheckDisallowedSides()
defaultSide = i + RandomSelectorCount;
}

foreach (XNADropDown dd in ddPlayerSides)
foreach (PlayerInfo pInfo in playerInfos)
{
//dd.Items[0].Selectable = false;
var dd = ddPlayerSides[pInfo.Index];
for (int i = 0; i < RandomSelectorCount; i++)
dd.Items[i].Selectable = false;
}
}
else
{
foreach (XNADropDown dd in ddPlayerSides)
foreach (PlayerInfo pInfo in playerInfos)
{
//dd.Items[0].Selectable = true;
var dd = ddPlayerSides[pInfo.Index];
for (int i = 0; i < RandomSelectorCount; i++)
dd.Items[i].Selectable = true;
}
}

var concatPlayerList = Players.Concat(AIPlayers);

// Disable custom random groups if all or all except one of included sides are unavailable.
int c = 0;
var playerInfos = concatPlayerList.ToList();
foreach (int[] randomSides in RandomSelectors)
{
int disableCount = 0;
Expand All @@ -1092,11 +1090,11 @@ protected void CheckDisallowedSides()

bool disabled = disableCount >= randomSides.Length - 1;

foreach (XNADropDown dd in ddPlayerSides)
dd.Items[1 + c].Selectable = !disabled;

foreach (PlayerInfo pInfo in playerInfos)
{
var dd = ddPlayerSides[pInfo.Index];
dd.Items[1 + c].Selectable = !disabled;

if (pInfo.SideId == 1 + c && disabled)
pInfo.SideId = defaultSide;
}
Expand All @@ -1112,21 +1110,24 @@ protected void CheckDisallowedSides()

if (disabled)
{
foreach (XNADropDown dd in ddPlayerSides)
dd.Items[i + RandomSelectorCount].Selectable = false;

// Change the sides of players that use the disabled
// side to the default side
foreach (PlayerInfo pInfo in playerInfos)
{
var dd = ddPlayerSides[pInfo.Index];
dd.Items[i + RandomSelectorCount].Selectable = false;

if (pInfo.SideId == i + RandomSelectorCount)
pInfo.SideId = defaultSide;
}
}
else
{
foreach (XNADropDown dd in ddPlayerSides)
foreach (PlayerInfo pInfo in playerInfos)
{
var dd = ddPlayerSides[pInfo.Index];
dd.Items[i + RandomSelectorCount].Selectable = true;
}
}
}

Expand All @@ -1150,22 +1151,51 @@ protected void CheckDisallowedSides()
pInfo.SideId = defaultSide;
}

foreach (XNADropDown dd in ddPlayerSides)
foreach (PlayerInfo pInfo in playerInfos)
{
var dd = ddPlayerSides[pInfo.Index];
if (dd.Items.Count > GetSpectatorSideIndex())
dd.Items[SideCount + RandomSelectorCount].Selectable = false;
}
}
else
{
foreach (XNADropDown dd in ddPlayerSides)
foreach (PlayerInfo pInfo in playerInfos)
{
var dd = ddPlayerSides[pInfo.Index];
if (dd.Items.Count > SideCount + RandomSelectorCount)
dd.Items[SideCount + RandomSelectorCount].Selectable = true;
}
}
}

/// <summary>
/// Applies disallowed side indexes to the side option drop-downs
/// and player options.
/// </summary>
protected void CheckDisallowedSides()
{
CheckDisallowedSidesForGroup(forHumanPlayers:false);
CheckDisallowedSidesForGroup(forHumanPlayers:true);
}

/// <summary>
/// Gets a list of side indexes that are disallowed for human or computer players.
/// </summary>
/// <returns>A list of disallowed side indexes.</returns>
protected bool[] GetDisallowedSidesForGroup(bool forHumanPlayers)
{
var returnValue = GetDisallowedSides();
var sides = forHumanPlayers ? GameMode?.DisallowedHumanPlayerSides : GameMode?.DisallowedComputerPlayerSides;
if (sides != null)
{
foreach (int i in sides)
returnValue[i] = true;
}

return returnValue;
}

/// <summary>
/// Gets a list of side indexes that are disallowed.
/// </summary>
Expand Down Expand Up @@ -1264,13 +1294,20 @@ protected virtual PlayerHouseInfo[] Randomize(List<TeamStartMapping> teamStartMa
{
PlayerInfo pInfo;
PlayerHouseInfo pHouseInfo = houseInfos[i];
bool[] disallowedSides;

if (i < Players.Count)
{
pInfo = Players[i];
disallowedSides = GetDisallowedSidesForGroup(forHumanPlayers:true);
}
else
{
pInfo = AIPlayers[i - Players.Count];
disallowedSides = GetDisallowedSidesForGroup(forHumanPlayers:false);
}

pHouseInfo.RandomizeSide(pInfo, SideCount, random, GetDisallowedSides(), RandomSelectors, RandomSelectorCount);
pHouseInfo.RandomizeSide(pInfo, SideCount, random, disallowedSides, RandomSelectors, RandomSelectorCount);

pHouseInfo.RandomizeColor(pInfo, freeColors, MPColors, random);
pHouseInfo.RandomizeStart(pInfo, random, freeStartingLocations, takenStartingLocations, teamStartMappings.Any());
Expand Down Expand Up @@ -2022,6 +2059,8 @@ protected virtual void CopyPlayerDataToUI()
MapPreviewBox.UpdateStartingLocationTexts();
UpdateMapPreviewBoxEnabledStatus();

CheckDisallowedSides();

PlayerUpdatingInProgress = false;
}

Expand Down Expand Up @@ -2159,8 +2198,6 @@ protected virtual void ChangeMap(GameModeMap gameModeMap)
pInfo.TeamId = 0;
}

CheckDisallowedSides();


if (Map.CoopInfo != null)
{
Expand Down
6 changes: 6 additions & 0 deletions DXMainClient/DXGUI/Multiplayer/GameLobby/LANGameLobby.cs
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,11 @@ private void HandlePlayerOptionsRequest(string sender, string data)
if (color < 0 || color > MPColors.Count)
return;

var disallowedSides = GetDisallowedSides();

if (side > 0 && side <= SideCount && disallowedSides[side - 1])
return;

if (Map.CoopInfo != null)
{
if (Map.CoopInfo.DisallowedPlayerSides.Contains(side - 1) || side == SideCount + RandomSelectorCount)
Expand Down Expand Up @@ -942,6 +947,7 @@ private void HandlePlayerOptionsBroadcast(string data)
}

CopyPlayerDataToUI();

localPlayer = FindLocalPlayer();
if (localPlayer != null && oldSideId != localPlayer.SideId)
UpdateDiscordPresence();
Expand Down
2 changes: 0 additions & 2 deletions DXMainClient/DXGUI/Multiplayer/GameLobby/SkirmishLobby.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ public override void Initialize()

LoadSettings();

CheckDisallowedSides();

CopyPlayerDataToUI();

ProgramConstants.PlayerNameChanged += ProgramConstants_PlayerNameChanged;
Expand Down
25 changes: 25 additions & 0 deletions DXMainClient/Domain/Multiplayer/GameMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ public GameMode(string name)
/// </summary>
public List<int> DisallowedPlayerSides = new List<int>();

/// <summary>
/// List of side indices human players cannot select in this game mode.
/// </summary>
public List<int> DisallowedHumanPlayerSides = new List<int>();

/// <summary>
/// List of side indices computer players cannot select in this game mode.
/// </summary>
public List<int> DisallowedComputerPlayerSides = new List<int>();


/// </summary>
/// Override for minimum amount of players needed to play any map in this game mode.
/// </summary>
Expand Down Expand Up @@ -100,6 +111,20 @@ public void Initialize()
foreach (string sideIndex in disallowedSides)
DisallowedPlayerSides.Add(int.Parse(sideIndex));

disallowedSides = forcedOptionsIni
.GetStringValue(Name, "DisallowedHumanPlayerSides", string.Empty)
.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

foreach (string sideIndex in disallowedSides)
DisallowedHumanPlayerSides.Add(int.Parse(sideIndex));

disallowedSides = forcedOptionsIni
.GetStringValue(Name, "DisallowedComputerPlayerSides", string.Empty)
.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

foreach (string sideIndex in disallowedSides)
DisallowedComputerPlayerSides.Add(int.Parse(sideIndex));

ParseForcedOptions(forcedOptionsIni);

ParseSpawnIniOptions(forcedOptionsIni);
Expand Down
Loading