Пример #1
0
        public override bool Process(AStarContext context)
        {
            PathFindingRequest request = context.Request as PathFindingRequest;

            ProcessingNode startNode = context.GetStartNode();
            ProcessingNode endNode   = context.GetTargetNode();

            if (startNode == null || endNode == null)
            {
                return(false);
            }

            FindPath(startNode, endNode, context);
            List <Int3> path = context.rawPathPoints;

            if (path.Count > 0)
            {
                // set the first and last point to 'from' and 'to'.
                path[0] = request.fromPosition;
                path[path.Count - 1] = request.toPosition;
                // then optimize
                NavMeshPathOptimizer.Optimize(ref context.rawPathNodeCache, ref path);
            }

            return(context.rawPathPoints.Count >= 2);
        }
Пример #2
0
        protected override int CalCostH(ProcessingNode node, AStarContext context)
        {
            float       heuristicScale = 1.0f;
            NavMeshNode calNode        = node.astarNode as NavMeshNode;
            NavMeshNode targetNode     = context.GetTargetNode().astarNode as NavMeshNode;
            int         dist           = (calNode.position - targetNode.position).costMagnitude;

            return(UnityEngine.Mathf.RoundToInt(dist * heuristicScale));
        }
Пример #3
0
        protected override int CalCostH(ProcessingNode node, AStarContext context)
        {
            Grid2DNode endNode = context.GetTargetNode().astarNode as Grid2DNode;
            Grid2DNode nd      = (Grid2DNode)(node.astarNode);
            int        dx      = Math.Abs(endNode.x - nd.x);
            int        dy      = Math.Abs(endNode.y - nd.y);
            int        dist    = dx > dy ? 14 * dy + 10 * (dx - dy) : 14 * dx + 10 * (dy - dx);

            return(dist);
        }
Пример #4
0
        protected ProcessingNode DoAStar(AStarContext context)
        {
            if (enableTracing)
            {
                context.TracingNodes.Clear();
            }

            ProcessingNode startNode = context.GetStartNode();

            context.openList   = null;
            context.closedList = null;

            startNode.g    = 0;
            startNode.h    = CalCostH(startNode, context);
            startNode.f    = startNode.g + startNode.h;
            startNode.prev = null;
            startNode.next = null;

            context.AddToOpenList(startNode);

            ProcessingNode arriveNode = null;

            int count = 0;

            while (context.openList != null)
            {
                count++;
                if (count > 10000)
                {
                    UnityEngine.Debug.LogError("AStarPathPlanner : too many nodes....");
                    break;
                }
                ProcessingNode curNode = context.openList;
                if (CheckArrived(curNode, context))
                {
                    arriveNode = curNode;
                    break;
                }
                EvaluateAllNeighbours(curNode, context);
                context.RemoveFromOpenList(curNode);
                context.AddToClosedList(curNode);

                if (enableTracing)
                {
                    context.TracingNodes.Add(curNode.astarNode);
                }
            }

            return(arriveNode);
        }
Пример #5
0
        private void EvaluateAllNeighbours(ProcessingNode node, AStarContext context)
        {
            int neighbourCount = context.map.GetNeighbourNodeCount(node.astarNode);

            for (int i = 0; i < neighbourCount; ++i)
            {
                AStarNode      astarNode = context.map.GetNeighbourNode(node.astarNode, i);
                ProcessingNode neighbour = context.GetNode(astarNode);
                if (neighbour != null)
                {
                    EvaluateNeighbour(node, neighbour, context);
                }
            }
        }
Пример #6
0
        public void FindPath(ProcessingNode start, ProcessingNode end, AStarContext context)
        {
            ProcessingNode endNode = DoAStar(context);

            Grid2DGraph    graph    = context.map as Grid2DGraph;
            ProcessingNode pathNode = endNode;

            while (pathNode != null)
            {
                Grid2DNode navNode = pathNode.astarNode as Grid2DNode;
                context.rawPathNodeCache.Add(navNode);
                context.rawPathPoints.Add(graph.Int2ToFixVector3(new Int2(navNode.x, navNode.y)));
                pathNode = pathNode.prev;
            }
        }
