TombEngine/TR5Main/Game/floordata.cpp
2021-06-03 23:19:38 -04:00

688 lines
17 KiB
C++

#include "framework.h"
#include "trmath.h"
#include "floordata.h"
#include "room.h"
#include "level.h"
#include "setup.h"
using namespace T5M::Floordata;
int FLOOR_INFO::SectorPlane(int x, int z) const
{
const auto point = GetSectorPoint(x, z);
auto vector = Vector2(point.x, point.y);
const auto matrix = Matrix::CreateRotationZ(FloorCollision.SplitAngle);
Vector2::Transform(vector, matrix, vector);
return vector.x < 0 ? 0 : 1;
}
int FLOOR_INFO::SectorPlaneCeiling(int x, int z) const
{
const auto point = GetSectorPoint(x, z);
auto vector = Vector2(point.x, point.y);
const auto matrix = Matrix::CreateRotationZ(CeilingCollision.SplitAngle);
Vector2::Transform(vector, matrix, vector);
return vector.x < 0 ? 0 : 1;
}
std::optional<int> FLOOR_INFO::RoomBelow(int plane) const
{
const auto room = FloorCollision.Portals[plane];
return room != -1 ? std::optional{room} : std::nullopt;
}
std::optional<int> FLOOR_INFO::RoomBelow(int x, int z) const
{
return RoomBelow(SectorPlane(x, z));
}
std::optional<int> FLOOR_INFO::RoomBelow(int x, int z, int y) const
{
const auto floorHeight = FloorHeight(x, z);
const auto ceilingHeight = CeilingHeight(x, z);
for (const auto itemNumber : BridgeItem)
{
const auto& item = g_Level.Items[itemNumber];
const auto itemHeight = Objects[item.objectNumber].floor(itemNumber, x, y, z);
if (itemHeight && *itemHeight >= y && *itemHeight <= floorHeight && *itemHeight >= ceilingHeight)
return std::nullopt;
}
return RoomBelow(x, z);
}
std::optional<int> FLOOR_INFO::RoomAbove(int plane) const
{
const auto room = CeilingCollision.Portals[plane];
return room != -1 ? std::optional{room} : std::nullopt;
}
std::optional<int> FLOOR_INFO::RoomAbove(int x, int z) const
{
return RoomAbove(SectorPlane(x, z));
}
std::optional<int> FLOOR_INFO::RoomAbove(int x, int z, int y) const
{
const auto floorHeight = FloorHeight(x, z);
const auto ceilingHeight = CeilingHeight(x, z);
for (const auto itemNumber : BridgeItem)
{
const auto& item = g_Level.Items[itemNumber];
const auto itemHeight = Objects[item.objectNumber].ceiling(itemNumber, x, y, z);
if (itemHeight && *itemHeight <= y && *itemHeight <= floorHeight && *itemHeight >= ceilingHeight)
return std::nullopt;
}
return RoomAbove(x, z);
}
std::optional<int> FLOOR_INFO::RoomSide() const
{
return WallPortal != -1 ? std::optional{WallPortal} : std::nullopt;
}
int FLOOR_INFO::FloorHeight(int x, int z) const
{
const auto plane = SectorPlane(x, z);
const auto vector = GetSectorPoint(x, z);
return FloorCollision.Planes[plane].x * vector.x + FloorCollision.Planes[plane].y * vector.y + FloorCollision.Planes[plane].z;
}
int FLOOR_INFO::FloorHeight(int x, int z, int y) const
{
auto height = FloorHeight(x, z);
const auto ceilingHeight = CeilingHeight(x, z);
for (const auto itemNumber : BridgeItem)
{
const auto& item = g_Level.Items[itemNumber];
const auto itemHeight = Objects[item.objectNumber].floor(itemNumber, x, y, z);
if (itemHeight && *itemHeight >= y && *itemHeight < height && *itemHeight >= ceilingHeight)
height = *itemHeight;
}
return height;
}
int FLOOR_INFO::BridgeFloorHeight(int x, int z, int y) const
{
for (const auto itemNumber : BridgeItem)
{
const auto& item = g_Level.Items[itemNumber];
const auto floorHeight = Objects[item.objectNumber].floor(itemNumber, x, y, z);
const auto ceilingHeight = Objects[item.objectNumber].ceiling(itemNumber, x, y, z);
if (floorHeight && ceilingHeight && y > *floorHeight && y <= *ceilingHeight)
return *floorHeight;
}
return FloorHeight(x, z, y);
}
int FLOOR_INFO::CeilingHeight(int x, int z) const
{
const auto plane = SectorPlaneCeiling(x, z);
const auto vector = GetSectorPoint(x, z);
return CeilingCollision.Planes[plane].x * vector.x + CeilingCollision.Planes[plane].y * vector.y + CeilingCollision.Planes[plane].z;
}
int FLOOR_INFO::CeilingHeight(int x, int z, int y) const
{
auto height = CeilingHeight(x, z);
const auto floorHeight = FloorHeight(x, z);
for (const auto itemNumber : BridgeItem)
{
const auto& item = g_Level.Items[itemNumber];
const auto itemHeight = Objects[item.objectNumber].ceiling(itemNumber, x, y, z);
if (itemHeight && *itemHeight <= y && *itemHeight > height && *itemHeight <= floorHeight)
height = *itemHeight;
}
return height;
}
int FLOOR_INFO::BridgeCeilingHeight(int x, int z, int y) const
{
for (const auto itemNumber : BridgeItem)
{
const auto& item = g_Level.Items[itemNumber];
const auto floorHeight = Objects[item.objectNumber].floor(itemNumber, x, y, z);
const auto ceilingHeight = Objects[item.objectNumber].ceiling(itemNumber, x, y, z);
if (floorHeight && ceilingHeight && y >= *floorHeight && y < *ceilingHeight)
return *ceilingHeight;
}
return CeilingHeight(x, z, y);
}
Vector2 FLOOR_INFO::FloorSlope(int plane) const
{
return Vector2{FloorCollision.Planes[plane].x, FloorCollision.Planes[plane].y};
}
Vector2 FLOOR_INFO::FloorSlope(int x, int z) const
{
return FloorSlope(SectorPlane(x, z));
}
Vector2 FLOOR_INFO::CeilingSlope(int plane) const
{
return Vector2{CeilingCollision.Planes[plane].x, CeilingCollision.Planes[plane].y};
}
Vector2 FLOOR_INFO::CeilingSlope(int x, int z) const
{
return CeilingSlope(SectorPlane(x, z));
}
bool FLOOR_INFO::IsWall(int plane) const
{
return FloorCollision.SplitAngle == CeilingCollision.SplitAngle && FloorCollision.Planes[plane] == CeilingCollision.Planes[plane];
}
bool FLOOR_INFO::IsWall(int x, int z) const
{
return IsWall(SectorPlane(x, z));
}
bool FLOOR_INFO::InsideBridge(int x, int z, int y, bool floorBorder, bool ceilingBorder) const
{
for (const auto itemNumber : BridgeItem)
{
const auto& item = g_Level.Items[itemNumber];
const auto floorHeight = Objects[item.objectNumber].floor(itemNumber, x, y, z);
const auto ceilingHeight = Objects[item.objectNumber].ceiling(itemNumber, x, y, z);
if (floorHeight && ceilingHeight && (y > *floorHeight && y < *ceilingHeight || floorBorder && y == *floorHeight || ceilingBorder && y == *ceilingHeight))
return true;
}
return false;
}
void FLOOR_INFO::AddItem(short itemNumber)
{
BridgeItem.insert(itemNumber);
}
void FLOOR_INFO::RemoveItem(short itemNumber)
{
BridgeItem.erase(itemNumber);
}
namespace T5M::Floordata
{
VectorInt2 GetSectorPoint(int x, int z)
{
const auto xPoint = x % SECTOR(1) - SECTOR(1) / 2;
const auto yPoint = z % SECTOR(1) - SECTOR(1) / 2;
return VectorInt2{xPoint, yPoint};
}
VectorInt2 GetRoomPosition(int roomNumber, int x, int z)
{
const auto& room = g_Level.Rooms[roomNumber];
const auto xRoom = (z - room.z) / SECTOR(1);
const auto yRoom = (x - room.x) / SECTOR(1);
auto pos = VectorInt2{xRoom, yRoom};
if (pos.x < 0)
{
pos.x = 0;
}
else if (pos.x > room.xSize - 1)
{
pos.x = room.xSize - 1;
}
if (pos.y < 0)
{
pos.y = 0;
}
else if (pos.y > room.ySize - 1)
{
pos.y = room.ySize - 1;
}
return pos;
}
FLOOR_INFO& GetFloor(int roomNumber, const VectorInt2& pos)
{
auto& room = g_Level.Rooms[roomNumber];
return room.floor[room.xSize * pos.y + pos.x];
}
FLOOR_INFO& GetFloor(int roomNumber, int x, int z)
{
return GetFloor(roomNumber, GetRoomPosition(roomNumber, x, z));
}
FLOOR_INFO& GetFloorSide(int roomNumber, int x, int z, int* sideRoomNumber)
{
auto floor = &GetFloor(roomNumber, x, z);
auto roomSide = floor->RoomSide();
while (roomSide)
{
roomNumber = *roomSide;
floor = &GetFloor(roomNumber, x, z);
roomSide = floor->RoomSide();
}
if (sideRoomNumber)
*sideRoomNumber = roomNumber;
return *floor;
}
FLOOR_INFO& GetBottomFloor(int roomNumber, int x, int z, int* bottomRoomNumber)
{
auto floor = &GetFloorSide(roomNumber, x, z, bottomRoomNumber);
auto wall = floor->IsWall(x, z);
while (wall)
{
const auto roomBelow = floor->RoomBelow(x, z);
if (!roomBelow)
break;
floor = &GetFloorSide(*roomBelow, x, z, bottomRoomNumber);
wall = floor->IsWall(x, z);
}
return *floor;
}
FLOOR_INFO& GetTopFloor(int roomNumber, int x, int z, int* topRoomNumber)
{
auto floor = &GetFloorSide(roomNumber, x, z, topRoomNumber);
auto wall = floor->IsWall(x, z);
while (wall)
{
const auto roomAbove = floor->RoomAbove(x, z);
if (!roomAbove)
break;
floor = &GetFloorSide(*roomAbove, x, z, topRoomNumber);
wall = floor->IsWall(x, z);
}
return *floor;
}
std::optional<int> GetTopHeight(FLOOR_INFO& startFloor, int x, int z, int y, int* topRoomNumber, FLOOR_INFO** topFloor)
{
auto floor = &startFloor;
int roomNumber;
if (topRoomNumber)
roomNumber = *topRoomNumber;
do
{
y = floor->BridgeFloorHeight(x, z, y);
while (y <= floor->CeilingHeight(x, z))
{
const auto roomAbove = floor->RoomAbove(x, z);
if (!roomAbove)
return std::nullopt;
floor = &GetFloorSide(*roomAbove, x, z, &roomNumber);
}
}
while (floor->InsideBridge(x, z, y, false, true));
if (topRoomNumber)
*topRoomNumber = roomNumber;
if (topFloor)
*topFloor = floor;
return std::optional{y};
}
std::optional<int> GetBottomHeight(FLOOR_INFO& startFloor, int x, int z, int y, int* bottomRoomNumber, FLOOR_INFO** bottomFloor)
{
auto floor = &startFloor;
int roomNumber;
if (bottomRoomNumber)
roomNumber = *bottomRoomNumber;
do
{
y = floor->BridgeCeilingHeight(x, z, y);
while (y >= floor->FloorHeight(x, z))
{
const auto roomBelow = floor->RoomBelow(x, z);
if (!roomBelow)
return std::nullopt;
floor = &GetFloorSide(*roomBelow, x, z, &roomNumber);
}
}
while (floor->InsideBridge(x, z, y, true, false));
if (bottomRoomNumber)
*bottomRoomNumber = roomNumber;
if (bottomFloor)
*bottomFloor = floor;
return std::optional{y};
}
std::optional<int> GetFloorHeight(const ROOM_VECTOR& location, int x, int z)
{
auto floor = &GetFloorSide(location.roomNumber, x, z);
auto y = location.yNumber;
if (floor->IsWall(x, z))
{
floor = &GetTopFloor(location.roomNumber, x, z);
if (!floor->IsWall(x, z))
{
y = floor->FloorHeight(x, z);
}
else
{
floor = &GetBottomFloor(location.roomNumber, x, z);
if (!floor->IsWall(x, z))
{
y = floor->CeilingHeight(x, z);
}
else
{
return std::nullopt;
}
}
}
const auto floorHeight = floor->FloorHeight(x, z, y);
const auto ceilingHeight = floor->CeilingHeight(x, z, y);
y = std::clamp(y, ceilingHeight, floorHeight);
if (floor->InsideBridge(x, z, y, y == ceilingHeight, y == floorHeight))
{
auto height = GetTopHeight(*floor, x, z, y);
if (height)
return height;
height = GetBottomHeight(*floor, x, z, y, nullptr, &floor);
if (!height)
return std::nullopt;
y = *height;
}
auto roomBelow = floor->RoomBelow(x, z, y);
while (roomBelow)
{
floor = &GetFloorSide(*roomBelow, x, z);
roomBelow = floor->RoomBelow(x, z, y);
}
return std::optional{floor->FloorHeight(x, z, y)};
}
std::optional<int> GetCeilingHeight(const ROOM_VECTOR& location, int x, int z)
{
auto floor = &GetFloorSide(location.roomNumber, x, z);
auto y = location.yNumber;
if (floor->IsWall(x, z))
{
floor = &GetBottomFloor(location.roomNumber, x, z);
if (!floor->IsWall(x, z))
{
y = floor->CeilingHeight(x, z);
}
else
{
floor = &GetTopFloor(location.roomNumber, x, z);
if (!floor->IsWall(x, z))
{
y = floor->FloorHeight(x, z);
}
else
{
return std::nullopt;
}
}
}
const auto floorHeight = floor->FloorHeight(x, z, y);
const auto ceilingHeight = floor->CeilingHeight(x, z, y);
y = std::clamp(y, ceilingHeight, floorHeight);
if (floor->InsideBridge(x, z, y, y == ceilingHeight, y == floorHeight))
{
auto height = GetBottomHeight(*floor, x, z, y);
if (height)
return height;
height = GetTopHeight(*floor, x, z, y, nullptr, &floor);
if (!height)
return std::nullopt;
y = *height;
}
auto roomAbove = floor->RoomAbove(x, z, y);
while (roomAbove)
{
floor = &GetFloorSide(*roomAbove, x, z);
roomAbove = floor->RoomAbove(x, z, y);
}
return std::optional{floor->CeilingHeight(x, z, y)};
}
std::optional<ROOM_VECTOR> GetBottomRoom(ROOM_VECTOR location, int x, int y, int z)
{
auto floor = &GetFloorSide(location.roomNumber, x, z, &location.roomNumber);
if (floor->IsWall(x, z))
{
floor = &GetBottomFloor(location.roomNumber, x, z, &location.roomNumber);
if (floor->IsWall(x, z))
return std::nullopt;
location.yNumber = floor->CeilingHeight(x, z);
}
auto floorHeight = floor->FloorHeight(x, z, location.yNumber);
auto ceilingHeight = floor->CeilingHeight(x, z, location.yNumber);
location.yNumber = std::clamp(location.yNumber, ceilingHeight, floorHeight);
if (floor->InsideBridge(x, z, location.yNumber, location.yNumber == ceilingHeight, location.yNumber == floorHeight))
{
const auto height = GetBottomHeight(*floor, x, z, location.yNumber, &location.roomNumber, &floor);
if (!height)
return std::nullopt;
location.yNumber = *height;
}
floorHeight = floor->FloorHeight(x, z, location.yNumber);
ceilingHeight = floor->CeilingHeight(x, z, location.yNumber);
if (y < ceilingHeight)
return std::nullopt;
if (y <= floorHeight && y >= ceilingHeight)
{
location.yNumber = y;
return std::optional{location};
}
auto roomBelow = floor->RoomBelow(x, z, location.yNumber);
while (roomBelow)
{
floor = &GetFloorSide(*roomBelow, x, z, &location.roomNumber);
location.yNumber = floor->CeilingHeight(x, z);
floorHeight = floor->FloorHeight(x, z, location.yNumber);
ceilingHeight = floor->CeilingHeight(x, z, location.yNumber);
if (y < ceilingHeight)
return std::nullopt;
if (y <= floorHeight && y >= ceilingHeight)
{
location.yNumber = y;
return std::optional{location};
}
roomBelow = floor->RoomBelow(x, z, location.yNumber);
}
return std::nullopt;
}
std::optional<ROOM_VECTOR> GetTopRoom(ROOM_VECTOR location, int x, int y, int z)
{
auto floor = &GetFloorSide(location.roomNumber, x, z, &location.roomNumber);
if (floor->IsWall(x, z))
{
floor = &GetTopFloor(location.roomNumber, x, z, &location.roomNumber);
if (floor->IsWall(x, z))
return std::nullopt;
location.yNumber = floor->FloorHeight(x, z);
}
auto floorHeight = floor->FloorHeight(x, z, location.yNumber);
auto ceilingHeight = floor->CeilingHeight(x, z, location.yNumber);
location.yNumber = std::clamp(location.yNumber, ceilingHeight, floorHeight);
if (floor->InsideBridge(x, z, location.yNumber, location.yNumber == ceilingHeight, location.yNumber == floorHeight))
{
const auto height = GetTopHeight(*floor, x, z, location.yNumber, &location.roomNumber, &floor);
if (!height)
return std::nullopt;
location.yNumber = *height;
}
floorHeight = floor->FloorHeight(x, z, location.yNumber);
ceilingHeight = floor->CeilingHeight(x, z, location.yNumber);
if (y > floorHeight)
return std::nullopt;
if (y <= floorHeight && y >= ceilingHeight)
{
location.yNumber = y;
return std::optional{location};
}
auto roomAbove = floor->RoomAbove(x, z, location.yNumber);
while (roomAbove)
{
floor = &GetFloorSide(*roomAbove, x, z, &location.roomNumber);
location.yNumber = floor->FloorHeight(x, z);
floorHeight = floor->FloorHeight(x, z, location.yNumber);
ceilingHeight = floor->CeilingHeight(x, z, location.yNumber);
if (y > floorHeight)
return std::nullopt;
if (y <= floorHeight && y >= ceilingHeight)
{
location.yNumber = y;
return std::optional{location};
}
roomAbove = floor->RoomAbove(x, z, location.yNumber);
}
return std::nullopt;
}
ROOM_VECTOR GetRoom(ROOM_VECTOR location, int x, int y, int z)
{
const auto locationBelow = GetBottomRoom(location, x, y, z);
if (locationBelow)
return *locationBelow;
const auto locationAbove = GetTopRoom(location, x, y, z);
if (locationAbove)
return *locationAbove;
return location;
}
void AddBridge(short itemNumber)
{
const auto& item = g_Level.Items[itemNumber];
const auto x = item.pos.xPos;
const auto z = item.pos.zPos;
auto floor = &GetFloorSide(item.roomNumber, x, z);
floor->AddItem(itemNumber);
const auto floorBorder = Objects[item.objectNumber].floorBorder(itemNumber);
while (floorBorder <= floor->CeilingHeight(x, z))
{
const auto roomAbove = floor->RoomAbove(x, z);
if (!roomAbove)
break;
floor = &GetFloorSide(*roomAbove, x, z);
floor->AddItem(itemNumber);
}
const auto ceilingBorder = Objects[item.objectNumber].ceilingBorder(itemNumber);
while (ceilingBorder >= floor->FloorHeight(x, z))
{
const auto roomBelow = floor->RoomBelow(x, z);
if (!roomBelow)
break;
floor = &GetFloorSide(*roomBelow, x, z);
floor->AddItem(itemNumber);
}
}
void RemoveBridge(short itemNumber)
{
const auto& item = g_Level.Items[itemNumber];
const auto x = item.pos.xPos;
const auto z = item.pos.zPos;
auto floor = &GetFloorSide(item.roomNumber, x, z);
floor->RemoveItem(itemNumber);
const auto floorBorder = Objects[item.objectNumber].floorBorder(itemNumber);
while (floorBorder <= floor->CeilingHeight(x, z))
{
const auto roomAbove = floor->RoomAbove(x, z);
if (!roomAbove)
break;
floor = &GetFloorSide(*roomAbove, x, z);
floor->RemoveItem(itemNumber);
}
const auto ceilingBorder = Objects[item.objectNumber].ceilingBorder(itemNumber);
while (ceilingBorder >= floor->FloorHeight(x, z))
{
const auto roomBelow = floor->RoomBelow(x, z);
if (!roomBelow)
break;
floor = &GetFloorSide(*roomBelow, x, z);
floor->RemoveItem(itemNumber);
}
}
}