// not called.. private JArray Path2JSON(AStar AS) { JArray ja = new JArray(); Node oldnode = null; foreach (Node n in AS.PathByNodes) { JObject jo = new JObject(); double dist = 0; if (oldnode != null) { dist = SystemData.Distance(n.System, oldnode.System); } oldnode = n; jo["name"] = n.System.name; //jo.Add("name", n.Name); jo["x"] = n.X; jo["y"] = n.Y; jo["z"] = n.Z; jo["dist"] = Math.Round(dist * 100) / 100; ja.Add(jo); } // serialize JSON directly to a file using (StreamWriter file = File.CreateText(@"route.json")) { JsonSerializer serializer = new JsonSerializer(); serializer.Serialize(file, ja); } return(ja); }
private void AddHistoryRow(bool insert, SystemPosition item, SystemPosition item2) { SystemClass sys1 = null, sys2; double dist; sys1 = SystemData.GetSystem(item.Name); if (sys1 == null) { sys1 = new SystemClass(item.Name); if (SQLiteDBClass.globalSystemNotes.ContainsKey(sys1.SearchName)) { sys1.Note = SQLiteDBClass.globalSystemNotes[sys1.SearchName].Note; } } if (item2 != null) { sys2 = SystemData.GetSystem(item2.Name); if (sys2 == null) { sys2 = new SystemClass(item2.Name); } } else { sys2 = null; } item.curSystem = sys1; item.prevSystem = sys2; if (!insert) { if (item.vs == null) { SystemPosition known = visitedSystems.First(x => x.Name == item.Name); if (known != null) { item.vs = known.vs; } } } string diststr = ""; dist = 0; if (sys2 != null) { if (sys1.HasCoordinate && sys2.HasCoordinate) { dist = SystemData.Distance(sys1, sys2); } else { dist = DistanceClass.Distance(sys1, sys2); } if (dist > 0) { diststr = dist.ToString("0.00"); } } item.strDistance = diststr; //richTextBox_History.AppendText(item.time + " " + item.Name + Environment.NewLine); object[] rowobj = { item.time, item.Name, diststr, item.curSystem.Note, "█" }; int rownr; if (insert) { dataGridViewTravel.Rows.Insert(0, rowobj); rownr = 0; } else { dataGridViewTravel.Rows.Add(rowobj); rownr = dataGridViewTravel.Rows.Count - 1; } var cell = dataGridViewTravel.Rows[rownr].Cells[1]; cell.Tag = item; dataGridViewTravel.Rows[rownr].DefaultCellStyle.ForeColor = (sys1.HasCoordinate) ? _discoveryForm.theme.VisitedSystemColor : _discoveryForm.theme.NonVisitedSystemColor; cell = dataGridViewTravel.Rows[rownr].Cells[4]; cell.Style.ForeColor = (item.vs == null) ? Color.FromArgb(defaultMapColour) : Color.FromArgb(item.vs.MapColour); }
private void RunTrilaterationWorker() { var systemsEntries = new Dictionary <SystemClass, Trilateration.Entry>(); for (int i = 0, count = dataGridViewDistances.Rows.Count - 1; i < count; i++) { var systemCell = dataGridViewDistances[0, i]; var distanceCell = dataGridViewDistances[1, i]; if (systemCell.Tag == null || distanceCell.Value == null) { continue; } var system = (SystemClass)systemCell.Tag; if (system != null && system.HasCoordinate) { var culture = new CultureInfo("en-US"); var distance = double.Parse(distanceCell.Value.ToString().Replace(",", "."), culture); var entry = new Trilateration.Entry(system.x, system.y, system.z, distance); systemsEntries.Add(system, entry); } } if (systemsEntries.Count < 3) { return; } Invoke((MethodInvoker) delegate { LogText("Starting trilateration..." + Environment.NewLine); labelStatus.Text = "Calculating…"; labelStatus.BackColor = Color.Gold; }); var trilateration = new Trilateration { Logger = Console.WriteLine }; foreach (var item in systemsEntries) { trilateration.AddEntry(item.Value); } //var trilaterationResultCS = trilateration.RunCSharp(); var trilaterationAlgorithm = radioButtonAlgorithmJs.Checked ? Trilateration.Algorithm.RedWizzard_Emulated : Trilateration.Algorithm.RedWizzard_Native; var stopwatch = new Stopwatch(); stopwatch.Start(); var trilaterationResult = trilateration.Run(trilaterationAlgorithm); stopwatch.Stop(); var spentTimeString = (stopwatch.ElapsedMilliseconds / 1000.0).ToString("0.0000") + "ms"; lastTrilatelationResult = trilaterationResult; lastTrilatelationEntries = systemsEntries; if (trilaterationResult.State == Trilateration.ResultState.Exact) { Invoke((MethodInvoker) delegate { SystemClass s1, s2, s3; s1 = SystemData.GetSystem("Sol"); s2 = SystemData.GetSystem("Sagittarius A*"); s3 = new SystemClass(); s3.x = trilaterationResult.Coordinate.X; s3.y = trilaterationResult.Coordinate.Y; s3.z = trilaterationResult.Coordinate.Z; LogText("Trilateration successful (" + spentTimeString + "), exact coordinates found." + Environment.NewLine); LogText("x=" + trilaterationResult.Coordinate.X + ", y=" + trilaterationResult.Coordinate.Y + ", z=" + trilaterationResult.Coordinate.Z + " Sol: " + SystemData.Distance(s1, s3).ToString("0.0") + " Sag A* " + SystemData.Distance(s2, s3).ToString("0.0") + Environment.NewLine); labelStatus.Text = "Success, coordinates found!"; labelStatus.BackColor = Color.LawnGreen; }); } else if (trilaterationResult.State == Trilateration.ResultState.NotExact || trilaterationResult.State == Trilateration.ResultState.MultipleSolutions) { Invoke((MethodInvoker) delegate { LogText("Trilateration not successful (" + spentTimeString + "), only approximate coordinates found." + Environment.NewLine); //LogText("x=" + trilaterationResult.Coordinate.X + ", y=" + trilaterationResult.Coordinate.Y + ", z=" + trilaterationResult.Coordinate.Z + Environment.NewLine); LogText("Enter more distances." + Environment.NewLine); labelStatus.Text = "Enter More Distances"; labelStatus.BackColor = Color.Orange; }); } else if (trilaterationResult.State == Trilateration.ResultState.NeedMoreDistances) { Invoke((MethodInvoker) delegate { LogText("Trilateration not successful (" + spentTimeString + "), coordinates not found." + Environment.NewLine); LogText("Enter more distances." + Environment.NewLine); labelStatus.Text = "Enter More Distances"; labelStatus.BackColor = Color.Red; ClearCalculatedDataGridViewDistancesRows(); }); } // update trilaterated coordinates if (trilaterationResult.Coordinate != null) { Invoke((MethodInvoker) delegate { textBoxCoordinateX.Text = trilaterationResult.Coordinate.X.ToString(); textBoxCoordinateY.Text = trilaterationResult.Coordinate.Y.ToString(); textBoxCoordinateZ.Text = trilaterationResult.Coordinate.Z.ToString(); if (TargetSystem != null) { TargetSystem.x = trilaterationResult.Coordinate.X; TargetSystem.y = trilaterationResult.Coordinate.Y; TargetSystem.z = trilaterationResult.Coordinate.Z; } toolStripButtonMap.Enabled = (TargetSystem != null); }); var suggestedSystems = GetListOfSuggestedSystems(trilaterationResult.Coordinate.X, trilaterationResult.Coordinate.Y, trilaterationResult.Coordinate.Z, 16); Invoke((MethodInvoker)(() => PopulateSuggestedSystems(suggestedSystems))); } else { Invoke((MethodInvoker) delegate { textBoxCoordinateX.Text = "?"; textBoxCoordinateY.Text = "?"; textBoxCoordinateZ.Text = "?"; }); } //var hasInvalidDistances = false; // update dataGrid with calculated distances and status var entriesDistances = trilaterationResult.EntriesDistances; for (int i = 0, count = dataGridViewDistances.Rows.Count - 1; i < count; i++) { var systemCell = dataGridViewDistances[0, i]; var calculatedDistanceCell = dataGridViewDistances[2, i]; var statusCell = dataGridViewDistances[3, i]; var system = (SystemClass)systemCell.Tag; if (system == null) { continue; } if (system.HasCoordinate) { calculatedDistanceCell.Value = null; statusCell.Value = null; } if (entriesDistances == null || systemCell.Value == null || systemCell.Tag == null) { continue; } if (!systemsEntries.ContainsKey(system)) // calculated without this system, so skip the row { continue; } var systemEntry = systemsEntries[system]; var calculatedDistance = entriesDistances[systemEntry]; calculatedDistanceCell.Value = calculatedDistance.ToString(); if (systemEntry.Distance == calculatedDistance) { calculatedDistanceCell.Style.ForeColor = Color.Green; statusCell.Value = "OK"; statusCell.Style.ForeColor = Color.Green; } else { //hasInvalidDistances = true; calculatedDistanceCell.Style.ForeColor = Color.Salmon; statusCell.Value = "Wrong distance?"; statusCell.Style.ForeColor = Color.Salmon; } } }
private void AddHistoryRow(bool insert, SystemPosition item, SystemPosition item2) { SystemClass sys1 = null, sys2; double dist; sys1 = SystemData.GetSystem(item.Name); if (sys1 == null) { sys1 = new SystemClass(item.Name); if (SQLiteDBClass.globalSystemNotes.ContainsKey(sys1.SearchName)) { sys1.Note = SQLiteDBClass.globalSystemNotes[sys1.SearchName].Note; } } if (item2 != null) { sys2 = SystemData.GetSystem(item2.Name); if (sys2 == null) { sys2 = new SystemClass(item2.Name); } } else { sys2 = null; } item.curSystem = sys1; item.prevSystem = sys2; string diststr = ""; dist = 0; if (sys2 != null) { if (sys1.HasCoordinate && sys2.HasCoordinate) { dist = SystemData.Distance(sys1, sys2); } else { dist = DistanceClass.Distance(sys1, sys2); } if (dist > 0) { diststr = dist.ToString("0.00"); } } item.strDistance = diststr; //richTextBox_History.AppendText(item.time + " " + item.Name + Environment.NewLine); object[] rowobj = { item.time, item.Name, diststr, item.curSystem.Note }; int rownr; if (insert) { dataGridView1.Rows.Insert(0, rowobj); rownr = 0; } else { dataGridView1.Rows.Add(rowobj); rownr = dataGridView1.Rows.Count - 1; } var cell = dataGridView1.Rows[rownr].Cells[1]; cell.Tag = item; if (!sys1.HasCoordinate) // Mark all systems without coordinates { cell.Style.ForeColor = Color.Blue; } }
private void Route(string s1, string s2, float maxrange, int mode) { Stopwatch sw = new Stopwatch(); if (lastJumprange != maxrange) { G = new Graph(); PrepareNodes(G, out nodes, maxrange, mode); //Console.WriteLine("Prepare nodes time: {0}", sw.Elapsed); lastJumprange = maxrange; } AStar AS = new AStar(G); Node start, stop; start = nodes.FirstOrDefault(x => x.System.SearchName == s1.ToLower()); stop = nodes.FirstOrDefault(x => x.System.SearchName == s2.ToLower()); bool res; if (start == null) { AppendText("Start system: " + s1 + " unknown"); return; } if (stop == null) { AppendText("Destination system: " + s2 + " unknown"); return; } sw = new Stopwatch(); sw.Start(); res = AS.SearchPath(start, stop); sw.Stop(); AppendText("Searching route from " + s1 + " to " + s2 + Environment.NewLine); AppendText("Find route Time: " + sw.Elapsed.TotalSeconds.ToString("0.000s") + Environment.NewLine); AppendText("Total distance: " + SystemData.Distance(s1, s2).ToString("0.00") + Environment.NewLine); AppendText("Max jumprange:" + maxrange + Environment.NewLine); double totdist = 0; int jumps = 0; if (res) { foreach (Arc A in AS.PathByArcs) { double dist = SystemData.Distance(A.StartNode.System.name, A.EndNode.System.name); AppendText(A.EndNode.System.name + " \tDist: " + dist.ToString("0.00") + " ly" + Environment.NewLine); totdist += dist; jumps++; Console.WriteLine(A.ToString()); } // JArray ja = Path2JSON(AS); } else { Console.WriteLine("No result !"); } AppendText("Total distance: " + totdist.ToString("0.00") + Environment.NewLine); AppendText("Jumps: " + jumps + Environment.NewLine); }