2023-11-13 05:04:33 -05:00
|
|
|
|
using Content.Server.GameTicking;
|
2023-09-04 05:55:34 +01:00
|
|
|
|
using Content.Server.GameTicking.Rules.Components;
|
|
|
|
|
|
using Content.Server.Mind;
|
2023-11-13 05:04:33 -05:00
|
|
|
|
using Content.Server.Shuttles.Systems;
|
|
|
|
|
|
using Content.Shared.Cuffs.Components;
|
2023-09-04 05:55:34 +01:00
|
|
|
|
using Content.Shared.Mind;
|
2023-11-13 05:04:33 -05:00
|
|
|
|
using Content.Shared.Mobs.Systems;
|
2023-09-16 07:18:10 +01:00
|
|
|
|
using Content.Shared.Objectives.Components;
|
|
|
|
|
|
using Content.Shared.Objectives.Systems;
|
2023-08-28 16:53:24 -07:00
|
|
|
|
using Content.Shared.Random;
|
|
|
|
|
|
using Content.Shared.Random.Helpers;
|
|
|
|
|
|
using Robust.Shared.Prototypes;
|
|
|
|
|
|
using Robust.Shared.Random;
|
2023-09-04 05:55:34 +01:00
|
|
|
|
using System.Linq;
|
2023-08-28 16:53:24 -07:00
|
|
|
|
|
|
|
|
|
|
namespace Content.Server.Objectives;
|
|
|
|
|
|
|
2023-09-16 07:18:10 +01:00
|
|
|
|
public sealed class ObjectivesSystem : SharedObjectivesSystem
|
2023-08-28 16:53:24 -07:00
|
|
|
|
{
|
2023-09-04 05:55:34 +01:00
|
|
|
|
[Dependency] private readonly GameTicker _gameTicker = default!;
|
2023-08-28 16:53:24 -07:00
|
|
|
|
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
|
|
|
|
|
|
[Dependency] private readonly IRobustRandom _random = default!;
|
2023-09-04 05:55:34 +01:00
|
|
|
|
[Dependency] private readonly MindSystem _mind = default!;
|
2023-11-13 05:04:33 -05:00
|
|
|
|
[Dependency] private readonly EmergencyShuttleSystem _emergencyShuttle = default!;
|
2023-09-04 05:55:34 +01:00
|
|
|
|
|
|
|
|
|
|
public override void Initialize()
|
|
|
|
|
|
{
|
|
|
|
|
|
base.Initialize();
|
|
|
|
|
|
|
|
|
|
|
|
SubscribeLocalEvent<RoundEndTextAppendEvent>(OnRoundEndText);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Adds objective text for each game rule's players on round end.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private void OnRoundEndText(RoundEndTextAppendEvent ev)
|
|
|
|
|
|
{
|
2023-09-16 07:18:10 +01:00
|
|
|
|
// go through each gamerule getting data for the roundend summary.
|
|
|
|
|
|
var summaries = new Dictionary<string, Dictionary<string, List<EntityUid>>>();
|
2023-09-04 05:55:34 +01:00
|
|
|
|
var query = EntityQueryEnumerator<GameRuleComponent>();
|
|
|
|
|
|
while (query.MoveNext(out var uid, out var gameRule))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!_gameTicker.IsGameRuleAdded(uid, gameRule))
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
var info = new ObjectivesTextGetInfoEvent(new List<EntityUid>(), string.Empty);
|
|
|
|
|
|
RaiseLocalEvent(uid, ref info);
|
|
|
|
|
|
if (info.Minds.Count == 0)
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
2023-09-16 07:18:10 +01:00
|
|
|
|
// first group the gamerules by their agents, for example 2 different dragons
|
2023-09-04 05:55:34 +01:00
|
|
|
|
var agent = info.AgentName;
|
2023-09-16 07:18:10 +01:00
|
|
|
|
if (!summaries.ContainsKey(agent))
|
|
|
|
|
|
summaries[agent] = new Dictionary<string, List<EntityUid>>();
|
|
|
|
|
|
|
|
|
|
|
|
var prepend = new ObjectivesTextPrependEvent("");
|
2023-09-04 05:55:34 +01:00
|
|
|
|
RaiseLocalEvent(uid, ref prepend);
|
|
|
|
|
|
|
2023-09-16 07:18:10 +01:00
|
|
|
|
// next group them by their prepended texts
|
|
|
|
|
|
// for example with traitor rule, group them by the codewords they share
|
|
|
|
|
|
var summary = summaries[agent];
|
|
|
|
|
|
if (summary.ContainsKey(prepend.Text))
|
2023-09-04 05:55:34 +01:00
|
|
|
|
{
|
2023-09-16 07:18:10 +01:00
|
|
|
|
// same prepended text (usually empty) so combine them
|
|
|
|
|
|
summary[prepend.Text].AddRange(info.Minds);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
summary[prepend.Text] = info.Minds;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-09-04 05:55:34 +01:00
|
|
|
|
|
2023-09-16 07:18:10 +01:00
|
|
|
|
// convert the data into summary text
|
|
|
|
|
|
foreach (var (agent, summary) in summaries)
|
|
|
|
|
|
{
|
|
|
|
|
|
// first get the total number of players that were in these game rules combined
|
|
|
|
|
|
var total = 0;
|
2023-11-13 05:04:33 -05:00
|
|
|
|
var totalInCustody = 0;
|
2023-09-16 07:18:10 +01:00
|
|
|
|
foreach (var (_, minds) in summary)
|
|
|
|
|
|
{
|
|
|
|
|
|
total += minds.Count;
|
2023-11-13 05:04:33 -05:00
|
|
|
|
totalInCustody += minds.Where(m => IsInCustody(m)).Count();
|
2023-09-16 07:18:10 +01:00
|
|
|
|
}
|
2023-09-04 05:55:34 +01:00
|
|
|
|
|
2023-09-16 07:18:10 +01:00
|
|
|
|
var result = Loc.GetString("objectives-round-end-result", ("count", total), ("agent", agent));
|
2023-11-13 05:04:33 -05:00
|
|
|
|
if (agent == Loc.GetString("traitor-round-end-agent-name"))
|
|
|
|
|
|
{
|
|
|
|
|
|
result += "\n" + Loc.GetString("objectives-round-end-result-in-custody", ("count", total), ("custody", totalInCustody), ("agent", agent));
|
|
|
|
|
|
}
|
2023-09-16 07:18:10 +01:00
|
|
|
|
// next add all the players with its own prepended text
|
|
|
|
|
|
foreach (var (prepend, minds) in summary)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (prepend != string.Empty)
|
|
|
|
|
|
result += prepend;
|
2023-09-04 05:55:34 +01:00
|
|
|
|
|
2023-09-16 07:18:10 +01:00
|
|
|
|
// add space between the start text and player list
|
2023-09-04 05:55:34 +01:00
|
|
|
|
result += "\n";
|
|
|
|
|
|
|
2023-09-16 07:18:10 +01:00
|
|
|
|
AddSummary(ref result, agent, minds);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ev.AddLine(result + "\n");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void AddSummary(ref string result, string agent, List<EntityUid> minds)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var mindId in minds)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!TryComp(mindId, out MindComponent? mind))
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
2023-11-27 21:43:48 +00:00
|
|
|
|
var title = GetTitle(mindId, mind);
|
|
|
|
|
|
if (title == null)
|
|
|
|
|
|
continue;
|
2023-09-04 05:55:34 +01:00
|
|
|
|
|
2023-09-16 07:18:10 +01:00
|
|
|
|
result += "\n";
|
|
|
|
|
|
|
2023-11-13 05:04:33 -05:00
|
|
|
|
var custody = IsInCustody(mindId, mind) ? Loc.GetString("objectives-in-custody") + " " : "";
|
|
|
|
|
|
|
2023-09-16 07:18:10 +01:00
|
|
|
|
var objectives = mind.AllObjectives.ToArray();
|
|
|
|
|
|
if (objectives.Length == 0)
|
|
|
|
|
|
{
|
2023-11-13 05:04:33 -05:00
|
|
|
|
result += Loc.GetString("objectives-no-objectives", ("custody", custody), ("title", title), ("agent", agent));
|
2023-09-16 07:18:10 +01:00
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-13 05:04:33 -05:00
|
|
|
|
result += Loc.GetString("objectives-with-objectives", ("custody", custody), ("title", title), ("agent", agent));
|
2023-09-16 07:18:10 +01:00
|
|
|
|
|
|
|
|
|
|
foreach (var objectiveGroup in objectives.GroupBy(o => Comp<ObjectiveComponent>(o).Issuer))
|
|
|
|
|
|
{
|
2023-12-24 12:58:28 +03:00
|
|
|
|
//TO DO:
|
|
|
|
|
|
//check for the right group here. Getting the target issuer is easy: objectiveGroup.Key
|
|
|
|
|
|
//It should be compared to the type of the group's issuer.
|
2023-09-16 07:18:10 +01:00
|
|
|
|
result += "\n" + Loc.GetString($"objective-issuer-{objectiveGroup.Key}");
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var objective in objectiveGroup)
|
2023-09-04 05:55:34 +01:00
|
|
|
|
{
|
2023-09-16 07:18:10 +01:00
|
|
|
|
var info = GetInfo(objective, mindId, mind);
|
|
|
|
|
|
if (info == null)
|
|
|
|
|
|
continue;
|
2023-09-04 05:55:34 +01:00
|
|
|
|
|
2023-09-16 07:18:10 +01:00
|
|
|
|
var objectiveTitle = info.Value.Title;
|
|
|
|
|
|
var progress = info.Value.Progress;
|
|
|
|
|
|
if (progress > 0.99f)
|
2023-09-04 05:55:34 +01:00
|
|
|
|
{
|
2023-09-16 07:18:10 +01:00
|
|
|
|
result += "\n- " + Loc.GetString(
|
|
|
|
|
|
"objectives-objective-success",
|
|
|
|
|
|
("objective", objectiveTitle),
|
|
|
|
|
|
("markupColor", "green")
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
result += "\n- " + Loc.GetString(
|
|
|
|
|
|
"objectives-objective-fail",
|
|
|
|
|
|
("objective", objectiveTitle),
|
|
|
|
|
|
("progress", (int) (progress * 100)),
|
|
|
|
|
|
("markupColor", "red")
|
|
|
|
|
|
);
|
2023-09-04 05:55:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-08-28 16:53:24 -07:00
|
|
|
|
|
2023-09-16 07:18:10 +01:00
|
|
|
|
public EntityUid? GetRandomObjective(EntityUid mindId, MindComponent mind, string objectiveGroupProto)
|
2023-08-28 16:53:24 -07:00
|
|
|
|
{
|
|
|
|
|
|
if (!_prototypeManager.TryIndex<WeightedRandomPrototype>(objectiveGroupProto, out var groups))
|
|
|
|
|
|
{
|
2023-09-16 07:18:10 +01:00
|
|
|
|
Log.Error($"Tried to get a random objective, but can't index WeightedRandomPrototype {objectiveGroupProto}");
|
2023-08-28 16:53:24 -07:00
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TODO replace whatever the fuck this is with a proper objective selection system
|
|
|
|
|
|
// yeah the old 'preventing infinite loops' thing wasn't super elegant either and it mislead people on what exactly it did
|
|
|
|
|
|
var tries = 0;
|
|
|
|
|
|
while (tries < 20)
|
|
|
|
|
|
{
|
|
|
|
|
|
var groupName = groups.Pick(_random);
|
|
|
|
|
|
|
|
|
|
|
|
if (!_prototypeManager.TryIndex<WeightedRandomPrototype>(groupName, out var group))
|
|
|
|
|
|
{
|
2023-09-16 07:18:10 +01:00
|
|
|
|
Log.Error($"Couldn't index objective group prototype {groupName}");
|
2023-08-28 16:53:24 -07:00
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-09-16 07:18:10 +01:00
|
|
|
|
var proto = group.Pick(_random);
|
|
|
|
|
|
var objective = TryCreateObjective(mindId, mind, proto);
|
|
|
|
|
|
if (objective != null)
|
2023-08-28 16:53:24 -07:00
|
|
|
|
return objective;
|
2023-09-16 07:18:10 +01:00
|
|
|
|
|
|
|
|
|
|
tries++;
|
2023-08-28 16:53:24 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
2023-11-13 05:04:33 -05:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Returns whether a target is considered 'in custody' (cuffed on the shuttle).
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private bool IsInCustody(EntityUid mindId, MindComponent? mind = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!Resolve(mindId, ref mind))
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
// Ghosting will not save you
|
|
|
|
|
|
bool originalEntityInCustody = false;
|
|
|
|
|
|
EntityUid? originalEntity = GetEntity(mind.OriginalOwnedEntity);
|
|
|
|
|
|
if (originalEntity.HasValue && originalEntity != mind.OwnedEntity)
|
|
|
|
|
|
{
|
|
|
|
|
|
originalEntityInCustody = TryComp<CuffableComponent>(originalEntity, out var origCuffed) && origCuffed.CuffedHandCount > 0
|
|
|
|
|
|
&& _emergencyShuttle.IsTargetEscaping(originalEntity.Value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return originalEntityInCustody || (TryComp<CuffableComponent>(mind.OwnedEntity, out var cuffed) && cuffed.CuffedHandCount > 0
|
|
|
|
|
|
&& _emergencyShuttle.IsTargetEscaping(mind.OwnedEntity.Value));
|
|
|
|
|
|
}
|
2023-11-27 21:43:48 +00:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Get the title for a player's mind used in round end.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public string? GetTitle(EntityUid mindId, MindComponent? mind = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!Resolve(mindId, ref mind))
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
|
|
var name = mind.CharacterName;
|
|
|
|
|
|
_mind.TryGetSession(mindId, out var session);
|
|
|
|
|
|
var username = session?.Name;
|
|
|
|
|
|
|
|
|
|
|
|
if (username != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (name != null)
|
|
|
|
|
|
return Loc.GetString("objectives-player-user-named", ("user", username), ("name", name));
|
|
|
|
|
|
|
|
|
|
|
|
return Loc.GetString("objectives-player-user", ("user", username));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// nothing to identify the player by, just give up
|
|
|
|
|
|
if (name == null)
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
|
|
return Loc.GetString("objectives-player-named", ("name", name));
|
|
|
|
|
|
}
|
2023-08-28 16:53:24 -07:00
|
|
|
|
}
|
2023-09-04 05:55:34 +01:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Raised on the game rule to get info for any objectives.
|
|
|
|
|
|
/// If its minds list is set then the players will have their objectives shown in the round end text.
|
|
|
|
|
|
/// AgentName is the generic name for a player in the list.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <remarks>
|
|
|
|
|
|
/// The objectives system already checks if the game rule is added so you don't need to check that in this event's handler.
|
|
|
|
|
|
/// </remarks>
|
|
|
|
|
|
[ByRefEvent]
|
|
|
|
|
|
public record struct ObjectivesTextGetInfoEvent(List<EntityUid> Minds, string AgentName);
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Raised on the game rule before text for each agent's objectives is added, letting you prepend something.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[ByRefEvent]
|
|
|
|
|
|
public record struct ObjectivesTextPrependEvent(string Text);
|