Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tr2: migrate to common BOUNDS_16 #2255

Merged
merged 3 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 9 additions & 17 deletions src/libtrx/game/anims/frames.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,7 @@ static int32_t M_ParseFrame(
const uint8_t frame_size)
{
const int16_t *const frame_start = data_ptr;
#if TR_VERSION > 1
frame->bounds.min_x = *data_ptr++;
frame->bounds.max_x = *data_ptr++;
frame->bounds.min_y = *data_ptr++;
frame->bounds.max_y = *data_ptr++;
frame->bounds.min_z = *data_ptr++;
frame->bounds.max_z = *data_ptr++;
frame->offset.x = *data_ptr++;
frame->offset.y = *data_ptr++;
frame->offset.z = *data_ptr++;

frame->mesh_rots =
GameBuf_Alloc(sizeof(int16_t) * (frame_size - 9), GBUF_ANIM_FRAMES);
memcpy(frame->mesh_rots, data_ptr, sizeof(int16_t) * (frame_size - 9));
data_ptr += MAX(0, frame_size - (data_ptr - frame_start));
#else
frame->bounds.min.x = *data_ptr++;
frame->bounds.max.x = *data_ptr++;
frame->bounds.min.y = *data_ptr++;
Expand All @@ -91,17 +76,24 @@ static int32_t M_ParseFrame(
frame->offset.x = *data_ptr++;
frame->offset.y = *data_ptr++;
frame->offset.z = *data_ptr++;
#if TR_VERSION == 1
#if TR_VERSION == 1
mesh_count = *data_ptr++;
#endif
#endif

#if TR_VERSION == 1
frame->mesh_rots =
GameBuf_Alloc(sizeof(XYZ_16) * mesh_count, GBUF_ANIM_FRAMES);
for (int32_t i = 0; i < mesh_count; i++) {
XYZ_16 *const rot = &frame->mesh_rots[i];
M_ParseMeshRotation(rot, &data_ptr);
}
#else
frame->mesh_rots =
GameBuf_Alloc(sizeof(int16_t) * (frame_size - 9), GBUF_ANIM_FRAMES);
memcpy(frame->mesh_rots, data_ptr, sizeof(int16_t) * (frame_size - 9));
data_ptr += MAX(0, frame_size - (data_ptr - frame_start));
#endif

return data_ptr - frame_start;
}

Expand Down
12 changes: 1 addition & 11 deletions src/libtrx/include/libtrx/game/math/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,16 @@ typedef enum {
DIR_WEST = 3,
} DIRECTION;

#if TR_VERSION == 1
typedef struct {
XYZ_16 min;
XYZ_16 max;
} BOUNDS_16;

#if TR_VERSION == 1
typedef struct {
XYZ_32 min;
XYZ_32 max;
} BOUNDS_32;

#elif TR_VERSION == 2
typedef struct {
int16_t min_x;
int16_t max_x;
int16_t min_y;
int16_t max_y;
int16_t min_z;
int16_t max_z;
} BOUNDS_16;
#endif

#pragma pack(pop)
10 changes: 5 additions & 5 deletions src/tr2/game/camera.c
Original file line number Diff line number Diff line change
Expand Up @@ -732,10 +732,10 @@ void Camera_Update(void)

int32_t y = item->pos.y;
if (fixed_camera) {
y += (bounds->min_y + bounds->max_y) / 2;
y += (bounds->min.y + bounds->max.y) / 2;
} else {
y += bounds->max_y
+ (((int32_t)(bounds->min_y - bounds->max_y)) * 3 >> 2);
y += bounds->max.y
+ (((int32_t)(bounds->min.y - bounds->max.y)) * 3 >> 2);
}

if (g_Camera.item && !fixed_camera) {
Expand All @@ -748,7 +748,7 @@ void Camera_Update(void)

int16_t tilt = Math_Atan(
shift,
y - (bounds->min_y + bounds->max_y) / 2 - g_Camera.item->pos.y);
y - (bounds->min.y + bounds->max.y) / 2 - g_Camera.item->pos.y);
angle >>= 1;
tilt >>= 1;

Expand Down Expand Up @@ -800,7 +800,7 @@ void Camera_Update(void)
g_Camera.target.z = item->pos.z;

if (g_Camera.flags == CF_FOLLOW_CENTRE) {
const int32_t shift = (bounds->min_z + bounds->max_z) / 2;
const int32_t shift = (bounds->min.z + bounds->max.z) / 2;
g_Camera.target.z += (shift * Math_Cos(item->rot.y)) >> W2V_SHIFT;
g_Camera.target.x += (shift * Math_Sin(item->rot.y)) >> W2V_SHIFT;
}
Expand Down
36 changes: 18 additions & 18 deletions src/tr2/game/collide.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,35 +320,35 @@ int32_t Collide_CollideStaticObjects(
int32_t x_max;
int32_t z_min;
int32_t z_max;
const int32_t y_min = mesh->pos.y + sinfo->collision_bounds.min_y;
const int32_t y_max = mesh->pos.y + sinfo->collision_bounds.max_y;
const int32_t y_min = mesh->pos.y + sinfo->collision_bounds.min.y;
const int32_t y_max = mesh->pos.y + sinfo->collision_bounds.max.y;
switch (mesh->rot.y) {
case PHD_90:
x_min = mesh->pos.x + sinfo->collision_bounds.min_z;
x_max = mesh->pos.x + sinfo->collision_bounds.max_z;
z_min = mesh->pos.z - sinfo->collision_bounds.max_x;
z_max = mesh->pos.z - sinfo->collision_bounds.min_x;
x_min = mesh->pos.x + sinfo->collision_bounds.min.z;
x_max = mesh->pos.x + sinfo->collision_bounds.max.z;
z_min = mesh->pos.z - sinfo->collision_bounds.max.x;
z_max = mesh->pos.z - sinfo->collision_bounds.min.x;
break;

case -PHD_180:
x_min = mesh->pos.x - sinfo->collision_bounds.max_x;
x_max = mesh->pos.x - sinfo->collision_bounds.min_x;
z_min = mesh->pos.z - sinfo->collision_bounds.max_z;
z_max = mesh->pos.z - sinfo->collision_bounds.min_z;
x_min = mesh->pos.x - sinfo->collision_bounds.max.x;
x_max = mesh->pos.x - sinfo->collision_bounds.min.x;
z_min = mesh->pos.z - sinfo->collision_bounds.max.z;
z_max = mesh->pos.z - sinfo->collision_bounds.min.z;
break;

case -PHD_90:
x_min = mesh->pos.x - sinfo->collision_bounds.max_z;
x_max = mesh->pos.x - sinfo->collision_bounds.min_z;
z_min = mesh->pos.z + sinfo->collision_bounds.min_x;
z_max = mesh->pos.z + sinfo->collision_bounds.max_x;
x_min = mesh->pos.x - sinfo->collision_bounds.max.z;
x_max = mesh->pos.x - sinfo->collision_bounds.min.z;
z_min = mesh->pos.z + sinfo->collision_bounds.min.x;
z_max = mesh->pos.z + sinfo->collision_bounds.max.x;
break;

default:
x_min = mesh->pos.x + sinfo->collision_bounds.min_x;
x_max = mesh->pos.x + sinfo->collision_bounds.max_x;
z_min = mesh->pos.z + sinfo->collision_bounds.min_z;
z_max = mesh->pos.z + sinfo->collision_bounds.max_z;
x_min = mesh->pos.x + sinfo->collision_bounds.min.x;
x_max = mesh->pos.x + sinfo->collision_bounds.max.x;
z_min = mesh->pos.z + sinfo->collision_bounds.min.z;
z_max = mesh->pos.z + sinfo->collision_bounds.max.z;
break;
}

Expand Down
6 changes: 3 additions & 3 deletions src/tr2/game/creature.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ void Creature_Mood(const ITEM *item, const AI_INFO *info, int32_t violent)
lot->target = enemy->pos;
lot->required_box = enemy->box_num;
if (lot->fly != 0 && g_Lara.water_status == LWS_ABOVE_WATER) {
lot->target.y += Item_GetBestFrame(enemy)->bounds.min_y;
lot->target.y += Item_GetBestFrame(enemy)->bounds.min.y;
}
break;

Expand Down Expand Up @@ -412,7 +412,7 @@ int32_t Creature_Animate(
}

const BOUNDS_16 *const bounds = Item_GetBoundsAccurate(item);
int32_t y = item->pos.y + bounds->min_y;
int32_t y = item->pos.y + bounds->min.y;

int16_t room_num = item->room_num;
Room_GetSector(old.x, y, old.z, &room_num);
Expand Down Expand Up @@ -567,7 +567,7 @@ int32_t Creature_Animate(
const int32_t ceiling =
Room_GetCeiling(sector, item->pos.x, y, item->pos.z);
const int32_t min_y =
item->object_id == O_SHARK ? 128 : bounds->min_y;
item->object_id == O_SHARK ? 128 : bounds->min.y;
if (item->pos.y + min_y + dy < ceiling) {
if (item->pos.y + min_y < ceiling) {
item->pos.x = old.x;
Expand Down
6 changes: 3 additions & 3 deletions src/tr2/game/gun/gun_misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,9 @@ int32_t Gun_FireWeapon(
void Gun_FindTargetPoint(const ITEM *const item, GAME_VECTOR *const target)
{
const BOUNDS_16 *const bounds = &Item_GetBestFrame(item)->bounds;
const int32_t x = bounds->min_x + (bounds->max_x - bounds->min_x) / 2;
const int32_t y = bounds->min_y + (bounds->max_y - bounds->min_y) / 3;
const int32_t z = bounds->min_z + (bounds->max_z - bounds->min_z) / 2;
const int32_t x = bounds->min.x + (bounds->max.x - bounds->min.x) / 2;
const int32_t y = bounds->min.y + (bounds->max.y - bounds->min.y) / 3;
const int32_t z = bounds->min.z + (bounds->max.z - bounds->min.z) / 2;
const int32_t cy = Math_Cos(item->rot.y);
const int32_t sy = Math_Sin(item->rot.y);
target->pos.x = item->pos.x + ((cy * x + sy * z) >> W2V_SHIFT);
Expand Down
30 changes: 15 additions & 15 deletions src/tr2/game/items.c
Original file line number Diff line number Diff line change
Expand Up @@ -344,10 +344,10 @@ int32_t Item_TestBoundsCollide(
const BOUNDS_16 *const src_bounds = &Item_GetBestFrame(src_item)->bounds;
const BOUNDS_16 *const dst_bounds = &Item_GetBestFrame(dst_item)->bounds;

if (src_item->pos.y + src_bounds->max_y
<= dst_item->pos.y + dst_bounds->min_y
|| src_item->pos.y + src_bounds->min_y
>= dst_item->pos.y + dst_bounds->max_y) {
if (src_item->pos.y + src_bounds->max.y
<= dst_item->pos.y + dst_bounds->min.y
|| src_item->pos.y + src_bounds->min.y
>= dst_item->pos.y + dst_bounds->max.y) {
return false;
}

Expand All @@ -360,10 +360,10 @@ int32_t Item_TestBoundsCollide(

// clang-format off
return (
rx >= src_bounds->min_x - radius &&
rx <= src_bounds->max_x + radius &&
rz >= src_bounds->min_z - radius &&
rz <= src_bounds->max_z + radius);
rx >= src_bounds->min.x - radius &&
rx <= src_bounds->max.x + radius &&
rz >= src_bounds->min.z - radius &&
rz <= src_bounds->max.z + radius);
// clang-format on
}

Expand Down Expand Up @@ -675,12 +675,12 @@ BOUNDS_16 *Item_GetBoundsAccurate(const ITEM *const item)
target->prop = (b1)->prop + ((((b2)->prop - (b1)->prop) * frac) / rate);

BOUNDS_16 *const result = &m_InterpolatedBounds;
CALC(result, &frmptr[0]->bounds, &frmptr[1]->bounds, min_x);
CALC(result, &frmptr[0]->bounds, &frmptr[1]->bounds, max_x);
CALC(result, &frmptr[0]->bounds, &frmptr[1]->bounds, min_y);
CALC(result, &frmptr[0]->bounds, &frmptr[1]->bounds, max_y);
CALC(result, &frmptr[0]->bounds, &frmptr[1]->bounds, min_z);
CALC(result, &frmptr[0]->bounds, &frmptr[1]->bounds, max_z);
CALC(result, &frmptr[0]->bounds, &frmptr[1]->bounds, min.x);
CALC(result, &frmptr[0]->bounds, &frmptr[1]->bounds, max.x);
CALC(result, &frmptr[0]->bounds, &frmptr[1]->bounds, min.y);
CALC(result, &frmptr[0]->bounds, &frmptr[1]->bounds, max.y);
CALC(result, &frmptr[0]->bounds, &frmptr[1]->bounds, min.z);
CALC(result, &frmptr[0]->bounds, &frmptr[1]->bounds, max.z);
return result;
}

Expand Down Expand Up @@ -709,7 +709,7 @@ bool Item_IsNearItem(
}

const BOUNDS_16 *const bounds = Item_GetBoundsAccurate(item);
return d.y >= bounds->min_y && d.y <= bounds->max_y + 100;
return d.y >= bounds->min.y && d.y <= bounds->max.y + 100;
}

int32_t Item_GetDistance(const ITEM *const item, const XYZ_32 *const target)
Expand Down
6 changes: 3 additions & 3 deletions src/tr2/game/lara/hair.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,11 +320,11 @@ void Lara_Hair_Control(const bool in_cutscene)
} else {
water_height = Room_GetWaterHeight(
g_LaraItem->pos.x
+ (frame_1->bounds.min_x + frame_1->bounds.max_x) / 2,
+ (frame_1->bounds.min.x + frame_1->bounds.max.x) / 2,
g_LaraItem->pos.y
+ (frame_1->bounds.max_y + frame_1->bounds.min_y) / 2,
+ (frame_1->bounds.max.y + frame_1->bounds.min.y) / 2,
g_LaraItem->pos.z
+ (frame_1->bounds.max_z + frame_1->bounds.min_z) / 2,
+ (frame_1->bounds.max.z + frame_1->bounds.min.z) / 2,
room_num);
}

Expand Down
34 changes: 17 additions & 17 deletions src/tr2/game/lara/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,8 @@ int32_t Lara_TestHangOnClimbWall(ITEM *item, COLL_INFO *coll)
}

const BOUNDS_16 *const bounds = Item_GetBoundsAccurate(item);
int32_t y = bounds->min_y;
int32_t h = bounds->max_y - y;
int32_t y = bounds->min.y;
int32_t h = bounds->max.y - y;

int32_t shift;
if (!Lara_TestClimbPos(item, coll->radius, coll->radius, y, h, &shift)) {
Expand Down Expand Up @@ -444,7 +444,7 @@ void Lara_HangTest(ITEM *item, COLL_INFO *coll)
item->current_anim_state = LS_UP_JUMP;
Item_SwitchToAnim(item, LA_JUMP_UP, LF_STOP_HANG);
const BOUNDS_16 *const bounds = Item_GetBoundsAccurate(item);
item->pos.y += bounds->max_y;
item->pos.y += bounds->max.y;
item->pos.x += coll->shift.x;
item->pos.z += coll->shift.z;
item->gravity = 1;
Expand All @@ -455,7 +455,7 @@ void Lara_HangTest(ITEM *item, COLL_INFO *coll)
}

const BOUNDS_16 *const bounds = Item_GetBoundsAccurate(item);
int32_t hdif = coll->side_front.floor - bounds->min_y;
int32_t hdif = coll->side_front.floor - bounds->min.y;

if (ABS(coll->side_left.floor - coll->side_right.floor) >= SLOPE_DIF
|| coll->side_mid.ceiling >= 0 || coll->coll_type != COLL_FRONT || flag
Expand Down Expand Up @@ -493,10 +493,10 @@ void Lara_HangTest(ITEM *item, COLL_INFO *coll)
int32_t Lara_TestEdgeCatch(ITEM *item, COLL_INFO *coll, int32_t *edge)
{
const BOUNDS_16 *const bounds = Item_GetBoundsAccurate(item);
int32_t hdif1 = coll->side_front.floor - bounds->min_y;
int32_t hdif1 = coll->side_front.floor - bounds->min.y;
int32_t hdif2 = hdif1 + item->fall_speed;
if ((hdif1 < 0 && hdif2 < 0) || (hdif1 > 0 && hdif2 > 0)) {
hdif1 = item->pos.y + bounds->min_y;
hdif1 = item->pos.y + bounds->min.y;
hdif2 = hdif1 + item->fall_speed;
if ((hdif1 >> (WALL_SHIFT - 2)) == (hdif2 >> (WALL_SHIFT - 2))) {
return 0;
Expand Down Expand Up @@ -539,9 +539,9 @@ int32_t Lara_TestHangJumpUp(ITEM *item, COLL_INFO *coll)

const BOUNDS_16 *const bounds = Item_GetBoundsAccurate(item);
if (edge_catch > 0) {
item->pos.y += coll->side_front.floor - bounds->min_y;
item->pos.y += coll->side_front.floor - bounds->min.y;
} else {
item->pos.y = edge - bounds->min_y;
item->pos.y = edge - bounds->min.y;
}
item->pos.x += coll->shift.x;
item->pos.z += coll->shift.z;
Expand Down Expand Up @@ -585,11 +585,11 @@ int32_t Lara_TestHangJump(ITEM *item, COLL_INFO *coll)

const BOUNDS_16 *const bounds = Item_GetBoundsAccurate(item);
if (edge_catch > 0) {
item->pos.y += coll->side_front.floor - bounds->min_y;
item->pos.y += coll->side_front.floor - bounds->min.y;
item->pos.x += coll->shift.x;
item->pos.z += coll->shift.z;
} else {
item->pos.y = edge - bounds->min_y;
item->pos.y = edge - bounds->min.y;
}

item->rot.y = angle;
Expand Down Expand Up @@ -1106,10 +1106,10 @@ void Lara_Push(
int32_t rz = (c * dz + s * dx) >> W2V_SHIFT;

const BOUNDS_16 *const bounds = &Item_GetBestFrame(item)->bounds;
int32_t min_x = bounds->min_x;
int32_t max_x = bounds->max_x;
int32_t min_z = bounds->min_z;
int32_t max_z = bounds->max_z;
int32_t min_x = bounds->min.x;
int32_t max_x = bounds->max.x;
int32_t min_z = bounds->min.z;
int32_t max_z = bounds->max.z;

if (big_push) {
max_x += coll->radius;
Expand Down Expand Up @@ -1140,12 +1140,12 @@ void Lara_Push(
lara_item->pos.x = item->pos.x + ((rz * s + rx * c) >> W2V_SHIFT);
lara_item->pos.z = item->pos.z + ((rz * c - rx * s) >> W2V_SHIFT);

rz = (bounds->max_z + bounds->min_z) / 2;
rx = (bounds->max_x + bounds->min_x) / 2;
rz = (bounds->max.z + bounds->min.z) / 2;
rx = (bounds->max.x + bounds->min.x) / 2;
dx -= (c * rx + s * rz) >> W2V_SHIFT;
dz -= (c * rz - s * rx) >> W2V_SHIFT;

if (spaz_on && bounds->max_y - bounds->min_y > STEP_L) {
if (spaz_on && bounds->max.y - bounds->min.y > STEP_L) {
M_TakeHit(lara_item, dx, dz);
}

Expand Down
Loading
Loading