move ninja objectives code into generic antag system (#20186)

Co-authored-by: deltanedas <@deltanedas:kde.org>
This commit is contained in:
deltanedas
2023-09-21 15:33:18 +01:00
committed by GitHub
parent 8629a3ab9e
commit 37c5020a71
11 changed files with 205 additions and 120 deletions

View File

@@ -0,0 +1,30 @@
using Content.Server.GameTicking.Rules.Components;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
namespace Content.Server.GenericAntag;
/// <summary>
/// Added to a mob to make it a generic antagonist where all its objectives are fixed.
/// This is unlike say traitor where it gets objectives picked randomly using difficulty.
/// </summary>
/// <remarks>
/// A GenericAntag is not necessarily an antagonist, that depends on the roles you do or do not add after.
/// </remarks>
[RegisterComponent, Access(typeof(GenericAntagSystem))]
public sealed partial class GenericAntagComponent : Component
{
/// <summary>
/// Gamerule to start when a mind is added.
/// This must have <see cref="GenericAntagRuleComponent"/> or it will not work.
/// </summary>
[DataField(required: true), ViewVariables(VVAccess.ReadWrite)]
public EntProtoId Rule = string.Empty;
/// <summary>
/// The rule that's been spawned.
/// Used to prevent spawning multiple rules.
/// </summary>
[DataField, ViewVariables(VVAccess.ReadWrite)]
public EntityUid? RuleEntity;
}

View File

@@ -0,0 +1,67 @@
using Content.Server.GameTicking.Rules;
using Content.Shared.Mind;
using Content.Shared.Mind.Components;
namespace Content.Server.GenericAntag;
/// <summary>
/// Handles adding objectives to <see cref="GenericAntagComponent"/>s.
/// Roundend summary is handled by <see cref="GenericAntagRuleSystem"/>.
/// </summary>
public sealed class GenericAntagSystem : EntitySystem
{
[Dependency] private readonly SharedMindSystem _mind = default!;
[Dependency] private readonly GenericAntagRuleSystem _genericAntagRule = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<GenericAntagComponent, MindAddedMessage>(OnMindAdded);
}
private void OnMindAdded(EntityUid uid, GenericAntagComponent comp, MindAddedMessage args)
{
if (!TryComp<MindContainerComponent>(uid, out var mindContainer) || mindContainer.Mind == null)
return;
var mindId = mindContainer.Mind.Value;
MakeAntag(uid, mindId, comp);
}
/// <summary>
/// Turns a player into this antagonist.
/// Does the same thing that having a mind added does, use for antag ctrl.
/// </summary>
public void MakeAntag(EntityUid uid, EntityUid mindId, GenericAntagComponent? comp = null, MindComponent? mind = null)
{
if (!Resolve(uid, ref comp) || !Resolve(mindId, ref mind))
return;
// only add the rule once
if (comp.RuleEntity != null)
return;
// start the rule
if (!_genericAntagRule.StartRule(comp.Rule, mindId, out comp.RuleEntity, out var rule))
return;
// let other systems know the antag was created so they can add briefing, roles, etc.
// its important that this is before objectives are added since they may depend on roles added here
var ev = new GenericAntagCreatedEvent(mindId, mind);
RaiseLocalEvent(uid, ref ev);
// add the objectives from the rule
foreach (var id in rule.Objectives)
{
_mind.TryAddObjective(mindId, mind, id);
}
}
}
/// <summary>
/// Event raised on a player's entity after its simple antag rule is started and objectives get added.
/// Use this to add a briefing, roles, etc.
/// </summary>
[ByRefEvent]
public record struct GenericAntagCreatedEvent(EntityUid MindId, MindComponent Mind);