Files
crystall-punk-14/Content.Server/GameTicking/Rules/DeathMatchRuleSystem.cs

134 lines
5.2 KiB
C#
Raw Normal View History

using System.Linq;
using Content.Server.Clothing.Systems;
2023-04-25 20:23:14 -04:00
using Content.Server.GameTicking.Rules.Components;
using Content.Server.KillTracking;
using Content.Server.Mind;
using Content.Server.Points;
using Content.Server.RoundEnd;
using Content.Server.Station.Systems;
2024-11-19 21:16:49 -08:00
using Content.Shared.GameTicking;
using Content.Shared.GameTicking.Components;
using Content.Shared.Points;
using Content.Shared.Storage;
using Robust.Server.GameObjects;
2021-12-21 21:23:29 +01:00
using Robust.Server.Player;
using Robust.Shared.Utility;
2021-12-21 21:23:29 +01:00
namespace Content.Server.GameTicking.Rules;
/// <summary>
2023-04-25 20:23:14 -04:00
/// Manages <see cref="DeathMatchRuleComponent"/>
2021-12-21 21:23:29 +01:00
/// </summary>
2023-04-25 20:23:14 -04:00
public sealed class DeathMatchRuleSystem : GameRuleSystem<DeathMatchRuleComponent>
2021-12-21 21:23:29 +01:00
{
[Dependency] private readonly IPlayerManager _player = default!;
[Dependency] private readonly MindSystem _mind = default!;
[Dependency] private readonly OutfitSystem _outfitSystem = default!;
[Dependency] private readonly PointSystem _point = default!;
[Dependency] private readonly RespawnRuleSystem _respawn = default!;
[Dependency] private readonly RoundEndSystem _roundEnd = default!;
[Dependency] private readonly StationSpawningSystem _stationSpawning = default!;
[Dependency] private readonly TransformSystem _transform = default!;
2021-12-21 21:23:29 +01:00
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<PlayerBeforeSpawnEvent>(OnBeforeSpawn);
SubscribeLocalEvent<PlayerSpawnCompleteEvent>(OnSpawnComplete);
SubscribeLocalEvent<KillReportedEvent>(OnKillReported);
SubscribeLocalEvent<DeathMatchRuleComponent, PlayerPointChangedEvent>(OnPointChanged);
2023-04-25 20:23:14 -04:00
}
private void OnBeforeSpawn(PlayerBeforeSpawnEvent ev)
2023-04-25 20:23:14 -04:00
{
var query = EntityQueryEnumerator<DeathMatchRuleComponent, RespawnTrackerComponent, PointManagerComponent, GameRuleComponent>();
while (query.MoveNext(out var uid, out var dm, out var tracker, out var point, out var rule))
{
if (!GameTicker.IsGameRuleActive(uid, rule))
continue;
2021-12-21 21:23:29 +01:00
var newMind = _mind.CreateMind(ev.Player.UserId, ev.Profile.Name);
_mind.SetUserId(newMind, ev.Player.UserId);
2021-12-21 21:23:29 +01:00
var mobMaybe = _stationSpawning.SpawnPlayerCharacterOnStation(ev.Station, null, ev.Profile);
DebugTools.AssertNotNull(mobMaybe);
var mob = mobMaybe!.Value;
2023-04-25 20:23:14 -04:00
_mind.TransferTo(newMind, mob);
_outfitSystem.SetOutfit(mob, dm.Gear);
EnsureComp<KillTrackerComponent>(mob);
2024-06-02 23:28:38 -04:00
_respawn.AddToTracker(ev.Player.UserId, (uid, tracker));
2021-12-21 21:23:29 +01:00
_point.EnsurePlayer(ev.Player.UserId, uid, point);
2021-12-21 21:23:29 +01:00
ev.Handled = true;
break;
}
2021-12-21 21:23:29 +01:00
}
private void OnSpawnComplete(PlayerSpawnCompleteEvent ev)
2021-12-21 21:23:29 +01:00
{
EnsureComp<KillTrackerComponent>(ev.Mob);
var query = EntityQueryEnumerator<DeathMatchRuleComponent, RespawnTrackerComponent, GameRuleComponent>();
while (query.MoveNext(out var uid, out _, out var tracker, out var rule))
2021-12-21 21:23:29 +01:00
{
if (!GameTicker.IsGameRuleActive(uid, rule))
continue;
2024-06-02 23:28:38 -04:00
_respawn.AddToTracker((ev.Mob, null), (uid, tracker));
2021-12-21 21:23:29 +01:00
}
}
private void OnKillReported(ref KillReportedEvent ev)
2021-12-21 21:23:29 +01:00
{
var query = EntityQueryEnumerator<DeathMatchRuleComponent, PointManagerComponent, GameRuleComponent>();
while (query.MoveNext(out var uid, out var dm, out var point, out var rule))
2023-04-25 20:23:14 -04:00
{
if (!GameTicker.IsGameRuleActive(uid, rule))
2023-04-25 20:23:14 -04:00
continue;
2021-12-21 21:23:29 +01:00
// YOU SUICIDED OR GOT THROWN INTO LAVA!
// WHAT A GIANT FUCKING NERD! LAUGH NOW!
if (ev.Primary is not KillPlayerSource player)
{
_point.AdjustPointValue(ev.Entity, -1, uid, point);
continue;
}
2021-12-21 21:23:29 +01:00
_point.AdjustPointValue(player.PlayerId, 1, uid, point);
2021-12-21 21:23:29 +01:00
if (ev.Assist is KillPlayerSource assist && dm.Victor == null)
_point.AdjustPointValue(assist.PlayerId, 1, uid, point);
2021-12-21 21:23:29 +01:00
var spawns = EntitySpawnCollection.GetSpawns(dm.RewardSpawns).Cast<string?>().ToList();
EntityManager.SpawnEntities(_transform.GetMapCoordinates(ev.Entity), spawns);
2021-12-21 21:23:29 +01:00
}
}
2021-12-21 21:23:29 +01:00
private void OnPointChanged(EntityUid uid, DeathMatchRuleComponent component, ref PlayerPointChangedEvent args)
{
if (component.Victor != null)
2021-12-21 21:23:29 +01:00
return;
if (args.Points < component.KillCap)
2021-12-21 21:23:29 +01:00
return;
component.Victor = args.Player;
_roundEnd.EndRound(component.RestartDelay);
}
2021-12-21 21:23:29 +01:00
protected override void AppendRoundEndText(EntityUid uid, DeathMatchRuleComponent component, GameRuleComponent gameRule, ref RoundEndTextAppendEvent args)
{
if (!TryComp<PointManagerComponent>(uid, out var point))
return;
2021-12-21 21:23:29 +01:00
if (component.Victor != null && _player.TryGetPlayerData(component.Victor.Value, out var data))
{
args.AddLine(Loc.GetString("point-scoreboard-winner", ("player", data.UserName)));
args.AddLine("");
2021-12-21 21:23:29 +01:00
}
args.AddLine(Loc.GetString("point-scoreboard-header"));
args.AddLine(new FormattedMessage(point.Scoreboard).ToMarkup());
2021-12-21 21:23:29 +01:00
}
}