Beispiel #1
0
 public AStarSearch(Grid3di map, Vec3di start, Vec3di goal, bool AllowDiagonal)
 {
     searchState     = SearchState.NOT_INITIALISED;
     curSolutionNode = null;
     cancelRequested = false;
     SetStartAndGoalStates(map, start, goal);
     allowDiagonal = AllowDiagonal;
 }
 public Grid3di(Grid3di rhs)
 {
     _width  = 0;
     _height = 0;
     _depth  = 0;
     _data   = null;
     CopyFrom(rhs);
 }
        void CopyFrom(Grid3di rhs)
        {
            _width  = rhs._width;
            _height = rhs._height;
            _depth  = rhs._depth;

            Resize(_width, _height, _depth);

            if (_width > 0 && _height > 0 && _depth > 0 && _data != null)
            {
                Array.Copy(rhs._data, _data, _width * _height * _depth);
            }

            _borderCode = rhs._borderCode;
        }
Beispiel #4
0
        public void ApplyToMap(Grid3di map, int emptyCode)
        {
            map.Resize(w * 3, h * 3, 1);

            for (int Y = 0; Y < h; Y++)
            {
                for (int X = 0; X < w; X++)
                {
                    Maze.Cell cell = GetAt(X, Y);

                    int x0 = X * 3;
                    int y0 = Y * 3;

                    // fill block
                    for (int y = y0; y < y0 + 3; y++)
                    {
                        for (int x = x0; x < x0 + 3; x++)
                        {
                            map.SetAt(x, y, map.BorderCode);
                        }
                    }

                    // empty field in the center
                    map.SetAt(x0 + 1, y0 + 1, emptyCode);

                    if (cell.right == false)
                    {
                        map.SetAt(x0 + 2, y0 + 1, emptyCode);
                    }

                    if (cell.left == false)
                    {
                        map.SetAt(x0 + 0, y0 + 1, emptyCode);
                    }

                    if (cell.upper == false)
                    {
                        map.SetAt(x0 + 1, y0 + 0, emptyCode);
                    }

                    if (cell.lower == false)
                    {
                        map.SetAt(x0 + 1, y0 + 2, emptyCode);
                    }
                }
            }
        }
Beispiel #5
0
            // Set Start and goal states
            void SetStartAndGoalStates(Grid3di map, Vec3di start, Vec3di goal)
            {
                cancelRequested = false;

                startNode = new SearchNode(new GridPosition(map, start.x, start.y, start.z));
                endNode   = new SearchNode(new GridPosition(map, goal.x, goal.y, goal.z));

                searchState = SearchState.SEARCHING;

                // Initialise the AStar specific parts of the Start Node
                // The user only needs fill out the state information
                startNode.g        = 0;
                startNode.h        = startNode.position.EstimatedDistanceToNode(endNode.position);
                startNode.f        = startNode.g + startNode.h;
                startNode.prevNode = null;

                openNodes.Clear();
                // Push the start node on the Open list
                openNodes.Add(startNode);   // heap now unsorted

                // Initialise counter for search steps
                searchSteps = 0;
            }
Beispiel #6
0
        static public void TestFunction()
        {
            String Map = "##############################" +
                         "#               #        #   #" +
                         "#    #  ###     #   #    #   #" +
                         "#    #    #aaaaa#   #    #   #" +
                         "#    #    #aaaaa#   #    #   #" +
                         "#    #    #aaaaaa   #    #   #" +
                         "#    ###  #######   #    #   #" +
                         "#      #            #        #" +
                         "#      #            #        #" +
                         "##############################";

            Grid3di map = new Grid3di();

            map.Resize(30, 10);
            map.BorderCode = 99;

            Dictionary <char, int> valueLookup = new Dictionary <char, int>();

            valueLookup['#'] = map.BorderCode;
            valueLookup[' '] = 0;
            valueLookup['a'] = 1;

            map.ParseLayerFromString(Map, valueLookup, 0);

            Dictionary <int, char> codeLookup = new Dictionary <int, char>();

            codeLookup[0] = '=';
            codeLookup[map.BorderCode] = '@';
            codeLookup[1] = 'a';
            codeLookup[9] = '*';  // symbol for path

            map.ShowDebugInfo(codeLookup);

            List <Vec3di> solution = new List <Vec3di>();

            AStarAgent pf = new AStarAgent();

            pf.ownPosition = new Vec3di(2, 9, 0);
            pf.map         = map;

            if (pf.FindPath(new Vec3di(28, 2, 0), solution, false) == AStarSearch.SearchState.SUCCEEDED)
            {
                Grid3di copy = new Grid3di(map);

                int i = 1;
                foreach (Vec3di step in solution)
                {
                    //Zentronic.Debug.Log("step " + i + ":" +  step.x + "/" + step.y);
                    i++;
                    copy.SetAt(step.x, step.y, step.z, 9);
                }

                copy.ShowDebugInfo(codeLookup);
            }
            else
            {
                Debug.LogError("unable to find path!");
            }
        }
Beispiel #7
0
 public GridPosition(Grid3di Map, Vec3di p)
 {
     grid = Map;
     pos  = p;
 }
Beispiel #8
0
 public GridPosition(Grid3di Map, int px, int py, int pz)
 {
     grid = Map;
     pos  = new Vec3di(px, py, pz);
 }
Beispiel #9
0
 public GridPosition(Grid3di Map)
 {
     grid = Map;
     pos  = new Vec3di();
 }