Пример #1
0
 public void Recycle(ref ListCopyable <T> value)
 {
     if (value != null)
     {
         PoolListCopyable <T> .Recycle(ref value);
     }
 }
Пример #2
0
        public override void ApplyBeforeConnections(Graph graph)
        {
            var layer  = this.gameObject.layer;
            var bounds = this.bounds;

            bounds.center += this.transform.position;

            var nodes = PoolListCopyable <Node> .Spawn(10);

            graph.GetNodesInBounds(nodes, bounds);
            foreach (var node in nodes)
            {
                var ray = new Ray(node.worldPosition + Vector3.up * 10f, Vector3.down);
                if (Physics.Raycast(ray, out var hit, 1000f, this.layerMask) == true)
                {
                    if (hit.collider.gameObject.layer == layer)
                    {
                        var dt = this.penaltyDelta;
                        if (dt < 0)
                        {
                            node.penalty -= (uint)(-this.penaltyDelta);
                        }
                        else
                        {
                            node.penalty += (uint)this.penaltyDelta;
                        }

                        node.tag = this.tag;
                    }
                }
            }
            PoolListCopyable <Node> .Recycle(ref nodes);
        }
Пример #3
0
        public void Recycle()
        {
            this.result       = PathCompleteState.NotCalculated;
            this.graph        = null;
            this.cacheEnabled = default;

            if (this.nodes != null)
            {
                PoolListCopyable <Node> .Recycle(ref this.nodes);
            }
            if (this.nodesModified != null)
            {
                PoolListCopyable <Node> .Recycle(ref this.nodesModified);
            }

            if (this.cacheEnabled == false && this.flowField.arr != null)
            {
                PoolArray <byte> .Recycle(ref this.flowField);
            }

            if (this.navMeshPoints != null)
            {
                PoolListCopyable <Vector3> .Recycle(ref this.navMeshPoints);
            }
        }
Пример #4
0
        /// <summary>
        /// Returns all events by tick range [from..to] (including from and to)
        /// </summary>
        /// <param name="from"></param>
        /// <param name="to"></param>
        /// <returns></returns>
        HistoryStorage IStatesHistoryModuleBase.GetHistoryStorage(Tick from, Tick to)
        {
            var list = PoolListCopyable <HistoryEvent> .Spawn(100);

            foreach (var data in this.events)
            {
                var tick = data.Key;
                if ((from != Tick.Invalid && tick < from) ||
                    (to != Tick.Invalid && tick > to))
                {
                    continue;
                }

                var values = data.Value.Values;
                for (int i = 0, cnt = values.Count; i < cnt; ++i)
                {
                    var evt = values[i];
                    if (evt.storeInHistory == true)
                    {
                        list.Add(evt);
                    }
                }
            }

            var storage = new HistoryStorage();

            storage.events = list.ToArray();
            PoolListCopyable <HistoryEvent> .Recycle(ref list);

            return(storage);
        }
Пример #5
0
 public void Recycle()
 {
     if (this.nodes != null)
     {
         PoolListCopyable <Node> .Recycle(ref this.nodes);
     }
     if (this.nodesModified != null)
     {
         PoolListCopyable <Node> .Recycle(ref this.nodesModified);
     }
 }
Пример #6
0
 public override void OnDeconstruct()
 {
     this.pool.Clear();
     this.pool = null;
     if (this.currentTransformArray.isCreated == true)
     {
         this.currentTransformArray.Dispose();
     }
     //if (this.resultTransforms != null) PoolList<UnityEngine.Transform>.Recycle(ref this.resultTransforms);
     if (this.tempList != null)
     {
         PoolListCopyable <MonoBehaviourView> .Recycle(ref this.tempList);
     }
 }
