Beispiel #1
0
        public bool PassableForMod(Vector3 end)
        {
            if (mNavMeshQuery == null)
            {
                return(false);
            }

            long endRef = 0;

            float[] nearestPt = new float[3];
            float[] endPos    = new float[] { end.X, end.Y, end.Z };

            try
            {
                Detour.Status status = mNavMeshQuery.FindNearestPoly(endPos, new[] { 0.2f, 10f, 0.2f }, mQueryFilter, ref endRef, ref nearestPt);
                if (status == Detour.Status.Success)
                {
                    return(mNavMeshQuery.IsValidPolyRef(endRef, mQueryFilter));
                }
            }
            catch (System.Exception ex)
            {
                Log.Log.Server.Print(ex.ToString());
                Log.Log.Server.Print(ex.StackTrace.ToString());
            }

            return(false);
        }
Beispiel #2
0
        public bool FindRandomPoint(Vector3 start, Vector3 center, float radius, ref Vector3 point)
        {
            if (mNavMeshQuery == null)
            {
                return(false);
            }

            long startRef  = 0;
            long randomRef = 0;

            float[] nearestPt = new float[3];
            float[] randomPt  = new float[3];
            float[] startPos  = new float[] { start.X, start.Y, start.Z };
            float[] centerPos = new float[] { center.X, center.Y, center.Z };

            Detour.Status status = mNavMeshQuery.FindNearestPoly(startPos, new[] { 0.2f, 10f, 0.2f }, mQueryFilter, ref startRef, ref nearestPt);
            status = mNavMeshQuery.FindRandomPointAroundCircle(startRef, centerPos, radius, mQueryFilter, Rand, ref randomRef, ref randomPt);
            if (status == Detour.Status.Success)
            {
                point = new Vector3(randomPt[0], randomPt[1], randomPt[2]);
                return(true);
            }

            return(false);
        }
Beispiel #3
0
        public bool FindPath(Vector3 start, Vector3 end, out NavpointList nodes)
        {
            nodes = new NavpointList();
            if (mNavMeshQuery == null)
            {
                return(false);
            }

            long startRef = 0, endRef = 0;
            int  maxPolys = 256;

            float[] nearestPt = new float[3];
            float[] startPos  = new float[] { start.X, 0, start.Z };
            float[] endPos    = new float[] { end.X, 0, end.Z };

            Detour.Status status = mNavMeshQuery.FindNearestPoly(startPos, new[] { 0.2f, 10f, 0.2f }, mQueryFilter, ref startRef, ref nearestPt);
            status = mNavMeshQuery.FindNearestPoly(endPos, new[] { 0.2f, 10f, 0.2f }, mQueryFilter, ref endRef, ref nearestPt);

            long[] polys     = new long[maxPolys];
            int    polyCount = 0;

            try
            {
                status = mNavMeshQuery.FindPath(startRef, endRef, startPos, endPos, mQueryFilter, ref polys, ref polyCount, maxPolys);
            }
            catch (System.Exception ex)
            {
                Log.Log.Server.Print(ex.ToString());
                Log.Log.Server.Print(ex.StackTrace.ToString());
            }
            if (status != Detour.Status.Success || polyCount == 0)
            {
                return(false);
            }

            long[] smoothPolys = new long[maxPolys];
            System.Array.Copy(polys, smoothPolys, polyCount);
            int smoothPolyCount = polyCount;

            float[] iterPos = new float[3], targetPos = new float[3];

            mNavMeshQuery.ClosestPointOnPoly(startRef, startPos, ref iterPos);
            mNavMeshQuery.ClosestPointOnPoly(smoothPolys[smoothPolyCount - 1], endPos, ref targetPos);

            float StepSize = 0.5f;
            float Slop     = 0.01f;

            int maxSmooth = 683;

            nodes.AddLast(new Vector3(iterPos[0], iterPos[1], iterPos[2]));

            while (smoothPolyCount > 0 && nodes.Count < maxSmooth)
            {
                float[] steerPos     = new float[3];
                short   steerPosFlag = 0;
                long    steerPosRef  = 0;

                if (!GetSteerTarget(mNavMeshQuery, iterPos, targetPos, Slop, smoothPolys, smoothPolyCount, ref steerPos, ref steerPosFlag, ref steerPosRef))
                {
                    break;
                }

                bool endOfPath         = (steerPosFlag & mStraightPathEnd) != 0;
                bool offMeshConnection = (steerPosFlag & mStraightPathOffMeshConnection) != 0;

                float[] delta = Helper.VSub(steerPos[0], steerPos[1], steerPos[2], iterPos[0], iterPos[1], iterPos[2]);
                float   len   = (float)System.Math.Sqrt(Helper.VDot(delta, delta));

                if ((endOfPath || offMeshConnection) && len < StepSize)
                {
                    len = 1;
                }
                else
                {
                    len = StepSize / len;
                }

                float[] moveTarget = new float[3];
                Helper.VMad(ref moveTarget, iterPos, delta, len);

                float[] result   = new float[3];
                long[]  visited  = new long[16];
                int     nVisited = 0;

                mNavMeshQuery.MoveAlongSurface(smoothPolys[0], iterPos, moveTarget, mQueryFilter, ref result, ref visited, ref nVisited, 16);
                smoothPolyCount = FixupCorridor(ref smoothPolys, smoothPolyCount, maxPolys, visited, nVisited);
                float h = 0;
                mNavMeshQuery.GetPolyHeight(smoothPolys[0], result, ref h);
                result[1] = h;
                System.Array.Copy(result, iterPos, 3);

                if (endOfPath && InRange(iterPos, steerPos, Slop, 1.0f))
                {
                    System.Array.Copy(targetPos, iterPos, 3);
                    if (nodes.Count < maxSmooth)
                    {
                        nodes.AddLast(new Vector3(iterPos[0], iterPos[1], iterPos[2]));
                    }

                    break;
                }
                else if (offMeshConnection && InRange(iterPos, steerPos, Slop, 1.0f))
                {
                    float[] startPosOffMesh = new float[3], endPosOffMesh = new float[3];
                    long    prevRef = 0, polyRef = smoothPolys[0];
                    int     npos = 0;
                    while (npos < smoothPolyCount && polyRef != steerPosRef)
                    {
                        prevRef = polyRef;
                        polyRef = smoothPolys[npos];
                        npos++;
                    }
                    for (int i = npos; i < smoothPolyCount; i++)
                    {
                        smoothPolys[i - npos] = smoothPolys[i];
                    }
                    smoothPolyCount -= npos;

                    status = mNavMeshQuery.NavMesh.GetOffMeshConnectionPolyEndPoints(prevRef, polyRef, ref startPosOffMesh, ref endPosOffMesh);
                    if ((status & Status.Success) != 0)
                    {
                        if (nodes.Count < maxSmooth)
                        {
                            nodes.AddLast(new Vector3(startPosOffMesh[0], startPosOffMesh[1], startPosOffMesh[2]));

                            if ((nodes.Count & 1) == 1)
                            {
                                nodes.AddLast(new Vector3(startPosOffMesh[0], startPosOffMesh[1], startPosOffMesh[2]));
                            }
                        }
                        System.Array.Copy(endPosOffMesh, iterPos, 3);
                        float eh = 0.0f;
                        mNavMeshQuery.GetPolyHeight(smoothPolys[0], iterPos, ref eh);
                        iterPos[1] = eh;
                    }
                }

                if (nodes.Count < maxSmooth)
                {
                    nodes.AddLast(new Vector3(iterPos[0], iterPos[1], iterPos[2]));
                }
            }

            return(true);
        }