Skip to content

Commit

Permalink
rooms: introduce utility sector getters
Browse files Browse the repository at this point in the history
This provides a function to get a room's sector by world X/Z pos, and
another by X/Z index.
  • Loading branch information
lahm86 committed Feb 5, 2025
1 parent 4796788 commit aee8a0f
Show file tree
Hide file tree
Showing 22 changed files with 76 additions and 129 deletions.
14 changes: 14 additions & 0 deletions src/libtrx/game/rooms/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,17 @@ BOUNDS_32 Room_GetWorldBounds(void)

return bounds;
}

SECTOR *Room_GetWorldSector(
const ROOM *const room, const int32_t x_pos, const int32_t z_pos)
{
const int32_t x_sector = (x_pos - room->pos.x) >> WALL_SHIFT;
const int32_t z_sector = (z_pos - room->pos.z) >> WALL_SHIFT;
return Room_GetUnitSector(room, x_sector, z_sector);
}

SECTOR *Room_GetUnitSector(
const ROOM *const room, const int32_t x_sector, const int32_t z_sector)
{
return &room->sectors[z_sector + x_sector * room->size.z];
}
4 changes: 4 additions & 0 deletions src/libtrx/include/libtrx/game/rooms/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ void Room_PopulateSectorData(
int16_t Room_GetIndexFromPos(int32_t x, int32_t y, int32_t z);
int32_t Room_FindByPos(int32_t x, int32_t y, int32_t z);
BOUNDS_32 Room_GetWorldBounds(void);

SECTOR *Room_GetWorldSector(const ROOM *room, int32_t x_pos, int32_t z_pos);
SECTOR *Room_GetUnitSector(
const ROOM *room, int32_t x_sector, int32_t z_sector);
15 changes: 6 additions & 9 deletions src/tr1/game/camera/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -412,15 +412,14 @@ static void M_SmartShift(
int32_t z_sector = (g_Camera.target.z - room->pos.z) >> WALL_SHIFT;
int32_t x_sector = (g_Camera.target.x - room->pos.x) >> WALL_SHIFT;

const int16_t item_box =
room->sectors[z_sector + x_sector * room->size.z].box;
const int16_t item_box = Room_GetUnitSector(room, x_sector, z_sector)->box;
BOX_INFO *box = &g_Boxes[item_box];

room = Room_Get(ideal->room_num);
z_sector = (ideal->z - room->pos.z) >> WALL_SHIFT;
x_sector = (ideal->x - room->pos.x) >> WALL_SHIFT;

int16_t camera_box = room->sectors[z_sector + x_sector * room->size.z].box;
int16_t camera_box = Room_GetUnitSector(room, x_sector, z_sector)->box;
if (camera_box != NO_BOX
&& (ideal->z < box->left || ideal->z > box->right || ideal->x < box->top
|| ideal->x > box->bottom)) {
Expand All @@ -436,7 +435,7 @@ static void M_SmartShift(
const bool bad_left =
M_BadPosition(ideal->x, ideal->y, test, ideal->room_num);
if (!bad_left) {
camera_box = room->sectors[z_sector - 1 + x_sector * room->size.z].box;
camera_box = Room_GetUnitSector(room, x_sector, z_sector - 1)->box;
if (camera_box != NO_ITEM && g_Boxes[camera_box].left < left) {
left = g_Boxes[camera_box].left;
}
Expand All @@ -446,7 +445,7 @@ static void M_SmartShift(
const bool bad_right =
M_BadPosition(ideal->x, ideal->y, test, ideal->room_num);
if (!bad_right) {
camera_box = room->sectors[z_sector + 1 + x_sector * room->size.z].box;
camera_box = Room_GetUnitSector(room, x_sector, z_sector + 1)->box;
if (camera_box != NO_ITEM && g_Boxes[camera_box].right > right) {
right = g_Boxes[camera_box].right;
}
Expand All @@ -456,8 +455,7 @@ static void M_SmartShift(
const bool bad_top =
M_BadPosition(test, ideal->y, ideal->z, ideal->room_num);
if (!bad_top) {
camera_box =
room->sectors[z_sector + (x_sector - 1) * room->size.z].box;
camera_box = Room_GetUnitSector(room, x_sector - 1, z_sector)->box;
if (camera_box != NO_ITEM && g_Boxes[camera_box].top < top) {
top = g_Boxes[camera_box].top;
}
Expand All @@ -467,8 +465,7 @@ static void M_SmartShift(
const bool bad_bottom =
M_BadPosition(test, ideal->y, ideal->z, ideal->room_num);
if (!bad_bottom) {
camera_box =
room->sectors[z_sector + (x_sector + 1) * room->size.z].box;
camera_box = Room_GetUnitSector(room, x_sector + 1, z_sector)->box;
if (camera_box != NO_ITEM && g_Boxes[camera_box].bottom > bottom) {
bottom = g_Boxes[camera_box].bottom;
}
Expand Down
9 changes: 3 additions & 6 deletions src/tr1/game/creature.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,12 @@ void Creature_AIInfo(ITEM *item, AI_INFO *info)
}

const ROOM *room = Room_Get(item->room_num);
int32_t z_sector = (item->pos.z - room->pos.z) >> WALL_SHIFT;
int32_t x_sector = (item->pos.x - room->pos.x) >> WALL_SHIFT;
item->box_num = room->sectors[z_sector + x_sector * room->size.z].box;
item->box_num = Room_GetWorldSector(room, item->pos.x, item->pos.z)->box;
info->zone_num = zone[item->box_num];

room = Room_Get(g_LaraItem->room_num);
z_sector = (g_LaraItem->pos.z - room->pos.z) >> WALL_SHIFT;
x_sector = (g_LaraItem->pos.x - room->pos.x) >> WALL_SHIFT;
g_LaraItem->box_num = room->sectors[z_sector + x_sector * room->size.z].box;
g_LaraItem->box_num =
Room_GetWorldSector(room, g_LaraItem->pos.x, g_LaraItem->pos.z)->box;
info->enemy_zone = zone[g_LaraItem->box_num];

if (g_Boxes[g_LaraItem->box_num].overlap_index & creature->lot.block_mask) {
Expand Down
2 changes: 1 addition & 1 deletion src/tr1/game/inject.c
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,7 @@ static void M_FloorDataEdits(INJECTION *injection, LEVEL_INFO *level_info)
LOG_WARNING(
"Sector [%d,%d] is invalid for room %d", x, z, room_num);
} else {
sector = &room->sectors[room->size.z * x + z];
sector = Room_GetUnitSector(room, x, z);
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/tr1/game/items.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,8 @@ void Item_Initialise(int16_t item_num)
ROOM *const room = Room_Get(item->room_num);
item->next_item = room->item_num;
room->item_num = item_num;
const int32_t z_sector = (item->pos.z - room->pos.z) >> WALL_SHIFT;
const int32_t x_sector = (item->pos.x - room->pos.x) >> WALL_SHIFT;
const SECTOR *const sector =
&room->sectors[z_sector + x_sector * room->size.z];
Room_GetWorldSector(room, item->pos.x, item->pos.z);
item->floor = sector->floor.height;

if (g_GameInfo.bonus_flag & GBF_NGPLUS) {
Expand Down
4 changes: 1 addition & 3 deletions src/tr1/game/lara/control.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ static void M_WaterCurrent(COLL_INFO *coll)
ITEM *const item = g_LaraItem;
const ROOM *const room = Room_Get(item->room_num);
const SECTOR *const sector =
&room->sectors
[((item->pos.z - room->pos.z) >> WALL_SHIFT)
+ ((item->pos.x - room->pos.x) >> WALL_SHIFT) * room->size.z];
Room_GetWorldSector(room, item->pos.x, item->pos.z);
item->box_num = sector->box;

if (Box_CalculateTarget(&target, item, &g_Lara.lot) == TARGET_NONE) {
Expand Down
10 changes: 3 additions & 7 deletions src/tr1/game/lara/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ int32_t Lara_GetWaterDepth(
x_sector = room->size.x - 1;
}

sector = &room->sectors[z_sector + x_sector * room->size.z];
sector = Room_GetUnitSector(room, x_sector, z_sector);
if (sector->portal_room.wall == NO_ROOM) {
break;
}
Expand All @@ -672,9 +672,7 @@ int32_t Lara_GetWaterDepth(
sector = Room_GetSector(x, y, z, &room_num);
return Room_GetHeight(sector, x, y, z) - water_height;
}
const int32_t z_sector = (z - room->pos.z) >> WALL_SHIFT;
const int32_t x_sector = (x - room->pos.x) >> WALL_SHIFT;
sector = &room->sectors[z_sector + x_sector * room->size.z];
sector = Room_GetWorldSector(room, x, z);
}
return 0x7FFF;
}
Expand All @@ -686,9 +684,7 @@ int32_t Lara_GetWaterDepth(
sector = Room_GetSector(x, y, z, &room_num);
return Room_GetHeight(sector, x, y, z) - water_height;
}
const int32_t z_sector = (z - room->pos.z) >> WALL_SHIFT;
const int32_t x_sector = (x - room->pos.x) >> WALL_SHIFT;
sector = &room->sectors[z_sector + x_sector * room->size.z];
sector = Room_GetWorldSector(room, x, z);
}
return NO_HEIGHT;
}
Expand Down
4 changes: 1 addition & 3 deletions src/tr1/game/lot.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,7 @@ void LOT_CreateZone(ITEM *item)
}

const ROOM *const room = Room_Get(item->room_num);
const int32_t z_sector = (item->pos.z - room->pos.z) >> WALL_SHIFT;
const int32_t x_sector = (item->pos.x - room->pos.x) >> WALL_SHIFT;
item->box_num = room->sectors[z_sector + x_sector * room->size.z].box;
item->box_num = Room_GetWorldSector(room, item->pos.x, item->pos.z)->box;

int16_t zone_num = zone[item->box_num];
int16_t flip_num = flip[item->box_num];
Expand Down
2 changes: 1 addition & 1 deletion src/tr1/game/objects/general/door.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ static SECTOR *M_GetRoomRelSector(
.x = ((item->pos.x - room->pos.x) >> WALL_SHIFT) + sector_dx,
.z = ((item->pos.z - room->pos.z) >> WALL_SHIFT) + sector_dz,
};
return &room->sectors[sector.x * room->size.z + sector.z];
return Room_GetUnitSector(room, sector.x, sector.z);
}

static void M_Initialise(
Expand Down
2 changes: 1 addition & 1 deletion src/tr1/game/output.c
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ void Output_DrawRoomTriggers(const ROOM *const room)
S_Output_SetBlendingMode(GFX_BLEND_MODE_NORMAL);
for (int32_t z = 0; z < room->size.z; z++) {
for (int32_t x = 0; x < room->size.x; x++) {
const SECTOR *sector = &room->sectors[z + x * room->size.z];
const SECTOR *sector = Room_GetUnitSector(room, x, z);
if (sector->trigger == nullptr) {
continue;
}
Expand Down
32 changes: 9 additions & 23 deletions src/tr1/game/room.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,7 @@ SECTOR *Room_GetPitSector(
{
while (sector->portal_room.pit != NO_ROOM) {
const ROOM *const room = Room_Get(sector->portal_room.pit);
const int32_t z_sector = (z - room->pos.z) >> WALL_SHIFT;
const int32_t x_sector = (x - room->pos.x) >> WALL_SHIFT;
sector = &room->sectors[z_sector + x_sector * room->size.z];
sector = Room_GetWorldSector(room, x, z);
}

return (SECTOR *)sector;
Expand All @@ -243,9 +241,7 @@ static SECTOR *M_GetSkySector(
{
while (sector->portal_room.sky != NO_ROOM) {
const ROOM *const room = Room_Get(sector->portal_room.sky);
const int32_t z_sector = (z - room->pos.z) >> WALL_SHIFT;
const int32_t x_sector = (x - room->pos.x) >> WALL_SHIFT;
sector = &room->sectors[z_sector + x_sector * room->size.z];
sector = Room_GetWorldSector(room, x, z);
}

return (SECTOR *)sector;
Expand Down Expand Up @@ -280,7 +276,7 @@ SECTOR *Room_GetSector(int32_t x, int32_t y, int32_t z, int16_t *room_num)
x_sector = room->size.x - 1;
}

sector = &room->sectors[z_sector + x_sector * room->size.z];
sector = Room_GetUnitSector(room, x_sector, z_sector);
portal_room = sector->portal_room.wall;
if (portal_room != NO_ROOM) {
*room_num = portal_room;
Expand All @@ -295,11 +291,8 @@ SECTOR *Room_GetSector(int32_t x, int32_t y, int32_t z, int16_t *room_num)
}

*room_num = sector->portal_room.pit;

room = Room_Get(sector->portal_room.pit);
const int32_t z_sector = (z - room->pos.z) >> WALL_SHIFT;
const int32_t x_sector = (x - room->pos.x) >> WALL_SHIFT;
sector = &room->sectors[z_sector + x_sector * room->size.z];
sector = Room_GetWorldSector(room, x, z);
} while (y >= sector->floor.height);
} else if (y < sector->ceiling.height) {
do {
Expand All @@ -308,11 +301,8 @@ SECTOR *Room_GetSector(int32_t x, int32_t y, int32_t z, int16_t *room_num)
}

*room_num = sector->portal_room.sky;

room = Room_Get(sector->portal_room.sky);
const int32_t z_sector = (z - room->pos.z) >> WALL_SHIFT;
const int32_t x_sector = (x - room->pos.x) >> WALL_SHIFT;
sector = &room->sectors[z_sector + x_sector * room->size.z];
sector = Room_GetWorldSector(room, x, z);
} while (y < sector->ceiling.height);
}

Expand Down Expand Up @@ -472,7 +462,7 @@ int16_t Room_GetWaterHeight(int32_t x, int32_t y, int32_t z, int16_t room_num)
x_sector = room->size.x - 1;
}

sector = &room->sectors[z_sector + x_sector * room->size.z];
sector = Room_GetUnitSector(room, x_sector, z_sector);
portal_room = sector->portal_room.wall;
if (portal_room != NO_ROOM) {
room = Room_Get(portal_room);
Expand All @@ -485,9 +475,7 @@ int16_t Room_GetWaterHeight(int32_t x, int32_t y, int32_t z, int16_t room_num)
if (!(room->flags & RF_UNDERWATER)) {
break;
}
z_sector = (z - room->pos.z) >> WALL_SHIFT;
x_sector = (x - room->pos.x) >> WALL_SHIFT;
sector = &room->sectors[z_sector + x_sector * room->size.z];
sector = Room_GetWorldSector(room, x, z);
}
return sector->ceiling.height;
} else {
Expand All @@ -496,9 +484,7 @@ int16_t Room_GetWaterHeight(int32_t x, int32_t y, int32_t z, int16_t room_num)
if (room->flags & RF_UNDERWATER) {
return sector->floor.height;
}
z_sector = (z - room->pos.z) >> WALL_SHIFT;
x_sector = (x - room->pos.x) >> WALL_SHIFT;
sector = &room->sectors[z_sector + x_sector * room->size.z];
sector = Room_GetWorldSector(room, x, z);
}
return NO_HEIGHT;
}
Expand Down Expand Up @@ -528,7 +514,7 @@ void Room_AlterFloorHeight(ITEM *item, int32_t height)
CLAMP(x_sector, 0, room->size.x - 1);
}

sector = &room->sectors[z_sector + x_sector * room->size.z];
sector = Room_GetUnitSector(room, x_sector, z_sector);
portal_room = sector->portal_room.wall;
if (portal_room != NO_ROOM) {
room = Room_Get(portal_room);
Expand Down
9 changes: 3 additions & 6 deletions src/tr2/game/camera.c
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,8 @@ void Camera_SmartShift(
LOS_Check(&g_Camera.target, target);

const ROOM *room = Room_Get(g_Camera.target.room_num);
int32_t z_sector = (g_Camera.target.z - room->pos.z) >> WALL_SHIFT;
int32_t x_sector = (g_Camera.target.x - room->pos.x) >> WALL_SHIFT;
int16_t item_box = room->sectors[z_sector + x_sector * room->size.z].box;
int16_t item_box =
Room_GetWorldSector(room, g_Camera.target.x, g_Camera.target.z)->box;
const BOX_INFO *box = &g_Boxes[item_box];

int32_t left = (int32_t)box->left << WALL_SHIFT;
Expand All @@ -302,9 +301,7 @@ void Camera_SmartShift(
int32_t bottom = ((int32_t)box->bottom << WALL_SHIFT) - 1;

room = Room_Get(target->room_num);
z_sector = (target->z - room->pos.z) >> WALL_SHIFT;
x_sector = (target->x - room->pos.x) >> WALL_SHIFT;
int16_t camera_box = room->sectors[z_sector + x_sector * room->size.z].box;
int16_t camera_box = Room_GetWorldSector(room, target->x, target->z)->box;

if (camera_box != NO_BOX
&& (target->z < left || target->z > right || target->x < top
Expand Down
10 changes: 4 additions & 6 deletions src/tr2/game/creature.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,15 @@ void Creature_AIInfo(ITEM *const item, AI_INFO *const info)

{
const ROOM *const room = Room_Get(item->room_num);
const int32_t z_sector = (item->pos.z - room->pos.z) >> WALL_SHIFT;
const int32_t x_sector = (item->pos.x - room->pos.x) >> WALL_SHIFT;
item->box_num = room->sectors[z_sector + x_sector * room->size.z].box;
item->box_num =
Room_GetWorldSector(room, item->pos.x, item->pos.z)->box;
info->zone_num = zone[item->box_num];
}

{
const ROOM *const room = Room_Get(enemy->room_num);
const int32_t z_sector = (enemy->pos.z - room->pos.z) >> WALL_SHIFT;
const int32_t x_sector = (enemy->pos.x - room->pos.x) >> WALL_SHIFT;
enemy->box_num = room->sectors[z_sector + x_sector * room->size.z].box;
enemy->box_num =
Room_GetWorldSector(room, enemy->pos.x, enemy->pos.z)->box;
info->enemy_zone_num = zone[enemy->box_num];
}

Expand Down
2 changes: 1 addition & 1 deletion src/tr2/game/inject.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ static void M_FloorDataEdits(
LOG_WARNING(
"Sector [%d,%d] is invalid for room %d", x, z, room_num);
} else {
sector = &room->sectors[room->size.z * x + z];
sector = Room_GetUnitSector(room, x, z);
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/tr2/game/items.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,8 @@ void Item_Initialise(const int16_t item_num)
item->next_item = room->item_num;
room->item_num = item_num;

const int32_t dx = (item->pos.x - room->pos.x) >> WALL_SHIFT;
const int32_t dz = (item->pos.z - room->pos.z) >> WALL_SHIFT;
const SECTOR *const sector = &room->sectors[dx * room->size.z + dz];
const SECTOR *const sector =
Room_GetWorldSector(room, item->pos.x, item->pos.z);
item->floor = sector->floor.height;

if (g_SaveGame.bonus_flag && GF_GetCurrentLevel()->type != GFL_DEMO) {
Expand Down
13 changes: 5 additions & 8 deletions src/tr2/game/lara/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1512,7 +1512,7 @@ int32_t Lara_GetWaterDepth(
x_sector = room->size.x - 1;
}

sector = &room->sectors[z_sector + x_sector * room->size.z];
sector = Room_GetUnitSector(room, x_sector, z_sector);
if (sector->portal_room.wall == NO_ROOM) {
break;
}
Expand All @@ -1528,9 +1528,7 @@ int32_t Lara_GetWaterDepth(
sector = Room_GetSector(x, y, z, &room_num);
return Room_GetHeight(sector, x, y, z) - water_height;
}
const int32_t z_sector = (z - room->pos.z) >> WALL_SHIFT;
const int32_t x_sector = (x - room->pos.x) >> WALL_SHIFT;
sector = &room->sectors[z_sector + x_sector * room->size.z];
sector = Room_GetWorldSector(room, x, z);
}
return 0x7FFF;
}
Expand All @@ -1542,9 +1540,7 @@ int32_t Lara_GetWaterDepth(
sector = Room_GetSector(x, y, z, &room_num);
return Room_GetHeight(sector, x, y, z) - water_height;
}
const int32_t z_sector = (z - room->pos.z) >> WALL_SHIFT;
const int32_t x_sector = (x - room->pos.x) >> WALL_SHIFT;
sector = &room->sectors[z_sector + x_sector * room->size.z];
sector = Room_GetWorldSector(room, x, z);
}
return NO_HEIGHT;
}
Expand Down Expand Up @@ -1651,7 +1647,8 @@ void Lara_WaterCurrent(COLL_INFO *const coll)
const ROOM *const room = Room_Get(g_LaraItem->room_num);
const int32_t z_sector = (g_LaraItem->pos.z - room->pos.z) >> WALL_SHIFT;
const int32_t x_sector = (g_LaraItem->pos.x - room->pos.x) >> WALL_SHIFT;
g_LaraItem->box_num = room->sectors[z_sector + x_sector * room->size.z].box;
g_LaraItem->box_num =
Room_GetWorldSector(room, g_LaraItem->pos.x, g_LaraItem->pos.z)->box;

if (g_Lara.creature == nullptr) {
g_Lara.current_active = 0;
Expand Down
Loading

0 comments on commit aee8a0f

Please sign in to comment.