some code cleanup

started to move game-specific stuff into own class
This commit is contained in:
jeefo 2016-03-05 21:04:46 +03:00
commit fa64fe2ea7
12 changed files with 131 additions and 85 deletions

View file

@ -1062,6 +1062,7 @@ public:
float m_spawnTime; // time this bot spawned float m_spawnTime; // time this bot spawned
float m_timeTeamOrder; // time of last radio command float m_timeTeamOrder; // time of last radio command
float m_timePeriodicUpdate; // time to per-second think float m_timePeriodicUpdate; // time to per-second think
float m_timeRepotingInDelay; // time to delay report-in
bool m_isVIP; // bot is vip? bool m_isVIP; // bot is vip?
@ -1502,6 +1503,7 @@ public:
}; };
#include <engine.h> #include <engine.h>
#include <gamestate.h>
// expose bot super-globals // expose bot super-globals
extern NetworkMsg netmsg; extern NetworkMsg netmsg;
@ -1509,6 +1511,7 @@ extern Localizer locale;
extern Waypoint waypoints; extern Waypoint waypoints;
extern BotManager bots; extern BotManager bots;
extern Engine engine; extern Engine engine;
extern Game game;
// prototypes of bot functions... // prototypes of bot functions...
extern int GetWeaponReturn (bool isString, const char *weaponAlias, int weaponIndex = -1); extern int GetWeaponReturn (bool isString, const char *weaponAlias, int weaponIndex = -1);

View file

@ -1,4 +1,4 @@
// //
// Yet Another POD-Bot, based on PODBot by Markus Klinge ("CountFloyd"). // Yet Another POD-Bot, based on PODBot by Markus Klinge ("CountFloyd").
// Copyright (c) YaPB Development Team. // Copyright (c) YaPB Development Team.
// //

42
include/gamestate.h Normal file
View file

@ -0,0 +1,42 @@
//
// Yet Another POD-Bot, based on PODBot by Markus Klinge ("CountFloyd").
// Copyright (c) YaPB Development Team.
//
// This software is licensed under the BSD-style license.
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
// http://yapb.jeefo.net/license
//
// Purpose: Represents Counter-Strike Game.
//
#pragma once
//
class IGameEvents
{
public:
// called when new round occurs
virtual void OnNewRound (void) = 0;
};
class Game
{
private:
IGameEvents *m_listener;
public:
Game (void) : m_listener (NULL) { }
~Game (void) { }
public:
// we have only one listener, so register it here
void RegisterEventListener (IGameEvents *listener) { m_listener = listener; }
// get events interface
IGameEvents *GetGameEventListener (void) { return m_listener; }
};
// helper macros
#define events game.GetGameEventListener ()

View file

@ -27,6 +27,7 @@
<ClInclude Include="..\include\engine\progdefs.h" /> <ClInclude Include="..\include\engine\progdefs.h" />
<ClInclude Include="..\include\engine\sdk_util.h" /> <ClInclude Include="..\include\engine\sdk_util.h" />
<ClInclude Include="..\include\engine\util.h" /> <ClInclude Include="..\include\engine\util.h" />
<ClInclude Include="..\include\gamestate.h" />
<ClInclude Include="..\include\globals.h" /> <ClInclude Include="..\include\globals.h" />
<ClInclude Include="..\include\platform.h" /> <ClInclude Include="..\include\platform.h" />
<ClInclude Include="..\include\resource.h" /> <ClInclude Include="..\include\resource.h" />
@ -35,6 +36,7 @@
<ClCompile Include="..\source\basecode.cpp" /> <ClCompile Include="..\source\basecode.cpp" />
<ClCompile Include="..\source\combat.cpp" /> <ClCompile Include="..\source\combat.cpp" />
<ClCompile Include="..\source\engine.cpp" /> <ClCompile Include="..\source\engine.cpp" />
<ClCompile Include="..\source\gamestate.cpp" />
<ClCompile Include="..\source\manager.cpp" /> <ClCompile Include="..\source\manager.cpp" />
<ClCompile Include="..\source\chatlib.cpp" /> <ClCompile Include="..\source\chatlib.cpp" />
<ClCompile Include="..\source\globals.cpp" /> <ClCompile Include="..\source\globals.cpp" />

