From 2eec5839bdf8946c7b515163e244e61c55044566 Mon Sep 17 00:00:00 2001 From: jeefo Date: Thu, 15 Sep 2016 13:26:13 +0300 Subject: [PATCH] fixed prefixed names are not cleared upon bot disconnect --- .gitignore | 1 + include/core.h | 2 +- source/interface.cpp | 4 +--- source/manager.cpp | 29 +++++++++++++++++++---------- 4 files changed, 22 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index 85a05d9..6dedbe7 100644 --- a/.gitignore +++ b/.gitignore @@ -63,3 +63,4 @@ project/yapb.vcxproj.user /project/#Verone/incremental_index *.vsp *.lastcodeanalysissucceeded +*.json diff --git a/include/core.h b/include/core.h index 41ef130..4b52807 100644 --- a/include/core.h +++ b/include/core.h @@ -602,7 +602,7 @@ struct BotName { String steamId; String name; - bool used; + int usedBy; }; // voice config structure definition diff --git a/source/interface.cpp b/source/interface.cpp index 5ca1bd4..4316d2d 100644 --- a/source/interface.cpp +++ b/source/interface.cpp @@ -512,7 +512,7 @@ void InitConfig (void) memset (&item, 0, sizeof (item)); item.name = line; - item.used = false; + item.usedBy = 0; if (pair.GetElementNumber () > 1) item.steamId = pair[1].Trim (); @@ -1134,8 +1134,6 @@ void ClientDisconnect (edict_t *ent) if (bot->pev == &ent->v) { bot->EnableChatterIcon (false); - bot->ReleaseUsedName (); - bots.Free (i); } } diff --git a/source/manager.cpp b/source/manager.cpp index b52ae87..58c4ac2 100644 --- a/source/manager.cpp +++ b/source/manager.cpp @@ -152,6 +152,7 @@ BotCreationResult BotManager::CreateBot (const String &name, int difficulty, int } String steamId = ""; + BotName *botName = nullptr; // setup name if (name.IsEmpty ()) @@ -160,23 +161,23 @@ BotCreationResult BotManager::CreateBot (const String &name, int difficulty, int { bool nameFound = false; - for (int i = 0; i < g_botNames.GetSize (); i++) + FOR_EACH_AE (g_botNames, i) { if (nameFound) break; - BotName *pickedName = &g_botNames.GetRandomElement (); + botName = &g_botNames.GetRandomElement (); - if (pickedName == nullptr) + if (botName == nullptr) continue; - if (pickedName->used) + if (botName->usedBy != 0) continue; - pickedName->used = nameFound = true; - strncpy (outputName, pickedName->name, SIZEOF_CHAR (outputName)); + nameFound = true; + strncpy (outputName, botName->name, SIZEOF_CHAR (outputName)); - steamId = pickedName->steamId; + steamId = botName->steamId; } } else @@ -205,6 +206,9 @@ BotCreationResult BotManager::CreateBot (const String &name, int difficulty, int } int index = engine.IndexOfEntity (bot) - 1; + // ensure it free + Free (index); + InternalAssert (index >= 0 && index <= MAX_ENGINE_PLAYERS); // check index InternalAssert (m_bots[index] == nullptr); // check bot slot @@ -213,6 +217,10 @@ BotCreationResult BotManager::CreateBot (const String &name, int difficulty, int if (m_bots[index] == nullptr) TerminateOnMalloc (); + // assign owner of bot name + if (botName != nullptr) + botName->usedBy = m_bots[index]->GetIndex (); + engine.Printf ("Connecting Bot..."); if (isConsoleCmd) @@ -853,7 +861,7 @@ Bot::Bot (edict_t *bot, int difficulty, int personality, int team, int member, c int clientIndex = engine.IndexOfEntity (bot); memset (reinterpret_cast (this), 0, sizeof (*this)); - + pev = &bot->v; if (bot->pvPrivateData != nullptr) @@ -969,9 +977,9 @@ void Bot::ReleaseUsedName (void) { BotName &name = g_botNames[j]; - if (strcmp (name.name, STRING (pev->netname)) == 0) + if (name.usedBy == GetIndex ()) { - name.used = false; + name.usedBy = 0; break; } } @@ -981,6 +989,7 @@ Bot::~Bot (void) { // this is bot destructor + ReleaseUsedName (); DeleteSearchNodes (); ResetTasks (); }