Objectives ecs rework (#19967)
Co-authored-by: deltanedas <@deltanedas:kde.org>
This commit is contained in:
@@ -3,11 +3,13 @@ using Content.Shared.Administration.Logs;
|
||||
using Content.Shared.Database;
|
||||
using Content.Shared.Examine;
|
||||
using Content.Shared.GameTicking;
|
||||
using Content.Shared.Humanoid;
|
||||
using Content.Shared.Interaction.Events;
|
||||
using Content.Shared.Mind.Components;
|
||||
using Content.Shared.Mobs.Components;
|
||||
using Content.Shared.Mobs.Systems;
|
||||
using Content.Shared.Objectives;
|
||||
using Content.Shared.Objectives.Systems;
|
||||
using Content.Shared.Players;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Network;
|
||||
@@ -20,9 +22,9 @@ namespace Content.Shared.Mind;
|
||||
public abstract class SharedMindSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly ISharedAdminLogManager _adminLogger = default!;
|
||||
[Dependency] private readonly IPrototypeManager _proto = default!;
|
||||
[Dependency] private readonly MobStateSystem _mobStateSystem = default!;
|
||||
[Dependency] private readonly SharedPlayerSystem _playerSystem = default!;
|
||||
[Dependency] private readonly MobStateSystem _mobState = default!;
|
||||
[Dependency] private readonly SharedObjectivesSystem _objectives = default!;
|
||||
[Dependency] private readonly SharedPlayerSystem _player = default!;
|
||||
|
||||
// This is dictionary is required to track the minds of disconnected players that may have had their entity deleted.
|
||||
protected readonly Dictionary<NetUserId, EntityUid> UserMinds = new();
|
||||
@@ -90,7 +92,7 @@ public abstract class SharedMindSystem : EntitySystem
|
||||
if (!mindContainer.ShowExamineInfo || !args.IsInDetailsRange)
|
||||
return;
|
||||
|
||||
var dead = _mobStateSystem.IsDead(uid);
|
||||
var dead = _mobState.IsDead(uid);
|
||||
var hasSession = CompOrNull<MindComponent>(mindContainer.Mind)?.Session;
|
||||
|
||||
if (dead && !mindContainer.HasMind)
|
||||
@@ -166,7 +168,7 @@ public abstract class SharedMindSystem : EntitySystem
|
||||
if (targetMobState == null)
|
||||
return true;
|
||||
// They might actually be alive.
|
||||
return _mobStateSystem.IsDead(mind.OwnedEntity.Value, targetMobState);
|
||||
return _mobState.IsDead(mind.OwnedEntity.Value, targetMobState);
|
||||
}
|
||||
|
||||
public virtual void Visit(EntityUid mindId, EntityUid entity, MindComponent? mind = null)
|
||||
@@ -215,7 +217,7 @@ public abstract class SharedMindSystem : EntitySystem
|
||||
|
||||
public void WipeMind(ICommonSession player)
|
||||
{
|
||||
var mind = _playerSystem.ContentData(player)?.Mind;
|
||||
var mind = _player.ContentData(player)?.Mind;
|
||||
DebugTools.Assert(GetMind(player.UserId) == mind);
|
||||
WipeMind(mind);
|
||||
}
|
||||
@@ -251,59 +253,44 @@ public abstract class SharedMindSystem : EntitySystem
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds an objective to this mind.
|
||||
/// Tries to create and add an objective from its prototype id.
|
||||
/// </summary>
|
||||
public bool TryAddObjective(EntityUid mindId, MindComponent mind, ObjectivePrototype objectivePrototype)
|
||||
/// <returns>Returns true if adding the objective succeeded.</returns>
|
||||
public bool TryAddObjective(EntityUid mindId, MindComponent mind, string proto)
|
||||
{
|
||||
if (!objectivePrototype.CanBeAssigned(mindId, mind))
|
||||
return false;
|
||||
var objective = objectivePrototype.GetObjective(mindId, mind);
|
||||
if (mind.Objectives.Contains(objective))
|
||||
var objective = _objectives.TryCreateObjective(mindId, mind, proto);
|
||||
if (objective == null)
|
||||
return false;
|
||||
|
||||
foreach (var condition in objective.Conditions)
|
||||
{
|
||||
_adminLogger.Add(LogType.Mind, LogImpact.Low, $"'{condition.Title}' added to mind of {MindOwnerLoggingString(mind)}");
|
||||
}
|
||||
|
||||
mind.Objectives.Add(objective);
|
||||
AddObjective(mindId, mind, objective.Value);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds an objective, by id, to this mind.
|
||||
/// Adds an objective that already exists, and is assumed to have had its requirements checked.
|
||||
/// </summary>
|
||||
public bool TryAddObjective(EntityUid mindId, string name, MindComponent? mind = null)
|
||||
public void AddObjective(EntityUid mindId, MindComponent mind, EntityUid objective)
|
||||
{
|
||||
if (!Resolve(mindId, ref mind))
|
||||
return false;
|
||||
|
||||
if (!_proto.TryIndex<ObjectivePrototype>(name, out var objective))
|
||||
{
|
||||
Log.Error($"Tried to add unknown objective prototype: {name}");
|
||||
return false;
|
||||
}
|
||||
|
||||
return TryAddObjective(mindId, mind, objective);
|
||||
var title = Name(objective);
|
||||
_adminLogger.Add(LogType.Mind, LogImpact.Low, $"Objective {objective} ({title}) added to mind of {MindOwnerLoggingString(mind)}");
|
||||
mind.Objectives.Add(objective);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes an objective to this mind.
|
||||
/// Removes an objective from this mind.
|
||||
/// </summary>
|
||||
/// <returns>Returns true if the removal succeeded.</returns>
|
||||
public bool TryRemoveObjective(MindComponent mind, int index)
|
||||
public bool TryRemoveObjective(EntityUid mindId, MindComponent mind, int index)
|
||||
{
|
||||
if (index < 0 || index >= mind.Objectives.Count)
|
||||
return false;
|
||||
|
||||
var objective = mind.Objectives[index];
|
||||
|
||||
foreach (var condition in objective.Conditions)
|
||||
{
|
||||
_adminLogger.Add(LogType.Mind, LogImpact.Low, $"'{condition.Title}' removed from the mind of {MindOwnerLoggingString(mind)}");
|
||||
}
|
||||
|
||||
var title = Name(objective);
|
||||
_adminLogger.Add(LogType.Mind, LogImpact.Low, $"Objective {objective} ({title}) removed from the mind of {MindOwnerLoggingString(mind)}");
|
||||
mind.Objectives.Remove(objective);
|
||||
Del(objective);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -356,7 +343,7 @@ public abstract class SharedMindSystem : EntitySystem
|
||||
{
|
||||
mindId = default;
|
||||
mind = null;
|
||||
return _playerSystem.ContentData(player) is { } data && TryGetMind(data, out mindId, out mind);
|
||||
return _player.ContentData(player) is { } data && TryGetMind(data, out mindId, out mind);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -432,6 +419,30 @@ public abstract class SharedMindSystem : EntitySystem
|
||||
{
|
||||
return TryGetMind(userId, out _, out var mind) ? mind.CharacterName : null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a list of every living humanoid player's minds, except for a single one which is exluded.
|
||||
/// </summary>
|
||||
public List<EntityUid> GetAliveHumansExcept(EntityUid exclude)
|
||||
{
|
||||
var mindQuery = EntityQuery<MindComponent>();
|
||||
|
||||
var allHumans = new List<EntityUid>();
|
||||
// HumanoidAppearanceComponent is used to prevent mice, pAIs, etc from being chosen
|
||||
var query = EntityQueryEnumerator<MindContainerComponent, MobStateComponent, HumanoidAppearanceComponent>();
|
||||
while (query.MoveNext(out var uid, out var mc, out var mobState, out _))
|
||||
{
|
||||
// the player needs to have a mind and not be the excluded one
|
||||
if (mc.Mind == null || mc.Mind == exclude)
|
||||
continue;
|
||||
|
||||
// the player has to be alive
|
||||
if (_mobState.IsAlive(uid, mobState))
|
||||
allHumans.Add(mc.Mind.Value);
|
||||
}
|
||||
|
||||
return allHumans;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user