From 7a46019af3dd728d31c44c8d40acc57ba1bd4f2f Mon Sep 17 00:00:00 2001
From: bones_was_here <bones_was_here@xonotic.au>
Date: Fri, 3 Feb 2023 14:40:07 +1000
Subject: [PATCH] Include boxesoverlap() in WarpZoneLib_ExactTrigger_Touch()
 for convenient efficiency and less code duplication

---
 qcsrc/lib/warpzone/common.qc      | 9 +++++++--
 qcsrc/lib/warpzone/common.qh      | 4 ++--
 qcsrc/lib/warpzone/server.qc      | 7 +++----
 qcsrc/lib/warpzone/util_server.qh | 1 -
 4 files changed, 12 insertions(+), 9 deletions(-)

diff --git a/qcsrc/lib/warpzone/common.qc b/qcsrc/lib/warpzone/common.qc
index 4e82e2a7dd..d9a12517d1 100644
--- a/qcsrc/lib/warpzone/common.qc
+++ b/qcsrc/lib/warpzone/common.qc
@@ -808,7 +808,7 @@ entity WarpZone_RefSys_SpawnSameRefSys(entity me)
 	return e;
 }
 
-bool WarpZoneLib_ExactTrigger_Touch(entity this, entity toucher)
+bool WarpZoneLib_ExactTrigger_Touch(entity this, entity toucher, bool touchfunc)
 {
 	vector emin = toucher.absmin, emax = toucher.absmax;
 	if(STAT(Q3COMPAT))
@@ -818,7 +818,12 @@ bool WarpZoneLib_ExactTrigger_Touch(entity this, entity toucher)
 		emin += '1 1 1';
 		emax -= '1 1 1';
 	}
-	return WarpZoneLib_BoxTouchesBrush(emin, emax, this, toucher);
+
+	// if called from a touch func, we can assume the boxes do overlap
+	if (!touchfunc && !boxesoverlap(emin, emax, this.absmin, this.absmax)) // quick
+		return false;
+
+	return WarpZoneLib_BoxTouchesBrush(emin, emax, this, toucher); // accurate
 }
 
 
diff --git a/qcsrc/lib/warpzone/common.qh b/qcsrc/lib/warpzone/common.qh
index 0203915289..ea7619af4d 100644
--- a/qcsrc/lib/warpzone/common.qh
+++ b/qcsrc/lib/warpzone/common.qh
@@ -109,9 +109,9 @@ entity WarpZone_RefSys_SpawnSameRefSys(entity me); // spawn().R = me.R
 bool WarpZoneLib_MoveOutOfSolid(entity e);
 #define move_out_of_solid(e) WarpZoneLib_MoveOutOfSolid(e)
 
-bool WarpZoneLib_ExactTrigger_Touch(entity this, entity toucher);
+bool WarpZoneLib_ExactTrigger_Touch(entity this, entity toucher, bool touchfunc);
 void WarpZoneLib_ExactTrigger_Init(entity this);
 
 // WARNING: this kills the trace globals
-#define EXACTTRIGGER_TOUCH(e,t) if(!WarpZoneLib_ExactTrigger_Touch((e), (t))) return
+#define EXACTTRIGGER_TOUCH(e,t) if(!WarpZoneLib_ExactTrigger_Touch((e), (t), true)) return // intended for use in touch funcs
 #define EXACTTRIGGER_INIT  WarpZoneLib_ExactTrigger_Init(this)
diff --git a/qcsrc/lib/warpzone/server.qc b/qcsrc/lib/warpzone/server.qc
index b9157d2526..f58a9801b3 100644
--- a/qcsrc/lib/warpzone/server.qc
+++ b/qcsrc/lib/warpzone/server.qc
@@ -193,8 +193,7 @@ void WarpZone_Touch(entity this, entity toucher)
 	if(toucher.move_movetype == MOVETYPE_NONE || toucher.move_movetype == MOVETYPE_FOLLOW || toucher.tag_entity)
 		return;
 
-	if(!WarpZoneLib_ExactTrigger_Touch(this, toucher))
-		return;
+	EXACTTRIGGER_TOUCH(this, toucher);
 
 	if(WarpZone_PlaneDist(this, toucher.origin + toucher.view_ofs) >= 0) // wrong side of the trigger_warpzone (don't teleport yet)
 		return;
@@ -815,7 +814,7 @@ void WarpZone_StartFrame()
 			if (warpzone_warpzones_exist) {
 				entity e = WarpZone_Find(it.origin + it.mins, it.origin + it.maxs);
 				if (e)
-				if (WarpZoneLib_ExactTrigger_Touch(e, it))
+				if (WarpZoneLib_ExactTrigger_Touch(e, it, false))
 				if (WarpZone_PlaneDist(e, it.origin + it.view_ofs) <= 0)
 					WarpZone_Teleport(e, it, -1, 0); // NOT triggering targets by this!
 			}
@@ -825,7 +824,7 @@ void WarpZone_StartFrame()
 			{
 				entity ent = Teleport_Find(it.origin + it.mins, it.origin + it.maxs);
 				if (ent)
-				if (WarpZoneLib_ExactTrigger_Touch(ent, it))
+				if (WarpZoneLib_ExactTrigger_Touch(ent, it, false))
 					Simple_TeleportPlayer(ent, it); // NOT triggering targets by this!
 			}
 		}
diff --git a/qcsrc/lib/warpzone/util_server.qh b/qcsrc/lib/warpzone/util_server.qh
index 515f8db2e7..c4799c662c 100644
--- a/qcsrc/lib/warpzone/util_server.qh
+++ b/qcsrc/lib/warpzone/util_server.qh
@@ -1,7 +1,6 @@
 #pragma once
 
 bool WarpZoneLib_MoveOutOfSolid(entity e);
-bool WarpZoneLib_ExactTrigger_Touch(entity this, entity toucher);
 #ifdef SVQC
 void WarpZoneLib_ExactTrigger_Init(entity this);
 #endif
-- 
2.39.5