From b75537d0b1b9ef9713064cf3bf8842d59154f6f9 Mon Sep 17 00:00:00 2001 From: ikpil Date: Sat, 10 Jun 2023 13:48:00 +0900 Subject: [PATCH] class DtStatus => readonly struct DtStatus --- src/DotRecast.Detour.Crowd/DtCrowd.cs | 4 ++-- src/DotRecast.Detour.Crowd/DtPathQuery.cs | 6 ++---- src/DotRecast.Detour.Crowd/DtPathQueue.cs | 3 +-- src/DotRecast.Detour/DtStatus.cs | 23 +++++++++++++++++++-- src/DotRecast.Detour/QueryResults/Result.cs | 4 ++-- test/DotRecast.Detour.Test/FindPathTest.cs | 2 +- 6 files changed, 29 insertions(+), 13 deletions(-) diff --git a/src/DotRecast.Detour.Crowd/DtCrowd.cs b/src/DotRecast.Detour.Crowd/DtCrowd.cs index fd4c621..64ac1a4 100644 --- a/src/DotRecast.Detour.Crowd/DtCrowd.cs +++ b/src/DotRecast.Detour.Crowd/DtCrowd.cs @@ -698,7 +698,7 @@ namespace DotRecast.Detour.Crowd // _telemetry.RecordPathWaitTime(ag.targetReplanTime); // Poll path queue. DtStatus status = ag.targetPathQueryResult.status; - if (status != null && status.IsFailed()) + if (status.IsFailed()) { // Path find failed, retry if the target location is still // valid. @@ -714,7 +714,7 @@ namespace DotRecast.Detour.Crowd ag.targetReplanTime = 0; } - else if (status != null && status.IsSuccess()) + else if (status.IsSuccess()) { List path = ag.corridor.GetPath(); if (0 == path.Count) diff --git a/src/DotRecast.Detour.Crowd/DtPathQuery.cs b/src/DotRecast.Detour.Crowd/DtPathQuery.cs index 6106ba5..2bc9714 100644 --- a/src/DotRecast.Detour.Crowd/DtPathQuery.cs +++ b/src/DotRecast.Detour.Crowd/DtPathQuery.cs @@ -26,14 +26,12 @@ namespace DotRecast.Detour.Crowd { /// Path find start and end location. public RcVec3f startPos = new RcVec3f(); - public RcVec3f endPos = new RcVec3f(); public long startRef; public long endRef; - public IDtQueryFilter filter; - - /// < TODO: This is potentially dangerous! + public readonly DtPathQueryResult result = new DtPathQueryResult(); + public IDtQueryFilter filter; // < TODO: This is potentially dangerous! public DtNavMeshQuery navQuery; } diff --git a/src/DotRecast.Detour.Crowd/DtPathQueue.cs b/src/DotRecast.Detour.Crowd/DtPathQueue.cs index 1f309db..d9f2d0b 100644 --- a/src/DotRecast.Detour.Crowd/DtPathQueue.cs +++ b/src/DotRecast.Detour.Crowd/DtPathQueue.cs @@ -49,7 +49,7 @@ namespace DotRecast.Detour.Crowd queue.RemoveFirst(); // Handle query start. - if (q.result.status == null) + if (q.result.status.IsEmpty()) { q.navQuery = new DtNavMeshQuery(navMesh); q.result.status = q.navQuery.InitSlicedFindPath(q.startRef, q.endRef, q.startPos, q.endPos, q.filter, 0); @@ -89,7 +89,6 @@ namespace DotRecast.Detour.Crowd q.startRef = startRef; q.endPos = endPos; q.endRef = endRef; - q.result.status = null; q.filter = filter; queue.AddLast(q); return q.result; diff --git a/src/DotRecast.Detour/DtStatus.cs b/src/DotRecast.Detour/DtStatus.cs index 63a2797..890c5cc 100644 --- a/src/DotRecast.Detour/DtStatus.cs +++ b/src/DotRecast.Detour/DtStatus.cs @@ -22,8 +22,10 @@ using System.Runtime.CompilerServices; namespace DotRecast.Detour { - public class DtStatus + public readonly struct DtStatus { + public static readonly DtStatus Empty = new DtStatus(0); + // High level status. public static readonly DtStatus DT_FAILURE = new DtStatus(1u << 31); // Operation failed. public static readonly DtStatus DT_SUCCSESS = new DtStatus(1u << 30); // Operation succeed. @@ -40,7 +42,6 @@ namespace DotRecast.Detour public static readonly DtStatus DT_PARTIAL_RESULT = new DtStatus(1 << 6); // Query did not reach the end location, returning best guess. public static readonly DtStatus DT_ALREADY_OCCUPIED = new DtStatus(1 << 7); // A tile has already been assigned to the given x,y coordinate - public readonly uint Value; private DtStatus(uint value) @@ -48,6 +49,12 @@ namespace DotRecast.Detour Value = value; } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public bool IsEmpty() + { + return 0 == Value; + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool IsSuccess() { @@ -71,5 +78,17 @@ namespace DotRecast.Detour { return 0 != (Value & DT_PARTIAL_RESULT.Value); } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static DtStatus operator |(DtStatus left, DtStatus right) + { + return new DtStatus(left.Value | right.Value); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static DtStatus operator &(DtStatus left, DtStatus right) + { + return new DtStatus(left.Value & right.Value); + } } } \ No newline at end of file diff --git a/src/DotRecast.Detour/QueryResults/Result.cs b/src/DotRecast.Detour/QueryResults/Result.cs index f0a1bcd..598724b 100644 --- a/src/DotRecast.Detour/QueryResults/Result.cs +++ b/src/DotRecast.Detour/QueryResults/Result.cs @@ -34,7 +34,7 @@ namespace DotRecast.Detour.QueryResults public static Result InvalidParam() { - return new Result(default, DtStatus.DT_INVALID_PARAM, null); + return new Result(default, DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM, null); } public static Result Failure(string message) @@ -44,7 +44,7 @@ namespace DotRecast.Detour.QueryResults public static Result InvalidParam(string message) { - return new Result(default, DtStatus.DT_INVALID_PARAM, message); + return new Result(default, DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM, message); } public static Result Failure(T result) diff --git a/test/DotRecast.Detour.Test/FindPathTest.cs b/test/DotRecast.Detour.Test/FindPathTest.cs index 325d782..da0acca 100644 --- a/test/DotRecast.Detour.Test/FindPathTest.cs +++ b/test/DotRecast.Detour.Test/FindPathTest.cs @@ -160,7 +160,7 @@ public class FindPathTest : AbstractDetourTest var endPos = endPoss[i]; query.InitSlicedFindPath(startRef, endRef, startPos, endPos, filter, DtNavMeshQuery.DT_FINDPATH_ANY_ANGLE); DtStatus status = DtStatus.DT_IN_PROGRESS; - while (status == DtStatus.DT_IN_PROGRESS) + while (status.IsInProgress()) { Result res = query.UpdateSlicedFindPath(10); status = res.status;