Files
wwdpublic/Content.Shared/Objectives/Systems/SharedObjectivesSystem.cs
sleepyyapril 885ee5a831 Wizmerge for Station AI (#1351)
<!--
This is a semi-strict format, you can add/remove sections as needed but
the order/format should be kept the same
Remove these comments before submitting
-->

# Description

the adding AI is now up to y'all because i'm not touching loadout code
for name datasets, but it shouldn't be too bad from here

---------

Signed-off-by: sleepyyapril <123355664+sleepyyapril@users.noreply.github.com>
Signed-off-by: SolStar <44028047+ewokswagger@users.noreply.github.com>
Signed-off-by: deltanedas <39013340+deltanedas@users.noreply.github.com>
Co-authored-by: themias <89101928+themias@users.noreply.github.com>
Co-authored-by: Verm <32827189+Vermidia@users.noreply.github.com>
Co-authored-by: DrSmugleaf <10968691+DrSmugleaf@users.noreply.github.com>
Co-authored-by: Sphiral <145869023+SphiraI@users.noreply.github.com>
Co-authored-by: Ed <96445749+TheShuEd@users.noreply.github.com>
Co-authored-by: Mr. 27 <45323883+Dutch-VanDerLinde@users.noreply.github.com>
Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
Co-authored-by: Alzore <140123969+Blackern5000@users.noreply.github.com>
Co-authored-by: ravage <142820619+ravage123321@users.noreply.github.com>
Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com>
Co-authored-by: Intoxicating-Innocence <188202277+Intoxicating-Innocence@users.noreply.github.com>
Co-authored-by: Saphire <lattice@saphi.re>
Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
Co-authored-by: Errant <35878406+Errant-4@users.noreply.github.com>
Co-authored-by: Tayrtahn <tayrtahn@gmail.com>
Co-authored-by: CaasGit <87243814+CaasGit@users.noreply.github.com>
Co-authored-by: BramvanZijp <56019239+BramvanZijp@users.noreply.github.com>
Co-authored-by: Boaz1111 <149967078+Boaz1111@users.noreply.github.com>
Co-authored-by: NakataRin <45946146+NakataRin@users.noreply.github.com>
Co-authored-by: Kara <lunarautomaton6@gmail.com>
Co-authored-by: Plykiya <58439124+Plykiya@users.noreply.github.com>
Co-authored-by: SlamBamActionman <slambamactionman@gmail.com>
Co-authored-by: Doomsdrayk <robotdoughnut@comcast.net>
Co-authored-by: Brandon Hu <103440971+Brandon-Huu@users.noreply.github.com>
Co-authored-by: SlamBamActionman <83650252+SlamBamActionman@users.noreply.github.com>
Co-authored-by: ElectroJr <leonsfriedrich@gmail.com>
Co-authored-by: Pieter-Jan Briers <pieterjan.briers+git@gmail.com>
Co-authored-by: DrSmugleaf <DrSmugleaf@users.noreply.github.com>
Co-authored-by: Julian Giebel <juliangiebel@live.de>
Co-authored-by: nikthechampiongr <32041239+nikthechampiongr@users.noreply.github.com>
Co-authored-by: Repo <47093363+Titian3@users.noreply.github.com>
Co-authored-by: Chief-Engineer <119664036+Chief-Engineer@users.noreply.github.com>
Co-authored-by: icekot8 <93311212+icekot8@users.noreply.github.com>
Co-authored-by: AJCM-git <60196617+AJCM-git@users.noreply.github.com>
Co-authored-by: Leon Friedrich <60421075+ElectroJr@users.noreply.github.com>
Co-authored-by: no <165581243+pissdemon@users.noreply.github.com>
Co-authored-by: Tornado Tech <54727692+Tornado-Technology@users.noreply.github.com>
Co-authored-by: osjarw <62134478+osjarw@users.noreply.github.com>
Co-authored-by: Simon <63975668+Simyon264@users.noreply.github.com>
Co-authored-by: TGRCDev <tgrc@tgrc.dev>
Co-authored-by: Milon <milonpl.git@proton.me>
Co-authored-by: deltanedas <39013340+deltanedas@users.noreply.github.com>
Co-authored-by: ShadowCommander <10494922+ShadowCommander@users.noreply.github.com>
Co-authored-by: Fildrance <fildrance@gmail.com>
Co-authored-by: pa.pecherskij <pa.pecherskij@interfax.ru>
Co-authored-by: chavonadelal <156101927+chavonadelal@users.noreply.github.com>
Co-authored-by: SolStar <44028047+ewokswagger@users.noreply.github.com>
Co-authored-by: K-Dynamic <20566341+K-Dynamic@users.noreply.github.com>
Co-authored-by: lzk <124214523+lzk228@users.noreply.github.com>
Co-authored-by: ArchRBX <5040911+ArchRBX@users.noreply.github.com>
Co-authored-by: archrbx <punk.gear5260@fastmail.com>
Co-authored-by: Radezolid <snappednexus@gmail.com>
Co-authored-by: Nemanja <98561806+EmoGarbage404@users.noreply.github.com>
Co-authored-by: EmoGarbage404 <retron404@gmail.com>
Co-authored-by: MilenVolf <63782763+MilenVolf@users.noreply.github.com>
Co-authored-by: Velcroboy <107660393+IamVelcroboy@users.noreply.github.com>
Co-authored-by: Velcroboy <velcroboy333@hotmail.com>
Co-authored-by: neuPanda <chriseparton@gmail.com>
Co-authored-by: neuPanda <spainman0@yahoo.com>
Co-authored-by: Dvir <39403717+dvir001@users.noreply.github.com>
Co-authored-by: Whatstone <whatston3@gmail.com>
Co-authored-by: VideoKompany <135313844+VlaDOS1408@users.noreply.github.com>

(cherry picked from commit 93ed70acfeda357133a701f637d3faeec02749bb)
2025-01-14 00:13:42 +03:00

157 lines
5.3 KiB
C#

using Content.Shared.Mind;
using Content.Shared.Objectives;
using Content.Shared.Objectives.Components;
using Robust.Shared.Prototypes;
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!;
[Dependency] private readonly IPrototypeManager _protoMan = 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)
{
if (!_protoMan.HasIndex<EntityPrototype>(proto))
return null;
var uid = Spawn(proto);
if (!TryComp<ObjectiveComponent>(uid, out var comp))
{
Del(uid);
Log.Error($"Invalid objective prototype {proto}, missing ObjectiveComponent");
return null;
}
if (!CanBeAssigned(uid, mindId, mind, comp))
{
Log.Warning($"Objective {proto} 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 {proto}, 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);
Log.Debug($"Created objective {ToPrettyString(uid):objective}");
return uid;
}
/// <summary>
/// Get the title, description, icon and progress of an objective using <see cref="ObjectiveGetProgressEvent"/>.
/// 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;
if (GetProgress(uid, (mindId, mind)) is not {} progress)
return null;
var comp = Comp<ObjectiveComponent>(uid);
var meta = MetaData(uid);
var title = meta.EntityName;
var description = meta.EntityDescription;
if (comp.Icon == null)
{
Log.Error($"An objective {ToPrettyString(uid):objective} of {_mind.MindOwnerLoggingString(mind)} is missing an icon!");
return null;
}
return new ObjectiveInfo(title, description, comp.Icon, progress);
}
/// <summary>
/// Gets the progress of an objective using <see cref="ObjectiveGetProgressEvent"/>.
/// Returning null is a programmer error.
/// </summary>
public float? GetProgress(EntityUid uid, Entity<MindComponent> mind)
{
var ev = new ObjectiveGetProgressEvent(mind, mind.Comp);
RaiseLocalEvent(uid, ref ev);
if (ev.Progress != null)
return ev.Progress;
Log.Error($"Objective {ToPrettyString(uid):objective} of {_mind.MindOwnerLoggingString(mind.Comp)} didn't set a progress value!");
return null;
}
/// <summary>
/// Returns true if an objective is completed.
/// </summary>
public bool IsCompleted(EntityUid uid, Entity<MindComponent> mind)
{
return (GetProgress(uid, mind) ?? 0f) >= 0.999f;
}
/// <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;
}
}