Register for your free account! | Forgot your password?

You last visited: Today at 00:38

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



[Release] AStar Algorithm

Discussion on [Release] AStar Algorithm within the CO2 PServer Guides & Releases forum part of the CO2 Private Server category.

Reply
 
Old   #1
 
Mr_PoP's Avatar
 
elite*gold: 0
Join Date: Apr 2008
Posts: 759
Received Thanks: 285
[Release] AStar Algorithm

Well , I was working on the AStar Algorithm , and am almost finished it.
the way I intended to design it with so I don't need anther struct/class for Node, but anyways it needs some Optimization ("like using Heap (data structure) instead of Dictionary") but so far it works fine.

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MapData.Interfaces;

namespace MapData.Algorithm {
    public class AStar {
        IDMap map;
        List<Location2D> openset = new List<Location2D>(); // The set of tentative nodes to be evaluated, initially containing the start node.
        List<Location2D> closedset = new List<Location2D>(); // The set of nodes already evaluated.
        List<Location2D> path = new List<Location2D>(); // The path result.
        Dictionary<Location2D, Double> g_scores = new Dictionary<Location2D, Double>(); // Cost from start along best known path.
        Dictionary<Location2D, Double> h_scores = new Dictionary<Location2D, Double>(); // Estimated cost to get from a node to the goal.
        Dictionary<Location2D, Double> f_scores = new Dictionary<Location2D, Double>(); // Estimated total cost from start to goal.
        Dictionary<Location2D, Location2D> came_from = new Dictionary<Location2D, Location2D>(); // The navigated nodes.
        int[] ArrayX = { 0, -1, -1, -1, 0, 1, 1, 1 };
        int[] ArrayY = { 1, 1, 0, -1, -1, -1, 0, 1 };

        /// <summary>
        /// Initing the Algorithm with MapInfo
        /// </summary>
        /// <param name="map">DMap info.</param>
        public AStar(IDMap map) {
            this.map = map;
        }

        /// <summary>
        /// Start searching untile it's find it's goal or return false.
        /// </summary>
        /// <param name="start">Starting Node.</param>
        /// <param name="goal">Ending Node.</param>
        public bool Find( Location2D start, Location2D goal) {
            #region Clear
            openset.Clear();
            closedset.Clear();
            path.Clear();
            g_scores.Clear();
            h_scores.Clear();
            f_scores.Clear();
            came_from.Clear();
            #endregion

            openset.Add(start);
            g_scores[start] = 0;
            h_scores[start] = GetHValue(start, goal);
            f_scores[start] = g_scores[start] + h_scores[start];

            while (openset.Count != 0) { // while openset is not empty

                Location2D current = CheckBestNode(); //the node in openset having the lowest f_score[] value
                

                if (current.Equals(goal)) {
                    ReconstructPathRecursive(current);
                    return true;
                }

                Location2D neighbor = new Location2D();

                for (int i = 0; i < 8; i++) { // neighbor nodes.
                    neighbor.X = (ushort)(current.X + ArrayX[i]);
                    neighbor.Y = (ushort)(current.Y + ArrayY[i]);

                    //TODO: check walkeable areas!

                    g_scores[neighbor] = NeighborDistance(current, neighbor);
                    h_scores[neighbor] = GetHValue(neighbor, goal);
                    f_scores[neighbor] = g_scores[neighbor] + h_scores[neighbor];

                    bool tentative_is_better = false;
                    Double tentative_g_score = 0;

                    if (closedset.Contains(neighbor)) continue;

                    if (!openset.Contains(neighbor)) {
                        openset.Add(neighbor);
                        tentative_g_score = g_scores[neighbor] + GetHValue(neighbor, goal);
                        tentative_is_better = true;
                    } else if (tentative_g_score < g_scores[neighbor]) {
                        tentative_is_better = true;
                    } else {
                        tentative_is_better = false;
                    }

                    if (tentative_is_better) {
                        came_from[neighbor] = current;
                        g_scores[neighbor] = tentative_g_score;
                        h_scores[neighbor] = GetHValue(neighbor, goal);
                        f_scores[neighbor] = g_scores[neighbor] + h_scores[neighbor];
                    }
                }
            }
            return false;
        }

