IEnumerator StartQuery()
        {
            NavMeshWorld world = NavMeshWorld.GetDefaultWorld();
            NavMeshQuery query = new NavMeshQuery(world, Allocator.Persistent, maxPath);

            NavMeshLocation startLocation = query.MapLocation(start.position, Vector3.up * extents, 0);
            NavMeshLocation endLocation   = query.MapLocation(end.position, Vector3.up * extents, 0);
            PathQueryStatus status        = query.BeginFindPath(startLocation, endLocation);

            yield return(new WaitWhile(() => {
                status = query.UpdateFindPath(8, out int iterationsPerformed);
                return status == PathQueryStatus.InProgress;
            }));

            status = query.EndFindPath(out int pathsize);

            NativeArray <PolygonId> path = new NativeArray <PolygonId>(pathsize, Allocator.Temp);
            int pathResult = query.GetPathResult(path);

            //NativeArray<NavMeshLocation> pathStraight = new NativeArray<NavMeshLocation>(maxPath, Allocator.Temp);
            ////NativeArray<StraightPathFlag> pathStreaigthFlag = new NativeArray<StraightPathFlags>(maxPath, Allocator.Temp);
            //NativeArray<float> vertexSize = new NativeArray<float>(maxPath, Allocator.Temp);
            //
            //int straghtPathCount = 0;

            for (var i = 0; i < pathResult; i++)
            {
                var p = path[i];

                var loc    = query.CreateLocation(start.position, p);
                var target = loc.position;
            }

            query.Dispose();
        }
Example #2
0
    public void TryFindPath(Vector3 start, Vector3 end)
    {
        using (var polygonIds = new NativeArray <PolygonId>(100, Allocator.Persistent))
            using (var query = new NavMeshQuery(NavMeshWorld.GetDefaultWorld(), Allocator.Persistent, 100))
            {
                int maxIterations = 1024;
                var from          = query.MapLocation(start, Vector3.one * 10, 0);
                var to            = query.MapLocation(end, Vector3.one * 10, 0);

                var status = query.BeginFindPath(from, to);

                status = query.UpdateFindPath(maxIterations, out int currentIterations);

                var finalStatus = query.EndFindPath(out int pathLength);
                var pathResult  = query.GetPathResult(polygonIds);

                var straightPath = new NativeArray <NavMeshLocation>(pathLength, Allocator.Temp);
                paths = new Vector3[pathResult];

                for (int i = 0; i < pathResult; i++)
                {
                    var polyId           = polygonIds[i];
                    var polyWorldToLocal = query.PolygonWorldToLocalMatrix(polyId);

                    var b = query.CreateLocation(paths[i], polyId);
                    paths[i] = b.position;
                    //Debug.Log(b.position);
                }

                //Debug.Log(pathResult);
                Debug.DrawLine(from.position, to.position);
            }
    }
        public void Execute()
        {
            nml_FromLocation = query.MapLocation(fromLocation, extents, 0);
            nml_ToLocation   = query.MapLocation(toLocation, extents, 0);
            if (query.IsValid(nml_FromLocation) && query.IsValid(nml_ToLocation))
            {
                status = query.BeginFindPath(nml_FromLocation, nml_ToLocation, -1);
                if (status == PathQueryStatus.InProgress)
                {
                    status = query.UpdateFindPath(maxIteration, out int iterationPerformed);
                }
                if (status == PathQueryStatus.Success)
                {
                    status = query.EndFindPath(out int polygonSize);
                    NativeArray <NavMeshLocation>   res = new NativeArray <NavMeshLocation>(polygonSize, Allocator.Temp);
                    NativeArray <StraightPathFlags> straightPathFlag = new NativeArray <StraightPathFlags>(maxPathSize, Allocator.Temp);
                    NativeArray <float>             vertexSide       = new NativeArray <float>(maxPathSize, Allocator.Temp);
                    NativeArray <PolygonId>         polys            = new NativeArray <PolygonId>(polygonSize, Allocator.Temp);
                    int straightPathCount = 0;
                    query.GetPathResult(polys);
                    returningStatus = PathUtils.FindStraightPath(
                        query,
                        fromLocation,
                        toLocation,
                        polys,
                        polygonSize,
                        ref res,
                        ref straightPathFlag,
                        ref vertexSide,
                        ref straightPathCount,
                        maxPathSize
                        );
                    if (returningStatus == PathQueryStatus.Success)
                    {
                        int fromKey = ((int)fromLocation.x + (int)fromLocation.y + (int)fromLocation.z) * maxPathSize;
                        int toKey   = ((int)toLocation.x + (int)toLocation.y + (int)toLocation.z) * maxPathSize;
                        int key     = fromKey + toKey;
                        statusOutput[0] = 1;
                        statusOutput[1] = key;
                        statusOutput[2] = straightPathCount;

                        for (int i = 0; i < straightPathCount; i++)
                        {
                            result[i] = (float3)res[i].position + new float3(0, 0.75f, 0); // elevated point
                            ub.Add(new Unit_Buffer {
                                wayPoints = result[i]
                            });
                        }
                    }
                    res.Dispose();
                    straightPathFlag.Dispose();
                    polys.Dispose();
                    vertexSide.Dispose();
                }
            }
        }
            private static void StartPathSearch(int jobIndex, Entity entityRequest, NavMeshQuery query,
                                                EntityCommandBuffer.Concurrent commandBuffer, NavMeshPathfindingRequestData request)
            {
                var from = query.MapLocation(request.Start, request.Extents, request.AgentTypeId);
                var to   = query.MapLocation(request.Destination, request.Extents, request.AgentTypeId);

                query.BeginFindPath(from, to);
                request.Status = PathSearchStatus.Started;
                commandBuffer.SetComponent(jobIndex, entityRequest, request);
            }
