File: server\server.h

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