Advertisement
imk0tter

Untitled

Jul 30th, 2011
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.38 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. using System.IO;
  7. using System.Threading;
  8. using pwnagebot.LotroInterface;
  9. using pwnagebot.GameInterface.Frameworks.Logging;
  10.  
  11. using System.Collections;
  12. namespace Delta9.Delta9Navigation
  13. {
  14.     public class Delta9Map
  15.     {
  16.         float mapGridSize;
  17.         ulong mapGridCount;
  18.  
  19.         HashSet<ulong> mapData;
  20.  
  21.         Delta9Instance instance;
  22.         Thread updateThread;
  23.         Thread removeThread;
  24.         bool running;
  25.         bool _running;
  26.         EventHandler updateMap;
  27.         public Delta9Map(Delta9Instance instance)
  28.         {
  29.             this.instance = instance;
  30.             updateMap = new EventHandler(UpdateMap);
  31.             mapGridSize = 2.5f;
  32.             mapGridCount = 4294967294;
  33.             mapData = new HashSet<ulong>();
  34.             running = false;
  35.             _running = false;
  36.         }
  37.         public void RemoveMapRun()
  38.         {
  39.             _running = true;
  40.             while (_running)
  41.             {
  42.                 LotroEntity me = (LotroEntity)instance.Interface.EntityManager.Me;
  43.                 if (me != null)
  44.                 {
  45.                     me.Update();
  46.                     if (RemoveMapData(me.X, me.Y))
  47.                     {
  48.                         Debug("Removed waypoint: (" + me.X + ", " + me.Y + ")");
  49.                     }
  50.                 }
  51.                 Thread.Sleep(25);
  52.             }
  53.         }
  54.         public void UpdateMapRun()
  55.         {
  56.             running = true;
  57.             while (running)
  58.             {
  59.                 LotroEntity me = (LotroEntity)instance.Interface.EntityManager.Me;
  60.                 if (me != null)
  61.                 {
  62.                     me.Update();
  63.                     if (AddMapData(me.X, me.Y))
  64.                     {
  65.                         Debug("Added new waypoint: (" + me.X + ", " + me.Y + ")");
  66.                     }
  67.                 }
  68.                 Thread.Sleep(25);
  69.             }
  70.         }
  71.         public bool ContainsKey(ulong mapKey)
  72.         {
  73.             return mapData.Contains(mapKey);
  74.         }
  75.         public void UpdateMap(object o, EventArgs e)
  76.         {
  77.             LotroEntity me = (LotroEntity)instance.Interface.EntityManager.Me;
  78.  
  79.             if (AddMapData(me.X, me.Y)) Debug("Added new waypoint: (" + me.X + ", " + me.Y + ")");
  80.  
  81.         }
  82.         public void Debug(string s)
  83.         {
  84.             instance.debug(s);
  85.         }
  86.         public void StartRemoving()
  87.         {
  88.             if (!_running)
  89.             {
  90.                 removeThread = new Thread(RemoveMapRun);
  91.                 removeThread.Start();
  92.                 Debug("Starting remover...");
  93.             }
  94.         }
  95.         public void StopRemoving()
  96.         {
  97.             if (_running)
  98.             {
  99.                 _running = false;
  100.                 Debug("Stopping remover...");
  101.             }
  102.         }
  103.         public void StartMapping()
  104.         {
  105.             if (!running)
  106.             {
  107.                 updateThread = new Thread(UpdateMapRun);
  108.                 updateThread.Start();
  109.                 //instance.Interface.OnUpdate += updateMap;
  110.                 Debug("Starting mapper...");
  111.             }
  112.         }
  113.         public void StopMapping()
  114.         {
  115.             if (running)
  116.             {
  117.                 running = false;
  118.                 //instance.Interface.OnUpdate -= updateMap;
  119.                 Debug("Stopping mapper...");
  120.             }
  121.         }
  122.         public ulong GetMapKey(float x, float y)
  123.         {
  124.             ulong mapX = GetMapCoord(x);
  125.             ulong mapY = GetMapCoord(y);
  126.  
  127.             return GetMapKey(mapX, mapY);
  128.         }
  129.         public ulong GetMapKey(ulong x, ulong y) { return ((y * mapGridCount) + x); }
  130.         public ulong GetMapCoord(float coord) { return (ulong)Math.Round(coord / mapGridSize); }
  131.  
  132.         public void GetMapCoords(ref ulong x, ref ulong y, ulong mapKey)
  133.         {
  134.             x = mapKey % mapGridCount;
  135.             mapKey -= x;
  136.             y = mapKey / mapGridCount;
  137.         }
  138.  
  139.         class PathNode
  140.         {
  141.             public ulong mapKey;
  142.             public ulong x, y;
  143.             public float F, G, H;
  144.             public PathNode parent;
  145.  
  146.             public PathNode(ulong mapKey, ulong x, ulong y, PathNode parent)
  147.             {
  148.                 this.mapKey = mapKey;
  149.                 this.x = x;
  150.                 this.y = y;
  151.                 this.parent = parent;
  152.                 G = F = H = 0;
  153.             }
  154.  
  155.             public void GenerateGValue()
  156.             {
  157.  
  158.                 ulong _x = 0;
  159.                 ulong _y = 0;
  160.                 G = 0;
  161.                 if (parent != null)
  162.                 {
  163.                     _x = (ulong)Math.Abs((float)(parent.y - y));
  164.                     _y = (ulong)Math.Abs((float)(parent.x - x));
  165.                     G = (float)Math.Sqrt((_x * _x) + (_y + _y)) + parent.G;
  166.                 }
  167.             }
  168.             public void GenerateHValue(PathNode dst)
  169.             {
  170.                 H = (float)(Math.Abs((long)(dst.x - x)) + Math.Abs((long)(dst.y - y)));
  171.             }
  172.             public void GenerateFValue(PathNode dst)
  173.             {
  174.                 GenerateHValue(dst);
  175.                 GenerateGValue();
  176.                 F = G + H;
  177.             }
  178.         }
  179.  
  180.         public void FillOpenSet(HashSet<ulong> openSet, HashSet<ulong> closedSet, ulong curKey, ulong dstKey, Hashtable pathnodeHash)
  181.         {
  182.             PathNode currentPath = (PathNode)pathnodeHash[curKey];
  183.             PathNode destPath = (PathNode)pathnodeHash[dstKey];
  184.  
  185.             HashSet<ulong> localArea = new HashSet<ulong>();
  186.  
  187.             ulong x = currentPath.x, y = currentPath.y;
  188.  
  189.             ulong tr = GetMapKey(x - 1, y + 1); localArea.Add(tr);
  190.             ulong t = GetMapKey(x, y + 1); localArea.Add(t);
  191.             ulong tl = GetMapKey(x + 1, y + 1); localArea.Add(tl);
  192.             ulong mr = GetMapKey(x - 1, y); localArea.Add(mr);
  193.             ulong ml = GetMapKey(x + 1, y); localArea.Add(ml);
  194.             ulong br = GetMapKey(x - 1, y - 1); localArea.Add(br);
  195.             ulong b = GetMapKey(x, y - 1); localArea.Add(b);
  196.             ulong bl = GetMapKey(x + 1, y - 1); localArea.Add(bl);
  197.             foreach (ulong l in localArea)
  198.             {
  199.                 if (l == dstKey)
  200.                 {
  201.                     destPath.parent = currentPath;
  202.                     closedSet.Add(dstKey);
  203.                 }
  204.                 else if (!closedSet.Contains(l) && mapData.Contains(l) && !openSet.Contains(l))
  205.                 {
  206.                     ulong _x = 0, _y = 0;
  207.                     GetMapCoords(ref _x, ref _y, l);
  208.                     PathNode newNode = new PathNode(l, _x, _y, currentPath);
  209.                     pathnodeHash.Add(l, newNode);
  210.  
  211.                     newNode.GenerateFValue(destPath);
  212.  
  213.                     openSet.Add(l);
  214.                 }
  215.                 else if (!closedSet.Contains(l) && mapData.Contains(l) && openSet.Contains(l))
  216.                 {
  217.                     PathNode nextNode = (PathNode)pathnodeHash[l];
  218.                     nextNode.parent = currentPath;
  219.                     nextNode.GenerateFValue(destPath);
  220.                 }
  221.             }
  222.         }
  223.  
  224.         public List<Waypoint> GeneratePath(float sx, float sy, float dx, float dy)
  225.         {
  226.             Hashtable pathnodeHash = new Hashtable();
  227.  
  228.             HashSet<ulong> openSet = new HashSet<ulong>();
  229.             HashSet<ulong> closedSet = new HashSet<ulong>();
  230.  
  231.             long timeOut = 5000;
  232.  
  233.             List<Waypoint> waypoints = new List<Waypoint>();
  234.            
  235.             ulong srcMapKey = GetMapKey(sx, sy);
  236.             ulong dstMapKey = GetMapKey(dx, dy);
  237.  
  238.             if (srcMapKey == dstMapKey) return waypoints;
  239.  
  240.             ulong _x = 0, _y = 0;
  241.  
  242.             GetMapCoords(ref _x, ref _y, srcMapKey);
  243.             PathNode srcPath = new PathNode(srcMapKey, _x, _y, null);
  244.  
  245.             GetMapCoords(ref _x, ref _y, dstMapKey);
  246.             PathNode dstPath = new PathNode(dstMapKey, _x, _y, null);
  247.  
  248.             openSet.Add(srcMapKey);
  249.  
  250.             DateTime startTime = DateTime.Now;
  251.  
  252.             pathnodeHash.Add(srcMapKey, srcPath);
  253.             pathnodeHash.Add(dstMapKey, dstPath);
  254.  
  255.             while (openSet.Count > 0)
  256.             {
  257.                 TimeSpan ts = DateTime.Now - startTime;
  258.                 if (ts.TotalMilliseconds >= timeOut) break;
  259.  
  260.                 float currFValue = -1;
  261.                 ulong currMapValue = 0;
  262.  
  263.                 foreach (ulong l in openSet)
  264.                 {
  265.                     PathNode currentPath = (PathNode)pathnodeHash[l];
  266.  
  267.                     float fValue = currentPath.F;
  268.                     if (currFValue == -1 || fValue < currFValue)
  269.                     {
  270.                         currFValue = fValue;
  271.                         currMapValue = l;
  272.                     }
  273.                 }
  274.  
  275.                 if (currMapValue > 0)
  276.                 {
  277.                     //ulong x = 0, y = 0;
  278.                     //GetMapCoords(ref x, ref y, currMapValue);
  279.                     //Debug("Added point (" + (x * mapGridSize) + ", " + (y * mapGridSize) + ") to the closed set");
  280.                     openSet.Remove(currMapValue);
  281.                     closedSet.Add(currMapValue);
  282.                     FillOpenSet(openSet, closedSet, currMapValue, dstMapKey, pathnodeHash);
  283.                     if (closedSet.Contains(dstMapKey))
  284.                     {
  285.                         Debug("Path find time: " + ts.TotalSeconds + " seconds");
  286.                         break;
  287.                     }
  288.                 }
  289.             }
  290.             if (closedSet.Contains(dstMapKey))
  291.             {
  292.                 PathNode currentPathNode = dstPath;
  293.                 while (currentPathNode.parent != null)
  294.                 {
  295.                     //Debug("Added (" + (currentPathNode.x * mapGridSize) + ", " + (currentPathNode.y * mapGridSize) + ") to the waypoint list");
  296.                     waypoints.Add(new Waypoint(currentPathNode.x * mapGridSize, currentPathNode.y * mapGridSize));
  297.                     currentPathNode = currentPathNode.parent;
  298.                 }
  299.                 waypoints.Reverse();
  300.                 return waypoints;
  301.             }
  302.             return null;
  303.         }
  304.         public bool AddMapData(ulong x, ulong y)
  305.         {
  306.             ulong mapKey = GetMapKey(x, y);
  307.             return mapData.Add(mapKey);
  308.         }
  309.         public bool RemoveMapData(ulong x, ulong y)
  310.         {
  311.             ulong mapKey = GetMapKey(x, y);
  312.             return mapData.Remove(mapKey);
  313.         }
  314.         public bool AddMapData(float x, float y)
  315.         {
  316.             ulong mapKey = GetMapKey(x, y);
  317.             return mapData.Add(mapKey);
  318.         }
  319.         public bool RemoveMapData(float x, float y)
  320.         {
  321.             ulong mapKey = GetMapKey(x, y);
  322.             return mapData.Remove(mapKey);
  323.         }
  324.         public bool ExportMapData(string fileName)
  325.         {
  326.             try
  327.             {
  328.                 FileStream fileStream = new FileStream("data\\" + fileName, FileMode.Create);
  329.                 foreach (ulong l in mapData)
  330.                 {
  331.                     byte[] b = new byte[8];
  332.                     for(int i= 0; i < 8; ++i){  
  333.                        b[7 - i] = (byte)(l >> (i * 8));  
  334.                     }  
  335.                     fileStream.Write(b, 0, 8);
  336.                 }
  337.                 fileStream.Flush();
  338.                 fileStream.Close();
  339.                 return true;
  340.             }
  341.             catch (IOException e)
  342.             {
  343.                 return false;
  344.             }
  345.  
  346.         }
  347.         public bool ImportMapData(string fileName)
  348.         {
  349.             try
  350.             {
  351.                 FileStream fileStream = new FileStream("data\\" + fileName, FileMode.OpenOrCreate);
  352.                 for (;fileStream.Position + 8 <= fileStream.Length;)
  353.                 {
  354.                     ulong l = 0;
  355.                     byte[] b = new byte[8];
  356.                     fileStream.Read(b, 0, 8);
  357.                     for (int j = 0; j < 8; ++j)
  358.                     {
  359.                         l <<= 8;
  360.                         l ^= (ulong)b[j] & 0xFF;
  361.                     }
  362.                     mapData.Add(l);
  363.                    
  364.                 }
  365.                 fileStream.Close();
  366.                 return true;
  367.             }
  368.             catch (IOException e)
  369.             {
  370.                 return false;
  371.             }
  372.         }
  373.     }
  374. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement