From 0ebfc36c651983f62283fdee31252b571fd26f41 Mon Sep 17 00:00:00 2001 From: Bas Date: Fri, 13 Nov 2020 13:48:17 +0100 Subject: [PATCH] Tweaked readability of a few scripts. --- src/openrct2-dll/Bindings/Graphics.cpp | 7 +++-- .../Bindings/TileElements/Surface.cpp | 29 ++++++++++++------- .../Retro/SurfaceGenerator.Textures.cs | 21 ++++++++------ .../Generation/Retro/SurfaceGenerator.cs | 26 +++++++++++++---- .../OpenRCT2/TileElements/TileElement.cs | 2 +- .../ProjectSettings/EditorBuildSettings.asset | 6 ++-- 6 files changed, 60 insertions(+), 31 deletions(-) diff --git a/src/openrct2-dll/Bindings/Graphics.cpp b/src/openrct2-dll/Bindings/Graphics.cpp index 30d118c9ea..58bd79d0aa 100644 --- a/src/openrct2-dll/Bindings/Graphics.cpp +++ b/src/openrct2-dll/Bindings/Graphics.cpp @@ -37,11 +37,12 @@ extern "C" // Returns the image index of the tile element and its texture size. EXPORT void GetTextureData(uint32_t imageIndex, sprite_data* data) { - const rct_g1_element* g1 = gfx_get_g1_element(imageIndex & 0x7FFFF); + const int32_t maskedImageId = imageIndex & 0x7FFFF; + const rct_g1_element* g1 = gfx_get_g1_element(maskedImageId); if (g1 == nullptr) { - dll_log("Could not find g1 element for %i.", imageIndex); + dll_log("Could not find g1 element data for %i. (Unmasked: %i)", maskedImageId, imageIndex); return; } @@ -60,7 +61,7 @@ extern "C" if (g1 == nullptr) { - dll_log("Could not find g1 element for %i.", maskedImageId); + dll_log("Could not find g1 element pixels for %i. (Unmasked: %i)", maskedImageId, imageIndex); return; } diff --git a/src/openrct2-dll/Bindings/TileElements/Surface.cpp b/src/openrct2-dll/Bindings/TileElements/Surface.cpp index 4338e7325d..80cde83231 100644 --- a/src/openrct2-dll/Bindings/TileElements/Surface.cpp +++ b/src/openrct2-dll/Bindings/TileElements/Surface.cpp @@ -14,15 +14,19 @@ extern "C" EXPORT uint32_t GetSurfaceImageIndex(const TileElement* tileElement, int32_t tileX, int32_t tileY, uint8_t direction) { SurfaceElement* surface = tileElement->AsSurface(); - auto surfaceIndex = surface->GetSurfaceStyle(); - auto grassLength = surface->GetGrassLength(); + uint32_t surfaceIndex = surface->GetSurfaceStyle(); + uint8_t grassLength = surface->GetGrassLength(); - auto imageId = (uint32_t)SPR_NONE; + uint32_t imageId = (uint32_t)SPR_NONE; - auto& objMgr = OpenRCT2::GetContext()->GetObjectManager(); - auto obj = objMgr.GetLoadedObject(OBJECT_TYPE_TERRAIN_SURFACE, surfaceIndex); + IObjectManager& objMgr = OpenRCT2::GetContext()->GetObjectManager(); + Object* obj = objMgr.GetLoadedObject(OBJECT_TYPE_TERRAIN_SURFACE, surfaceIndex); - if (obj != nullptr) + if (obj == nullptr) + { + dll_log("Could not find surface object: %i", surfaceIndex); + } + else { TerrainSurfaceObject* result = static_cast(obj); @@ -41,11 +45,16 @@ extern "C" EXPORT uint32_t GetSurfaceEdgeImageIndex(const TileElement* tileElement) { SurfaceElement* surface = tileElement->AsSurface(); - auto edgeIndex = surface->GetEdgeStyle(); + uint32_t edgeIndex = surface->GetEdgeStyle(); - auto& objMgr = OpenRCT2::GetContext()->GetObjectManager(); - auto obj = objMgr.GetLoadedObject(OBJECT_TYPE_TERRAIN_EDGE, edgeIndex); - if (obj != nullptr) + IObjectManager& objMgr = OpenRCT2::GetContext()->GetObjectManager(); + Object* obj = objMgr.GetLoadedObject(OBJECT_TYPE_TERRAIN_EDGE, edgeIndex); + + if (obj == nullptr) + { + dll_log("Could not find surface edge object: %i", edgeIndex); + } + else { auto tobj = static_cast(obj); return tobj->BaseImageId + 5; // EDGE_BOTTOMRIGHT = +5 diff --git a/src/openrct2-unity/Assets/Scripts/Generation/Retro/SurfaceGenerator.Textures.cs b/src/openrct2-unity/Assets/Scripts/Generation/Retro/SurfaceGenerator.Textures.cs index 4afa0669be..256604fb5f 100644 --- a/src/openrct2-unity/Assets/Scripts/Generation/Retro/SurfaceGenerator.Textures.cs +++ b/src/openrct2-unity/Assets/Scripts/Generation/Retro/SurfaceGenerator.Textures.cs @@ -15,9 +15,12 @@ namespace Generation.Retro [SerializeField] string _waterRefractionField = "WaterRefraction"; - const byte TypeSurface = 1; - const byte TypeEdge = 2; - const byte TypeWater = 3; + enum TextureType : byte + { + Surface = 1, + Edge = 2, + Water = 3 + } List _images; @@ -26,7 +29,7 @@ namespace Generation.Retro /// /// Pushes a image index to the materials stack and returns its list index. /// - int PushImageIndex(uint imageIndex, byte type) + int PushImageIndex(uint imageIndex, TextureType type) { int position = _images.FindIndex(i => i.ImageIndex == imageIndex); if (position == -1) @@ -63,17 +66,17 @@ namespace Generation.Retro switch (image.Type) { - case TypeSurface: + case TextureType.Surface: material = new Material(_surfaceShader); material.SetTexture(_surfaceTextureField, texture); break; - case TypeEdge: + case TextureType.Edge: material = new Material(_edgeShader); material.SetTexture(_edgeTextureField, texture); break; - case TypeWater: + case TextureType.Water: material = new Material(_waterShader); material.SetTexture(_waterTextureField, texture); @@ -103,10 +106,10 @@ namespace Generation.Retro readonly struct RequestedImage { public readonly uint ImageIndex; - public readonly byte Type; + public readonly TextureType Type; - public RequestedImage(uint imageIndex, byte type) + public RequestedImage(uint imageIndex, TextureType type) { ImageIndex = imageIndex; Type = type; diff --git a/src/openrct2-unity/Assets/Scripts/Generation/Retro/SurfaceGenerator.cs b/src/openrct2-unity/Assets/Scripts/Generation/Retro/SurfaceGenerator.cs index 474f9d3afc..2f01c87df2 100644 --- a/src/openrct2-unity/Assets/Scripts/Generation/Retro/SurfaceGenerator.cs +++ b/src/openrct2-unity/Assets/Scripts/Generation/Retro/SurfaceGenerator.cs @@ -64,6 +64,10 @@ namespace Generation.Retro foreach (Chunk chunk in _chunks) { Mesh mesh = chunk.builder.ToMesh(); + + if (mesh.vertexCount == 0) + continue; + mesh.name = $"Chunk ({chunk.x}, {chunk.y})"; mesh.RecalculateNormals(); @@ -131,6 +135,16 @@ namespace Generation.Retro * y * x --> */ + + uint surfaceImage = OpenRCT2.GetSurfaceImageIndex(tile, x, y, 0); + if (surfaceImage == uint.MaxValue) + { + Debug.LogWarning($"Invalid surface image at ({x}, {y})."); + return; // empty tile can happen? + } + + int surfaceSubmesh = chunk.AddMaterialIndex(PushImageIndex(surfaceImage, TextureType.Surface)); + SurfaceElement surface = tile.AsSurface(); SurfaceSlope slope = surface.Slope; int baseHeight = tile.baseHeight; @@ -140,9 +154,6 @@ namespace Generation.Retro int southHeight = GetSurfaceCorner(localX, localY, baseHeight, slope, SurfaceSlope.SouthUp, out Vertex south); int westHeight = GetSurfaceCorner(localX, localY + 1, baseHeight, slope, SurfaceSlope.WestUp, out Vertex west); - uint surfaceImage = OpenRCT2.GetSurfaceImageIndex(tile, x, y, 0); - int surfaceSubmesh = chunk.AddMaterialIndex(PushImageIndex(surfaceImage, TypeSurface)); - SurfaceSlope rotatedSlope = (slope & SurfaceSlope.WestEastValley); if (rotatedSlope == 0 || rotatedSlope == SurfaceSlope.WestEastValley) { @@ -166,16 +177,21 @@ namespace Generation.Retro Vertex waterSouth = new Vertex(south.position.x, waterVertexHeight, south.position.z, Vector3.up, south.uv); Vertex waterWest = new Vertex(west.position.x, waterVertexHeight, west.position.z, Vector3.up, west.uv); - int waterSubmesh = chunk.AddMaterialIndex(PushImageIndex(_waterImageIndex, TypeWater)); + int waterSubmesh = chunk.AddMaterialIndex(PushImageIndex(_waterImageIndex, TextureType.Water)); chunk.builder.AddQuad(waterNorth, waterEast, waterSouth, waterWest, waterSubmesh); } // Edges uint edgeImage = OpenRCT2.GetSurfaceEdgeImageIndex(tile); + if (edgeImage == uint.MaxValue) + { + Debug.LogWarning($"Invalid surface edge image at ({x}, {y})."); + return; // empty tile can happen? + } // HACK: only add the material stack index when any of the add-edges succeed. // Otherwise it will create inconsistency in materials, because some may not be used. - int materialStackIndex = PushImageIndex(edgeImage, TypeEdge); + int materialStackIndex = PushImageIndex(edgeImage, TextureType.Edge); int edgeSubmesh = chunk.materialIndices.IndexOf(materialStackIndex); bool addIndexOnSuccess = false; if (edgeSubmesh == -1) diff --git a/src/openrct2-unity/Assets/Scripts/OpenRCT2/TileElements/TileElement.cs b/src/openrct2-unity/Assets/Scripts/OpenRCT2/TileElements/TileElement.cs index ac49564d3a..88bad9f6a2 100644 --- a/src/openrct2-unity/Assets/Scripts/OpenRCT2/TileElements/TileElement.cs +++ b/src/openrct2-unity/Assets/Scripts/OpenRCT2/TileElements/TileElement.cs @@ -3,7 +3,7 @@ using System.Runtime.InteropServices; namespace Lib { - [StructLayout(LayoutKind.Sequential, Size = 16)] + [StructLayout(LayoutKind.Sequential)] public readonly struct TileElement : IEquatable { public readonly byte type; diff --git a/src/openrct2-unity/ProjectSettings/EditorBuildSettings.asset b/src/openrct2-unity/ProjectSettings/EditorBuildSettings.asset index 2a4cbc567e..9bd6d101e2 100644 --- a/src/openrct2-unity/ProjectSettings/EditorBuildSettings.asset +++ b/src/openrct2-unity/ProjectSettings/EditorBuildSettings.asset @@ -5,7 +5,7 @@ EditorBuildSettings: m_ObjectHideFlags: 0 serializedVersion: 2 m_Scenes: - - enabled: 1 - path: Assets/Scenes/SampleScene.unity - guid: d1c3109bdb54ad54c8a2b2838528e640 + - enabled: 0 + path: + guid: 00000000000000000000000000000000 m_configObjects: {}