Пример #7
0
        public void Recycle()
        {
            if (this.nodes != null)
            {
                PoolListCopyable <Node> .Recycle(ref this.nodes);
            }
            if (this.nodesModified != null)
            {
                PoolListCopyable <Node> .Recycle(ref this.nodesModified);
            }

            if (this.cacheEnabled == false && this.flowField.arr != null)
            {
                PoolArray <byte> .Recycle(ref this.flowField);
            }
        }
        public override void ApplyAfterConnections(Graph graph)
        {
            var visited = PoolHashSet <Node> .Spawn();

            {
                var gridGraph = (GridGraph)graph;
                var result    = PoolListCopyable <Node> .Spawn(1);

                graph.GetNodesInBounds(result, new Bounds(graph.graphCenter, (Vector3)gridGraph.size * gridGraph.nodeSize), Constraint.Empty);
                {
                    foreach (var node in result)
                    {
                        var worldPos = node.worldPosition;
                        var cellPos  = this.tilemap.layoutGrid.WorldToCell(worldPos);
                        var tile     = this.tilemap.GetTile(cellPos);
                        for (int i = 0; i < this.items.Length; ++i)
                        {
                            var item = this.items[i];
                            if (item.modifyWalkability == false)
                            {
                                continue;
                            }

                            if (item.requiredTile == null || item.requiredTile == tile)
                            {
                                if (item.checkSprite == true)
                                {
                                    var idx = System.Array.IndexOf(item.spriteOneOf, this.tilemap.GetSprite(cellPos));
                                    if (idx < 0)
                                    {
                                        continue;
                                    }
                                }

                                if (visited.Contains(node) == false)
                                {
                                    visited.Add(node);
                                    node.walkable = item.walkable;
                                }
                            }
                        }
                    }
                }
                PoolListCopyable <Node> .Recycle(ref result);
            }
            PoolHashSet <Node> .Recycle(ref visited);
        }
Пример #9
0
        public override void ApplyAfterConnections(Graph graph)
        {
            var halfOffset = new Vector3(this.tilemap.cellSize.x, 0f, this.tilemap.cellSize.z) * 0.5f;

            var visited = PoolHashSet <Node> .Spawn();

            foreach (var pos in this.bounds.allPositionsWithin)
            {
                var worldPos = pos + halfOffset;
                var cellPos  = this.tilemap.layoutGrid.WorldToCell(worldPos);
                var tile     = this.tilemap.GetTile(cellPos);
                for (int i = 0; i < this.items.Length; ++i)
                {
                    var item = this.items[i];
                    if (item.modifyWalkability == false)
                    {
                        continue;
                    }

                    if (item.requiredTile == null || item.requiredTile == tile)
                    {
                        if (item.checkSprite == true)
                        {
                            var idx = System.Array.IndexOf(item.spriteOneOf, this.tilemap.GetSprite(cellPos));
                            if (idx < 0)
                            {
                                continue;
                            }
                        }

                        var result = PoolListCopyable <Node> .Spawn(1);

                        graph.GetNodesInBounds(result, new Bounds(worldPos, this.tilemap.cellSize * 0.5f));
                        foreach (var node in result)
                        {
                            if (visited.Contains(node) == false)
                            {
                                visited.Add(node);
                                node.walkable = item.walkable;
                            }
                        }
                        PoolListCopyable <Node> .Recycle(ref result);
                    }
                }
            }
            PoolHashSet <Node> .Recycle(ref visited);
        }
Пример #10
0
        public virtual bool IsSuitable(Constraint constraint)
        {
            if (constraint.checkWalkability == true && this.walkable != constraint.walkable)
            {
                return(false);
            }
            if (constraint.checkArea == true && (constraint.areaMask & (1 << this.area)) == 0)
            {
                return(false);
            }
            if (constraint.checkTags == true && (constraint.tagsMask & (1 << this.tag)) == 0)
            {
                return(false);
            }
            if (constraint.graphMask >= 0 && (constraint.graphMask & (1 << this.graph.index)) == 0)
            {
                return(false);
            }

            if (constraint.tagsMask > 0L &&
                (constraint.agentSize.x > 0f ||
                 constraint.agentSize.y > 0f ||
                 constraint.agentSize.z > 0f))
            {
                var result = PoolListCopyable <Node> .Spawn(10);

                this.graph.GetNodesInBounds(result, new Bounds(this.worldPosition, constraint.agentSize));
                for (int e = 0, cnt = result.Count; e < cnt; ++e)
                {
                    var node = result[e];
                    var constraintErosion = constraint;
                    constraintErosion.agentSize = Vector3.zero;
                    if (node.IsSuitable(constraintErosion) == false)
                    {
                        return(false);
                    }
                }
                PoolListCopyable <Node> .Recycle(ref result);
            }

            return(true);
        }
