Skip to content

Commit

Permalink
LostArtefacts#219 TR3 Boxes, overlaps and zones
Browse files Browse the repository at this point in the history
Some tidying/refactoring for finding room sectors.
  • Loading branch information
lahm86 committed Nov 4, 2021
1 parent 3d213fc commit cc220aa
Showing 1 changed file with 8 additions and 45 deletions.
53 changes: 8 additions & 45 deletions TRFDControl/Utilities/FDUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ public static void RemoveEntityTriggers(IEnumerable<TRRoomSector> sectorList, in
public static readonly short NO_ROOM = 0xff;
public static readonly short WALL_SHIFT = 10;

// See Control.c line 516
public static TRRoomSector GetRoomSector(int x, int y, int z, short roomNumber, TR2Level level, FDControl floorData)
{
int xFloor, yFloor;
Expand All @@ -140,11 +139,10 @@ public static TRRoomSector GetRoomSector(int x, int y, int z, short roomNumber,

do
{
// Clip position to edge of room (that way doorways are detected)
// Clip position to edge of tile
xFloor = (z - room.Info.Z) >> WALL_SHIFT;
yFloor = (x - room.Info.X) >> WALL_SHIFT;

// Ensure we don't test the corner of a room's floor data, as this can cause problems when 4 rooms join at a point
if (xFloor <= 0)
{
xFloor = 0;
Expand Down Expand Up @@ -178,7 +176,6 @@ public static TRRoomSector GetRoomSector(int x, int y, int z, short roomNumber,
yFloor = room.NumXSectors - 1;
}

// If doorway, go through and retest, else move onto next stage
sector = room.SectorList[xFloor + yFloor * room.NumZSectors];
data = GetDoor(sector, floorData);
if (data != NO_ROOM && data >= 0 && data < level.Rooms.Length - 1)
Expand All @@ -188,7 +185,6 @@ public static TRRoomSector GetRoomSector(int x, int y, int z, short roomNumber,
}
while (data != NO_ROOM);

// If below floor and pit, go down
if (y >= (sector.Floor << 8))
{
do
Expand All @@ -205,7 +201,6 @@ public static TRRoomSector GetRoomSector(int x, int y, int z, short roomNumber,
}
else if (y < (sector.Ceiling << 8))
{
// If above ceiling and skylight, go up
do
{
if (sector.RoomAbove == NO_ROOM)
Expand All @@ -222,7 +217,6 @@ public static TRRoomSector GetRoomSector(int x, int y, int z, short roomNumber,
return sector;
}

// See Control.c line 766
public static TRRoomSector GetRoomSector(int x, int y, int z, short roomNumber, TR3Level level, FDControl floorData)
{
int xFloor, yFloor;
Expand All @@ -232,11 +226,10 @@ public static TRRoomSector GetRoomSector(int x, int y, int z, short roomNumber,

do
{
// Clip position to edge of room (that way doorways are detected)
// Clip position to edge of tile
xFloor = (z - room.Info.Z) >> WALL_SHIFT;
yFloor = (x - room.Info.X) >> WALL_SHIFT;

// Ensure we don't test the corner of a room's floor data, as this can cause problems when 4 rooms join at a point
if (xFloor <= 0)
{
xFloor = 0;
Expand Down Expand Up @@ -270,7 +263,6 @@ public static TRRoomSector GetRoomSector(int x, int y, int z, short roomNumber,
yFloor = room.NumXSectors - 1;
}

// If doorway, go through and retest, else move onto next stage
sector = room.Sectors[xFloor + yFloor * room.NumZSectors];
data = GetDoor(sector, floorData);
if (data != NO_ROOM && data >= 0 && data < level.Rooms.Length - 1)
Expand All @@ -280,7 +272,6 @@ public static TRRoomSector GetRoomSector(int x, int y, int z, short roomNumber,
}
while (data != NO_ROOM);

// If below floor and pit, go down
if (y >= (sector.Floor << 8))
{
do
Expand All @@ -290,8 +281,8 @@ public static TRRoomSector GetRoomSector(int x, int y, int z, short roomNumber,
return sector;
}

int triCheck = CheckNoColFloorTriangle(floorData, sector, x, z);
if (triCheck == 1) // If on collision side of a split quad then stop.
int triCheck = CheckFloorTriangle(floorData, sector, x, z);
if (triCheck == 1)
{
break;
}
Expand All @@ -307,16 +298,15 @@ public static TRRoomSector GetRoomSector(int x, int y, int z, short roomNumber,
}
else if (y < (sector.Ceiling << 8))
{
// If above ceiling and skylight, go up
do
{
if (sector.RoomAbove == NO_ROOM)
{
return sector;
}

int triCheck = CheckNoColCeilingTriangle(floorData, sector, x, z);
if (triCheck == 1) // If on collision side of a split quad then stop.
int triCheck = CheckCeilingTriangle(floorData, sector, x, z);
if (triCheck == 1)
{
break;
}
Expand All @@ -334,7 +324,6 @@ public static TRRoomSector GetRoomSector(int x, int y, int z, short roomNumber,
return sector;
}

// See Control.c line 1344
public static short GetDoor(TRRoomSector sector, FDControl floorData)
{
if (sector.FDIndex == 0)
Expand All @@ -354,26 +343,13 @@ public static short GetDoor(TRRoomSector sector, FDControl floorData)
return NO_ROOM;
}

private static int CheckNoColFloorTriangle(FDControl floorData, TRRoomSector sector, int x, int z)
private static int CheckFloorTriangle(FDControl floorData, TRRoomSector sector, int x, int z)
{
if (sector.FDIndex == 0)
{
return 0;
}

/*
* -------
* | /|
* | T / | Split type 1
* | / |
* | / B |
* |/ |
* -------
* NOCOLF1T == FloorTriangulationNWSE_SW
* NOCOLF1B == FloorTriangulationNWSE_NE
* NOCOLF2T == FloorTriangulationNESW_SW
* NOCOLF2B == FloorTriangulationNESW_NW
*/
if (floorData.Entries[sector.FDIndex].Find(e => e is TR3TriangulationEntry) is TR3TriangulationEntry triangulation)
{
FDFunctions func = (FDFunctions)triangulation.Setup.Value;
Expand Down Expand Up @@ -403,26 +379,13 @@ private static int CheckNoColFloorTriangle(FDControl floorData, TRRoomSector sec
return 0;
}

private static int CheckNoColCeilingTriangle(FDControl floorData, TRRoomSector sector, int x, int z)
private static int CheckCeilingTriangle(FDControl floorData, TRRoomSector sector, int x, int z)
{
if (sector.FDIndex == 0)
{
return 0;
}

/*
* -------
* | /|
* | T / | Split type 3
* | / |
* | / B |
* |/ |
* -------
* NOCOLC1T == CeilingTriangulationNW_SW
* NOCOLC1B == CeilingTriangulationNW_NE
* NOCOLC2T == CeilingTriangulationNE_NW
* NOCOLC2B == CeilingTriangulationNE_SE
*/
if (floorData.Entries[sector.FDIndex].Find(e => e is TR3TriangulationEntry) is TR3TriangulationEntry triangulation)
{
FDFunctions func = (FDFunctions)triangulation.Setup.Value;
Expand Down

0 comments on commit cc220aa

Please sign in to comment.