Example #1
0
    public TDPath checkPath(TDMap map, TDTile start, int maxStamina)
    {
        TDTile selected = new TDTile();
        TDTile finalSelected = new TDTile();
        TDTile pathRun = new TDTile();
        ArrayList finalPath = new ArrayList();
        ArrayList unvisited = new ArrayList();
        for (int i = start.position - maxStamina - maxStamina * map.getWidth(); i < start.position + maxStamina + maxStamina * map.getWidth(); i++)
        {
            if (i > 0 && i < map.getWidth() * map.getHeight())
            {
                map.getTile(i).distance = 20000;
                map.getTile(i).previousTile = null;
                unvisited.Add(map.getTile(i));
            }
            Debug.Log("On position " + i);
            if (i % map.getWidth() == start.x + maxStamina)
            {

                i += map.getWidth() - maxStamina * 2 - 1;
                Debug.Log("Switched Rows " + i);

            }
        }
        map.getTile(start.position).distance = 0;
        int looped = 0;
        int finalDistance = -1;
        while (unvisited.Count > 0 && !selected.equals(map.getTile((int)currentTileCoord.x, (int)currentTileCoord.z)) && selected.distance < maxStamina)
        {
            looped++;
            selected.distance = 200000;
            for (int i = 0; i < unvisited.Count; i++)
            {
                TDTile comparing = (TDTile)unvisited[i];
                if (comparing.distance < selected.distance)
                {

                    selected = comparing;
                    if (selected.equals(map.getTile((int)currentTileCoord.x, (int)currentTileCoord.z)))
                    {
                        if (selected.distance > maxStamina)
                        {
                            return null;
                        }
                        finalDistance = selected.distance;
                        finalSelected = selected;
                        pathRun = finalSelected;
                        while (pathRun.previousTile != null)
                        {
                            finalPath.Add(pathRun);
                            pathRun = pathRun.previousTile;
                        }

                        return new TDPath(finalPath, finalDistance);
                    }
                    Debug.Log(" Selected: " + selected + " " + selected.distance + "looped: " + looped);
                }
                unvisited.Remove(selected);
                for (int j = 0; j < selected.findNeighbors().Length; j++)
                {
                    TDTile neighbor = map.getTile(selected.findNeighbors()[j]);
                    if (neighbor != null)
                    {
                        if (unvisited.Contains(neighbor))
                        {
                            int alternate = selected.distance + 1;
                            if (alternate < neighbor.distance)
                            {
                                neighbor.distance = alternate;
                                neighbor.previousTile = selected;
                            }

                        }
                    }
                }
            }
        }
        return null;
    }