From 1a5995968c4c5630d00516f9d9e909aba6c8a4f9 Mon Sep 17 00:00:00 2001
From: lachablock <lachsrb2@gmail.com>
Date: Wed, 19 Jan 2022 00:01:26 +1100
Subject: [PATCH] Revert server-sidedness of bots

---
 src/b_bot.c         | 15 +++++++------
 src/d_clisrv.c      | 31 --------------------------
 src/d_ticcmd.h      |  9 --------
 src/deh_tables.c    |  5 -----
 src/g_demo.c        | 12 ----------
 src/g_game.c        | 53 ++++++++++++++++++++++++++-------------------
 src/lua_baselib.c   |  3 ---
 src/lua_playerlib.c |  4 ----
 src/p_saveg.c       | 43 ++++++++++++++----------------------
 src/p_user.c        | 10 +++------
 10 files changed, 58 insertions(+), 127 deletions(-)

diff --git a/src/b_bot.c b/src/b_bot.c
index 4fdfc8714d..82075eb8ef 100644
--- a/src/b_bot.c
+++ b/src/b_bot.c
@@ -17,6 +17,7 @@
 #include "p_local.h"
 #include "b_bot.h"
 #include "lua_hook.h"
+#include "i_system.h" // I_BaseTiccmd
 
 void B_UpdateBotleader(player_t *player)
 {
@@ -176,6 +177,7 @@ static void B_BuildTailsTiccmd(mobj_t *sonic, mobj_t *tails, ticcmd_t *cmd)
 			{
 				jump = true;
 				mem->thinkstate = AI_FLYSTANDBY;
+				bot->pflags |= PF_CANCARRY;
 			}
 		}
 
@@ -187,10 +189,7 @@ static void B_BuildTailsTiccmd(mobj_t *sonic, mobj_t *tails, ticcmd_t *cmd)
 			&& P_IsObjectOnGround(sonic) && P_IsObjectOnGround(tails)
 			&& !(player->pflags & PF_STASIS)
 			&& bot->charability == CA_FLY)
-		{
-			mem->thinkstate = AI_THINKFLY;
-			cmd->flags |= TCF_FLIGHTINDICATOR;
-		}
+				mem->thinkstate = AI_THINKFLY;
 		else if (mem->thinkstate == AI_THINKFLY)
 			mem->thinkstate = AI_FOLLOW;
 
@@ -211,8 +210,6 @@ static void B_BuildTailsTiccmd(mobj_t *sonic, mobj_t *tails, ticcmd_t *cmd)
 			// Abort if the player moves away or spins
 			if (dist > followthres || player->dashspeed)
 				mem->thinkstate = AI_FOLLOW;
-			else
-				cmd->flags |= TCF_SETCARRY;
 		}
 		// Read player inputs while carrying
 		else if (mem->thinkstate == AI_FLYCARRY)
