ComponentInTile.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using Content.Shared.Construction;
  2. using Content.Shared.Examine;
  3. using Content.Shared.Maps;
  4. using JetBrains.Annotations;
  5. using Robust.Shared.Map;
  6. using Robust.Shared.Map.Components;
  7. using Robust.Shared.Utility;
  8. namespace Content.Server.Construction.Conditions
  9. {
  10. /// <summary>
  11. /// Makes the condition fail if any entities on a tile have (or not) a component.
  12. /// </summary>
  13. [UsedImplicitly]
  14. [DataDefinition]
  15. public sealed partial class ComponentInTile : IGraphCondition
  16. {
  17. /// <summary>
  18. /// If true, any entity on the tile must have the component.
  19. /// If false, no entity on the tile must have the component.
  20. /// </summary>
  21. [DataField("hasEntity")]
  22. public bool HasEntity { get; private set; }
  23. [DataField("examineText")]
  24. public string? ExamineText { get; private set; }
  25. [DataField("guideText")]
  26. public string? GuideText { get; private set; }
  27. [DataField("guideIcon")]
  28. public SpriteSpecifier? GuideIcon { get; private set; }
  29. /// <summary>
  30. /// The component name in question.
  31. /// </summary>
  32. [DataField("component")]
  33. public string Component { get; private set; } = string.Empty;
  34. public bool Condition(EntityUid uid, IEntityManager entityManager)
  35. {
  36. if (string.IsNullOrEmpty(Component)) return false;
  37. var type = IoCManager.Resolve<IComponentFactory>().GetRegistration(Component).Type;
  38. var transform = entityManager.GetComponent<TransformComponent>(uid);
  39. if (transform.GridUid == null)
  40. return false;
  41. var transformSys = entityManager.System<SharedTransformSystem>();
  42. var indices = transform.Coordinates.ToVector2i(entityManager, IoCManager.Resolve<IMapManager>(), transformSys);
  43. var lookup = entityManager.EntitySysManager.GetEntitySystem<EntityLookupSystem>();
  44. if (!entityManager.TryGetComponent<MapGridComponent>(transform.GridUid.Value, out var grid))
  45. return !HasEntity;
  46. if (!entityManager.System<SharedMapSystem>().TryGetTileRef(transform.GridUid.Value, grid, indices, out var tile))
  47. return !HasEntity;
  48. var entities = tile.GetEntitiesInTile(LookupFlags.Approximate | LookupFlags.Static, lookup);
  49. foreach (var ent in entities)
  50. {
  51. if (entityManager.HasComponent(ent, type))
  52. return HasEntity;
  53. }
  54. return !HasEntity;
  55. }
  56. public bool DoExamine(ExaminedEvent args)
  57. {
  58. if (string.IsNullOrEmpty(ExamineText))
  59. return false;
  60. args.PushMarkup(Loc.GetString(ExamineText));
  61. return true;
  62. }
  63. public IEnumerable<ConstructionGuideEntry> GenerateGuideEntry()
  64. {
  65. if (string.IsNullOrEmpty(GuideText))
  66. yield break;
  67. yield return new ConstructionGuideEntry()
  68. {
  69. Localization = GuideText,
  70. Icon = GuideIcon,
  71. };
  72. }
  73. }
  74. }