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

Implement forced team start mappings #559

Open
wants to merge 1 commit 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
10 changes: 5 additions & 5 deletions DXMainClient/DXGUI/Multiplayer/GameLobby/GameLobbyBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1312,11 +1312,11 @@ private PlayerHouseInfo[] WriteSpawnIni()
pInfo.TeamId = 1;
}

var teamStartMappings = new List<TeamStartMapping>(0);
if (PlayerExtraOptionsPanel != null)
{
List<TeamStartMapping> teamStartMappings = [];
if (GameMode.ForceTeamStartMappings)
teamStartMappings = GameMode.TeamStartMappings;
else if (PlayerExtraOptionsPanel != null)
teamStartMappings = PlayerExtraOptionsPanel.GetTeamStartMappings();
}

PlayerHouseInfo[] houseInfos = Randomize(teamStartMappings);

Expand Down Expand Up @@ -2205,7 +2205,7 @@ protected virtual void ChangeMap(GameModeMap gameModeMap)

disableGameOptionUpdateBroadcast = false;

PlayerExtraOptionsPanel?.UpdateForMap(Map);
PlayerExtraOptionsPanel?.UpdateForMap(GameModeMap);
}

private void ApplyForcedCheckBoxOptions(List<GameLobbyCheckBox> optionList,
Expand Down
71 changes: 62 additions & 9 deletions DXMainClient/DXGUI/Multiplayer/PlayerExtraOptionsPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class PlayerExtraOptionsPanel : XNAWindow
private const int teamMappingPanelWidth = 50;
private const int teamMappingPanelHeight = 22;
private readonly string customPresetName = "Custom".L10N("Client:Main:CustomPresetName");
private readonly string gameModeForcedPresetName = "Mode Preset".L10N("Client:Main:GameModeForcedPresetName");

private XNAClientCheckBox chkBoxForceRandomSides;
private XNAClientCheckBox chkBoxForceRandomTeams;
Expand All @@ -32,7 +33,9 @@ public class PlayerExtraOptionsPanel : XNAWindow
public EventHandler OptionsChanged;
public EventHandler OnClose;

private Map _map;
private GameModeMap _gameModeMap;
private Map _map => _gameModeMap?.Map;
private GameMode _gameMode => _gameModeMap?.GameMode;

public PlayerExtraOptionsPanel(WindowManager windowManager) : base(windowManager)
{
Expand All @@ -57,9 +60,11 @@ private void Mapping_Changed(object sender, EventArgs e)

private void ChkBoxUseTeamStartMappings_Changed(object sender, EventArgs e)
{
bool gameModeForceTeamStartMappings = _gameMode?.ForceTeamStartMappings ?? false;

RefreshTeamStartMappingsPanel();
chkBoxForceRandomTeams.Checked = chkBoxForceRandomTeams.Checked || chkBoxUseTeamStartMappings.Checked;
chkBoxForceRandomTeams.AllowChecking = !chkBoxUseTeamStartMappings.Checked;
chkBoxForceRandomTeams.AllowChecking = !chkBoxUseTeamStartMappings.Checked && !gameModeForceTeamStartMappings;

// chkBoxForceRandomStarts.Checked = chkBoxForceRandomStarts.Checked || chkBoxUseTeamStartMappings.Checked;
// chkBoxForceRandomStarts.AllowChecking = !chkBoxUseTeamStartMappings.Checked;
Expand Down Expand Up @@ -106,6 +111,8 @@ private void ClearTeamStartMappingSelections()

private void RefreshTeamStartMappingPanels()
{
bool gameModeForceTeamStartMappings = _gameMode?.ForceTeamStartMappings ?? false;

ClearTeamStartMappingSelections();
var teamStartMappingPanels = teamStartMappingsPanel.GetTeamStartMappingPanels();
for (int i = 0; i < teamStartMappingPanels.Count; i++)
Expand All @@ -115,9 +122,32 @@ private void RefreshTeamStartMappingPanels()
if (!IsUseTeamStartMappings())
continue;

teamStartMappingPanel.EnableControls(_isHost && chkBoxUseTeamStartMappings.Checked && i < _map?.MaxPlayers);
RefreshTeamStartMappingPresets(_map?.TeamStartMappingPresets);
teamStartMappingPanel.EnableControls(_isHost && chkBoxUseTeamStartMappings.Checked && !gameModeForceTeamStartMappings && i < _map?.MaxPlayers);
}

// Note: originally in commit 4cf9b33b03795eb51e445b4907927cb69d82c4bc, RefreshTeamStartMappingPresets() was called in each for loop iteration
// This is changed to call it only once after the loop.
RefreshTeamStartMappingPresets();
}

private void RefreshTeamStartMappingPresets()
{
if (_gameMode?.ForceTeamStartMappings ?? false)
RefreshTeamStartMappingPresets_Forced(_gameMode.TeamStartMappings);
else
RefreshTeamStartMappingPresets(_map?.TeamStartMappingPresets);
}

private void RefreshTeamStartMappingPresets_Forced(List<TeamStartMapping> teamStartMappings)
{
ddTeamStartMappingPreset.Items.Clear();
ddTeamStartMappingPreset.AddItem(new XNADropDownItem
{
Text = gameModeForcedPresetName,
Tag = teamStartMappings,
});
ddTeamStartMappingPreset.SelectedIndex = 0;
DdTeamMappingPreset_SelectedIndexChanged();
}

private void RefreshTeamStartMappingPresets(List<TeamStartMappingPreset> teamStartMappingPresets)
Expand All @@ -140,7 +170,8 @@ private void RefreshTeamStartMappingPresets(List<TeamStartMappingPreset> teamSta
ddTeamStartMappingPreset.SelectedIndex = 1;
}

private void DdTeamMappingPreset_SelectedIndexChanged(object sender, EventArgs e)
private void DdTeamMappingPreset_SelectedIndexChanged(object sender, EventArgs e) => DdTeamMappingPreset_SelectedIndexChanged();
private void DdTeamMappingPreset_SelectedIndexChanged()
{
var selectedItem = ddTeamStartMappingPreset.SelectedItem;
if (selectedItem?.Text == customPresetName)
Expand All @@ -153,7 +184,12 @@ private void DdTeamMappingPreset_SelectedIndexChanged(object sender, EventArgs e
ignoreMappingChanges = false;
}

private void RefreshPresetDropdown() => ddTeamStartMappingPreset.AllowDropDown = _isHost && chkBoxUseTeamStartMappings.Checked;
private void RefreshPresetDropdown()
{
bool gameModeForceTeamStartMappings = _gameMode?.ForceTeamStartMappings ?? false;

ddTeamStartMappingPreset.AllowDropDown = _isHost && chkBoxUseTeamStartMappings.Checked && !gameModeForceTeamStartMappings;
}

public override void Initialize()
{
Expand Down Expand Up @@ -257,12 +293,29 @@ private void BtnHelp_LeftClick(object sender, EventArgs args)
);
}

public void UpdateForMap(Map map)
public void UpdateForMap(GameModeMap gameModeMap)
{
if (_map == map)
if (_gameModeMap == gameModeMap)
return;

_map = map;
var lastGameModeMap = _gameModeMap;
_gameModeMap = gameModeMap;

if (lastGameModeMap?.GameMode?.ForceTeamStartMappings ?? false)
{
// Clear player extra options if the last game mode forced them
SetPlayerExtraOptions(new PlayerExtraOptions());
}

if (_gameMode?.ForceTeamStartMappings ?? false)
{
chkBoxUseTeamStartMappings.Checked = true;
chkBoxUseTeamStartMappings.AllowChecking = false;
}
else
{
chkBoxUseTeamStartMappings.AllowChecking = true;
}

RefreshTeamStartMappingPanels();
}
Expand Down
20 changes: 17 additions & 3 deletions DXMainClient/Domain/Multiplayer/GameMode.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using ClientCore;
using System;
using System.Collections.Generic;

using ClientCore;
using ClientCore.Extensions;

using Rampastring.Tools;
using System;
using System.Collections.Generic;

namespace DTAClient.Domain.Multiplayer
{
Expand Down Expand Up @@ -55,6 +57,16 @@ public GameMode(string name)
/// </summary>
public bool ForceNoTeams { get; private set; }

/// <summary>
/// If set, given start mapping 'TeamStartMappings' is forced on this game mode.
/// </summary>
public bool ForceTeamStartMappings { get; private set; }

/// <summary>
/// If 'ForceTeamStartMappings' is set, this variable indicates the start mapping being forced on this game mode.
/// </summary>
public List<TeamStartMapping> TeamStartMappings { get; private set; } = [];

/// <summary>
/// List of side indices players cannot select in this game mode.
/// </summary>
Expand Down Expand Up @@ -88,6 +100,8 @@ public void Initialize()
MultiplayerOnly = forcedOptionsIni.GetBooleanValue(Name, "MultiplayerOnly", false);
HumanPlayersOnly = forcedOptionsIni.GetBooleanValue(Name, "HumanPlayersOnly", false);
ForceRandomStartLocations = forcedOptionsIni.GetBooleanValue(Name, "ForceRandomStartLocations", false);
ForceTeamStartMappings = forcedOptionsIni.GetBooleanValue(Name, "ForceTeamStartMappings", false);
TeamStartMappings = TeamStartMapping.FromListString(forcedOptionsIni.GetStringValue(Name, "TeamStartMappings", string.Empty));
ForceNoTeams = forcedOptionsIni.GetBooleanValue(Name, "ForceNoTeams", false);
MinPlayersOverride = forcedOptionsIni.GetIntValue(Name, "MinPlayersOverride", -1);
forcedOptionsSection = forcedOptionsIni.GetStringValue(Name, "ForcedOptions", string.Empty);
Expand Down
Loading