public static void GetSystemListBySqDistancesFrom(BaseUtils.SortedListDoubleDuplicate <ISystem> distlist, // MUST use duplicate double list to protect against EDSM having two at the same point double x, double y, double z, int maxitems, double mindist, // 0 = no min dist, always spherical double maxdist, bool spherical, // enforces sphere on maxdist, else its a cube for maxdist SQLiteConnectionSystem cn, Action <ISystem> LookedUp = null ) { // for comparision, using the grid screener is slower than the xy index. keep code for record // grid screener.. "s.sectorid IN (Select id FROM Sectors sx where sx.gridid IN (" + strinlist + ")) " + //var gridids = GridId.Ids(x - maxdist, x + maxdist, z - maxdist, z + maxdist); // find applicable grid ids across this range.. //var strinlist = string.Join(",", (from x1 in gridids select x1.ToStringInvariant())); // here we convert using invariant for paranoia sake. int mindistint = mindist > 0 ? SystemClass.DoubleToInt(mindist) * SystemClass.DoubleToInt(mindist) : 0; // needs a xz index for speed using (DbCommand cmd = cn.CreateSelect("Systems s", MakeSystemQueryEDDB, where : "s.x >= @xv - @maxdist " + "AND s.x <= @xv + @maxdist " + "AND s.z >= @zv - @maxdist " + "AND s.z <= @zv + @maxdist " + "AND s.y >= @yv - @maxdist " + "AND s.y <= @yv + @maxdist " + (mindist > 0 ? ("AND (s.x-@xv)*(s.x-@xv)+(s.y-@yv)*(s.y-@yv)+(s.z-@zv)*(s.z-@zv)>=" + (mindistint).ToStringInvariant()) : ""), orderby: "(s.x-@xv)*(s.x-@xv)+(s.y-@yv)*(s.y-@yv)+(s.z-@zv)*(s.z-@zv)", // just use squares to order joinlist: MakeSystemQueryEDDBJoinList, limit: "@max" )) { cmd.AddParameterWithValue("@xv", SystemClass.DoubleToInt(x)); cmd.AddParameterWithValue("@yv", SystemClass.DoubleToInt(y)); cmd.AddParameterWithValue("@zv", SystemClass.DoubleToInt(z)); cmd.AddParameterWithValue("@max", maxitems + 1); // 1 more, because if we are on a System, that will be returned cmd.AddParameterWithValue("@maxdist", SystemClass.DoubleToInt(maxdist)); // System.Diagnostics.Debug.WriteLine(cn.ExplainQueryPlanString(cmd)); using (DbDataReader reader = cmd.ExecuteReader()) { while (reader.Read())// && distlist.Count < maxitems) // already sorted, and already limited to max items { SystemClass s = MakeSystem(reader); LookedUp?.Invoke(s); // callback to say looked up double distsq = s.DistanceSq(x, y, z); if ((!spherical || distsq <= maxdist * maxdist)) { distlist.Add(distsq, s); // which Rob has seen crashing the program! Bad EDSM! } } } } }