Objectives ecs rework (#19967)
Co-authored-by: deltanedas <@deltanedas:kde.org>
This commit is contained in:
69
Content.Shared/Objectives/Components/ObjectiveComponent.cs
Normal file
69
Content.Shared/Objectives/Components/ObjectiveComponent.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
using Content.Shared.Mind;
|
||||
using Content.Shared.Objectives;
|
||||
using Content.Shared.Objectives.Systems;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
namespace Content.Shared.Objectives.Components;
|
||||
|
||||
/// <summary>
|
||||
/// Required component for an objective entity prototype.
|
||||
/// </summary>
|
||||
[RegisterComponent, Access(typeof(SharedObjectivesSystem))]
|
||||
public sealed partial class ObjectiveComponent : Component
|
||||
{
|
||||
/// <summary>
|
||||
/// Difficulty rating used to avoid assigning too many difficult objectives.
|
||||
/// </summary>
|
||||
[DataField(required: true), ViewVariables(VVAccess.ReadWrite)]
|
||||
public float Difficulty;
|
||||
|
||||
/// <summary>
|
||||
/// Organisation that issued this objective, used for grouping and as a header above common objectives.
|
||||
/// </summary>
|
||||
[DataField(required: true), ViewVariables(VVAccess.ReadWrite)]
|
||||
public string Issuer = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Unique objectives can only have 1 per prototype id.
|
||||
/// Set this to false if you want multiple objectives of the same prototype.
|
||||
/// </summary>
|
||||
[DataField, ViewVariables(VVAccess.ReadWrite)]
|
||||
public bool Unique = true;
|
||||
|
||||
/// <summary>
|
||||
/// Icon of this objective to display in the character menu.
|
||||
/// Can be specified by an <see cref="ObjectiveGetInfoEvent"/> handler but is usually done in the prototype.
|
||||
/// </summary>
|
||||
[DataField, ViewVariables(VVAccess.ReadWrite)]
|
||||
public SpriteSpecifier? Icon;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Event raised on an objective after spawning it to see if it meets all the requirements.
|
||||
/// Requirement components should have subscriptions and cancel if the requirements are not met.
|
||||
/// If a requirement is not met then the objective is deleted.
|
||||
/// </summary>
|
||||
[ByRefEvent]
|
||||
public record struct RequirementCheckEvent(EntityUid MindId, MindComponent Mind, bool Cancelled = false);
|
||||
|
||||
/// <summary>
|
||||
/// Event raised on an objective after its requirements have been checked.
|
||||
/// If <see cref="Cancelled"/> is set to true, the objective is deleted.
|
||||
/// Use this if the objective cannot be used, like a kill objective with no people alive.
|
||||
/// </summary>
|
||||
[ByRefEvent]
|
||||
public record struct ObjectiveAssignedEvent(EntityUid MindId, MindComponent Mind, bool Cancelled = false);
|
||||
|
||||
/// <summary>
|
||||
/// Event raised on an objective after everything has handled <see cref="ObjectiveAssignedEvent"/>.
|
||||
/// Use this to set the objective's title description or icon.
|
||||
/// </summary>
|
||||
[ByRefEvent]
|
||||
public record struct ObjectiveAfterAssignEvent(EntityUid MindId, MindComponent Mind, ObjectiveComponent Objective, MetaDataComponent Meta);
|
||||
|
||||
/// <summary>
|
||||
/// Event raised on an objective to update the Progress field.
|
||||
/// To use this yourself call <see cref="SharedObjectivesSystem.GetInfo"/> with the mind.
|
||||
/// </summary>
|
||||
[ByRefEvent]
|
||||
public record struct ObjectiveGetProgressEvent(EntityUid MindId, MindComponent Mind, float? Progress = null);
|
||||
@@ -1,22 +0,0 @@
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
namespace Content.Shared.Objectives
|
||||
{
|
||||
[Serializable, NetSerializable]
|
||||
public sealed class ConditionInfo
|
||||
{
|
||||
public string Title { get; }
|
||||
public string Description { get; }
|
||||
public SpriteSpecifier SpriteSpecifier { get; }
|
||||
public float Progress { get; }
|
||||
|
||||
public ConditionInfo(string title, string description, SpriteSpecifier spriteSpecifier, float progress)
|
||||
{
|
||||
Title = title;
|
||||
Description = description;
|
||||
SpriteSpecifier = spriteSpecifier;
|
||||
Progress = progress;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
using Content.Shared.Mind;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
namespace Content.Shared.Objectives.Interfaces
|
||||
{
|
||||
// TODO refactor all of this to be ecs
|
||||
public interface IObjectiveCondition
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns a copy of the IObjectiveCondition which is assigned to the mind.
|
||||
/// </summary>
|
||||
/// <param name="mindId">Mind id to assign to.</param>
|
||||
/// <param name="mind">Mind to assign to.</param>
|
||||
/// <returns>The new IObjectiveCondition.</returns>
|
||||
IObjectiveCondition GetAssigned(EntityUid mindId, MindComponent mind);
|
||||
|
||||
/// <summary>
|
||||
/// Returns the title of the condition.
|
||||
/// </summary>
|
||||
string Title { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns the description of the condition.
|
||||
/// </summary>
|
||||
string Description { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns a SpriteSpecifier to be used as an icon for the condition.
|
||||
/// </summary>
|
||||
SpriteSpecifier Icon { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns the current progress of the condition in % from 0 to 1.
|
||||
/// </summary>
|
||||
/// <returns>Current progress in %.</returns>
|
||||
float Progress { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns a difficulty of the condition.
|
||||
/// </summary>
|
||||
float Difficulty { get; }
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
using Content.Shared.Mind;
|
||||
|
||||
namespace Content.Shared.Objectives.Interfaces
|
||||
{
|
||||
// TODO refactor all of this to be ecs
|
||||
public interface IObjectiveRequirement
|
||||
{
|
||||
/// <summary>
|
||||
/// Checks whether or not the entity & its surroundings are valid to be given the objective.
|
||||
/// </summary>
|
||||
/// <returns>Returns true if objective can be given.</returns>
|
||||
bool CanBeAssigned(EntityUid mindId, MindComponent mind);
|
||||
}
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
using Content.Shared.Mind;
|
||||
using Content.Shared.Objectives.Interfaces;
|
||||
|
||||
namespace Content.Shared.Objectives
|
||||
{
|
||||
public sealed class Objective : IEquatable<Objective>
|
||||
{
|
||||
[ViewVariables]
|
||||
public readonly EntityUid MindId;
|
||||
[ViewVariables]
|
||||
public readonly MindComponent Mind;
|
||||
[ViewVariables]
|
||||
public readonly ObjectivePrototype Prototype;
|
||||
private readonly List<IObjectiveCondition> _conditions = new();
|
||||
[ViewVariables]
|
||||
public IReadOnlyList<IObjectiveCondition> Conditions => _conditions;
|
||||
|
||||
public Objective(ObjectivePrototype prototype, EntityUid mindId, MindComponent mind)
|
||||
{
|
||||
Prototype = prototype;
|
||||
MindId = mindId;
|
||||
Mind = mind;
|
||||
foreach (var condition in prototype.Conditions)
|
||||
{
|
||||
_conditions.Add(condition.GetAssigned(mindId, mind));
|
||||
}
|
||||
}
|
||||
|
||||
public bool Equals(Objective? other)
|
||||
{
|
||||
if (other is null) return false;
|
||||
if (ReferenceEquals(this, other)) return true;
|
||||
if (!Equals(Mind, other.Mind) || !Equals(Prototype, other.Prototype)) return false;
|
||||
if (_conditions.Count != other._conditions.Count) return false;
|
||||
for (var i = 0; i < _conditions.Count; i++)
|
||||
{
|
||||
if (!_conditions[i].Equals(other._conditions[i])) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public override bool Equals(object? obj)
|
||||
{
|
||||
if (ReferenceEquals(null, obj)) return false;
|
||||
if (ReferenceEquals(this, obj)) return true;
|
||||
if (obj.GetType() != GetType()) return false;
|
||||
return Equals((Objective) obj);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return HashCode.Combine(Mind, Prototype, _conditions);
|
||||
}
|
||||
}
|
||||
}
|
||||
17
Content.Shared/Objectives/ObjectiveInfo.cs
Normal file
17
Content.Shared/Objectives/ObjectiveInfo.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
namespace Content.Shared.Objectives;
|
||||
|
||||
/// <summary>
|
||||
/// Info about objectives visible in the character menu and on round end.
|
||||
/// Description and icon are displayed only in the character menu.
|
||||
/// Progress is a percentage from 0.0 to 1.0.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// All of these fields must eventually be set by condition event handlers.
|
||||
/// Everything but progress can be set to static data in yaml on the entity and <see cref="ObjectiveComponent"/>.
|
||||
/// If anything is null it will be logged and return null.
|
||||
/// </remarks>
|
||||
[Serializable, NetSerializable]
|
||||
public record struct ObjectiveInfo(string Title, string Description, SpriteSpecifier Icon, float Progress);
|
||||
@@ -1,63 +0,0 @@
|
||||
using System.Linq;
|
||||
using Content.Shared.Mind;
|
||||
using Content.Shared.Objectives.Interfaces;
|
||||
using Robust.Shared.Prototypes;
|
||||
|
||||
namespace Content.Shared.Objectives
|
||||
{
|
||||
/// <summary>
|
||||
/// Prototype for objectives. Remember that to be assigned, it should be added to one or more objective groups in prototype. E.g. crew, traitor, wizard
|
||||
/// </summary>
|
||||
[Prototype("objective")]
|
||||
public sealed class ObjectivePrototype : IPrototype
|
||||
{
|
||||
[ViewVariables]
|
||||
[IdDataField]
|
||||
public string ID { get; private set; } = default!;
|
||||
|
||||
[DataField("issuer")] public string Issuer { get; private set; } = "Unknown";
|
||||
|
||||
[ViewVariables]
|
||||
public float Difficulty => _difficultyOverride ?? _conditions.Sum(c => c.Difficulty);
|
||||
|
||||
[DataField("conditions", serverOnly: true)]
|
||||
private List<IObjectiveCondition> _conditions = new();
|
||||
[DataField("requirements")]
|
||||
private List<IObjectiveRequirement> _requirements = new();
|
||||
|
||||
[ViewVariables]
|
||||
public IReadOnlyList<IObjectiveCondition> Conditions => _conditions;
|
||||
|
||||
[DataField("canBeDuplicate")]
|
||||
public bool CanBeDuplicateAssignment { get; private set; }
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField("difficultyOverride")]
|
||||
private float? _difficultyOverride = null;
|
||||
|
||||
public bool CanBeAssigned(EntityUid mindId, MindComponent mind)
|
||||
{
|
||||
foreach (var requirement in _requirements)
|
||||
{
|
||||
if (!requirement.CanBeAssigned(mindId, mind))
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!CanBeDuplicateAssignment)
|
||||
{
|
||||
foreach (var objective in mind.AllObjectives)
|
||||
{
|
||||
if (objective.Prototype.ID == ID)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public Objective GetObjective(EntityUid mindId, MindComponent mind)
|
||||
{
|
||||
return new Objective(this, mindId, mind);
|
||||
}
|
||||
}
|
||||
}
|
||||
130
Content.Shared/Objectives/Systems/SharedObjectivesSystem.cs
Normal file
130
Content.Shared/Objectives/Systems/SharedObjectivesSystem.cs
Normal file
@@ -0,0 +1,130 @@
|
||||
using Content.Shared.Mind;
|
||||
using Content.Shared.Objectives;
|
||||
using Content.Shared.Objectives.Components;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
namespace Content.Shared.Objectives.Systems;
|
||||
|
||||
/// <summary>
|
||||
/// Provides API for creating and interacting with objectives.
|
||||
/// </summary>
|
||||
public abstract class SharedObjectivesSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly SharedMindSystem _mind = default!;
|
||||
|
||||
private EntityQuery<MetaDataComponent> _metaQuery;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
_metaQuery = GetEntityQuery<MetaDataComponent>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks requirements and duplicate objectives to see if an objective can be assigned.
|
||||
/// </summary>
|
||||
public bool CanBeAssigned(EntityUid uid, EntityUid mindId, MindComponent mind, ObjectiveComponent? comp = null)
|
||||
{
|
||||
if (!Resolve(uid, ref comp))
|
||||
return false;
|
||||
|
||||
var ev = new RequirementCheckEvent(mindId, mind);
|
||||
RaiseLocalEvent(uid, ref ev);
|
||||
if (ev.Cancelled)
|
||||
return false;
|
||||
|
||||
// only check for duplicate prototypes if it's unique
|
||||
if (comp.Unique)
|
||||
{
|
||||
var proto = _metaQuery.GetComponent(uid).EntityPrototype?.ID;
|
||||
foreach (var objective in mind.AllObjectives)
|
||||
{
|
||||
if (_metaQuery.GetComponent(objective).EntityPrototype?.ID == proto)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Spawns and assigns an objective for a mind.
|
||||
/// The objective is not added to the mind's objectives, mind system does that in TryAddObjective.
|
||||
/// If the objective could not be assigned the objective is deleted and null is returned.
|
||||
/// </summary>
|
||||
public EntityUid? TryCreateObjective(EntityUid mindId, MindComponent mind, string proto)
|
||||
{
|
||||
var uid = Spawn(proto);
|
||||
if (!TryComp<ObjectiveComponent>(uid, out var comp))
|
||||
{
|
||||
Del(uid);
|
||||
Log.Error($"Invalid objective prototype {proto}, missing ObjectiveComponent");
|
||||
return null;
|
||||
}
|
||||
|
||||
Log.Debug($"Created objective {proto} ({uid})");
|
||||
|
||||
if (!CanBeAssigned(uid, mindId, mind, comp))
|
||||
{
|
||||
Del(uid);
|
||||
Log.Warning($"Objective {uid} did not match the requirements for {_mind.MindOwnerLoggingString(mind)}, deleted it");
|
||||
return null;
|
||||
}
|
||||
|
||||
var ev = new ObjectiveAssignedEvent(mindId, mind);
|
||||
RaiseLocalEvent(uid, ref ev);
|
||||
if (ev.Cancelled)
|
||||
{
|
||||
Del(uid);
|
||||
Log.Warning($"Could not assign objective {uid}, deleted it");
|
||||
return null;
|
||||
}
|
||||
|
||||
// let the title description and icon be set by systems
|
||||
var afterEv = new ObjectiveAfterAssignEvent(mindId, mind, comp, MetaData(uid));
|
||||
RaiseLocalEvent(uid, ref afterEv);
|
||||
|
||||
return uid;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the title, description, icon and progress of an objective using <see cref="ObjectiveGetInfoEvent"/>.
|
||||
/// If any of them are null it is logged and null is returned.
|
||||
/// </summary>
|
||||
/// <param name="uid"/>ID of the condition entity</param>
|
||||
/// <param name="mindId"/>ID of the player's mind entity</param>
|
||||
/// <param name="mind"/>Mind component of the player's mind</param>
|
||||
public ObjectiveInfo? GetInfo(EntityUid uid, EntityUid mindId, MindComponent? mind = null)
|
||||
{
|
||||
if (!Resolve(mindId, ref mind))
|
||||
return null;
|
||||
|
||||
var ev = new ObjectiveGetProgressEvent(mindId, mind);
|
||||
RaiseLocalEvent(uid, ref ev);
|
||||
|
||||
var comp = Comp<ObjectiveComponent>(uid);
|
||||
var meta = MetaData(uid);
|
||||
var title = meta.EntityName;
|
||||
var description = meta.EntityDescription;
|
||||
if (comp.Icon == null || ev.Progress == null)
|
||||
{
|
||||
Log.Error($"An objective {ToPrettyString(uid):objective} of {_mind.MindOwnerLoggingString(mind)} is missing icon or progress ({ev.Progress})");
|
||||
return null;
|
||||
}
|
||||
|
||||
return new ObjectiveInfo(title, description, comp.Icon, ev.Progress.Value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the objective's icon to the one specified.
|
||||
/// Intended for <see cref="ObjectiveAfterAssignEvent"/> handlers to set an icon.
|
||||
/// </summary>
|
||||
public void SetIcon(EntityUid uid, SpriteSpecifier icon, ObjectiveComponent? comp = null)
|
||||
{
|
||||
if (!Resolve(uid, ref comp))
|
||||
return;
|
||||
|
||||
comp.Icon = icon;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user