From 902dd90dc9bdfd74e86cf117bcb3c7c16261bd71 Mon Sep 17 00:00:00 2001 From: Kurt Date: Mon, 11 May 2020 23:13:40 -0700 Subject: [PATCH] Remove hardcoded map dimension properties we can refer to MaxWidth/MaxHeight instead --- NHSE.Core/Structures/Map/Layers/FieldItemLayer.cs | 12 ++++++------ NHSE.Core/Structures/Map/Layers/TerrainLayer.cs | 10 +++++----- NHSE.Core/Structures/Map/MapGrid.cs | 10 ++++------ NHSE.Core/Structures/Misc/MapView.cs | 14 +++++++------- NHSE.Sprites/Field/MapViewer.cs | 4 ++-- NHSE.Sprites/Field/TerrainSprite.cs | 4 ++-- NHSE.WinForms/Subforms/Map/MapDumpHelper.cs | 8 ++++---- 7 files changed, 30 insertions(+), 32 deletions(-) diff --git a/NHSE.Core/Structures/Map/Layers/FieldItemLayer.cs b/NHSE.Core/Structures/Map/Layers/FieldItemLayer.cs index 346e8b8..5381bec 100644 --- a/NHSE.Core/Structures/Map/Layers/FieldItemLayer.cs +++ b/NHSE.Core/Structures/Map/Layers/FieldItemLayer.cs @@ -14,7 +14,7 @@ public FieldItemLayer(Item[] tiles) : base(tiles, 32 * AcreWidth, 32 * AcreHeigh public byte[] DumpAcre(int acre) { - int count = AcreTileCount; + int count = GridTileCount; var result = new byte[Item.SIZE * count]; for (int i = 0; i < count; i++) { @@ -27,7 +27,7 @@ public byte[] DumpAcre(int acre) public void ImportAcre(int acre, byte[] data) { - int count = AcreTileCount; + int count = GridTileCount; var tiles = Item.GetArray(data); for (int i = 0; i < count; i++) { @@ -36,10 +36,10 @@ public void ImportAcre(int acre, byte[] data) } } - public int ClearFieldPlanted(Func criteria) => ClearFieldPlanted(0, 0, MapWidth, MapHeight, criteria); - public int RemoveAll(Func criteria) => RemoveAll(0, 0, MapWidth, MapHeight, criteria); - public int RemoveAll(HashSet items) => RemoveAll(0, 0, MapWidth, MapHeight, z => items.Contains(z.DisplayItemId)); - public int RemoveAll(ushort item) => RemoveAll(0, 0, MapWidth, MapHeight, z => z.DisplayItemId == item); + public int ClearFieldPlanted(Func criteria) => ClearFieldPlanted(0, 0, MaxWidth, MaxHeight, criteria); + public int RemoveAll(Func criteria) => RemoveAll(0, 0, MaxWidth, MaxHeight, criteria); + public int RemoveAll(HashSet items) => RemoveAll(0, 0, MaxWidth, MaxHeight, z => items.Contains(z.DisplayItemId)); + public int RemoveAll(ushort item) => RemoveAll(0, 0, MaxWidth, MaxHeight, z => z.DisplayItemId == item); public int ClearFieldPlanted(int xmin, int ymin, int width, int height, Func criteria) { diff --git a/NHSE.Core/Structures/Map/Layers/TerrainLayer.cs b/NHSE.Core/Structures/Map/Layers/TerrainLayer.cs index eefd519..1061b24 100644 --- a/NHSE.Core/Structures/Map/Layers/TerrainLayer.cs +++ b/NHSE.Core/Structures/Map/Layers/TerrainLayer.cs @@ -11,7 +11,7 @@ public TerrainLayer(TerrainTile[] tiles, byte[] acres) : base(16, 16, AcreWidth { BaseAcres = acres; Tiles = tiles; - Debug.Assert(MapTileCount == tiles.Length); + Debug.Assert(MaxTileCount == tiles.Length); } public TerrainTile GetTile(int x, int y) => this[GetTileIndex(x, y)]; @@ -34,7 +34,7 @@ public byte[] DumpAll() public byte[] DumpAcre(int acre) { - int count = AcreTileCount; + int count = GridTileCount; var result = new byte[TerrainTile.SIZE * count]; for (int i = 0; i < count; i++) { @@ -54,7 +54,7 @@ public void ImportAll(byte[] data) public void ImportAcre(int acre, byte[] data) { - int count = AcreTileCount; + int count = GridTileCount; var tiles = TerrainTile.GetArray(data); for (int i = 0; i < count; i++) { @@ -70,8 +70,8 @@ public void SetAll(TerrainTile tile, in bool interiorOnly) // skip outermost ring of tiles int xmin = GridWidth; int ymin = GridHeight; - int xmax = MapWidth - GridWidth; - int ymax = MapHeight - GridHeight; + int xmax = MaxWidth - GridWidth; + int ymax = MaxHeight - GridHeight; for (int x = xmin; x < xmax; x++) { for (int y = ymin; y < ymax; y++) diff --git a/NHSE.Core/Structures/Map/MapGrid.cs b/NHSE.Core/Structures/Map/MapGrid.cs index bec7017..77f457b 100644 --- a/NHSE.Core/Structures/Map/MapGrid.cs +++ b/NHSE.Core/Structures/Map/MapGrid.cs @@ -26,10 +26,8 @@ protected MapGrid(int gw, int gh, int mw, int mh) public const int AcreHeight = 6; public const int AcreCount = AcreWidth * AcreHeight; - public int AcreTileCount => GridWidth * GridHeight; - public int MapHeight => AcreHeight * GridHeight; // 96 (when 16x16) - public int MapWidth => AcreWidth * GridWidth; // 112 (when 16x16) - public int MapTileCount => MapWidth * MapHeight; // 0x2A00 bytes (when 16x16) + public int GridTileCount => GridWidth * GridHeight; + public int MaxTileCount => MaxWidth * MaxHeight; public const int MapTileCount16x16 = 16 * 16 * AcreCount; public const int MapTileCount32x32 = 32 * 32 * AcreCount; @@ -81,8 +79,8 @@ public void GetViewAnchorCoordinates(int acre, out int x, out int y) public void ClampCoordinates(ref int x, ref int y) { - x = Math.Max(0, Math.Min(x, MapWidth - 1)); - y = Math.Max(0, Math.Min(y, MapHeight - 1)); + x = Math.Max(0, Math.Min(x, MaxWidth - 1)); + y = Math.Max(0, Math.Min(y, MaxHeight - 1)); } } } diff --git a/NHSE.Core/Structures/Misc/MapView.cs b/NHSE.Core/Structures/Misc/MapView.cs index 0b9508b..55b1c47 100644 --- a/NHSE.Core/Structures/Misc/MapView.cs +++ b/NHSE.Core/Structures/Misc/MapView.cs @@ -18,9 +18,9 @@ public class MapView protected MapView(MapManager m) => Map = m; public bool CanUp => Y != 0; - public bool CanDown => Y < Map.CurrentLayer.MapHeight - Map.CurrentLayer.GridHeight; + public bool CanDown => Y < Map.CurrentLayer.MaxHeight - Map.CurrentLayer.GridHeight; public bool CanLeft => X != 0; - public bool CanRight => X < Map.CurrentLayer.MapWidth - Map.CurrentLayer.GridWidth; + public bool CanRight => X < Map.CurrentLayer.MaxWidth - Map.CurrentLayer.GridWidth; public bool ArrowUp() { @@ -40,7 +40,7 @@ public bool ArrowLeft() public bool ArrowRight() { - if (X >= Map.CurrentLayer.MapWidth - 2) + if (X >= Map.CurrentLayer.MaxWidth - 2) return false; X += ViewInterval; return true; @@ -48,7 +48,7 @@ public bool ArrowRight() public bool ArrowDown() { - if (Y >= Map.CurrentLayer.MapHeight - ViewInterval) + if (Y >= Map.CurrentLayer.MaxHeight - ViewInterval) return false; Y += ViewInterval; return true; @@ -57,8 +57,8 @@ public bool ArrowDown() public bool SetViewTo(in int x, in int y) { var info = Map.CurrentLayer; - var newX = Math.Max(0, Math.Min(x, info.MapWidth - info.GridWidth)); - var newY = Math.Max(0, Math.Min(y, info.MapHeight - info.GridHeight)); + var newX = Math.Max(0, Math.Min(x, info.MaxWidth - info.GridWidth)); + var newY = Math.Max(0, Math.Min(y, info.MaxHeight - info.GridHeight)); bool diff = X != newX || Y != newY; X = newX; Y = newY; @@ -76,7 +76,7 @@ public int RemoveFieldItems(Func removal, bool wholeMap { var layer = Map.CurrentLayer; return wholeMap - ? removal(0, 0, layer.MapWidth, layer.MapHeight) + ? removal(0, 0, layer.MaxWidth, layer.MaxHeight) : removal(X, Y, layer.GridWidth, layer.GridHeight); } diff --git a/NHSE.Sprites/Field/MapViewer.cs b/NHSE.Sprites/Field/MapViewer.cs index 2da288a..92fcf9f 100644 --- a/NHSE.Sprites/Field/MapViewer.cs +++ b/NHSE.Sprites/Field/MapViewer.cs @@ -27,8 +27,8 @@ public MapViewer(MapManager m) : base(m) PixelsItemAcreX = new int[PixelsItemAcre1.Length * AcreScale * AcreScale]; ScaleAcre = new Bitmap(l1.GridWidth * AcreScale, l1.GridHeight * AcreScale); - PixelsItemMap = new int[l1.MapWidth * l1.MapHeight * MapScale * MapScale]; - MapReticle = new Bitmap(l1.MapWidth * MapScale, l1.MapHeight * MapScale); + PixelsItemMap = new int[l1.MaxWidth * l1.MaxHeight * MapScale * MapScale]; + MapReticle = new Bitmap(l1.MaxWidth * MapScale, l1.MaxHeight * MapScale); PixelsBackgroundAcre1 = new int[16 * 16]; PixelsBackgroundAcreX = new int[PixelsItemAcreX.Length]; diff --git a/NHSE.Sprites/Field/TerrainSprite.cs b/NHSE.Sprites/Field/TerrainSprite.cs index 8465fac..e9d23a0 100644 --- a/NHSE.Sprites/Field/TerrainSprite.cs +++ b/NHSE.Sprites/Field/TerrainSprite.cs @@ -20,9 +20,9 @@ public static class TerrainSprite public static void CreateMap(TerrainLayer mgr, int[] pixels) { int i = 0; - for (int y = 0; y < mgr.MapHeight; y++) + for (int y = 0; y < mgr.MaxHeight; y++) { - for (int x = 0; x < mgr.MapWidth; x++, i++) + for (int x = 0; x < mgr.MaxWidth; x++, i++) { pixels[i] = mgr.GetTileColor(x, y); } diff --git a/NHSE.WinForms/Subforms/Map/MapDumpHelper.cs b/NHSE.WinForms/Subforms/Map/MapDumpHelper.cs index ef0d52d..85ca084 100644 --- a/NHSE.WinForms/Subforms/Map/MapDumpHelper.cs +++ b/NHSE.WinForms/Subforms/Map/MapDumpHelper.cs @@ -20,7 +20,7 @@ public static bool ImportToLayerAcreSingle(FieldItemLayer layer, int acreIndex, var path = ofd.FileName; var fi = new FileInfo(path); - int expect = layer.AcreTileCount * Item.SIZE; + int expect = layer.GridTileCount * Item.SIZE; if (fi.Length != expect) { WinFormsUtil.Error(string.Format(MessageStrings.MsgDataSizeMismatchImport, fi.Length, expect)); @@ -45,7 +45,7 @@ public static bool ImportToLayerAcreAll(FieldItemLayer layer) var path = ofd.FileName; var fi = new FileInfo(path); - int expect = layer.MapTileCount * Item.SIZE; + int expect = layer.MaxTileCount * Item.SIZE; if (fi.Length != expect) { WinFormsUtil.Error(string.Format(MessageStrings.MsgDataSizeMismatchImport, fi.Length, expect)); @@ -100,7 +100,7 @@ public static bool ImportTerrainAcre(TerrainLayer m, int acreIndex, string acre) var path = ofd.FileName; var fi = new FileInfo(path); - int expect = m.AcreTileCount * TerrainTile.SIZE; + int expect = m.GridTileCount * TerrainTile.SIZE; if (fi.Length != expect) { WinFormsUtil.Error(string.Format(MessageStrings.MsgDataSizeMismatchImport, fi.Length, expect)); @@ -125,7 +125,7 @@ public static bool ImportTerrainAll(TerrainLayer m) var path = ofd.FileName; var fi = new FileInfo(path); - int expect = m.MapTileCount * TerrainTile.SIZE; + int expect = m.MaxTileCount * TerrainTile.SIZE; if (fi.Length != expect) { WinFormsUtil.Error(string.Format(MessageStrings.MsgDataSizeMismatchImport, fi.Length, expect));