From aee8a0f76842e2509972a570926c02d37cc32482 Mon Sep 17 00:00:00 2001 From: lahm86 <33758420+lahm86@users.noreply.github.com> Date: Wed, 5 Feb 2025 13:19:24 +0000 Subject: [PATCH] rooms: introduce utility sector getters This provides a function to get a room's sector by world X/Z pos, and another by X/Z index. --- src/libtrx/game/rooms/common.c | 14 ++++++++ src/libtrx/include/libtrx/game/rooms/common.h | 4 +++ src/tr1/game/camera/common.c | 15 ++++----- src/tr1/game/creature.c | 9 ++---- src/tr1/game/inject.c | 2 +- src/tr1/game/items.c | 4 +-- src/tr1/game/lara/control.c | 4 +-- src/tr1/game/lara/misc.c | 10 ++---- src/tr1/game/lot.c | 4 +-- src/tr1/game/objects/general/door.c | 2 +- src/tr1/game/output.c | 2 +- src/tr1/game/room.c | 32 ++++++------------- src/tr2/game/camera.c | 9 ++---- src/tr2/game/creature.c | 10 +++--- src/tr2/game/inject.c | 2 +- src/tr2/game/items.c | 5 ++- src/tr2/game/lara/misc.c | 13 +++----- src/tr2/game/lot.c | 4 +-- src/tr2/game/objects/creatures/diver.c | 18 ++--------- src/tr2/game/objects/general/door.c | 2 +- src/tr2/game/objects/general/window.c | 12 ++----- src/tr2/game/room.c | 28 +++++----------- 22 files changed, 76 insertions(+), 129 deletions(-) diff --git a/src/libtrx/game/rooms/common.c b/src/libtrx/game/rooms/common.c index 26fa316eb..f4f9e151c 100644 --- a/src/libtrx/game/rooms/common.c +++ b/src/libtrx/game/rooms/common.c @@ -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]; +} diff --git a/src/libtrx/include/libtrx/game/rooms/common.h b/src/libtrx/include/libtrx/game/rooms/common.h index 9aeccb959..555553f4a 100644 --- a/src/libtrx/include/libtrx/game/rooms/common.h +++ b/src/libtrx/include/libtrx/game/rooms/common.h @@ -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); diff --git a/src/tr1/game/camera/common.c b/src/tr1/game/camera/common.c index 0b065e103..be94a597a 100644 --- a/src/tr1/game/camera/common.c +++ b/src/tr1/game/camera/common.c @@ -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)) { @@ -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; } @@ -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; } @@ -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; } @@ -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; } diff --git a/src/tr1/game/creature.c b/src/tr1/game/creature.c index 7af10822f..90b99cb73 100644 --- a/src/tr1/game/creature.c +++ b/src/tr1/game/creature.c @@ -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) { diff --git a/src/tr1/game/inject.c b/src/tr1/game/inject.c index 9e62ec448..550925423 100644 --- a/src/tr1/game/inject.c +++ b/src/tr1/game/inject.c @@ -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); } } diff --git a/src/tr1/game/items.c b/src/tr1/game/items.c index 445471546..7a372c705 100644 --- a/src/tr1/game/items.c +++ b/src/tr1/game/items.c @@ -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) { diff --git a/src/tr1/game/lara/control.c b/src/tr1/game/lara/control.c index 3a566e982..816b56edf 100644 --- a/src/tr1/game/lara/control.c +++ b/src/tr1/game/lara/control.c @@ -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) { diff --git a/src/tr1/game/lara/misc.c b/src/tr1/game/lara/misc.c index 7c5acc730..cde6e900b 100644 --- a/src/tr1/game/lara/misc.c +++ b/src/tr1/game/lara/misc.c @@ -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; } @@ -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; } @@ -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; } diff --git a/src/tr1/game/lot.c b/src/tr1/game/lot.c index 75b7b3e61..9e46863d3 100644 --- a/src/tr1/game/lot.c +++ b/src/tr1/game/lot.c @@ -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]; diff --git a/src/tr1/game/objects/general/door.c b/src/tr1/game/objects/general/door.c index 42f63a7fd..c74096baf 100644 --- a/src/tr1/game/objects/general/door.c +++ b/src/tr1/game/objects/general/door.c @@ -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( diff --git a/src/tr1/game/output.c b/src/tr1/game/output.c index 435c74f9d..7e1fa4280 100644 --- a/src/tr1/game/output.c +++ b/src/tr1/game/output.c @@ -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; } diff --git a/src/tr1/game/room.c b/src/tr1/game/room.c index 237360953..9a8fea3a5 100644 --- a/src/tr1/game/room.c +++ b/src/tr1/game/room.c @@ -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; @@ -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; @@ -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; @@ -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 { @@ -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); } @@ -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); @@ -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 { @@ -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; } @@ -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); diff --git a/src/tr2/game/camera.c b/src/tr2/game/camera.c index f44cff3a7..c2fc51413 100644 --- a/src/tr2/game/camera.c +++ b/src/tr2/game/camera.c @@ -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; @@ -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 diff --git a/src/tr2/game/creature.c b/src/tr2/game/creature.c index 36c94b31f..b303dfe6c 100644 --- a/src/tr2/game/creature.c +++ b/src/tr2/game/creature.c @@ -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]; } diff --git a/src/tr2/game/inject.c b/src/tr2/game/inject.c index 11db839f6..13e8d95cf 100644 --- a/src/tr2/game/inject.c +++ b/src/tr2/game/inject.c @@ -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); } } diff --git a/src/tr2/game/items.c b/src/tr2/game/items.c index 6d570c543..6a445c182 100644 --- a/src/tr2/game/items.c +++ b/src/tr2/game/items.c @@ -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) { diff --git a/src/tr2/game/lara/misc.c b/src/tr2/game/lara/misc.c index e0780abd7..aa39a0b3b 100644 --- a/src/tr2/game/lara/misc.c +++ b/src/tr2/game/lara/misc.c @@ -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; } @@ -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; } @@ -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; } @@ -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; diff --git a/src/tr2/game/lot.c b/src/tr2/game/lot.c index a908f9b19..e3e70e58d 100644 --- a/src/tr2/game/lot.c +++ b/src/tr2/game/lot.c @@ -188,9 +188,7 @@ void LOT_CreateZone(ITEM *const 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]; diff --git a/src/tr2/game/objects/creatures/diver.c b/src/tr2/game/objects/creatures/diver.c index 493e649c3..38c0ea26a 100644 --- a/src/tr2/game/objects/creatures/diver.c +++ b/src/tr2/game/objects/creatures/diver.c @@ -38,23 +38,11 @@ static const BITE m_DiverBite = { .mesh_num = 18, }; -static const SECTOR *M_GetRelSector(const ROOM *room, int32_t x, int32_t z); - -static const SECTOR *M_GetRelSector( - const ROOM *const room, const int32_t x, const int32_t z) -{ - const XZ_32 sector_pos = { - .x = (x - room->pos.x) >> WALL_SHIFT, - .z = (z - room->pos.z) >> WALL_SHIFT, - }; - return &room->sectors[sector_pos.z + room->size.z * sector_pos.x]; -} - int32_t Diver_GetWaterSurface( const int32_t x, const int32_t y, const int32_t z, const int16_t room_num) { const ROOM *room = Room_Get(room_num); - const SECTOR *sector = M_GetRelSector(room, x, z); + const SECTOR *sector = Room_GetWorldSector(room, x, z); if ((room->flags & RF_UNDERWATER)) { while (sector->portal_room.sky != NO_ROOM) { @@ -62,7 +50,7 @@ int32_t Diver_GetWaterSurface( if (!(room->flags & RF_UNDERWATER)) { return sector->ceiling.height; } - sector = M_GetRelSector(room, x, z); + sector = Room_GetWorldSector(room, x, z); } } else { while (sector->portal_room.pit != NO_ROOM) { @@ -70,7 +58,7 @@ int32_t Diver_GetWaterSurface( if ((room->flags & RF_UNDERWATER)) { return sector->floor.height; } - sector = M_GetRelSector(room, x, z); + sector = Room_GetWorldSector(room, x, z); } } return NO_HEIGHT; diff --git a/src/tr2/game/objects/general/door.c b/src/tr2/game/objects/general/door.c index 30d270024..d8728e88a 100644 --- a/src/tr2/game/objects/general/door.c +++ b/src/tr2/game/objects/general/door.c @@ -32,7 +32,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( diff --git a/src/tr2/game/objects/general/window.c b/src/tr2/game/objects/general/window.c index 4ba12277a..968147f78 100644 --- a/src/tr2/game/objects/general/window.c +++ b/src/tr2/game/objects/general/window.c @@ -38,10 +38,8 @@ void Window_Initialise(const int16_t item_num) item->mesh_bits = 1; 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; const SECTOR *const sector = - &room->sectors[z_sector + x_sector * room->size.z]; + Room_GetWorldSector(room, item->pos.x, item->pos.z); BOX_INFO *const box = &g_Boxes[sector->box]; if (box->overlap_index & BOX_BLOCKABLE) { @@ -79,10 +77,8 @@ void Window_2_Control(const int16_t item_num) } 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; const SECTOR *const sector = - &room->sectors[z_sector + x_sector * room->size.z]; + Room_GetWorldSector(room, item->pos.x, item->pos.z); BOX_INFO *const box = &g_Boxes[sector->box]; if (box->overlap_index & BOX_BLOCKED) { @@ -103,10 +99,8 @@ void Window_Smash(const int16_t item_num) { ITEM *const item = Item_Get(item_num); 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; const SECTOR *const sector = - &room->sectors[z_sector + x_sector * room->size.z]; + Room_GetWorldSector(room, item->pos.x, item->pos.z); BOX_INFO *const box = &g_Boxes[sector->box]; if (box->overlap_index & BOX_BLOCKABLE) { diff --git a/src/tr2/game/room.c b/src/tr2/game/room.c index 131b32b16..491991fc7 100644 --- a/src/tr2/game/room.c +++ b/src/tr2/game/room.c @@ -494,9 +494,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; @@ -507,9 +505,7 @@ SECTOR *Room_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; @@ -545,7 +541,7 @@ SECTOR *Room_GetSector( 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; } @@ -558,9 +554,7 @@ SECTOR *Room_GetSector( while (sector->portal_room.pit != NO_ROOM) { *room_num = sector->portal_room.pit; const ROOM *const room = Room_Get(*room_num); - 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); if (y < sector->floor.height) { break; } @@ -569,9 +563,7 @@ SECTOR *Room_GetSector( while (sector->portal_room.sky != NO_ROOM) { *room_num = sector->portal_room.sky; 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); if (y >= sector->ceiling.height) { break; } @@ -612,7 +604,7 @@ int32_t Room_GetWaterHeight( x_sector = room->size.x - 1; } - sector = &room->sectors[z_sector + x_sector * room->size.z]; + sector = Room_GetUnitSector(room, x_sector, z_sector); room_num = sector->portal_room.wall; } while (room_num != NO_ROOM); @@ -622,9 +614,7 @@ int32_t Room_GetWaterHeight( if (!(room->flags & RF_UNDERWATER)) { break; } - 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->ceiling.height; } else { @@ -633,9 +623,7 @@ int32_t Room_GetWaterHeight( if (room->flags & RF_UNDERWATER) { return sector->floor.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; }