add limits to steal objectives (#22672)

* add ObjectiveLimit comp/system

* give ian and disk limits

* all

* up limit

---------

Co-authored-by: deltanedas <@deltanedas:kde.org>
This commit is contained in:
deltanedas
2023-12-25 01:20:55 +00:00
committed by GitHub
parent b20fcf5141
commit 2ca649edfa
3 changed files with 88 additions and 0 deletions

View File

@@ -0,0 +1,64 @@
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.AllObjectives)
{
if (Prototype(objective)?.ID == proto)
return true;
}
return false;
}
}