- use FString to manage strings in bot code.

This commit is contained in:
Christoph Oelckers 2020-04-11 12:11:22 +02:00
parent 6bfdf904b9
commit 5865c4dcca
4 changed files with 23 additions and 44 deletions

View file

@ -847,7 +847,7 @@ void FLevelLocals::CopyPlayer(player_t *dst, player_t *src, const char *name)
if (dst->Bot != nullptr) if (dst->Bot != nullptr)
{ {
botinfo_t *thebot = BotInfo.botinfo; botinfo_t *thebot = BotInfo.botinfo;
while (thebot && stricmp(name, thebot->name)) while (thebot && thebot->Name.CompareNoCase(name))
{ {
thebot = thebot->next; thebot = thebot->next;
} }

View file

@ -186,7 +186,7 @@ void FCajunMaster::ClearPlayer (int i, bool keepTeam)
players[i].mo = nullptr; players[i].mo = nullptr;
} }
botinfo_t *bot = botinfo; 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; bot = bot->next;
if (bot) if (bot)
{ {
@ -237,7 +237,7 @@ CCMD (listbots)
while (thebot) 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; thebot = thebot->next;
count++; count++;
} }

View file

@ -72,8 +72,8 @@ enum
struct botinfo_t struct botinfo_t
{ {
botinfo_t *next; botinfo_t *next;
char *name; FString Name;
char *info; FString Info;
botskill_t skill; botskill_t skill;
int inuse; int inuse;
int lastteam; int lastteam;

View file

@ -237,7 +237,7 @@ bool FCajunMaster::SpawnBot (const char *name, int color)
if (name) if (name)
{ {
// Check if exist or already in the game. // Check if exist or already in the game.
while (thebot && stricmp (name, thebot->name)) while (thebot && thebot->Name.CompareNoCase(name))
{ {
botshift++; botshift++;
thebot = thebot->next; thebot = thebot->next;
@ -296,16 +296,14 @@ bool FCajunMaster::SpawnBot (const char *name, int color)
Net_WriteByte (botshift); Net_WriteByte (botshift);
{ {
//Set color. //Set color.
char concat[512]; FString concat = thebot->Info;
strcpy (concat, thebot->info);
if (color == NOCOLOR && bot_next_color < NOCOLOR && bot_next_color >= 0) 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)) if (TeamLibrary.IsValidTeam (thebot->lastteam))
{ // Keep the bot on the same team when switching levels { // Keep the bot on the same team when switching levels
mysnprintf (concat + strlen(concat), countof(concat) - strlen(concat), concat.AppendFormat("\\team\\%d\n", thebot->lastteam);
"\\team\\%d\n", thebot->lastteam);
} }
Net_WriteString (concat); Net_WriteString (concat);
} }
@ -453,26 +451,9 @@ void FCajunMaster::RemoveAllBots (FLevelLocals *Level, bool fromlist)
// ??? any other valid userinfo strings can go here // ??? 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; front << "\\" << back;
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;
} }
void FCajunMaster::ForgetBots () void FCajunMaster::ForgetBots ()
@ -482,8 +463,6 @@ void FCajunMaster::ForgetBots ()
while (thebot) while (thebot)
{ {
botinfo_t *next = thebot->next; botinfo_t *next = thebot->next;
delete[] thebot->name;
delete[] thebot->info;
delete thebot; delete thebot;
thebot = next; thebot = next;
} }
@ -523,7 +502,7 @@ bool FCajunMaster::LoadBots ()
memset (newinfo, 0, sizeof(*newinfo)); memset (newinfo, 0, sizeof(*newinfo));
newinfo->info = copystring ("\\autoaim\\0\\movebob\\.25"); newinfo->Info = "\\autoaim\\0\\movebob\\.25";
for (;;) for (;;)
{ {
@ -535,9 +514,9 @@ bool FCajunMaster::LoadBots ()
{ {
case BOTCFG_NAME: case BOTCFG_NAME:
sc.MustGetString (); sc.MustGetString ();
appendinfo (newinfo->info, "name"); appendinfo (newinfo->Info, "name");
appendinfo (newinfo->info, sc.String); appendinfo (newinfo->Info, sc.String);
newinfo->name = copystring (sc.String); newinfo->Name = sc.String;
break; break;
case BOTCFG_AIMING: case BOTCFG_AIMING:
@ -586,9 +565,9 @@ bool FCajunMaster::LoadBots ()
} }
} }
} }
appendinfo (newinfo->info, "team"); appendinfo (newinfo->Info, "team");
mysnprintf (teamstr, countof(teamstr), "%d", teamnum); mysnprintf (teamstr, countof(teamstr), "%d", teamnum);
appendinfo (newinfo->info, teamstr); appendinfo (newinfo->Info, teamstr);
gotteam = true; gotteam = true;
break; break;
} }
@ -598,21 +577,21 @@ bool FCajunMaster::LoadBots ()
{ {
gotclass = true; gotclass = true;
} }
appendinfo (newinfo->info, sc.String); appendinfo (newinfo->Info, sc.String);
sc.MustGetString (); sc.MustGetString ();
appendinfo (newinfo->info, sc.String); appendinfo (newinfo->Info, sc.String);
break; break;
} }
} }
if (!gotclass) if (!gotclass)
{ // Bots that don't specify a class get a random one { // Bots that don't specify a class get a random one
appendinfo (newinfo->info, "playerclass"); appendinfo (newinfo->Info, "playerclass");
appendinfo (newinfo->info, "random"); appendinfo (newinfo->Info, "random");
} }
if (!gotteam) if (!gotteam)
{ // Same for bot teams { // Same for bot teams
appendinfo (newinfo->info, "team"); appendinfo (newinfo->Info, "team");
appendinfo (newinfo->info, "255"); appendinfo (newinfo->Info, "255");
} }
newinfo->next = botinfo; newinfo->next = botinfo;
newinfo->lastteam = TEAM_NONE; newinfo->lastteam = TEAM_NONE;