Tweaked readability of a few scripts.

This commit is contained in:
Bas
2020-11-13 13:48:17 +01:00
parent 78d8f9bf3a
commit 0ebfc36c65
6 changed files with 60 additions and 31 deletions

View File

@@ -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;
}

View File

@@ -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<TerrainSurfaceObject*>(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<TerrainEdgeObject*>(obj);
return tobj->BaseImageId + 5; // EDGE_BOTTOMRIGHT = +5

View File

@@ -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<RequestedImage> _images;
@@ -26,7 +29,7 @@ namespace Generation.Retro
/// <summary>
/// Pushes a image index to the materials stack and returns its list index.
/// </summary>
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;

View File

@@ -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)

View File

@@ -3,7 +3,7 @@ using System.Runtime.InteropServices;
namespace Lib
{
[StructLayout(LayoutKind.Sequential, Size = 16)]
[StructLayout(LayoutKind.Sequential)]
public readonly struct TileElement : IEquatable<TileElement>
{
public readonly byte type;

View File

@@ -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: {}