Пример #7
0
        protected override int CalCostH(ProcessingNode node, AStarContext context)
        {
            Grid3DNode targetNode = context.GetTargetNode().astarNode as Grid3DNode;
            Grid3DNode calNode    = node.astarNode as Grid3DNode;
            int        dx         = Math.Abs(targetNode.x - calNode.x);
            int        dy         = Math.Abs(targetNode.y - calNode.y);
            int        dz         = Math.Abs(targetNode.z - calNode.z);

            dx *= 10;
            dy *= 10;
            dz *= 10;
            int distxz = (int)(dx > dz ? 1.4f * dz + (dx - dz) : 1.4f * dx + (dz - dx));
            int dist   = distxz + dy;

            return(dist);
        }
Пример #8
0
        private void FindPath(ProcessingNode start, ProcessingNode end, AStarContext context)
        {
            ProcessingNode endNode = DoAStar(context);

            context.rawPathNodeCache.Clear();
            context.rawPathPoints.Clear();
            ProcessingNode pathNode = endNode;

            while (pathNode != null)
            {
                NavMeshNode navNode = pathNode.astarNode as NavMeshNode;
                context.rawPathNodeCache.Add(navNode);
                context.rawPathPoints.Add(navNode.position);
                pathNode = pathNode.prev;
            }
        }
Пример #9
0
        private void FindPath(ProcessingNode start, ProcessingNode end, AStarContext context)
        {
            Grid3DGraph    graphMap = context.map as Grid3DGraph;
            ProcessingNode endNode  = DoAStar(context);

            context.rawPathNodeCache.Clear();
            context.rawPathPoints.Clear();
            ProcessingNode pathNode = endNode;

            while (pathNode != null)
            {
                Grid3DNode navNode = pathNode.astarNode as Grid3DNode;
                Grid3DNode node    = graphMap.GetNodeAt(navNode.x, navNode.y, navNode.z);
                context.rawPathNodeCache.Add(navNode);
                context.rawPathPoints.Add(node.worldPosition);
                pathNode = pathNode.prev;
            }

            //var points = FindPath3D(startNode.id, endNode.id);

            /*Grid3DPathOptimizer.Optimize(graphMap, ref points);
             *
             * result.Clear();
             * for (int i = 0; i < points.Count; ++i)
             * {
             *      var point = points[i];
             *      var node = graphMap.GetNodeAt(point.x, point.y, point.z);
             *      result.Add(new Int3(node.worldPosition.x, node.worldPosition.y, node.worldPosition.z));
             * }
             * if (result.Count >= 2)
             * {
             *      result[0] = from;
             *      result[result.Count - 1] = to;
             * }
             * if (result.Count >= 4)
             * {
             *      int cellSize = graphMap.navGraphData.buildConfig.cellSize;
             *      if ((result[1] - from).sqrMagnitudeLong < cellSize * cellSize)
             *              result.RemoveAt(1);
             *      if ((result[result.Count - 2] - to).sqrMagnitudeLong < cellSize * cellSize)
             *              result.RemoveAt(result.Count - 2);
             * }
             * return result.Count >= 2;*/
        }
Пример #10
0
        /// <summary>
        /// calculate tactical G
        /// </summary>
        private int TacticalCost(NavMeshNode prevNode, NavMeshNode currentNode, AStarContext context)
        {
            int distCost = prevNode.GetConnectionCost(currentNode.id);
            int tacCost  = 0;

            PathFindingRequest request = context.Request as PathFindingRequest;

            // doer's team
            TwGame.Team team = (TwGame.Team)request.extend1;

            switch (team)
            {
            case TwGame.Team.Neutral:
                tacCost = 0;
                break;

            case TwGame.Team.Team_1:
            case TwGame.Team.Team_2:
            {
                int MaxInfluence = TwGame.ComInfluenceMap.MaxTeamStrengthValue;
                int cur          = TwGame.AIUtil.GetTeamStrength(currentNode.position, team);
                int pre          = TwGame.AIUtil.GetTeamStrength(prevNode.position, team);
                // avarage influence between current node's position and previous node's position.
                int infl = (cur + pre) >> 1;
                if (infl > 0)
                {
                    tacCost = System.Math.Max(-distCost + 1, -distCost * infl / MaxInfluence);
                }
                else if (infl < 0)
                {
                    tacCost = -distCost * infl / MaxInfluence * 2;
                }
            }
            break;
            }

            return(distCost + tacCost);
        }
