Skip to content

Commit

Permalink
max team nades
Browse files Browse the repository at this point in the history
  • Loading branch information
yonilerner committed Apr 1, 2024
1 parent 3aa5f19 commit aeb907a
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 3 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,18 @@ The valid keys for nades on `CounterTerrorist` are:

If you mix up `Incendiary` and `Molotov`, the plugin will fix it for you.

- `MaxTeamNades` - This config works similarly to `MaxNades`, except it affects the max number of nades an entire team
can have. The structure is the same as `MaxNades` except that after the map and team keys, it maps a round type to a
max nade setting. The possible max nade settings are:
- `One`, `Two`, ... until `Ten`
- `AveragePointFivePerPlayer` (rounds up)
- `AverageOnePerPlayer` (rounds up)
- `AverageOnePointFivePerPlayer` (rounds up)
- `AverageTwoPerPlayer` (rounds up)
- `None`

*NOTE: There is a bug right now where the plugin will not always give the maximum number of nades, even if players have room for it*.

#### Other Configuration

- `EnableNextRoundTypeVoting`: Whether to allow voting for the next round type via `!nextround`. `false` by default.
Expand Down
31 changes: 31 additions & 0 deletions RetakesAllocatorCore/Config/Configs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,37 @@ public Dictionary<
}
};

public Dictionary<
string,
Dictionary<
CsTeam,
Dictionary<RoundType, MaxTeamNadesSetting>
>
> MaxTeamNades { get; set; } = new()
{
{
NadeHelpers.GlobalSettingName, new()
{
{
CsTeam.Terrorist, new()
{
{RoundType.Pistol, MaxTeamNadesSetting.AverageOnePerPlayer},
{RoundType.HalfBuy, MaxTeamNadesSetting.AverageOnePointFivePerPlayer},
{RoundType.FullBuy, MaxTeamNadesSetting.AverageOnePointFivePerPlayer},
}
},
{
CsTeam.CounterTerrorist, new()
{
{RoundType.Pistol, MaxTeamNadesSetting.AverageOnePerPlayer},
{RoundType.HalfBuy, MaxTeamNadesSetting.AverageOnePointFivePerPlayer},
{RoundType.FullBuy, MaxTeamNadesSetting.AverageOnePointFivePerPlayer},
}
},
}
}
};

public RoundTypeSelectionOption RoundTypeSelection { get; set; } = RoundTypeSelectionOption.Random;

public Dictionary<RoundType, int> RoundTypePercentages { get; set; } = new()
Expand Down
79 changes: 76 additions & 3 deletions RetakesAllocatorCore/NadeHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,25 @@

namespace RetakesAllocatorCore;

public enum MaxTeamNadesSetting
{
None,
One,
Two,
Three,
Four,
Five,
Six,
Seven,
Eight,
Nine,
Ten,
AveragePointFivePerPlayer,
AverageOnePerPlayer,
AverageOnePointFivePerPlayer,
AverageTwoPerPlayer,
}

public class NadeHelpers
{
public static string GlobalSettingName = "GLOBAL";
Expand All @@ -12,7 +31,38 @@ public static Stack<CsItem> GetUtilForTeam(string? map, RoundType roundType, CsT
{
map ??= GlobalSettingName;

var maxTotalNades = (int) Math.Ceiling(numPlayers * (roundType == RoundType.Pistol ? 1 : 1.5));
var maxNadesSetting = GetMaxTeamNades(map, team, roundType);
if (maxNadesSetting == MaxTeamNadesSetting.None)
{
return new();
}

var multiplier = maxNadesSetting switch
{
MaxTeamNadesSetting.AveragePointFivePerPlayer => 0.5,
MaxTeamNadesSetting.AverageOnePerPlayer => 1,
MaxTeamNadesSetting.AverageOnePointFivePerPlayer => 1.5,
MaxTeamNadesSetting.AverageTwoPerPlayer => 2,
_ => 0,
};

var maxTotalNades = maxNadesSetting switch
{
MaxTeamNadesSetting.One => 1,
MaxTeamNadesSetting.Two => 2,
MaxTeamNadesSetting.Three => 3,
MaxTeamNadesSetting.Four => 4,
MaxTeamNadesSetting.Five => 5,
MaxTeamNadesSetting.Six => 6,
MaxTeamNadesSetting.Seven => 7,
MaxTeamNadesSetting.Eight => 8,
MaxTeamNadesSetting.Nine => 9,
MaxTeamNadesSetting.Ten => 10,
_ => (int) Math.Ceiling(numPlayers * multiplier)
};

Log.Debug($"Nade setting: {maxNadesSetting}. Total: {maxTotalNades}");

var molly = team == CsTeam.Terrorist ? CsItem.Molotov : CsItem.Incendiary;
var nadeDistribution = new List<CsItem>
{
Expand Down Expand Up @@ -54,6 +104,27 @@ public static Stack<CsItem> GetUtilForTeam(string? map, RoundType roundType, CsT
return nades;
}

private static MaxTeamNadesSetting GetMaxTeamNades(string map, CsTeam team, RoundType roundType)
{
if (Configs.GetConfigData().MaxTeamNades.TryGetValue(map, out var mapMaxNades))
{
if (mapMaxNades.TryGetValue(team, out var teamMaxNades))
{
if (teamMaxNades.TryGetValue(roundType, out var maxNadesSetting))
{
return maxNadesSetting;
}
}
}

if (map == GlobalSettingName)
{
return MaxTeamNadesSetting.None;
}

return GetMaxTeamNades(GlobalSettingName, team, roundType);
}

private static int GetMaxNades(string map, CsTeam team, CsItem nade)
{
if (Configs.GetConfigData().MaxNades.TryGetValue(map, out var mapNades))
Expand Down Expand Up @@ -101,6 +172,7 @@ private static bool PlayerReachedMaxNades(ICollection<CsItem> nades)
{
return true;
}

allowancePerType[nade]--;
}

Expand All @@ -122,7 +194,7 @@ Dictionary<T, ICollection<CsItem>> nadesByPlayer
while (teamNades.Count != 0 && teamPlayersShuffled.Count != 0)
{
var player = teamPlayersShuffled[playerI];

if (!nadesByPlayer.TryGetValue(player, out var nadesForPlayer))
{
nadesForPlayer = new List<CsItem>();
Expand All @@ -141,8 +213,9 @@ Dictionary<T, ICollection<CsItem>> nadesByPlayer
{
break;
}

nadesForPlayer.Add(nextNade);

playerI++;
if (playerI >= teamPlayersShuffled.Count)
{
Expand Down

0 comments on commit aeb907a

Please sign in to comment.