public bool Update(float xp, float 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 void Initialise() { for (int z = 0; z < GridId.GridZRange; z++) { for (int x = 0; x < GridId.GridXRange; x++) { int id = GridId.IdFromComponents(x, z); float 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 = SystemsDB.SystemAskType.UnpopulatedStars; } grids.Add(grd); } } systemlistgrid = new StarGrid(-1, 0, 0, Color.Orange, 1.0F); // grid ID -1 means it won't be filled by the Update task grids.Add(systemlistgrid); 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 = SystemsDB.SystemAskType.PopulatedStars; grids.Add(populatedgrid); // add last so shown last Console.WriteLine("Grids " + grids.Count + " mid " + midpercentage + " far " + farpercentage); }
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); float 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 = SystemClassDB.SystemAskType.UnPopulatedStars; } grids.Add(grd); } } systemlistgrid = new StarGrid(-1, 0, 0, Color.Orange, 1.0F); // grid ID -1 means it won't be filled by the Update task grids.Add(systemlistgrid); 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 = SystemClassDB.SystemAskType.PopulatedStars; grids.Add(populatedgrid); // add last so shown last long total = SystemClassDB.GetTotalSystemsFast(); 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); }
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); float 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 grids.Add(grd); } } systemlistgrid = new StarGrid(-1, 0, 0, Color.Orange, 1.0F); // grid ID -1 means it won't be filled by the Update task grids.Add(systemlistgrid); Trace.WriteLine("Grids " + grids.Count + " mid " + midpercentage + " far " + farpercentage); }
void ComputeThread() { //Console.WriteLine("Start COMPUTE"); var canceltoken = canceltokensource.Token; var computed = new BlockingCollection <StarGrid>(); this.computed = computed; while (WaitHandle.WaitAny(new WaitHandle[] { canceltoken.WaitHandle, ewh }) == 1) { while (!canceltoken.IsCancellationRequested) { float mindist = float.MaxValue; float maxdist = 0; StarGrid selmin = null; StarGrid selmax = null; for (int i = grids.Count - 1; i >= 0; i--) // go backwards thru the list, so the ones painted last gets considered first { StarGrid gcheck = grids[i]; //if (gcheck.Id == 2200) //System.Diagnostics.Debug.WriteLine("Cell 2200 "); if (gcheck.Id >= 0 && !gcheck.Working) // if not a special grid { float 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) { //System.Diagnostics.Debug.WriteLine("Repaint Grid -- " + selmin.Id + " dist from " + curx + "," + curz + " is " + selmin.DistanceFrom(curz,curz)); selmin.Working = true; // stops another go by this thread, only cleared by UI when it has displayed int prevpercent = selmin.Percentage; float 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)); //Tools.LogToFile(String.Format("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 } } } Trace.WriteLine($"{Environment.TickCount} compute exit"); }
void ComputeThread() { //Console.WriteLine("Start COMPUTE"); while (true) { ewh.WaitOne(); while (true) { if (computeExit) { return; } float mindist = float.MaxValue; float maxdist = 0; StarGrid selmin = null; StarGrid selmax = null; for (int i = grids.Count - 1; i >= 0; i--) // go backwards thru the list, so the ones painted last gets considered first { StarGrid gcheck = grids[i]; if (gcheck.Id >= 0 && !gcheck.Working) // if not a special grid { float 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; float 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)); //Tools.LogToFile(String.Format("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 } } } }