WarpCommand.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using System.Linq;
  2. using System.Numerics;
  3. using Content.Server.Warps;
  4. using Content.Shared.Administration;
  5. using Content.Shared.Follower;
  6. using Content.Shared.Ghost;
  7. using Robust.Shared.Console;
  8. using Robust.Shared.Enums;
  9. using Robust.Shared.Map;
  10. using Robust.Shared.Physics.Components;
  11. using Robust.Shared.Physics.Systems;
  12. namespace Content.Server.Administration.Commands
  13. {
  14. [AdminCommand(AdminFlags.Admin)]
  15. public sealed class WarpCommand : IConsoleCommand
  16. {
  17. [Dependency] private readonly IEntityManager _entManager = default!;
  18. public string Command => "warp";
  19. public string Description => "Teleports you to predefined areas on the map.";
  20. public string Help =>
  21. "warp <location>\nLocations you can teleport to are predefined by the map. " +
  22. "You can specify '?' as location to get a list of valid locations.";
  23. public void Execute(IConsoleShell shell, string argStr, string[] args)
  24. {
  25. var player = shell.Player;
  26. if (player == null)
  27. {
  28. shell.WriteLine("Only players can use this command");
  29. return;
  30. }
  31. if (args.Length != 1)
  32. {
  33. shell.WriteLine("Expected a single argument.");
  34. return;
  35. }
  36. var location = args[0];
  37. if (location == "?")
  38. {
  39. var locations = string.Join(", ", GetWarpPointNames());
  40. shell.WriteLine(locations);
  41. }
  42. else
  43. {
  44. if (player.Status != SessionStatus.InGame || player.AttachedEntity is not {Valid: true} playerEntity)
  45. {
  46. shell.WriteLine("You are not in-game!");
  47. return;
  48. }
  49. var currentMap = _entManager.GetComponent<TransformComponent>(playerEntity).MapID;
  50. var currentGrid = _entManager.GetComponent<TransformComponent>(playerEntity).GridUid;
  51. var found = GetWarpPointByName(location)
  52. .OrderBy(p => p.Item1, Comparer<EntityCoordinates>.Create((a, b) =>
  53. {
  54. // Sort so that warp points on the same grid/map are first.
  55. // So if you have two maps loaded with the same warp points,
  56. // it will prefer the warp points on the map you're currently on.
  57. var aGrid = a.GetGridUid(_entManager);
  58. var bGrid = b.GetGridUid(_entManager);
  59. if (aGrid == bGrid)
  60. {
  61. return 0;
  62. }
  63. if (aGrid == currentGrid)
  64. {
  65. return -1;
  66. }
  67. if (bGrid == currentGrid)
  68. {
  69. return 1;
  70. }
  71. var mapA = a.GetMapId(_entManager);
  72. var mapB = a.GetMapId(_entManager);
  73. if (mapA == mapB)
  74. {
  75. return 0;
  76. }
  77. if (mapA == currentMap)
  78. {
  79. return -1;
  80. }
  81. if (mapB == currentMap)
  82. {
  83. return 1;
  84. }
  85. return 0;
  86. }))
  87. .FirstOrDefault();
  88. var (coords, follow) = found;
  89. if (coords.EntityId == EntityUid.Invalid)
  90. {
  91. shell.WriteError("That location does not exist!");
  92. return;
  93. }
  94. if (follow && _entManager.HasComponent<GhostComponent>(playerEntity))
  95. {
  96. _entManager.System<FollowerSystem>().StartFollowingEntity(playerEntity, coords.EntityId);
  97. return;
  98. }
  99. var xform = _entManager.GetComponent<TransformComponent>(playerEntity);
  100. var xformSystem = _entManager.System<SharedTransformSystem>();
  101. xform.Coordinates = coords;
  102. xformSystem.AttachToGridOrMap(playerEntity, xform);
  103. if (_entManager.TryGetComponent(playerEntity, out PhysicsComponent? physics))
  104. {
  105. _entManager.System<SharedPhysicsSystem>().SetLinearVelocity(playerEntity, Vector2.Zero, body: physics);
  106. }
  107. }
  108. }
  109. private IEnumerable<string> GetWarpPointNames()
  110. {
  111. List<string> points = new(_entManager.Count<WarpPointComponent>());
  112. var query = _entManager.AllEntityQueryEnumerator<WarpPointComponent, MetaDataComponent>();
  113. while (query.MoveNext(out _, out var warp, out var meta))
  114. {
  115. points.Add(warp.Location ?? meta.EntityName);
  116. }
  117. points.Sort();
  118. return points;
  119. }
  120. private List<(EntityCoordinates, bool)> GetWarpPointByName(string name)
  121. {
  122. List<(EntityCoordinates, bool)> points = new();
  123. var query = _entManager.AllEntityQueryEnumerator<WarpPointComponent, MetaDataComponent, TransformComponent>();
  124. while (query.MoveNext(out var uid, out var warp, out var meta, out var xform))
  125. {
  126. if (name == (warp.Location ?? meta.EntityName))
  127. points.Add((xform.Coordinates, warp.Follow));
  128. }
  129. return points;
  130. }
  131. public CompletionResult GetCompletion(IConsoleShell shell, string[] args)
  132. {
  133. if (args.Length == 1)
  134. {
  135. var options = new[] { "?" }.Concat(GetWarpPointNames());
  136. return CompletionResult.FromHintOptions(options, "<warp point | ?>");
  137. }
  138. return CompletionResult.Empty;
  139. }
  140. }
  141. }