ReGameDLL_CS/regamedll/dlls/bot/cs_bot_chatter.h

605 lines
19 KiB
C
Raw Normal View History

2015-06-30 12:46:07 +03:00
/*
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* In addition, as a special exception, the author gives permission to
* link the code of this program with the Half-Life Game Engine ("HL
* Engine") and Modified Game Libraries ("MODs") developed by Valve,
* L.L.C ("Valve"). You must obey the GNU General Public License in all
* respects for all of the code used other than the HL Engine and MODs
* from Valve. If you modify this file, you may extend this exception
* to your version of the file, but you are not obligated to do so. If
* you do not wish to do so, delete this exception statement from your
* version.
*
*/
#ifndef CS_BOT_CHATTER_H
#define CS_BOT_CHATTER_H
#ifdef _WIN32
#pragma once
#endif
2015-09-16 23:19:21 +03:00
#define UNDEFINED_COUNT 0xFFFF
#define MAX_PLACES_PER_MAP 64
#define UNDEFINED_SUBJECT (-1)
#define COUNT_MANY 4 // equal to or greater than this is "many"
2015-09-16 23:19:21 +03:00
2015-06-30 12:46:07 +03:00
class CCSBot;
2015-09-16 23:19:21 +03:00
class BotChatterInterface;
typedef unsigned int PlaceCriteria;
typedef unsigned int CountCriteria;
// A meme is a unit information that bots use to
// transmit information to each other via the radio
2015-06-30 12:46:07 +03:00
class BotMeme
{
public:
void Transmit(CCSBot *sender) const; // transmit meme to other bots
virtual ~BotMeme(){}
virtual void Interpret(CCSBot *sender, CCSBot *receiver) const = 0; // cause the given bot to act on this meme
};
2015-09-16 23:19:21 +03:00
2015-06-30 12:46:07 +03:00
class BotAllHostagesGoneMeme: public BotMeme
{
public:
virtual void Interpret(CCSBot *sender, CCSBot *receiver) const; // cause the given bot to act on this meme
};
2015-09-16 23:19:21 +03:00
2015-06-30 12:46:07 +03:00
class BotHostageBeingTakenMeme: public BotMeme
{
public:
virtual void Interpret(CCSBot *sender, CCSBot *receiver) const; // cause the given bot to act on this meme
};
2015-09-16 23:19:21 +03:00
2015-06-30 12:46:07 +03:00
class BotHelpMeme: public BotMeme
{
public:
BotHelpMeme(Place place = UNDEFINED_PLACE)
{
m_place = place;
}
virtual void Interpret(CCSBot *sender, CCSBot *receiver) const; // cause the given bot to act on this meme
2015-06-30 12:46:07 +03:00
private:
Place m_place;
};
2015-09-16 23:19:21 +03:00
2015-06-30 12:46:07 +03:00
class BotBombsiteStatusMeme: public BotMeme
{
public:
enum StatusType { CLEAR, PLANTED };
BotBombsiteStatusMeme(int zoneIndex = 0, StatusType status = CLEAR)
2015-06-30 12:46:07 +03:00
{
m_zoneIndex = zoneIndex;
m_status = status;
}
virtual void Interpret(CCSBot *sender, CCSBot *receiver) const; // cause the given bot to act on this meme
2015-06-30 12:46:07 +03:00
private:
int m_zoneIndex; // the bombsite
StatusType m_status; // whether it is cleared or the bomb is there (planted)
};
2015-09-16 23:19:21 +03:00
2015-06-30 12:46:07 +03:00
class BotBombStatusMeme: public BotMeme
{
public:
BotBombStatusMeme(CSGameState::BombState state = CSGameState::MOVING, const Vector &pos = nullptr)
2015-06-30 12:46:07 +03:00
{
m_state = state;
m_pos = pos;
}
2015-06-30 12:46:07 +03:00
public:
virtual void Interpret(CCSBot *sender, CCSBot *receiver) const; // cause the given bot to act on this meme
2015-06-30 12:46:07 +03:00
private:
CSGameState::BombState m_state;
Vector m_pos;
};
2015-09-16 23:19:21 +03:00
2015-06-30 12:46:07 +03:00
class BotFollowMeme: public BotMeme
{
public:
virtual void Interpret(CCSBot *sender, CCSBot *receiver) const; // cause the given bot to act on this meme
};
2015-09-16 23:19:21 +03:00
class BotDefendHereMeme: public BotMeme
{
public:
BotDefendHereMeme(const Vector &pos = nullptr)
2015-09-16 23:19:21 +03:00
{
m_pos = pos;
}
virtual void Interpret(CCSBot *sender, CCSBot *receiver) const; // cause the given bot to act on this meme
2015-09-16 23:19:21 +03:00
private:
Vector m_pos;
};
2015-09-16 23:19:21 +03:00
class BotWhereBombMeme: public BotMeme
{
public:
virtual void Interpret(CCSBot *sender, CCSBot *receiver) const; // cause the given bot to act on this meme
};
2015-09-16 23:19:21 +03:00
class BotRequestReportMeme: public BotMeme
{
public:
virtual void Interpret(CCSBot *sender, CCSBot *receiver) const; // cause the given bot to act on this meme
};
2015-09-16 23:19:21 +03:00
enum BotStatementType
{
REPORT_VISIBLE_ENEMIES,
2015-09-16 23:19:21 +03:00
REPORT_ENEMY_ACTION,
REPORT_MY_CURRENT_TASK,
REPORT_MY_INTENTION,
REPORT_CRITICAL_EVENT,
REPORT_REQUEST_HELP,
REPORT_REQUEST_INFORMATION,
REPORT_ROUND_END,
REPORT_MY_PLAN,
REPORT_INFORMATION,
REPORT_EMOTE,
REPORT_ACKNOWLEDGE, // affirmative or negative
2015-09-16 23:19:21 +03:00
REPORT_ENEMIES_REMAINING,
REPORT_FRIENDLY_FIRE,
REPORT_KILLED_FRIEND,
//REPORT_ENEMY_LOST
2015-09-16 23:19:21 +03:00
NUM_BOT_STATEMENT_TYPES,
};
// BotSpeakables are the smallest unit of bot chatter.
// They represent a specific wav file of a phrase, and the criteria for which it is useful
2015-06-30 12:46:07 +03:00
class BotSpeakable
{
public:
BotSpeakable();
~BotSpeakable();
2015-06-30 12:46:07 +03:00
char *m_phrase;
float m_duration;
PlaceCriteria m_place;
CountCriteria m_count;
};
2015-06-30 12:46:07 +03:00
typedef std::STD_VECTOR<BotSpeakable *> BotSpeakableVector;
typedef std::STD_VECTOR<BotSpeakableVector *> BotVoiceBankVector;
2015-09-16 23:19:21 +03:00
// The BotPhrase class is a collection of Speakables associated with a name, ID, and criteria
2015-09-16 23:19:21 +03:00
class BotPhrase
{
2015-06-30 12:46:07 +03:00
public:
char *GetSpeakable(int bankIndex, float *duration = nullptr) const; // return a random speakable and its duration in seconds that meets the current criteria
2015-09-16 23:19:21 +03:00
// NOTE: Criteria must be set just before the GetSpeakable() call, since they are shared among all bots
void ClearCriteria() const;
void SetPlaceCriteria(PlaceCriteria place) const; // all returned phrases must have this place criteria
void SetCountCriteria(CountCriteria count) const; // all returned phrases must have this count criteria
const char *GetName() const { return m_name; }
Place GetID() const { return m_id; }
GameEventType GetRadioEquivalent() const { return m_radioEvent; }
bool IsImportant() const { return m_isImportant; } // return true if this phrase is part of an important statement
bool IsPlace() const { return m_isPlace; }
void Randomize(); // randomly shuffle the speakable order
private:
2015-09-16 23:19:21 +03:00
friend class BotPhraseManager;
BotPhrase(unsigned int id, bool isPlace);
~BotPhrase();
2015-09-16 23:19:21 +03:00
char *m_name;
Place m_id;
bool m_isPlace; // true if this is a Place phrase
2015-09-16 23:19:21 +03:00
GameEventType m_radioEvent;
bool m_isImportant; // mission-critical statement
mutable BotVoiceBankVector m_voiceBank; // array of voice banks (arrays of speakables)
std::STD_VECTOR<int> m_count; // number of speakables
mutable std::STD_VECTOR< int > m_index; // index of next speakable to return
int m_numVoiceBanks; // number of voice banks that have been initialized
void InitVoiceBank(int bankIndex); // sets up the vector of voice banks for the first bankIndex voice banks
2015-09-16 23:19:21 +03:00
mutable PlaceCriteria m_placeCriteria;
mutable CountCriteria m_countCriteria;
};
2015-09-16 23:19:21 +03:00
typedef std::STD_LIST<BotPhrase *> BotPhraseList;
2015-09-16 23:19:21 +03:00
inline void BotPhrase::ClearCriteria() const
2015-09-16 23:19:21 +03:00
{
2015-06-30 12:46:07 +03:00
m_placeCriteria = ANY_PLACE;
2015-09-16 23:19:21 +03:00
m_countCriteria = UNDEFINED_COUNT;
}
2015-06-30 12:46:07 +03:00
inline void BotPhrase::SetPlaceCriteria(PlaceCriteria place) const
{
m_placeCriteria = place;
}
2015-09-16 23:19:21 +03:00
inline void BotPhrase::SetCountCriteria(CountCriteria count) const
2015-06-30 12:46:07 +03:00
{
m_countCriteria = count;
2015-09-16 23:19:21 +03:00
}
// The BotPhraseManager is a singleton that provides an interface to all BotPhrase collections
2015-06-30 12:46:07 +03:00
class BotPhraseManager
2015-09-16 23:19:21 +03:00
{
public:
BotPhraseManager();
~BotPhraseManager();
2015-09-16 23:19:21 +03:00
// initialize phrase system from database file for a specific voice bank (0 is the default voice bank)
bool Initialize(const char *filename, int bankIndex = 0);
2015-09-16 23:19:21 +03:00
// invoked when round resets
void OnRoundRestart();
2015-09-16 23:19:21 +03:00
// invoked when map changes
void OnMapChange();
2015-09-16 23:19:21 +03:00
Place NameToID(const char *name) const;
const char *IDToName(Place id) const;
// given a name, return the associated phrase collection
const BotPhrase *GetPhrase(const char *name) const;
2015-09-16 23:19:21 +03:00
// given a name, return the associated Place phrase collection
const BotPhrase *GetPlace(const char *name) const;
2015-09-16 23:19:21 +03:00
// given an id, return the associated Place phrase collection
const BotPhrase *GetPlace(PlaceCriteria place) const;
2015-09-16 23:19:21 +03:00
const BotPhraseList *GetPlaceList() const { return &m_placeList; }
2015-09-16 23:19:21 +03:00
// return time last statement of given type was emitted by a teammate for the given place
float GetPlaceStatementInterval(Place place) const;
// set time of last statement of given type was emitted by a teammate for the given place
void ResetPlaceStatementInterval(Place place) const;
2015-06-30 12:46:07 +03:00
private:
2015-09-16 23:19:21 +03:00
int FindPlaceIndex(Place where) const;
// master list of all phrase collections
BotPhraseList m_list;
// master list of all Place phrases
BotPhraseList m_placeList;
struct PlaceTimeInfo
{
Place placeID;
IntervalTimer timer;
};
mutable PlaceTimeInfo m_placeStatementHistory[MAX_PLACES_PER_MAP];
2015-09-16 23:19:21 +03:00
mutable int m_placeCount;
};
2015-09-16 23:19:21 +03:00
inline int BotPhraseManager::FindPlaceIndex(Place where) const
{
2016-01-25 20:02:57 +03:00
for (int i = 0; i < m_placeCount; ++i)
2015-06-30 12:46:07 +03:00
{
if (m_placeStatementHistory[i].placeID == where)
return i;
}
2015-06-30 12:46:07 +03:00
if (m_placeCount < MAX_PLACES_PER_MAP)
{
m_placeStatementHistory[++m_placeCount].placeID = where;
m_placeStatementHistory[++m_placeCount].timer.Invalidate();
return m_placeCount - 1;
2015-06-30 12:46:07 +03:00
}
2015-09-16 23:19:21 +03:00
return -1;
}
2015-06-30 12:46:07 +03:00
inline float BotPhraseManager::GetPlaceStatementInterval(Place place) const
{
int index = FindPlaceIndex(place);
if (index < 0)
return 999999.9f;
if (index >= m_placeCount)
return 999999.9f;
return m_placeStatementHistory[index].timer.GetElapsedTime();
2015-09-16 23:19:21 +03:00
}
2015-06-30 12:46:07 +03:00
inline void BotPhraseManager::ResetPlaceStatementInterval(Place place) const
{
int index = FindPlaceIndex(place);
2015-06-30 12:46:07 +03:00
if (index < 0)
return;
if (index >= m_placeCount)
return;
m_placeStatementHistory[index].timer.Reset();
2015-09-16 23:19:21 +03:00
}
// Statements are meaningful collections of phrases
2015-09-16 23:19:21 +03:00
class BotStatement
{
public:
BotStatement(BotChatterInterface *chatter, BotStatementType type, float expireDuration);
~BotStatement();
2015-09-16 23:19:21 +03:00
public:
BotChatterInterface *GetChatter() const { return m_chatter; }
CCSBot *GetOwner() const;
BotStatementType GetType() const { return m_type; } // return the type of statement this is
bool IsImportant() const; // return true if this statement is "important" and not personality chatter
bool HasSubject() const { return (m_subject != UNDEFINED_SUBJECT); }
void SetSubject(int playerID) { m_subject = playerID; } // who this statement is about
int GetSubject() const { return m_subject; } // who this statement is about
bool HasPlace() const { return (GetPlace()) ? true : false; }
Place GetPlace() const; // if this statement refers to a specific place, return that place
void SetPlace(Place where) { m_place = where; } // explicitly set place
bool HasCount() const; // return true if this statement has an associated count
bool IsRedundant(const BotStatement *say) const; // return true if this statement is the same as the given one
bool IsObsolete() const; // return true if this statement is no longer appropriate to say
void Convert(const BotStatement *say); // possibly change what were going to say base on what teammate is saying
void AppendPhrase(const BotPhrase *phrase);
void SetStartTime(float timestamp) { m_startTime = timestamp; } // define the earliest time this statement can be spoken
float GetStartTime() const { return m_startTime; }
2015-09-16 23:19:21 +03:00
enum ConditionType
{
IS_IN_COMBAT,
RADIO_SILENCE,
ENEMIES_REMAINING,
NUM_CONDITIONS,
};
void AddCondition(ConditionType condition); // conditions must be true for the statement to be spoken
bool IsValid() const; // verify all attached conditions
2015-09-16 23:19:21 +03:00
enum ContextType
{
CURRENT_ENEMY_COUNT,
REMAINING_ENEMY_COUNT,
SHORT_DELAY,
LONG_DELAY,
ACCUMULATE_ENEMIES_DELAY,
};
void AppendPhrase(ContextType contextPhrase); // special phrases that depend on the context
bool Update(); // emit statement over time, return false if statement is done
bool IsSpeaking() const { return m_isSpeaking; } // return true if this statement is currently being spoken
float GetTimestamp() const { return m_timestamp; } // get time statement was created (but not necessarily started talking)
void AttachMeme(BotMeme *meme); // attach a meme to this statement, to be transmitted to other friendly bots when spoken
2015-09-16 23:19:21 +03:00
public:
friend class BotChatterInterface;
BotChatterInterface *m_chatter; // the chatter system this statement is part of
BotStatement *m_next, *m_prev; // linked list hooks
BotStatementType m_type; // what kind of statement this is
int m_subject; // who this subject is about
Place m_place; // explicit place - note some phrases have implicit places as well
BotMeme *m_meme; // a statement can only have a single meme for now
float m_timestamp; // time when message was created
float m_startTime; // the earliest time this statement can be spoken
float m_expireTime; // time when this statement is no longer valid
float m_speakTimestamp; // time when message began being spoken
bool m_isSpeaking; // true if this statement is current being spoken
float m_nextTime; // time for next phrase to begin
2015-09-16 23:19:21 +03:00
enum { MAX_BOT_PHRASES = 4 };
struct
{
bool isPhrase;
union
{
const BotPhrase *phrase;
ContextType context;
};
}
m_statement[MAX_BOT_PHRASES];
2015-09-16 23:19:21 +03:00
enum { MAX_BOT_CONDITIONS = 4 };
ConditionType m_condition[MAX_BOT_CONDITIONS]; // conditions that must be true for the statement to be said
2015-09-16 23:19:21 +03:00
int m_conditionCount;
int m_index; // m_index refers to the phrase currently being spoken, or -1 if we havent started yet
2015-09-16 23:19:21 +03:00
int m_count;
};
2015-09-16 23:19:21 +03:00
// This class defines the interface to the bot radio chatter system
2015-09-16 23:19:21 +03:00
class BotChatterInterface
{
public:
BotChatterInterface() {};
2015-09-16 23:19:21 +03:00
BotChatterInterface(CCSBot *me);
~BotChatterInterface();
2015-09-16 23:19:21 +03:00
void Reset(); // reset to initial state
void Update(); // process ongoing chatter
2015-09-16 23:19:21 +03:00
void OnEvent(GameEventType event, CBaseEntity *entity, CBaseEntity *other); // invoked when event occurs in the game (some events have NULL entities)
void OnDeath(); // invoked when we die
2015-09-16 23:19:21 +03:00
enum VerbosityType
{
NORMAL, // full chatter
MINIMAL, // only scenario-critical events
RADIO, // use the standard radio instead
OFF // no chatter at all
2015-09-16 23:19:21 +03:00
};
VerbosityType GetVerbosity() const; // return our current level of verbosity
2015-09-16 23:19:21 +03:00
CCSBot *GetOwner() const { return m_me; }
bool IsTalking() const; // return true if we are currently talking
float GetRadioSilenceDuration(); // return time since any teammate said anything
void ResetRadioSilenceDuration();
2015-09-16 23:19:21 +03:00
enum { MUST_ADD = 1 };
void AddStatement(BotStatement *statement, bool mustAdd = false); // register a statement for speaking
void RemoveStatement(BotStatement *statement); // remove a statement
2015-09-16 23:19:21 +03:00
BotStatement *GetActiveStatement(); // returns the statement that is being spoken, or is next to be spoken if no-one is speaking now
BotStatement *GetStatement() const; // returns our current statement, or NULL if we aren't speaking
2015-09-16 23:19:21 +03:00
int GetPitch() const { return m_pitch; }
2015-09-16 23:19:21 +03:00
// things the bots can say
2015-09-16 23:19:21 +03:00
void Say(const char *phraseName, float lifetime = 3.0f, float delay = 0.0f);
void AnnouncePlan(const char *phraseName, Place place);
void Affirmative();
void Negative();
void EnemySpotted(); // report enemy sightings
void KilledMyEnemy(int victimID);
void EnemiesRemaining();
void Clear(Place place);
void ReportIn(); // ask for current situation
void ReportingIn(); // report current situation
bool NeedBackup();
void PinnedDown();
void Scared();
void HeardNoise(const Vector *pos);
void TheyPickedUpTheBomb();
void GoingToPlantTheBomb(Place place);
void BombsiteClear(int zoneIndex);
void FoundPlantedBomb(int zoneIndex);
void PlantingTheBomb(Place place);
void SpottedBomber(CBasePlayer *bomber);
void SpottedLooseBomb(CBaseEntity *bomb);
void GuardingLooseBomb(CBaseEntity *bomb);
void RequestBombLocation();
2016-01-25 20:02:57 +03:00
#define IS_PLAN true
void GuardingHostages(Place place, bool isPlan = false);
void GuardingHostageEscapeZone(bool isPlan = false);
void HostagesBeingTaken();
void HostagesTaken();
void TalkingToHostages();
void EscortingHostages();
void HostageDown();
void CelebrateWin();
void Encourage(const char *phraseName, float repeatInterval = 10.0f, float lifetime = 3.0f); // "encourage" the player to do the scenario
void KilledFriend();
void FriendlyFire();
bool SeesAtLeastOneEnemy() const { return m_seeAtLeastOneEnemy; }
2015-09-16 23:19:21 +03:00
private:
BotStatement *m_statementList; // list of all active/pending messages for this bot
void ReportEnemies(); // track nearby enemy count and generate enemy activity statements
bool ShouldSpeak() const; // return true if we speaking makes sense now
2015-09-16 23:19:21 +03:00
CCSBot *m_me; // the bot this chatter is for
2015-09-16 23:19:21 +03:00
bool m_seeAtLeastOneEnemy;
float m_timeWhenSawFirstEnemy;
bool m_reportedEnemies;
bool m_requestedBombLocation; // true if we already asked where the bomb has been planted
2015-09-16 23:19:21 +03:00
int m_pitch;
static IntervalTimer IMPL(m_radioSilenceInterval)[2]; // one timer for each team
2015-09-16 23:19:21 +03:00
IntervalTimer m_needBackupInterval;
IntervalTimer m_spottedBomberInterval;
IntervalTimer m_scaredInterval;
IntervalTimer m_planInterval;
CountdownTimer m_spottedLooseBombTimer;
CountdownTimer m_heardNoiseTimer;
CountdownTimer m_escortingHostageTimer;
static CountdownTimer IMPL(m_encourageTimer); // timer to know when we can "encourage" the human player again - shared by all bots
};
2015-09-16 23:19:21 +03:00
inline BotChatterInterface::VerbosityType BotChatterInterface::GetVerbosity() const
2015-06-30 12:46:07 +03:00
{
const char *string = cv_bot_chatter.string;
if (string == nullptr)
2015-06-30 12:46:07 +03:00
return NORMAL;
if (string[0] == 'm' || string[0] == 'M')
return MINIMAL;
if (string[0] == 'r' || string[0] == 'R')
return RADIO;
if (string[0] == 'o' || string[0] == 'O')
return OFF;
return NORMAL;
2015-09-16 23:19:21 +03:00
}
inline bool BotChatterInterface::IsTalking() const
2015-09-16 23:19:21 +03:00
{
if (m_statementList) {
2015-06-30 12:46:07 +03:00
return m_statementList->IsSpeaking();
}
2015-06-30 12:46:07 +03:00
return false;
}
inline BotStatement *BotChatterInterface::GetStatement() const
2015-06-30 12:46:07 +03:00
{
return m_statementList;
}
extern BotPhraseManager *TheBotPhrases;
extern CBaseEntity *g_pSelectedZombieSpawn;
2015-06-30 12:46:07 +03:00
inline void BotChatterInterface::Say(const char *phraseName, float lifetime, float delay)
{
BotStatement *say = new BotStatement(this, REPORT_MY_INTENTION, lifetime);
2015-06-30 12:46:07 +03:00
say->AppendPhrase(TheBotPhrases->GetPhrase(phraseName));
if (delay > 0.0f)
say->SetStartTime(gpGlobals->time + delay);
2015-06-30 12:46:07 +03:00
AddStatement(say);
2015-06-30 12:46:07 +03:00
}
const Vector *GetRandomSpotAtPlace(Place place);
2015-06-30 12:46:07 +03:00
#endif // CS_BOT_CHATTER_H