Example #5
0
        public void Execute(Entity entity, int index, ref AgentComponent c0, ref LocalToWorld c2)
        {
            //c2 = new LocalToWorld { Value = new float4x4(quaternion.identity, c2.Position + new float3(0, c0.moveSpeed * 0.01f, 0))};
            var navQuery         = new NavMeshQuery(navWorld, Allocator.TempJob, 20);
            var startPos         = navQuery.MapLocation(c0.position, new float3(10), 0);
            var endPos           = navQuery.MapLocation(setting.position1, new float3(10), 0);
            var status           = navQuery.BeginFindPath(startPos, endPos);
            int pathFindingInter = 0;

            if (status == PathQueryStatus.InProgress)
            {
                bool end       = false;
                bool err       = false;
                int  nodeCount = 0;
                while (!end)
                {
                    var upStatus = navQuery.UpdateFindPath(10, out pathFindingInter);
                    if (upStatus != PathQueryStatus.InProgress)
                    {
                        end = true;
                        err = upStatus == PathQueryStatus.Success;
                    }
                }
                if (!err)
                {
                    var endStatus = navQuery.EndFindPath(out nodeCount);
                    if (endStatus == PathQueryStatus.Failure)
                    {
                        err = true;
                    }
                }
                if (!err)
                {
                    NativeSlice <PolygonId> path = new NativeSlice <PolygonId>();
                    var count = navQuery.GetPathResult(path);
                    Debug.Log(count);
                }
            }
        }
        public void Execute()
        {
            var startLoc = query.MapLocation(localToWorld[0].Position, Vector3.one * 3.5f, 0);
            var endLoc   = query.MapLocation(new Vector3(3, 0, 3), Vector3.one * 3.5f, 0);

            //Debug.Log("MoveJob");
            if (!query.IsValid(startLoc) || !query.IsValid(endLoc))
            {
                //Debug.Log("!IsValid");
                return;
            }
            //var status = navagent.PathQueryStatus;/if(navagent.PathQueryStatus == )
            //Debug.Log(navAgent[0].PathQueryStatus);

            if (navAgent[0].PathQueryStatus == 0)
            {
                navAgent[0] = new NavAgent(startLoc, query.BeginFindPath(startLoc, endLoc));
                //Debug.Log(navAgent[0].PathQueryStatus);
            }

            navAgent[0] = new NavAgent(startLoc, query.UpdateFindPath(10, out var iterations));
            //navAgent.PathQueryStatus = status;
            if (navAgent[0].PathQueryStatus.Equals(PathQueryStatus.InProgress))
            {
                navAgent[0] = new NavAgent(startLoc, query.UpdateFindPath(10, out iterations));
                //var performed = 0;
                //status =  query.UpdateFindPath(10, out performed);
                //navAgent.PathQueryStatus = status;
                //Debug.Log("InProgress;");
            }

            if ((navAgent[0].PathQueryStatus & PathQueryStatus.Success) != 0)
            {
                //Debug.Log("Success;");
            }
        }
