Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Цели станции: Часть 2 #2338

Merged
merged 10 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions Content.Server/Corvax/StationGoal/StationGoalCommand.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Linq;
using Content.Server.Administration;
using Content.Server.Commands;
using Content.Shared.Administration;
using Robust.Shared.Console;
using Robust.Shared.Prototypes;
Expand All @@ -9,19 +10,27 @@ namespace Content.Server.Corvax.StationGoal
[AdminCommand(AdminFlags.Fun)]
public sealed class StationGoalCommand : IConsoleCommand
{
[Dependency] private readonly IEntityManager _entManager = default!;

public string Command => "sendstationgoal";
public string Description => Loc.GetString("send-station-goal-command-description");
public string Help => Loc.GetString("send-station-goal-command-help-text", ("command", Command));

public void Execute(IConsoleShell shell, string argStr, string[] args)
{
if (args.Length != 1)
if (args.Length != 2)
{
shell.WriteError(Loc.GetString("shell-need-exactly-one-argument"));
shell.WriteError(Loc.GetString("shell-wrong-arguments-number"));
return;
}

var protoId = args[0];
if (!NetEntity.TryParse(args[0], out var euidNet) || !_entManager.TryGetEntity(euidNet, out var euid))
{
shell.WriteError($"Failed to parse euid '{args[0]}'.");
return;
}

var protoId = args[1];
var prototypeManager = IoCManager.Resolve<IPrototypeManager>();
if (!prototypeManager.TryIndex<StationGoalPrototype>(protoId, out var proto))
{
Expand All @@ -30,24 +39,27 @@ public void Execute(IConsoleShell shell, string argStr, string[] args)
}

var stationGoalPaper = IoCManager.Resolve<IEntityManager>().System<StationGoalPaperSystem>();
if (!stationGoalPaper.SendStationGoal(proto))
if (!stationGoalPaper.SendStationGoal(euid, protoId))
{
shell.WriteError("Station goal was not sent");
return;
}
}

public CompletionResult GetCompletion(IConsoleShell shell, string[] args)
{
if (args.Length == 1)
switch (args.Length)
{
var options = IoCManager.Resolve<IPrototypeManager>()
.EnumeratePrototypes<StationGoalPrototype>()
.Select(p => new CompletionOption(p.ID));
case 1:
var stations = ContentCompletionHelper.StationIds(_entManager);
return CompletionResult.FromHintOptions(stations, "[StationId]");
case 2:
var options = IoCManager.Resolve<IPrototypeManager>()
.EnumeratePrototypes<StationGoalPrototype>()
.Select(p => new CompletionOption(p.ID));

return CompletionResult.FromHintOptions(options, Loc.GetString("send-station-goal-command-arg-id"));
return CompletionResult.FromHintOptions(options, Loc.GetString("send-station-goal-command-arg-id"));
}

return CompletionResult.Empty;
}
}
Expand Down
14 changes: 14 additions & 0 deletions Content.Server/Corvax/StationGoal/StationGoalComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Robust.Shared.Prototypes;

namespace Content.Server.Corvax.StationGoal
{
/// <summary>
/// if attached to a station prototype, will send the station a random goal from the list
/// </summary>
[RegisterComponent]
public sealed partial class StationGoalComponent : Component
{
[DataField]
public List<ProtoId<StationGoalPrototype>> Goals = new();
}
}
108 changes: 72 additions & 36 deletions Content.Server/Corvax/StationGoal/StationGoalPaperSystem.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System.Linq;
using Content.Server.Fax;
using Content.Server.GameTicking.Events;
using Content.Server.Station.Components;
using Content.Server.Station.Systems;
using Content.Shared.Fax.Components;
using Content.Shared.GameTicking;
using Content.Shared.Paper;
Expand All @@ -14,67 +17,100 @@ namespace Content.Server.Corvax.StationGoal
/// </summary>
public sealed class StationGoalPaperSystem : EntitySystem
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IPrototypeManager _proto = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly FaxSystem _faxSystem = default!;
[Dependency] private readonly FaxSystem _fax = default!;
[Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly StationSystem _station = default!;

public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<RoundStartedEvent>(OnRoundStarted);
SubscribeLocalEvent<RoundStartingEvent>(OnRoundStarting);
}

private void OnRoundStarted(RoundStartedEvent ev)
private void OnRoundStarting(RoundStartingEvent ev)
{
SendRandomGoal();
}

public bool SendRandomGoal()
{
var availableGoals = _prototypeManager.EnumeratePrototypes<StationGoalPrototype>().ToList();
var playerCount = _playerManager.PlayerCount;

var validGoals = availableGoals.Where(goal =>
(!goal.MinPlayers.HasValue || playerCount >= goal.MinPlayers.Value) &&
(!goal.MaxPlayers.HasValue || playerCount <= goal.MaxPlayers.Value)).ToList();

if (!validGoals.Any())
var query = EntityQueryEnumerator<StationGoalComponent>();
while (query.MoveNext(out var uid, out var station))
{
return false;
var tempGoals = new List<ProtoId<StationGoalPrototype>>(station.Goals);
StationGoalPrototype? selGoal = null;
while (tempGoals.Count > 0)
{
var goalId = _random.Pick(tempGoals);
var goalProto = _proto.Index(goalId);

if (playerCount > goalProto.MaxPlayers ||
playerCount < goalProto.MinPlayers)
{
tempGoals.Remove(goalId);
continue;
}

selGoal = goalProto;
break;
}

if (selGoal is null)
return;

if (SendStationGoal(uid, selGoal))
{
Log.Info($"Goal {selGoal.ID} has been sent to station {MetaData(uid).EntityName}");
}
}
}

var goal = _random.Pick(validGoals);
return SendStationGoal(goal);
public bool SendStationGoal(EntityUid? ent, ProtoId<StationGoalPrototype> goal)
{
return SendStationGoal(ent, _proto.Index(goal));
}

/// <summary>
/// Send a station goal to all faxes which are authorized to receive it.
/// Send a station goal on selected station to all faxes which are authorized to receive it.
/// </summary>
/// <returns>True if at least one fax received paper</returns>
public bool SendStationGoal(StationGoalPrototype goal)
public bool SendStationGoal(EntityUid? ent, StationGoalPrototype goal)
{
var faxes = EntityManager.EntityQuery<FaxMachineComponent>();
if (ent is null)
return false;

if (!TryComp<StationDataComponent>(ent, out var stationData))
return false;

var printout = new FaxPrintout(
Loc.GetString(goal.Text, ("station", MetaData(ent.Value).EntityName)),
Loc.GetString("station-goal-fax-paper-name"),
null,
null,
"paper_stamp-centcom",
new List<StampDisplayInfo>
{
new() { StampedName = Loc.GetString("stamp-component-stamped-name-centcom"), StampedColor = Color.FromHex("#006600") },
});

var wasSent = false;
foreach (var fax in faxes)
var query = EntityQueryEnumerator<FaxMachineComponent>();
while (query.MoveNext(out var faxUid, out var fax))
{
if (!fax.ReceiveStationGoal) continue;
if (!fax.ReceiveStationGoal)
continue;

var printout = new FaxPrintout(
Loc.GetString(goal.Text),
Loc.GetString("station-goal-fax-paper-name"),
null,
null,
"paper_stamp-centcom",
new List<StampDisplayInfo>
var largestGrid = _station.GetLargestGrid(stationData);
var grid = Transform(faxUid).GridUid;
if (grid is not null && largestGrid == grid.Value)
{
_fax.Receive(faxUid, printout, null, fax);
foreach (var spawnEnt in goal.Spawns)
{
new() { StampedName = Loc.GetString("stamp-component-stamped-name-centcom"), StampedColor = Color.FromHex("#006600") },
});
_faxSystem.Receive(fax.Owner, printout, null, fax);

wasSent = true;
SpawnAtPosition(spawnEnt, Transform(faxUid).Coordinates);
}
wasSent = true;
}
}

return wasSent;
}
}
Expand Down
6 changes: 6 additions & 0 deletions Content.Server/Corvax/StationGoal/StationGoalPrototype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,11 @@ public sealed class StationGoalPrototype : IPrototype
[DataField]
public int? MaxPlayers;

/// <summary>
/// Goal may require certain items to complete. These items will appear near the receving fax machine at the start of the round.
/// TODO: They should be spun up at the tradepost instead of at the fax machine, but I'm too lazy to do that right now. Maybe in the future.
/// </summary>
[DataField]
public List<EntProtoId> Spawns = new();
}
}
Loading
Loading