1
0

TabletopSystem.Map.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System.Numerics;
  2. using Content.Shared.GameTicking;
  3. using Robust.Shared.Map;
  4. using Robust.Shared.Map.Components;
  5. namespace Content.Server.Tabletop
  6. {
  7. public sealed partial class TabletopSystem
  8. {
  9. /// <summary>
  10. /// Separation between tabletops in the tabletop map.
  11. /// </summary>
  12. private const int TabletopSeparation = 100;
  13. /// <summary>
  14. /// Map where all tabletops reside.
  15. /// </summary>
  16. public MapId TabletopMap { get; private set; } = MapId.Nullspace;
  17. /// <summary>
  18. /// The number of tabletops created in the map.
  19. /// Used for calculating the position of the next one.
  20. /// </summary>
  21. private int _tabletops = 0;
  22. /// <summary>
  23. /// Despite the name, this method is only used to subscribe to events.
  24. /// </summary>
  25. private void InitializeMap()
  26. {
  27. SubscribeLocalEvent<RoundRestartCleanupEvent>(OnRoundRestart);
  28. }
  29. /// <summary>
  30. /// Gets the next available position for a tabletop, and increments the tabletop count.
  31. /// </summary>
  32. /// <returns></returns>
  33. private Vector2 GetNextTabletopPosition()
  34. {
  35. return UlamSpiral(_tabletops++) * TabletopSeparation;
  36. }
  37. /// <summary>
  38. /// Ensures that the tabletop map exists. Creates it if it doesn't.
  39. /// </summary>
  40. private void EnsureTabletopMap()
  41. {
  42. if (TabletopMap != MapId.Nullspace && _mapManager.MapExists(TabletopMap))
  43. return;
  44. TabletopMap = _mapManager.CreateMap();
  45. _tabletops = 0;
  46. var mapUid = _mapManager.GetMapEntityId(TabletopMap);
  47. var mapComp = EntityManager.GetComponent<MapComponent>(mapUid);
  48. // Lighting is always disabled in tabletop world.
  49. mapComp.LightingEnabled = false;
  50. Dirty(mapUid, mapComp);
  51. }
  52. /// <summary>
  53. /// Algorithm for mapping scalars to 2D positions in the same pattern as an Ulam Spiral.
  54. /// </summary>
  55. /// <param name="n">Scalar to map to a 2D position.</param>
  56. /// <returns>The mapped 2D position for the scalar.</returns>
  57. private Vector2i UlamSpiral(int n)
  58. {
  59. var k = (int)MathF.Ceiling(MathF.Sqrt(n) - 1) / 2;
  60. var t = 2 * k + 1;
  61. var m = (int)MathF.Pow(t, 2);
  62. t--;
  63. if (n >= m - t)
  64. return new Vector2i(k - (m - n), -k);
  65. m -= t;
  66. if (n >= m - t)
  67. return new Vector2i(-k, -k + (m - n));
  68. m -= t;
  69. if (n >= m - t)
  70. return new Vector2i(-k + (m - n), k);
  71. return new Vector2i(k, k - (m - n - t));
  72. }
  73. private void OnRoundRestart(RoundRestartCleanupEvent _)
  74. {
  75. if (TabletopMap == MapId.Nullspace || !_mapManager.MapExists(TabletopMap))
  76. return;
  77. // This will usually *not* be the case, but better make sure.
  78. _mapManager.DeleteMap(TabletopMap);
  79. // Reset tabletop count.
  80. _tabletops = 0;
  81. }
  82. }
  83. }