vkQuake2 doxygen  1.0 dev
sv_game.c
Go to the documentation of this file.
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 // sv_game.c -- interface to the game dll
21 
22 #include "server.h"
23 
25 
26 
27 /*
28 ===============
29 PF_Unicast
30 
31 Sends the contents of the mutlicast buffer to a single client
32 ===============
33 */
34 void PF_Unicast (edict_t *ent, qboolean reliable)
35 {
36  int p;
37  client_t *client;
38 
39  if (!ent)
40  return;
41 
42  p = NUM_FOR_EDICT(ent);
43  if (p < 1 || p > maxclients->value)
44  return;
45 
46  client = svs.clients + (p-1);
47 
48  if (reliable)
50  else
52 
54 }
55 
56 
57 /*
58 ===============
59 PF_dprintf
60 
61 Debug print to server console
62 ===============
63 */
64 void PF_dprintf (char *fmt, ...)
65 {
66  char msg[1024];
67  va_list argptr;
68 
69  va_start (argptr,fmt);
70  vsnprintf (msg, 1024, fmt, argptr);
71  va_end (argptr);
72 
73  Com_Printf ("%s", msg);
74 }
75 
76 
77 /*
78 ===============
79 PF_cprintf
80 
81 Print to a single client
82 ===============
83 */
84 void PF_cprintf (edict_t *ent, int level, char *fmt, ...)
85 {
86  char msg[1024];
87  va_list argptr;
88  int n = 0;
89 
90  if (ent)
91  {
92  n = NUM_FOR_EDICT(ent);
93  if (n < 1 || n > maxclients->value)
94  Com_Error (ERR_DROP, "cprintf to a non-client");
95  }
96 
97  va_start (argptr,fmt);
98  vsnprintf (msg, 1024, fmt, argptr);
99  va_end (argptr);
100 
101  if (ent)
102  SV_ClientPrintf (svs.clients+(n-1), level, "%s", msg);
103  else
104  Com_Printf ("%s", msg);
105 }
106 
107 
108 /*
109 ===============
110 PF_centerprintf
111 
112 centerprint to a single client
113 ===============
114 */
115 void PF_centerprintf (edict_t *ent, char *fmt, ...)
116 {
117  char msg[1024];
118  va_list argptr;
119  int n;
120 
121  n = NUM_FOR_EDICT(ent);
122  if (n < 1 || n > maxclients->value)
123  return; // Com_Error (ERR_DROP, "centerprintf to a non-client");
124 
125  va_start (argptr,fmt);
126  vsnprintf (msg, 1024, fmt, argptr);
127  va_end (argptr);
128 
131  PF_Unicast (ent, true);
132 }
133 
134 
135 /*
136 ===============
137 PF_error
138 
139 Abort the server with a game error
140 ===============
141 */
142 void PF_error (char *fmt, ...)
143 {
144  char msg[1024];
145  va_list argptr;
146 
147  va_start (argptr,fmt);
148  vsnprintf (msg, 1024, fmt, argptr);
149  va_end (argptr);
150 
151  Com_Error (ERR_DROP, "Game Error: %s", msg);
152 }
153 
154 
155 /*
156 =================
157 PF_setmodel
158 
159 Also sets mins and maxs for inline bmodels
160 =================
161 */
162 void PF_setmodel (edict_t *ent, char *name)
163 {
164  int i;
165  cmodel_t *mod;
166 
167  if (!name)
168  Com_Error (ERR_DROP, "PF_setmodel: NULL");
169 
170  i = SV_ModelIndex (name);
171 
172 // ent->model = name;
173  ent->s.modelindex = i;
174 
175 // if it is an inline model, get the size information for it
176  if (name[0] == '*')
177  {
178  mod = CM_InlineModel (name);
179  VectorCopy (mod->mins, ent->mins);
180  VectorCopy (mod->maxs, ent->maxs);
181  SV_LinkEdict (ent);
182  }
183 
184 }
185 
186 /*
187 ===============
188 PF_Configstring
189 
190 ===============
191 */
192 void PF_Configstring (int index, char *val)
193 {
194  if (index < 0 || index >= MAX_CONFIGSTRINGS)
195  Com_Error (ERR_DROP, "configstring: bad index %i\n", index);
196 
197  if (!val)
198  val = "";
199 
200  // change the string in sv
201  strcpy (sv.configstrings[index], val);
202 
203  if (sv.state != ss_loading)
204  { // send the update to everyone
205  SZ_Clear (&sv.multicast);
207  MSG_WriteShort (&sv.multicast, index);
208  MSG_WriteString (&sv.multicast, val);
209 
211  }
212 }
213 
214 
215 
216 void PF_WriteChar (int c) {MSG_WriteChar (&sv.multicast, c);}
217 void PF_WriteByte (int c) {MSG_WriteByte (&sv.multicast, c);}
219 void PF_WriteLong (int c) {MSG_WriteLong (&sv.multicast, c);}
220 void PF_WriteFloat (float f) {MSG_WriteFloat (&sv.multicast, f);}
224 void PF_WriteAngle (float f) {MSG_WriteAngle (&sv.multicast, f);}
225 
226 
227 /*
228 =================
229 PF_inPVS
230 
231 Also checks portalareas so that doors block sight
232 =================
233 */
235 {
236  int leafnum;
237  int cluster;
238  int area1, area2;
239  byte *mask;
240 
241  leafnum = CM_PointLeafnum (p1);
242  cluster = CM_LeafCluster (leafnum);
243  area1 = CM_LeafArea (leafnum);
244  mask = CM_ClusterPVS (cluster);
245 
246  leafnum = CM_PointLeafnum (p2);
247  cluster = CM_LeafCluster (leafnum);
248  area2 = CM_LeafArea (leafnum);
249  if ( mask && (!(mask[cluster>>3] & (1<<(cluster&7)) ) ) )
250  return false;
251  if (!CM_AreasConnected (area1, area2))
252  return false; // a door blocks sight
253  return true;
254 }
255 
256 
257 /*
258 =================
259 PF_inPHS
260 
261 Also checks portalareas so that doors block sound
262 =================
263 */
265 {
266  int leafnum;
267  int cluster;
268  int area1, area2;
269  byte *mask;
270 
271  leafnum = CM_PointLeafnum (p1);
272  cluster = CM_LeafCluster (leafnum);
273  area1 = CM_LeafArea (leafnum);
274  mask = CM_ClusterPHS (cluster);
275 
276  leafnum = CM_PointLeafnum (p2);
277  cluster = CM_LeafCluster (leafnum);
278  area2 = CM_LeafArea (leafnum);
279  if ( mask && (!(mask[cluster>>3] & (1<<(cluster&7)) ) ) )
280  return false; // more than one bounce away
281  if (!CM_AreasConnected (area1, area2))
282  return false; // a door blocks hearing
283 
284  return true;
285 }
286 
287 void PF_StartSound (edict_t *entity, int channel, int sound_num, float volume,
288  float attenuation, float timeofs)
289 {
290  if (!entity)
291  return;
292  SV_StartSound (NULL, entity, channel, sound_num, volume, attenuation, timeofs);
293 }
294 
295 //==============================================
296 
297 /*
298 ===============
299 SV_ShutdownGameProgs
300 
301 Called when either the entire server is being killed, or
302 it is changing to a different game directory.
303 ===============
304 */
306 {
307  if (!ge)
308  return;
309  ge->Shutdown ();
310  Sys_UnloadGame ();
311  ge = NULL;
312 }
313 
314 /*
315 ===============
316 SV_InitGameProgs
317 
318 Init the game subsystem for a new map
319 ===============
320 */
321 void SCR_DebugGraph (float value, int color);
322 
323 void SV_InitGameProgs (void)
324 {
325  game_import_t import;
326 
327  // unload anything we have now
328  if (ge)
330 
331 
332  // load a new game dll
333  import.multicast = SV_Multicast;
334  import.unicast = PF_Unicast;
335  import.bprintf = SV_BroadcastPrintf;
336  import.dprintf = PF_dprintf;
337  import.cprintf = PF_cprintf;
338  import.centerprintf = PF_centerprintf;
339  import.error = PF_error;
340 
341  import.linkentity = SV_LinkEdict;
342  import.unlinkentity = SV_UnlinkEdict;
343  import.BoxEdicts = SV_AreaEdicts;
344  import.trace = SV_Trace;
345  import.pointcontents = SV_PointContents;
346  import.setmodel = PF_setmodel;
347  import.inPVS = PF_inPVS;
348  import.inPHS = PF_inPHS;
349  import.Pmove = Pmove;
350 
351  import.modelindex = SV_ModelIndex;
352  import.soundindex = SV_SoundIndex;
353  import.imageindex = SV_ImageIndex;
354 
355  import.configstring = PF_Configstring;
356  import.sound = PF_StartSound;
357  import.positioned_sound = SV_StartSound;
358 
359  import.WriteChar = PF_WriteChar;
360  import.WriteByte = PF_WriteByte;
361  import.WriteShort = PF_WriteShort;
362  import.WriteLong = PF_WriteLong;
363  import.WriteFloat = PF_WriteFloat;
364  import.WriteString = PF_WriteString;
365  import.WritePosition = PF_WritePos;
366  import.WriteDir = PF_WriteDir;
367  import.WriteAngle = PF_WriteAngle;
368 
369  import.TagMalloc = Z_TagMalloc;
370  import.TagFree = Z_Free;
371  import.FreeTags = Z_FreeTags;
372 
373  import.cvar = Cvar_Get;
374  import.cvar_set = Cvar_Set;
375  import.cvar_forceset = Cvar_ForceSet;
376 
377  import.argc = Cmd_Argc;
378  import.argv = Cmd_Argv;
379  import.args = Cmd_Args;
380  import.AddCommandString = Cbuf_AddText;
381 
382  import.DebugGraph = SCR_DebugGraph;
383  import.SetAreaPortalState = CM_SetAreaPortalState;
384  import.AreasConnected = CM_AreasConnected;
385 
386  ge = (game_export_t *)Sys_GetGameAPI (&import);
387 
388  if (!ge)
389  Com_Error (ERR_DROP, "failed to load game DLL");
391  Com_Error (ERR_DROP, "game is version %i, not %i", ge->apiversion,
393 
394  ge->Init ();
395 }
396 
game_import_t
Definition: game.h:102
sv
server_t sv
Definition: sv_init.c:24
edict_s::s
entity_state_t s
Definition: g_local.h:970
value
GLfloat value
Definition: qgl_win.c:63
svc_centerprint
@ svc_centerprint
Definition: qcommon.h:241
client_s::datagram
sizebuf_t datagram
Definition: server.h:120
GAME_API_VERSION
#define GAME_API_VERSION
Definition: game.h:23
Z_TagMalloc
void * Z_TagMalloc(int size, int tag)
Definition: common.c:1172
PF_WritePos
void PF_WritePos(vec3_t pos)
Definition: sv_game.c:222
cmodel_s::maxs
vec3_t maxs
Definition: q_shared.h:434
maxclients
cvar_t * maxclients
Definition: g_main.c:44
PF_setmodel
void PF_setmodel(edict_t *ent, char *name)
Definition: sv_game.c:162
PF_WriteByte
void PF_WriteByte(int c)
Definition: sv_game.c:217
SV_LinkEdict
void SV_LinkEdict(edict_t *ent)
Definition: sv_world.c:166
netchan_t::message
sizebuf_t message
Definition: qcommon.h:608
game_export_t::Shutdown
void(* Shutdown)(void)
Definition: game.h:189
PF_WriteDir
void PF_WriteDir(vec3_t dir)
Definition: sv_game.c:223
game_export_t::apiversion
int apiversion
Definition: game.h:183
qboolean
qboolean
Definition: q_shared.h:63
i
int i
Definition: q_shared.c:305
server_t::multicast
sizebuf_t multicast
Definition: server.h:61
game_export_t::Init
void(* Init)(void)
Definition: game.h:188
MSG_WriteLong
void MSG_WriteLong(sizebuf_t *sb, int c)
Definition: common.c:330
SV_BroadcastPrintf
void SV_BroadcastPrintf(int level, char *fmt,...)
Definition: sv_send.c:89
PF_WriteChar
void PF_WriteChar(int c)
Definition: sv_game.c:216
SV_ShutdownGameProgs
void SV_ShutdownGameProgs(void)
Definition: sv_game.c:305
edict_s::mins
vec3_t mins
Definition: g_local.h:990
Sys_UnloadGame
void Sys_UnloadGame(void)
Definition: sys_win.c:488
Cmd_Args
char * Cmd_Args(void)
Definition: cmd.c:531
PF_inPVS
qboolean PF_inPVS(vec3_t p1, vec3_t p2)
Definition: sv_game.c:234
sizebuf_s::data
byte * data
Definition: qcommon.h:96
Cvar_Get
cvar_t * Cvar_Get(char *var_name, char *var_value, int flags)
Definition: cvar.c:127
SZ_Write
void SZ_Write(sizebuf_t *buf, void *data, int length)
Definition: common.c:921
SZ_Clear
void SZ_Clear(sizebuf_t *buf)
Definition: common.c:892
PF_WriteLong
void PF_WriteLong(int c)
Definition: sv_game.c:219
CM_LeafArea
int CM_LeafArea(int leafnum)
Definition: cmodel.c:681
PF_dprintf
void PF_dprintf(char *fmt,...)
Definition: sv_game.c:64
Cmd_Argv
char * Cmd_Argv(int arg)
Definition: cmd.c:517
msg
cvar_t * msg
Definition: cl_main.c:83
Cmd_Argc
int Cmd_Argc(void)
Definition: cmd.c:507
Cvar_ForceSet
cvar_t * Cvar_ForceSet(char *var_name, char *value)
Definition: cvar.c:268
server_static_t::clients
client_t * clients
Definition: server.h:168
client_s::netchan
netchan_t netchan
Definition: server.h:134
SV_AreaEdicts
int SV_AreaEdicts(vec3_t mins, vec3_t maxs, edict_t **list, int maxcount, int areatype)
Definition: sv_world.c:409
edict_s
Definition: g_local.h:968
CM_ClusterPVS
byte * CM_ClusterPVS(int cluster)
Definition: cmodel.c:1575
CM_AreasConnected
qboolean CM_AreasConnected(int area1, int area2)
Definition: cmodel.c:1662
SV_UnlinkEdict
void SV_UnlinkEdict(edict_t *ent)
Definition: sv_world.c:150
PF_Unicast
void PF_Unicast(edict_t *ent, qboolean reliable)
Definition: sv_game.c:34
NUM_FOR_EDICT
#define NUM_FOR_EDICT(e)
Definition: server.h:70
MSG_WriteDir
void MSG_WriteDir(sizebuf_t *sb, vec3_t dir)
Definition: common.c:438
SV_StartSound
void SV_StartSound(vec3_t origin, edict_t *entity, int channel, int soundindex, float volume, float attenuation, float timeofs)
Definition: sv_send.c:272
PF_centerprintf
void PF_centerprintf(edict_t *ent, char *fmt,...)
Definition: sv_game.c:115
SV_Trace
trace_t SV_Trace(vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end, edict_t *passedict, int contentmask)
Definition: sv_world.c:625
MULTICAST_ALL_R
@ MULTICAST_ALL_R
Definition: q_shared.h:119
Cbuf_AddText
void Cbuf_AddText(char *text)
Definition: cmd.c:90
cvar_s::value
float value
Definition: q_shared.h:331
cmodel_s::mins
vec3_t mins
Definition: q_shared.h:434
MSG_WritePos
void MSG_WritePos(sizebuf_t *sb, vec3_t pos)
Definition: common.c:369
MSG_WriteString
void MSG_WriteString(sizebuf_t *sb, char *s)
Definition: common.c:356
NULL
#define NULL
Definition: q_shared.h:67
MSG_WriteShort
void MSG_WriteShort(sizebuf_t *sb, int c)
Definition: common.c:316
MSG_WriteByte
void MSG_WriteByte(sizebuf_t *sb, int c)
Definition: common.c:303
Com_Error
void Com_Error(int code, char *fmt,...)
Definition: common.c:181
cmodel_s
Definition: q_shared.h:432
PF_error
void PF_error(char *fmt,...)
Definition: sv_game.c:142
ERR_DROP
#define ERR_DROP
Definition: qcommon.h:744
CM_InlineModel
cmodel_t * CM_InlineModel(char *name)
Definition: cmodel.c:639
svs
server_static_t svs
Definition: sv_init.c:23
MSG_WriteChar
void MSG_WriteChar(sizebuf_t *sb, int c)
Definition: common.c:290
MAX_CONFIGSTRINGS
#define MAX_CONFIGSTRINGS
Definition: q_shared.h:1119
SV_Multicast
void SV_Multicast(vec3_t origin, multicast_t to)
Definition: sv_send.c:161
name
cvar_t * name
Definition: cl_main.c:79
s
static fixed16_t s
Definition: r_scan.c:30
PF_WriteFloat
void PF_WriteFloat(float f)
Definition: sv_game.c:220
SV_ClientPrintf
void SV_ClientPrintf(client_t *cl, int level, char *fmt,...)
Definition: sv_send.c:65
VectorCopy
#define VectorCopy(a, b)
Definition: q_shared.h:165
game_export_t
Definition: game.h:181
CM_ClusterPHS
byte * CM_ClusterPHS(int cluster)
Definition: cmodel.c:1584
ge
game_export_t * ge
Definition: sv_game.c:24
vec3_origin
vec3_t vec3_origin
Definition: q_shared.c:24
Z_Free
void Z_Free(void *ptr)
Definition: common.c:1122
MSG_WriteAngle
void MSG_WriteAngle(sizebuf_t *sb, float f)
Definition: common.c:376
SV_PointContents
int SV_PointContents(vec3_t p)
Definition: sv_world.c:432
level
GLint level
Definition: qgl_win.c:116
svc_configstring
@ svc_configstring
Definition: qcommon.h:239
PF_inPHS
qboolean PF_inPHS(vec3_t p1, vec3_t p2)
Definition: sv_game.c:264
CM_LeafCluster
int CM_LeafCluster(int leafnum)
Definition: cmodel.c:674
SV_SoundIndex
int SV_SoundIndex(char *name)
Definition: sv_init.c:69
SV_InitGameProgs
void SV_InitGameProgs(void)
Definition: sv_game.c:323
edict_s::maxs
vec3_t maxs
Definition: g_local.h:990
entity_state_s::modelindex
int modelindex
Definition: q_shared.h:1152
server_t::state
server_state_t state
Definition: server.h:45
sizebuf_s::cursize
int cursize
Definition: qcommon.h:98
SV_ImageIndex
int SV_ImageIndex(char *name)
Definition: sv_init.c:74
Cvar_Set
cvar_t * Cvar_Set(char *var_name, char *value)
Definition: cvar.c:278
Pmove
void Pmove(pmove_t *pmove)
Definition: pmove.c:1240
client_s
Definition: server.h:95
SCR_DebugGraph
void SCR_DebugGraph(float value, int color)
Definition: cl_scrn.c:137
Com_Printf
void Com_Printf(char *fmt,...)
Definition: common.c:104
MSG_WriteFloat
void MSG_WriteFloat(sizebuf_t *sb, float f)
Definition: common.c:341
server.h
server_t::configstrings
char configstrings[MAX_CONFIGSTRINGS][MAX_QPATH]
Definition: server.h:56
mask
GLint GLuint mask
Definition: qgl_win.c:317
CM_SetAreaPortalState
void CM_SetAreaPortalState(int portalnum, qboolean open)
Definition: cmodel.c:1653
PF_cprintf
void PF_cprintf(edict_t *ent, int level, char *fmt,...)
Definition: sv_game.c:84
ss_loading
@ ss_loading
Definition: server.h:34
channel
Definition: jar_mod.h:142
PF_WriteAngle
void PF_WriteAngle(float f)
Definition: sv_game.c:224
SV_ModelIndex
int SV_ModelIndex(char *name)
Definition: sv_init.c:64
vec3_t
vec_t vec3_t[3]
Definition: q_shared.h:134
Sys_GetGameAPI
void * Sys_GetGameAPI(void *parms)
Definition: sys_win.c:502
CM_PointLeafnum
int CM_PointLeafnum(vec3_t p)
Definition: cmodel.c:826
PF_WriteShort
void PF_WriteShort(int c)
Definition: sv_game.c:218
PF_StartSound
void PF_StartSound(edict_t *entity, int channel, int sound_num, float volume, float attenuation, float timeofs)
Definition: sv_game.c:287
PF_Configstring
void PF_Configstring(int index, char *val)
Definition: sv_game.c:192
PF_WriteString
void PF_WriteString(char *s)
Definition: sv_game.c:221
Z_FreeTags
void Z_FreeTags(int tag)
Definition: common.c:1155