Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
port Room_SetBounds
Browse files Browse the repository at this point in the history
  • Loading branch information
rr- committed Jul 24, 2024
1 parent 2f23a59 commit 01ef6c5
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 11 deletions.
16 changes: 8 additions & 8 deletions docs/progress.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 8 additions & 1 deletion docs/progress.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2606,6 +2606,12 @@ typedef struct __unaligned {
} shift, rot;
} OBJECT_BOUNDS;

typedef struct __unaligned {
int32_t xv;
int32_t yv;
int32_t zv;
} DOOR_VBUF;

typedef enum {
TO_OBJECT = 0,
TO_CAMERA = 1,
Expand Down Expand Up @@ -2883,7 +2889,7 @@ typedef enum {
0x00418990 0x0037 - int32_t __cdecl Game_Draw(void);
0x004189D0 0x02B0 - void __cdecl Room_DrawAllRooms(int16_t current_room);
0x00418C80 0x01C6 + void __cdecl Room_GetBounds(void);
0x00418E50 0x037F - void __cdecl Room_SetBounds(const int16_t *objptr, int32_t room_num, ROOM_INFO *parent);
0x00418E50 0x037F + void __cdecl Room_SetBounds(const int16_t *objptr, int32_t room_num, ROOM_INFO *parent);
0x004191D0 0x03D2 - void __cdecl Room_Clip(ROOM_INFO *r);
0x004195B0 0x00B4 - void __cdecl Room_DrawSingleRoomGeometry(int16_t room_num);
0x00419670 0x0218 - void __cdecl Room_DrawSingleRoomObjects(int16_t room_num);
Expand Down Expand Up @@ -4338,3 +4344,4 @@ typedef enum {
0x005261AC - int32_t g_OutsideTop;
0x00525B00 - int32_t g_OutsideBottom;
0x00525900 - int32_t g_BoundRooms[MAX_BOUND_ROOMS];
0x005258C0 - DOOR_VBUF g_DoorVBuf[4];
148 changes: 147 additions & 1 deletion src/game/room_draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include "game/matrix.h"
#include "global/funcs.h"
#include "global/types.h"
#include "global/vars.h"

void __cdecl Room_GetBounds(void)
Expand Down Expand Up @@ -76,3 +75,150 @@ void __cdecl Room_GetBounds(void)
Matrix_Pop();
}
}

void __cdecl Room_SetBounds(
const int16_t *obj_ptr, int32_t room_num, const ROOM_INFO *parent)
{
ROOM_INFO *const r = &g_Rooms[room_num];
const DOOR_INFO *const door = (const DOOR_INFO *)(obj_ptr - 1);

// clang-format off
if (r->bound_left <= parent->test_left &&
r->bound_right >= parent->test_right &&
r->bound_top <= parent->test_top &&
r->bound_bottom >= parent->test_bottom
) {
return;
}
// clang-format on

const MATRIX *const m = g_MatrixPtr;
int32_t left = parent->test_right;
int32_t right = parent->test_left;
int32_t bottom = parent->test_top;
int32_t top = parent->test_bottom;

DOOR_VBUF door_vbuf[4];
int32_t z_behind = 0;
int32_t z_too_far = 0;

for (int32_t i = 0; i < 4; i++) {
DOOR_VBUF *const dvbuf = &door_vbuf[i];
const XYZ_16 *const dvtx = &door->vertex[i];
const int32_t xv =
m->_03 + dvtx->x * m->_00 + m->_01 * dvtx->y + m->_02 * dvtx->z;
const int32_t yv =
m->_13 + m->_12 * dvtx->z + m->_11 * dvtx->y + m->_10 * dvtx->x;
const int32_t zv =
m->_23 + m->_21 * dvtx->y + m->_22 * dvtx->z + m->_20 * dvtx->x;
dvbuf->xv = xv;
dvbuf->yv = yv;
dvbuf->zv = zv;

if (zv <= 0) {
z_behind++;
continue;
}

if (zv > g_PhdFarZ) {
z_too_far++;
}

int32_t xs;
int32_t ys;
const int32_t zp = zv / g_PhdPersp;
if (zp) {
xs = xv / zp + g_PhdWinCenterX;
ys = yv / zp + g_PhdWinCenterY;
} else {
xs = xv < 0 ? g_PhdWinLeft : g_PhdWinRight;
ys = yv < 0 ? g_PhdWinTop : g_PhdWinBottom;
}

if (xs - 1 < left) {
left = xs - 1;
}
if (xs + 1 > right) {
right = xs + 1;
}
if (ys - 1 < top) {
top = ys - 1;
}
if (ys + 1 > bottom) {
bottom = ys + 1;
}
}

if (z_behind == 4 || z_too_far == 4) {
return;
}

if (z_behind > 0) {
const DOOR_VBUF *dest = &door_vbuf[0];
const DOOR_VBUF *last = &door_vbuf[3];

for (int32_t i = 0; i < 4; i++, last = dest++) {
if ((dest->zv < 0) == (last->zv < 0)) {
continue;
}

if (dest->xv < 0 && last->xv < 0) {
left = 0;
} else if (dest->xv > 0 && last->xv > 0) {
right = g_PhdWinMaxX;
} else {
left = 0;
right = g_PhdWinMaxX;
}

if (dest->yv < 0 && last->yv < 0) {
top = 0;
} else if (dest->yv > 0 && last->yv > 0) {
bottom = g_PhdWinMaxY;
} else {
top = 0;
bottom = g_PhdWinMaxY;
}
}
}

if (left < parent->test_left) {
left = parent->test_left;
}
if (right > parent->test_right) {
right = parent->test_right;
}
if (top < parent->test_top) {
top = parent->test_top;
}
if (bottom > parent->test_bottom) {
bottom = parent->test_bottom;
}

if (left >= right || top >= bottom) {
return;
}

if (r->bound_active & 2) {
if (left < r->test_left) {
r->test_left = left;
}
if (top < r->test_top) {
r->test_top = top;
}
if (right > r->test_right) {
r->test_right = right;
}
if (bottom > r->test_bottom) {
r->test_bottom = bottom;
}
} else {
g_BoundRooms[g_BoundEnd++ % MAX_BOUND_ROOMS] = room_num;
r->bound_active |= 2;
r->bound_active += (int16_t)(g_MidSort << 8);
r->test_left = left;
r->test_right = right;
r->test_top = top;
r->test_bottom = bottom;
}
}
4 changes: 4 additions & 0 deletions src/game/room_draw.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#pragma once

#include "global/types.h"

void __cdecl Room_GetBounds(void);
void __cdecl Room_SetBounds(
const int16_t *obj_ptr, int32_t room_num, const ROOM_INFO *parent);
1 change: 0 additions & 1 deletion src/global/funcs.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
#define Game_DrawCinematic ((int32_t __cdecl (*)(void))0x00418950)
#define Game_Draw ((int32_t __cdecl (*)(void))0x00418990)
#define Room_DrawAllRooms ((void __cdecl (*)(int16_t current_room))0x004189D0)
#define Room_SetBounds ((void __cdecl (*)(const int16_t *objptr, int32_t room_num, ROOM_INFO *parent))0x00418E50)
#define Room_Clip ((void __cdecl (*)(ROOM_INFO *r))0x004191D0)
#define Room_DrawSingleRoomGeometry ((void __cdecl (*)(int16_t room_num))0x004195B0)
#define Room_DrawSingleRoomObjects ((void __cdecl (*)(int16_t room_num))0x00419670)
Expand Down
6 changes: 6 additions & 0 deletions src/global/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -2622,6 +2622,12 @@ typedef struct __unaligned {
} shift, rot;
} OBJECT_BOUNDS;

