NPCImprintingOnSpawnBehaviourSystem.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System.Numerics;
  2. using Content.Shared.NPC.Components;
  3. using Content.Shared.NPC.Systems;
  4. using Content.Shared.Whitelist;
  5. using Robust.Shared.Collections;
  6. using Robust.Shared.Map;
  7. using Robust.Shared.Random;
  8. using NPCImprintingOnSpawnBehaviourComponent = Content.Server.NPC.Components.NPCImprintingOnSpawnBehaviourComponent;
  9. namespace Content.Server.NPC.Systems;
  10. public sealed partial class NPCImprintingOnSpawnBehaviourSystem : SharedNPCImprintingOnSpawnBehaviourSystem
  11. {
  12. [Dependency] private readonly EntityLookupSystem _lookup = default!;
  13. [Dependency] private readonly NPCSystem _npc = default!;
  14. [Dependency] private readonly IRobustRandom _random = default!;
  15. [Dependency] private readonly EntityWhitelistSystem _whitelistSystem = default!;
  16. public override void Initialize()
  17. {
  18. base.Initialize();
  19. SubscribeLocalEvent<NPCImprintingOnSpawnBehaviourComponent, MapInitEvent>(OnMapInit);
  20. }
  21. private void OnMapInit(Entity<NPCImprintingOnSpawnBehaviourComponent> imprinting, ref MapInitEvent args)
  22. {
  23. HashSet<EntityUid> friends = new();
  24. _lookup.GetEntitiesInRange(imprinting, imprinting.Comp.SpawnFriendsSearchRadius, friends);
  25. foreach (var friend in friends)
  26. {
  27. if (_whitelistSystem.IsWhitelistPassOrNull(imprinting.Comp.Whitelist, friend))
  28. {
  29. AddImprintingTarget(imprinting, friend, imprinting.Comp);
  30. }
  31. }
  32. if (imprinting.Comp.Follow && imprinting.Comp.Friends.Count > 0)
  33. {
  34. var mommy = _random.Pick(imprinting.Comp.Friends);
  35. _npc.SetBlackboard(imprinting, NPCBlackboard.FollowTarget, new EntityCoordinates(mommy, Vector2.Zero));
  36. }
  37. }
  38. public void AddImprintingTarget(EntityUid entity, EntityUid friend, NPCImprintingOnSpawnBehaviourComponent component)
  39. {
  40. component.Friends.Add(friend);
  41. var exception = EnsureComp<FactionExceptionComponent>(entity);
  42. exception.Ignored.Add(friend);
  43. }
  44. }