From 533cd8a93cb3528817c9d8e10103f94508c3b08b Mon Sep 17 00:00:00 2001 From: jeefo Date: Sun, 26 Jul 2015 21:47:29 +0300 Subject: [PATCH] reduced lag, when killing bots all together a little housekeeping --- include/core.h | 9 ++++-- project/yapb.vcxproj | 5 +--- source/basecode.cpp | 15 ++++------ source/interface.cpp | 6 ++++ source/manager.cpp | 71 +++++++++++++++++++++++++++----------------- 5 files changed, 63 insertions(+), 43 deletions(-) diff --git a/include/core.h b/include/core.h index 889effb..cf73760 100644 --- a/include/core.h +++ b/include/core.h @@ -1242,10 +1242,10 @@ public: inline Vector EyePosition (void) { return pev->origin + pev->view_ofs; }; // the main function that decides intervals of running bot ai - void ThinkMain (void); + void Think (void); /// the things that can be executed while skipping frames - void Think (void); + void ThinkDelayed (void); void NewRound (void); void EquipInBuyzone (int buyCount); @@ -1319,6 +1319,7 @@ private: // holds currently active grenades in the map Array m_activeGrenades; + edict_t *m_killerEntity; // killer entity for bots protected: int CreateBot (const String &name, int difficulty, int personality, int team, int member); @@ -1346,6 +1347,10 @@ public: void Think (void); void PeriodicThink (void); + void CreateKillerEntity (void); + void DestroyKillerEntity (void); + void TouchWithKillerEntity (Bot *bot); + void Free (void); void Free (int index); void CheckAutoVacate (void); diff --git a/project/yapb.vcxproj b/project/yapb.vcxproj index 5cb734b..d8ed05f 100644 --- a/project/yapb.vcxproj +++ b/project/yapb.vcxproj @@ -182,16 +182,13 @@ AnySuitable true Speed - false false ..\mmgr;..\include\engine;..\include;%(AdditionalIncludeDirectories) NDEBUG;WIN32;%(PreprocessorDefinitions) true false MultiThreaded - 16Bytes false - false StreamingSIMDExtensions2 false false @@ -205,7 +202,6 @@ true None CompileAsCpp - true SingleFile true false @@ -215,6 +211,7 @@ AVXI false false + false NDEBUG;%(PreprocessorDefinitions) diff --git a/source/basecode.cpp b/source/basecode.cpp index 07de227..d5a2236 100644 --- a/source/basecode.cpp +++ b/source/basecode.cpp @@ -2869,12 +2869,12 @@ void Bot::ChooseAimDirection (void) m_lookAt = m_destOrigin; } -void Bot::ThinkMain (void) +void Bot::Think (void) { if (m_thinkFps <= GetWorldTime ()) { // execute delayed think - Think (); + ThinkDelayed (); // skip some frames m_thinkFps = GetWorldTime () + m_thinkInterval; @@ -2883,7 +2883,7 @@ void Bot::ThinkMain (void) UpdateLookAngles (); } -void Bot::Think (void) +void Bot::ThinkDelayed (void) { pev->button = 0; pev->flags |= FL_FAKECLIENT; // restore fake client bit, if it were removed by some evil action =) @@ -2907,7 +2907,7 @@ void Bot::Think (void) else if (!m_notKilled) { // no movement allowed in - if (m_voteKickIndex != m_lastVoteKick && yb_tkpunish.GetBool ()) // We got a Teamkiller? Vote him away... + if (m_voteKickIndex != m_lastVoteKick && yb_tkpunish.GetBool ()) // we got a teamkiller? vote him away... { FakeClientCommand (GetEntity (), "vote %d", m_voteKickIndex); m_lastVoteKick = m_voteKickIndex; @@ -2961,7 +2961,7 @@ void Bot::Think (void) } } } - else if (m_buyingFinished) + else if (m_notKilled && m_buyingFinished && !(pev->maxspeed < 10 && GetTaskId () != TASK_PLANTBOMB && GetTaskId () != TASK_DEFUSEBOMB) && !yb_freeze_bots.GetBool ()) botMovement = true; CheckMessageQueue (); // check for pending messages @@ -2970,10 +2970,7 @@ void Bot::Think (void) if (g_lastRadioTime[g_clients[IndexOfEntity (GetEntity ()) - 1].realTeam] + Random.Float (0.8f, 2.1f) < GetWorldTime ()) SwitchChatterIcon (false); // hide icon - if (pev->maxspeed < 10 && GetTaskId () != TASK_PLANTBOMB && GetTaskId () != TASK_DEFUSEBOMB) - botMovement = false; - - if (m_notKilled && botMovement && !yb_freeze_bots.GetBool ()) + if (botMovement) BotAI (); // execute main code RunPlayerMovement (); // run the player movement diff --git a/source/interface.cpp b/source/interface.cpp index 790be7b..bc250b6 100644 --- a/source/interface.cpp +++ b/source/interface.cpp @@ -2055,6 +2055,9 @@ void ServerActivate (edict_t *pentEdictList, int edictCount, int clientMax) waypoints.Init (); waypoints.Load (); + // create global killer entity + bots.CreateKillerEntity (); + // execute main config ServerCommand ("exec addons/yapb/conf/yapb.cfg"); @@ -2089,6 +2092,9 @@ void ServerDeactivate (void) waypoints.SaveExperienceTab (); waypoints.SaveVisibilityTab (); + // destroy global killer entity + bots.DestroyKillerEntity (); + FreeLibraryMemory (); if (g_isMetamod) diff --git a/source/manager.cpp b/source/manager.cpp index a020466..81673b3 100644 --- a/source/manager.cpp +++ b/source/manager.cpp @@ -43,10 +43,50 @@ BotManager::BotManager (void) BotManager::~BotManager (void) { // this is a bot manager class destructor, do not use GetMaxClients () here !! - Free (); } +void BotManager::CreateKillerEntity (void) +{ + // this function creates single trigger_hurt for using in Bot::Kill, to reduce lags, when killing all the bots + + m_killerEntity = g_engfuncs.pfnCreateNamedEntity (MAKE_STRING ("trigger_hurt")); + + m_killerEntity->v.dmg = 9999.0f; + m_killerEntity->v.dmg_take = 1.0f; + m_killerEntity->v.dmgtime = 2.0f; + m_killerEntity->v.effects |= EF_NODRAW; + + g_engfuncs.pfnSetOrigin (m_killerEntity, Vector (-99999.0f, -99999.0f, -99999.0f)); + MDLL_Spawn (m_killerEntity); +} + +void BotManager::DestroyKillerEntity (void) +{ + if (!IsEntityNull (m_killerEntity)) + g_engfuncs.pfnRemoveEntity (m_killerEntity); +} + +void BotManager::TouchWithKillerEntity (Bot *bot) +{ + if (IsEntityNull (m_killerEntity)) + { + MDLL_ClientKill (bot->GetEntity ()); + return; + } + m_killerEntity->v.classname = MAKE_STRING (g_weaponDefs[bot->m_currentWeapon].className); + m_killerEntity->v.dmg_inflictor = bot->GetEntity (); + + KeyValueData kv; + kv.szClassName = const_cast (g_weaponDefs[bot->m_currentWeapon].className); + kv.szKeyName = "damagetype"; + kv.szValue = const_cast (FormatBuffer ("%d", (1 << 4))); + kv.fHandled = FALSE; + + MDLL_KeyValue (m_killerEntity, &kv); + MDLL_Touch (m_killerEntity, bot->GetEntity ()); +} + void BotManager::CallGameEntity (entvars_t *vars) { // this function calls gamedll player() function, in case to create player entity in game @@ -240,7 +280,7 @@ void BotManager::Think (void) for (int i = 0; i < GetMaxClients (); i++) { if (m_bots[i] != NULL) - m_bots[i]->ThinkMain (); + m_bots[i]->Think (); } } @@ -1130,32 +1170,7 @@ void Bot::Kill (void) // this function kills a bot (not just using ClientKill, but like the CSBot does) // base code courtesy of Lazy (from bots-united forums!) - edict_t *hurtEntity = (*g_engfuncs.pfnCreateNamedEntity) (MAKE_STRING ("trigger_hurt")); - - if (IsEntityNull (hurtEntity)) - return; - - hurtEntity->v.classname = MAKE_STRING (g_weaponDefs[m_currentWeapon].className); - hurtEntity->v.dmg_inflictor = GetEntity (); - hurtEntity->v.dmg = 9999.0; - hurtEntity->v.dmg_take = 1.0; - hurtEntity->v.dmgtime = 2.0; - hurtEntity->v.effects |= EF_NODRAW; - - (*g_engfuncs.pfnSetOrigin) (hurtEntity, Vector (-4000, -4000, -4000)); - - KeyValueData kv; - kv.szClassName = const_cast (g_weaponDefs[m_currentWeapon].className); - kv.szKeyName = "damagetype"; - kv.szValue = const_cast (FormatBuffer ("%d", (1 << 4))); - kv.fHandled = FALSE; - - MDLL_KeyValue (hurtEntity, &kv); - - MDLL_Spawn (hurtEntity); - MDLL_Touch (hurtEntity, GetEntity ()); - - (*g_engfuncs.pfnRemoveEntity) (hurtEntity); + bots.TouchWithKillerEntity (this); } void Bot::Kick (void)