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

Randomized game mode feature #519

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
6 changes: 5 additions & 1 deletion DXMainClient/DXGUI/Multiplayer/GameLobby/GameLobbyBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@
ddGameModeMapFilter.SelectedIndex = gameModeMapFilterIndex;
}

protected void AddSideToDropDown(XNADropDown dd, string name, string? uiName = null, Texture2D? texture = null)

Check warning on line 752 in DXMainClient/DXGUI/Multiplayer/GameLobby/GameLobbyBase.cs

View workflow job for this annotation

GitHub Actions / build-clients (Ares)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 752 in DXMainClient/DXGUI/Multiplayer/GameLobby/GameLobbyBase.cs

View workflow job for this annotation

GitHub Actions / build-clients (Ares)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 752 in DXMainClient/DXGUI/Multiplayer/GameLobby/GameLobbyBase.cs

View workflow job for this annotation

GitHub Actions / build-clients (TS)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 752 in DXMainClient/DXGUI/Multiplayer/GameLobby/GameLobbyBase.cs

View workflow job for this annotation

GitHub Actions / build-clients (TS)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 752 in DXMainClient/DXGUI/Multiplayer/GameLobby/GameLobbyBase.cs

View workflow job for this annotation

GitHub Actions / build-clients (YR)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 752 in DXMainClient/DXGUI/Multiplayer/GameLobby/GameLobbyBase.cs

View workflow job for this annotation

GitHub Actions / build-clients (YR)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
XNADropDownItem item = new()
{
Expand Down Expand Up @@ -1557,7 +1557,11 @@

IniFile globalCodeIni = new IniFile(SafePath.CombineFilePath(ProgramConstants.GamePath, "INI", "Map Code", "GlobalCode.ini"));

MapCodeHelper.ApplyMapCode(mapIni, GameMode.GetMapRulesIniFile());
foreach (IniFile iniFile in GameMode.GetMapRulesIniFiles(RandomSeed))
{
MapCodeHelper.ApplyMapCode(mapIni, iniFile);
}

MapCodeHelper.ApplyMapCode(mapIni, globalCodeIni);

if (isMultiplayer)
Expand Down
24 changes: 22 additions & 2 deletions DXMainClient/Domain/Multiplayer/GameMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Rampastring.Tools;
using System;
using System.Collections.Generic;
using System.Linq;

namespace DTAClient.Domain.Multiplayer
{
Expand Down Expand Up @@ -66,6 +67,8 @@ public GameMode(string name)
public int MinPlayersOverride { get; private set; } = -1;

private string mapCodeININame;
private List<string> randomizedMapCodeININames;
private int randomizedMapCodesCount;

private string forcedOptionsSection;

Expand All @@ -92,6 +95,8 @@ public void Initialize()
MinPlayersOverride = forcedOptionsIni.GetIntValue(Name, "MinPlayersOverride", -1);
forcedOptionsSection = forcedOptionsIni.GetStringValue(Name, "ForcedOptions", string.Empty);
mapCodeININame = forcedOptionsIni.GetStringValue(Name, "MapCodeININame", Name + ".ini");
randomizedMapCodeININames = forcedOptionsIni.GetStringValue(Name, "RandomizedMapCodeININames", string.Empty).Split(',', StringSplitOptions.RemoveEmptyEntries).ToList();
randomizedMapCodesCount = forcedOptionsIni.GetIntValue(Name, "RandomizedMapCodesCount", 1);

string[] disallowedSides = forcedOptionsIni
.GetStringValue(Name, "DisallowedPlayerSides", string.Empty)
Expand Down Expand Up @@ -153,9 +158,24 @@ public void ApplySpawnIniCode(IniFile spawnIni)
spawnIni.SetStringValue("Settings", key.Key, key.Value);
}

public IniFile GetMapRulesIniFile()
public List<IniFile> GetMapRulesIniFiles(int randomSeed)
{
return new IniFile(SafePath.CombineFilePath(ProgramConstants.GamePath, BASE_INI_PATH, mapCodeININame));
var mapRules = new List<IniFile>() { new IniFile(SafePath.CombineFilePath(ProgramConstants.GamePath, BASE_INI_PATH, mapCodeININame)) };
if (randomizedMapCodeININames.Count == 0)
return mapRules;

Random random = new Random(randomSeed);
Dictionary<string, int> randomOrder = new();
foreach (string name in randomizedMapCodeININames)
{
randomOrder[name] = random.Next();
}

mapRules.AddRange(
from iniName in randomizedMapCodeININames.OrderBy(x => randomOrder[x]).Take(randomizedMapCodesCount)
select new IniFile(SafePath.CombineFilePath(ProgramConstants.GamePath, BASE_INI_PATH, iniName)));

return mapRules;
}

protected bool Equals(GameMode other) => string.Equals(Name, other?.Name, StringComparison.InvariantCultureIgnoreCase);
Expand Down
Loading