public static Vertex ClosestVetrex(string town, Point3D location) { if (town == null || !m_GraphDefinitions.ContainsKey(town)) { return(null); } List <Vertex> vertices = m_GraphDefinitions[town]; Vertex closest = null; double min = int.MaxValue; double distance; foreach (Vertex v in vertices) { distance = Math.Sqrt(Math.Pow(location.X - v.Location.X, 2) + Math.Pow(location.Y - v.Location.Y, 2)); if (distance < min) { closest = v; min = distance; } } return(closest); }
public override void OnThink() { base.OnThink(); if (ControlMaster != null && m_Path != null && m_Path.Count > 0) { Vertex v = m_Path[m_Path.Count - 1]; Mobile m = ControlMaster; if (m.NetState != null) { if (Math.Abs(v.DistanceTo(m.Location) - v.DistanceTo(Location)) > 10) { Frozen = true; } else { Frozen = false; } if (CurrentWayPoint == null) { CurrentWayPoint = new WayPoint(); CurrentWayPoint.MoveToWorld(v.Location, Map); } int dist = v.DistanceTo(Location); if (dist < (v.Teleporter ? 1 : 3) || dist > 100) { m_Path.RemoveAt(m_Path.Count - 1); if (m_Path.Count > 0) { if (CurrentWayPoint == null) { CurrentWayPoint = new WayPoint(); } CurrentWayPoint.MoveToWorld(m_Path[m_Path.Count - 1].Location, Map); } else { Timer.DelayCall <Mobile>(TimeSpan.FromSeconds(3), CommandFollow, m); Say(1076051); // We have reached our destination CommandStop(m); } } } } }
public override void OnResponse(NetState sender, RelayInfo info) { int shop = info.ButtonID - 100; if (m_Guide == null || m_Guide.Deleted || m_Guide.Region == null || info.ButtonID == 0) { return; } Vertex source = GuideHelper.ClosestVetrex(m_Guide.Region.Name, m_Guide.Location); if (m_Shops.ContainsKey(shop)) { Vertex destination = m_Shops[shop]; List <Vertex> path = GuideHelper.Dijkstra(m_Guide.Region.Name, source, destination); m_Guide.StartGuiding(path); } }
public GuideVertexEditGump(Vertex vertex, Map map, string town) : base(50, 50) { m_Vertex = vertex; m_Map = map; m_Town = town; Closable = true; Disposable = true; Dragable = true; Resizable = false; int size = m_ShopDefinitions.Count; bool on = false; AddPage(0); AddBackground(0, 0, 540, 35 + size * 30 / 2, 9200); AddAlphaRegion(15, 10, 510, 15 + size * 30 / 2); for (int i = 0; i < size; i += 2) { on = m_Vertex.Shops.Contains(i); AddButton(25, 25 + i * 30 / 2, on ? 2361 : 2360, on ? 2360 : 2361, i + 1, GumpButtonType.Reply, 0); AddHtmlLocalized(50, 20 + i * 30 / 2, 200, 20, m_ShopDefinitions[i], 0x7773, false, false); if (i + 1 < size) { on = m_Vertex.Shops.Contains(i + 1); AddButton(280, 25 + i * 30 / 2, on ? 2361 : 2360, on ? 2360 : 2361, i + 2, GumpButtonType.Reply, 0); AddHtmlLocalized(305, 20 + i * 30 / 2, 200, 20, m_ShopDefinitions[i + 1], 0x7773, false, false); } } m_Item = new Item(0x1183) { Visible = false }; m_Item.MoveToWorld(m_Vertex.Location, map); }
public static void VertexEdit_OnCommand(CommandEventArgs e) { Mobile m = e.Mobile; if (m.Region != null) { Vertex closest = ClosestVetrex(m.Region.Name, m.Location); if (closest != null) { m.SendGump(new GuideVertexEditGump(closest, m.Map, m.Region.Name)); } else { m.SendLocalizedMessage(1076113); // There are no shops nearby. Please try again when you get to a town or city. } } else { m.SendLocalizedMessage(1076113); // There are no shops nearby. Please try again when you get to a town or city. } }
public static Dictionary <int, Vertex> FindShops(string town, Point3D location) { if (town == null || !m_GraphDefinitions.ContainsKey(town)) { return(null); } List <Vertex> vertices = m_GraphDefinitions[town]; Dictionary <int, Vertex> shops = new Dictionary <int, Vertex>(); foreach (Vertex v in vertices) { foreach (int shop in v.Shops) { if (shops.ContainsKey(shop)) { Vertex d = shops[shop]; if (v.DistanceTo(location) < d.DistanceTo(location)) { shops[shop] = v; } } else { shops.Add(shop, v); } } } if (shops.Count > 0) { return(shops); } return(null); }
public static void Initialize() { CommandSystem.Register("GuideEdit", AccessLevel.GameMaster, VertexEdit_OnCommand); try { using (FileStream stream = File.OpenRead(Path.Combine("Data", "Guide", "Definitions.cfg"))) using (StreamReader reader = new StreamReader(stream)) { while (!reader.EndOfStream) { string line = reader.ReadLine(); if (!string.IsNullOrEmpty(line) && !line.StartsWith("#")) { string[] split = line.Split(m_Separators, StringSplitOptions.RemoveEmptyEntries); if (split != null && split.Length > 1) { m_ShopDefinitions.Add(int.Parse(split[1])); } } } } foreach (string file in Directory.GetFiles(Path.Combine("Data", "Guide"), "*.graph")) { using (FileStream stream = File.OpenRead(file)) using (StreamReader reader = new StreamReader(stream)) { List <Vertex> list = new List <Vertex>(); Vertex current = null; Vertex neighbour = null; while (!reader.EndOfStream) { string line = reader.ReadLine(); if (!string.IsNullOrEmpty(line)) { string[] split = line.Split(m_Separators, StringSplitOptions.RemoveEmptyEntries); int num; if (line.StartsWith("N:")) { if (current != null) { for (int i = 1; i < split.Length; i++) { num = int.Parse(split[i]); neighbour = FindVertex(list, num); if (neighbour == null) { neighbour = new Vertex(num); list.Add(neighbour); } current.Vertices.Add(neighbour); } } } else if (line.StartsWith("S:")) { if (current != null) { for (int i = 1; i < split.Length; i++) { num = int.Parse(split[i]); if (num >= 0 && num < m_ShopDefinitions.Count) { current.Shops.Add(num); } else { throw new Exception(string.Format("Invalid shop ID: {0}", num)); } } } } else if (line.StartsWith("V:")) { if (split.Length > 5) { num = int.Parse(split[1]); neighbour = FindVertex(list, num); if (neighbour != null) { current = neighbour; } else { current = new Vertex(num); list.Add(current); } Point3D location = new Point3D { X = int.Parse(split[2]), Y = int.Parse(split[3]), Z = int.Parse(split[4]) }; current.Location = location; current.Teleporter = bool.Parse(split[5]); } else { throw new Exception(string.Format("Incomplete vertex definition!")); } } } } m_GraphDefinitions.Add(Path.GetFileNameWithoutExtension(file), list); } } } catch (Exception e) { Diagnostics.ExceptionLogging.LogException(e); LogMessage(m_Delimiter); } }
public static List <Vertex> Dijkstra(string town, Vertex source, Vertex destination) { if (town == null || !m_GraphDefinitions.ContainsKey(town)) { return(null); } Heap <Vertex> heap = new Heap <Vertex>(); List <Vertex> path = new List <Vertex>(); heap.Push(source); foreach (Vertex v in m_GraphDefinitions[town]) { v.Distance = int.MaxValue; v.Previous = null; v.Visited = false; v.Removed = false; } source.Distance = 0; Vertex from; int dist = 0; while (heap.Count > 0) { from = heap.Pop(); from.Removed = true; if (from == destination) { while (from != source) { path.Add(from); from = from.Previous; } path.Add(source); return(path); } foreach (Vertex v in from.Vertices) { if (!v.Removed) { dist = from.Distance + (from.Teleporter ? 1 : from.DistanceTo(v)); if (dist < v.Distance) { v.Distance = dist; v.Previous = from; if (!v.Visited) { heap.Push(v); } else { heap.Fix(v); } } } } } return(null); }