CatStation/Content.Server/Humanoid/Systems/RandomHumanoidSystem.cs
Whatstone f42f8df6d1
Cleanup: ProtoMan Part 2 - remove Client and Server EntitySystem IPrototypeManager instances (#44416)
* Remove all IPrototypeManager instances

* Remove redundant IComponentFactory instances too

* Found some stragglers

* DestructibleSystem.PrototypeManager: no dependency

* not based
2026-06-29 21:33:56 +00:00

64 lines
2.4 KiB
C#

using Content.Server.Humanoid.Components;
using Content.Server.RandomMetadata;
using Content.Shared.Body;
using Content.Shared.Humanoid.Prototypes;
using Content.Shared.Humanoid;
using Content.Shared.Preferences;
using Robust.Shared.Map;
using Robust.Shared.Serialization.Manager;
namespace Content.Server.Humanoid.Systems;
/// <summary>
/// This deals with spawning and setting up random humanoids.
/// </summary>
public sealed partial class RandomHumanoidSystem : EntitySystem
{
[Dependency] private HumanoidProfileSystem _humanoidProfile = default!;
[Dependency] private ISerializationManager _serialization = default!;
[Dependency] private MetaDataSystem _metaData = default!;
[Dependency] private SharedVisualBodySystem _visualBody = default!;
/// <inheritdoc/>
public override void Initialize()
{
SubscribeLocalEvent<RandomHumanoidSpawnerComponent, MapInitEvent>(OnMapInit,
after: new []{ typeof(RandomMetadataSystem) });
}
private void OnMapInit(EntityUid uid, RandomHumanoidSpawnerComponent component, MapInitEvent args)
{
QueueDel(uid);
if (component.SettingsPrototypeId != null)
SpawnRandomHumanoid(component.SettingsPrototypeId, Transform(uid).Coordinates, MetaData(uid).EntityName);
}
public EntityUid SpawnRandomHumanoid(string prototypeId, EntityCoordinates coordinates, string name)
{
if (!ProtoMan.TryIndex<RandomHumanoidSettingsPrototype>(prototypeId, out var prototype))
throw new ArgumentException("Could not get random humanoid settings");
var profile = HumanoidCharacterProfile.Random(prototype.SpeciesBlacklist);
var speciesProto = ProtoMan.Index<SpeciesPrototype>(profile.Species);
var humanoid = EntityManager.CreateEntityUninitialized(speciesProto.Prototype, coordinates);
_metaData.SetEntityName(humanoid, prototype.RandomizeName ? profile.Name : name);
if (prototype.Components != null)
{
foreach (var entry in prototype.Components.Values)
{
var comp = (Component)_serialization.CreateCopy(entry.Component, notNullableOverride: true);
RemComp(humanoid, comp.GetType());
AddComp(humanoid, comp);
}
}
EntityManager.InitializeAndStartEntity(humanoid);
_visualBody.ApplyProfileTo(humanoid, profile);
_humanoidProfile.ApplyProfileTo(humanoid, profile);
return humanoid;
}
}