BaseWorldSystem.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System.Numerics;
  2. using Content.Server.Worldgen.Components;
  3. using JetBrains.Annotations;
  4. namespace Content.Server.Worldgen.Systems;
  5. /// <summary>
  6. /// This provides some additional functions for world generation systems.
  7. /// Exists primarily for convenience and to avoid code duplication.
  8. /// </summary>
  9. [PublicAPI]
  10. public abstract class BaseWorldSystem : EntitySystem
  11. {
  12. [Dependency] private readonly WorldControllerSystem _worldController = default!;
  13. [Dependency] private readonly SharedTransformSystem _transformSystem = default!;
  14. /// <summary>
  15. /// Gets a chunk's coordinates in chunk space as an integer value.
  16. /// </summary>
  17. /// <param name="ent"></param>
  18. /// <param name="xform"></param>
  19. /// <returns>Chunk space coordinates</returns>
  20. [Pure]
  21. public Vector2i GetChunkCoords(EntityUid ent, TransformComponent? xform = null)
  22. {
  23. if (!Resolve(ent, ref xform))
  24. throw new Exception("Failed to resolve transform, somehow.");
  25. return WorldGen.WorldToChunkCoords(_transformSystem.GetWorldPosition(xform)).Floored();
  26. }
  27. /// <summary>
  28. /// Gets a chunk's coordinates in chunk space as a floating point value.
  29. /// </summary>
  30. /// <param name="ent"></param>
  31. /// <param name="xform"></param>
  32. /// <returns>Chunk space coordinates</returns>
  33. [Pure]
  34. public Vector2 GetFloatingChunkCoords(EntityUid ent, TransformComponent? xform = null)
  35. {
  36. if (!Resolve(ent, ref xform))
  37. throw new Exception("Failed to resolve transform, somehow.");
  38. return WorldGen.WorldToChunkCoords(_transformSystem.GetWorldPosition(xform));
  39. }
  40. /// <summary>
  41. /// Attempts to get a chunk, creating it if it doesn't exist.
  42. /// </summary>
  43. /// <param name="chunk">Chunk coordinates to get the chunk entity for.</param>
  44. /// <param name="map">Map the chunk is in.</param>
  45. /// <param name="controller">The controller this chunk belongs to.</param>
  46. /// <returns>A chunk, if available.</returns>
  47. [Pure]
  48. public EntityUid? GetOrCreateChunk(Vector2i chunk, EntityUid map, WorldControllerComponent? controller = null)
  49. {
  50. return _worldController.GetOrCreateChunk(chunk, map, controller);
  51. }
  52. }