From 99cbba7586d12bb828aab05467d7998c3981f47c Mon Sep 17 00:00:00 2001 From: Gabriel Alexandre Date: Sat, 28 Oct 2023 11:04:11 +0100 Subject: [PATCH] Migrated some small types to structs --- src/DotRecast.Detour.Dynamic/DtDynamicTile.cs | 2 +- .../Builder/SampleAreaModifications.cs | 10 +++++++++- src/DotRecast.Recast/RcAreaModification.cs | 6 +++--- src/DotRecast.Recast/RcCommons.cs | 2 +- src/DotRecast.Recast/RcCompactSpan.cs | 2 +- src/DotRecast.Recast/RcCompacts.cs | 8 ++++---- src/DotRecast.Recast/RcContours.cs | 7 +------ src/DotRecast.Recast/RcLayers.cs | 10 +--------- src/DotRecast.Recast/RcPotentialDiagonal.cs | 14 ++++++++++---- src/DotRecast.Recast/RcSweepSpan.cs | 4 ++-- 10 files changed, 33 insertions(+), 32 deletions(-) diff --git a/src/DotRecast.Detour.Dynamic/DtDynamicTile.cs b/src/DotRecast.Detour.Dynamic/DtDynamicTile.cs index 878efed..896ce12 100644 --- a/src/DotRecast.Detour.Dynamic/DtDynamicTile.cs +++ b/src/DotRecast.Detour.Dynamic/DtDynamicTile.cs @@ -99,7 +99,7 @@ namespace DotRecast.Detour.Dynamic config.maxEdgeLen, config.maxSimplificationError, Math.Min(DtDynamicNavMesh.MAX_VERTS_PER_POLY, config.vertsPerPoly), config.detailSampleDistance, config.detailSampleMaxError, - true, true, true, null, true); + true, true, true, default, true); RcBuilderResult r = builder.Build(vt.tileX, vt.tileZ, null, rcConfig, heightfield, telemetry); if (config.keepIntermediateResults) { diff --git a/src/DotRecast.Recast.Toolset/Builder/SampleAreaModifications.cs b/src/DotRecast.Recast.Toolset/Builder/SampleAreaModifications.cs index 510f3b5..1ed8bc7 100644 --- a/src/DotRecast.Recast.Toolset/Builder/SampleAreaModifications.cs +++ b/src/DotRecast.Recast.Toolset/Builder/SampleAreaModifications.cs @@ -61,7 +61,15 @@ namespace DotRecast.Recast.Toolset.Builder public static RcAreaModification OfValue(int value) { - return Values.FirstOrDefault(x => x.Value == value) ?? SAMPLE_AREAMOD_GRASS; + foreach(var v in Values) + { + if(v.Value == value) + { + return v; + } + } + + return SAMPLE_AREAMOD_GRASS; } } } \ No newline at end of file diff --git a/src/DotRecast.Recast/RcAreaModification.cs b/src/DotRecast.Recast/RcAreaModification.cs index a9946c7..00df1f5 100644 --- a/src/DotRecast.Recast/RcAreaModification.cs +++ b/src/DotRecast.Recast/RcAreaModification.cs @@ -20,7 +20,7 @@ freely, subject to the following restrictions: namespace DotRecast.Recast { - public class RcAreaModification + public readonly struct RcAreaModification { public const int RC_AREA_FLAGS_MASK = 0x3F; @@ -58,12 +58,12 @@ namespace DotRecast.Recast Mask = other.Mask; } - public int GetMaskedValue() + public readonly int GetMaskedValue() { return Value & Mask; } - public int Apply(int area) + public readonly int Apply(int area) { return ((Value & Mask) | (area & ~Mask)); } diff --git a/src/DotRecast.Recast/RcCommons.cs b/src/DotRecast.Recast/RcCommons.cs index 93ba642..1cb3373 100644 --- a/src/DotRecast.Recast/RcCommons.cs +++ b/src/DotRecast.Recast/RcCommons.cs @@ -36,7 +36,7 @@ namespace DotRecast.Recast /// @param[in] span The span to update. /// @param[in] direction The direction to set. [Limits: 0 <= value < 4] /// @param[in] neighborIndex The index of the neighbor span. - public static void SetCon(RcCompactSpan span, int direction, int neighborIndex) + public static void SetCon(ref RcCompactSpan span, int direction, int neighborIndex) { int shift = direction * 6; int con = span.con; diff --git a/src/DotRecast.Recast/RcCompactSpan.cs b/src/DotRecast.Recast/RcCompactSpan.cs index de1e55d..066ab8e 100644 --- a/src/DotRecast.Recast/RcCompactSpan.cs +++ b/src/DotRecast.Recast/RcCompactSpan.cs @@ -21,7 +21,7 @@ freely, subject to the following restrictions: namespace DotRecast.Recast { /** Represents a span of unobstructed space within a compact heightfield. */ - public class RcCompactSpan + public struct RcCompactSpan { /** The lower extent of the span. (Measured from the heightfield's base.) */ public int y; diff --git a/src/DotRecast.Recast/RcCompacts.cs b/src/DotRecast.Recast/RcCompacts.cs index d53eae0..a1cfc82 100644 --- a/src/DotRecast.Recast/RcCompacts.cs +++ b/src/DotRecast.Recast/RcCompacts.cs @@ -115,11 +115,11 @@ namespace DotRecast.Recast RcCompactCell c = chf.cells[x + y * w]; for (int i = c.index, ni = c.index + c.count; i < ni; ++i) { - RcCompactSpan s = chf.spans[i]; + ref RcCompactSpan s = ref chf.spans[i]; for (int dir = 0; dir < 4; ++dir) { - SetCon(s, dir, RC_NOT_CONNECTED); + SetCon(ref s, dir, RC_NOT_CONNECTED); int nx = x + GetDirOffsetX(dir); int ny = y + GetDirOffsetY(dir); // First check that the neighbour cell is in bounds. @@ -131,7 +131,7 @@ namespace DotRecast.Recast RcCompactCell nc = chf.cells[nx + ny * w]; for (int k = nc.index, nk = nc.index + nc.count; k < nk; ++k) { - RcCompactSpan ns = chf.spans[k]; + ref RcCompactSpan ns = ref chf.spans[k]; int bot = Math.Max(s.y, ns.y); int top = Math.Min(s.y + s.h, ns.y + ns.h); @@ -147,7 +147,7 @@ namespace DotRecast.Recast continue; } - SetCon(s, dir, lidx); + SetCon(ref s, dir, lidx); break; } } diff --git a/src/DotRecast.Recast/RcContours.cs b/src/DotRecast.Recast/RcContours.cs index 1ccffc8..055e168 100644 --- a/src/DotRecast.Recast/RcContours.cs +++ b/src/DotRecast.Recast/RcContours.cs @@ -632,10 +632,6 @@ namespace DotRecast.Recast maxVerts += region.holes[i].contour.nverts; RcPotentialDiagonal[] diags = new RcPotentialDiagonal[maxVerts]; - for (int pd = 0; pd < maxVerts; pd++) - { - diags[pd] = new RcPotentialDiagonal(); - } RcContour outline = region.outline; @@ -664,8 +660,7 @@ namespace DotRecast.Recast { int dx = outline.verts[j * 4 + 0] - hole.verts[corner + 0]; int dz = outline.verts[j * 4 + 2] - hole.verts[corner + 2]; - diags[ndiags].vert = j; - diags[ndiags].dist = dx * dx + dz * dz; + diags[ndiags] = new RcPotentialDiagonal(j, dx * dx + dz * dz); ndiags++; } } diff --git a/src/DotRecast.Recast/RcLayers.cs b/src/DotRecast.Recast/RcLayers.cs index 1073794..6ffd8d5 100644 --- a/src/DotRecast.Recast/RcLayers.cs +++ b/src/DotRecast.Recast/RcLayers.cs @@ -30,10 +30,6 @@ namespace DotRecast.Recast public static class RcLayers { - const int RC_MAX_LAYERS = RcConstants.RC_NOT_CONNECTED; - const int RC_MAX_NEIS = 16; - - private static void AddUnique(List a, int v) { if (!a.Contains(v)) @@ -63,10 +59,6 @@ namespace DotRecast.Recast Array.Fill(srcReg, 0xFF); int nsweeps = chf.width; // Math.Max(chf.width, chf.height); RcSweepSpan[] sweeps = new RcSweepSpan[nsweeps]; - for (int i = 0; i < sweeps.Length; i++) - { - sweeps[i] = new RcSweepSpan(); - } // Partition walkable area into monotone regions. int[] prevCount = new int[256]; @@ -305,7 +297,7 @@ namespace DotRecast.Recast int newId = ri.layerId; - for (;;) + while (true) { int oldId = 0xff; diff --git a/src/DotRecast.Recast/RcPotentialDiagonal.cs b/src/DotRecast.Recast/RcPotentialDiagonal.cs index 8b04328..95b0699 100644 --- a/src/DotRecast.Recast/RcPotentialDiagonal.cs +++ b/src/DotRecast.Recast/RcPotentialDiagonal.cs @@ -1,8 +1,14 @@ -namespace DotRecast.Recast +namespace DotRecast.Recast { - public class RcPotentialDiagonal + public readonly struct RcPotentialDiagonal { - public int dist; - public int vert; + public readonly int vert; + public readonly int dist; + + public RcPotentialDiagonal(int vert, int dist) + { + this.vert = vert; + this.dist = dist; + } } } \ No newline at end of file diff --git a/src/DotRecast.Recast/RcSweepSpan.cs b/src/DotRecast.Recast/RcSweepSpan.cs index 4dcb80b..fcc87cb 100644 --- a/src/DotRecast.Recast/RcSweepSpan.cs +++ b/src/DotRecast.Recast/RcSweepSpan.cs @@ -1,6 +1,6 @@ -namespace DotRecast.Recast +namespace DotRecast.Recast { - public class RcSweepSpan + public struct RcSweepSpan { public int rid; // row id public int id; // region id