View file

@ -72,6 +72,9 @@
<ClInclude Include="..\include\engine.h"> <ClInclude Include="..\include\engine.h">
<Filter>include</Filter> <Filter>include</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\include\gamestate.h">
<Filter>include</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\source\chatlib.cpp"> <ClCompile Include="..\source\chatlib.cpp">
@ -107,6 +110,9 @@
<ClCompile Include="..\source\engine.cpp"> <ClCompile Include="..\source\engine.cpp">
<Filter>source</Filter> <Filter>source</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\source\gamestate.cpp">
<Filter>source</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ResourceCompile Include="yapb.rc"> <ResourceCompile Include="yapb.rc">

View file

@ -1,4 +1,4 @@
// //
// Yet Another POD-Bot, based on PODBot by Markus Klinge ("CountFloyd"). // Yet Another POD-Bot, based on PODBot by Markus Klinge ("CountFloyd").
// Copyright (c) YaPB Development Team. // Copyright (c) YaPB Development Team.
// //
@ -988,15 +988,13 @@ void Bot::InstantChatterMessage (int type)
if (m_notKilled) if (m_notKilled)
SwitchChatterIcon (true); SwitchChatterIcon (true);
static float reportTime = engine.Time ();
// delay only reportteam // delay only reportteam
if (type == Radio_ReportTeam) if (type == Radio_ReportTeam && m_timeRepotingInDelay < engine.Time ())
{ {
if (reportTime >= engine.Time ()) if (m_timeRepotingInDelay < engine.Time ())
return; return;
reportTime = engine.Time () + Random.Float (30.0f, 80.0f); m_timeRepotingInDelay = engine.Time () + Random.Float (30.0f, 60.0f);
} }
String defaultSound = g_chatterFactory[type].GetRandomElement ().name; String defaultSound = g_chatterFactory[type].GetRandomElement ().name;
@ -1720,18 +1718,6 @@ void Bot::PurchaseWeapons (void)
PushMessageQueue (GSM_BUY_STUFF); PushMessageQueue (GSM_BUY_STUFF);
} }
TaskItem *ClampDesire (TaskItem *first, float min, float max)
{
// this function iven some values min and max, clamp the inputs to be inside the [min, max] range.
if (first->desire < min)
first->desire = min;
else if (first->desire > max)
first->desire = max;
return first;
}
TaskItem *MaxDesire (TaskItem *first, TaskItem *second) TaskItem *MaxDesire (TaskItem *first, TaskItem *second)
{ {
// this function returns the behavior having the higher activation level. // this function returns the behavior having the higher activation level.
@ -2626,9 +2612,7 @@ void Bot::CheckRadioCommands (void)
int bombPoint = -1; int bombPoint = -1;
// check if it's a ct command // check if it's a ct command
if (GetTeam (m_radioEntity) == CT && m_team == CT && IsValidBot (m_radioEntity)) if (GetTeam (m_radioEntity) == CT && m_team == CT && IsValidBot (m_radioEntity) && g_timeNextBombUpdate < engine.Time ())
{
if (g_timeNextBombUpdate < engine.Time ())
{ {
float minDistance = 99999.0f; float minDistance = 99999.0f;
@ -2663,7 +2647,6 @@ void Bot::CheckRadioCommands (void)
g_timeNextBombUpdate = engine.Time () + 0.5f; g_timeNextBombUpdate = engine.Time () + 0.5f;
} }
} }
}
break; break;
case Radio_GetInPosition: case Radio_GetInPosition:
@ -3223,11 +3206,9 @@ void Bot::RunTask_Normal (void)
if (m_personality == PERSONALITY_RUSHER) if (m_personality == PERSONALITY_RUSHER)
campTime *= 0.5f; campTime *= 0.5f;
PushTask (TASK_CAMP, TASKPRI_CAMP, -1, engine.Time () + Random.Float (25.0, 40.0), true); // push camp task on to stack PushTask (TASK_CAMP, TASKPRI_CAMP, -1, engine.Time () + campTime, true); // push camp task on to stack
PushTask (TASK_MOVETOPOSITION, TASKPRI_MOVETOPOSITION, index, engine.Time () + Random.Float (5.0f, 11.0f), true); // push move command PushTask (TASK_MOVETOPOSITION, TASKPRI_MOVETOPOSITION, index, engine.Time () + Random.Float (5.0f, 11.0f), true); // push move command
DebugMsg ("i'm ct and going to defend bomb!");
if (waypoints.GetPath (index)->vis.crouch <= waypoints.GetPath (index)->vis.stand) if (waypoints.GetPath (index)->vis.crouch <= waypoints.GetPath (index)->vis.stand)
m_campButtons |= IN_DUCK; m_campButtons |= IN_DUCK;
else else
@ -4274,7 +4255,7 @@ void Bot::RunTask_Throw_SG (void)
void Bot::RunTask_DoubleJump (void) void Bot::RunTask_DoubleJump (void)
{ {
if (IsNullEntity (m_doubleJumpEntity) || !IsAlive (m_doubleJumpEntity) || (m_aimFlags & AIM_ENEMY) || (m_travelStartIndex != -1 && GetTask ()->time + (waypoints.GetTravelTime (pev->maxspeed, waypoints.GetPath (m_travelStartIndex)->origin, m_doubleJumpOrigin) + 11.0) < engine.Time ())) if (!IsAlive (m_doubleJumpEntity) || (m_aimFlags & AIM_ENEMY) || (m_travelStartIndex != -1 && GetTask ()->time + (waypoints.GetTravelTime (pev->maxspeed, waypoints.GetPath (m_travelStartIndex)->origin, m_doubleJumpOrigin) + 11.0) < engine.Time ()))
{ {
ResetDoubleJumpState (); ResetDoubleJumpState ();
return; return;
@ -4453,8 +4434,6 @@ void Bot::RunTask_PickupItem ()
// find the distance to the item // find the distance to the item
float itemDistance = (dest - pev->origin).GetLength (); float itemDistance = (dest - pev->origin).GetLength ();
int id = 0;
switch (m_pickupType) switch (m_pickupType)
{ {
case PICKUP_DROPPED_C4: case PICKUP_DROPPED_C4:
@ -4467,6 +4446,8 @@ void Bot::RunTask_PickupItem ()
// near to weapon? // near to weapon?
if (itemDistance < 50.0f) if (itemDistance < 50.0f)
{ {
int id = 0;
for (id = 0; id < 7; id++) for (id = 0; id < 7; id++)
{ {
if (strcmp (g_weaponSelect[id].modelName, STRING (m_pickupItem->v.model) + 9) == 0) if (strcmp (g_weaponSelect[id].modelName, STRING (m_pickupItem->v.model) + 9) == 0)
@ -4805,8 +4786,7 @@ void Bot::BotAI (void)
{ {
// this function gets called each frame and is the core of all bot ai. from here all other subroutines are called // this function gets called each frame and is the core of all bot ai. from here all other subroutines are called
float movedDistance; // length of different vector (distance bot moved) float movedDistance = 2.0f; // length of different vector (distance bot moved)
TraceResult tr;
// increase reaction time // increase reaction time
m_actualReactionTime += 0.3f; m_actualReactionTime += 0.3f;
@ -4834,8 +4814,6 @@ void Bot::BotAI (void)
m_prevOrigin = pev->origin; m_prevOrigin = pev->origin;
m_prevTime = engine.Time () + 0.2f; m_prevTime = engine.Time () + 0.2f;
} }
else
movedDistance = 2.0f;
// if there's some radio message to respond, check it // if there's some radio message to respond, check it
if (m_radioOrder != 0) if (m_radioOrder != 0)

View file

@ -445,11 +445,11 @@ void ConVarWrapper::RegisterVariable (const char *variable, const char *value, V
{ {
// this function adds globally defined variable to registration stack // this function adds globally defined variable to registration stack
VarPair newVariable; VarPair pair;
memset (&newVariable, 0, sizeof (VarPair)); memset (&pair, 0, sizeof (VarPair));
newVariable.reg.name = const_cast <char *> (variable); pair.reg.name = const_cast <char *> (variable);
newVariable.reg.string = const_cast <char *> (value); pair.reg.string = const_cast <char *> (value);
int engineFlags = FCVAR_EXTDLL; int engineFlags = FCVAR_EXTDLL;
@ -460,11 +460,11 @@ void ConVarWrapper::RegisterVariable (const char *variable, const char *value, V
else if (varType == VT_PASSWORD) else if (varType == VT_PASSWORD)
engineFlags |= FCVAR_PROTECTED; engineFlags |= FCVAR_PROTECTED;
newVariable.reg.flags = engineFlags; pair.reg.flags = engineFlags;
newVariable.self = self; pair.self = self;
newVariable.type = varType; pair.type = varType;
m_regs.Push (newVariable); m_regs.Push (pair);
} }
void ConVarWrapper::PushRegisteredConVarsToEngine (bool gameVars) void ConVarWrapper::PushRegisteredConVarsToEngine (bool gameVars)

10
source/gamestate.cpp Normal file
View file

@ -0,0 +1,10 @@
//
// Yet Another POD-Bot, based on PODBot by Markus Klinge ("CountFloyd").
// Copyright (c) YaPB Development Team.
//
// This software is licensed under the BSD-style license.
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
// http://yapb.jeefo.net/license
//
#include <core.h>

View file

@ -15,6 +15,7 @@ Localizer locale;
Waypoint waypoints; Waypoint waypoints;
BotManager bots; BotManager bots;
Engine engine; Engine engine;
Game game;
bool g_canSayBombPlanted = true; bool g_canSayBombPlanted = true;
bool g_isMetamod = false; bool g_isMetamod = false;

View file

@ -457,6 +457,14 @@ void CommandHandler (void)
engine.Printf ("Unknown command: %s", CMD_ARGV (1)); engine.Printf ("Unknown command: %s", CMD_ARGV (1));
} }
class GameEvents : public IGameEvents
{
virtual void OnNewRound (void)
{
RoundInit ();
}
} g_botEventListener;
void ParseVoiceEvent (const String &base, int type, float timeToRepeat) void ParseVoiceEvent (const String &base, int type, float timeToRepeat)
{ {
// this function does common work of parsing single line of voice chatter // this function does common work of parsing single line of voice chatter
@ -918,6 +926,9 @@ void GameDLLInit (void)
// server is enabled. Here is a good place to do our own game session initialization, and // server is enabled. Here is a good place to do our own game session initialization, and
// to register by the engine side the server commands we need to administrate our bots. // to register by the engine side the server commands we need to administrate our bots.
// register bot event listner
game.RegisterEventListener (&g_botEventListener);
DetectCSVersion (); DetectCSVersion ();
{ {
// print game detection info // print game detection info
@ -1014,9 +1025,7 @@ int Spawn (edict_t *ent)
PRECACHE_SOUND (ENGINE_STR ("common/wpn_moveselect.wav")); // path add/delete cancel PRECACHE_SOUND (ENGINE_STR ("common/wpn_moveselect.wav")); // path add/delete cancel
PRECACHE_SOUND (ENGINE_STR ("common/wpn_denyselect.wav")); // path add/delete error PRECACHE_SOUND (ENGINE_STR ("common/wpn_denyselect.wav")); // path add/delete error
g_roundEnded = true; events->OnNewRound ();
RoundInit ();
g_mapType = NULL; // reset map type as worldspawn is the first entity spawned g_mapType = NULL; // reset map type as worldspawn is the first entity spawned
// detect official csbots here, as they causing crash in linkent code when active for some reason // detect official csbots here, as they causing crash in linkent code when active for some reason
@ -2335,7 +2344,7 @@ edict_t *pfnFindEntityByString (edict_t *edictStartSearchAfter, const char *fiel
{ {
// round starts in counter-strike 1.5 // round starts in counter-strike 1.5
if (strcmp (value, "info_map_parameters") == 0) if (strcmp (value, "info_map_parameters") == 0)
RoundInit (); events->OnNewRound ();
if (g_isMetamod) if (g_isMetamod)
RETURN_META_VALUE (MRES_IGNORED, 0); RETURN_META_VALUE (MRES_IGNORED, 0);

View file

@ -1,4 +1,4 @@
// //
// Yet Another POD-Bot, based on PODBot by Markus Klinge ("CountFloyd"). // Yet Another POD-Bot, based on PODBot by Markus Klinge ("CountFloyd").
// Copyright (c) YaPB Development Team. // Copyright (c) YaPB Development Team.
// //
@ -550,7 +550,6 @@ void BotManager::RemoveMenu (edict_t *ent, int selection)
{ {
// this function displays remove bot menu to specified entity (this function show's only yapb's). // this function displays remove bot menu to specified entity (this function show's only yapb's).
if (selection > 4 || selection < 1) if (selection > 4 || selection < 1)
return; return;
@ -740,16 +739,11 @@ void BotManager::ListBots (void)
for (int i = 0; i < engine.MaxClients (); i++) for (int i = 0; i < engine.MaxClients (); i++)
{ {
edict_t *player = EntityOfIndex (i); Bot *bot = GetBot (i);
// is this player slot valid // is this player slot valid
if (IsValidBot (player))
{
Bot *bot = GetBot (player);
if (bot != NULL) if (bot != NULL)
engine.Printf ("[%-3.1d] %-9.13s %-17.18s %-3.4s %-3.1d %-3.1d", i, STRING (player->v.netname), bot->m_personality == PERSONALITY_RUSHER ? "rusher" : bot->m_personality == PERSONALITY_NORMAL ? "normal" : "careful", GetTeam (player) != 0 ? "CT" : "T", bot->m_difficulty, static_cast <int> (player->v.frags)); engine.Printf ("[%-3.1d] %-9.13s %-17.18s %-3.4s %-3.1d %-3.1d", i, STRING (bot->pev->netname), bot->m_personality == PERSONALITY_RUSHER ? "rusher" : bot->m_personality == PERSONALITY_NORMAL ? "normal" : "careful", bot->m_team == CT ? "CT" : "T", bot->m_difficulty, static_cast <int> (bot->pev->frags));
}
} }
} }
@ -777,7 +771,7 @@ Bot *BotManager::GetHighestFragsBot (int team)
{ {
Bot *bot = bots.GetBot (i); Bot *bot = bots.GetBot (i);
if (bot != NULL && IsAlive (bot->GetEntity ()) && GetTeam (bot->GetEntity ()) == team) if (bot != NULL && bot->m_notKilled && bot->m_team == team)
{ {
if (bot->pev->frags > bestScore) if (bot->pev->frags > bestScore)
{ {
@ -1101,6 +1095,7 @@ void Bot::NewRound (void)
m_maxThrowTimer = 0.0f; m_maxThrowTimer = 0.0f;
m_timeTeamOrder = 0.0f; m_timeTeamOrder = 0.0f;
m_timeRepotingInDelay = 0.0f;
m_askCheckTime = 0.0f; m_askCheckTime = 0.0f;
m_minSpeed = 260.0f; m_minSpeed = 260.0f;
m_prevSpeed = 0.0f; m_prevSpeed = 0.0f;
@ -1156,7 +1151,7 @@ void Bot::NewRound (void)
SetIdealReactionTimes (true); SetIdealReactionTimes (true);
m_targetEntity = NULL; m_targetEntity = NULL;
m_tasks = NULL; m_tasks.RemoveAll ();
m_followWaitTime = 0.0f; m_followWaitTime = 0.0f;
for (i = 0; i < MAX_HOSTAGES; i++) for (i = 0; i < MAX_HOSTAGES; i++)

View file

@ -1,4 +1,4 @@
// //
// Yet Another POD-Bot, based on PODBot by Markus Klinge ("CountFloyd"). // Yet Another POD-Bot, based on PODBot by Markus Klinge ("CountFloyd").
// Copyright (c) YaPB Development Team. // Copyright (c) YaPB Development Team.
// //