From 5a63e22f563e3729516b25c16356bd252187e76a Mon Sep 17 00:00:00 2001
From: toaster <rollerorbital@gmail.com>
Date: Sat, 23 Nov 2019 14:43:36 +0000
Subject: [PATCH] It's not safe to store a file-scope pointer to a mobj_t*
 without having any mechanism to clear it between map reloadings, so move the
 Tailsbot "press jump now to fly!" indicator into `tails->target`.

Resolves #331, and likely also #330 and #309.
---
 src/b_bot.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/src/b_bot.c b/src/b_bot.c
index 12cdc36a0f..709a280b57 100644
--- a/src/b_bot.c
+++ b/src/b_bot.c
@@ -31,7 +31,6 @@ static boolean panic = false;
 static UINT8 flymode = 0;
 static boolean spinmode = false;
 static boolean thinkfly = false;
-static mobj_t *overlay;
 
 static inline void B_ResetAI(void)
 {
@@ -336,17 +335,20 @@ static inline void B_BuildTailsTiccmd(mobj_t *sonic, mobj_t *tails, ticcmd_t *cm
 	// Thinkfly overlay
 	if (thinkfly)
 	{
-		if (overlay == NULL)
+		if (!tails->target)
 		{
-			overlay = P_SpawnMobjFromMobj(tails, 0, 0, 0, MT_OVERLAY);
-			P_SetTarget(&overlay->target, tails);
-			P_SetMobjState(overlay, S_FLIGHTINDICATOR);
+			P_SetTarget(&tails->target, P_SpawnMobjFromMobj(tails, 0, 0, 0, MT_OVERLAY));
+			if (tails->target)
+			{
+				P_SetTarget(&tails->target->target, tails);
+				P_SetMobjState(tails->target, S_FLIGHTINDICATOR);
+			}
 		}
 	}
-	else if (overlay != NULL)
+	else if (tails->target && tails->target->type == MT_OVERLAY && tails->target->state == states+S_FLIGHTINDICATOR)
 	{
-		P_RemoveMobj(overlay);
-		overlay = NULL;
+		P_RemoveMobj(tails->target);
+		P_SetTarget(&tails->target, NULL);
 	}
 
 	// Turn the virtual keypresses into ticcmd_t.
-- 
GitLab