@@ -374,6 +371,8 @@ static void B_BuildTailsTiccmd(mobj_t *sonic, mobj_t *tails, ticcmd_t *cmd)
 
 void B_BuildTiccmd(player_t *player, ticcmd_t *cmd)
 {
+	G_CopyTiccmd(cmd, I_BaseTiccmd(), 1); // empty, or external driver
+
 	// Can't build a ticcmd if we aren't spawned...
 	if (!player->mo)
 		return;
@@ -393,6 +392,7 @@ void B_BuildTiccmd(player_t *player, ticcmd_t *cmd)
 		return;
 
 	// Make sure we have a valid main character to follow
+	B_UpdateBotleader(player);
 	if (!player->botleader)
 		return;
 
@@ -594,12 +594,13 @@ void B_RespawnBot(INT32 playernum)
 void B_HandleFlightIndicator(player_t *player)
 {
 	mobj_t *tails = player->mo;
+	botmem_t *mem = &player->botmem;
 	boolean shouldExist;
 
 	if (!tails)
 		return;
 
-	shouldExist = (player->cmd.flags & TCF_FLIGHTINDICATOR) && player->botleader
+	shouldExist = (mem->thinkstate == AI_THINKFLY) && player->botleader
 		&& player->bot == BOT_2PAI && player->playerstate == PST_LIVE;
 
 	// check whether the indicator doesn't exist
diff --git a/src/d_clisrv.c b/src/d_clisrv.c
index c70532494c..78a3ebe6cb 100644
--- a/src/d_clisrv.c
+++ b/src/d_clisrv.c
@@ -46,7 +46,6 @@
 #include "lua_libs.h"
 #include "md5.h"
 #include "m_perfstats.h"
-#include "b_bot.h" // B_BuildTiccmd
 
 #ifndef NONET
 // cl loading screen
@@ -5167,25 +5166,6 @@ static void Local_Maketic(INT32 realtics)
 	localcmds2.angleturn |= TICCMD_RECEIVED;
 }
 
-static void SV_MakeBotTics(void)
-{
-	UINT8 i;
-	for (i = 0; i < MAXPLAYERS; i++)
-	{
-		if (!playeringame[i])
-			continue;
-
-		if (players[i].bot == BOT_2PAI || players[i].bot == BOT_MPAI)
-		{
-			ticcmd_t *cmd = &netcmds[maketic % BACKUPTICS][i];
-
-			G_CopyTiccmd(cmd, I_BaseTiccmd(), 1); // empty, or external driver
-			B_BuildTiccmd(&players[i], cmd);
-			cmd->angleturn |= TICCMD_RECEIVED;
-		}
-	}
-}
-
 // create missed tic
 static void SV_Maketic(void)
 {
@@ -5429,15 +5409,6 @@ void NetUpdate(void)
 	if (client)
 		maketic = neededtic;
 
-	// update players' lastbuttons so they can be used in ticcmd generation
-	for (i = 0; i < MAXPLAYERS; i++)
-	{
-		if (!playeringame[i])
-			continue;
-
-		players[i].lastbuttons = players[i].cmd.buttons;
-	}
-
 	Local_Maketic(realtics); // make local tic, and call menu?
 
 	if (server)
@@ -5481,8 +5452,6 @@ void NetUpdate(void)
 						Net_ConnectionTimeout(i);
 				}
 
-			SV_MakeBotTics();
-
 			// Don't erase tics not acknowledged
 			counts = realtics;
 
diff --git a/src/d_ticcmd.h b/src/d_ticcmd.h
index 480fbe2d4c..182b30e6ae 100644
--- a/src/d_ticcmd.h
+++ b/src/d_ticcmd.h
@@ -45,14 +45,6 @@ typedef enum
 	BT_CUSTOM3    = 1<<15,
 } buttoncode_t;
 
-// ticcmd flags
-typedef enum
-{
-	TCF_FLIGHTINDICATOR = 1 << 0, // show flight indicator
-	TCF_SETCARRY        = 1 << 1, // set PF_CARRY upon activating flight
-	// free up to and including 1 << 7
-} ticcmdflag_t;
-
 // The data sampled per tick (single player)
 // and transmitted to other peers (multiplayer).
 // Mainly movements/button commands per game tick,
@@ -74,7 +66,6 @@ typedef struct
 	INT16 aiming; // vertical aiming, see G_BuildTicCmd
 	UINT16 buttons;
 	UINT8 latency; // Netgames: how many tics ago was this ticcmd generated from this player's end?
-	UINT8 flags; // miscellaneous info
 } ATTRPACK ticcmd_t;
 
 #if defined(_MSC_VER)
diff --git a/src/deh_tables.c b/src/deh_tables.c
index b150aa51d0..cfc98f631e 100644
--- a/src/deh_tables.c
+++ b/src/deh_tables.c
@@ -5345,11 +5345,6 @@ struct int_const_s const INT_CONST[] = {
 	{"BT_CUSTOM2",BT_CUSTOM2}, // Lua customizable
 	{"BT_CUSTOM3",BT_CUSTOM3}, // Lua customizable
 
-	// Ticcmd flags (ticcmdflag_t)
-	// (maybe move these into their own table in the future but I cba when there's only 2 LOL)
-	{"TCF_FLIGHTINDICATOR", TCF_FLIGHTINDICATOR},
-	{"TCF_SETCARRY", TCF_SETCARRY},
-
 	// Lua command registration flags
 	{"COM_ADMIN",COM_ADMIN},
 	{"COM_SPLITSCREEN",COM_SPLITSCREEN},
diff --git a/src/g_demo.c b/src/g_demo.c
index 2e2f6d56a6..c97dbcf9ee 100644
--- a/src/g_demo.c
+++ b/src/g_demo.c
@@ -110,7 +110,6 @@ demoghost *ghosts = NULL;
 #define ZT_BUTTONS 0x08
 #define ZT_AIMING  0x10
 #define ZT_LATENCY 0x20
-#define ZT_FLAGS   0x40
 #define DEMOMARKER 0x80 // demoend
 #define METALDEATH 0x44
 #define METALSNICE 0x69
@@ -185,8 +184,6 @@ void G_ReadDemoTiccmd(ticcmd_t *cmd, INT32 playernum)
 		oldcmd.aiming = READINT16(demo_p);
 	if (ziptic & ZT_LATENCY)
 		oldcmd.latency = READUINT8(demo_p);
-	if (ziptic & ZT_FLAGS)
-		oldcmd.flags = READUINT8(demo_p);
 
 	G_CopyTiccmd(cmd, &oldcmd, 1);
 	players[playernum].angleturn = cmd->angleturn;
@@ -251,13 +248,6 @@ void G_WriteDemoTiccmd(ticcmd_t *cmd, INT32 playernum)
 		ziptic |= ZT_LATENCY;
 	}
 