Пример #11
0
        public virtual void UpdateGraph(GraphUpdateObject graphUpdateObject)
        {
            var bounds = graphUpdateObject.GetBounds();
            var nodes  = PoolListCopyable <Node> .Spawn(10);

            this.GetNodesInBounds(nodes, bounds, Constraint.Empty);
            for (int i = 0, cnt = nodes.Count; i < cnt; ++i)
            {
                var node = nodes[i];
                if (graphUpdateObject.checkRadius == true)
                {
                    if ((node.worldPosition - graphUpdateObject.center).sqrMagnitude > graphUpdateObject.radius * graphUpdateObject.radius)
                    {
                        continue;
                    }
                }

                graphUpdateObject.Apply(node);
            }
            PoolListCopyable <Node> .Recycle(ref nodes);
        }
Пример #12
0
        partial void EndRestoreEntitiesPlugin5()
        {
            if (this.debugSettings.createGameObjectsRepresentation == true)
            {
                var unused = PoolListCopyable <Entity> .Spawn(100);

                foreach (var entKv in this.debugEntities)
                {
                    if (this.used.Contains(entKv.Key) == false)
                    {
                        unused.Add(entKv.Key);
                    }
                }

                for (int i = 0; i < unused.Count; ++i)
                {
                    this.DestroyEntityPlugin5(unused[i]);
                }

                PoolListCopyable <Entity> .Recycle(ref unused);

                PoolHashSet <Entity> .Recycle(ref this.used);
            }
        }