Example #7
0
    public void UpdateTimesliced(int maxIter = 100)
    {
        var state = m_State[0];

        while (maxIter > 0 && (state.currentAgentIndex >= 0 || state.requestCount > 0 && state.requestIndex < state.requestCount))
        {
            if (state.currentAgentIndex < 0 && state.requestCount > 0 && state.requestIndex < state.requestCount)
            {
                // Initialize a new query
                var request = m_Requests[state.requestIndex];
                request.uid = RequestEcs.invalidId;
                m_Requests[state.requestIndex] = request;
                state.requestIndex++;
                var startLoc = m_Query.MapLocation(request.start, 10.0f * Vector3.one, 0, request.mask);
                var endLoc   = m_Query.MapLocation(request.end, 10.0f * Vector3.one, 0, request.mask);
                if (!m_Query.IsValid(startLoc) || !m_Query.IsValid(endLoc))
                {
                    continue;
                }

                state.currentPathRequest = new PathInfo()
                {
                    begin = 0,
                    size  = 0,
                    start = startLoc,
                    end   = endLoc
                };

                var status = m_Query.BeginFindPath(startLoc, endLoc, request.mask, m_Costs);
                if (!status.IsFailure())
                {
                    state.currentAgentIndex = request.agentIndex;
                }
            }

            if (state.resultPathsCount >= m_ResultRanges.Length)
            {
                break;
            }

            if (state.currentAgentIndex >= 0)
            {
                // Continue existing query
                int niter;
                var status = m_Query.UpdateFindPath(maxIter, out niter);
                maxIter -= niter;

                if (status.IsSuccess())
                {
                    int npath;
                    status = m_Query.EndFindPath(out npath);
                    if (status.IsSuccess())
                    {
                        var resPolygons = new NativeArray <PolygonId>(npath, Allocator.Temp);
                        var pathInfo    = state.currentPathRequest;
                        pathInfo.size = m_Query.GetPathResult(resPolygons);
                        if (pathInfo.size > 0)
                        {
#if DEBUG_CROWDSYSTEM_ASSERTS
                            Debug.Assert(pathInfo.size + state.resultNodesCount <= m_ResultNodes.Length);
#endif

                            pathInfo.begin = state.resultNodesCount;
                            for (var i = 0; i < npath; i++)
                            {
                                m_ResultNodes[state.resultNodesCount] = resPolygons[i];
                                state.resultNodesCount++;
                            }
                            m_ResultRanges[state.resultPathsCount] = pathInfo;
                            m_AgentIndices[state.resultPathsCount] = state.currentAgentIndex;
                            state.resultPathsCount++;
                        }
                        state.currentPathRequest = pathInfo;
                        resPolygons.Dispose();
                    }
                }

                if (!status.IsInProgress())
                {
                    state.currentAgentIndex = -1;
                }
            }
        }

        m_State[0] = state;
    }
