Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

193 lines
7.1 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $Workfile: $
  6. // $Date: $
  7. //
  8. //-----------------------------------------------------------------------------
  9. // $Log: $
  10. //
  11. // $NoKeywords: $
  12. //=============================================================================//
  13. #if !defined( IGAMEEVENTS_H )
  14. #define IGAMEEVENTS_H
  15. #ifdef _WIN32
  16. #pragma once
  17. #endif
  18. #include "tier1/interface.h"
  19. #define INTERFACEVERSION_GAMEEVENTSMANAGER "GAMEEVENTSMANAGER001" // old game event manager, don't use it!
  20. #define INTERFACEVERSION_GAMEEVENTSMANAGER2 "GAMEEVENTSMANAGER002" // new game event manager,
  21. #include "tier1/bitbuf.h"
  22. //-----------------------------------------------------------------------------
  23. // Purpose: Engine interface into global game event management
  24. //-----------------------------------------------------------------------------
  25. /*
  26. The GameEventManager keeps track and fires of all global game events. Game events
  27. are fired by game.dll for events like player death or team wins. Each event has a
  28. unique name and comes with a KeyValue structure providing informations about this
  29. event. Some events are generated also by the engine.
  30. Events are networked to connected clients and invoked there to. Therefore you
  31. have to specify all data fields and there data types in an public resource
  32. file which is parsed by server and broadcasted to it's clients. A typical game
  33. event is defined like this:
  34. "game_start" // a new game starts
  35. {
  36. "roundslimit" "long" // max round
  37. "timelimit" "long" // time limit
  38. "fraglimit" "long" // frag limit
  39. "objective" "string" // round objective
  40. }
  41. All events must have unique names (case sensitive) and may have a list
  42. of data fields. each data field must specify a data type, so the engine
  43. knows how to serialize/unserialize that event for network transmission.
  44. Valid data types are string, float, long, short, byte & bool. If a
  45. data field should not be broadcasted to clients, use the type "local".
  46. */
  47. #define MAX_EVENT_NAME_LENGTH 32 // max game event name length
  48. #define MAX_EVENT_BITS 9 // max bits needed for an event index
  49. #define MAX_EVENT_NUMBER (1<<MAX_EVENT_BITS) // max number of events allowed
  50. #define MAX_EVENT_BYTES 1024 // max size in bytes for a serialized event
  51. class KeyValues;
  52. class CGameEvent;
  53. abstract_class IGameEvent
  54. {
  55. public:
  56. virtual ~IGameEvent() {};
  57. virtual const char *GetName() const = 0; // get event name
  58. virtual bool IsReliable() const = 0; // if event handled reliable
  59. virtual bool IsLocal() const = 0; // if event is never networked
  60. virtual bool IsEmpty(const char *keyName = NULL) = 0; // check if data field exists
  61. // Data access
  62. virtual bool GetBool( const char *keyName = NULL, bool defaultValue = false ) = 0;
  63. virtual int GetInt( const char *keyName = NULL, int defaultValue = 0 ) = 0;
  64. virtual float GetFloat( const char *keyName = NULL, float defaultValue = 0.0f ) = 0;
  65. virtual const char *GetString( const char *keyName = NULL, const char *defaultValue = "" ) = 0;
  66. virtual void SetBool( const char *keyName, bool value ) = 0;
  67. virtual void SetInt( const char *keyName, int value ) = 0;
  68. virtual void SetFloat( const char *keyName, float value ) = 0;
  69. virtual void SetString( const char *keyName, const char *value ) = 0;
  70. };
  71. abstract_class IGameEventListener2
  72. {
  73. public:
  74. virtual ~IGameEventListener2( void ) {};
  75. // FireEvent is called by EventManager if event just occurred
  76. // KeyValue memory will be freed by manager if not needed anymore
  77. virtual void FireGameEvent( IGameEvent *event ) = 0;
  78. };
  79. abstract_class IGameEventManager2 : public IBaseInterface
  80. {
  81. public:
  82. virtual ~IGameEventManager2( void ) {};
  83. // load game event descriptions from a file eg "resource\gameevents.res"
  84. virtual int LoadEventsFromFile( const char *filename ) = 0;
  85. // removes all and anything
  86. virtual void Reset() = 0;
  87. // adds a listener for a particular event
  88. virtual bool AddListener( IGameEventListener2 *listener, const char *name, bool bServerSide ) = 0;
  89. // returns true if this listener is listens to given event
  90. virtual bool FindListener( IGameEventListener2 *listener, const char *name ) = 0;
  91. // removes a listener
  92. virtual void RemoveListener( IGameEventListener2 *listener) = 0;
  93. // create an event by name, but doesn't fire it. returns NULL is event is not
  94. // known or no listener is registered for it. bForce forces the creation even if no listener is active
  95. virtual IGameEvent *CreateEvent( const char *name, bool bForce = false ) = 0;
  96. // fires a server event created earlier, if bDontBroadcast is set, event is not send to clients
  97. virtual bool FireEvent( IGameEvent *event, bool bDontBroadcast = false ) = 0;
  98. // fires an event for the local client only, should be used only by client code
  99. virtual bool FireEventClientSide( IGameEvent *event ) = 0;
  100. // create a new copy of this event, must be free later
  101. virtual IGameEvent *DuplicateEvent( IGameEvent *event ) = 0;
  102. // if an event was created but not fired for some reason, it has to bee freed, same UnserializeEvent
  103. virtual void FreeEvent( IGameEvent *event ) = 0;
  104. // write/read event to/from bitbuffer
  105. virtual bool SerializeEvent( IGameEvent *event, bf_write *buf ) = 0;
  106. virtual IGameEvent *UnserializeEvent( bf_read *buf ) = 0; // create new KeyValues, must be deleted
  107. };
  108. // the old game event manager interface, don't use it. Rest is legacy support:
  109. abstract_class IGameEventListener
  110. {
  111. public:
  112. virtual ~IGameEventListener( void ) {};
  113. // FireEvent is called by EventManager if event just occurred
  114. // KeyValue memory will be freed by manager if not needed anymore
  115. virtual void FireGameEvent( KeyValues * event) = 0;
  116. };
  117. abstract_class IGameEventManager : public IBaseInterface
  118. {
  119. public:
  120. virtual ~IGameEventManager( void ) {};
  121. // load game event descriptions from a file eg "resource\gameevents.res"
  122. virtual int LoadEventsFromFile( const char * filename ) = 0;
  123. // removes all and anything
  124. virtual void Reset() = 0;
  125. virtual KeyValues *GetEvent(const char * name) = 0; // returns keys for event
  126. // adds a listener for a particular event
  127. virtual bool AddListener( IGameEventListener * listener, const char * event, bool bIsServerSide ) = 0;
  128. // registers for all known events
  129. virtual bool AddListener( IGameEventListener * listener, bool bIsServerSide ) = 0;
  130. // removes a listener
  131. virtual void RemoveListener( IGameEventListener * listener) = 0;
  132. // fires an global event, specific event data is stored in KeyValues
  133. // local listeners will receive the event instantly
  134. // a network message will be send to all connected clients to invoke
  135. // the same event there
  136. virtual bool FireEvent( KeyValues * event ) = 0;
  137. // fire a side server event, that it wont be broadcasted to player clients
  138. virtual bool FireEventServerOnly( KeyValues * event ) = 0;
  139. // fires an event only on this local client
  140. // can be used to fake events coming over the network
  141. virtual bool FireEventClientOnly( KeyValues * event ) = 0;
  142. // write/read event to/from bitbuffer
  143. virtual bool SerializeKeyValues( KeyValues *event, bf_write *buf, CGameEvent *eventtype = NULL ) = 0;
  144. virtual KeyValues *UnserializeKeyValue( bf_read *msg ) = 0; // create new KeyValues, must be deleted
  145. };
  146. #endif // IGAMEEVENTS_H