Ejemplo n.º 1
0
 private void FindPath()
 {
     _startNode = new SNode(GridHandler.S.GetGridCellCenter(GridHandler.S.GetGridIndex(_startPos)));
     _goalNode  = new SNode(GridHandler.S.GetGridCellCenter(GridHandler.S.GetGridIndex(_endPos)));
     //stopwatch.Reset();
     //stopwatch.Start();
     _path = AStarUtil.FindPath(_startNode, _goalNode);
     //stopwatch.Stop();
     //Debug.Log("Calculation time: " + stopwatch.ElapsedMilliseconds);
 }
Ejemplo n.º 2
0
    public override bool Execute(Context cx)
    {
        var curNode = cx.Nodes[Owner.NodeId];
        var best    = GetInstantCaptureNode(cx, Owner, curNode);

        if (best != null)
        {
            var bestNode = cx.Nodes[best.Value];
            if (bestNode.EnemyPods > 0 && bestNode.EnemyPods <= Owner.Pods && bestNode.Platinum <= curNode.Platinum)
            {
                return(false);
            }
            _curPath = null;
            RegisterPath(cx, new Path()
            {
                curNode.Id, bestNode.Id
            });
        }
        else
        {
            if (CanTraversePath(cx))
            {
                best = _curPath.NextNodeId(Owner.NodeId);
            }
            else
            {
                // do not use cached dist - it causes units to return back. Probably find a way weight owned territory
                _curPath = AStarUtil.FindPathDirection(cx.Nodes, cx.DistMapFromMe, cx.PathMap, cx.PathPlague, Owner.NodeId, 4);
                if (_curPath.Count == 1)
                {
                    return(false);
                }
                _forceTravers = !CanTraversePath(cx);
                if (_forceTravers)
                {
                    throw new Exception("why ? " + _curPath + " " + cx.Nodes[_curPath.Last()].IsMine);
                }
                RegisterPath(cx, _curPath);
                best = _curPath[1];
            }
        }

        if (!best.HasValue)
        {
            throw new Exception("no explore action");
        }

        cx.MoveTo(best.Value, Owner);
        return(true);
    }
Ejemplo n.º 3
0
 public void findPath(float startX, float startY, float endX, float endY)
 {
     m_Path = AStarUtil.findPath(startX, startY, endX, endY);
     if (m_Path == null)
     {
         Debug.Log("没有找到路径");
     }
     else
     {
         for (int i = 0; i < m_Path.Length; i++)
         {
             AStarNode node = m_Path[i];
             //Debug.Log("(X:" + node.X + ",Y:" + node.Y + ")");
         }
     }
 }
    //private void Update()
    //{
    //    if(Input.GetKeyDown(KeyCode.Space))
    //    {
    //        stopwatch.Reset();
    //        stopwatch.Start();
    //        _startPos = transform.position;
    //        _endPos = goal.position;
    //        FindPath();
    //        float currentY = transform.position.y;
    //        actualPath.Clear();
    //        if(_path != null && _path.Count > 0)
    //        {
    //            for (int i = 0; i < _path.Count; i++)
    //            {
    //                Vector3 nextPos = _path[i].position;

    //                List<Vector3> bezierPts = new List<Vector3>();
    //                for (int j = i; j < i + BezierPointsNum; j++)
    //                {
    //                    if (j < _path.Count)
    //                        bezierPts.Add(_path[j].position);
    //                }
    //                nextPos = Utils.Bezier(lerpParam, bezierPts);
    //                nextPos.y = currentY;
    //                actualPath.Add(nextPos);
    //            }
    //            stopwatch.Stop();
    //            Debug.Log("Calculation time: " + stopwatch.ElapsedMilliseconds);
    //            transform.DOPath(actualPath.ToArray(), _path.Count * 1 / speed).SetEase(Ease.Linear);
    //        }
    //        if (stopwatch.IsRunning)
    //            stopwatch.Stop();
    //    }

    //}

    private IEnumerator FindPath()
    {
        _startNode = new SNode(GridHandler.S.GetGridCellCenter(GridHandler.S.GetGridIndex(_startPos)));
        _goalNode  = new SNode(GridHandler.S.GetGridCellCenter(GridHandler.S.GetGridIndex(_endPos)));


        ThreadStart threadStart = delegate
        {
            _path = AStarUtil.FindPath(_startNode, _goalNode);
        };
        //threadStart.Invoke();
        Thread t = new Thread(threadStart);

        t.Start();

        yield return(new WaitUntil(() => !AStarUtil.IsCalculating));
    }