Пример #11
0
 /// <summary>
 /// apply InfluenceMap to G vlaue,to avoide eg. go throgh enemies
 /// </summary>
 protected override int CalCostG(ProcessingNode prevNode, ProcessingNode currentNode, AStarContext context)
 {
     if (enableTactical)
     {
         int tac = TacticalCost(prevNode.astarNode as NavMeshNode, currentNode.astarNode as NavMeshNode, context);
         return(prevNode.g + tac);
     }
     else
     {
         return(prevNode.g + (prevNode.astarNode as NavMeshNode).GetConnectionCost(currentNode.astarNode.id));
     }
 }
Пример #12
0
 protected abstract int CalCostG(ProcessingNode prevNode, ProcessingNode currentNode, AStarContext context);
Пример #13
0
        protected override bool CheckArrived(ProcessingNode node, AStarContext context)
        {
            ProcessingNode targetNode = context.GetTargetNode();

            return(node.astarNode.id == targetNode.astarNode.id);
        }
Пример #14
0
 protected override bool CheckArrived(ProcessingNode node, AStarContext context)
 {
     return(false);
 }
Пример #15
0
 protected abstract int CalCostH(ProcessingNode node, AStarContext context);
Пример #16
0
 protected override int CalCostH(ProcessingNode node, AStarContext context)
 {
     return(0);
 }
Пример #17
0
 protected override int CalCostG(ProcessingNode prevNode, ProcessingNode currentNode, AStarContext context)
 {
     return(0);
 }
Пример #18
0
        private void EvaluateNeighbour(ProcessingNode currentNode, ProcessingNode neighbourNode, AStarContext context)
        {
            int g = CalCostG(currentNode, neighbourNode, context);
            int h = CalCostH(neighbourNode, context);
            int f = g + h;

            ProcessingNode findNode = context.FindInOpenList(neighbourNode);

            if (findNode != null)
            {
                if (f < findNode.f)
                {
                    findNode.g    = g;
                    findNode.h    = h;
                    findNode.f    = f;
                    findNode.prev = currentNode;
                }
            }
            else
            {
                findNode = context.FindInClosedList(neighbourNode);
                if (findNode != null)
                {
                    if (f < findNode.f)
                    {
                        context.RemoveFromClosedList(findNode);
                        findNode.g    = g;
                        findNode.h    = h;
                        findNode.f    = f;
                        findNode.prev = currentNode;
                        context.AddToOpenList(findNode);
                    }
                }
                else
                {
                    ProcessingNode newNode = neighbourNode;
                    newNode.g    = g;
                    newNode.h    = h;
                    newNode.f    = f;
                    newNode.prev = currentNode;
                    context.AddToOpenList(newNode);
                }
            }
        }
Пример #19
0
 protected abstract bool CheckArrived(ProcessingNode node, AStarContext context);
Пример #20
0
        protected override int CalCostG(ProcessingNode prevNode, ProcessingNode currentNode, AStarContext context)
        {
            Grid2DNode pre  = (Grid2DNode)(prevNode.astarNode);
            Grid2DNode cur  = (Grid2DNode)(currentNode.astarNode);
            int        dx   = Math.Abs(pre.x - cur.x);
            int        dy   = Math.Abs(pre.y - cur.y);
            int        dist = dx > dy ? 14 * dy + 10 * (dx - dy) : 14 * dx + 10 * (dy - dx);

            return(prevNode.g + dist);
        }
Пример #21
0
 protected override int CalCostG(ProcessingNode prevNode, ProcessingNode currentNode, AStarContext context)
 {
     return(prevNode.g + context.map.GetEdge(prevNode.astarNode.id, currentNode.astarNode.id).cost);
 }
Пример #22
0
 public abstract bool Process(AStarContext handler);