From 6a6b65ddc45cc0c7d2964e7cd76c7214a5d23131 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustaf=20Alh=C3=A4ll?= Date: Thu, 16 Nov 2023 21:03:06 +0100 Subject: [PATCH 1/2] Optimize Z position functions on sloped sectors --- src/p_mobj.c | 8 ++++---- src/r_main.c | 28 ++++++++++++++++++++++++++++ src/r_main.h | 1 + 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/p_mobj.c b/src/p_mobj.c index 7a5aaf424..2f9702d1c 100644 --- a/src/p_mobj.c +++ b/src/p_mobj.c @@ -1109,7 +1109,7 @@ fixed_t P_MobjFloorZ(mobj_t *mobj, sector_t *sector, sector_t *boundsec, fixed_t testy += y; // If the highest point is in the sector, then we have it easy! Just get the Z at that point - if (R_PointInSubsector(testx, testy)->sector == (boundsec ? boundsec : sector)) + if (R_IsPointInSector(boundsec ? boundsec : sector, testx, testy)) return P_GetSlopeZAt(slope, testx, testy); // If boundsec is set, we're looking for specials. In that case, iterate over every line in this sector to find the TRUE highest/lowest point @@ -1186,7 +1186,7 @@ fixed_t P_MobjCeilingZ(mobj_t *mobj, sector_t *sector, sector_t *boundsec, fixed testy += y; // If the highest point is in the sector, then we have it easy! Just get the Z at that point - if (R_PointInSubsector(testx, testy)->sector == (boundsec ? boundsec : sector)) + if (R_IsPointInSector(boundsec ? boundsec : sector, testx, testy)) return P_GetSlopeZAt(slope, testx, testy); // If boundsec is set, we're looking for specials. In that case, iterate over every line in this sector to find the TRUE highest/lowest point @@ -1264,7 +1264,7 @@ fixed_t P_CameraFloorZ(camera_t *mobj, sector_t *sector, sector_t *boundsec, fix testy += y; // If the highest point is in the sector, then we have it easy! Just get the Z at that point - if (R_PointInSubsector(testx, testy)->sector == (boundsec ? boundsec : sector)) + if (R_IsPointInSector(boundsec ? boundsec : sector, testx, testy)) return P_GetSlopeZAt(slope, testx, testy); // If boundsec is set, we're looking for specials. In that case, iterate over every line in this sector to find the TRUE highest/lowest point @@ -1341,7 +1341,7 @@ fixed_t P_CameraCeilingZ(camera_t *mobj, sector_t *sector, sector_t *boundsec, f testy += y; // If the highest point is in the sector, then we have it easy! Just get the Z at that point - if (R_PointInSubsector(testx, testy)->sector == (boundsec ? boundsec : sector)) + if (R_IsPointInSector(boundsec ? boundsec : sector, testx, testy)) return P_GetSlopeZAt(slope, testx, testy); // If boundsec is set, we're looking for specials. In that case, iterate over every line in this sector to find the TRUE highest/lowest point diff --git a/src/r_main.c b/src/r_main.c index 6c7bedbf1..aab6ed620 100644 --- a/src/r_main.c +++ b/src/r_main.c @@ -1027,6 +1027,34 @@ void R_Init(void) framecount = 0; } +// +// R_IsPointInSector +// +boolean R_IsPointInSector(sector_t *sector, fixed_t x, fixed_t y) +{ + size_t i; + line_t *closest = NULL; + fixed_t closestdist = 0x7fffffff; + + for (i = 0; i < sector->linecount; i++) + { + vertex_t v; + fixed_t dist; + + // find the line closest to the point we're looking for. + P_ClosestPointOnLine(x, y, sector->lines[i], &v); + dist = R_PointToDist2(0, 0, v.x - x, v.y - y); + if (dist < closestdist) + { + closest = sector->lines[i]; + closestdist = dist; + } + } + + // if the side of the closest line is in this sector, we're inside of it. + return P_PointOnLineSide(x, y, closest) == 0 ? closest->frontsector == sector : closest->backsector == sector; +} + // // R_PointInSubsector // diff --git a/src/r_main.h b/src/r_main.h index a6fb42ba2..02c640b51 100644 --- a/src/r_main.h +++ b/src/r_main.h @@ -79,6 +79,7 @@ fixed_t R_PointToDist(fixed_t x, fixed_t y); fixed_t R_PointToDist2(fixed_t px2, fixed_t py2, fixed_t px1, fixed_t py1); fixed_t R_ScaleFromGlobalAngle(angle_t visangle); +boolean R_IsPointInSector(sector_t *sector, fixed_t x, fixed_t y); subsector_t *R_PointInSubsector(fixed_t x, fixed_t y); subsector_t *R_PointInSubsectorOrNull(fixed_t x, fixed_t y); From db9b26c8764ca157510821350ee0fbb9d52807d2 Mon Sep 17 00:00:00 2001 From: Zwip-Zwap Zapony Date: Thu, 16 Nov 2023 20:36:08 +0000 Subject: [PATCH 2/2] Use INT32_MAX --- src/r_main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/r_main.c b/src/r_main.c index aab6ed620..06454646b 100644 --- a/src/r_main.c +++ b/src/r_main.c @@ -1034,7 +1034,7 @@ boolean R_IsPointInSector(sector_t *sector, fixed_t x, fixed_t y) { size_t i; line_t *closest = NULL; - fixed_t closestdist = 0x7fffffff; + fixed_t closestdist = INT32_MAX; for (i = 0; i < sector->linecount; i++) {