/// <summary> /// Calculates the height map defined by current terrain configuration. Entire height interval is linearly interpolated into [0,255] interval. /// </summary> /// <param name="zoom">Zoom factor.</param> /// <param name="tile">Number of times to tile the height map.</param> /// <returns>Grayscale Image (Bitmap) representing the height map of current terrain configuration.</returns> public Image CreateImage(int zoom, int tile) { FastBitmap b; int w, h; int x, y; VerifyPositiveInt(zoom, "zoom"); VerifyPositiveInt(tile, "tile"); w = Size.Width; h = Size.Height; #region REPORT INIT if (PBW != null) { PBW.Report("Creating Image", w * tile * zoom * h * tile * zoom); if (PBW.CancellationPending) { return(null); } } #endregion // Create and populate the image (each array item corresponds to one image pixel). // GetColor performs the interpolation. b = new FastBitmap(w * tile * zoom, h * tile * zoom, PixelFormat.Format24bppRgb); b.Lock(); for (int nx = 0; nx < w; nx++) { for (int ny = 0; ny < h; ny++) { for (int tx = 0; tx < tile; tx++) { for (int ty = 0; ty < tile; ty++) { for (int zx = 0; zx < zoom; zx++) { for (int zy = 0; zy < zoom; zy++) { #region REPORT if (PBW != null) { PBW.Report(); if (PBW.CancellationPending) { return(null); } } #endregion x = zx + nx * zoom + tx * zoom * w; y = zy + ny * zoom + ty * zoom * h; b.SetPixel(x, y, GetColor(HeightMap[nx][ny], Min, Max)); } } } } } } b.Unlock(); return((Image)b.B);; }
private void Login() { try { PBW.Login(Config.Instance.Username, Config.Instance.Password); } catch (Exception ex) { var inex = ex.InnermostException(); if (inex is AuthenticationException && (inex.Message == "The remote certificate is invalid according to the validation procedure." || inex.Message.StartsWith("The remote certificate is invalid because of errors in the certificate chain:"))) { if (Config.Instance.IgnoreBadCertificates || MessageBox.Show("PBW's security certificate appears to be invalid or expired. Log in anyway?", "Invalid Certificate", MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.No) == MessageBoxResult.Yes) { try { PBW.OverrideBadCertificates(); PBW.Login(Config.Instance.Username, Config.Instance.Password, false); } catch (Exception ex2) { if (!pbwIsDown) { ShowBalloonTip("Login failed", "Unable to log in to PBW: " + ex2.Message, null, BalloonIcon.Error); } pbwIsDown = true; } } else { exiting = true; Close(); } } else { if (!pbwIsDown) { ShowBalloonTip("Login failed", "Unable to log in to PBW: " + ex.Message, null, BalloonIcon.Error); } pbwIsDown = true; } } }
/// <summary> /// Initializes the Height Map Array. /// </summary> /// <param name="width">Array width.</param> /// <param name="height">Array height.</param> /// <param name="defaultHeight">Height that will be applied to each item.</param> private void InitializeSize(int width, int height, double defaultHeight) { VerifyPositiveInt(width, "width"); VerifyPositiveInt(height, "height"); VerifyValidDouble(defaultHeight, "defaultHeight"); #region REPORT INIT if (PBW != null) { PBW.Report("Initializing terrain", width * height); if (PBW.CancellationPending) { return; } } #endregion _size = new Size(width, height); _min = defaultHeight; _max = defaultHeight; _heightMap = new double[width][]; for (int nx = 0; nx < width; nx++) { _heightMap[nx] = new double[height]; for (int ny = 0; ny < height; ny++) { #region REPORT if (PBW != null) { PBW.Report(); if (PBW.CancellationPending) { return; } } #endregion _heightMap[nx][ny] = defaultHeight; } } }
private void acceptButton_Click(object sender, EventArgs e) { DataTable addMembers = Members.Clone(); DataView dv = Members.DefaultView; dv.Sort = "[ФИО] ASC"; Members = dv.ToTable(); foreach (Int32 num in memberСheckedListBox.CheckedIndices) { addMembers.ImportRow(Members.Rows[num]); } PBW.AddInForm(addMembers, PBW.BlankDataGridView); PBW.CheckMembersInDB(PBW.BlankDataGridView); PBW.MarkMembers(); PBW.BlankDataGridView.AutoResizeColumns(); PBW.BlankDataGridView.AutoResizeRows(); PBW.BlankDataGridView.ClearSelection(); PBW.PC.Calculation(PBW); this.Close(); }
private void RefreshData() { // set cursor Cursor = Cursors.Wait; if (!PBW.isLoggedIn) { Login(); } if (PBW.isLoggedIn) { // remember selection var selGame = gridPlayerGames.SelectedItem as PlayerGame; var selCode = selGame == null ? null : selGame.Code; var selPlayerNumber = selGame == null ? null : (int?)selGame.PlayerNumber; IEnumerable <HostGame> hostGames = null; IEnumerable <PlayerGame> playerGames = null; try { // load host games { var hostGameViewSource = ((CollectionViewSource)(this.FindResource("hostGameViewSource"))); hostGames = PBW.GetHostGames().ToArray(); hostGameViewSource.Source = hostGames; } // load player games { var playerGameViewSource = ((CollectionViewSource)(this.FindResource("playerGameViewSource"))); var oldGames = (IEnumerable <PlayerGame>)playerGameViewSource.Source; if (oldGames == null) { oldGames = Enumerable.Empty <PlayerGame>(); } if (Config.Instance.HidePlayerZero) { playerGames = PBW.GetPlayerGames().Where(q => q.PlayerNumber > 0).ToArray(); } else { playerGames = PBW.GetPlayerGames().ToArray(); } playerGameViewSource.Source = playerGames; var newReady = new HashSet <PlayerGame>(); var waiting = playerGames.Where(g => g.Status == PlayerStatus.Waiting); var waitingPLR = waiting.Where(g => g.PlayerNumber > 0 && g.TurnNumber > 0); // don't count waiting for host PLR, that would get annoying if not all players are even ready yet var waitingEMP = waiting.Where(g => g.PlayerNumber > 0 && g.TurnNumber == 0); foreach (var ng in waiting) { var og = oldGames.SingleOrDefault(g => g.Code == ng.Code && g.PlayerNumber == ng.PlayerNumber); if (og == null || og.Status != PlayerStatus.Waiting) { newReady.Add(ng); } if (og != null && og.TurnNumber == ng.TurnNumber) { ng.HasDownloaded = og.HasDownloaded; } } RefreshDirectoryWatchers(); // newly ready games, show a popup if (waitingPLR.Intersect(newReady).Count() > 1) { ShowBalloonTip("New turns ready", waitingPLR.Intersect(newReady).Count() + " new games are ready to play.", waitingPLR.Intersect(newReady)); } else if (waitingPLR.Intersect(newReady).Count() == 1) { ShowBalloonTip("New turn ready", waitingPLR.Intersect(newReady).Single() + " is ready to play.", waitingPLR.Intersect(newReady).Single()); } else if (waitingEMP.Intersect(newReady).Count() > 1) { ShowBalloonTip("Awaiting empires", waitingEMP.Intersect(newReady).Count() + " games are awaiting empire setup files.", waitingEMP.Intersect(newReady)); } else if (waitingEMP.Intersect(newReady).Count() == 1) { ShowBalloonTip("Awaiting empire", waitingEMP.Intersect(newReady).Single() + " is awaiting an empire setup file.", waitingEMP.Intersect(newReady).Single()); } // all ready games, set a tooltip if (waitingPLR.Count() > 1) { taskbarIcon.ToolTipText = waitingPLR.Count() + " games are ready to play."; } else if (waitingPLR.Count() == 1) { taskbarIcon.ToolTipText = waitingPLR.Single() + " is ready to play."; } else if (waitingEMP.Count() > 1) { taskbarIcon.ToolTipText = waitingEMP.Count() + " games are awaiting empire setup files."; } else if (waitingEMP.Count() == 1) { taskbarIcon.ToolTipText = waitingEMP.Single() + " is awaiting an empire setup file."; } else { taskbarIcon.ToolTipText = "All caught up!"; } } pbwIsDown = false; } catch (Exception ex) { if (!pbwIsDown) { ShowBalloonTip("Unable to refresh", "Unable to refresh games lists: " + ex.Message, null, BalloonIcon.Error); } pbwIsDown = true; taskbarIcon.ToolTipText = "Unable to connect to PBW"; } try { // load engines var engineViewSource = ((CollectionViewSource)(this.FindResource("engineViewSource"))); engineViewSource.Source = Config.Instance.Engines; lstEngines.GetBindingExpression(ListView.ItemsSourceProperty).UpdateTarget(); ddlEngine.GetBindingExpression(ComboBox.ItemsSourceProperty).UpdateTarget(); // load mods var modViewSource = ((CollectionViewSource)(this.FindResource("modViewSource"))); modViewSource.Source = Config.Instance.Mods; lstMods.GetBindingExpression(ListView.ItemsSourceProperty).UpdateTarget(); } catch (Exception ex) { ShowBalloonTip("Unable to refresh", "Unable to refresh engines/mods lists: " + ex.Message, null, BalloonIcon.Error); } // load log lstLog.DataContext = PBW.Log.ReadAll(); // process turn if needed if (Config.Instance.EnableHosting && hostGames != null && HostGame.ProcessingGame == null && currentTurnProcess == null) { var gamesToProcess = hostGames.Where(g => g.Status == HostStatus.PlayersReady).ToList(); while (gamesToProcess.Any()) { var game = gamesToProcess.First(); PBW.Log.Write($"{game} is ready to be processed; enqueueing it."); if (CheckModAndEngine(game)) { ShowBalloonTip("Processing turn", "Processing turn for " + game + ".", game); try { game.DownloadTurns(); currentTurnProcess = new Process(); currentTurnProcess.StartInfo = game.ProcessTurnPrepare(); currentTurnProcess.EnableRaisingEvents = true; currentTurnProcess.Exited += process_Exited; btnReset.Foreground = Brushes.Red; currentTurnProcess.Start(); break; // wait till we finish this one } catch (Exception ex) { ShowBalloonTip("Turn processing failed", "Turn processing for " + game + " failed:\n" + ex.Message + "\n" + ex.Message.Split('\n').First(), game); game.PlaceHold(ex.Message); if (currentTurnProcess != null) { currentTurnProcess.Close(); currentTurnProcess = null; HostGame.ProcessingGame = null; currentTurnExitCode = null; HostGame.ProcessingGame = null; btnReset.ClearValue(Control.ForegroundProperty); } gamesToProcess.Remove(game); HostGame.ProcessingGame = null; } } else { PBW.Log.Write($"Can't process {game}: unconfigured mod {game.Mod} or engine {game.Engine}."); gamesToProcess.Remove(game); // can't process this game now } } } // auto-download if (Config.Instance.AutoDownload && playerGames != null) { var waitingPLR = playerGames.Where(g => g.Status == PlayerStatus.Waiting && !g.HasDownloaded && g.PlayerNumber > 0 && g.TurnNumber > 0); foreach (var ng in waitingPLR) { try { ng.DownloadTurn(); } catch (Exception ex) { ShowBalloonTip("Download failed", "Auto-download for " + ng + " failed:\n" + ex.Message + "\n" + ex.Message.Split('\n').First(), ng); } } } // remember selection var newGame = gridPlayerGames.Items.Cast <PlayerGame>().SingleOrDefault(g => g.Code == selCode && g.PlayerNumber == selPlayerNumber); gridPlayerGames.SelectedItem = newGame; } // start a timer so we can refresh in a few minutes refreshTimer.Start(); // reset cursor Cursor = Cursors.Arrow; }
private void RenderStarted(RenderData rd) { btnStartStop.Text = "Stop"; btnExport.Enabled = false; PBW.RunWorkerAsync(rd); }
private void StopRender() { PBW.CancelAsync(); }
private void AddUserWindow_FormClosed(object sender, FormClosedEventArgs e) { PBW.Enabled = true; PBW.Show(); }