Files
OldThink/Content.Server/Station/Systems/StationSpawningSystem.cs
MJSailor a95fe13180 Goida revert (#600)
* Revert "- fix: YAML linter fixes. (#598)"

This reverts commit 012bf3c357.

* Revert "Automatic changelog update"

This reverts commit cf1c3a9af5.

* Revert "[Fix] Base Layer Prototype (#597)"

This reverts commit b000423999.

* Revert "Modules update (#596)"

This reverts commit 00fbdead77.

* Revert "Automatic changelog update"

This reverts commit 0d7a12b2a2.

* Revert "Fixes (#593)"

This reverts commit 943c77031c.

* Revert "minor loadout fixes (#594)"

This reverts commit 143c010a89.

* Revert "Update DryDock.yml (#595)"

This reverts commit 4cd0100ac7.

* Revert "Automatic changelog update"

This reverts commit 08eadc690f.

* Revert "fix: Maximum message size (#591)"

This reverts commit 343f3612eb.

* Revert "Черри пики 7 (#592)"

This reverts commit 3f97bdce2f.

* Revert "Automatic changelog update"

This reverts commit 0678eca250.

* Revert "Рандомфиксы (#590)"

This reverts commit 2b9e5e2437.

* Revert "Нижнее бельё в лодауты (#580)"

This reverts commit e01a47b089.

* Revert "add lathe sounds (#588)"

This reverts commit c80a2985f2.

* Revert "Добавил параметр группы для некоторых реагентов (#585)"

This reverts commit 713b16bb98.

* Revert "add hrp ++++ aspect (#587)"

This reverts commit a6a69cc60f.

* Revert "Новые амбиенты и пару песен (#586)"

This reverts commit 48c86bd846.

* Revert "Сообщения в ПДА 2 (#583)"

This reverts commit cced3cc98b.

* Revert "Automatic changelog update"

This reverts commit abf435b11d.

* Revert "Chem stuff and more (#584)"

This reverts commit 3608960f5c.

* Revert "JobRequiremet refactor (#579)"

This reverts commit 9a9c9598e0.

* Revert "Revert "Reapply "Нижнее бельё в лодауты"""

This reverts commit 44447d573f.

* Revert "Reapply "Нижнее бельё в лодауты""

This reverts commit 0c4d082ad3.

* Revert "Revert "Нижнее бельё в лодауты""

This reverts commit 56473c5492.

* Revert "Нижнее бельё в лодауты"

This reverts commit d1cb0cb364.

* Revert "DryDock and WhiteMoose update (#578)"

This reverts commit 14755808af.

* Revert "Automatic changelog update"

This reverts commit 0133f82722.

* Revert "Fixes (#576)"

This reverts commit b7cc49896c.

* Revert "порт системы регенерации солюшена цинки (#574)"

This reverts commit a22cf3d50b.

* Revert "Воровские перчатки (#573)"

This reverts commit bb7140f3d4.

* Revert "mood resprite (#572)"

This reverts commit 4db96dc569.

* Revert "fix missing letter (#571)"

This reverts commit 94ea756794.

* Revert "Сообщения в ПДА (#564)"

This reverts commit d023d29e54.

* Revert "- fix: No visible aghost."

This reverts commit 27e7f25f7e.

* Revert "- tweak: Nerf cult shield."

This reverts commit 6a384246b8.
2024-08-09 19:09:22 +03:00

380 lines
14 KiB
C#

using System.Linq;
using Content.Server.Access.Systems;
using Content.Server.Humanoid;
using Content.Server.IdentityManagement;
using Content.Server.Mind.Commands;
using Content.Server.PDA;
using Content.Server.Shuttles.Systems;
using Content.Server.Spawners.EntitySystems;
using Content.Server.Station.Components;
using Content.Shared.Access.Components;
using Content.Shared.Access.Systems;
using Content.Shared.CCVar;
using Content.Shared.Clothing;
using Content.Shared.Humanoid;
using Content.Shared.Humanoid.Prototypes;
using Content.Shared.PDA;
using Content.Shared.Preferences;
using Content.Shared.Preferences.Loadouts;
using Content.Shared.Random;
using Content.Shared.Random.Helpers;
using Content.Shared.Roles;
using Content.Shared.Roles.Jobs;
using Content.Shared.Station;
using Content.Shared.StatusIcon;
using Content.Shared._White.CharacterExamine;
using Content.Shared.NameIdentifier;
using JetBrains.Annotations;
using Robust.Shared.Configuration;
using Robust.Shared.Map;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using Robust.Shared.Utility;
namespace Content.Server.Station.Systems;
/// <summary>
/// Manages spawning into the game, tracking available spawn points.
/// Also provides helpers for spawning in the player's mob.
/// </summary>
[PublicAPI]
public sealed class StationSpawningSystem : SharedStationSpawningSystem
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly IConfigurationManager _configurationManager = default!;
[Dependency] private readonly HumanoidAppearanceSystem _humanoidSystem = default!;
[Dependency] private readonly IdCardSystem _cardSystem = default!;
[Dependency] private readonly PdaSystem _pdaSystem = default!;
[Dependency] private readonly SharedAccessSystem _accessSystem = default!;
[Dependency] private readonly IdentitySystem _identity = default!;
[Dependency] private readonly MetaDataSystem _metaSystem = default!;
[Dependency] private readonly ArrivalsSystem _arrivalsSystem = default!;
[Dependency] private readonly ContainerSpawnPointSystem _containerSpawnPointSystem = default!;
private bool _randomizeCharacters;
private Dictionary<SpawnPriorityPreference, Action<PlayerSpawningEvent>> _spawnerCallbacks = new();
/// <inheritdoc/>
public override void Initialize()
{
Subs.CVar(_configurationManager, CCVars.ICRandomCharacters, e => _randomizeCharacters = e, true);
_spawnerCallbacks = new Dictionary<SpawnPriorityPreference, Action<PlayerSpawningEvent>>()
{
{ SpawnPriorityPreference.Arrivals, _arrivalsSystem.HandlePlayerSpawning },
{ SpawnPriorityPreference.Cryosleep, _containerSpawnPointSystem.HandlePlayerSpawning }
};
}
/// <summary>
/// Attempts to spawn a player character onto the given station.
/// </summary>
/// <param name="station">Station to spawn onto.</param>
/// <param name="job">The job to assign, if any.</param>
/// <param name="profile">The character profile to use, if any.</param>
/// <param name="stationSpawning">Resolve pattern, the station spawning component for the station.</param>
/// <returns>The resulting player character, if any.</returns>
/// <exception cref="ArgumentException">Thrown when the given station is not a station.</exception>
/// <remarks>
/// This only spawns the character, and does none of the mind-related setup you'd need for it to be playable.
/// </remarks>
public EntityUid? SpawnPlayerCharacterOnStation(EntityUid? station, JobComponent? job, HumanoidCharacterProfile? profile, StationSpawningComponent? stationSpawning = null)
{
if (station != null && !Resolve(station.Value, ref stationSpawning))
throw new ArgumentException("Tried to use a non-station entity as a station!", nameof(station));
var ev = new PlayerSpawningEvent(job, profile, station);
if (station != null && profile != null)
{
// Try to call the character's preferred spawner first.
if (_spawnerCallbacks.TryGetValue(profile.SpawnPriority, out var preferredSpawner))
{
preferredSpawner(ev);
foreach (var (key, remainingSpawner) in _spawnerCallbacks)
{
if (key == profile.SpawnPriority)
continue;
remainingSpawner(ev);
}
}
else
{
// Call all of them in the typical order.
foreach (var typicalSpawner in _spawnerCallbacks.Values)
{
typicalSpawner(ev);
}
}
}
RaiseLocalEvent(ev);
DebugTools.Assert(ev.SpawnResult is { Valid: true } or null);
return ev.SpawnResult;
}
//TODO: Figure out if everything in the player spawning region belongs somewhere else.
#region Player spawning helpers
/// <summary>
/// Spawns in a player's mob according to their job and character information at the given coordinates.
/// Used by systems that need to handle spawning players.
/// </summary>
/// <param name="coordinates">Coordinates to spawn the character at.</param>
/// <param name="job">Job to assign to the character, if any.</param>
/// <param name="profile">Appearance profile to use for the character.</param>
/// <param name="station">The station this player is being spawned on.</param>
/// <param name="entity">The entity to use, if one already exists.</param>
/// <returns>The spawned entity</returns>
public EntityUid SpawnPlayerMob(
EntityCoordinates coordinates,
JobComponent? job,
HumanoidCharacterProfile? profile,
EntityUid? station,
EntityUid? entity = null)
{
_prototypeManager.TryIndex(job?.Prototype ?? string.Empty, out var prototype);
// If we're not spawning a humanoid, we're gonna exit early without doing all the humanoid stuff.
if (prototype?.JobEntity != null)
{
DebugTools.Assert(entity is null);
var jobEntity = EntityManager.SpawnEntity(prototype.JobEntity, coordinates);
MakeSentientCommand.MakeSentient(jobEntity, EntityManager);
DoJobSpecials(job, jobEntity, profile);
_identity.QueueIdentityUpdate(jobEntity);
return jobEntity;
}
string speciesId;
if (_randomizeCharacters)
{
var weightId = _configurationManager.GetCVar(CCVars.ICRandomSpeciesWeights);
var weights = _prototypeManager.Index<WeightedRandomSpeciesPrototype>(weightId);
speciesId = weights.Pick(_random);
}
else if (profile != null)
{
speciesId = profile.Species;
}
else
{
speciesId = SharedHumanoidAppearanceSystem.DefaultSpecies;
}
if (!_prototypeManager.TryIndex<SpeciesPrototype>(speciesId, out var species))
throw new ArgumentException($"Invalid species prototype was used: {speciesId}");
entity ??= Spawn(species.Prototype, coordinates);
if (_randomizeCharacters)
{
profile = HumanoidCharacterProfile.RandomWithSpecies(speciesId);
}
if (prototype?.StartingGear != null)
{
var startingGear = _prototypeManager.Index<StartingGearPrototype>(prototype.StartingGear);
EquipStartingGear(entity.Value, startingGear);
}
// Run loadouts after so stuff like storage loadouts can get
var jobLoadout = LoadoutSystem.GetJobPrototype(prototype?.ID);
if (_prototypeManager.TryIndex(jobLoadout, out RoleLoadoutPrototype? roleProto))
{
RoleLoadout? loadout = null;
profile?.Loadouts.TryGetValue(jobLoadout, out loadout);
// Set to default if not present
if (loadout == null)
{
loadout = new RoleLoadout(jobLoadout);
loadout.SetDefault(_prototypeManager);
}
// Order loadout selections by the order they appear on the prototype.
foreach (var group in loadout.SelectedLoadouts.OrderBy(x => roleProto.Groups.FindIndex(e => e == x.Key)))
{
foreach (var items in group.Value)
{
if (!_prototypeManager.TryIndex(items.Prototype, out var loadoutProto))
{
Log.Error($"Unable to find loadout prototype for {items.Prototype}");
continue;
}
if (!_prototypeManager.TryIndex(loadoutProto.Equipment, out var startingGear))
{
Log.Error($"Unable to find starting gear {loadoutProto.Equipment} for loadout {loadoutProto}");
continue;
}
// Handle any extra data here.
EquipStartingGear(entity.Value, startingGear);
}
}
}
if (profile != null)
{
if (prototype != null)
{
if (prototype.ID.Contains("Clown"))
SetPdaAndIdCardData(entity.Value, profile.ClownName, prototype, station);
else if (prototype.ID.Contains("Mime"))
SetPdaAndIdCardData(entity.Value, profile.MimeName, prototype, station);
else
SetPdaAndIdCardData(entity.Value, profile.Name, prototype, station);
}
_humanoidSystem.LoadProfile(entity.Value, profile);
_metaSystem.SetEntityName(entity.Value, profile.Name);
if (profile.FlavorText != "" && _configurationManager.GetCVar(CCVars.FlavorText))
{
var detail = AddComp<DetailExaminableComponent>(entity.Value);
detail.Content = profile.FlavorText;
Dirty(detail);
}
}
DoJobSpecials(job, entity.Value, profile);
_identity.QueueIdentityUpdate(entity.Value);
return entity.Value;
}
private void DoJobSpecials(JobComponent? job, EntityUid entity, HumanoidCharacterProfile? profile)
{
if (!_prototypeManager.TryIndex(job?.Prototype ?? string.Empty, out JobPrototype? prototype))
return;
foreach (var jobSpecial in prototype.Special)
{
jobSpecial.AfterEquip(entity);
}
if (prototype.ID.Contains("Borg"))
{
if (!TryComp<NameIdentifierComponent>(entity, out var identifier))
return;
if (_randomizeCharacters || profile == null)
{
_metaSystem.SetEntityName(entity, $"{HumanoidCharacterProfile.GetBorgName()} {identifier.FullIdentifier}");
}
else
{
_metaSystem.SetEntityName(entity, $"{profile.BorgName} {identifier.FullIdentifier}");
}
}
if (prototype.ID.Contains("Clown"))
{
if (_randomizeCharacters || profile == null)
{
_metaSystem.SetEntityName(entity, HumanoidCharacterProfile.GetClownName());
}
else
{
_metaSystem.SetEntityName(entity, profile.ClownName);
}
}
if (prototype.ID.Contains("Mime"))
{
if (_randomizeCharacters || profile == null)
{
_metaSystem.SetEntityName(entity, HumanoidCharacterProfile.GetMimeName());
}
else
{
_metaSystem.SetEntityName(entity, profile.MimeName);
}
}
}
/// <summary>
/// Sets the ID card and PDA name, job, and access data.
/// </summary>
/// <param name="entity">Entity to load out.</param>
/// <param name="characterName">Character name to use for the ID.</param>
/// <param name="jobPrototype">Job prototype to use for the PDA and ID.</param>
/// <param name="station">The station this player is being spawned on.</param>
public void SetPdaAndIdCardData(EntityUid entity, string characterName, JobPrototype jobPrototype, EntityUid? station)
{
if (!InventorySystem.TryGetSlotEntity(entity, "id", out var idUid))
return;
var cardId = idUid.Value;
if (TryComp<PdaComponent>(idUid, out var pdaComponent) && pdaComponent.ContainedId != null)
cardId = pdaComponent.ContainedId.Value;
if (!TryComp<IdCardComponent>(cardId, out var card))
return;
_cardSystem.TryChangeFullName(cardId, characterName, card);
_cardSystem.TryChangeJobTitle(cardId, jobPrototype.LocalizedName, card);
if (_prototypeManager.TryIndex<StatusIconPrototype>(jobPrototype.Icon, out var jobIcon))
{
_cardSystem.TryChangeJobIcon(cardId, jobIcon, card);
}
var extendedAccess = false;
if (station != null)
{
var data = Comp<StationJobsComponent>(station.Value);
extendedAccess = data.ExtendedAccess;
}
_accessSystem.SetAccessToJob(cardId, jobPrototype, extendedAccess);
if (pdaComponent != null)
_pdaSystem.SetOwner(idUid.Value, pdaComponent, characterName);
}
#endregion Player spawning helpers
}
/// <summary>
/// Ordered broadcast event fired on any spawner eligible to attempt to spawn a player.
/// This event's success is measured by if SpawnResult is not null.
/// You should not make this event's success rely on random chance.
/// This event is designed to use ordered handling. You probably want SpawnPointSystem to be the last handler.
/// </summary>
[PublicAPI]
public sealed class PlayerSpawningEvent : EntityEventArgs
{
/// <summary>
/// The entity spawned, if any. You should set this if you succeed at spawning the character, and leave it alone if it's not null.
/// </summary>
public EntityUid? SpawnResult;
/// <summary>
/// The job to use, if any.
/// </summary>
public readonly JobComponent? Job;
/// <summary>
/// The profile to use, if any.
/// </summary>
public readonly HumanoidCharacterProfile? HumanoidCharacterProfile;
/// <summary>
/// The target station, if any.
/// </summary>
public readonly EntityUid? Station;
public PlayerSpawningEvent(JobComponent? job, HumanoidCharacterProfile? humanoidCharacterProfile, EntityUid? station)
{
Job = job;
HumanoidCharacterProfile = humanoidCharacterProfile;
Station = station;
}
}