using System.Numerics;
using Content.Server.Worldgen.Components;
using JetBrains.Annotations;
namespace Content.Server.Worldgen.Systems;
///
/// This provides some additional functions for world generation systems.
/// Exists primarily for convenience and to avoid code duplication.
///
[PublicAPI]
public abstract class BaseWorldSystem : EntitySystem
{
[Dependency] private readonly WorldControllerSystem _worldController = default!;
[Dependency] private readonly SharedTransformSystem _transformSystem = default!;
///
/// Gets a chunk's coordinates in chunk space as an integer value.
///
///
///
/// Chunk space coordinates
[Pure]
public Vector2i GetChunkCoords(EntityUid ent, TransformComponent? xform = null)
{
if (!Resolve(ent, ref xform))
throw new Exception("Failed to resolve transform, somehow.");
return WorldGen.WorldToChunkCoords(_transformSystem.GetWorldPosition(xform)).Floored();
}
///
/// Gets a chunk's coordinates in chunk space as a floating point value.
///
///
///
/// Chunk space coordinates
[Pure]
public Vector2 GetFloatingChunkCoords(EntityUid ent, TransformComponent? xform = null)
{
if (!Resolve(ent, ref xform))
throw new Exception("Failed to resolve transform, somehow.");
return WorldGen.WorldToChunkCoords(_transformSystem.GetWorldPosition(xform));
}
///
/// Attempts to get a chunk, creating it if it doesn't exist.
///
/// Chunk coordinates to get the chunk entity for.
/// Map the chunk is in.
/// The controller this chunk belongs to.
/// A chunk, if available.
[Pure]
public EntityUid? GetOrCreateChunk(Vector2i chunk, EntityUid map, WorldControllerComponent? controller = null)
{
return _worldController.GetOrCreateChunk(chunk, map, controller);
}
}