public void getRouteTest() { Solarsystem A = new Solarsystem(); A.name = "A"; Solarsystem B = new Solarsystem(); B.name = "B"; Solarsystem C = new Solarsystem(); C.name = "C"; Solarsystem D = new Solarsystem(); D.name = "D"; List<Solarsystem> systems = new List<Solarsystem>(); systems.Add(A); systems.Add(B); systems.Add(C); systems.Add(D); Node AB = new Node(); AB.distance = 1; AB.pointa = A; AB.pointb = B; A.nodes.Add(AB); B.nodes.Add(AB); Node AC = new Node(); AC.distance = 10; AC.pointa = A; AC.pointb = C; A.nodes.Add(AC); C.nodes.Add(AC); Node BD = new Node(); BD.distance = 100; BD.pointa = B; BD.pointb = D; B.nodes.Add(BD); D.nodes.Add(BD); Node CD = new Node(); CD.distance = 5; CD.pointa = C; CD.pointb = D; C.nodes.Add(CD); D.nodes.Add(CD); Main main = null; NodeTrackingSystem target = new NodeTrackingSystem(main); Solarsystem start = A; Solarsystem end = D; bool dontUsePing = false; Route actual; actual = target.getRoute(start, end, dontUsePing); Assert.AreEqual(A, actual.start); Assert.AreEqual(D, actual.end); Assert.AreEqual(15, actual.distance); }
public SystemInfo(Solarsystem system, GameData data) { if (data == null) { MessageBox.Show("Nur möglich, wenn eine GameData Datei eigelesen wurde!"); Close(); } this.system = system; this.data = data; InitializeComponent(); }
public LinkedList<Solarsystem> getSystemsInRange(Solarsystem start, double range) { LinkedList<Solarsystem> list = new LinkedList<Solarsystem>(); if (start != null) { Parallel.ForEach(systemList, system => { if ((system != null) && (start != system)) { double distance = getDistance(start, system); if (distance <= range) { lock (list) { list.AddLast(system); } } } }); } return list; }
public Route getRoute(Solarsystem start, Solarsystem end, bool dontUsePing = false) { foreach (Route element in routelist) { if ((start == element.start) && (end == element.end)) { return element; } } if (dontUsePing) { Route startS = new Route(); startS.systems.Add(start); Route result = searchRoute(start, end, startS, 0); result.start = start; result.end = end; routelist.Add(result); Route route2 = new Route(); route2.start = end; route2.end = start; route2.nodelist = InverseList(result.nodelist); route2.systems = InverseList(result.systems); routelist.Add(route2); return result; } else { try { this.pingSolarsystem(start, start, end); Route[] results = pingResponse[start][end]; Route best = null; foreach (Route rt in results) { double length = 0; foreach (Node nd in rt.nodelist) { length += nd.distance; } rt.distance = length; if ((best == null) || (best.distance > rt.distance)) { best = rt; } } best.systems.Add(end); return best; } catch (TrackingCanceledException) { return null; } } }
private Route searchRoute(Solarsystem position, Solarsystem end, Route list, int depth) { Route result = null; // Console.WriteLine("Bin im System: " + position); if (depth < maxdepth) // Parallel.ForEach(position.nodes, node => foreach (Node node in position.nodes) { // Console.WriteLine("Checke Node: " + node); // bool stop = false; Solarsystem other; other = (node.pointa == position) ? node.pointb : node.pointa; Route VList = new Route(); VList.nodelist = CloneList(list.nodelist); VList.nodelist.Add(node); VList.systems = CloneList(list.systems); VList.systems.Add(other); if (list.systems.Contains(other)) { // Console.WriteLine("Bereits besucht"); // stop = true; continue; } // if (!stop) // { if (other == end) { double distance = 0; foreach (Node tmp in VList.nodelist) { distance += tmp.distance; } VList.distance = distance; // Console.WriteLine("Ergebnis gefunden"); // result = VList; return VList; } else { // Console.WriteLine("Suche Rekursiv"); Route search = searchRoute(other, end, VList, depth + 1); if (search.distance != -1) { // result = search; return search; } else { // Console.WriteLine("Ergebnis nicht gefunden"); } // } } } // }); if (result == null) { result = new Route(); result.distance = -1; } return result; }
public void pingSolarsystem(Solarsystem current, Solarsystem source, Solarsystem destination, Solarsystem[] visited = null, Node[] visitedNode = null, int maxResults = 3, int depth = 0) { if (visited == null || visitedNode == null) { visited = new Solarsystem[0]; visitedNode = new Node[0]; //pingResponse = new Dictionary<Solarsystem, Dictionary<Solarsystem, Route[]>>(); } if ((main != null) && (!main.run())) { return; } if (!pingResponse.ContainsKey(source)) { pingResponse.Add(source, new Dictionary<Solarsystem, Route[]>()); } if (pingResponse[source].ContainsKey(destination) && (pingResponse[source][destination].Length >= maxResults)) { return; } if (depth > maxdepth) { throw new TrackingCanceledException(); } if (current == destination) { try { Route route = new Route(); route.start = source; route.end = destination; route.systems = new List<Solarsystem>(); route.nodelist = new List<Node>(); foreach (Solarsystem system in visited) { route.systems.Add(system); } foreach (Node node in visitedNode) { route.nodelist.Add(node); } if (!pingResponse[source].ContainsKey(destination)) { pingResponse[source].Add(destination, new Route[0]); } Route[] routes = new Route[pingResponse[source][destination].Length + 1]; copyArray(pingResponse[source][destination], routes); routes[routes.Length - 1] = route; pingResponse[source][destination] = routes; } catch { // Das ist sehr seltsam, dass hier was fliegt ... } } else if (visited.Contains(current)) { // Das System wurde bereits besucht ... return; } else { //Trage das aktuelle System in die Liste, der besuchten Systeme ein Solarsystem[] visited2 = new Solarsystem[visited.Length + 1]; copyArray(visited, visited2); visited2[visited2.Length - 1] = current; List<Node> nodelist = current.nodes; bool error = false; Parallel.ForEach(nodelist, node => // foreach (Node node in nodelist) { try { Solarsystem other = (node.pointa == current) ? node.pointb : node.pointa; Node[] visitedNode2 = new Node[visitedNode.Length + 1]; copyArray(visitedNode, visitedNode2); visitedNode2[visitedNode2.Length - 1] = node; pingSolarsystem(other, source, destination, visited2, visitedNode2, maxResults, depth + 1); //} } catch (TrackingCanceledException) { error = true; } }); if (error) { throw new TrackingCanceledException(); } } if (depth == 0) { // if (!pingResponse.ContainsKey(source) || !(pingResponse[source].ContainsKey(destination)) || !(pingResponse[source][destination].Length > 0)) // { // throw new TrackingCanceledException(); // } // else // { return; // } } else { return; } }
public bool isConnection(Solarsystem start, Solarsystem end) { bool connection = false; Parallel.ForEach(start.nodes, node => { if ((node.pointa == end) || (node.pointb == end)) { connection = true; } }); return connection; }
public double getDistance(Solarsystem start, Solarsystem end) { double distance = Math.Sqrt(Math.Pow((start.x - end.x), 2) + Math.Pow((start.y - end.y), 2)); return distance; }
public int getConnectionCount(Solarsystem system) { return system.nodes.Count; }
private void generateMapData(int addCount = -1) { /* Erstellung der Map: * 1. Erstellen der Refferenzen der Sonnensystem * 2. Berrechnen der Abstände / Koordinaten der Systeme * 3. Erstellen der Verbindundungen zwischen den Systemen * 4. Überprüfen, ob jedes System mindestens eine Verbindung hat * 5. Überprüfen, ob es von jedem Punkt eine Verbindung zu jedem Punkt gibt */ if (loadedMap != null) { if (loadedMap.randomArea != null) { int systemcount = loadedMap.systemcount; int min_distance = loadedMap.min_distance; if (addCount != -1) { systemcount = addCount; } int min_X, min_Y, max_X, max_Y; if (loadedMap.randomArea.x1 > loadedMap.randomArea.x2) { min_X = loadedMap.randomArea.x2; max_X = loadedMap.randomArea.x1; } else { min_X = loadedMap.randomArea.x1; max_X = loadedMap.randomArea.x2; } if (loadedMap.randomArea.y1 > loadedMap.randomArea.y2) { min_Y = loadedMap.randomArea.y2; max_Y = loadedMap.randomArea.y1; } else { min_Y = loadedMap.randomArea.y1; max_Y = loadedMap.randomArea.y2; } int connectrange = 100; int maxconnections = 4; try { connectrange = int.Parse(main.config["Game/Map/ConnectRange"]); maxconnections = int.Parse(main.config["Game/Map/MaxConnections"]); } catch { } // Parallel.For(0, systemcount, i => for (int i = 0; i < systemcount; i++) { Random rand = new Random(); Solarsystem system = new Solarsystem(); int x = rand.Next(min_X, max_X); int y = rand.Next(min_Y, max_Y); system.x = x; system.y = y; system.name = Game.Game.Data.Names.SolarsystemNames[rand.Next(Game.Game.Data.Names.SolarsystemNames.Length - 1)]; int count = 0; bool ok = true; while (getSystemsInRange(system, min_distance).Count > 0) { count++; x = rand.Next(min_X, max_X); y = rand.Next(min_Y, max_Y); system.x = x; system.y = y; if (count > 100) { ok = false; break; } } if (ok) { LinkedList<Solarsystem> near = getSystemsInRange(system, connectrange); if (near.Count > 0) { int anzahl = rand.Next(1, near.Count); int j = 0; foreach (Solarsystem nearSystem in near) { try { j++; if (j > anzahl) { break; } if ((getConnectionCount(system) < maxconnections) && (getConnectionCount(nearSystem) < maxconnections) && !isConnection(system, nearSystem)) { Node node = new Node(); node.pointa = system; node.pointb = nearSystem; node.distance = getDistance(system, nearSystem); system.nodes.Add(node); nearSystem.nodes.Add(node); } } catch { } } } if (system != null) systemList.Add(system); } // }); } // Überprüfung der Systeme bool sysok = true; List<Solarsystem> delet = new List<Solarsystem>(); Parallel.ForEach(systemList, system => { if ((system != null) && sysok) { if (getSystemsInRange(system, 0).Count > 0) { Console.WriteLine("Invalides System endteckt!"); system.nodes.Clear(); foreach (Node node in system.nodes) { node.pointa.nodes.Remove(node); node.pointb.nodes.Remove(node); } try { delet.Add(system); } catch { } } else if (system.nodes.Count == 0) { LinkedList<Solarsystem> near = getSystemsInRange(system, connectrange * 2); if (near.Count > 0) { int anzahl = (new Random()).Next(1, near.Count); int j = 0; foreach (Solarsystem nearSystem in near) { j++; if (j > anzahl) { break; } if ((getConnectionCount(system) < 2) && (getConnectionCount(nearSystem) < maxconnections) && !isConnection(system, nearSystem)) { Node node = new Node(); node.pointa = system; node.pointb = nearSystem; node.distance = getDistance(system, nearSystem); system.nodes.Add(node); nearSystem.nodes.Add(node); } } } if (system.nodes.Count == 0) { main.log("Unnereichbares System gefunden! Bitte Passen Sie die Map-Daten oder die AnwendungsKonfiguration an"); sysok = false; } } } }); if (!sysok) { // reCreateMap(); return; } // Parallel.ForEach(delet, del => { systemList.Remove(del); }); // Console.WriteLine(delet.Count + " Systeme entfernt"); foreach (Solarsystem del in delet) { if (systemList.Contains(del)) { del.nodes.Clear(); foreach (Node node in del.nodes) { node.pointa.nodes.Remove(node); node.pointb.nodes.Remove(node); } systemList.Remove(del); } } systemList.Remove(null); // if (delet.Count > 0) // generateMapData(delet.Count); } //Erstelle Zufalls Planeten List<PlanetClass> planetTypes = game.getPlanetTypes(); if (planetTypes.Count > 0) { Random random = new Random(DateTime.Now.Millisecond); Parallel.ForEach(systemList, system => { for (int i = 0; i < random.Next(15); i++) { Planet planet = new Planet(Game.Game.Data.Names.PlanetNames[random.Next(Game.Game.Data.Names.PlanetNames.Length - 1)]); planet.type = planetTypes[random.Next(planetTypes.Count - 1)]; if (system.planets == null) { system.planets = new List<Planet>(); } system.planets.Add(planet); planet.Solarsystem = system; } }); } } else { throw new GameException("Keine Map-Datei geladen"); } }
private void panel1_MouseClick(object sender, MouseEventArgs e) { if (tool_systemAdd.Checked) { PictureBox picture = new PictureBox(); panel1.Controls.Add(picture); picture.Image = getImage("sun"); picture.Left = e.X; picture.Top = e.Y; picture.Width = picture.Image.Width; picture.Height = picture.Image.Height; picture.Visible = true; picture.MouseClick += new MouseEventHandler(picture_Click); Solarsystem system = new Solarsystem(); system.x = e.X; system.y = e.Y; solarsystemMap.Add(system); refferenzTable.Add(picture, system); refferenzTable2.Add(system, picture); } else if (tool_randomArea.Checked) { if (!randomAreaReady) { if (randomArea == null) { randomArea = new Area(); randomArea.x1 = e.X; randomArea.y1 = e.Y; } else { randomArea.x2 = e.X; randomArea.y2 = e.Y; randomAreaReady = true; } } else { randomArea = null; randomAreaReady = false; } } else if (tool_delRandomArea.Checked) { randomArea = null; randomAreaReady = false; } printConnections(); }