        /// <summary>
        /// Check the best node that has the smallest f value;
        /// </summary>
        /// <returns>The best Node.</returns>
        Location2D CheckBestNode() {
            var BestNode = f_scores.OrderBy(x => x.Value).First().Key;

            openset.Remove(BestNode);
            closedset.Add(BestNode);
            return BestNode;
        }

        private void ReconstructPathRecursive(Location2D current_node) {
            Location2D temp;
            if (came_from.TryGetValue(current_node, out temp)) {
                ReconstructPathRecursive(temp);
                path.Add(temp);
            }
        }

        int GetHValue(Location2D start, Location2D goal) {
            int nDx = Math.Abs(start.X - goal.X);
            int nDy = Math.Abs(start.Y - goal.Y);
            if (nDx > nDy)
                return 10 * nDx + 6 * nDy;
            return 10 * nDy + 6 * nDx;
        }

        readonly Double SQRT_2 = Math.Sqrt(2);

        protected  Double NeighborDistance(Location2D inStart, Location2D inEnd) {
            int diffX = Math.Abs(inStart.X - inEnd.X);
            int diffY = Math.Abs(inStart.Y - inEnd.Y);

            switch (diffX + diffY) {
                case 1: return 1;
                case 2: return SQRT_2;
                case 0: return 0;
                default:
                    throw new ApplicationException();
            }
        }

        public List<Location2D> Path {
            get {
                return path;
            }
        }
    }
}
for more info check A* search algorithm - Wikipedia, the free encyclopedia

how to use it , simply change Location2D to a Point or if you have your own location struct use it , so every map should have a PatherFinder , and so you can use it , I hope it help you guys , any suggestion to Improve it would be cool ;P.
Mr_PoP is offline  
Thanks
2 Users
Old 03/07/2012, 01:10   #2
 
elite*gold: 0
Join Date: Jan 2012
Posts: 164
Received Thanks: 22
im fine with struct and classes for nodes but it still awesome =) +k
injection illusion logic is offline  
Reply


Similar Threads Similar Threads
Astar/Pikker lvl Tipp
04/01/2011 - Last Chaos - 12 Replies
Wer Lilith der Flammen bekommen will sollte folgende Methode anwenden: 1. Hunger auf 1-20 sinken lassen 2. Astar sterben lassen bis Zuneigung auf 0 (am besten auf lvl 1) 3. Zuneigung mit Malerei wieder auf 5 machen 4. stupides lvln (darauf achten das Hunger nicht unter 6 und über 20 geht)
L2 enchant algorithm
03/18/2011 - Lineage 2 - 0 Replies
Hello everyone! I wan't to know the algorithm of the enchanting system on L2j if someone could help!.....I actually wanna know how it works and if there's something i can do to proliferate the chances to enchant a weapon for example! thanks guys! great forum!
Getting blowfish key and using the algorithm
03/17/2011 - SRO Coding Corner - 10 Replies
Good evening:) I am still busy with the packets of silkroad and I'm now trying to understand on how to decrypt the packets but I'm having some problems with it. I read drew benton's artikel about the silkroad security but I still don't know on which blowfish key is used to decrypt the packets. let's say these four packets are the first 4 S -> C 00000 25 00 00 50 00 00 0e d2 a9 27 80 2d c3 23 6c f6 %..P.....'.-.#l. 00016 00 00 00 62 00 00 00 8d ac fe 38 3d d0 9c ef 67 ...
Algorithm for Awarding Virtue Points.
03/13/2011 - CO2 Programming - 25 Replies
Does anyone have it? I mean the official algorithm, or as close as possible. If all else fails, I'll spend a while looking through the binaries for it... lol.



All times are GMT +2. The time now is 00:38.


Powered by vBulletin®
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2024 elitepvpers All Rights Reserved.