Ejemplo n.º 1
0
        /// <summary>
        /// Finds a path from own position to given position in maze.
        /// </summary>
        /// <returns>The path.</returns>
        /// <param name="map">Map.</param>
        /// <param name="start">Start.</param>
        /// <param name="end">End.</param>
        /// <param name="solution">Solution.</param>
        /// <param name="allowDiagonals">If set to <c>true</c> allow diagonals.</param>
        public AStarSearch.SearchState FindPath(Vec3di end, List <Vec3di> solution, bool allowDiagonals)
        {
            solution.Clear();

            AStarSearch astarsearch = new AStarSearch(map, ownPosition, end, allowDiagonals);

            AStarSearch.SearchState SearchState;

            do
            {
                SearchState = astarsearch.SearchStep();
            }while(SearchState == AStarSearch.SearchState.SEARCHING);

            if (SearchState == AStarSearch.SearchState.SUCCEEDED)
            {
                GridPosition mapPos = astarsearch.GetSolutionStart();

                while (mapPos != null)
                {
                    solution.Add(mapPos.Position);

                    mapPos = astarsearch.GetSolutionNext();
                }
                ;

                // Once you're done with the solution you can free the nodes up
                astarsearch.Cleanup();
            }
            else if (SearchState == AStarSearch.SearchState.FAILED)
            {
                //Zentronic.Debug.LogError("search terminated. Goal state not found!");
            }

            return(SearchState);
        }
Ejemplo n.º 2
0
    static void astar(String[] args)
    {
      XmlDocument doc = new XmlDocument();
      doc.Load("c:/njones/src/SkippingRock/weewar.net/map.xml");
      WeewarMap map = new WeewarMap(doc.DocumentElement);
      AStarSearch astarsearch = new AStarSearch();
      bool[] path = new bool[map.Height * map.Width];
      Unit u = new Unit();
      u.Type = UnitType.Trooper;
      List<Terrain> bases = map.getTerrainsByType(TerrainType.Base);
      Coordinate cs = bases[0].Coordinate;
      Coordinate ce = bases[bases.Count-1].Coordinate;

      SticksNode start = new SticksNode(map, cs, u);
      SticksNode end = new SticksNode(map, ce, u);

      astarsearch.SetStartAndGoalStates(start, end);

      SearchState searchState;
      uint searchSteps = 0;

      do
      {
        searchState = astarsearch.SearchStep();
        searchSteps++;
      }
      while (searchState == SearchState.Searching);

      if (searchState == SearchState.Succeeded)
      {
        Console.WriteLine("Search found goal state");
        SticksNode node = astarsearch.GetSolutionStart() as SticksNode;

        Console.WriteLine( "Displaying solution");
        int steps = 0;
        for (; ; )
        {
          node = astarsearch.GetSolutionNext() as SticksNode;
          if (node == null)
          {
            break;
          }

          
          path[node.Coordinate.Y * map.Height + node.Coordinate.X] = true;
          //node.PrintNodeInfo();
          steps++;
        };
        for (int y = 0; y < map.Height; y++)
        {
          if (y % 2 == 1) Console.Write(" ");
          for (int x = 0; x < map.Width; x++)
          {
            if (x == start.Coordinate.X &&
              y == start.Coordinate.Y)
              Console.Write("S");
            else if (x == end.Coordinate.X &&
              y == end.Coordinate.Y)
              Console.Write("G");
            else
              Console.Write(path[y * map.Height + x] ? "x" : "o");
          }
          Console.WriteLine();
        }
        Console.WriteLine("Solution steps {0}", steps);
        // Once you're done with the solution you can free the nodes up
        astarsearch.FreeSolutionNodes();
      }
      else if (searchState == SearchState.Failed)
      {
        Console.WriteLine("Search terminated. Did not find goal state");

      }

      // Display the number of loops the search went through
      Console.WriteLine("searchSteps : " + searchSteps);

    }
Ejemplo n.º 3
0
    // Main


    public static int main(string[] args)
    {

      Console.WriteLine("STL A* Search implementation\n(C)2001 Justin Heyes-Jones");

      // Our sample problem defines the world as a 2d array representing a terrain
      // Each element contains an integer from 0 to 5 which indicates the cost 
      // of travel across the terrain. Zero means the least possible difficulty 
      // in travelling (think ice rink if you can skate) whilst 5 represents the 
      // most difficult. 9 indicates that we cannot pass.

      // Create an instance of the search class...

      AStarSearch astarsearch = new AStarSearch();
      bool[] path = new bool[MAP_HEIGHT * MAP_WIDTH];

      // Create a start state
      MapSearchNode nodeStart = new MapSearchNode();
      nodeStart.x = 0;
      nodeStart.y = 0;

      // Define the goal state
      MapSearchNode nodeEnd = new MapSearchNode();
      nodeEnd.x = 19;
      nodeEnd.y = 19;

      // Set Start and goal states

      astarsearch.SetStartAndGoalStates(nodeStart, nodeEnd);

      SearchState searchState;
      uint searchSteps = 0;

      do
      {
        searchState = astarsearch.SearchStep();
        searchSteps++;
      }
      while (searchState == SearchState.Searching);

      if (searchState == SearchState.Succeeded)
      {
        Console.Write("Search found goal state");
        MapSearchNode node = astarsearch.GetSolutionStart() as MapSearchNode;

        Console.WriteLine( "Displaying solution");
        int steps = 0;
        node.PrintNodeInfo();
        for (; ; )
        {
          node = astarsearch.GetSolutionNext() as MapSearchNode;
          if (node == null)
          {
            break;
          }

          path[node.y * MAP_HEIGHT + node.x] = true;
          //node.PrintNodeInfo();
          steps++;
        };
        for (int y = 0; y < MAP_HEIGHT; y++)
        {
          for (int x = 0; x < MAP_WIDTH; x++)
          {
            Console.Write(path[y * MAP_HEIGHT + x] ? "x" : "o");
          }
          Console.WriteLine();
        }
        Console.WriteLine("Solution steps {0}", steps);
        // Once you're done with the solution you can free the nodes up
        astarsearch.FreeSolutionNodes();
      }
      else if (searchState == SearchState.Failed)
      {
        Console.WriteLine("Search terminated. Did not find goal state");

      }

      // Display the number of loops the search went through
      Console.WriteLine("searchSteps : " + searchSteps);

      return 0;
    }