RandomHumanoidSystem.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using Content.Server.Humanoid.Components;
  2. using Content.Server.RandomMetadata;
  3. using Content.Shared.Humanoid.Prototypes;
  4. using Content.Shared.Preferences;
  5. using Robust.Shared.Map;
  6. using Robust.Shared.Prototypes;
  7. using Robust.Shared.Serialization.Manager;
  8. namespace Content.Server.Humanoid.Systems;
  9. /// <summary>
  10. /// This deals with spawning and setting up random humanoids.
  11. /// </summary>
  12. public sealed class RandomHumanoidSystem : EntitySystem
  13. {
  14. [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
  15. [Dependency] private readonly ISerializationManager _serialization = default!;
  16. [Dependency] private readonly MetaDataSystem _metaData = default!;
  17. [Dependency] private readonly HumanoidAppearanceSystem _humanoid = default!;
  18. /// <inheritdoc/>
  19. public override void Initialize()
  20. {
  21. SubscribeLocalEvent<RandomHumanoidSpawnerComponent, MapInitEvent>(OnMapInit,
  22. after: new []{ typeof(RandomMetadataSystem) });
  23. }
  24. private void OnMapInit(EntityUid uid, RandomHumanoidSpawnerComponent component, MapInitEvent args)
  25. {
  26. QueueDel(uid);
  27. if (component.SettingsPrototypeId != null)
  28. SpawnRandomHumanoid(component.SettingsPrototypeId, Transform(uid).Coordinates, MetaData(uid).EntityName);
  29. }
  30. public EntityUid SpawnRandomHumanoid(string prototypeId, EntityCoordinates coordinates, string name)
  31. {
  32. if (!_prototypeManager.TryIndex<RandomHumanoidSettingsPrototype>(prototypeId, out var prototype))
  33. throw new ArgumentException("Could not get random humanoid settings");
  34. var profile = HumanoidCharacterProfile.Random(prototype.SpeciesBlacklist);
  35. var speciesProto = _prototypeManager.Index<SpeciesPrototype>(profile.Species);
  36. var humanoid = EntityManager.CreateEntityUninitialized(speciesProto.Prototype, coordinates);
  37. _metaData.SetEntityName(humanoid, prototype.RandomizeName ? profile.Name : name);
  38. _humanoid.LoadProfile(humanoid, profile);
  39. if (prototype.Components != null)
  40. {
  41. foreach (var entry in prototype.Components.Values)
  42. {
  43. var comp = (Component)_serialization.CreateCopy(entry.Component, notNullableOverride: true);
  44. EntityManager.RemoveComponent(humanoid, comp.GetType());
  45. EntityManager.AddComponent(humanoid, comp);
  46. }
  47. }
  48. EntityManager.InitializeAndStartEntity(humanoid);
  49. return humanoid;
  50. }
  51. }