internal static void InvokeGeneratorClose(Player player, Generator079 generator, ref bool allow) { if (GeneratorCloseEvent == null) { return; } var ev = new GeneratorEvent { Allow = allow, Generator = generator, Player = player }; GeneratorCloseEvent.Invoke(ev); allow = ev.Allow; }
/// <summary> /// Run the generator. /// </summary> private void Run() { cancellationTokenSource = new CancellationTokenSource(); var ct = cancellationTokenSource.Token; task = Task.Run(() => { try { dumpFolder = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds().ToString(); dumpCount = 0; var layoutGenerator = settings.LayoutGenerator; if (layoutGenerator == null) { var defaultGenerator = new DungeonGenerator <int>(settings.MapDescription); defaultGenerator.InjectRandomGenerator(new Random(settings.RandomGeneratorSeed)); layoutGenerator = defaultGenerator; } // Set cancellation token if (layoutGenerator is ICancellable cancellable) { cancellable.SetCancellationToken(ct); } infoStopwatch.Start(); // Register handler that shows generated layouts OnValid layoutGenerator.OnValid += layout => { if (!showFinalLayouts.Checked) { return; } lastEvent = GeneratorEvent.OnValid; layoutToDraw = layout; mainPictureBox.BeginInvoke((Action)(() => mainPictureBox.Refresh())); SleepWithFastCancellation((int)showFinalLayoutsTime.Value, ct); }; // Register handler that shows generated layouts OnPartialValid layoutGenerator.OnPartialValid += layout => { if (!showPartialValidLayouts.Checked) { return; } lastEvent = GeneratorEvent.OnPartialValid; layoutToDraw = layout; mainPictureBox.BeginInvoke((Action)(() => mainPictureBox.Refresh())); SleepWithFastCancellation((int)showAcceptedLayoutsTime.Value, ct); }; // Register handler that shows generated layouts OnPerturbed layoutGenerator.OnPerturbed += layout => { if (!showPerturbedLayouts.Checked) { return; } lastEvent = GeneratorEvent.OnPerturbed; layoutToDraw = layout; mainPictureBox.BeginInvoke((Action)(() => mainPictureBox.Refresh())); SleepWithFastCancellation((int)showPerturbedLayoutsTime.Value, ct); }; // Register handler that counts iteration count layoutGenerator.OnPerturbed += layout => { lastEvent = GeneratorEvent.OnPerturbed; iterationsCount++; if (infoStopwatch.ElapsedMilliseconds >= 200) { BeginInvoke((Action)(UpdateInfoPanel)); infoStopwatch.Restart(); } }; // Register handler that resets iteration count layoutGenerator.OnValid += layout => { lastEvent = GeneratorEvent.OnValid; iterationsCount = 0; layoutsCount++; BeginInvoke((Action)(UpdateInfoPanel)); infoStopwatch.Restart(); }; generatedLayouts = new List <MapLayout <int> >() { }; for (int i = 0; i < settings.NumberOfLayouts; i++) { generatedLayouts.Add(layoutGenerator.GenerateLayout()); } isRunning = false; BeginInvoke((Action)(UpdateInfoPanel)); BeginInvoke((Action)(OnFinished)); } catch (Exception e) { ShowExceptionAndClose(e); } }, ct); }