-	if (cmd->flags != oldcmd.flags)
-	{
-		WRITEUINT8(demo_p, cmd->flags);
-		oldcmd.flags = cmd->flags;
-		ziptic |= ZT_FLAGS;
-	}
-
 	*ziptic_p = ziptic;
 
 	// attention here for the ticcmd size!
@@ -701,8 +691,6 @@ void G_GhostTicker(void)
 			g->p += 2;
 		if (ziptic & ZT_LATENCY)
 			g->p++;
-		if (ziptic & ZT_FLAGS)
-			g->p++;
 
 		// Grab ghost data.
 		ziptic = READUINT8(g->p);
diff --git a/src/g_game.c b/src/g_game.c
index eae22a0d4a..c12b7e5223 100644
--- a/src/g_game.c
+++ b/src/g_game.c
@@ -1546,7 +1546,7 @@ void G_BuildTiccmd(ticcmd_t *cmd, INT32 realtics, UINT8 ssplayer)
 	cmd->forwardmove = (SINT8)(cmd->forwardmove + forward);
 	cmd->sidemove = (SINT8)(cmd->sidemove + side);
 
-	// Note: Majority of botstuffs are handled in G_Ticker and NetUpdate now.
+	// Note: Majority of botstuffs are handled in G_Ticker now.
 	if (player->bot == BOT_2PAI
 		&& !player->powers[pw_tailsfly]
 		&& (cmd->forwardmove || cmd->sidemove || cmd->buttons))
@@ -1710,7 +1710,6 @@ ticcmd_t *G_MoveTiccmd(ticcmd_t* dest, const ticcmd_t* src, const size_t n)
 		dest[i].aiming = (INT16)SHORT(src[i].aiming);
 		dest[i].buttons = (UINT16)SHORT(src[i].buttons);
 		dest[i].latency = src[i].latency;
-		dest[i].flags = src[i].flags;
 	}
 	return dest;
 }
@@ -2313,34 +2312,44 @@ void G_Ticker(boolean run)
 
 	buf = gametic % BACKUPTICS;
 
+	// Generate ticcmds for bots FIRST, then copy received ticcmds for players.
+	// This emulates pre-2.2.10 behaviour where the bot referenced their leader's last copied ticcmd,
+	// which is desirable because P_PlayerThink can override inputs (e.g. while PF_STASIS is applied or in a waterslide),
+	// and the bot AI needs to respect that.
+#define ISHUMAN (players[i].bot == BOT_NONE || players[i].bot == BOT_2PHUMAN)
 	for (i = 0; i < MAXPLAYERS; i++)
 	{
-		if (playeringame[i])
+		if (playeringame[i] && !ISHUMAN) // Less work is required if we're building a bot ticcmd.
 		{
-			G_CopyTiccmd(&players[i].cmd, &netcmds[buf][i], 1);
+			players[i].lastbuttons = players[i].cmd.buttons; // Save last frame's button readings
+			B_BuildTiccmd(&players[i], &players[i].cmd);
 
-			if (players[i].bot == BOT_NONE || players[i].bot == BOT_2PHUMAN)
-			{
-				// Use the leveltime sent in the player's ticcmd to determine control lag
-				players[i].cmd.latency = min(((leveltime & 0xFF) - players[i].cmd.latency) & 0xFF, MAXPREDICTTICS-1);
+			// Since bot TicCmd is pre-determined for both the client and server, the latency and packet checks are simplified.
+			players[i].cmd.latency = 0;
+			P_SetPlayerAngle(&players[i], players[i].cmd.angleturn << 16);
+		}
+	}
 
-				// Do angle adjustments.
+	for (i = 0; i < MAXPLAYERS; i++)
+	{
+		if (playeringame[i] && ISHUMAN)
+		{
+			players[i].lastbuttons = players[i].cmd.buttons; // Save last frame's button readings
+			G_CopyTiccmd(&players[i].cmd, &netcmds[buf][i], 1);
 
-				players[i].angleturn += players[i].cmd.angleturn - players[i].oldrelangleturn;
-				players[i].oldrelangleturn = players[i].cmd.angleturn;
-				if (P_ControlStyle(&players[i]) == CS_LMAOGALOG)
-					P_ForceLocalAngle(&players[i], players[i].angleturn << 16);
-				else
-					players[i].cmd.angleturn = (players[i].angleturn & ~TICCMD_RECEIVED) | (players[i].cmd.angleturn & TICCMD_RECEIVED);
-			}
-			else // Less work is required if we're building a bot ticcmd.
-			{
-				// Since bot TicCmd is pre-determined for both the client and server, the latency and packet checks are simplified.
-				players[i].cmd.latency = 0;
-				P_SetPlayerAngle(&players[i], players[i].cmd.angleturn << 16);
-			}
+			// Use the leveltime sent in the player's ticcmd to determine control lag
+			players[i].cmd.latency = min(((leveltime & 0xFF) - players[i].cmd.latency) & 0xFF, MAXPREDICTTICS-1);
+
+			// Do angle adjustments.
+			players[i].angleturn += players[i].cmd.angleturn - players[i].oldrelangleturn;
+			players[i].oldrelangleturn = players[i].cmd.angleturn;
+			if (P_ControlStyle(&players[i]) == CS_LMAOGALOG)
+				P_ForceLocalAngle(&players[i], players[i].angleturn << 16);
+			else
+				players[i].cmd.angleturn = (players[i].angleturn & ~TICCMD_RECEIVED) | (players[i].cmd.angleturn & TICCMD_RECEIVED);
 		}
 	}
