public bool Update(double xp, double zp, float zoom, GLControl gl) // Foreground UI thread, tells it if anything has changed.. { Debug.Assert(Application.MessageLoop); StarGrid grd = null; bool displayed = false; while (computed.TryTake(out grd)) // remove from the computed queue and mark done { grd.Display(gl); // swap to using this one.. Thread.MemoryBarrier(); // finish above before clearing working grd.Working = false; displayed = true; } Thread.MemoryBarrier(); // finish above curx = xp; curz = zp; ewh.Set(); // tick thread again to consider.. return(displayed); // return if we updated anything.. }
public bool IsDisplayed(double xp, double zp) { int gridid = GridId.Id(xp, zp); StarGrid grid = grids.Find(x => x.Id == gridid); if (grid != null) { return(grid.Displayed); } else { return(false); } }
public void Initialise() { for (int z = 0; z < GridId.gridzrange; z++) { for (int x = 0; x < GridId.gridxrange; x++) { int id = GridId.IdFromComponents(x, z); double xp = 0, zp = 0; bool ok = GridId.XZ(id, out xp, out zp); Debug.Assert(ok); StarGrid grd = new StarGrid(id, xp, zp, Color.Transparent, 1.0F); //A=0 means use default colour array if (xp == 0 && zp == 0) // sol grid, unpopulated stars please { grd.dBAsk = SystemClass.SystemAskType.UnPopulatedStars; } grids.Add(grd); } } visitedsystemsgrid = new StarGrid(-1, 0, 0, Color.Orange, 1.0F); // grid ID -1 means it won't be filled by the Update task grids.Add(visitedsystemsgrid); int solid = GridId.Id(0, 0); populatedgrid = new StarGrid(solid, 0, 0, Color.Transparent, 1.0F); // Duplicate grid id but asking for populated stars populatedgrid.dBAsk = SystemClass.SystemAskType.PopulatedStars; grids.Add(populatedgrid); // add last, so displayed last, so overwrites anything else long total = SystemClass.GetTotalSystems(); total = Math.Min(total, 10000000); // scaling limit at 10mil long offset = (total - 1000000) / 100000; // scale down slowly.. experimental! midpercentage -= (int)(offset / 2); farpercentage -= (int)(offset / 3); //midpercentage = 10; // agressive debugging options //farpercentage = 1; Console.WriteLine("Grids " + grids.Count + "Database Stars " + total + " mid " + midpercentage + " far " + farpercentage); }
void ComputeThread() { //Console.WriteLine("Start COMPUTE"); while (true) { ewh.WaitOne(); while (true) { if (computeExit) { return; } double mindist = double.MaxValue; double maxdist = 0; StarGrid selmin = null; StarGrid selmax = null; foreach (StarGrid gcheck in grids) { if (gcheck.Id >= 0 && !gcheck.Working) // if not a special grid { double dist = gcheck.DistanceFrom(curx, curz); if (Math.Abs(dist - gcheck.CalculatedDistance) > MinRecalcDistance) // if its too small a change, ignore.. histerisis { int percentage = GetPercentage(dist); if (percentage > gcheck.Percentage) // if increase, it has priority.. { if (dist < mindist) // if best.. pick { mindist = dist; selmin = gcheck; //Console.WriteLine("Select {0} incr perc {1} to {2} dist {3,8:0.0}", gcheck.Id, gcheck.Percentage, percentage, dist); } } else if (selmin == null && percentage < gcheck.Percentage) // if not selected a min one, pick the further one to decrease { if (dist > maxdist) { maxdist = dist; selmax = gcheck; //Console.WriteLine("Select {0} decr perc {1} to {2} dist {3,8:0.0}", gcheck.Id, gcheck.Percentage, percentage, dist); } } } } } if (selmin == null) { selmin = selmax; } if (selmin != null) { selmin.Working = true; // stops another go by this thread, only cleared by UI when it has displayed int prevpercent = selmin.Percentage; double prevdist = selmin.CalculatedDistance; selmin.CalculatedDistance = selmin.DistanceFrom(curx, curz); selmin.Percentage = GetPercentage(selmin.CalculatedDistance); selmin.FillFromDB(); Thread.MemoryBarrier(); // finish above before trying to trigger another go.. Debug.Assert(Math.Abs(prevdist - selmin.CalculatedDistance) > MinRecalcDistance); Debug.Assert(!computed.Contains(selmin)); //Console.WriteLine("Grid repaint {0} {1}%->{2}% dist {3,8:0.0}->{4,8:0.0} s{5}", selmin.Id, prevpercent, selmin.Percentage, prevdist, selmin.CalculatedDistance, selmin.CountJustMade); computed.Add(selmin); } else { break; // nothing to do, wait for kick } } } }