server.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. Copyright (C) 1997-2001 Id Software, Inc.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. See the GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. // server.h
  16. //define PARANOID // speed sapping error checking
  17. #include "../qcommon/qcommon.h"
  18. #include "../game/game.h"
  19. //=============================================================================
  20. #define MAX_MASTERS 8 // max recipients for heartbeat packets
  21. typedef enum {
  22. ss_dead, // no map loaded
  23. ss_loading, // spawning level edicts
  24. ss_game, // actively running
  25. ss_cinematic,
  26. ss_demo,
  27. ss_pic
  28. } server_state_t;
  29. // some qc commands are only valid before the server has finished
  30. // initializing (precache commands, static sounds / objects, etc)
  31. typedef struct
  32. {
  33. server_state_t state; // precache commands are only valid during load
  34. qboolean attractloop; // running cinematics and demos for the local system only
  35. qboolean loadgame; // client begins should reuse existing entity
  36. unsigned time; // always sv.framenum * 100 msec
  37. int framenum;
  38. char name[MAX_QPATH]; // map name, or cinematic name
  39. struct cmodel_s *models[MAX_MODELS];
  40. char configstrings[MAX_CONFIGSTRINGS][MAX_QPATH];
  41. entity_state_t baselines[MAX_EDICTS];
  42. // the multicast buffer is used to send a message to a set of clients
  43. // it is only used to marshall data until SV_Multicast is called
  44. sizebuf_t multicast;
  45. byte multicast_buf[MAX_MSGLEN];
  46. // demo server information
  47. FILE *demofile;
  48. qboolean timedemo; // don't time sync
  49. } server_t;
  50. #define EDICT_NUM(n) ((edict_t *)((byte *)ge->edicts + ge->edict_size*(n)))
  51. #define NUM_FOR_EDICT(e) ( ((byte *)(e)-(byte *)ge->edicts ) / ge->edict_size)
  52. typedef enum
  53. {
  54. cs_free, // can be reused for a new connection
  55. cs_zombie, // client has been disconnected, but don't reuse
  56. // connection for a couple seconds
  57. cs_connected, // has been assigned to a client_t, but not in game yet
  58. cs_spawned // client is fully in game
  59. } client_state_t;
  60. typedef struct
  61. {
  62. int areabytes;
  63. byte areabits[MAX_MAP_AREAS/8]; // portalarea visibility bits
  64. player_state_t ps;
  65. int num_entities;
  66. int first_entity; // into the circular sv_packet_entities[]
  67. int senttime; // for ping calculations
  68. } client_frame_t;
  69. #define LATENCY_COUNTS 16
  70. #define RATE_MESSAGES 10
  71. typedef struct client_s
  72. {
  73. client_state_t state;
  74. char userinfo[MAX_INFO_STRING]; // name, etc
  75. int lastframe; // for delta compression
  76. usercmd_t lastcmd; // for filling in big drops
  77. int commandMsec; // every seconds this is reset, if user
  78. // commands exhaust it, assume time cheating
  79. int frame_latency[LATENCY_COUNTS];
  80. int ping;
  81. int message_size[RATE_MESSAGES]; // used to rate drop packets
  82. int rate;
  83. int surpressCount; // number of messages rate supressed
  84. edict_t *edict; // EDICT_NUM(clientnum+1)
  85. char name[32]; // extracted from userinfo, high bits masked
  86. int messagelevel; // for filtering printed messages
  87. // The datagram is written to by sound calls, prints, temp ents, etc.
  88. // It can be harmlessly overflowed.
  89. sizebuf_t datagram;
  90. byte datagram_buf[MAX_MSGLEN];
  91. client_frame_t frames[UPDATE_BACKUP]; // updates can be delta'd from here
  92. byte *download; // file being downloaded
  93. int downloadsize; // total bytes (can't use EOF because of paks)
  94. int downloadcount; // bytes sent
  95. int lastmessage; // sv.framenum when packet was last received
  96. int lastconnect;
  97. int challenge; // challenge of this user, randomly generated
  98. netchan_t netchan;
  99. } client_t;
  100. // a client can leave the server in one of four ways:
  101. // dropping properly by quiting or disconnecting
  102. // timing out if no valid messages are received for timeout.value seconds
  103. // getting kicked off by the server operator
  104. // a program error, like an overflowed reliable buffer
  105. //=============================================================================
  106. // MAX_CHALLENGES is made large to prevent a denial
  107. // of service attack that could cycle all of them
  108. // out before legitimate users connected
  109. #define MAX_CHALLENGES 1024
  110. typedef struct
  111. {
  112. netadr_t adr;
  113. int challenge;
  114. int time;
  115. } challenge_t;
  116. typedef struct
  117. {
  118. qboolean initialized; // sv_init has completed
  119. int realtime; // always increasing, no clamping, etc
  120. char mapcmd[MAX_TOKEN_CHARS]; // ie: *intro.cin+base
  121. int spawncount; // incremented each server start
  122. // used to check late spawns
  123. client_t *clients; // [maxclients->value];
  124. int num_client_entities; // maxclients->value*UPDATE_BACKUP*MAX_PACKET_ENTITIES
  125. int next_client_entities; // next client_entity to use
  126. entity_state_t *client_entities; // [num_client_entities]
  127. int last_heartbeat;
  128. challenge_t challenges[MAX_CHALLENGES]; // to prevent invalid IPs from connecting
  129. // serverrecord values
  130. FILE *demofile;
  131. sizebuf_t demo_multicast;
  132. byte demo_multicast_buf[MAX_MSGLEN];
  133. } server_static_t;
  134. //=============================================================================
  135. extern netadr_t net_from;
  136. extern sizebuf_t net_message;
  137. extern netadr_t master_adr[MAX_MASTERS]; // address of the master server
  138. extern server_static_t svs; // persistant server info
  139. extern server_t sv; // local server
  140. extern cvar_t *sv_paused;
  141. extern cvar_t *maxclients;
  142. extern cvar_t *sv_noreload; // don't reload level state when reentering
  143. extern cvar_t *sv_airaccelerate; // don't reload level state when reentering
  144. // development tool
  145. extern cvar_t *sv_enforcetime;
  146. extern client_t *sv_client;
  147. extern edict_t *sv_player;
  148. //===========================================================
  149. //
  150. // sv_main.c
  151. //
  152. void SV_FinalMessage (char *message, qboolean reconnect);
  153. void SV_DropClient (client_t *drop);
  154. int SV_ModelIndex (char *name);
  155. int SV_SoundIndex (char *name);
  156. int SV_ImageIndex (char *name);
  157. void SV_WriteClientdataToMessage (client_t *client, sizebuf_t *msg);
  158. void SV_ExecuteUserCommand (char *s);
  159. void SV_InitOperatorCommands (void);
  160. void SV_SendServerinfo (client_t *client);
  161. void SV_UserinfoChanged (client_t *cl);
  162. void Master_Heartbeat (void);
  163. void Master_Packet (void);
  164. //
  165. // sv_init.c
  166. //
  167. void SV_InitGame (void);
  168. void SV_Map (qboolean attractloop, char *levelstring, qboolean loadgame);
  169. //
  170. // sv_phys.c
  171. //
  172. void SV_PrepWorldFrame (void);
  173. //
  174. // sv_send.c
  175. //
  176. typedef enum {RD_NONE, RD_CLIENT, RD_PACKET} redirect_t;
  177. #define SV_OUTPUTBUF_LENGTH (MAX_MSGLEN - 16)
  178. extern char sv_outputbuf[SV_OUTPUTBUF_LENGTH];
  179. void SV_FlushRedirect (int sv_redirected, char *outputbuf);
  180. void SV_DemoCompleted (void);
  181. void SV_SendClientMessages (void);
  182. void SV_Multicast (vec3_t origin, multicast_t to);
  183. void SV_StartSound (vec3_t origin, edict_t *entity, int channel,
  184. int soundindex, float volume,
  185. float attenuation, float timeofs);
  186. void SV_ClientPrintf (client_t *cl, int level, char *fmt, ...);
  187. void SV_BroadcastPrintf (int level, char *fmt, ...);
  188. void SV_BroadcastCommand (char *fmt, ...);
  189. //
  190. // sv_user.c
  191. //
  192. void SV_Nextserver (void);
  193. void SV_ExecuteClientMessage (client_t *cl);
  194. //
  195. // sv_ccmds.c
  196. //
  197. void SV_ReadLevelFile (void);
  198. void SV_Status_f (void);
  199. //
  200. // sv_ents.c
  201. //
  202. void SV_WriteFrameToClient (client_t *client, sizebuf_t *msg);
  203. void SV_RecordDemoMessage (void);
  204. void SV_BuildClientFrame (client_t *client);
  205. void SV_Error (char *error, ...);
  206. //
  207. // sv_game.c
  208. //
  209. extern game_export_t *ge;
  210. void SV_InitGameProgs (void);
  211. void SV_ShutdownGameProgs (void);
  212. void SV_InitEdict (edict_t *e);
  213. //============================================================
  214. //
  215. // high level object sorting to reduce interaction tests
  216. //
  217. void SV_ClearWorld (void);
  218. // called after the world model has been loaded, before linking any entities
  219. void SV_UnlinkEdict (edict_t *ent);
  220. // call before removing an entity, and before trying to move one,
  221. // so it doesn't clip against itself
  222. void SV_LinkEdict (edict_t *ent);
  223. // Needs to be called any time an entity changes origin, mins, maxs,
  224. // or solid. Automatically unlinks if needed.
  225. // sets ent->v.absmin and ent->v.absmax
  226. // sets ent->leafnums[] for pvs determination even if the entity
  227. // is not solid
  228. int SV_AreaEdicts (vec3_t mins, vec3_t maxs, edict_t **list, int maxcount, int areatype);
  229. // fills in a table of edict pointers with edicts that have bounding boxes
  230. // that intersect the given area. It is possible for a non-axial bmodel
  231. // to be returned that doesn't actually intersect the area on an exact
  232. // test.
  233. // returns the number of pointers filled in
  234. // ??? does this always return the world?
  235. //===================================================================
  236. //
  237. // functions that interact with everything apropriate
  238. //
  239. int SV_PointContents (vec3_t p);
  240. // returns the CONTENTS_* value from the world at the given point.
  241. // Quake 2 extends this to also check entities, to allow moving liquids
  242. trace_t SV_Trace (vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end, edict_t *passedict, int contentmask);
  243. // mins and maxs are relative
  244. // if the entire move stays in a solid volume, trace.allsolid will be set,
  245. // trace.startsolid will be set, and trace.fraction will be 0
  246. // if the starting point is in a solid, it will be allowed to move out
  247. // to an open area
  248. // passedict is explicitly excluded from clipping checks (normally NULL)