| | | 1 | | using FixedMathSharp; |
| | | 2 | | using MemoryPack; |
| | | 3 | | using SwiftCollections; |
| | | 4 | | using System; |
| | | 5 | | using System.Text.Json.Serialization; |
| | | 6 | | |
| | | 7 | | namespace GridForge.Configuration; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Defines the configuration parameters for a grid, including boundaries and scan cell size. |
| | | 11 | | /// Used to describe grid properties before a world normalizes and registers the grid. |
| | | 12 | | /// </summary> |
| | | 13 | | [Serializable] |
| | | 14 | | [MemoryPackable] |
| | | 15 | | public readonly partial struct GridConfiguration |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// The default size of each scan cell. |
| | | 19 | | /// </summary> |
| | | 20 | | public const int DefaultScanCellSize = 8; |
| | | 21 | | |
| | | 22 | | #region Properties |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// The minimum boundary of the grid in world coordinates. |
| | | 26 | | /// </summary> |
| | | 27 | | [JsonInclude] |
| | | 28 | | [MemoryPackInclude] |
| | | 29 | | public readonly Vector3d BoundsMin; |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// The maximum boundary of the grid in world coordinates. |
| | | 33 | | /// </summary> |
| | | 34 | | [JsonInclude] |
| | | 35 | | [MemoryPackInclude] |
| | | 36 | | public readonly Vector3d BoundsMax; |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// The size of each scan cell, determining the granularity of spatial partitioning. |
| | | 40 | | /// Customizable based on grid density and expected entity distribution. |
| | | 41 | | /// </summary> |
| | | 42 | | [JsonInclude] |
| | | 43 | | [MemoryPackInclude] |
| | | 44 | | public readonly int ScanCellSize; |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// The center point of the grid's bounding volume. |
| | | 48 | | /// </summary> |
| | | 49 | | [JsonIgnore] |
| | | 50 | | [MemoryPackIgnore] |
| | 87 | 51 | | public readonly Vector3d GridCenter => (BoundsMin + BoundsMax) * Fixed64.Half; |
| | | 52 | | |
| | | 53 | | #endregion |
| | | 54 | | |
| | | 55 | | #region Constructors |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Initializes a new instance of <see cref="GridConfiguration"/> with specified bounds and scan cell size. |
| | | 59 | | /// Ensures that <see cref="BoundsMin"/> is always less than or equal to <see cref="BoundsMax"/>. |
| | | 60 | | /// </summary> |
| | | 61 | | /// <param name="boundsMin">The minimum boundary of the grid.</param> |
| | | 62 | | /// <param name="boundsMax">The maximum boundary of the grid.</param> |
| | | 63 | | /// <param name="scanCellSize">The size of scan cells within the grid. Default is 8.</param> |
| | | 64 | | [JsonConstructor] |
| | | 65 | | public GridConfiguration( |
| | | 66 | | Vector3d boundsMin, |
| | | 67 | | Vector3d boundsMax, |
| | | 68 | | int scanCellSize = DefaultScanCellSize) |
| | | 69 | | { |
| | 369 | 70 | | if (boundsMin > boundsMax) |
| | 2 | 71 | | GridForgeLogger.Channel.Warn($"GridMin was greater than GridMax, auto-correcting values."); |
| | | 72 | | |
| | 369 | 73 | | BoundsMin = new Vector3d( |
| | 369 | 74 | | FixedMath.Min(boundsMin.x, boundsMax.x), |
| | 369 | 75 | | FixedMath.Min(boundsMin.y, boundsMax.y), |
| | 369 | 76 | | FixedMath.Min(boundsMin.z, boundsMax.z)); |
| | 369 | 77 | | BoundsMax = new Vector3d( |
| | 369 | 78 | | FixedMath.Max(boundsMin.x, boundsMax.x), |
| | 369 | 79 | | FixedMath.Max(boundsMin.y, boundsMax.y), |
| | 369 | 80 | | FixedMath.Max(boundsMin.z, boundsMax.z)); |
| | | 81 | | |
| | 369 | 82 | | ScanCellSize = scanCellSize > 0 ? scanCellSize : DefaultScanCellSize; |
| | 369 | 83 | | } |
| | | 84 | | |
| | | 85 | | #endregion |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Creates an exact identity key for this configuration's snapped bounds. |
| | | 89 | | /// </summary> |
| | 199 | 90 | | public readonly BoundsKey ToBoundsKey() => new(BoundsMin, BoundsMax); |
| | | 91 | | |
| | | 92 | | /// <inheritdoc/> |
| | | 93 | | public override readonly int GetHashCode() => |
| | 2 | 94 | | SwiftHashTools.CombineHashCodes(BoundsMin.GetHashCode(), BoundsMax.GetHashCode()); |
| | | 95 | | } |