Skip to content

Commit

Permalink
game rule tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirus59 committed Jan 11, 2025
1 parent 6288554 commit 6e6bf68
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 16 deletions.
55 changes: 52 additions & 3 deletions Content.Server/SS220/EventCapturePoint/EventCapturePointSystem.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// © SS220, An EULA/CLA with a hosting restriction, full text: https://raw.githubusercontent.com/SerbiaStrong-220/space-station-14/master/CLA.txt
using System.Numerics;
using Content.Server.SS220.FractWar;
using Content.Shared.DoAfter;
using Content.Shared.Interaction;
using Content.Shared.Item;
Expand All @@ -24,6 +25,11 @@ public sealed class EventCapturePointSystem : EntitySystem
[Dependency] private readonly AppearanceSystem _appearance = default!;
[Dependency] private readonly SharedContainerSystem _container = default!;
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly FractWarRuleSystem _fractWarRule = default!;

private const float RefreshWPRate = 60f;

private TimeSpan _nextRefreshWP = TimeSpan.Zero;

public override void Initialize()
{
Expand All @@ -42,16 +48,26 @@ public override void Update(float frameTime)
{
base.Update(frameTime);

var gameRule = _fractWarRule.GetActiveGameRule();
if (gameRule is null)
return;

var query = EntityQueryEnumerator<EventCapturePointComponent>();
while (query.MoveNext(out var uid, out var component))
while (query.MoveNext(out _, out var component))
{
if (component.FlagEntity is not { } flagUid ||
!TryComp<EventCapturePointFlagComponent>(flagUid, out var flagComp) ||
flagComp.Fraction is not { } flagFraction)
continue;

if (!component.PointRetentionTime.TryAdd(flagFraction, TimeSpan.Zero))
component.PointRetentionTime[flagFraction] += _timing.TickPeriod;
if (!component.PointRetentionTime.TryAdd(flagFraction, 0))
component.PointRetentionTime[flagFraction] += frameTime;
}

if (_timing.CurTime >= _nextRefreshWP)
{
RefreshWP(gameRule);
_nextRefreshWP = _timing.CurTime + TimeSpan.FromSeconds(RefreshWPRate);
}
}

Expand Down Expand Up @@ -91,6 +107,8 @@ private void OnFlagRemoved(Entity<EventCapturePointComponent> entity, ref FlagRe

private void OnPointShutdown(Entity<EventCapturePointComponent> entity, ref ComponentShutdown args)
{
RefreshPointWP(entity.Comp);

if (entity.Comp.FlagEntity.HasValue &&
entity.Comp.FlagEntity.Value.Valid &&
EntityManager.EntityExists(entity.Comp.FlagEntity.Value))
Expand Down Expand Up @@ -197,4 +215,35 @@ public void AddOrRemoveFlag(Entity<EventCapturePointComponent> entity, EntityUid
else
AddFlag(entity, user, newFlag);
}

public void RefreshWP(FractWarRuleComponent? gameRule = null)
{
gameRule ??= _fractWarRule.GetActiveGameRule();
if (gameRule is null)
return;

var query = EntityQueryEnumerator<EventCapturePointComponent>();
while (query.MoveNext(out _, out var comp))
{
RefreshPointWP(comp, gameRule);
}
}

public void RefreshPointWP(EventCapturePointComponent comp, FractWarRuleComponent? gameRule = null)
{
gameRule ??= _fractWarRule.GetActiveGameRule();

if (gameRule is null)
return;

foreach (var (fraction, retTime) in comp.PointRetentionTime)
{
var wp = retTime / comp.RetentionTimeForWP;

if (!gameRule.FractionsWP.TryAdd(fraction, wp))
gameRule.FractionsWP[fraction] += wp;

comp.PointRetentionTime[fraction] = 0;
}
}
}
2 changes: 2 additions & 0 deletions Content.Server/SS220/FractWar/FractWarRuleComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ namespace Content.Server.SS220.FractWar;
[RegisterComponent]
public sealed partial class FractWarRuleComponent : Component
{
[ViewVariables]
public Dictionary<string, float> FractionsWP = [];
}
30 changes: 18 additions & 12 deletions Content.Server/SS220/FractWar/FractWarRuleSystem.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
using Content.Server.GameTicking;
using Content.Server.GameTicking.Rules;
using Content.Server.SS220.EventCapturePoint;
using Content.Shared.GameTicking.Components;
using Content.Shared.SS220.EventCapturePoint;
using System.Linq;

namespace Content.Server.SS220.FractWar;

public sealed partial class FractWarRuleSystem : GameRuleSystem<FractWarRuleComponent>
{
[Dependency] private readonly EventCapturePointSystem _eventCapturePoint = default!;

protected override void AppendRoundEndText(EntityUid uid, FractWarRuleComponent component, GameRuleComponent gameRule, ref RoundEndTextAppendEvent args)
{
base.AppendRoundEndText(uid, component, gameRule, ref args);

args.AddLine(Loc.GetString("fractwar-round-end-score-points"));

Dictionary<string, float> fractionsWinPoints = new();
var capturePoints = EntityQueryEnumerator<EventCapturePointComponent>();
while (capturePoints.MoveNext(out _, out var capturePointComponent))
{
foreach (var (fraction, retentionTime) in capturePointComponent.PointRetentionTime)
{
var winPoints = (float)(retentionTime.TotalSeconds / capturePointComponent.RetentionTimeForWP.TotalSeconds) * capturePointComponent.WinPoints;
if (!fractionsWinPoints.TryAdd(fraction, winPoints))
fractionsWinPoints[fraction] += winPoints;
}
}
_eventCapturePoint.RefreshWP(component);
var fractionsWinPoints = component.FractionsWP;

if (fractionsWinPoints.Count <= 0)
return;
Expand All @@ -41,4 +34,17 @@ protected override void AppendRoundEndText(EntityUid uid, FractWarRuleComponent
args.AddLine("");
args.AddLine(Loc.GetString("fractwar-round-end-winner", ("fraction", Loc.GetString(fractionsWinPoints.First().Key))));
}

public FractWarRuleComponent? GetActiveGameRule()
{
FractWarRuleComponent? comp = null;
var query = QueryActiveRules();
while (query.MoveNext(out _, out var fractComp, out _))
{
comp = fractComp;
break;
}

return comp;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public sealed partial class EventCapturePointComponent : Component
public float RetentionTimeForWP = 60;

[ViewVariables]
public Dictionary<string, TimeSpan> PointRetentionTime = new();
public Dictionary<string, float> PointRetentionTime = new();
}

[Serializable, NetSerializable]
Expand Down
4 changes: 4 additions & 0 deletions Resources/Prototypes/SS220/GameRules/roundstart.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
parent: BaseGameRule
id: FractWar
components:
- type: GameRule
delay:
min: 0
max: 0
- type: LoadMapRule
mapPath: /Maps/SS220/EventShuttles/FractWarEbent/NT.yml
- type: FractWarRule

0 comments on commit 6e6bf68

Please sign in to comment.