Esempio n. 1
0
        /// <summary>
        /// Builds a new path between two given locations.
        /// </summary>
        /// <param name="startPos">The starting position.</param>
        /// <param name="targetPos">The target position.</param>
        /// <param name="testMode">Whether or not the algorithm is being run in testing mode, or if it is live within a map visualization.</param>
        public override void Build(Coord2 startPos, Coord2 targetPos, bool testMode = false)
        {
            path  = new List <Coord2>();
            nodes = new NodeCollection(GridSize);

            searchedNodes = 0;

            this.start  = nodes.Get(startPos);
            this.target = nodes.Get(targetPos);

            // Initialize bot position
            nodes.Get(startPos).cost = 0;
            bool firstLoop = true;

            while (!target.closed)
            {
                if (firstLoop)
                {
                    currentLowest = start;
                    firstLoop     = false;
                }
                else
                {
                    FindLowestCost(); // Find lowest cost
                }
                // Mark lowest cost as closed
                currentLowest.closed = true;
                map.SetRenderColor(currentLowest.position, CLOSED_COLOR);

                // Find the neigbour positions
                List <Node> neighbours = GetNeighours(currentLowest);

                // Update visualization
                UpdateVisualization(neighbours);

                // Recalculate Costs
                RecalculateCosts(neighbours, currentLowest);

                // Update number of searched nodes
                searchedNodes++;
            }

            // Trace the completed path, if target has been found
            if (target.parent != null)
            {
                TracePath();
            }
        }
Esempio n. 2
0
        private void FindLowestCost()
        {
            currentLowestPos = targetPos;

            for (int x = 0; x < GridSize; x++)
            {
                for (int y = 0; y < GridSize; y++)
                {
                    // If cost is lower than current, position not closed, and position is valid within level, new lowest is found
                    if (nodes.Get(currentLowestPos.X, currentLowestPos.Y).cost >= nodes.Get(x, y).cost&& nodes.Get(x, y).closed == false &&
                        map.ValidPosition(new Coord2(x, y)))
                    {
                        currentLowestPos = new Coord2(x, y);
                    }
                }
            }
        }
Esempio n. 3
0
        public void Build(Coord2 startPos, Coord2 targetPos)
        {
            if (InputHandler.IsKeyPressed(Microsoft.Xna.Framework.Input.Keys.Enter))
            {
                this.startPos  = startPos;
                this.targetPos = targetPos;
                path           = new List <Coord2>();

                nodes = new NodeCollection(GridSize);

                // Initialize bot position
                nodes.Get(startPos.X, startPos.Y).cost = 0;
                bool firstLoop = true;

                while (nodes.Get(targetPos.X, targetPos.Y).closed == false)
                {
                    if (firstLoop)
                    {
                        currentLowestPos = startPos;
                        firstLoop        = false;
                    }
                    else
                    {
                        FindLowestCost(); // Find lowest cost
                    }
                    // Mark lowest cost as closed
                    nodes.Get(currentLowestPos.X, currentLowestPos.Y).closed = true;

                    // Find the neigbour positions
                    Coord2[] neighbours = GetNeighbours(currentLowestPos);

                    // Recalculate Costs
                    RecalculateCosts(neighbours, currentLowestPos);
                }

                // Trace the completed path
                TracePath();
            }
        }
Esempio n. 4
0
        public void Build(Coord2 startPos, Coord2 targetPos)
        {
            if(InputHandler.IsKeyPressed(Microsoft.Xna.Framework.Input.Keys.Enter))
            {
                this.startPos = startPos;
                this.targetPos = targetPos;
                path = new List<Coord2>();

                nodes = new NodeCollection(GridSize);

                // Initialize bot position
                nodes.Get(startPos.X, startPos.Y).cost = 0;
                bool firstLoop = true;

                while (nodes.Get(targetPos.X, targetPos.Y).closed == false)
                {
                    if (firstLoop)
                    {
                        currentLowestPos = startPos;
                        firstLoop = false;
                    }
                    else
                        FindLowestCost(); // Find lowest cost

                    // Mark lowest cost as closed
                    nodes.Get(currentLowestPos.X, currentLowestPos.Y).closed = true;

                    // Find the neigbour positions
                    Coord2[] neighbours = GetNeighbours(currentLowestPos);

                    // Recalculate Costs
                    RecalculateCosts(neighbours, currentLowestPos);
                }

                // Trace the completed path
                TracePath();
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Get the valid neighbour locations for the given node.
        /// </summary>
        protected virtual List <Node> GetNeighours(Node node)
        {
            List <Node> list = new List <Node>();

            // Horizontal and vertical
            if (map.ValidPosition(node.position + new Coord2(1, 0)))
            {
                list.Add(nodes.Get(node.position + new Coord2(1, 0)));
            }

            if (map.ValidPosition(node.position + new Coord2(-1, 0)))
            {
                list.Add(nodes.Get(node.position + new Coord2(-1, 0)));
            }

            if (map.ValidPosition(node.position + new Coord2(0, 1)))
            {
                list.Add(nodes.Get(node.position + new Coord2(0, 1)));
            }

            if (map.ValidPosition(node.position + new Coord2(0, -1)))
            {
                list.Add(nodes.Get(node.position + new Coord2(0, -1)));
            }

            // Diagonal
            if (map.ValidPosition(node.position + new Coord2(1, 1)))
            {
                list.Add(nodes.Get(node.position + new Coord2(1, 1)));
            }

            if (map.ValidPosition(node.position + new Coord2(-1, -1)))
            {
                list.Add(nodes.Get(node.position + new Coord2(-1, -1)));
            }

            if (map.ValidPosition(node.position + new Coord2(1, -1)))
            {
                list.Add(nodes.Get(node.position + new Coord2(1, -1)));
            }

            if (map.ValidPosition(node.position + new Coord2(-1, 1)))
            {
                list.Add(nodes.Get(node.position + new Coord2(-1, 1)));
            }

            return(list);
        }
Esempio n. 6
0
        /// <summary>
        /// Builds a new path between two given locations.
        /// </summary>
        /// <param name="startPos">The starting position.</param>
        /// <param name="targetPos">The target position.</param>
        /// <param name="testMode">Whether or not the algorithm is being run in testing mode, or if it is live within a map visualization.</param>
        public override void Build(Coord2 startPos, Coord2 targetPos, bool testMode = false)
        {
            path = new List<Coord2>();
            nodes = new NodeCollection(GridSize);

            searchedNodes = 0;

            this.start = nodes.Get(startPos);
            this.target = nodes.Get(targetPos);

            // Initialize bot position
            nodes.Get(startPos).cost = 0;
            bool firstLoop = true;

            while (!target.closed)
            {
                if (firstLoop)
                {
                    currentLowest = start;
                    firstLoop = false;
                }
                else
                    FindLowestCost(); // Find lowest cost

                // Mark lowest cost as closed
                currentLowest.closed = true;
                map.SetRenderColor(currentLowest.position, CLOSED_COLOR);

                // Find the neigbour positions
                List<Node> neighbours = GetNeighours(currentLowest);

                // Update visualization
                UpdateVisualization(neighbours);

                // Recalculate Costs
                RecalculateCosts(neighbours, currentLowest);

                // Update number of searched nodes
                searchedNodes++;
            }

            // Trace the completed path, if target has been found
            if(target.parent != null)
                TracePath();
        }