public static GuideVertex ClosestVetrex(string town, Point3D location) { if (town == null || !m_GraphDefinitions.ContainsKey(town)) { return(null); } List <GuideVertex> vertices = m_GraphDefinitions[town]; GuideVertex closest = null; double min = Int32.MaxValue; double distance; foreach (GuideVertex 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 static void VertexEdit_OnCommand(CommandEventArgs e) { Mobile m = e.Mobile; if (m.Region != null) { GuideVertex 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 GuideVertexEditGump(GuideVertex 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); m_Item.Visible = false; m_Item.MoveToWorld(m_Vertex.Location, map); }
public static Dictionary <int, GuideVertex> FindShops(string town, Point3D location) { if (town == null || !m_GraphDefinitions.ContainsKey(town)) { return(null); } List <GuideVertex> vertices = m_GraphDefinitions[town]; Dictionary <int, GuideVertex> shops = new Dictionary <int, GuideVertex>(); foreach (GuideVertex v in vertices) { foreach (int shop in v.Shops) { if (shops.ContainsKey(shop)) { GuideVertex 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(Int32.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 <GuideVertex> list = new List <GuideVertex>(); GuideVertex current = null; GuideVertex 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 = Int32.Parse(split[i]); neighbour = FindVertex(list, num); if (neighbour == null) { neighbour = new GuideVertex(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 = Int32.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 = Int32.Parse(split[1]); neighbour = FindVertex(list, num); if (neighbour != null) { current = neighbour; } else { current = new GuideVertex(num); list.Add(current); } Point3D location = new Point3D(); location.X = Int32.Parse(split[2]); location.Y = Int32.Parse(split[3]); location.Z = Int32.Parse(split[4]); current.Location = location; current.Teleporter = Boolean.Parse(split[5]); } else { throw new Exception(String.Format("Incomplete vertex definition!")); } } } } m_GraphDefinitions.Add(Path.GetFileNameWithoutExtension(file), list); } } } catch (Exception e) { Server.Diagnostics.ExceptionLogging.LogException(e); LogMessage(m_Delimiter); } }
public static List <GuideVertex> Dijkstra(string town, GuideVertex source, GuideVertex destination) { if (town == null || !m_GraphDefinitions.ContainsKey(town)) { return(null); } Heap <GuideVertex> heap = new Heap <GuideVertex>(); List <GuideVertex> path = new List <GuideVertex>(); heap.Push(source); foreach (GuideVertex v in m_GraphDefinitions[town]) { v.Distance = Int32.MaxValue; v.Previous = null; v.Visited = false; v.Removed = false; } source.Distance = 0; GuideVertex 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 (GuideVertex 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); }