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

Add xeno roles, playtime, make them selectable to be played #2182

Merged
merged 1 commit into from
Jun 1, 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
5 changes: 3 additions & 2 deletions Content.Client/LateJoin/LateJoinGui.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
using System.Numerics;
using Content.Client.CrewManifest;
using Content.Client.GameTicking.Managers;
using Content.Client.UserInterface.Controls;
using Content.Client.Players.PlayTimeTracking;
using Content.Client.UserInterface.Controls;
using Content.Shared._CM14.Prototypes;
using Content.Shared.CCVar;
using Content.Shared.Roles;
using Content.Shared.StatusIcon;
Expand Down Expand Up @@ -160,7 +161,7 @@ private void RebuildUI()
};

var firstCategory = true;
var departments = _prototypeManager.EnumeratePrototypes<DepartmentPrototype>().ToArray();
var departments = _prototypeManager.EnumerateCM<DepartmentPrototype>().ToArray();
Array.Sort(departments, DepartmentUIComparer.Instance);

_jobButtons[id] = new Dictionary<string, List<JobButton>>();
Expand Down
6 changes: 5 additions & 1 deletion Content.Client/Lobby/UI/HumanoidProfileEditor.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Content.Client.Message;
using Content.Client.Players.PlayTimeTracking;
using Content.Client.UserInterface.Systems.Guidebook;
using Content.Shared._CM14.Prototypes;
using Content.Shared.CCVar;
using Content.Shared.Clothing;
using Content.Shared.GameTicking;
Expand Down Expand Up @@ -380,6 +381,8 @@ public sealed partial class HumanoidProfileEditor : BoxContainer
#endregion Jobs

TabContainer.SetTabTitle(2, Loc.GetString("humanoid-profile-editor-antags-tab"));
// TODO CM14 antags
TabContainer.SetTabVisible(2, false);

RefreshTraits();

Expand Down Expand Up @@ -542,7 +545,7 @@ public void RefreshAntags()
("humanoid-profile-editor-antag-preference-no-button", 1)
};

foreach (var antag in _prototypeManager.EnumeratePrototypes<AntagPrototype>().OrderBy(a => Loc.GetString(a.Name)))
foreach (var antag in _prototypeManager.EnumerateCM<AntagPrototype>().OrderBy(a => Loc.GetString(a.Name)))
{
if (!antag.SetPreference)
continue;
Expand Down Expand Up @@ -778,6 +781,7 @@ public void RefreshJobs()

var jobs = department.Roles.Select(jobId => _prototypeManager.Index<JobPrototype>(jobId))
.Where(job => job.SetPreference)
.Where(job => !job.Hidden)
.ToArray();

Array.Sort(jobs, JobUIComparer.Instance);
Expand Down
3 changes: 2 additions & 1 deletion Content.Server/GameTicking/GameTicker.RoundFlow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Content.Server.GameTicking.Events;
using Content.Server.Ghost;
using Content.Server.Maps;
using Content.Shared._CM14.Prototypes;
using Content.Shared.CCVar;
using Content.Shared.Database;
using Content.Shared.GameTicking;
Expand Down Expand Up @@ -624,7 +625,7 @@ private void AnnounceRound()
{
if (CurrentPreset == null) return;

var options = _prototypeManager.EnumeratePrototypes<RoundAnnouncementPrototype>().ToList();
var options = _prototypeManager.EnumerateCM<RoundAnnouncementPrototype>().ToList();

if (options.Count == 0)
return;
Expand Down
10 changes: 9 additions & 1 deletion Content.Server/GameTicking/Rules/GameRuleSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,14 @@ private void OnStartAttempt(RoundStartAttemptEvent args)
return;

var query = QueryAllRules();
while (query.MoveNext(out var uid, out _, out var gameRule))
while (query.MoveNext(out var uid, out var rule, out var gameRule))
{
var minPlayers = gameRule.MinPlayers;
if (args.Players.Length >= minPlayers)
{
OnStartAttempt((uid, rule, gameRule), args);
continue;
}

ChatManager.SendAdminAnnouncement(Loc.GetString("preset-not-enough-ready-players",
("readyPlayersCount", args.Players.Length),
Expand Down Expand Up @@ -82,6 +85,11 @@ private void OnRoundEndTextAppend(RoundEndTextAppendEvent ev)
}
}

protected virtual void OnStartAttempt(Entity<T, GameRuleComponent> gameRule, RoundStartAttemptEvent ev)
{

}

/// <summary>
/// Called when the gamerule is added
/// </summary>
Expand Down
10 changes: 10 additions & 0 deletions Content.Server/_CM14/Announce/RoundAnnouncementPrototype.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Content.Shared._CM14.Prototypes;

// ReSharper disable CheckNamespace
namespace Content.Server.Announcements;

public sealed partial class RoundAnnouncementPrototype : ICMSpecific
{
[DataField]
public bool IsCM { get; }
}
2 changes: 1 addition & 1 deletion Content.Server/_CM14/Dropship/DropshipSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected override bool FlyTo(Entity<DropshipNavigationComputerComponent> comput
var shuttle = Transform(computer).GridUid;
if (!TryComp(shuttle, out ShuttleComponent? shuttleComp))
{
Log.Warning($"Tried to launch through dropship computer {ToPrettyString(computer)} outside of a shuttle.");
Log.Warning($"Tried to launch {ToPrettyString(computer)} outside of a shuttle.");
return false;
}

Expand Down
15 changes: 14 additions & 1 deletion Content.Server/_CM14/Rules/CMDistressSignalRuleComponent.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Robust.Shared.Prototypes;
using Content.Shared.Roles;
using Robust.Shared.Prototypes;

namespace Content.Server._CM14.Rules;

Expand Down Expand Up @@ -30,4 +31,16 @@ public sealed partial class CMDistressSignalRuleComponent : Component
// TODO CM14
[DataField]
public bool XenosEverOnShip;

[DataField]
public ProtoId<JobPrototype> QueenJob = "CMXenoQueen";

[DataField]
public EntProtoId QueenEnt = "CMXenoQueen";

[DataField]
public ProtoId<JobPrototype> XenoSelectableJob = "CMXenoSelectableXenomorph";

[DataField]
public EntProtoId LarvaEnt = "CMXenoLarva";
}
176 changes: 160 additions & 16 deletions Content.Server/_CM14/Rules/CMDistressSignalRuleSystem.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices;
using Content.Server._CM14.Marines;
using Content.Server.Administration.Components;
using Content.Server.Administration.Managers;
using Content.Server.GameTicking;
using Content.Server.GameTicking.Components;
using Content.Server.GameTicking.Rules;
using Content.Server.Mind;
using Content.Server.Parallax;
using Content.Server.Players.PlayTimeTracking;
using Content.Server.Power.Components;
using Content.Server.Preferences.Managers;
using Content.Server.RoundEnd;
using Content.Server.Shuttles.Systems;
using Content.Server.Spawners.Components;
Expand All @@ -22,12 +26,15 @@
using Content.Shared.Mobs.Systems;
using Content.Shared.Nutrition.Components;
using Content.Shared.Nutrition.EntitySystems;
using Content.Shared.Parallax.Biomes;
using Content.Shared.Preferences;
using Content.Shared.Roles;
using Content.Shared.StatusIcon;
using Robust.Server.Containers;
using Robust.Server.GameObjects;
using Robust.Shared.Console;
using Robust.Server.Player;
using Robust.Shared.Map.Components;
using Robust.Shared.Network;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using Robust.Shared.Utility;
Expand All @@ -36,13 +43,17 @@ namespace Content.Server._CM14.Rules;

public sealed class CMDistressSignalRuleSystem : GameRuleSystem<CMDistressSignalRuleComponent>
{
[Dependency] private readonly IConsoleHost _console = default!;
[Dependency] private readonly IBanManager _bans = default!;
[Dependency] private readonly BiomeSystem _biome = default!;
[Dependency] private readonly ContainerSystem _containers = default!;
[Dependency] private readonly HungerSystem _hunger = default!;
[Dependency] private readonly MarineSystem _marines = default!;
[Dependency] private readonly MindSystem _mind = default!;
[Dependency] private readonly MapSystem _map = default!;
[Dependency] private readonly MobStateSystem _mobState = default!;
[Dependency] private readonly IPlayerManager _player = default!;
[Dependency] private readonly PlayTimeTrackingSystem _playTime = default!;
[Dependency] private readonly IServerPreferencesManager _prefsManager = default!;
[Dependency] private readonly IPrototypeManager _prototypes = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly RoundEndSystem _roundEnd = default!;
Expand All @@ -51,6 +62,9 @@ public sealed class CMDistressSignalRuleSystem : GameRuleSystem<CMDistressSignal
[Dependency] private readonly TransformSystem _transform = default!;
[Dependency] private readonly XenoSystem _xeno = default!;

[ValidatePrototypeId<BiomeTemplatePrototype>]
private const string PlanetBiome = "Grasslands";

public override void Initialize()
{
base.Initialize();
Expand Down Expand Up @@ -83,28 +97,120 @@ private void OnRulePlayerSpawning(RulePlayerSpawningEvent ev)
continue;
}

var totalXenos = ev.PlayerPool.Count / comp.PlayersPerXeno;
var profiles = ev.Profiles.ToDictionary();
bool IsAllowed(NetUserId id, ProtoId<JobPrototype> role)
{
if (!_player.TryGetSessionById(id, out var player))
return false;

var jobBans = _bans.GetJobBans(player.UserId);
if (jobBans == null || jobBans.Contains(role))
return false;

// TODO CM14 preferences
// Xenos
for (var i = 0; i < totalXenos; i++)
if (!_playTime.IsAllowed(player, role))
return false;

return true;
}

NetUserId? SpawnXeno(List<NetUserId> list, EntProtoId ent)
{
var player = _random.PickAndTake(ev.PlayerPool);
profiles.Remove(player.UserId);
var playerId = _random.PickAndTake(list);
if (!_player.TryGetSessionById(playerId, out var player))
{
Log.Error($"Failed to find player with id {playerId} during xeno selection.");
return null;
}

ev.PlayerPool.Remove(player);
GameTicker.PlayerJoinGame(player);

// TODO CM14 xeno spawn points
var xenoEnt = Spawn("CMXenoDrone", comp.XenoMap.ToCoordinates());
var xenoEnt = Spawn(ent, comp.XenoMap.ToCoordinates());

_xeno.MakeXeno(xenoEnt);
_xeno.SetHive(xenoEnt, comp.Hive);

// TODO CM14 mind name
if (!_mind.TryGetMind(player.UserId, out var mind))
mind = _mind.CreateMind(player.UserId, Name(xenoEnt));
if (!_mind.TryGetMind(playerId, out var mind))
mind = _mind.CreateMind(playerId);

_mind.TransferTo(mind.Value, xenoEnt);
return playerId;
}

var totalXenos = Math.Max(1, ev.PlayerPool.Count / comp.PlayersPerXeno);
var xenoCandidates = new List<NetUserId>[Enum.GetValues<JobPriority>().Length];
for (var i = 0; i < xenoCandidates.Length; i++)
{
xenoCandidates[i] = [];
}

foreach (var (id, profile) in ev.Profiles)
{
if (!IsAllowed(id, comp.QueenJob))
continue;

if (profile.JobPriorities.TryGetValue(comp.QueenJob, out var priority) &&
priority > JobPriority.Never)
{
xenoCandidates[(int) priority].Add(id);
}
}

NetUserId? queenSelected = null;
for (var i = xenoCandidates.Length - 1; i >= 0; i--)
{
var list = xenoCandidates[i];
while (list.Count > 0)
{
queenSelected = SpawnXeno(list, comp.QueenEnt);
if (queenSelected != null)
break;
}

if (queenSelected != null)
{
totalXenos--;
break;
}
}

foreach (var list in xenoCandidates)
{
list.Clear();
}

foreach (var (id, profile) in ev.Profiles)
{
if (id == queenSelected)
continue;

if (!IsAllowed(id, comp.XenoSelectableJob))
continue;

if (profile.JobPriorities.TryGetValue(comp.XenoSelectableJob, out var priority) &&
priority > JobPriority.Never)
{
xenoCandidates[(int) priority].Add(id);
}
}

var selected = 0;
for (var i = xenoCandidates.Length - 1; i >= 0; i--)
{
var list = xenoCandidates[i];
while (list.Count > 0 && selected < totalXenos)
{
if (SpawnXeno(list, comp.LarvaEnt) != null)
selected++;
}
}

for (var i = selected; i < totalXenos; i++)
{
// TODO CM14 xeno spawn points
var xenoEnt = Spawn(comp.LarvaEnt, comp.XenoMap.ToCoordinates());
_xeno.MakeXeno(xenoEnt);
_xeno.SetHive(xenoEnt, comp.Hive);
}
}
}
Expand Down Expand Up @@ -188,6 +294,44 @@ private void OnAlmayerMapInit(Entity<AlmayerComponent> almayer, ref MapInitEvent
}
}

protected override void OnStartAttempt(Entity<CMDistressSignalRuleComponent, GameRuleComponent> gameRule, RoundStartAttemptEvent ev)
{
if (ev.Forced || ev.Cancelled)
return;

var query = QueryAllRules();
while (query.MoveNext(out _, out var distress, out _))
{
var xenoCandidate = false;
foreach (var player in ev.Players)
{
if (_prefsManager.TryGetCachedPreferences(player.UserId, out var preferences))
{
var profile = (HumanoidCharacterProfile) preferences.GetProfile(preferences.SelectedCharacterIndex);
if (profile.JobPriorities.TryGetValue(distress.XenoSelectableJob, out var xenoPriority) &&
xenoPriority > JobPriority.Never)
{
xenoCandidate = true;
break;
}

if (profile.JobPriorities.TryGetValue(distress.QueenJob, out var queenPriority) &&
queenPriority > JobPriority.Never)
{
xenoCandidate = true;
break;
}
}
}

if (xenoCandidate)
continue;

ChatManager.SendAdminAnnouncement("Can't start distress signal. Requires at least 1 xeno player but we have 0.");
ev.Cancel();
}
}

private IEnumerable<EntityUid> GetChildren(EntityUid almayer)
{
if (TryComp<StationDataComponent>(almayer, out var station))
Expand Down Expand Up @@ -313,7 +457,7 @@ private void CheckRoundShouldEnd()
private bool SpawnXenoMap(Entity<CMDistressSignalRuleComponent> rule)
{
var mapId = _map.CreateMap();
_console.ExecuteCommand($"planet {mapId} Grasslands");
_biome.EnsurePlanet(mapId, _prototypes.Index<BiomeTemplatePrototype>(PlanetBiome));
rule.Comp.XenoMap = mapId;
return true;
}
Expand Down
Loading
Loading