Ejemplo n.º 5
0
    public void InitCellConnections()
    {
        CellConnections = new Map <Point[][]>(Height, Width);

        for (int i = 0; i < Height; i++)
        {
            for (int j = 0; j < Width; j++)
            {
                var flags = GetFlags(i, j);
                if (flags.CHasFlag(CellFlags.Wall))
                {
                    CellConnections[i, j] = new Point[0][];
                    continue;
                }

                var points = AStarUtil.FindNearest(this, new Point(j, i), 20);
                CellConnections[i, j] = points.Select(r => r.ToArray()).ToArray();
            }
        }
    }
Ejemplo n.º 6
0
    public static List <List <Point> > FindNearest(GameField gameField, Point pos, int hops = 10)
    {
        var openList = new List <Point> {
            pos
        };
        var nextOpenList = new List <Point>();

        var rowLen     = gameField.Width;
        var colLen     = gameField.Height;
        var closedList = AStarUtil.GetClosedList(gameField);

        var res     = new List <List <Point> >(hops);
        var hopList = new List <Point>(8);

        res.Add(hopList);
        var iterations = 1;

        for (var i = 0;; i++)
        {
            if (i == openList.Count)
            {
                ++iterations;
                if (nextOpenList.Count == 0 || iterations == hops)
                {
                    return(res);
                }

                hopList = new List <Point>();
                res.Add(hopList);

                var sw = openList;
                openList     = nextOpenList;
                nextOpenList = sw;
                nextOpenList.Clear();
                i = 0;
            }

            var src = openList[i];

            for (int j = 0; j < 4; j++)
            {
                var adj = new Point(src.X + AStarUtil.ColNum[j], src.Y + AStarUtil.RowNum[j]);
                AStarUtil.Warp(ref adj, rowLen, colLen);
                if (!AStarUtil.IsValid(adj, rowLen, colLen))
                {
                    continue;
                }
                if (closedList[adj.ToIdx(rowLen)])
                {
                    continue;
                }
                if (!gameField.CanTraverse(adj))
                {
                    continue;
                }

                closedList[adj.ToIdx(rowLen)] = true;
                nextOpenList.Add(adj);

                hopList.Add(adj);
            }
        }
    }
Ejemplo n.º 7
0
    public void WaveUncheck(int x, int y)
    {
        var openList = new List <Point>();

        openList.Add(new Point(x, y));
        var closedList = AStarUtil.GetClosedList(this);

        closedList[new Point(x, y).ToIdx(Width)] = true;

        var adjs = new List <Point>();

        while (openList.Count > 0)
        {
            var toTest = openList[openList.Count - 1];
            openList.RemoveAt(openList.Count - 1);

            var spaces  = 0;
            var pellets = 0;

            adjs.Clear();
            for (var i = 0; i < 4; i++)
            {
                var adj = new Point(toTest.X + AStarUtil.ColNum[i], toTest.Y + AStarUtil.RowNum[i]);
                AStarUtil.Warp(ref adj, Width, Height);
                if (!AStarUtil.IsValid(adj, Width, Height))
                {
                    continue;
                }

                var flags = Grid[adj.Y, adj.X].Flags;

                if (flags.CHasFlag(CellFlags.Wall))
                {
                    continue;
                }

                if (!closedList[adj.ToIdx(Width)])
                {
                    closedList[adj.ToIdx(Width)] = true;
                    adjs.Add(adj);
                }

                if (flags.CHasFlag(CellFlags.Pellet))
                {
                    ++pellets;
                }
                if (flags.CHasFlag(CellFlags.Space))
                {
                    ++spaces;
                }
            }

            if (spaces == 3 && pellets == 1)
            {
                Grid[toTest.Y, toTest.X].SetPellet(0);
                openList.AddRange(adjs.Where(a => !Grid[a.Y, a.X].HasFlag(CellFlags.Pellet)));
            }
            if ((spaces == 1 || spaces == 2) && pellets == 0)
            {
                // tunnel
                //Player.Print($"pred Un {toTest} from ori {x}:{y}");
                Grid[toTest.Y, toTest.X].SetPellet(0);
                openList.AddRange(adjs);
            }
        }
    }