[iortcw] 38/89: All: Split G_AddRandomBot into multiple functions

Simon McVittie smcv at debian.org
Fri Sep 8 10:44:22 UTC 2017


This is an automated email from the git hooks/post-receive script.

smcv pushed a commit to tag 1.51b
in repository iortcw.

commit 7f04fb1f7b199494da6f9a9d7d87ab1f5162ab7c
Author: MAN-AT-ARMS <M4N4T4RMS at gmail.com>
Date:   Wed Jul 19 11:29:36 2017 -0400

    All: Split G_AddRandomBot into multiple functions
---
 MP/code/game/g_bot.c | 127 ++++++++++++++++++++++++++++++---------------------
 SP/code/game/g_bot.c | 127 ++++++++++++++++++++++++++++++---------------------
 2 files changed, 152 insertions(+), 102 deletions(-)

diff --git a/MP/code/game/g_bot.c b/MP/code/game/g_bot.c
index 1505c1e..524b4b8 100644
--- a/MP/code/game/g_bot.c
+++ b/MP/code/game/g_bot.c
@@ -151,72 +151,97 @@ static void PlayerIntroSound( const char *modelAndSkin ) {
 
 /*
 ===============
-G_AddRandomBot
+G_CountBotPlayersByName
+
+Returns number of bots with name on specified team or whole server if team is -1.
 ===============
 */
-void G_AddRandomBot( int team ) {
-	int i, n, num, skill;
-	char    *value, netname[36], *teamstr;
+int G_CountBotPlayersByName( const char *name, int team ) {
+	int			i, num;
 	gclient_t   *cl;
 
 	num = 0;
-	for ( n = 0; n < g_numBots ; n++ ) {
-		value = Info_ValueForKey( g_botInfos[n], "name" );
-		//
-		for ( i = 0 ; i < g_maxclients.integer ; i++ ) {
-			cl = level.clients + i;
-			if ( cl->pers.connected != CON_CONNECTED ) {
-				continue;
-			}
-			if ( !(g_entities[i].r.svFlags & SVF_BOT) ) {
-				continue;
-			}
-			if ( team >= 0 && cl->sess.sessionTeam != team ) {
-				continue;
-			}
-			if ( !Q_stricmp( value, cl->pers.netname ) ) {
-				break;
-			}
+	for ( i=0 ; i< g_maxclients.integer ; i++ ) {
+		cl = level.clients + i;
+		if ( cl->pers.connected != CON_CONNECTED ) {
+			continue;
 		}
-		if ( i >= g_maxclients.integer ) {
-			num++;
+		if ( !(g_entities[i].r.svFlags & SVF_BOT) ) {
+			continue;
+		}
+		if ( team >= 0 && cl->sess.sessionTeam != team ) {
+			continue;
 		}
+		if ( name && Q_stricmp( name, cl->pers.netname ) ) {
+			continue;
+		}
+		num++;
 	}
-	num = random() * num;
+	return num;
+}
+
+/*
+===============
+G_SelectRandomBotInfo
+
+Get random unused bot info on team or whole server if team is -1.
+===============
+*/
+int G_SelectRandomBotInfo( int team ) {
+	int		selection[MAX_BOTS];
+	int		n, num;
+	char	*value;
+
+	num = 0;
 	for ( n = 0; n < g_numBots ; n++ ) {
 		value = Info_ValueForKey( g_botInfos[n], "name" );
 		//
-		for ( i = 0 ; i < g_maxclients.integer ; i++ ) {
-			cl = level.clients + i;
-			if ( cl->pers.connected != CON_CONNECTED ) {
-				continue;
-			}
-			if ( !(g_entities[i].r.svFlags & SVF_BOT) ) {
-				continue;
-			}
-			if ( team >= 0 && cl->sess.sessionTeam != team ) {
-				continue;
-			}
-			if ( !Q_stricmp( value, cl->pers.netname ) ) {
+		if ( G_CountBotPlayersByName( value, team ) == 0 ) {
+			selection[num++] = n;
+
+			if ( num == MAX_BOTS ) {
 				break;
 			}
 		}
-		if ( i >= g_maxclients.integer ) {
-			num--;
-			if ( num <= 0 ) {
-				skill = trap_Cvar_VariableIntegerValue( "g_spSkill" );
-				if ( team == TEAM_RED ) {
-					teamstr = "red";
-				} else if ( team == TEAM_BLUE ) {
-					teamstr = "blue";
-				} else { teamstr = "";}
-				Q_strncpyz(netname, value, sizeof(netname));
-				Q_CleanStr( netname );
-				trap_SendConsoleCommand( EXEC_INSERT, va( "addbot %s %i %s %i\n", netname, skill, teamstr, 0 ) );
-				return;
-			}
-		}
 	}
+
+	if ( num > 0 ) {
+		num = random() * ( num - 1 );
+		return selection[num];
+	}
+
+	return -1;
+}
+
+/*
+===============
+G_AddRandomBot
+===============
+*/
+void G_AddRandomBot( int team ) {
+	int		n, skill;
+	char	*value, netname[36], *teamstr;
+
+	n = G_SelectRandomBotInfo( team );
+
+	if ( n < 0 ) {
+		// all bot types are in use on team
+		return;
+	}
+
+	value = Info_ValueForKey( g_botInfos[n], "name" );
+
+	skill = trap_Cvar_VariableIntegerValue( "g_spSkill" );
+	if (team == TEAM_RED) {
+		teamstr = "red";
+	} else if (team == TEAM_BLUE) {
+		teamstr = "blue";
+	} else {
+		teamstr = "";
+	}
+	Q_strncpyz( netname, value, sizeof( netname ) );
+	Q_CleanStr( netname );
+	trap_SendConsoleCommand( EXEC_INSERT, va( "addbot %s %i %s %i\n", netname, skill, teamstr, 0 ) );
 }
 
 /*
diff --git a/SP/code/game/g_bot.c b/SP/code/game/g_bot.c
index d46764e..814cb7a 100644
--- a/SP/code/game/g_bot.c
+++ b/SP/code/game/g_bot.c
@@ -152,72 +152,97 @@ static void PlayerIntroSound( const char *modelAndSkin ) {
 
 /*
 ===============
-G_AddRandomBot
+G_CountBotPlayersByName
+
+Returns number of bots with name on specified team or whole server if team is -1.
 ===============
 */
-void G_AddRandomBot( int team ) {
-	int i, n, num, skill;
-	char    *value, netname[36], *teamstr;
+int G_CountBotPlayersByName( const char *name, int team ) {
+	int			i, num;
 	gclient_t   *cl;
 
 	num = 0;
-	for ( n = 0; n < g_numBots ; n++ ) {
-		value = Info_ValueForKey( g_botInfos[n], "name" );
-		//
-		for ( i = 0 ; i < g_maxclients.integer ; i++ ) {
-			cl = level.clients + i;
-			if ( cl->pers.connected != CON_CONNECTED ) {
-				continue;
-			}
-			if ( !(g_entities[i].r.svFlags & SVF_BOT) ) {
-				continue;
-			}
-			if ( team >= 0 && cl->sess.sessionTeam != team ) {
-				continue;
-			}
-			if ( !Q_stricmp( value, cl->pers.netname ) ) {
-				break;
-			}
+	for ( i=0 ; i< g_maxclients.integer ; i++ ) {
+		cl = level.clients + i;
+		if ( cl->pers.connected != CON_CONNECTED ) {
+			continue;
 		}
-		if ( i >= g_maxclients.integer ) {
-			num++;
+		if ( !(g_entities[i].r.svFlags & SVF_BOT) ) {
+			continue;
+		}
+		if ( team >= 0 && cl->sess.sessionTeam != team ) {
+			continue;
 		}
+		if ( name && Q_stricmp( name, cl->pers.netname ) ) {
+			continue;
+		}
+		num++;
 	}
-	num = random() * num;
+	return num;
+}
+
+/*
+===============
+G_SelectRandomBotInfo
+
+Get random unused bot info on team or whole server if team is -1.
+===============
+*/
+int G_SelectRandomBotInfo( int team ) {
+	int		selection[MAX_BOTS];
+	int		n, num;
+	char	*value;
+
+	num = 0;
 	for ( n = 0; n < g_numBots ; n++ ) {
 		value = Info_ValueForKey( g_botInfos[n], "name" );
 		//
-		for ( i = 0 ; i < g_maxclients.integer ; i++ ) {
-			cl = level.clients + i;
-			if ( cl->pers.connected != CON_CONNECTED ) {
-				continue;
-			}
-			if ( !(g_entities[i].r.svFlags & SVF_BOT) ) {
-				continue;
-			}
-			if ( team >= 0 && cl->sess.sessionTeam != team ) {
-				continue;
-			}
-			if ( !Q_stricmp( value, cl->pers.netname ) ) {
+		if ( G_CountBotPlayersByName( value, team ) == 0 ) {
+			selection[num++] = n;
+
+			if ( num == MAX_BOTS ) {
 				break;
 			}
 		}
-		if ( i >= g_maxclients.integer ) {
-			num--;
-			if ( num <= 0 ) {
-				skill = trap_Cvar_VariableIntegerValue( "g_spSkill" );
-				if ( team == TEAM_RED ) {
-					teamstr = "red";
-				} else if ( team == TEAM_BLUE ) {
-					teamstr = "blue";
-				} else { teamstr = "";}
-				Q_strncpyz(netname, value, sizeof(netname));
-				Q_CleanStr( netname );
-				trap_SendConsoleCommand( EXEC_INSERT, va( "addbot %s %i %s %i\n", netname, skill, teamstr, 0 ) );
-				return;
-			}
-		}
 	}
+
+	if ( num > 0 ) {
+		num = random() * ( num - 1 );
+		return selection[num];
+	}
+
+	return -1;
+}
+
+/*
+===============
+G_AddRandomBot
+===============
+*/
+void G_AddRandomBot( int team ) {
+	int		n, skill;
+	char	*value, netname[36], *teamstr;
+
+	n = G_SelectRandomBotInfo( team );
+
+	if ( n < 0 ) {
+		// all bot types are in use on team
+		return;
+	}
+
+	value = Info_ValueForKey( g_botInfos[n], "name" );
+
+	skill = trap_Cvar_VariableIntegerValue( "g_spSkill" );
+	if (team == TEAM_RED) {
+		teamstr = "red";
+	} else if (team == TEAM_BLUE) {
+		teamstr = "blue";
+	} else {
+		teamstr = "";
+	}
+	Q_strncpyz( netname, value, sizeof( netname ) );
+	Q_CleanStr( netname );
+	trap_SendConsoleCommand( EXEC_INSERT, va( "addbot %s %i %s %i\n", netname, skill, teamstr, 0 ) );
 }
 
 /*

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-games/iortcw.git



More information about the Pkg-games-commits mailing list