Skip to content

Commit

Permalink
libtrx: update rooms typing
Browse files Browse the repository at this point in the history
  • Loading branch information
rr- committed Sep 24, 2024
1 parent 3c2c4b0 commit 6b7150a
Show file tree
Hide file tree
Showing 16 changed files with 206 additions and 211 deletions.
20 changes: 10 additions & 10 deletions src/game/camera.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,17 @@ static void M_SmartShift(
LOS_Check(&g_Camera.target, ideal);

const ROOM *r = &g_RoomInfo[g_Camera.target.room_num];
int32_t z_sector = (g_Camera.target.z - r->z) >> WALL_SHIFT;
int32_t x_sector = (g_Camera.target.x - r->x) >> WALL_SHIFT;
int32_t z_sector = (g_Camera.target.z - r->pos.z) >> WALL_SHIFT;
int32_t x_sector = (g_Camera.target.x - r->pos.x) >> WALL_SHIFT;

const int16_t item_box = r->sectors[z_sector + x_sector * r->z_size].box;
const int16_t item_box = r->sectors[z_sector + x_sector * r->size.z].box;
BOX_INFO *box = &g_Boxes[item_box];

r = &g_RoomInfo[ideal->room_num];
z_sector = (ideal->z - r->z) >> WALL_SHIFT;
x_sector = (ideal->x - r->x) >> WALL_SHIFT;
z_sector = (ideal->z - r->pos.z) >> WALL_SHIFT;
x_sector = (ideal->x - r->pos.x) >> WALL_SHIFT;

int16_t camera_box = r->sectors[z_sector + x_sector * r->z_size].box;
int16_t camera_box = r->sectors[z_sector + x_sector * r->size.z].box;
if (camera_box != NO_BOX
&& (ideal->z < box->left || ideal->z > box->right || ideal->x < box->top
|| ideal->x > box->bottom)) {
Expand All @@ -129,7 +129,7 @@ static void M_SmartShift(
int32_t test = (ideal->z - WALL_L) | (WALL_L - 1);
bool bad_left = M_BadPosition(ideal->x, ideal->y, test, ideal->room_num);
if (!bad_left) {
camera_box = r->sectors[z_sector - 1 + x_sector * r->z_size].box;
camera_box = r->sectors[z_sector - 1 + x_sector * r->size.z].box;
if (camera_box != NO_ITEM && g_Boxes[camera_box].left < left) {
left = g_Boxes[camera_box].left;
}
Expand All @@ -138,7 +138,7 @@ static void M_SmartShift(
test = (ideal->z + WALL_L) & (~(WALL_L - 1));
bool bad_right = M_BadPosition(ideal->x, ideal->y, test, ideal->room_num);
if (!bad_right) {
camera_box = r->sectors[z_sector + 1 + x_sector * r->z_size].box;
camera_box = r->sectors[z_sector + 1 + x_sector * r->size.z].box;
if (camera_box != NO_ITEM && g_Boxes[camera_box].right > right) {
right = g_Boxes[camera_box].right;
}
Expand All @@ -147,7 +147,7 @@ static void M_SmartShift(
test = (ideal->x - WALL_L) | (WALL_L - 1);
bool bad_top = M_BadPosition(test, ideal->y, ideal->z, ideal->room_num);
if (!bad_top) {
camera_box = r->sectors[z_sector + (x_sector - 1) * r->z_size].box;
camera_box = r->sectors[z_sector + (x_sector - 1) * r->size.z].box;
if (camera_box != NO_ITEM && g_Boxes[camera_box].top < top) {
top = g_Boxes[camera_box].top;
}
Expand All @@ -156,7 +156,7 @@ static void M_SmartShift(
test = (ideal->x + WALL_L) & (~(WALL_L - 1));
bool bad_bottom = M_BadPosition(test, ideal->y, ideal->z, ideal->room_num);
if (!bad_bottom) {
camera_box = r->sectors[z_sector + (x_sector + 1) * r->z_size].box;
camera_box = r->sectors[z_sector + (x_sector + 1) * r->size.z].box;
if (camera_box != NO_ITEM && g_Boxes[camera_box].bottom > bottom) {
bottom = g_Boxes[camera_box].bottom;
}
Expand Down
2 changes: 1 addition & 1 deletion src/game/collide.c
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ bool Collide_CollideStaticObjects(
for (int i = 0; i < g_RoomsToDrawCount; i++) {
int16_t room_num = g_RoomsToDraw[i];
ROOM *r = &g_RoomInfo[room_num];
MESH *mesh = r->mesh;
MESH *mesh = r->meshes;

for (int j = 0; j < r->num_meshes; j++, mesh++) {
STATIC_INFO *sinfo = &g_StaticObjects[mesh->static_num];
Expand Down
12 changes: 6 additions & 6 deletions src/game/creature.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ void Creature_AIInfo(ITEM *item, AI_INFO *info)
}

const ROOM *r = &g_RoomInfo[item->room_num];
int32_t z_sector = (item->pos.z - r->z) >> WALL_SHIFT;
int32_t x_sector = (item->pos.x - r->x) >> WALL_SHIFT;
item->box_num = r->sectors[z_sector + x_sector * r->z_size].box;
int32_t z_sector = (item->pos.z - r->pos.z) >> WALL_SHIFT;
int32_t x_sector = (item->pos.x - r->pos.x) >> WALL_SHIFT;
item->box_num = r->sectors[z_sector + x_sector * r->size.z].box;
info->zone_num = zone[item->box_num];

r = &g_RoomInfo[g_LaraItem->room_num];
z_sector = (g_LaraItem->pos.z - r->z) >> WALL_SHIFT;
x_sector = (g_LaraItem->pos.x - r->x) >> WALL_SHIFT;
g_LaraItem->box_num = r->sectors[z_sector + x_sector * r->z_size].box;
z_sector = (g_LaraItem->pos.z - r->pos.z) >> WALL_SHIFT;
x_sector = (g_LaraItem->pos.x - r->pos.x) >> WALL_SHIFT;
g_LaraItem->box_num = r->sectors[z_sector + x_sector * r->size.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
8 changes: 4 additions & 4 deletions src/game/game/game_draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ void Game_DrawScene(bool draw_overlay)
for (int i = 0; i < g_RoomsToDrawCount; i++) {
int16_t room_num = g_RoomsToDraw[i];
ROOM *r = &g_RoomInfo[room_num];
r->top = 0;
r->left = 0;
r->right = Viewport_GetMaxX();
r->bottom = Viewport_GetMaxY();
r->bound_top = 0;
r->bound_left = 0;
r->bound_right = Viewport_GetMaxX();
r->bound_bottom = Viewport_GetMaxY();
Room_DrawSingleRoom(room_num);
}

Expand Down
10 changes: 5 additions & 5 deletions src/game/inject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1100,11 +1100,11 @@ static void M_FloorDataEdits(INJECTION *injection, LEVEL_INFO *level_info)
LOG_WARNING("Room index %d is invalid", room);
} else {
r = &g_RoomInfo[room];
if (x >= r->x_size || z >= r->z_size) {
if (x >= r->size.x || z >= r->size.z) {
LOG_WARNING(
"Sector [%d,%d] is invalid for room %d", x, z, room);
} else {
sector = &r->sectors[r->z_size * x + z];
sector = &r->sectors[r->size.z * x + z];
}
}

Expand Down Expand Up @@ -1216,8 +1216,8 @@ static void M_RoomShift(INJECTION *injection, int16_t room_num)
const int32_t y_shift = ROUND_TO_CLICK(VFile_ReadS32(fp));

ROOM *room = &g_RoomInfo[room_num];
room->x += x_shift;
room->z += z_shift;
room->pos.x += x_shift;
room->pos.z += z_shift;
room->min_floor += y_shift;
room->max_ceiling += y_shift;

Expand All @@ -1238,7 +1238,7 @@ static void M_RoomShift(INJECTION *injection, int16_t room_num)
}

// Update the sector floor and ceiling heights to match.
for (int32_t i = 0; i < room->z_size * room->x_size; i++) {
for (int32_t i = 0; i < room->size.z * room->size.x; i++) {
SECTOR *const sector = &room->sectors[i];
if (sector->floor.height == NO_HEIGHT
|| sector->ceiling.height == NO_HEIGHT) {
Expand Down
6 changes: 3 additions & 3 deletions src/game/items.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ void Item_Initialise(int16_t item_num)
ROOM *const r = &g_RoomInfo[item->room_num];
item->next_item = r->item_num;
r->item_num = item_num;
const int32_t z_sector = (item->pos.z - r->z) >> WALL_SHIFT;
const int32_t x_sector = (item->pos.x - r->x) >> WALL_SHIFT;
const SECTOR *const sector = &r->sectors[z_sector + x_sector * r->z_size];
const int32_t z_sector = (item->pos.z - r->pos.z) >> WALL_SHIFT;
const int32_t x_sector = (item->pos.x - r->pos.x) >> WALL_SHIFT;
const SECTOR *const sector = &r->sectors[z_sector + x_sector * r->size.z];
item->floor = sector->floor.height;

if (g_GameInfo.bonus_flag & GBF_NGPLUS) {
Expand Down
4 changes: 2 additions & 2 deletions src/game/lara/control.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ static void M_WaterCurrent(COLL_INFO *coll)
const ROOM *const r = &g_RoomInfo[item->room_num];
const SECTOR *const sector =
&r->sectors
[((item->pos.z - r->z) >> WALL_SHIFT)
+ ((item->pos.x - r->x) >> WALL_SHIFT) * r->z_size];
[((item->pos.z - r->pos.z) >> WALL_SHIFT)
+ ((item->pos.x - r->pos.x) >> WALL_SHIFT) * r->size.z];
item->box_num = sector->box;

if (Box_CalculateTarget(&target, item, &g_Lara.lot) == TARGET_NONE) {
Expand Down
89 changes: 42 additions & 47 deletions src/game/level.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,35 +149,33 @@ static void M_LoadRooms(VFILE *file)

g_RoomInfo = GameBuf_Alloc(sizeof(ROOM) * g_RoomCount, GBUF_ROOMS);
int i = 0;
for (ROOM *current_room_info = g_RoomInfo; i < g_RoomCount;
i++, current_room_info++) {
for (ROOM *r = g_RoomInfo; i < g_RoomCount; i++, r++) {
// Room position
current_room_info->x = VFile_ReadS32(file);
current_room_info->y = 0;
current_room_info->z = VFile_ReadS32(file);
r->pos.x = VFile_ReadS32(file);
r->pos.y = 0;
r->pos.z = VFile_ReadS32(file);

// Room floor/ceiling
current_room_info->min_floor = VFile_ReadS32(file);
current_room_info->max_ceiling = VFile_ReadS32(file);
r->min_floor = VFile_ReadS32(file);
r->max_ceiling = VFile_ReadS32(file);

// Room mesh
const uint32_t num_meshes = VFile_ReadS32(file);
const uint32_t inj_mesh_size = Inject_GetExtraRoomMeshSize(i);
current_room_info->data = GameBuf_Alloc(
r->data = GameBuf_Alloc(
sizeof(uint16_t) * (num_meshes + inj_mesh_size), GBUF_ROOM_MESH);
VFile_Read(
file, current_room_info->data, sizeof(uint16_t) * num_meshes);
VFile_Read(file, r->data, sizeof(uint16_t) * num_meshes);

// Doors
const uint16_t num_doors = VFile_ReadS16(file);
if (!num_doors) {
current_room_info->portals = NULL;
r->portals = NULL;
} else {
current_room_info->portals = GameBuf_Alloc(
r->portals = GameBuf_Alloc(
sizeof(uint16_t) + sizeof(PORTAL) * num_doors, GBUF_ROOM_DOOR);
current_room_info->portals->count = num_doors;
r->portals->count = num_doors;
for (int32_t j = 0; j < num_doors; j++) {
PORTAL *const portal = &current_room_info->portals->portal[j];
PORTAL *const portal = &r->portals->portal[j];
portal->room_num = VFile_ReadS16(file);
portal->normal.x = VFile_ReadS16(file);
portal->normal.y = VFile_ReadS16(file);
Expand All @@ -191,15 +189,14 @@ static void M_LoadRooms(VFILE *file)
}

// Room floor
current_room_info->z_size = VFile_ReadS16(file);
current_room_info->x_size = VFile_ReadS16(file);
const int32_t sector_count =
current_room_info->x_size * current_room_info->z_size;
current_room_info->sectors =
r->size.z = VFile_ReadS16(file);
r->size.x = VFile_ReadS16(file);
const int32_t sector_count = r->size.x * r->size.z;
r->sectors =
GameBuf_Alloc(sizeof(SECTOR) * sector_count, GBUF_ROOM_SECTOR);
for (int32_t j = 0; j < sector_count; j++) {
SECTOR *const sector = &current_room_info->sectors[j];
sector->index = VFile_ReadU16(file);
SECTOR *const sector = &r->sectors[j];
sector->idx = VFile_ReadU16(file);
sector->box = VFile_ReadS16(file);
sector->portal_room.pit = VFile_ReadU8(file);
const int8_t floor_clicks = VFile_ReadS8(file);
Expand All @@ -211,16 +208,15 @@ static void M_LoadRooms(VFILE *file)
}

// Room lights
current_room_info->ambient = VFile_ReadS16(file);
current_room_info->num_lights = VFile_ReadS16(file);
if (!current_room_info->num_lights) {
current_room_info->light = NULL;
r->ambient = VFile_ReadS16(file);
r->num_lights = VFile_ReadS16(file);
if (!r->num_lights) {
r->lights = NULL;
} else {
current_room_info->light = GameBuf_Alloc(
sizeof(LIGHT) * current_room_info->num_lights,
GBUF_ROOM_LIGHTS);
for (int32_t j = 0; j < current_room_info->num_lights; j++) {
LIGHT *light = &current_room_info->light[j];
r->lights =
GameBuf_Alloc(sizeof(LIGHT) * r->num_lights, GBUF_ROOM_LIGHTS);
for (int32_t j = 0; j < r->num_lights; j++) {
LIGHT *light = &r->lights[j];
light->pos.x = VFile_ReadS32(file);
light->pos.y = VFile_ReadS32(file);
light->pos.z = VFile_ReadS32(file);
Expand All @@ -230,15 +226,14 @@ static void M_LoadRooms(VFILE *file)
}

// Static mesh infos
current_room_info->num_meshes = VFile_ReadS16(file);
if (!current_room_info->num_meshes) {
current_room_info->mesh = NULL;
r->num_meshes = VFile_ReadS16(file);
if (!r->num_meshes) {
r->meshes = NULL;
} else {
current_room_info->mesh = GameBuf_Alloc(
sizeof(MESH) * current_room_info->num_meshes,
GBUF_ROOM_STATIC_MESHES);
for (int32_t j = 0; j < current_room_info->num_meshes; j++) {
MESH *mesh = &current_room_info->mesh[j];
r->meshes = GameBuf_Alloc(
sizeof(MESH) * r->num_meshes, GBUF_ROOM_STATIC_MESHES);
for (int32_t j = 0; j < r->num_meshes; j++) {
MESH *mesh = &r->meshes[j];
mesh->pos.x = VFile_ReadS32(file);
mesh->pos.y = VFile_ReadS32(file);
mesh->pos.z = VFile_ReadS32(file);
Expand All @@ -249,19 +244,19 @@ static void M_LoadRooms(VFILE *file)
}

// Flipped (alternative) room
current_room_info->flipped_room = VFile_ReadS16(file);
r->flipped_room = VFile_ReadS16(file);

// Room flags
current_room_info->flags = VFile_ReadU16(file);
r->flags = VFile_ReadU16(file);

// Initialise some variables
current_room_info->bound_active = 0;
current_room_info->left = Viewport_GetMaxX();
current_room_info->top = Viewport_GetMaxY();
current_room_info->bottom = 0;
current_room_info->right = 0;
current_room_info->item_num = NO_ITEM;
current_room_info->fx_num = NO_ITEM;
r->bound_active = 0;
r->bound_left = Viewport_GetMaxX();
r->bound_top = Viewport_GetMaxY();
r->bound_bottom = 0;
r->bound_right = 0;
r->item_num = NO_ITEM;
r->fx_num = NO_ITEM;
}

const int32_t fd_length = VFile_ReadS32(file);
Expand Down
6 changes: 3 additions & 3 deletions src/game/lot.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,9 @@ void LOT_CreateZone(ITEM *item)
}

const ROOM *const r = &g_RoomInfo[item->room_num];
const int32_t z_sector = (item->pos.z - r->z) >> WALL_SHIFT;
const int32_t x_sector = (item->pos.x - r->x) >> WALL_SHIFT;
item->box_num = r->sectors[z_sector + x_sector * r->z_size].box;
const int32_t z_sector = (item->pos.z - r->pos.z) >> WALL_SHIFT;
const int32_t x_sector = (item->pos.x - r->pos.x) >> WALL_SHIFT;
item->box_num = r->sectors[z_sector + x_sector * r->size.z].box;

int16_t zone_num = zone[item->box_num];
int16_t flip_num = flip[item->box_num];
Expand Down
4 changes: 2 additions & 2 deletions src/game/objects/creatures/bacon_lara.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ bool BaconLara_InitialiseAnchor(int32_t room_index)
}

ROOM *r = &g_RoomInfo[room_index];
m_AnchorX = r->x + r->x_size * (WALL_L >> 1);
m_AnchorZ = r->z + r->z_size * (WALL_L >> 1);
m_AnchorX = r->pos.x + r->size.x * (WALL_L >> 1);
m_AnchorZ = r->pos.z + r->size.z * (WALL_L >> 1);

return true;
}
Expand Down
Loading

0 comments on commit 6b7150a

Please sign in to comment.