Files
tbd-station-14/Content.Server/Objectives/Systems/ObjectiveLimitSystem.cs
Pieter-Jan Briers 68ce53ae17 Random spontaneous cleanup PR (#25131)
* Use new Subs.CVar helper

Removes manual config OnValueChanged calls, removes need to remember to manually unsubscribe.

This both reduces boilerplate and fixes many issues where subscriptions weren't removed on entity system shutdown.

* Fix a bunch of warnings

* More warning fixes

* Use new DateTime serializer to get rid of ISerializationHooks in changelog code.

* Get rid of some more ISerializationHooks for enums

* And a little more

* Apply suggestions from code review

Co-authored-by: 0x6273 <0x40@keemail.me>

---------

Co-authored-by: 0x6273 <0x40@keemail.me>
2024-02-13 16:48:39 -05:00

65 lines
1.8 KiB
C#

using Content.Server.GameTicking.Rules.Components;
using Content.Server.Objectives.Components;
using Content.Shared.Mind;
using Content.Shared.Objectives.Components;
public sealed class ObjectiveLimitSystem : EntitySystem
{
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ObjectiveLimitComponent, RequirementCheckEvent>(OnCheck);
}
private void OnCheck(Entity<ObjectiveLimitComponent> ent, ref RequirementCheckEvent args)
{
if (args.Cancelled)
return;
if (Prototype(ent)?.ID is not {} proto)
{
Log.Error($"ObjectiveLimit used for non-prototyped objective {ent}");
return;
}
var remaining = ent.Comp.Limit;
// all traitor rules are considered
// maybe this would interfere with multistation stuff in the future but eh
foreach (var rule in EntityQuery<TraitorRuleComponent>())
{
foreach (var mindId in rule.TraitorMinds)
{
if (mindId == args.MindId || !HasObjective(mindId, proto))
continue;
remaining--;
// limit has been reached, prevent adding the objective
if (remaining == 0)
{
args.Cancelled = true;
return;
}
}
}
}
/// <summary>
/// Returns true if the mind has an objective of a certain prototype.
/// </summary>
public bool HasObjective(EntityUid mindId, string proto, MindComponent? mind = null)
{
if (!Resolve(mindId, ref mind))
return false;
foreach (var objective in mind.Objectives)
{
if (Prototype(objective)?.ID == proto)
return true;
}
return false;
}
}