From 5865c4dcca50bd4182a6a444c7e25f54d4b41dfa Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sat, 11 Apr 2020 12:11:22 +0200 Subject: [PATCH] - use FString to manage strings in bot code. --- src/p_saveg.cpp | 2 +- src/playsim/bots/b_bot.cpp | 4 +-- src/playsim/bots/b_bot.h | 4 +-- src/playsim/bots/b_game.cpp | 57 ++++++++++++------------------------- 4 files changed, 23 insertions(+), 44 deletions(-) diff --git a/src/p_saveg.cpp b/src/p_saveg.cpp index add4ce024..2ff6b9bf9 100644 --- a/src/p_saveg.cpp +++ b/src/p_saveg.cpp @@ -847,7 +847,7 @@ void FLevelLocals::CopyPlayer(player_t *dst, player_t *src, const char *name) if (dst->Bot != nullptr) { botinfo_t *thebot = BotInfo.botinfo; - while (thebot && stricmp(name, thebot->name)) + while (thebot && thebot->Name.CompareNoCase(name)) { thebot = thebot->next; } diff --git a/src/playsim/bots/b_bot.cpp b/src/playsim/bots/b_bot.cpp index bb15fc8bc..6ef5bb7ae 100644 --- a/src/playsim/bots/b_bot.cpp +++ b/src/playsim/bots/b_bot.cpp @@ -186,7 +186,7 @@ void FCajunMaster::ClearPlayer (int i, bool keepTeam) players[i].mo = nullptr; } botinfo_t *bot = botinfo; - while (bot && stricmp (players[i].userinfo.GetName(), bot->name)) + while (bot && stricmp (players[i].userinfo.GetName(), bot->Name.GetChars())) bot = bot->next; if (bot) { @@ -237,7 +237,7 @@ CCMD (listbots) while (thebot) { - Printf ("%s%s\n", thebot->name, thebot->inuse == BOTINUSE_Yes ? " (active)" : ""); + Printf ("%s%s\n", thebot->Name.GetChars(), thebot->inuse == BOTINUSE_Yes ? " (active)" : ""); thebot = thebot->next; count++; } diff --git a/src/playsim/bots/b_bot.h b/src/playsim/bots/b_bot.h index a4f632eae..2125597ff 100644 --- a/src/playsim/bots/b_bot.h +++ b/src/playsim/bots/b_bot.h @@ -72,8 +72,8 @@ enum struct botinfo_t { botinfo_t *next; - char *name; - char *info; + FString Name; + FString Info; botskill_t skill; int inuse; int lastteam; diff --git a/src/playsim/bots/b_game.cpp b/src/playsim/bots/b_game.cpp index ad24d7bbd..d770f278e 100644 --- a/src/playsim/bots/b_game.cpp +++ b/src/playsim/bots/b_game.cpp @@ -237,7 +237,7 @@ bool FCajunMaster::SpawnBot (const char *name, int color) if (name) { // Check if exist or already in the game. - while (thebot && stricmp (name, thebot->name)) + while (thebot && thebot->Name.CompareNoCase(name)) { botshift++; thebot = thebot->next; @@ -296,16 +296,14 @@ bool FCajunMaster::SpawnBot (const char *name, int color) Net_WriteByte (botshift); { //Set color. - char concat[512]; - strcpy (concat, thebot->info); + FString concat = thebot->Info; if (color == NOCOLOR && bot_next_color < NOCOLOR && bot_next_color >= 0) { - strcat (concat, colors[bot_next_color]); + concat << colors[bot_next_color]; } if (TeamLibrary.IsValidTeam (thebot->lastteam)) { // Keep the bot on the same team when switching levels - mysnprintf (concat + strlen(concat), countof(concat) - strlen(concat), - "\\team\\%d\n", thebot->lastteam); + concat.AppendFormat("\\team\\%d\n", thebot->lastteam); } Net_WriteString (concat); } @@ -453,26 +451,9 @@ void FCajunMaster::RemoveAllBots (FLevelLocals *Level, bool fromlist) // ??? any other valid userinfo strings can go here //} -static void appendinfo (char *&front, const char *back) +static void appendinfo (FString &front, const char *back) { - char *newstr; - - if (front) - { - size_t newlen = strlen (front) + strlen (back) + 2; - newstr = new char[newlen]; - strcpy (newstr, front); - delete[] front; - } - else - { - size_t newlen = strlen (back) + 2; - newstr = new char[newlen]; - newstr[0] = 0; - } - strcat (newstr, "\\"); - strcat (newstr, back); - front = newstr; + front << "\\" << back; } void FCajunMaster::ForgetBots () @@ -482,8 +463,6 @@ void FCajunMaster::ForgetBots () while (thebot) { botinfo_t *next = thebot->next; - delete[] thebot->name; - delete[] thebot->info; delete thebot; thebot = next; } @@ -523,7 +502,7 @@ bool FCajunMaster::LoadBots () memset (newinfo, 0, sizeof(*newinfo)); - newinfo->info = copystring ("\\autoaim\\0\\movebob\\.25"); + newinfo->Info = "\\autoaim\\0\\movebob\\.25"; for (;;) { @@ -535,9 +514,9 @@ bool FCajunMaster::LoadBots () { case BOTCFG_NAME: sc.MustGetString (); - appendinfo (newinfo->info, "name"); - appendinfo (newinfo->info, sc.String); - newinfo->name = copystring (sc.String); + appendinfo (newinfo->Info, "name"); + appendinfo (newinfo->Info, sc.String); + newinfo->Name = sc.String; break; case BOTCFG_AIMING: @@ -586,9 +565,9 @@ bool FCajunMaster::LoadBots () } } } - appendinfo (newinfo->info, "team"); + appendinfo (newinfo->Info, "team"); mysnprintf (teamstr, countof(teamstr), "%d", teamnum); - appendinfo (newinfo->info, teamstr); + appendinfo (newinfo->Info, teamstr); gotteam = true; break; } @@ -598,21 +577,21 @@ bool FCajunMaster::LoadBots () { gotclass = true; } - appendinfo (newinfo->info, sc.String); + appendinfo (newinfo->Info, sc.String); sc.MustGetString (); - appendinfo (newinfo->info, sc.String); + appendinfo (newinfo->Info, sc.String); break; } } if (!gotclass) { // Bots that don't specify a class get a random one - appendinfo (newinfo->info, "playerclass"); - appendinfo (newinfo->info, "random"); + appendinfo (newinfo->Info, "playerclass"); + appendinfo (newinfo->Info, "random"); } if (!gotteam) { // Same for bot teams - appendinfo (newinfo->info, "team"); - appendinfo (newinfo->info, "255"); + appendinfo (newinfo->Info, "team"); + appendinfo (newinfo->Info, "255"); } newinfo->next = botinfo; newinfo->lastteam = TEAM_NONE;