Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- using System.Threading;
- using pwnagebot.LotroInterface;
- using pwnagebot.GameInterface.Frameworks.Logging;
- using System.Collections;
- namespace Delta9.Delta9Navigation
- {
- public class Delta9Map
- {
- float mapGridSize;
- ulong mapGridCount;
- HashSet<ulong> mapData;
- Delta9Instance instance;
- Thread updateThread;
- Thread removeThread;
- bool running;
- bool _running;
- EventHandler updateMap;
- public Delta9Map(Delta9Instance instance)
- {
- this.instance = instance;
- updateMap = new EventHandler(UpdateMap);
- mapGridSize = 2.5f;
- mapGridCount = 4294967294;
- mapData = new HashSet<ulong>();
- running = false;
- _running = false;
- }
- public void RemoveMapRun()
- {
- _running = true;
- while (_running)
- {
- LotroEntity me = (LotroEntity)instance.Interface.EntityManager.Me;
- if (me != null)
- {
- me.Update();
- if (RemoveMapData(me.X, me.Y))
- {
- Debug("Removed waypoint: (" + me.X + ", " + me.Y + ")");
- }
- }
- Thread.Sleep(25);
- }
- }
- public void UpdateMapRun()
- {
- running = true;
- while (running)
- {
- LotroEntity me = (LotroEntity)instance.Interface.EntityManager.Me;
- if (me != null)
- {
- me.Update();
- if (AddMapData(me.X, me.Y))
- {
- Debug("Added new waypoint: (" + me.X + ", " + me.Y + ")");
- }
- }
- Thread.Sleep(25);
- }
- }
- public bool ContainsKey(ulong mapKey)
- {
- return mapData.Contains(mapKey);
- }
- public void UpdateMap(object o, EventArgs e)
- {
- LotroEntity me = (LotroEntity)instance.Interface.EntityManager.Me;
- if (AddMapData(me.X, me.Y)) Debug("Added new waypoint: (" + me.X + ", " + me.Y + ")");
- }
- public void Debug(string s)
- {
- instance.debug(s);
- }
- public void StartRemoving()
- {
- if (!_running)
- {
- removeThread = new Thread(RemoveMapRun);
- removeThread.Start();
- Debug("Starting remover...");
- }
- }
- public void StopRemoving()
- {
- if (_running)
- {
- _running = false;
- Debug("Stopping remover...");
- }
- }
- public void StartMapping()
- {
- if (!running)
- {
- updateThread = new Thread(UpdateMapRun);
- updateThread.Start();
- //instance.Interface.OnUpdate += updateMap;
- Debug("Starting mapper...");
- }
- }
- public void StopMapping()
- {
- if (running)
- {
- running = false;
- //instance.Interface.OnUpdate -= updateMap;
- Debug("Stopping mapper...");
- }
- }
- public ulong GetMapKey(float x, float y)
- {
- ulong mapX = GetMapCoord(x);
- ulong mapY = GetMapCoord(y);
- return GetMapKey(mapX, mapY);
- }
- public ulong GetMapKey(ulong x, ulong y) { return ((y * mapGridCount) + x); }
- public ulong GetMapCoord(float coord) { return (ulong)Math.Round(coord / mapGridSize); }
- public void GetMapCoords(ref ulong x, ref ulong y, ulong mapKey)
- {
- x = mapKey % mapGridCount;
- mapKey -= x;
- y = mapKey / mapGridCount;
- }
- class PathNode
- {
- public ulong mapKey;
- public ulong x, y;
- public float F, G, H;
- public PathNode parent;
- public PathNode(ulong mapKey, ulong x, ulong y, PathNode parent)
- {
- this.mapKey = mapKey;
- this.x = x;
- this.y = y;
- this.parent = parent;
- G = F = H = 0;
- }
- public void GenerateGValue()
- {
- ulong _x = 0;
- ulong _y = 0;
- G = 0;
- if (parent != null)
- {
- _x = (ulong)Math.Abs((float)(parent.y - y));
- _y = (ulong)Math.Abs((float)(parent.x - x));
- G = (float)Math.Sqrt((_x * _x) + (_y + _y)) + parent.G;
- }
- }
- public void GenerateHValue(PathNode dst)
- {
- H = (float)(Math.Abs((long)(dst.x - x)) + Math.Abs((long)(dst.y - y)));
- }
- public void GenerateFValue(PathNode dst)
- {
- GenerateHValue(dst);
- GenerateGValue();
- F = G + H;
- }
- }
- public void FillOpenSet(HashSet<ulong> openSet, HashSet<ulong> closedSet, ulong curKey, ulong dstKey, Hashtable pathnodeHash)
- {
- PathNode currentPath = (PathNode)pathnodeHash[curKey];
- PathNode destPath = (PathNode)pathnodeHash[dstKey];
- HashSet<ulong> localArea = new HashSet<ulong>();
- ulong x = currentPath.x, y = currentPath.y;
- ulong tr = GetMapKey(x - 1, y + 1); localArea.Add(tr);
- ulong t = GetMapKey(x, y + 1); localArea.Add(t);
- ulong tl = GetMapKey(x + 1, y + 1); localArea.Add(tl);
- ulong mr = GetMapKey(x - 1, y); localArea.Add(mr);
- ulong ml = GetMapKey(x + 1, y); localArea.Add(ml);
- ulong br = GetMapKey(x - 1, y - 1); localArea.Add(br);
- ulong b = GetMapKey(x, y - 1); localArea.Add(b);
- ulong bl = GetMapKey(x + 1, y - 1); localArea.Add(bl);
- foreach (ulong l in localArea)
- {
- if (l == dstKey)
- {
- destPath.parent = currentPath;
- closedSet.Add(dstKey);
- }
- else if (!closedSet.Contains(l) && mapData.Contains(l) && !openSet.Contains(l))
- {
- ulong _x = 0, _y = 0;
- GetMapCoords(ref _x, ref _y, l);
- PathNode newNode = new PathNode(l, _x, _y, currentPath);
- pathnodeHash.Add(l, newNode);
- newNode.GenerateFValue(destPath);
- openSet.Add(l);
- }
- else if (!closedSet.Contains(l) && mapData.Contains(l) && openSet.Contains(l))
- {
- PathNode nextNode = (PathNode)pathnodeHash[l];
- nextNode.parent = currentPath;
- nextNode.GenerateFValue(destPath);
- }
- }
- }
- public List<Waypoint> GeneratePath(float sx, float sy, float dx, float dy)
- {
- Hashtable pathnodeHash = new Hashtable();
- HashSet<ulong> openSet = new HashSet<ulong>();
- HashSet<ulong> closedSet = new HashSet<ulong>();
- long timeOut = 5000;
- List<Waypoint> waypoints = new List<Waypoint>();
- ulong srcMapKey = GetMapKey(sx, sy);
- ulong dstMapKey = GetMapKey(dx, dy);
- if (srcMapKey == dstMapKey) return waypoints;
- ulong _x = 0, _y = 0;
- GetMapCoords(ref _x, ref _y, srcMapKey);
- PathNode srcPath = new PathNode(srcMapKey, _x, _y, null);
- GetMapCoords(ref _x, ref _y, dstMapKey);
- PathNode dstPath = new PathNode(dstMapKey, _x, _y, null);
- openSet.Add(srcMapKey);
- DateTime startTime = DateTime.Now;
- pathnodeHash.Add(srcMapKey, srcPath);
- pathnodeHash.Add(dstMapKey, dstPath);
- while (openSet.Count > 0)
- {
- TimeSpan ts = DateTime.Now - startTime;
- if (ts.TotalMilliseconds >= timeOut) break;
- float currFValue = -1;
- ulong currMapValue = 0;
- foreach (ulong l in openSet)
- {
- PathNode currentPath = (PathNode)pathnodeHash[l];
- float fValue = currentPath.F;
- if (currFValue == -1 || fValue < currFValue)
- {
- currFValue = fValue;
- currMapValue = l;
- }
- }
- if (currMapValue > 0)
- {
- //ulong x = 0, y = 0;
- //GetMapCoords(ref x, ref y, currMapValue);
- //Debug("Added point (" + (x * mapGridSize) + ", " + (y * mapGridSize) + ") to the closed set");
- openSet.Remove(currMapValue);
- closedSet.Add(currMapValue);
- FillOpenSet(openSet, closedSet, currMapValue, dstMapKey, pathnodeHash);
- if (closedSet.Contains(dstMapKey))
- {
- Debug("Path find time: " + ts.TotalSeconds + " seconds");
- break;
- }
- }
- }
- if (closedSet.Contains(dstMapKey))
- {
- PathNode currentPathNode = dstPath;
- while (currentPathNode.parent != null)
- {
- //Debug("Added (" + (currentPathNode.x * mapGridSize) + ", " + (currentPathNode.y * mapGridSize) + ") to the waypoint list");
- waypoints.Add(new Waypoint(currentPathNode.x * mapGridSize, currentPathNode.y * mapGridSize));
- currentPathNode = currentPathNode.parent;
- }
- waypoints.Reverse();
- return waypoints;
- }
- return null;
- }
- public bool AddMapData(ulong x, ulong y)
- {
- ulong mapKey = GetMapKey(x, y);
- return mapData.Add(mapKey);
- }
- public bool RemoveMapData(ulong x, ulong y)
- {
- ulong mapKey = GetMapKey(x, y);
- return mapData.Remove(mapKey);
- }
- public bool AddMapData(float x, float y)
- {
- ulong mapKey = GetMapKey(x, y);
- return mapData.Add(mapKey);
- }
- public bool RemoveMapData(float x, float y)
- {
- ulong mapKey = GetMapKey(x, y);
- return mapData.Remove(mapKey);
- }
- public bool ExportMapData(string fileName)
- {
- try
- {
- FileStream fileStream = new FileStream("data\\" + fileName, FileMode.Create);
- foreach (ulong l in mapData)
- {
- byte[] b = new byte[8];
- for(int i= 0; i < 8; ++i){
- b[7 - i] = (byte)(l >> (i * 8));
- }
- fileStream.Write(b, 0, 8);
- }
- fileStream.Flush();
- fileStream.Close();
- return true;
- }
- catch (IOException e)
- {
- return false;
- }
- }
- public bool ImportMapData(string fileName)
- {
- try
- {
- FileStream fileStream = new FileStream("data\\" + fileName, FileMode.OpenOrCreate);
- for (;fileStream.Position + 8 <= fileStream.Length;)
- {
- ulong l = 0;
- byte[] b = new byte[8];
- fileStream.Read(b, 0, 8);
- for (int j = 0; j < 8; ++j)
- {
- l <<= 8;
- l ^= (ulong)b[j] & 0xFF;
- }
- mapData.Add(l);
- }
- fileStream.Close();
- return true;
- }
- catch (IOException e)
- {
- return false;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement