From d35aab1f77b5fc63fa6d9ad9f69f2b9ffe6bb88d Mon Sep 17 00:00:00 2001 From: Kurt Date: Sun, 5 Apr 2020 09:04:31 -0700 Subject: [PATCH 1/6] Clamp read of encrypted uints guards against corrupt values? Closes #55 --- NHSE.WinForms/Editor.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/NHSE.WinForms/Editor.cs b/NHSE.WinForms/Editor.cs index 56cf484..2bb4093 100644 --- a/NHSE.WinForms/Editor.cs +++ b/NHSE.WinForms/Editor.cs @@ -183,9 +183,9 @@ private void LoadPlayer(int index) var pers = player.Personal; TB_Name.Text = pers.PlayerName; TB_TownName.Text = pers.TownName; - NUD_BankBells.Value = pers.Bank.Value; - NUD_NookMiles.Value = pers.NookMiles.Value; - NUD_Wallet.Value = pers.Wallet.Value; + NUD_BankBells.Value = Math.Min(int.MaxValue, pers.Bank.Value); + NUD_NookMiles.Value = Math.Min(int.MaxValue, pers.NookMiles.Value); + NUD_Wallet.Value = Math.Min(int.MaxValue, pers.Wallet.Value); var photo = pers.GetPhotoData(); var bmp = new Bitmap(new MemoryStream(photo)); From b603e5628cf6a747dcfb709b2febc0b415194894 Mon Sep 17 00:00:00 2001 From: Kurt Date: Sun, 5 Apr 2020 12:03:34 -0700 Subject: [PATCH 2/6] Rearrange map related buttons --- NHSE.WinForms/Editor.Designer.cs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/NHSE.WinForms/Editor.Designer.cs b/NHSE.WinForms/Editor.Designer.cs index 32fb304..b963d5d 100644 --- a/NHSE.WinForms/Editor.Designer.cs +++ b/NHSE.WinForms/Editor.Designer.cs @@ -40,6 +40,7 @@ private void InitializeComponent() this.Menu_RAMEdit = new System.Windows.Forms.ToolStripMenuItem(); this.TC_Editors = new System.Windows.Forms.TabControl(); this.Tab_Players = new System.Windows.Forms.TabPage(); + this.B_EditPlayerFlags = new System.Windows.Forms.Button(); this.B_EditActivities = new System.Windows.Forms.Button(); this.B_EditPlayerReceivedItems = new System.Windows.Forms.Button(); this.B_EditPlayerStorage = new System.Windows.Forms.Button(); @@ -90,7 +91,6 @@ private void InitializeComponent() this.B_EditTurnipExchange = new System.Windows.Forms.Button(); this.B_EditBuildings = new System.Windows.Forms.Button(); this.B_RecycleBin = new System.Windows.Forms.Button(); - this.B_EditPlayerFlags = new System.Windows.Forms.Button(); this.Menu_Editor.SuspendLayout(); this.TC_Editors.SuspendLayout(); this.Tab_Players.SuspendLayout(); @@ -232,6 +232,16 @@ private void InitializeComponent() this.Tab_Players.Text = "Players"; this.Tab_Players.UseVisualStyleBackColor = true; // + // B_EditPlayerFlags + // + this.B_EditPlayerFlags.Location = new System.Drawing.Point(202, 122); + this.B_EditPlayerFlags.Name = "B_EditPlayerFlags"; + this.B_EditPlayerFlags.Size = new System.Drawing.Size(92, 40); + this.B_EditPlayerFlags.TabIndex = 17; + this.B_EditPlayerFlags.Text = "Edit Flags"; + this.B_EditPlayerFlags.UseVisualStyleBackColor = true; + this.B_EditPlayerFlags.Click += new System.EventHandler(this.B_EditPlayerFlags_Click); + // // B_EditActivities // this.B_EditActivities.Location = new System.Drawing.Point(301, 122); @@ -749,7 +759,7 @@ private void InitializeComponent() // // B_EditTurnipExchange // - this.B_EditTurnipExchange.Location = new System.Drawing.Point(202, 168); + this.B_EditTurnipExchange.Location = new System.Drawing.Point(6, 122); this.B_EditTurnipExchange.Name = "B_EditTurnipExchange"; this.B_EditTurnipExchange.Size = new System.Drawing.Size(92, 40); this.B_EditTurnipExchange.TabIndex = 15; @@ -759,7 +769,7 @@ private void InitializeComponent() // // B_EditBuildings // - this.B_EditBuildings.Location = new System.Drawing.Point(104, 168); + this.B_EditBuildings.Location = new System.Drawing.Point(300, 76); this.B_EditBuildings.Name = "B_EditBuildings"; this.B_EditBuildings.Size = new System.Drawing.Size(92, 40); this.B_EditBuildings.TabIndex = 14; @@ -777,16 +787,6 @@ private void InitializeComponent() this.B_RecycleBin.UseVisualStyleBackColor = true; this.B_RecycleBin.Click += new System.EventHandler(this.B_RecycleBin_Click); // - // B_EditPlayerFlags - // - this.B_EditPlayerFlags.Location = new System.Drawing.Point(202, 122); - this.B_EditPlayerFlags.Name = "B_EditPlayerFlags"; - this.B_EditPlayerFlags.Size = new System.Drawing.Size(92, 40); - this.B_EditPlayerFlags.TabIndex = 17; - this.B_EditPlayerFlags.Text = "Edit Flags"; - this.B_EditPlayerFlags.UseVisualStyleBackColor = true; - this.B_EditPlayerFlags.Click += new System.EventHandler(this.B_EditPlayerFlags_Click); - // // Editor // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); From fc3276ca9964b2c24d68af88f7c622c8ba2e8389 Mon Sep 17 00:00:00 2001 From: Kurt Date: Sun, 5 Apr 2020 12:04:28 -0700 Subject: [PATCH 3/6] Add plaza coordinates to building editor How exactly to plot the coordinates??? --- NHSE.Core/Save/Files/MainSave.cs | 12 ++ .../Subforms/BuildingEditor.Designer.cs | 120 +++++++++++++----- NHSE.WinForms/Subforms/BuildingEditor.cs | 8 ++ 3 files changed, 109 insertions(+), 31 deletions(-) diff --git a/NHSE.Core/Save/Files/MainSave.cs b/NHSE.Core/Save/Files/MainSave.cs index 73914ee..110790d 100644 --- a/NHSE.Core/Save/Files/MainSave.cs +++ b/NHSE.Core/Save/Files/MainSave.cs @@ -64,5 +64,17 @@ public void SetAcreBytes(byte[] data) public TerrainTile[] GetTerrain() => TerrainTile.GetArray(Data.Slice(Offsets.Terrain, TerrainManager.TileCount * TerrainTile.SIZE)); public void SetTerrain(IReadOnlyList array) => TerrainTile.SetArray(array).CopyTo(Data, Offsets.Terrain); + + public uint PlazaX + { + get => BitConverter.ToUInt32(Data, Offsets.Acres + AcreSizeAll + 4); + set => BitConverter.GetBytes(value).CopyTo(Data, Offsets.Acres + AcreSizeAll + 4); + } + + public uint PlazaY + { + get => BitConverter.ToUInt32(Data, Offsets.Acres + AcreSizeAll + 8); + set => BitConverter.GetBytes(value).CopyTo(Data, Offsets.Acres + AcreSizeAll + 8); + } } } \ No newline at end of file diff --git a/NHSE.WinForms/Subforms/BuildingEditor.Designer.cs b/NHSE.WinForms/Subforms/BuildingEditor.Designer.cs index f1aee50..b5b7122 100644 --- a/NHSE.WinForms/Subforms/BuildingEditor.Designer.cs +++ b/NHSE.WinForms/Subforms/BuildingEditor.Designer.cs @@ -48,10 +48,14 @@ private void InitializeComponent() this.L_Building10 = new System.Windows.Forms.Label(); this.GB_Building = new System.Windows.Forms.GroupBox(); this.GB_Info = new System.Windows.Forms.GroupBox(); - this.CB_StructureType = new System.Windows.Forms.ComboBox(); - this.L_StructureType = new System.Windows.Forms.Label(); this.L_StructureValues = new System.Windows.Forms.Label(); this.CB_StructureValues = new System.Windows.Forms.ComboBox(); + this.L_StructureType = new System.Windows.Forms.Label(); + this.CB_StructureType = new System.Windows.Forms.ComboBox(); + this.L_PlazaX = new System.Windows.Forms.Label(); + this.NUD_PlazaX = new System.Windows.Forms.NumericUpDown(); + this.L_PlazaY = new System.Windows.Forms.Label(); + this.NUD_PlazaY = new System.Windows.Forms.NumericUpDown(); ((System.ComponentModel.ISupportInitialize)(this.PB_Map)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.NUD_BuildingType)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.NUD_X)).BeginInit(); @@ -62,14 +66,16 @@ private void InitializeComponent() ((System.ComponentModel.ISupportInitialize)(this.NUD_10)).BeginInit(); this.GB_Building.SuspendLayout(); this.GB_Info.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.NUD_PlazaX)).BeginInit(); + ((System.ComponentModel.ISupportInitialize)(this.NUD_PlazaY)).BeginInit(); this.SuspendLayout(); // // B_Save // this.B_Save.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.B_Save.Location = new System.Drawing.Point(597, 526); + this.B_Save.Location = new System.Drawing.Point(609, 501); this.B_Save.Name = "B_Save"; - this.B_Save.Size = new System.Drawing.Size(75, 23); + this.B_Save.Size = new System.Drawing.Size(65, 23); this.B_Save.TabIndex = 1; this.B_Save.Text = "Save"; this.B_Save.UseVisualStyleBackColor = true; @@ -78,9 +84,9 @@ private void InitializeComponent() // B_Cancel // this.B_Cancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.B_Cancel.Location = new System.Drawing.Point(516, 526); + this.B_Cancel.Location = new System.Drawing.Point(609, 527); this.B_Cancel.Name = "B_Cancel"; - this.B_Cancel.Size = new System.Drawing.Size(75, 23); + this.B_Cancel.Size = new System.Drawing.Size(65, 23); this.B_Cancel.TabIndex = 2; this.B_Cancel.Text = "Cancel"; this.B_Cancel.UseVisualStyleBackColor = true; @@ -290,37 +296,15 @@ private void InitializeComponent() this.GB_Info.Controls.Add(this.CB_StructureType); this.GB_Info.Location = new System.Drawing.Point(424, 404); this.GB_Info.Name = "GB_Info"; - this.GB_Info.Size = new System.Drawing.Size(250, 103); + this.GB_Info.Size = new System.Drawing.Size(250, 94); this.GB_Info.TabIndex = 22; this.GB_Info.TabStop = false; this.GB_Info.Text = "Info"; // - // CB_StructureType - // - this.CB_StructureType.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) - | System.Windows.Forms.AnchorStyles.Right))); - this.CB_StructureType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; - this.CB_StructureType.FormattingEnabled = true; - this.CB_StructureType.Location = new System.Drawing.Point(20, 33); - this.CB_StructureType.Name = "CB_StructureType"; - this.CB_StructureType.Size = new System.Drawing.Size(221, 21); - this.CB_StructureType.TabIndex = 0; - this.CB_StructureType.SelectedIndexChanged += new System.EventHandler(this.CB_StructureType_SelectedIndexChanged); - // - // L_StructureType - // - this.L_StructureType.AutoSize = true; - this.L_StructureType.Location = new System.Drawing.Point(17, 17); - this.L_StructureType.Name = "L_StructureType"; - this.L_StructureType.Size = new System.Drawing.Size(80, 13); - this.L_StructureType.TabIndex = 20; - this.L_StructureType.Text = "Structure Type:"; - this.L_StructureType.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; - // // L_StructureValues // this.L_StructureValues.AutoSize = true; - this.L_StructureValues.Location = new System.Drawing.Point(17, 58); + this.L_StructureValues.Location = new System.Drawing.Point(17, 51); this.L_StructureValues.Name = "L_StructureValues"; this.L_StructureValues.Size = new System.Drawing.Size(42, 13); this.L_StructureValues.TabIndex = 22; @@ -334,16 +318,84 @@ private void InitializeComponent() this.CB_StructureValues.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.CB_StructureValues.DropDownWidth = 322; this.CB_StructureValues.FormattingEnabled = true; - this.CB_StructureValues.Location = new System.Drawing.Point(20, 74); + this.CB_StructureValues.Location = new System.Drawing.Point(20, 64); this.CB_StructureValues.Name = "CB_StructureValues"; this.CB_StructureValues.Size = new System.Drawing.Size(221, 21); this.CB_StructureValues.TabIndex = 21; // + // L_StructureType + // + this.L_StructureType.AutoSize = true; + this.L_StructureType.Location = new System.Drawing.Point(17, 12); + this.L_StructureType.Name = "L_StructureType"; + this.L_StructureType.Size = new System.Drawing.Size(80, 13); + this.L_StructureType.TabIndex = 20; + this.L_StructureType.Text = "Structure Type:"; + this.L_StructureType.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; + // + // CB_StructureType + // + this.CB_StructureType.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.CB_StructureType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.CB_StructureType.FormattingEnabled = true; + this.CB_StructureType.Location = new System.Drawing.Point(20, 26); + this.CB_StructureType.Name = "CB_StructureType"; + this.CB_StructureType.Size = new System.Drawing.Size(221, 21); + this.CB_StructureType.TabIndex = 0; + this.CB_StructureType.SelectedIndexChanged += new System.EventHandler(this.CB_StructureType_SelectedIndexChanged); + // + // L_PlazaX + // + this.L_PlazaX.Location = new System.Drawing.Point(434, 503); + this.L_PlazaX.Name = "L_PlazaX"; + this.L_PlazaX.Size = new System.Drawing.Size(70, 20); + this.L_PlazaX.TabIndex = 104; + this.L_PlazaX.Text = "Plaza X:"; + this.L_PlazaX.TextAlign = System.Drawing.ContentAlignment.MiddleRight; + // + // NUD_PlazaX + // + this.NUD_PlazaX.Location = new System.Drawing.Point(506, 504); + this.NUD_PlazaX.Maximum = new decimal(new int[] { + 1024, + 0, + 0, + 0}); + this.NUD_PlazaX.Name = "NUD_PlazaX"; + this.NUD_PlazaX.Size = new System.Drawing.Size(45, 20); + this.NUD_PlazaX.TabIndex = 103; + // + // L_PlazaY + // + this.L_PlazaY.Location = new System.Drawing.Point(434, 526); + this.L_PlazaY.Name = "L_PlazaY"; + this.L_PlazaY.Size = new System.Drawing.Size(70, 20); + this.L_PlazaY.TabIndex = 102; + this.L_PlazaY.Text = "Plaza Y:"; + this.L_PlazaY.TextAlign = System.Drawing.ContentAlignment.MiddleRight; + // + // NUD_PlazaY + // + this.NUD_PlazaY.Location = new System.Drawing.Point(506, 527); + this.NUD_PlazaY.Maximum = new decimal(new int[] { + 1024, + 0, + 0, + 0}); + this.NUD_PlazaY.Name = "NUD_PlazaY"; + this.NUD_PlazaY.Size = new System.Drawing.Size(45, 20); + this.NUD_PlazaY.TabIndex = 101; + // // BuildingEditor // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(684, 561); + this.Controls.Add(this.L_PlazaX); + this.Controls.Add(this.NUD_PlazaX); + this.Controls.Add(this.L_PlazaY); + this.Controls.Add(this.NUD_PlazaY); this.Controls.Add(this.GB_Info); this.Controls.Add(this.GB_Building); this.Controls.Add(this.PB_Map); @@ -367,6 +419,8 @@ private void InitializeComponent() this.GB_Building.ResumeLayout(false); this.GB_Info.ResumeLayout(false); this.GB_Info.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)(this.NUD_PlazaX)).EndInit(); + ((System.ComponentModel.ISupportInitialize)(this.NUD_PlazaY)).EndInit(); this.ResumeLayout(false); } @@ -396,5 +450,9 @@ private void InitializeComponent() private System.Windows.Forms.ComboBox CB_StructureValues; private System.Windows.Forms.Label L_StructureType; private System.Windows.Forms.ComboBox CB_StructureType; + private System.Windows.Forms.Label L_PlazaX; + private System.Windows.Forms.NumericUpDown NUD_PlazaX; + private System.Windows.Forms.Label L_PlazaY; + private System.Windows.Forms.NumericUpDown NUD_PlazaY; } } \ No newline at end of file diff --git a/NHSE.WinForms/Subforms/BuildingEditor.cs b/NHSE.WinForms/Subforms/BuildingEditor.cs index 6a745a7..febae4e 100644 --- a/NHSE.WinForms/Subforms/BuildingEditor.cs +++ b/NHSE.WinForms/Subforms/BuildingEditor.cs @@ -9,6 +9,7 @@ namespace NHSE.WinForms public partial class BuildingEditor : Form { private readonly IReadOnlyList Buildings; + private readonly MainSave SAV; private readonly TerrainManager Terrain; private static readonly IReadOnlyDictionary HelpDictionary = StructureUtil.GetStructureHelpList(); @@ -16,6 +17,7 @@ public BuildingEditor(IReadOnlyList buildings, MainSave sav) { InitializeComponent(); Buildings = buildings; + SAV = sav; DialogResult = DialogResult.Cancel; foreach (var obj in buildings) @@ -27,12 +29,18 @@ public BuildingEditor(IReadOnlyList buildings, MainSave sav) foreach (var entry in HelpDictionary) CB_StructureType.Items.Add(entry.Key); CB_StructureType.SelectedIndex = 0; + + NUD_PlazaX.Value = sav.PlazaX; + NUD_PlazaY.Value = sav.PlazaY; } private void B_Cancel_Click(object sender, EventArgs e) => Close(); private void B_Save_Click(object sender, EventArgs e) { + SAV.PlazaX = (uint)NUD_PlazaX.Value; + SAV.PlazaY = (uint)NUD_PlazaY.Value; + DialogResult = DialogResult.OK; Close(); } From 6f228a72f77b65a155e50131855aedbf90e791b8 Mon Sep 17 00:00:00 2001 From: Kurt Date: Sun, 5 Apr 2020 12:04:44 -0700 Subject: [PATCH 4/6] Show coordinates on mouse move --- .../Subforms/TerrainEditor.Designer.cs | 17 ++++++++-- NHSE.WinForms/Subforms/TerrainEditor.cs | 34 ++++++++++++++----- 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/NHSE.WinForms/Subforms/TerrainEditor.Designer.cs b/NHSE.WinForms/Subforms/TerrainEditor.Designer.cs index d5deb00..d22ea4f 100644 --- a/NHSE.WinForms/Subforms/TerrainEditor.Designer.cs +++ b/NHSE.WinForms/Subforms/TerrainEditor.Designer.cs @@ -53,6 +53,7 @@ private void InitializeComponent() this.CM_Picture = new System.Windows.Forms.ContextMenuStrip(this.components); this.Menu_SavePNG = new System.Windows.Forms.ToolStripMenuItem(); this.CHK_SnapToAcre = new System.Windows.Forms.CheckBox(); + this.L_Coordinates = new System.Windows.Forms.Label(); this.CM_Click.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.PB_Map)).BeginInit(); this.CM_Picture.SuspendLayout(); @@ -250,7 +251,7 @@ private void InitializeComponent() // this.PB_Map.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.PB_Map.ContextMenuStrip = this.CM_Picture; - this.PB_Map.Location = new System.Drawing.Point(821, 348); + this.PB_Map.Location = new System.Drawing.Point(821, 343); this.PB_Map.Name = "PB_Map"; this.PB_Map.Size = new System.Drawing.Size(226, 194); this.PB_Map.TabIndex = 23; @@ -278,18 +279,29 @@ private void InitializeComponent() this.CHK_SnapToAcre.AutoSize = true; this.CHK_SnapToAcre.Checked = true; this.CHK_SnapToAcre.CheckState = System.Windows.Forms.CheckState.Checked; - this.CHK_SnapToAcre.Location = new System.Drawing.Point(820, 325); + this.CHK_SnapToAcre.Location = new System.Drawing.Point(820, 320); this.CHK_SnapToAcre.Name = "CHK_SnapToAcre"; this.CHK_SnapToAcre.Size = new System.Drawing.Size(167, 17); this.CHK_SnapToAcre.TabIndex = 24; this.CHK_SnapToAcre.Text = "Snap to nearest Acre on Click"; this.CHK_SnapToAcre.UseVisualStyleBackColor = true; // + // L_Coordinates + // + this.L_Coordinates.Font = new System.Drawing.Font("Courier New", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.L_Coordinates.Location = new System.Drawing.Point(876, 540); + this.L_Coordinates.Name = "L_Coordinates"; + this.L_Coordinates.Size = new System.Drawing.Size(173, 15); + this.L_Coordinates.TabIndex = 25; + this.L_Coordinates.Text = "(000,000) = (0x00,0x00)"; + this.L_Coordinates.TextAlign = System.Drawing.ContentAlignment.TopRight; + // // TerrainEditor // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(1061, 822); + this.Controls.Add(this.L_Coordinates); this.Controls.Add(this.CHK_SnapToAcre); this.Controls.Add(this.PB_Map); this.Controls.Add(this.B_Down); @@ -349,5 +361,6 @@ private void InitializeComponent() private System.Windows.Forms.ContextMenuStrip CM_Picture; private System.Windows.Forms.ToolStripMenuItem Menu_SavePNG; private System.Windows.Forms.CheckBox CHK_SnapToAcre; + private System.Windows.Forms.Label L_Coordinates; } } \ No newline at end of file diff --git a/NHSE.WinForms/Subforms/TerrainEditor.cs b/NHSE.WinForms/Subforms/TerrainEditor.cs index c072a4e..3e8d1c0 100644 --- a/NHSE.WinForms/Subforms/TerrainEditor.cs +++ b/NHSE.WinForms/Subforms/TerrainEditor.cs @@ -335,12 +335,10 @@ private void Menu_SavePNG_Click(object sender, EventArgs e) private void ClickMapAt(MouseEventArgs e, bool skipLagCheck) { - const int maxX = ((TerrainManager.AcreWidth - 1) * GridWidth); - const int maxY = ((TerrainManager.AcreHeight - 1) * GridHeight); - - int center = CHK_SnapToAcre.Checked ? 0 : GridWidth / 2; - var x = Math.Max(0, Math.Min((e.X / MapScale) - center, maxX)); - var y = Math.Max(0, Math.Min((e.Y / MapScale) - center, maxY)); + int mX = e.X; + int mY = e.Y; + bool centerReticle = CHK_SnapToAcre.Checked; + GetCoordinates(centerReticle, mX, mY, out var x, out var y); var acre = TerrainManager.GetAcre(x, y); bool sameAcre = AcreIndex == acre; @@ -371,11 +369,29 @@ private void ClickMapAt(MouseEventArgs e, bool skipLagCheck) CB_Acre.SelectedIndex = acre; } + private static void GetCoordinates(bool centerReticle, int mX, int mY, out int x, out int y) + { + int center = centerReticle ? 0 : GridWidth / 2; + const int maxX = ((TerrainManager.AcreWidth - 1) * GridWidth); + const int maxY = ((TerrainManager.AcreHeight - 1) * GridHeight); + + x = Math.Max(0, Math.Min((mX / MapScale) - center, maxX)); + y = Math.Max(0, Math.Min((mY / MapScale) - center, maxY)); + } + private void PB_Map_MouseMove(object sender, MouseEventArgs e) { - if (e.Button != MouseButtons.Left) - return; - ClickMapAt(e, false); + if (e.Button == MouseButtons.Left) + { + ClickMapAt(e, false); + } + else + { + int mX = e.X; + int mY = e.Y; + GetCoordinates(false, mX, mY, out var x, out var y); + L_Coordinates.Text = $"({x:000},{y:000}) = (0x{x:X2},0x{y:X2})"; + } } } } From 3482eebe5aacda133ae201bcb153d36a9c15ec2a Mon Sep 17 00:00:00 2001 From: Kurt Date: Sun, 5 Apr 2020 16:00:17 -0700 Subject: [PATCH 5/6] Show plaza on generated map --- NHSE.Sprites/TerrainSprite.cs | 39 ++++++++++++++----- .../Subforms/BuildingEditor.Designer.cs | 2 + NHSE.WinForms/Subforms/BuildingEditor.cs | 24 ++++++++---- 3 files changed, 49 insertions(+), 16 deletions(-) diff --git a/NHSE.Sprites/TerrainSprite.cs b/NHSE.Sprites/TerrainSprite.cs index 624091e..a5613b1 100644 --- a/NHSE.Sprites/TerrainSprite.cs +++ b/NHSE.Sprites/TerrainSprite.cs @@ -88,26 +88,40 @@ private static Bitmap DrawReticle(Bitmap map, int x, int y, int scale) return map; } - public static Bitmap GetMapWithBuildings(TerrainManager mgr, IReadOnlyList buildings, Font f, int scale = 4, int index = -1) + public static Bitmap GetMapWithBuildings(TerrainManager mgr, IReadOnlyList buildings, ushort plazaX, ushort plazaY, Font f, int scale = 4, int index = -1) { - // Although there is terrain in the Top Row and Left Column, no buildings can be placed there. - // Adjust the building coordinates down-right by an acre. - const int buildingShift = TerrainManager.GridWidth; var map = CreateMap(mgr, scale); using var gfx = Graphics.FromImage(map); + gfx.DrawPlaza(plazaX, plazaY, scale); + gfx.DrawBuildings(buildings, f, scale, index); + return map; + } + + private static void DrawPlaza(this Graphics gfx, ushort px, ushort py, int scale) + { + var plaza = Brushes.RosyBrown; + GetBuildingCoordinate(px, py, scale, out var x, out var y); + + var width = scale * 2 * 6; + var height = scale * 2 * 5; + + gfx.FillRectangle(plaza, x, y, width, height); + } + + private static void DrawBuildings(this Graphics gfx, IReadOnlyList buildings, Font f, int scale, int index = -1) + { var selected = Brushes.Red; var others = Brushes.Yellow; var text = Brushes.White; - var stringFormat = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center }; + var stringFormat = new StringFormat {Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center}; for (int i = 0; i < buildings.Count; i++) { var b = buildings[i]; if (b.BuildingType == 0) continue; - var x = (int)(((b.X / 2f) - buildingShift) * scale); - var y = (int)(((b.Y / 2f) - buildingShift) * scale); + GetBuildingCoordinate(b.X, b.Y, scale, out var x, out var y); var pen = index == i ? selected : others; gfx.FillRectangle(pen, x - scale, y - scale, scale * 2, scale * 2); @@ -115,8 +129,15 @@ public static Bitmap GetMapWithBuildings(TerrainManager mgr, IReadOnlyList buildings, MainSave sav) { InitializeComponent(); - Buildings = buildings; SAV = sav; - DialogResult = DialogResult.Cancel; + Buildings = buildings; + Terrain = new TerrainManager(sav.GetTerrain()); + + NUD_PlazaX.Value = sav.PlazaX; + NUD_PlazaY.Value = sav.PlazaY; foreach (var obj in buildings) LB_Items.Items.Add(obj.ToString()); - Terrain = new TerrainManager(sav.GetTerrain()); - LB_Items.SelectedIndex = 0; foreach (var entry in HelpDictionary) CB_StructureType.Items.Add(entry.Key); CB_StructureType.SelectedIndex = 0; - NUD_PlazaX.Value = sav.PlazaX; - NUD_PlazaY.Value = sav.PlazaY; + DialogResult = DialogResult.Cancel; } private void B_Cancel_Click(object sender, EventArgs e) => Close(); @@ -47,7 +47,15 @@ private void B_Save_Click(object sender, EventArgs e) private int Index; private bool Loading; - private void DrawMap(in int index) => PB_Map.Image = TerrainSprite.GetMapWithBuildings(Terrain, Buildings, B_Save.Font, 4, index); + + private void DrawMap(in int index) + { + var font = B_Save.Font; + const int scale = 4; + var px = (ushort) NUD_PlazaX.Value; + var py = (ushort) NUD_PlazaY.Value; + PB_Map.Image = TerrainSprite.GetMapWithBuildings(Terrain, Buildings, px, py, font, scale, index); + } private void LB_Items_SelectedIndexChanged(object sender, EventArgs e) { @@ -106,5 +114,7 @@ private void CB_StructureType_SelectedIndexChanged(object sender, EventArgs e) CB_StructureValues.Items.Add(item); CB_StructureValues.SelectedIndex = 0; } + + private void NUD_PlazaCoordinate_ValueChanged(object sender, EventArgs e) => DrawMap(Index); } } From 29ecc8946c2ee178786c01cce709a6e40432dadc Mon Sep 17 00:00:00 2001 From: Kurt Date: Sun, 5 Apr 2020 16:15:55 -0700 Subject: [PATCH 6/6] Fix terrain tile hover coordinate display was snapping same way as viewport reticle split things up and add comments --- NHSE.WinForms/Subforms/TerrainEditor.cs | 31 ++++++++++++++++++++----- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/NHSE.WinForms/Subforms/TerrainEditor.cs b/NHSE.WinForms/Subforms/TerrainEditor.cs index 3e8d1c0..6975e44 100644 --- a/NHSE.WinForms/Subforms/TerrainEditor.cs +++ b/NHSE.WinForms/Subforms/TerrainEditor.cs @@ -338,7 +338,7 @@ private void ClickMapAt(MouseEventArgs e, bool skipLagCheck) int mX = e.X; int mY = e.Y; bool centerReticle = CHK_SnapToAcre.Checked; - GetCoordinates(centerReticle, mX, mY, out var x, out var y); + GetViewAnchorCoordinates(mX, mY, out var x, out var y, centerReticle); var acre = TerrainManager.GetAcre(x, y); bool sameAcre = AcreIndex == acre; @@ -369,14 +369,33 @@ private void ClickMapAt(MouseEventArgs e, bool skipLagCheck) CB_Acre.SelectedIndex = acre; } - private static void GetCoordinates(bool centerReticle, int mX, int mY, out int x, out int y) + private static void GetCursorCoordinates(int mX, int mY, out int x, out int y) { - int center = centerReticle ? 0 : GridWidth / 2; + x = mX / MapScale; + y = mY / MapScale; + } + + private static void GetViewAnchorCoordinates(int mX, int mY, out int x, out int y, bool centerReticle) + { + GetCursorCoordinates(mX, mY, out x, out y); + + // Clamp to viewport dimensions, and center to nearest acre if desired. const int maxX = ((TerrainManager.AcreWidth - 1) * GridWidth); const int maxY = ((TerrainManager.AcreHeight - 1) * GridHeight); - x = Math.Max(0, Math.Min((mX / MapScale) - center, maxX)); - y = Math.Max(0, Math.Min((mY / MapScale) - center, maxY)); + // If we aren't snapping the reticle to the nearest acre + // we want to put the middle of the reticle rectangle where the cursor is. + // Adjust the view coordinate + if (!centerReticle) + { + // Reticle size is GridWidth, center = /2 + x -= GridWidth / 2; + y -= GridWidth / 2; + } + + // Clamp to boundaries so that we always have 16x16 to view. + x = Math.Max(0, Math.Min(x, maxX)); + y = Math.Max(0, Math.Min(y, maxY)); } private void PB_Map_MouseMove(object sender, MouseEventArgs e) @@ -389,7 +408,7 @@ private void PB_Map_MouseMove(object sender, MouseEventArgs e) { int mX = e.X; int mY = e.Y; - GetCoordinates(false, mX, mY, out var x, out var y); + GetCursorCoordinates(mX, mY, out var x, out var y); L_Coordinates.Text = $"({x:000},{y:000}) = (0x{x:X2},0x{y:X2})"; } }