public EDDBStation GetClosestStation(EdsmCoords coords) { var closestSystemId = systems.Select( system => new { system.id, distance = Math.Sqrt(Math.Pow(coords.X - system.x, 2) + Math.Pow(coords.Y - system.y, 2) + Math.Pow(coords.Z - system.z, 2)) }).OrderBy(x => x.distance).First().id; EDDBStation station = stations.Where(st => st.system_id == closestSystemId && st.distance_to_star != null) .OrderBy(st => st.distance_to_star) .FirstOrDefault(); return station; }
/* Attempts to calculate a distance in lightyears between two given systems. * This is done using EDSM coordinates. * TODO: Once done with testing, transition source to be a <List>TravelLog. * TODO: Wait - this is not smart. CalculateEDSMDistance should just give you * the distances between those two systems, not do any stunts with the source. * Move this functionality out of there, and leave CED requiring a valid source * coord - maybe even require source to be type EDSMCoords. */ public double CalculateEDSMDistance(string source, string target) { EdsmCoords sourcecoords = new EdsmCoords(); EdsmCoords targetcoords = new EdsmCoords(); IEnumerable<EdsmSystem> candidates; if (source == target) return 0; /* Well, it COULD happen? People have been known to do stupid things. */ foreach (TravelLog mysource in myTravelLog.Reverse()) { if (mysource.system.Coords == null) { AppendStatus("System in travellog has no coords:" + mysource.system.Name); } else { AppendStatus("Found coord'ed system " + mysource.system.Name + ", using as source."); sourcecoords = mysource.system.Coords; } } if (sourcecoords == null || source == "Fuelum") { AppendStatus("Search for travellog coordinated system failed, using Fuelum coords"); // Add a static Fuelum system reference so we don't have to query EDSM for it. sourcecoords = fuelumCoords; } candidates = QueryEDSMSystem(target); if (candidates == null || candidates.Count() < 1) { AppendStatus("EDSM does not know that system. Widening search..."); candidates = GetCandidateSystems(target); } if (candidates.FirstOrDefault().Coords == null) { AppendStatus("Known system, but no coords. Widening search..."); candidates = GetCandidateSystems(target); } if (candidates == null || candidates.Count() < 1) { //Still couldn't find something, abort. AppendStatus("Couldn't find a candidate system, aborting..."); return -1; } else { AppendStatus("I got " + candidates.Count() + " systems with coordinates. Picking the first."); targetcoords = candidates.FirstOrDefault().Coords; } if (sourcecoords != null && targetcoords != null) { AppendStatus("We have two sets of coords that we can use to find a distance."); double deltaX = sourcecoords.X - targetcoords.X; double deltaY = sourcecoords.Y - targetcoords.Y; double deltaZ = sourcecoords.Z - targetcoords.Z; double distance = (double) Math.Sqrt(deltaX*deltaX + deltaY*deltaY + deltaZ*deltaZ); AppendStatus("Distance should be " + distance.ToString()); return distance; } else { AppendStatus("Failed to find target coords. Giving up."); return -1; } }
public async Task<ClientDistance> GetDistanceToClient(string target) { int logdepth = 0; EdsmCoords targetcoords =new EdsmCoords(); ClientDistance cd = new ClientDistance(); EdsmCoords sourcecoords = FuelumCoords; EdsmSystem sourceSystem = new EdsmSystem(); sourceSystem.Name = "Fuelum"; sourceSystem.Coords = FuelumCoords; cd.SourceCertainty = "Fuelum"; if (_myTravelLog!=null) { foreach (TravelLog mysource in _myTravelLog.Reverse()) { if (mysource.System.Coords == null) { logdepth++; } else { Logger.Debug("Found TL system to use: " + mysource.System.Name); sourcecoords = mysource.System.Coords; if (logdepth == 0) { cd.SourceCertainty = "Exact"; sourceSystem = mysource.System; break; } else { cd.SourceCertainty = logdepth.ToString(); sourceSystem = mysource.System; break; } } } } IEnumerable<EdsmSystem> candidates = await QueryEdsmSystem(target); cd.TargetCertainty = "Exact"; if (!candidates.Any()) { Logger.Debug("EDSM does not know system '" + target + "'. Widening search..."); candidates = await GetCandidateSystems(target); cd.TargetCertainty = "Nearby"; } EdsmSystem firstOrDefault = candidates.FirstOrDefault(); if (firstOrDefault != null && firstOrDefault.Coords == null) { Logger.Debug("Known system '" + target + "', but no coords. Widening search..."); candidates = await GetCandidateSystems(target); cd.TargetCertainty = "Region"; } if (candidates == null || !candidates.Any()) { //Still couldn't find something, abort. AppendStatus("Couldn't find a candidate system, aborting..."); return new ClientDistance(); } Logger.Debug("We have two sets of coords that we can use to find a distance."); EdsmSystem edsmSystem = candidates.FirstOrDefault(); if (edsmSystem != null) targetcoords = edsmSystem.Coords; if(sourceSystem == null || sourceSystem.Name==null) { Logger.Debug("Err... Source system (or its name) is null, that shouldn't happen at this point. Bailing!"); return new ClientDistance(); } Logger.Debug("Finding from coords: " + sourcecoords.X + " " + sourcecoords.Y + " " + sourcecoords.Z + " ("+sourceSystem.Name+") to " + targetcoords.X + " " + targetcoords.Y + " " + targetcoords.Z+" ("+edsmSystem.Name+")"); double deltaX = sourcecoords.X - targetcoords.X; double deltaY = sourcecoords.Y - targetcoords.Y; double deltaZ = sourcecoords.Z - targetcoords.Z; double distance = Math.Sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ); Logger.Debug("Distance should be " + distance); cd.Distance = distance; return cd; }
/* Attempts to calculate a distance in lightyears between two given systems. * This is done using EDSM coordinates. * * Transitioned to be exact source system, searches for nearest for target. * For the client distance calculation, call GetDistanceToClient instead. */ public async Task<double> CalculateEdsmDistance(string source, string target) { try { EdsmCoords sourcecoords = new EdsmCoords(); if (source == target) { return 0; /* Well, it COULD happen? People have been known to do stupid things. */ } if (source == null) { /* Dafuq? */ Logger.Fatal("Null value passed as source to CalculateEDSMDistance! Falling back to Fuelum as source."); source = "Fuelum"; } if (source.Length < 3) { AppendStatus("Source system name '" + source + "' too short, searching from Fuelum."); source = "Fuelum"; } if (target.Length < 3) { AppendStatus("Target system name '" + target + "' is too short. Can't perform distance search."); return -1; } IEnumerable<EdsmSystem> candidates = await QueryEdsmSystem(source); if (candidates == null || !candidates.Any()) { Logger.Debug("Unknown source system."); return -1; } candidates = await QueryEdsmSystem(target); var edsmSystems = candidates as EdsmSystem[] ?? candidates.ToArray(); if (!edsmSystems.Any()) { Logger.Debug("EDSM does not know system '" + target + "'. Widening search..."); candidates = await GetCandidateSystems(target); } EdsmSystem firstOrDefault = edsmSystems.FirstOrDefault(); if (firstOrDefault != null && firstOrDefault.Coords == null) { Logger.Debug("Known system '" + target + "', but no coords. Widening search..."); candidates = await GetCandidateSystems(target); } if (candidates == null || !edsmSystems.Any()) { //Still couldn't find something, abort. AppendStatus("Couldn't find a candidate system, aborting..."); return -1; } Logger.Debug("I got " + edsmSystems.Count() + " systems with coordinates. Sorting by lexical match and picking first."); var sorted = edsmSystems.OrderBy(s => LexicalOrder(target, s.Name)); EdsmSystem edsmSystem = sorted.FirstOrDefault(); EdsmCoords targetcoords = edsmSystem?.Coords; if (targetcoords != null) { Logger.Debug("We have two sets of coords that we can use to find a distance."); double deltaX = sourcecoords.X - targetcoords.X; double deltaY = sourcecoords.Y - targetcoords.Y; double deltaZ = sourcecoords.Z - targetcoords.Z; double distance = Math.Sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ); Logger.Debug("Distance should be " + distance); return distance; } AppendStatus("EDSM failed to find coords for system '" + target + "'."); return -1; } catch(Exception ex) { Logger.Fatal("Exception in CalculateEdsmDistance: " + ex.Message); _tc.TrackException(ex); return -1; } }
public EddbStation GetClosestStation(EdsmCoords coords) { try { Logger.Debug("Calculating closest station to X:" + coords.X + " Y:" + coords.Y + " Z:" + coords.Z); var closestSystemId = Systems.Where(system => system.population > 0).Select( system => new { system.id,system.population, distance = Math.Sqrt(Math.Pow(coords.X - system.x, 2) + Math.Pow(coords.Y - system.y, 2) + Math.Pow(coords.Z - system.z, 2)) }).OrderBy(x => x.distance).First().id; Logger.Debug("Got system " + closestSystemId); EddbStation station = Stations.Where(st => st.system_id == closestSystemId && st.distance_to_star != null) .OrderBy(st => st.distance_to_star) .FirstOrDefault(); if (station != null) { Logger.Debug("Got station " + station.name); return station; } return new EddbStation(); } catch (Exception ex) { Logger.Fatal("Exception in GetClosestStation: " + ex.Message); return new EddbStation(); } }