From 60faecdc697ed25d17c586ffbfc9e6e90fad02da Mon Sep 17 00:00:00 2001 From: Kurt Date: Mon, 11 May 2020 22:14:27 -0700 Subject: [PATCH] Refactoring move files, abstract some logic for later reuse --- .../Acres/Terrain/FieldItemLayer.cs | 226 ------------------ .../Structures/Map/Layers/FieldItemLayer.cs | 80 +++++++ NHSE.Core/Structures/Map/Layers/ItemLayer.cs | 146 +++++++++++ .../Structures/Map/Layers/RoomItemLayer.cs | 33 +++ .../Layers/TerrainLayer.cs} | 8 +- .../Managers}/FieldItemManager.cs | 0 .../{Acres/Terrain => Map}/MapGrid.cs | 14 +- .../Structures/{Acres => Map}/OutsideAcre.cs | 0 .../{Acres => Map}/Terrain/AcreCoordinate.cs | 0 .../Map/Terrain/PlacedItemPermission.cs | 9 + .../{Acres => Map}/Terrain/TerrainTile.cs | 0 .../Terrain/TerrainUnitModel.cs | 0 NHSE.Core/Structures/Misc/MapManager.cs | 4 +- ...ItemSpriteDrawer.cs => ItemLayerSprite.cs} | 17 +- .../Map => NHSE.Sprites/Field}/MapViewer.cs | 7 +- NHSE.Sprites/Field/TerrainSprite.cs | 22 +- NHSE.WinForms/Subforms/Map/FieldItemEditor.cs | 9 +- NHSE.WinForms/Subforms/Map/MapDumpHelper.cs | 16 +- 18 files changed, 321 insertions(+), 270 deletions(-) delete mode 100644 NHSE.Core/Structures/Acres/Terrain/FieldItemLayer.cs create mode 100644 NHSE.Core/Structures/Map/Layers/FieldItemLayer.cs create mode 100644 NHSE.Core/Structures/Map/Layers/ItemLayer.cs create mode 100644 NHSE.Core/Structures/Map/Layers/RoomItemLayer.cs rename NHSE.Core/Structures/{Acres/Terrain/TerrainManager.cs => Map/Layers/TerrainLayer.cs} (95%) rename NHSE.Core/Structures/{Acres/Terrain => Map/Managers}/FieldItemManager.cs (100%) rename NHSE.Core/Structures/{Acres/Terrain => Map}/MapGrid.cs (85%) rename NHSE.Core/Structures/{Acres => Map}/OutsideAcre.cs (100%) rename NHSE.Core/Structures/{Acres => Map}/Terrain/AcreCoordinate.cs (100%) create mode 100644 NHSE.Core/Structures/Map/Terrain/PlacedItemPermission.cs rename NHSE.Core/Structures/{Acres => Map}/Terrain/TerrainTile.cs (100%) rename NHSE.Core/Structures/{Acres => Map}/Terrain/TerrainUnitModel.cs (100%) rename NHSE.Sprites/Field/{FieldItemSpriteDrawer.cs => ItemLayerSprite.cs} (87%) rename {NHSE.WinForms/Subforms/Map => NHSE.Sprites/Field}/MapViewer.cs (90%) diff --git a/NHSE.Core/Structures/Acres/Terrain/FieldItemLayer.cs b/NHSE.Core/Structures/Acres/Terrain/FieldItemLayer.cs deleted file mode 100644 index 8631eb0..0000000 --- a/NHSE.Core/Structures/Acres/Terrain/FieldItemLayer.cs +++ /dev/null @@ -1,226 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; - -namespace NHSE.Core -{ - public class FieldItemLayer : MapGrid - { - public readonly Item[] Tiles; - - public FieldItemLayer(Item[] tiles) : base(32, 32) - { - Tiles = tiles; - Debug.Assert(MapTileCount == tiles.Length); - } - - public Item GetTile(int x, int y) => this[GetTileIndex(x, y)]; - public Item GetTile(int acreX, int acreY, int gridX, int gridY) => this[GetTileIndex(acreX, acreY, gridX, gridY)]; - public Item GetAcreTile(int acreIndex, int tileIndex) => this[GetAcreTileIndex(acreIndex, tileIndex)]; - - public Item this[int index] - { - get => Tiles[index]; - set => Tiles[index] = value; - } - - public byte[] DumpAcre(int acre) - { - int count = AcreTileCount; - var result = new byte[Item.SIZE * count]; - for (int i = 0; i < count; i++) - { - var tile = GetAcreTile(acre, i); - var bytes = tile.ToBytesClass(); - bytes.CopyTo(result, i * Item.SIZE); - } - return result; - } - - public byte[] DumpAllAcres() - { - var result = new byte[Tiles.Length * Item.SIZE]; - for (int i = 0; i < Tiles.Length; i++) - Tiles[i].ToBytesClass().CopyTo(result, i * Item.SIZE); - return result; - } - - public void ImportAllAcres(byte[] data) - { - var tiles = Item.GetArray(data); - for (int i = 0; i < tiles.Length; i++) - Tiles[i].CopyFrom(tiles[i]); - } - - public void ImportAcre(int acre, byte[] data) - { - int count = AcreTileCount; - var tiles = Item.GetArray(data); - for (int i = 0; i < count; i++) - { - var tile = GetAcreTile(acre, i); - tile.CopyFrom(tiles[i]); - } - } - - 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(int xmin, int ymin, int width, int height, Func criteria) - { - int count = 0; - var fi = FieldItemList.Items; - - for (int x = xmin; x < xmin + width; x++) - { - for (int y = ymin; y < ymin + height; y++) - { - var t = GetTile(x, y); - var disp = t.DisplayItemId; - if (!fi.TryGetValue(disp, out var val)) - continue; - - if (!criteria(val.Kind)) - continue; - t.Delete(); - count++; - } - } - return count; - } - - public int RemoveAll(int xmin, int ymin, int width, int height, Func criteria) - { - int count = 0; - for (int x = xmin; x < xmin + width; x++) - { - for (int y = ymin; y < ymin + height; y++) - { - var t = GetTile(x, y); - if (!criteria(t)) - continue; - t.Delete(); - count++; - } - } - return count; - } - - public int RemoveAllHoles() => ClearFieldPlanted(z => z == FieldItemKind.UnitIconHole); - public int RemoveAllWeeds() => ClearFieldPlanted(z => z.IsWeed()); - public int RemoveAllPlants() => ClearFieldPlanted(z => z.IsPlant()); - public int RemoveAllFences() => ClearFieldPlanted(z => z.IsFence()); - public int RemoveAllObjects() => ClearFieldPlanted(_ => true); - public int RemoveAll() => RemoveAll(_ => true); - - public int RemoveAllHoles(int xmin, int ymin, int width, int height) => ClearFieldPlanted(xmin, ymin, width, height, z => z == FieldItemKind.UnitIconHole); - public int RemoveAllWeeds(int xmin, int ymin, int width, int height) => ClearFieldPlanted(xmin, ymin, width, height, z => z.IsWeed()); - public int RemoveAllPlants(int xmin, int ymin, int width, int height) => ClearFieldPlanted(xmin, ymin, width, height, z => z.IsPlant()); - public int RemoveAllFences(int xmin, int ymin, int width, int height) => ClearFieldPlanted(xmin, ymin, width, height, z => z.IsFence()); - public int RemoveAllFlowers(int xmin, int ymin, int width, int height) => ClearFieldPlanted(xmin, ymin, width, height, z => z.IsFlower()); - public int RemoveAllObjects(int xmin, int ymin, int width, int height) => ClearFieldPlanted(xmin, ymin, width, height, _ => true); - - public int RemoveAll(int xmin, int ymin, int width, int height) => RemoveAll(xmin, ymin, width, height, _ => true); - public int RemoveAllShells(int xmin, int ymin, int width, int height) => RemoveAll(xmin, ymin, width, height, z => GameLists.Shells.Contains(z.DisplayItemId)); - public int RemoveAllBranches(int xmin, int ymin, int width, int height) => RemoveAll(xmin, ymin, width, height, z => z.DisplayItemId == 2500); - public int RemoveAllPlacedItems(int xmin, int ymin, int width, int height) => RemoveAll(xmin, ymin, width, - height, z => z.DisplayItemId != Item.NONE && !FieldItemList.Items.ContainsKey(z.DisplayItemId)); - - public void DeleteExtensionTiles(Item tile, in int x, in int y) - { - GetTileWidthHeight(tile, x, y, out var w, out var h); - - for (int ix = 0; ix < w; ix++) - { - for (int iy = 0; iy < h; iy++) - { - if (iy == 0 && ix == 0) - continue; - var t = GetTile(x + ix, y + iy); - t.Delete(); - } - } - } - - public void SetExtensionTiles(Item tile, in int x, in int y) - { - GetTileWidthHeight(tile, x, y, out var w, out var h); - - for (byte ix = 0; ix < w; ix++) - { - for (byte iy = 0; iy < h; iy++) - { - if (iy == 0 && ix == 0) - continue; - var t = GetTile(x + ix, y + iy); - t.SetAsExtension(tile, ix, iy); - } - } - } - - private void GetTileWidthHeight(Item tile, int x, int y, out int w, out int h) - { - var type = ItemInfo.GetItemSize(tile); - w = type.GetWidth(); - h = type.GetHeight(); - - // Rotation - if ((tile.Rotation & 1) == 1) - { - var tmp = w; - w = h; - h = tmp; - } - - // Clamp to grid bounds - if (x + w - 1 >= MapWidth) - w = MapWidth - x; - if (y + h - 1 >= MapHeight) - h = MapHeight - y; - } - - /// - /// Checks if writing the at the specified and coordinates will overlap with any existing tiles. - /// - /// True if any tile will be overwritten, false if nothing is there. - public FieldItemPermission IsOccupied(Item tile, in int x, in int y) - { - var type = ItemInfo.GetItemSize(tile); - var w = type.GetWidth(); - var h = type.GetHeight(); - - if (x + w - 1 >= MapWidth) - return FieldItemPermission.OutOfBounds; - if (y + h - 1 >= MapHeight) - return FieldItemPermission.OutOfBounds; - - if ((tile.Rotation & 1) == 1) - { - var tmp = w; - w = h; - h = tmp; - } - - for (byte ix = 0; ix < w; ix++) - { - for (byte iy = 0; iy < h; iy++) - { - var t = GetTile(x + ix, y + iy); - if (!t.IsNone) - return FieldItemPermission.Collision; - } - } - - return FieldItemPermission.NoCollision; - } - } - - public enum FieldItemPermission - { - NoCollision, - Collision, - OutOfBounds, - } -} diff --git a/NHSE.Core/Structures/Map/Layers/FieldItemLayer.cs b/NHSE.Core/Structures/Map/Layers/FieldItemLayer.cs new file mode 100644 index 0000000..346e8b8 --- /dev/null +++ b/NHSE.Core/Structures/Map/Layers/FieldItemLayer.cs @@ -0,0 +1,80 @@ +using System; +using System.Collections.Generic; + +namespace NHSE.Core +{ + public class FieldItemLayer : ItemLayer + { + public FieldItemLayer(Item[] tiles) : base(tiles, 32 * AcreWidth, 32 * AcreHeight, 32, 32) + { + } + + public Item GetTile(int acreX, int acreY, int gridX, int gridY) => this[GetTileIndex(acreX, acreY, gridX, gridY)]; + public Item GetAcreTile(int acreIndex, int tileIndex) => this[GetAcreTileIndex(acreIndex, tileIndex)]; + + public byte[] DumpAcre(int acre) + { + int count = AcreTileCount; + var result = new byte[Item.SIZE * count]; + for (int i = 0; i < count; i++) + { + var tile = GetAcreTile(acre, i); + var bytes = tile.ToBytesClass(); + bytes.CopyTo(result, i * Item.SIZE); + } + return result; + } + + public void ImportAcre(int acre, byte[] data) + { + int count = AcreTileCount; + var tiles = Item.GetArray(data); + for (int i = 0; i < count; i++) + { + var tile = GetAcreTile(acre, i); + tile.CopyFrom(tiles[i]); + } + } + + 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(int xmin, int ymin, int width, int height, Func criteria) + { + int count = 0; + var fi = FieldItemList.Items; + + for (int x = xmin; x < xmin + width; x++) + { + for (int y = ymin; y < ymin + height; y++) + { + var t = GetTile(x, y); + var disp = t.DisplayItemId; + if (!fi.TryGetValue(disp, out var val)) + continue; + + if (!criteria(val.Kind)) + continue; + t.Delete(); + count++; + } + } + return count; + } + + public int RemoveAllHoles(int xmin, int ymin, int width, int height) => ClearFieldPlanted(xmin, ymin, width, height, z => z == FieldItemKind.UnitIconHole); + public int RemoveAllWeeds(int xmin, int ymin, int width, int height) => ClearFieldPlanted(xmin, ymin, width, height, z => z.IsWeed()); + public int RemoveAllPlants(int xmin, int ymin, int width, int height) => ClearFieldPlanted(xmin, ymin, width, height, z => z.IsPlant()); + public int RemoveAllFences(int xmin, int ymin, int width, int height) => ClearFieldPlanted(xmin, ymin, width, height, z => z.IsFence()); + public int RemoveAllFlowers(int xmin, int ymin, int width, int height) => ClearFieldPlanted(xmin, ymin, width, height, z => z.IsFlower()); + public int RemoveAllObjects(int xmin, int ymin, int width, int height) => ClearFieldPlanted(xmin, ymin, width, height, _ => true); + + public int RemoveAll(int xmin, int ymin, int width, int height) => RemoveAll(xmin, ymin, width, height, _ => true); + public int RemoveAllShells(int xmin, int ymin, int width, int height) => RemoveAll(xmin, ymin, width, height, z => GameLists.Shells.Contains(z.DisplayItemId)); + public int RemoveAllBranches(int xmin, int ymin, int width, int height) => RemoveAll(xmin, ymin, width, height, z => z.DisplayItemId == 2500); + public int RemoveAllPlacedItems(int xmin, int ymin, int width, int height) => RemoveAll(xmin, ymin, width, + height, z => z.DisplayItemId != Item.NONE && !FieldItemList.Items.ContainsKey(z.DisplayItemId)); + } +} diff --git a/NHSE.Core/Structures/Map/Layers/ItemLayer.cs b/NHSE.Core/Structures/Map/Layers/ItemLayer.cs new file mode 100644 index 0000000..3e3971a --- /dev/null +++ b/NHSE.Core/Structures/Map/Layers/ItemLayer.cs @@ -0,0 +1,146 @@ +using System; +using System.Diagnostics; + +namespace NHSE.Core +{ + public abstract class ItemLayer : MapGrid + { + public readonly Item[] Tiles; + + protected ItemLayer(Item[] tiles, int w, int h) : this(tiles, w, h, w, h) { } + + protected ItemLayer(Item[] tiles, int w, int h, int gw, int gh) : base(gw, gh, w, h) + { + Tiles = tiles; + Debug.Assert(MaxWidth * MaxHeight == tiles.Length); + } + + public Item GetTile(int x, int y) => this[GetTileIndex(x, y)]; + + public Item this[int index] + { + get => Tiles[index]; + set => Tiles[index] = value; + } + + public byte[] DumpAll() + { + var result = new byte[Tiles.Length * Item.SIZE]; + for (int i = 0; i < Tiles.Length; i++) + Tiles[i].ToBytesClass().CopyTo(result, i * Item.SIZE); + return result; + } + + public void ImportAll(byte[] data) + { + var tiles = Item.GetArray(data); + for (int i = 0; i < tiles.Length; i++) + Tiles[i].CopyFrom(tiles[i]); + } + + public int RemoveAll(int xmin, int ymin, int width, int height, Func criteria) + { + int count = 0; + for (int x = xmin; x < xmin + width; x++) + { + for (int y = ymin; y < ymin + height; y++) + { + var t = GetTile(x, y); + if (!criteria(t)) + continue; + t.Delete(); + count++; + } + } + return count; + } + + public void DeleteExtensionTiles(Item tile, in int x, in int y) + { + GetTileWidthHeight(tile, x, y, out var w, out var h); + + for (int ix = 0; ix < w; ix++) + { + for (int iy = 0; iy < h; iy++) + { + if (iy == 0 && ix == 0) + continue; + var t = GetTile(x + ix, y + iy); + t.Delete(); + } + } + } + + public void SetExtensionTiles(Item tile, in int x, in int y) + { + GetTileWidthHeight(tile, x, y, out var w, out var h); + + for (byte ix = 0; ix < w; ix++) + { + for (byte iy = 0; iy < h; iy++) + { + if (iy == 0 && ix == 0) + continue; + var t = GetTile(x + ix, y + iy); + t.SetAsExtension(tile, ix, iy); + } + } + } + + private void GetTileWidthHeight(Item tile, int x, int y, out int w, out int h) + { + var type = ItemInfo.GetItemSize(tile); + w = type.GetWidth(); + h = type.GetHeight(); + + // Rotation + if ((tile.Rotation & 1) == 1) + { + var tmp = w; + w = h; + h = tmp; + } + + // Clamp to grid bounds + if (x + w - 1 >= MaxWidth) + w = MaxWidth - x; + if (y + h - 1 >= MaxHeight) + h = MaxHeight - y; + } + + /// + /// Checks if writing the at the specified and coordinates will overlap with any existing tiles. + /// + /// True if any tile will be overwritten, false if nothing is there. + public PlacedItemPermission IsOccupied(Item tile, in int x, in int y) + { + var type = ItemInfo.GetItemSize(tile); + var w = type.GetWidth(); + var h = type.GetHeight(); + + if (x + w - 1 >= MaxWidth) + return PlacedItemPermission.OutOfBounds; + if (y + h - 1 >= MaxHeight) + return PlacedItemPermission.OutOfBounds; + + if ((tile.Rotation & 1) == 1) + { + var tmp = w; + w = h; + h = tmp; + } + + for (byte ix = 0; ix < w; ix++) + { + for (byte iy = 0; iy < h; iy++) + { + var t = GetTile(x + ix, y + iy); + if (!t.IsNone) + return PlacedItemPermission.Collision; + } + } + + return PlacedItemPermission.NoCollision; + } + } +} diff --git a/NHSE.Core/Structures/Map/Layers/RoomItemLayer.cs b/NHSE.Core/Structures/Map/Layers/RoomItemLayer.cs new file mode 100644 index 0000000..ac1a647 --- /dev/null +++ b/NHSE.Core/Structures/Map/Layers/RoomItemLayer.cs @@ -0,0 +1,33 @@ +using System.Collections.Generic; + +namespace NHSE.Core +{ + public class RoomItemLayer : ItemLayer + { + public const int SIZE = Width * Height * Item.SIZE; + private const int Width = 20; + private const int Height = 20; + + public RoomItemLayer(byte[] data) : this(Item.GetArray(data)) { } + public RoomItemLayer(Item[] tiles) : base(tiles, Width, Height) { } + + public static RoomItemLayer[] GetArray(byte[] data) + { + var result = new RoomItemLayer[data.Length / SIZE]; + for (int i = 0; i < result.Length; i++) + { + var slice = data.Slice(i * SIZE, SIZE); + result[i] = new RoomItemLayer(slice); + } + return result; + } + + public static byte[] SetArray(IReadOnlyList data) + { + var result = new byte[data.Count * SIZE]; + for (int i = 0; i < data.Count; i++) + data[i].DumpAll().CopyTo(result, i * SIZE); + return result; + } + } +} diff --git a/NHSE.Core/Structures/Acres/Terrain/TerrainManager.cs b/NHSE.Core/Structures/Map/Layers/TerrainLayer.cs similarity index 95% rename from NHSE.Core/Structures/Acres/Terrain/TerrainManager.cs rename to NHSE.Core/Structures/Map/Layers/TerrainLayer.cs index e90d7ed..eefd519 100644 --- a/NHSE.Core/Structures/Acres/Terrain/TerrainManager.cs +++ b/NHSE.Core/Structures/Map/Layers/TerrainLayer.cs @@ -2,12 +2,12 @@ namespace NHSE.Core { - public class TerrainManager : MapGrid + public class TerrainLayer : MapGrid { public readonly TerrainTile[] Tiles; public readonly byte[] BaseAcres; - public TerrainManager(TerrainTile[] tiles, byte[] acres) : base(16, 16) + public TerrainLayer(TerrainTile[] tiles, byte[] acres) : base(16, 16, AcreWidth * 16, AcreHeight * 16) { BaseAcres = acres; Tiles = tiles; @@ -24,7 +24,7 @@ public TerrainManager(TerrainTile[] tiles, byte[] acres) : base(16, 16) set => Tiles[index] = value; } - public byte[] DumpAllAcres() + public byte[] DumpAll() { var result = new byte[Tiles.Length * TerrainTile.SIZE]; for (int i = 0; i < Tiles.Length; i++) @@ -45,7 +45,7 @@ public byte[] DumpAcre(int acre) return result; } - public void ImportAllAcres(byte[] data) + public void ImportAll(byte[] data) { var tiles = TerrainTile.GetArray(data); for (int i = 0; i < tiles.Length; i++) diff --git a/NHSE.Core/Structures/Acres/Terrain/FieldItemManager.cs b/NHSE.Core/Structures/Map/Managers/FieldItemManager.cs similarity index 100% rename from NHSE.Core/Structures/Acres/Terrain/FieldItemManager.cs rename to NHSE.Core/Structures/Map/Managers/FieldItemManager.cs diff --git a/NHSE.Core/Structures/Acres/Terrain/MapGrid.cs b/NHSE.Core/Structures/Map/MapGrid.cs similarity index 85% rename from NHSE.Core/Structures/Acres/Terrain/MapGrid.cs rename to NHSE.Core/Structures/Map/MapGrid.cs index 4f57903..bec7017 100644 --- a/NHSE.Core/Structures/Acres/Terrain/MapGrid.cs +++ b/NHSE.Core/Structures/Map/MapGrid.cs @@ -9,14 +9,18 @@ public abstract class MapGrid { public static readonly AcreCoordinate[] Acres = AcreCoordinate.GetGrid(AcreWidth, AcreHeight); - protected MapGrid(int gw, int gh) + protected MapGrid(int gw, int gh, int mw, int mh) { GridWidth = gw; GridHeight = gh; + MaxWidth = mw; + MaxHeight = mh; } public readonly int GridWidth; public readonly int GridHeight; + public readonly int MaxWidth; + public readonly int MaxHeight; public const int AcreWidth = 7; public const int AcreHeight = 6; @@ -30,7 +34,7 @@ protected MapGrid(int gw, int gh) public const int MapTileCount16x16 = 16 * 16 * AcreCount; public const int MapTileCount32x32 = 32 * 32 * AcreCount; - protected int GetTileIndex(int x, int y) => (MapHeight * x) + y; + protected int GetTileIndex(int x, int y) => (MaxHeight * x) + y; protected int GetTileIndex(int acreX, int acreY, int gridX, int gridY) { @@ -74,5 +78,11 @@ public void GetViewAnchorCoordinates(int acre, out int x, out int y) x = (acre % AcreWidth) * GridWidth; y = (acre / AcreWidth) * GridHeight; } + + 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)); + } } } diff --git a/NHSE.Core/Structures/Acres/OutsideAcre.cs b/NHSE.Core/Structures/Map/OutsideAcre.cs similarity index 100% rename from NHSE.Core/Structures/Acres/OutsideAcre.cs rename to NHSE.Core/Structures/Map/OutsideAcre.cs diff --git a/NHSE.Core/Structures/Acres/Terrain/AcreCoordinate.cs b/NHSE.Core/Structures/Map/Terrain/AcreCoordinate.cs similarity index 100% rename from NHSE.Core/Structures/Acres/Terrain/AcreCoordinate.cs rename to NHSE.Core/Structures/Map/Terrain/AcreCoordinate.cs diff --git a/NHSE.Core/Structures/Map/Terrain/PlacedItemPermission.cs b/NHSE.Core/Structures/Map/Terrain/PlacedItemPermission.cs new file mode 100644 index 0000000..27c02e4 --- /dev/null +++ b/NHSE.Core/Structures/Map/Terrain/PlacedItemPermission.cs @@ -0,0 +1,9 @@ +namespace NHSE.Core +{ + public enum PlacedItemPermission + { + NoCollision, + Collision, + OutOfBounds, + } +} diff --git a/NHSE.Core/Structures/Acres/Terrain/TerrainTile.cs b/NHSE.Core/Structures/Map/Terrain/TerrainTile.cs similarity index 100% rename from NHSE.Core/Structures/Acres/Terrain/TerrainTile.cs rename to NHSE.Core/Structures/Map/Terrain/TerrainTile.cs diff --git a/NHSE.Core/Structures/Acres/Terrain/TerrainUnitModel.cs b/NHSE.Core/Structures/Map/Terrain/TerrainUnitModel.cs similarity index 100% rename from NHSE.Core/Structures/Acres/Terrain/TerrainUnitModel.cs rename to NHSE.Core/Structures/Map/Terrain/TerrainUnitModel.cs diff --git a/NHSE.Core/Structures/Misc/MapManager.cs b/NHSE.Core/Structures/Misc/MapManager.cs index a5ea8dc..c54efaa 100644 --- a/NHSE.Core/Structures/Misc/MapManager.cs +++ b/NHSE.Core/Structures/Misc/MapManager.cs @@ -18,7 +18,7 @@ public MapManager(MainSave sav) : base(sav) public class MapTerrainStructure { - public readonly TerrainManager Terrain; + public readonly TerrainLayer Terrain; public readonly IReadOnlyList Buildings; public uint PlazaX { get; set; } @@ -26,7 +26,7 @@ public class MapTerrainStructure public MapTerrainStructure(MainSave sav) { - Terrain = new TerrainManager(sav.GetTerrainTiles(), sav.GetAcreBytes()); + Terrain = new TerrainLayer(sav.GetTerrainTiles(), sav.GetAcreBytes()); Buildings = sav.Buildings; PlazaX = sav.EventPlazaLeftUpX; PlazaY = sav.EventPlazaLeftUpZ; diff --git a/NHSE.Sprites/Field/FieldItemSpriteDrawer.cs b/NHSE.Sprites/Field/ItemLayerSprite.cs similarity index 87% rename from NHSE.Sprites/Field/FieldItemSpriteDrawer.cs rename to NHSE.Sprites/Field/ItemLayerSprite.cs index 73db715..ec6e024 100644 --- a/NHSE.Sprites/Field/FieldItemSpriteDrawer.cs +++ b/NHSE.Sprites/Field/ItemLayerSprite.cs @@ -3,12 +3,12 @@ namespace NHSE.Sprites { - public static class FieldItemSpriteDrawer + public static class ItemLayerSprite { - public static Bitmap GetBitmapItemLayer(FieldItemLayer layer) + public static Bitmap GetBitmapItemLayer(ItemLayer layer) { var items = layer.Tiles; - var height = layer.MapHeight; + var height = layer.MaxHeight; var width = items.Length / height; var bmpData = new int[width * height]; @@ -31,7 +31,7 @@ private static void LoadBitmapLayer(Item[] items, int[] bmpData, int width, int } } - private static void LoadPixelsFromLayer(FieldItemLayer layer, int x0, int y0, int width, int[] bmpData) + private static void LoadPixelsFromLayer(ItemLayer layer, int x0, int y0, int width, int[] bmpData) { var stride = layer.GridWidth; @@ -49,7 +49,7 @@ private static void LoadPixelsFromLayer(FieldItemLayer layer, int x0, int y0, in } // non-allocation image generator - public static Bitmap GetBitmapItemLayerAcre(FieldItemLayer layer, int x0, int y0, int scale, int[] acre1, int[] acreScale, Bitmap dest, int transparency = -1) + public static Bitmap GetBitmapItemLayerViewGrid(ItemLayer layer, int x0, int y0, int scale, int[] acre1, int[] acreScale, Bitmap dest, int transparency = -1, int gridlineColor = 0) { var w = layer.GridWidth; var h = layer.GridHeight; @@ -65,7 +65,6 @@ public static Bitmap GetBitmapItemLayerAcre(FieldItemLayer layer, int x0, int y0 DrawDirectionals(acreScale, layer, w, x0, y0, scale); // Slap on a grid - const int gridlineColor = 0; // let the underlying image grid show instead DrawGrid(acreScale, w, h, scale, gridlineColor); // Return final data @@ -73,7 +72,7 @@ public static Bitmap GetBitmapItemLayerAcre(FieldItemLayer layer, int x0, int y0 return dest; } - private static void DrawDirectionals(int[] data, FieldItemLayer layer, int w, int x0, int y0, int scale) + private static void DrawDirectionals(int[] data, ItemLayer layer, int w, int x0, int y0, int scale) { for (int x = x0; x < x0 + layer.GridWidth; x++) { @@ -171,9 +170,9 @@ public static void DrawGrid(int[] data, int w, int h, int scale, int gridlineCol } } - public static Bitmap GetBitmapItemLayer(FieldItemLayer layer, int x, int y, int[] data, Bitmap dest, int transparency = -1) + public static Bitmap GetBitmapItemLayer(ItemLayer layer, int x, int y, int[] data, Bitmap dest, int transparency = -1) { - LoadBitmapLayer(layer.Tiles, data, layer.MapWidth, layer.MapHeight); + LoadBitmapLayer(layer.Tiles, data, layer.MaxWidth, layer.MaxHeight); if (transparency >> 24 != 0xFF) ImageUtil.ClampAllTransparencyTo(data, transparency); ImageUtil.SetBitmapData(dest, data); diff --git a/NHSE.WinForms/Subforms/Map/MapViewer.cs b/NHSE.Sprites/Field/MapViewer.cs similarity index 90% rename from NHSE.WinForms/Subforms/Map/MapViewer.cs rename to NHSE.Sprites/Field/MapViewer.cs index 88ac362..2da288a 100644 --- a/NHSE.WinForms/Subforms/Map/MapViewer.cs +++ b/NHSE.Sprites/Field/MapViewer.cs @@ -1,9 +1,8 @@ using System; using System.Drawing; using NHSE.Core; -using NHSE.Sprites; -namespace NHSE.WinForms +namespace NHSE.Sprites { public sealed class MapViewer : MapView, IDisposable { @@ -59,7 +58,7 @@ public Bitmap GetBackgroundTerrain(int index = -1) private Bitmap GetLayerAcre(int topX, int topY, int t) { var layer = Map.CurrentLayer; - return FieldItemSpriteDrawer.GetBitmapItemLayerAcre(layer, topX, topY, AcreScale, PixelsItemAcre1, PixelsItemAcreX, ScaleAcre, t); + return ItemLayerSprite.GetBitmapItemLayerViewGrid(layer, topX, topY, AcreScale, PixelsItemAcre1, PixelsItemAcreX, ScaleAcre, t); } public Bitmap GetBackgroundAcre(Font f, byte tbuild, byte tterrain, int index = -1) @@ -69,7 +68,7 @@ public Bitmap GetBackgroundAcre(Font f, byte tbuild, byte tterrain, int index = private Bitmap GetMapWithReticle(int topX, int topY, int t, FieldItemLayer layer) { - return FieldItemSpriteDrawer.GetBitmapItemLayer(layer, topX, topY, PixelsItemMap, MapReticle, t); + return ItemLayerSprite.GetBitmapItemLayer(layer, topX, topY, PixelsItemMap, MapReticle, t); } } } \ No newline at end of file diff --git a/NHSE.Sprites/Field/TerrainSprite.cs b/NHSE.Sprites/Field/TerrainSprite.cs index 116c777..8465fac 100644 --- a/NHSE.Sprites/Field/TerrainSprite.cs +++ b/NHSE.Sprites/Field/TerrainSprite.cs @@ -17,7 +17,7 @@ public static class TerrainSprite private const int PlazaWidth = 6 * 2; private const int PlazaHeight = 5 * 2; - public static void CreateMap(TerrainManager mgr, int[] pixels) + public static void CreateMap(TerrainLayer mgr, int[] pixels) { int i = 0; for (int y = 0; y < mgr.MapHeight; y++) @@ -29,7 +29,7 @@ public static void CreateMap(TerrainManager mgr, int[] pixels) } } - public static Bitmap CreateMap(TerrainManager mgr, int scale, int x, int y, int[] scale1, int[] scaleX, Bitmap map) + public static Bitmap CreateMap(TerrainLayer mgr, int scale, int x, int y, int[] scale1, int[] scaleX, Bitmap map) { CreateMap(mgr, scale1); ImageUtil.ScalePixelImage(scale1, scaleX, map.Width, map.Height, scale); @@ -37,7 +37,7 @@ public static Bitmap CreateMap(TerrainManager mgr, int scale, int x, int y, int[ return DrawReticle(map, mgr, x, y, scale); } - public static Bitmap CreateMap(TerrainManager mgr, int[] scale1, int[] scaleX, Bitmap map, int scale, int acreIndex = -1) + public static Bitmap CreateMap(TerrainLayer mgr, int[] scale1, int[] scaleX, Bitmap map, int scale, int acreIndex = -1) { CreateMap(mgr, scale1); ImageUtil.ScalePixelImage(scale1, scaleX, map.Width, map.Height, scale); @@ -74,7 +74,7 @@ public static Bitmap GetMapWithBuildings(MapTerrainStructure m, Font? f, int[] s return map; } - private static void DrawPlaza(this Graphics gfx, TerrainManager g, ushort px, ushort py, int scale) + private static void DrawPlaza(this Graphics gfx, TerrainLayer g, ushort px, ushort py, int scale) { g.GetBuildingCoordinate(px, py, scale, out var x, out var y); @@ -84,7 +84,7 @@ private static void DrawPlaza(this Graphics gfx, TerrainManager g, ushort px, us gfx.FillRectangle(Plaza, x, y, width, height); } - private static void DrawBuildings(this Graphics gfx, TerrainManager g, IReadOnlyList buildings, Font? f, int scale, int index = -1) + private static void DrawBuildings(this Graphics gfx, TerrainLayer g, IReadOnlyList buildings, Font? f, int scale, int index = -1) { for (int i = 0; i < buildings.Count; i++) { @@ -109,13 +109,13 @@ private static void DrawBuilding(Graphics gfx, Font? f, int scale, Brush pen, in } } - private static void SetAcreTerrainPixels(int x, int y, TerrainManager t, int[] data, int[] scaleX, int scale) + private static void SetAcreTerrainPixels(int x, int y, TerrainLayer t, int[] data, int[] scaleX, int scale) { GetAcre1(x, y, t, data); ImageUtil.ScalePixelImage(data, scaleX, 16 * scale, 16 * scale, scale); } - private static void GetAcre1(int topX, int topY, TerrainManager t, int[] data) + private static void GetAcre1(int topX, int topY, TerrainLayer t, int[] data) { int index = 0; for (int y = 0; y < 16; y++) @@ -157,8 +157,8 @@ public static Bitmap GetAcre(MapView m, Font f, int[] scale1, int[] scaleX, Bitm } ImageUtil.GetBitmapData(acre, scaleX); - FieldItemSpriteDrawer.DrawGrid(scaleX, acre.Width, acre.Height, m.AcreScale, grid1); - FieldItemSpriteDrawer.DrawGrid(scaleX, acre.Width, acre.Height, m.TerrainScale, grid2); + ItemLayerSprite.DrawGrid(scaleX, acre.Width, acre.Height, m.AcreScale, grid1); + ItemLayerSprite.DrawGrid(scaleX, acre.Width, acre.Height, m.TerrainScale, grid2); ImageUtil.SetBitmapData(acre, scaleX); foreach (var b in buildings) @@ -176,7 +176,7 @@ public static Bitmap GetAcre(MapView m, Font f, int[] scale1, int[] scaleX, Bitm return acre; } - private static void DrawTerrainTileNames(int topX, int topY, Graphics gfx, TerrainManager t, Font f, int scale, byte transparency) + private static void DrawTerrainTileNames(int topX, int topY, Graphics gfx, TerrainLayer t, Font f, int scale, byte transparency) { var pen= transparency != byte.MaxValue ? new SolidBrush(Color.FromArgb(transparency, Color.Black)) : Tile; @@ -196,7 +196,7 @@ private static void DrawTerrainTileNames(int topX, int topY, Graphics gfx, Terra } } - private static void DrawAcrePlaza(this Graphics gfx, TerrainManager g, int topX, int topY, ushort px, ushort py, int scale, byte transparency) + private static void DrawAcrePlaza(this Graphics gfx, TerrainLayer g, int topX, int topY, ushort px, ushort py, int scale, byte transparency) { g.GetBuildingRelativeCoordinates(topX, topY, scale, px, py, out var x, out var y); diff --git a/NHSE.WinForms/Subforms/Map/FieldItemEditor.cs b/NHSE.WinForms/Subforms/Map/FieldItemEditor.cs index cf5a2e0..0c5af7d 100644 --- a/NHSE.WinForms/Subforms/Map/FieldItemEditor.cs +++ b/NHSE.WinForms/Subforms/Map/FieldItemEditor.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Windows.Forms; using NHSE.Core; +using NHSE.Sprites; namespace NHSE.WinForms { @@ -290,8 +291,8 @@ private void ViewTile(Item tile, int x, int y) if (CHK_RedirectExtensionLoad.Checked && tile.IsExtension) { var l = Map.CurrentLayer; - var rx = Math.Max(0, Math.Min(l.MapWidth - 1, x - tile.ExtensionX)); - var ry = Math.Max(0, Math.Min(l.MapHeight - 1, y - tile.ExtensionY)); + var rx = Math.Max(0, Math.Min(l.MaxWidth - 1, x - tile.ExtensionX)); + var ry = Math.Max(0, Math.Min(l.MaxHeight - 1, y - tile.ExtensionY)); var redir = l.GetTile(rx, ry); if (redir.IsRoot && redir.ItemId == tile.ExtensionItemId) tile = redir; @@ -322,8 +323,8 @@ private void SetTile(Item tile, int x, int y) var permission = l.IsOccupied(pgt, x, y); switch (permission) { - case FieldItemPermission.OutOfBounds: - case FieldItemPermission.Collision when CHK_NoOverwrite.Checked: + case PlacedItemPermission.OutOfBounds: + case PlacedItemPermission.Collision when CHK_NoOverwrite.Checked: System.Media.SystemSounds.Asterisk.Play(); return; } diff --git a/NHSE.WinForms/Subforms/Map/MapDumpHelper.cs b/NHSE.WinForms/Subforms/Map/MapDumpHelper.cs index 00ec343..ef0d52d 100644 --- a/NHSE.WinForms/Subforms/Map/MapDumpHelper.cs +++ b/NHSE.WinForms/Subforms/Map/MapDumpHelper.cs @@ -53,7 +53,7 @@ public static bool ImportToLayerAcreAll(FieldItemLayer layer) } var data = File.ReadAllBytes(path); - layer.ImportAllAcres(data); + layer.ImportAll(data); return true; } @@ -83,11 +83,11 @@ public static void DumpLayerAcreAll(FieldItemLayer layer) return; var path = sfd.FileName; - var data = layer.DumpAllAcres(); + var data = layer.DumpAll(); File.WriteAllBytes(path, data); } - public static bool ImportTerrainAcre(TerrainManager m, int acreIndex, string acre) + public static bool ImportTerrainAcre(TerrainLayer m, int acreIndex, string acre) { using var ofd = new OpenFileDialog { @@ -112,7 +112,7 @@ public static bool ImportTerrainAcre(TerrainManager m, int acreIndex, string acr return true; } - public static bool ImportTerrainAll(TerrainManager m) + public static bool ImportTerrainAll(TerrainLayer m) { using var ofd = new OpenFileDialog { @@ -133,11 +133,11 @@ public static bool ImportTerrainAll(TerrainManager m) } var data = File.ReadAllBytes(path); - m.ImportAllAcres(data); + m.ImportAll(data); return true; } - public static void DumpTerrainAcre(TerrainManager m, int acreIndex, string acre) + public static void DumpTerrainAcre(TerrainLayer m, int acreIndex, string acre) { using var sfd = new SaveFileDialog { @@ -152,7 +152,7 @@ public static void DumpTerrainAcre(TerrainManager m, int acreIndex, string acre) File.WriteAllBytes(path, data); } - public static void DumpTerrainAll(TerrainManager m) + public static void DumpTerrainAll(TerrainLayer m) { using var sfd = new SaveFileDialog { @@ -163,7 +163,7 @@ public static void DumpTerrainAll(TerrainManager m) return; var path = sfd.FileName; - var data = m.DumpAllAcres(); + var data = m.DumpAll(); File.WriteAllBytes(path, data); }