Example #8
0
        public void Update(int maxIter = 100)
        {
            var             s      = state[0];
            PathQueryStatus status = PathQueryStatus.Success;

            if (s.entitySize == 0)
            {
                return;
            }
            while (maxIter > 0)
            {
                if (s.entityInUse == State.NONE)
                {
                    PathRequestData request = requests[0];
                    for (int i = s.entitySize - 1; i >= 0; --i)
                    {
                        request = requests[i];
                        if (request.status < PathRequestStatus.Done)
                        {
                            s.entityInUse = i;
                            break;
                        }
                    }
                    if (s.entityInUse == State.NONE)
                    {
                        break;
                    }
                    var startLoc = query.MapLocation(request.start, Vector3.one * 10f, request.agentType, request.mask);
                    var endLoc   = query.MapLocation(request.end, Vector3.one * 10f, request.agentType, request.mask);
                    status = query.BeginFindPath(startLoc, endLoc, request.mask);
                    if (status.IsFailure())
                    {
                        break;
                    }
                    request.status          = PathRequestStatus.InProgress;
                    requests[s.entityInUse] = request;
                }
                int nIter;
                status = query.UpdateFindPath(maxIter, out nIter);
                if (status.IsFailure())
                {
                    break;
                }
                maxIter -= nIter;
                if (status.IsSuccess())
                {
                    int pathSize;
                    var request = requests[s.entityInUse];
                    status = query.EndFindPath(out pathSize);
                    if (status.IsFailure())
                    {
                        break;
                    }
                    if (status.IsSuccess())
                    {
                        query.GetPathResult(resultBuffer);
                        var offset    = s.pathSize;
                        var pathSlice = new NativeSlice <PathPoint>(pathBuffer, offset);
                        status = PathUtils.FindStraightPath(query, request.start, request.end, resultBuffer, pathSize
                                                            , pathSlice, ref pathSize, MAX_PATHSIZE);
                        if (status.IsFailure())
                        {
                            break;
                        }
                        pathStart[s.entityInUse] = offset;
                        request.pathSize         = pathSize;
                        s.pathSize += pathSize;
                    }
                    request.status          = PathRequestStatus.Done;
                    requests[s.entityInUse] = request;
                    s.entityInUse           = State.NONE;
                }
                state[0] = s;
            }

            if (status.IsFailure())
            {
                var request = requests[s.entityInUse];
                request.status          = PathRequestStatus.Failure;
                requests[s.entityInUse] = request;
                s.entityInUse           = State.NONE;
                state[0] = s;
            }
        }
        // Mostly static data for path finding
        // TODO: cannot be read only, should investigate allowing that in the nav mesh apis
        //[ReadOnly]
        //public NativeArray<float> costs;

        public void Execute()
        {
            var pathInfo = pathsInfo[entity];

            // Check bit 1 and 2
            if ((pathInfo.bitmasks & 6) == 0)
            {
                //m_InitFindPath.Begin();
                // We need to do path finding
                var end = query.MapLocation(pathInfo.targetPosition, new Vector3(100, 100, 100), 0);
                query.BeginFindPath(navMeshLocation[entity].NavMeshLocation, end); //, NavMesh.AllAreas, costs);
                pathInfo.bitmasks |= 2;
                //m_InitFindPath.End();

                //Debug.Log("START PATH FINDING");
            }

            // Path searching is initialized, we should update stuff
            //m_UpdatePath.Begin();
            int performed;
            var status = query.UpdateFindPath(10, out performed);

            //m_UpdatePath.End();

            //Debug.Log("UPDATE SLICE " + performed + " " + status);

            if (status.IsSuccess())
            {
                //m_MovePath.Begin();
                int polySize;
                status = query.EndFindPath(out polySize);
                if (status.IsSuccess())
                {
                    query.GetPathResult(polygons);
                    pathInfo.currentCornerIndex = 0;

                    // Update the bitmask: Path finding done & path found
                    pathInfo.bitmasks &= ~2;
                    pathInfo.bitmasks |= 4;
                    completePathQueries.Enqueue(entity);

                    var minionPath = minionPaths[entity];

                    pathInfo.pathSize = 0;

                    var cornerCount = 0;
                    var pathStatus  = PathUtils.FindStraightPath(query, navMeshLocation[entity].NavMeshLocation.position,
                                                                 pathInfo.targetPosition,
                                                                 polygons, polySize,
                                                                 ref straightPath, ref straightPathFlags, ref vertexSide,
                                                                 ref cornerCount, maxPathSize);

                    if (pathStatus.IsSuccess() && cornerCount > 1 && cornerCount <= maxPathSize)
                    {
                        for (var i = 0; i < cornerCount; i++)
                        {
                            minionPath[i] = straightPath[i].position;
                        }

                        pathInfo.pathFoundToPosition = straightPath[cornerCount - 1].position;
                        pathInfo.pathSize            = cornerCount;
                    }

                    //Debug.Log("PATH FINDING DONE " + path.pathSize);
                }

                //m_MovePath.End();
            }

            if (status.IsFailure())
            {
                // Failure happened, reset stuff
                pathInfo.bitmasks &= ~2;
                completePathQueries.Enqueue(entity);
            }
            pathsInfo[entity] = pathInfo;
        }