+#undef ISHUMAN
 
 	// do main actions
 	switch (gamestate)
diff --git a/src/lua_baselib.c b/src/lua_baselib.c
index e0d09dee9c..12ad4fee05 100644
--- a/src/lua_baselib.c
+++ b/src/lua_baselib.c
@@ -32,7 +32,6 @@
 #include "b_bot.h" // B_UpdateBotleader
 #include "d_clisrv.h" // CL_RemovePlayer
 #include "i_system.h" // I_GetPreciseTime, I_PreciseToMicros
-#include "i_net.h" // doomcom
 
 #include "lua_script.h"
 #include "lua_libs.h"
@@ -3464,8 +3463,6 @@ static int lib_gAddPlayer(lua_State *L)
 
 	playeringame[newplayernum] = true;
 	G_AddPlayer(newplayernum);
-	if (newplayernum+1 > doomcom->numslots)
-		doomcom->numslots = (INT16)(newplayernum+1);
 	newplayer = &players[newplayernum];
 
 	newplayer->jointime = 0;
diff --git a/src/lua_playerlib.c b/src/lua_playerlib.c
index edd1351e80..1c634da45e 100644
--- a/src/lua_playerlib.c
+++ b/src/lua_playerlib.c
@@ -833,8 +833,6 @@ static int ticcmd_get(lua_State *L)
 		lua_pushinteger(L, cmd->buttons);
 	else if (fastcmp(field,"latency"))
 		lua_pushinteger(L, cmd->latency);
-	else if (fastcmp(field,"flags"))
-		lua_pushinteger(L, cmd->flags);
 	else
 		return NOFIELD;
 
@@ -863,8 +861,6 @@ static int ticcmd_set(lua_State *L)
 		cmd->buttons = (UINT16)luaL_checkinteger(L, 3);
 	else if (fastcmp(field,"latency"))
 		return NOSET;
-	else if (fastcmp(field,"flags"))
-		cmd->flags = (UINT8)luaL_checkinteger(L, 3);
 	else
 		return NOFIELD;
 
diff --git a/src/p_saveg.c b/src/p_saveg.c
index f9938763a7..722340f41f 100644
--- a/src/p_saveg.c
+++ b/src/p_saveg.c
@@ -51,15 +51,14 @@ UINT8 *save_p;
 // than an UINT16
 typedef enum
 {
-//	RFLAGPOINT = 0x001,
-//	BFLAGPOINT = 0x002,
-	CAPSULE    = 0x004,
-	AWAYVIEW   = 0x008,
-	FIRSTAXIS  = 0x010,
-	SECONDAXIS = 0x020,
-	FOLLOW     = 0x040,
-	DRONE      = 0x080,
-	BOTLEADER  = 0x100,
+//	RFLAGPOINT = 0x01,
+//	BFLAGPOINT = 0x02,
+	CAPSULE    = 0x04,
+	AWAYVIEW   = 0x08,
+	FIRSTAXIS  = 0x10,
+	SECONDAXIS = 0x20,
+	FOLLOW     = 0x40,
+	DRONE      = 0x80,
 } player_saveflags;
 
 static inline void P_ArchivePlayer(void)