Пример #13
0
        public Path Run <TMod>(LogLevel pathfindingLogLevel, Vector3 from, Vector3 to, Constraint constraint, Graph graph, TMod pathModifier, int threadIndex = 0) where TMod : IPathModifier
        {
            if (threadIndex < 0)
            {
                threadIndex = 0;
            }
            threadIndex = threadIndex % Pathfinding.THREADS_COUNT;

            var constraintStart = constraint;

            constraintStart.checkWalkability = true;
            constraintStart.walkable         = true;
            var startNode = graph.GetNearest(from, constraintStart);

            if (startNode == null)
            {
                return(new Path());
            }

            var constraintEnd = constraintStart;

            constraintEnd.checkArea = true;
            constraintEnd.areaMask  = (1 << startNode.area);

            var endNode = graph.GetNearest(to, constraintEnd);

            if (endNode == null)
            {
                return(new Path());
            }

            var visited = PoolListCopyable <Node> .Spawn(10);

            System.Diagnostics.Stopwatch swPath = null;
            if ((pathfindingLogLevel & LogLevel.Path) != 0)
            {
                swPath = System.Diagnostics.Stopwatch.StartNew();
            }
            var nodesPath = this.AstarSearch(graph, visited, startNode, endNode, constraint, threadIndex);

            var statVisited = visited.Count;
            var statLength  = 0;

            var path = new Path();

            path.graph  = graph;
            path.result = PathCompleteState.NotCalculated;

            if (nodesPath == null)
            {
                path.result = PathCompleteState.NotExist;
            }
            else
            {
                statLength = nodesPath.Count;

                path.result = PathCompleteState.Complete;
                path.nodes  = nodesPath;
            }

            for (int i = 0; i < visited.Count; ++i)
            {
                visited[i].Reset(threadIndex);
            }

            PoolListCopyable <Node> .Recycle(ref visited);

            System.Diagnostics.Stopwatch swModifier = null;
            if ((pathfindingLogLevel & LogLevel.PathMods) != 0)
            {
                swModifier = System.Diagnostics.Stopwatch.StartNew();
            }
            if (path.result == PathCompleteState.Complete)
            {
                path = pathModifier.Run(path, constraint);
            }

            if ((pathfindingLogLevel & LogLevel.Path) != 0)
            {
                Logger.Log(string.Format("Path result {0}, built in {1}ms. Path length: {2} (visited: {3})\nThread Index: {4}", path.result, swPath.ElapsedMilliseconds, statLength, statVisited, threadIndex));
            }

            if ((pathfindingLogLevel & LogLevel.PathMods) != 0)
            {
                Logger.Log(string.Format("Path Mods: {0}ms", swModifier.ElapsedMilliseconds));
            }

            return(path);
        }
        public Path Run <TMod>(LogLevel pathfindingLogLevel, Vector3 from, Vector3 to, Constraint constraint, Graph graph, TMod pathModifier, int threadIndex = 0, bool burstEnabled = true, bool cacheEnabled = false) where TMod : struct, IPathModifier
        {
            if (threadIndex < 0)
            {
                threadIndex = 0;
            }
            threadIndex = threadIndex % Pathfinding.THREADS_COUNT;

            var constraintStart = constraint;

            constraintStart.checkWalkability = true;
            constraintStart.walkable         = true;
            var startNode = graph.GetNearest(from, constraintStart);

            if (startNode == null)
            {
                return(new Path());
            }

            var constraintEnd = constraintStart;

            constraintEnd.checkArea = true;
            constraintEnd.areaMask  = (1 << startNode.area);

            var endNode = graph.GetNearest(to, constraintEnd);

            if (endNode == null)
            {
                return(new Path());
            }

            System.Diagnostics.Stopwatch swPath = null;
            if ((pathfindingLogLevel & LogLevel.Path) != 0)
            {
                swPath = System.Diagnostics.Stopwatch.StartNew();
            }

            //UnityEngine.Debug.Log(endNode.worldPosition + " :: " + ((GridNode)endNode).erosion);
            //UnityEngine.Debug.DrawLine(endNode.worldPosition, endNode.worldPosition + Vector3.up * 10f, Color.red, 3f);

            var key = MathUtils.GetKey(constraint.GetKey(), endNode.index);

            //UnityEngine.Debug.Log("Build path cache: " + cacheEnabled + ", burst: " + burstEnabled);
            if (cacheEnabled == true)
            {
                if (PathfindingFlowFieldProcessor.pathCache.TryGetValue(key, out var buffer) == true)
                {
                    var pathCache = new Path()
                    {
                        graph        = graph,
                        result       = PathCompleteState.Complete,
                        flowField    = buffer,
                        cacheEnabled = cacheEnabled,
                    };

                    if ((pathfindingLogLevel & LogLevel.Path) != 0)
                    {
                        Logger.Log(string.Format("Path result {0}, cache in {1}ms. \nThread Index: {2}", pathCache.result, swPath.ElapsedMilliseconds, threadIndex));
                    }

                    return(pathCache);
                }
            }

            var flowField = PoolArray <byte> .Spawn(graph.nodes.Count);

            int statVisited = 0;

            if (burstEnabled == true)   // burst

            {
                this.FlowFieldBurst(ref statVisited, (GridGraph)graph, ref flowField, (GridNode)endNode, constraint, pathModifier);
                if (cacheEnabled == true)
                {
                    if (PathfindingFlowFieldProcessor.pathCache.Count > PathfindingFlowFieldProcessor.CACHE_SIZE)
                    {
                        const int size = PathfindingFlowFieldProcessor.CACHE_SIZE / 10;
                        for (int i = 0; i < size; ++i)
                        {
                            var idx = PathfindingFlowFieldProcessor.pathCacheQueue.Dequeue();
                            PathfindingFlowFieldProcessor.pathCache.Remove(idx);
                        }

                        PathfindingFlowFieldProcessor.pathCacheQueue.Clear();
                    }

                    PathfindingFlowFieldProcessor.pathCache.Add(key, flowField);
                    PathfindingFlowFieldProcessor.pathCacheQueue.Enqueue(key);
                }
            }
            else     // no burst

            {
                var visited = PoolListCopyable <Node> .Spawn(10);

                for (int i = 0; i < graph.nodes.Count; ++i)
                {
                    graph.nodes[i].Reset(threadIndex);
                }

                this.CreateIntegrationField(graph, visited, endNode, constraint, threadIndex);
                this.CreateFlowField(graph, ref flowField, endNode, constraint, threadIndex);

                statVisited = visited.Count;
                for (int i = 0; i < visited.Count; ++i)
                {
                    visited[i].Reset(threadIndex);
                }
                PoolListCopyable <Node> .Recycle(ref visited);
            }

            var path = new Path();

            path.graph        = graph;
            path.result       = PathCompleteState.Complete;
            path.flowField    = flowField;
            path.cacheEnabled = cacheEnabled;

            if ((pathfindingLogLevel & LogLevel.Path) != 0)
            {
                Logger.Log(string.Format("Path result {0}, built in {1}ms. Path length: (visited: {2})\nThread Index: {3}", path.result, (swPath.ElapsedTicks / (double)System.TimeSpan.TicksPerMillisecond).ToString("0.##"), statVisited, threadIndex));
            }

            return(path);
        }
Пример #15
0
 public void OnRecycle()
 {
     PoolListCopyable <int> .Recycle(ref this.data);
 }