typedef struct __unaligned {
int32_t xv;
int32_t yv;
int32_t zv;
} DOOR_VBUF;

typedef enum {
TO_OBJECT = 0,
TO_CAMERA = 1,
Expand Down
1 change: 1 addition & 0 deletions src/global/vars.h
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ extern const char *g_TR2XVersion;
#define g_Outside (*(int32_t*)0x005252B4)
#define g_DrawRoomsCount (*(int32_t*)0x005252B8)
#define g_IMMatrixStack (*(MATRIX(*)[256])0x005252C0)
#define g_DoorVBuf (*(DOOR_VBUF(*)[4])0x005258C0)
#define g_IMFrac (*(int32_t*)0x005258F0)
#define g_Anims (*(ANIM_STRUCT **)0x005258F4)
#define g_BoundRooms (*(int32_t(*)[MAX_BOUND_ROOMS])0x00525900)
Expand Down
1 change: 1 addition & 0 deletions src/inject_exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ static void Inject_Room(const bool enable)
INJECT(enable, 0x00416700, Room_RemoveFlipItems);
INJECT(enable, 0x004167A0, Room_AddFlipItems);
INJECT(enable, 0x00418C80, Room_GetBounds);
INJECT(enable, 0x00418E50, Room_SetBounds);
}

static void Inject_Matrix(const bool enable)
Expand Down

0 comments on commit 01ef6c5

Please sign in to comment.