@@ -198,11 +197,10 @@ static void P_NetArchivePlayers(void)
 		// Bots //
 		//////////
 		WRITEUINT8(save_p, players[i].bot);
-		// We no longer need to sync these since ticcmds are generated only by the server
-		//WRITEUINT8(save_p, players[i].botmem.lastForward);
-		//WRITEUINT8(save_p, players[i].botmem.lastBlocked);
-		//WRITEUINT8(save_p, players[i].botmem.catchup_tics);
-		//WRITEUINT8(save_p, players[i].botmem.thinkstate);
+		WRITEUINT8(save_p, players[i].botmem.lastForward);
+		WRITEUINT8(save_p, players[i].botmem.lastBlocked);
+		WRITEUINT8(save_p, players[i].botmem.catchup_tics);
+		WRITEUINT8(save_p, players[i].botmem.thinkstate);
 		WRITEUINT8(save_p, players[i].removing);
 		
 		WRITEUINT8(save_p, players[i].blocked);
@@ -295,9 +293,6 @@ static void P_NetArchivePlayers(void)
 		if (players[i].drone)
 			flags |= DRONE;
 
-		if (players[i].botleader)
-			flags |= BOTLEADER;
-
 		WRITEINT16(save_p, players[i].lastsidehit);
 		WRITEINT16(save_p, players[i].lastlinehit);
 
@@ -330,9 +325,6 @@ static void P_NetArchivePlayers(void)
 		if (flags & DRONE)
 			WRITEUINT32(save_p, players[i].drone->mobjnum);
 
-		if (flags & BOTLEADER)
-			WRITEUINT8(save_p, (UINT8)(players[i].botleader - players));
-
 		WRITEFIXED(save_p, players[i].camerascale);
 		WRITEFIXED(save_p, players[i].shieldscale);
 
@@ -433,10 +425,10 @@ static void P_NetUnArchivePlayers(void)
 		//////////
 		players[i].bot = READUINT8(save_p);
 		
-		//players[i].botmem.lastForward = READUINT8(save_p);
-		//players[i].botmem.lastBlocked = READUINT8(save_p);
-		//players[i].botmem.catchup_tics = READUINT8(save_p);
-		//players[i].botmem.thinkstate = READUINT8(save_p);
+		players[i].botmem.lastForward = READUINT8(save_p);
+		players[i].botmem.lastBlocked = READUINT8(save_p);
+		players[i].botmem.catchup_tics = READUINT8(save_p);
+		players[i].botmem.thinkstate = READUINT8(save_p);
 		players[i].removing = READUINT8(save_p);
 
 		players[i].blocked = READUINT8(save_p);
@@ -543,9 +535,6 @@ static void P_NetUnArchivePlayers(void)
 		if (flags & DRONE)
 			players[i].drone = (mobj_t *)(size_t)READUINT32(save_p);
 
-		if (flags & BOTLEADER)
-			players[i].botleader = &players[READUINT8(save_p)];
-
 		players[i].camerascale = READFIXED(save_p);
 		players[i].shieldscale = READFIXED(save_p);
 
diff --git a/src/p_user.c b/src/p_user.c
index ca7252584d..b3e555e2d2 100644
--- a/src/p_user.c
+++ b/src/p_user.c
@@ -5349,10 +5349,10 @@ static void P_DoJumpStuff(player_t *player, ticcmd_t *cmd)
 						player->powers[pw_tailsfly] = tailsflytics + 1; // Set the fly timer
 
 						player->pflags &= ~(PF_JUMPED|PF_NOJUMPDAMAGE|PF_SPINNING|PF_STARTDASH);
-						if ((player->bot != BOT_2PAI) || (cmd->flags & TCF_SETCARRY))
-							player->pflags |= (PF_THOKKED|PF_CANCARRY);
-						else
+						if (player->bot == BOT_2PAI)
 							player->pflags |= PF_THOKKED;
+						else
+							player->pflags |= (PF_THOKKED|PF_CANCARRY);
 					}
 					break;
 				case CA_GLIDEANDCLIMB:
@@ -11441,11 +11441,7 @@ void P_PlayerThink(player_t *player)
 			if (B_CheckRespawn(player))
 				player->playerstate = PST_REBORN;
 			else
-			{
-				if (player->bot == BOT_2PAI)
-					B_UpdateBotleader(player);
 				B_HandleFlightIndicator(player);
-			}
 		}
 		if (player->playerstate == PST_REBORN)
 		{
-- 
GitLab