icculus quake2 doxygen  1.0 dev
g_main.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 
21 #include "g_local.h"
22 
28 
30 int snd_fry;
32 
34 
49 
51 
54 
60 
66 
68 
72 
74 
76 
77 void SpawnEntities (char *mapname, char *entities, char *spawnpoint);
78 void ClientThink (edict_t *ent, usercmd_t *cmd);
79 qboolean ClientConnect (edict_t *ent, char *userinfo);
80 void ClientUserinfoChanged (edict_t *ent, char *userinfo);
81 void ClientDisconnect (edict_t *ent);
82 void ClientBegin (edict_t *ent);
83 void ClientCommand (edict_t *ent);
84 void RunEntity (edict_t *ent);
85 void WriteGame (char *filename, qboolean autosave);
86 void ReadGame (char *filename);
87 void WriteLevel (char *filename);
88 void ReadLevel (char *filename);
89 void InitGame (void);
90 void G_RunFrame (void);
91 
92 
93 //===================================================================
94 
95 
96 void ShutdownGame (void)
97 {
98  gi.dprintf ("==== ShutdownGame ====\n");
99 
101  gi.FreeTags (TAG_GAME);
102 }
103 
104 
105 /*
106 =================
107 GetGameAPI
108 
109 Returns a pointer to the structure with all entry points
110 and global variables
111 =================
112 */
114 {
115  gi = *import;
116 
121 
126 
133 
135 
137 
138  globals.edict_size = sizeof(edict_t);
139 
140  return &globals;
141 }
142 
143 #ifndef GAME_HARD_LINKED
144 // this is only here so the functions in q_shared.c and q_shwin.c can link
145 void Sys_Error (char *error, ...)
146 {
147  va_list argptr;
148  char text[1024];
149 
150  va_start (argptr, error);
151  vsprintf (text, error, argptr);
152  va_end (argptr);
153 
154  gi.error (ERR_FATAL, "%s", text);
155 }
156 
157 void Com_Printf (char *msg, ...)
158 {
159  va_list argptr;
160  char text[1024];
161 
162  va_start (argptr, msg);
163  vsprintf (text, msg, argptr);
164  va_end (argptr);
165 
166  gi.dprintf ("%s", text);
167 }
168 
169 #endif
170 
171 //======================================================================
172 
173 
174 /*
175 =================
176 ClientEndServerFrames
177 =================
178 */
180 {
181  int i;
182  edict_t *ent;
183 
184  // calc the player views now that all pushing
185  // and damage has been added
186  for (i=0 ; i<maxclients->value ; i++)
187  {
188  ent = g_edicts + 1 + i;
189  if (!ent->inuse || !ent->client)
190  continue;
191  ClientEndServerFrame (ent);
192  }
193 
194 }
195 
196 /*
197 =================
198 CreateTargetChangeLevel
199 
200 Returns the created target changelevel
201 =================
202 */
204 {
205  edict_t *ent;
206 
207  ent = G_Spawn ();
208  ent->classname = "target_changelevel";
209  Com_sprintf(level.nextmap, sizeof(level.nextmap), "%s", map);
210  ent->map = level.nextmap;
211  return ent;
212 }
213 
214 /*
215 =================
216 EndDMLevel
217 
218 The timelimit or fraglimit has been exceeded
219 =================
220 */
221 void EndDMLevel (void)
222 {
223  edict_t *ent;
224  char *s, *t, *f;
225  static const char *seps = " ,\n\r";
226 
227  // stay on same level flag
228  if ((int)dmflags->value & DF_SAME_LEVEL)
229  {
231  return;
232  }
233 
234  // see if it's in the map list
235  if (*sv_maplist->string) {
236  s = strdup(sv_maplist->string);
237  f = NULL;
238  t = strtok(s, seps);
239  while (t != NULL) {
240  if (Q_stricmp(t, level.mapname) == 0) {
241  // it's in the list, go to the next one
242  t = strtok(NULL, seps);
243  if (t == NULL) { // end of list, go to first one
244  if (f == NULL) // there isn't a first one, same level
246  else
248  } else
250  free(s);
251  return;
252  }
253  if (!f)
254  f = t;
255  t = strtok(NULL, seps);
256  }
257  free(s);
258  }
259 
260  if (level.nextmap[0]) // go to a specific map
262  else { // search for a changelevel
263  ent = G_Find (NULL, FOFS(classname), "target_changelevel");
264  if (!ent)
265  { // the map designer didn't include a changelevel,
266  // so create a fake ent that goes back to the same level
268  return;
269  }
270  BeginIntermission (ent);
271  }
272 }
273 
274 
275 /*
276 =================
277 CheckNeedPass
278 =================
279 */
280 void CheckNeedPass (void)
281 {
282  int need;
283 
284  // if password or spectator_password has changed, update needpass
285  // as needed
287  {
289 
290  need = 0;
291 
292  if (*password->string && Q_stricmp(password->string, "none"))
293  need |= 1;
295  need |= 2;
296 
297  gi.cvar_set("needpass", va("%d", need));
298  }
299 }
300 
301 /*
302 =================
303 CheckDMRules
304 =================
305 */
306 void CheckDMRules (void)
307 {
308  int i;
309  gclient_t *cl;
310 
312  return;
313 
314  if (!deathmatch->value)
315  return;
316 
317  if (timelimit->value)
318  {
319  if (level.time >= timelimit->value*60)
320  {
321  gi.bprintf (PRINT_HIGH, "Timelimit hit.\n");
322  EndDMLevel ();
323  return;
324  }
325  }
326 
327  if (fraglimit->value)
328  {
329  for (i=0 ; i<maxclients->value ; i++)
330  {
331  cl = game.clients + i;
332  if (!g_edicts[i+1].inuse)
333  continue;
334 
335  if (cl->resp.score >= fraglimit->value)
336  {
337  gi.bprintf (PRINT_HIGH, "Fraglimit hit.\n");
338  EndDMLevel ();
339  return;
340  }
341  }
342  }
343 }
344 
345 
346 /*
347 =============
348 ExitLevel
349 =============
350 */
351 void ExitLevel (void)
352 {
353  int i;
354  edict_t *ent;
355  char command [256];
356 
357  Com_sprintf (command, sizeof(command), "gamemap \"%s\"\n", level.changemap);
358  gi.AddCommandString (command);
359  level.changemap = NULL;
363 
364  // clear some things before going to next level
365  for (i=0 ; i<maxclients->value ; i++)
366  {
367  ent = g_edicts + 1 + i;
368  if (!ent->inuse)
369  continue;
370  if (ent->health > ent->client->pers.max_health)
371  ent->health = ent->client->pers.max_health;
372  }
373 
374 }
375 
376 /*
377 ================
378 G_RunFrame
379 
380 Advances the world by 0.1 seconds
381 ================
382 */
383 void G_RunFrame (void)
384 {
385  int i;
386  edict_t *ent;
387 
388  level.framenum++;
390 
391  // choose a client for monsters to target this frame
393 
394  // exit intermissions
395 
397  {
398  ExitLevel ();
399  return;
400  }
401 
402  //
403  // treat each object in turn
404  // even the world gets a chance to think
405  //
406  ent = &g_edicts[0];
407  for (i=0 ; i<globals.num_edicts ; i++, ent++)
408  {
409  if (!ent->inuse)
410  continue;
411 
412  level.current_entity = ent;
413 
414  VectorCopy (ent->s.origin, ent->s.old_origin);
415 
416  // if the ground entity moved, make sure we are still on it
417  if ((ent->groundentity) && (ent->groundentity->linkcount != ent->groundentity_linkcount))
418  {
419  ent->groundentity = NULL;
420  if ( !(ent->flags & (FL_SWIM|FL_FLY)) && (ent->svflags & SVF_MONSTER) )
421  {
422  M_CheckGround (ent);
423  }
424  }
425 
426  if (i > 0 && i <= maxclients->value)
427  {
429  continue;
430  }
431 
432  G_RunEntity (ent);
433  }
434 
435  // see if it is time to end a deathmatch
436  CheckDMRules ();
437 
438  // see if needpass needs updated
439  CheckNeedPass ();
440 
441  // build the playerstate_t structures for all players
443 }
444 
entity_state_s::old_origin
vec3_t old_origin
Definition: q_shared.h:1175
game_import_t
Definition: game.h:102
deathmatch
cvar_t * deathmatch
Definition: g_main.c:35
game_export_t::SpawnEntities
void(* SpawnEntities)(char *mapname, char *entstring, char *spawnpoint)
Definition: game.h:192
WriteLevel
void WriteLevel(char *filename)
Definition: g_save.c:635
game_import_t::dprintf
void(* dprintf)(char *fmt,...)
Definition: game.h:106
edict_s::s
entity_state_t s
Definition: g_local.h:964
edict_s::groundentity
edict_t * groundentity
Definition: g_local.h:1073
game
game_locals_t game
Definition: g_main.c:23
game_export_t::ReadGame
void(* ReadGame)(char *filename)
Definition: game.h:199
sm_meat_index
int sm_meat_index
Definition: g_main.c:29
globals
game_export_t globals
Definition: g_main.c:26
RunEntity
void RunEntity(edict_t *ent)
game_export_t::edict_size
int edict_size
Definition: game.h:230
spawn_temp_t
Definition: g_local.h:347
value
GLfloat value
Definition: qgl_win.c:63
GAME_API_VERSION
#define GAME_API_VERSION
Definition: game.h:23
Com_Printf
void Com_Printf(char *msg,...)
Definition: g_main.c:157
ClientBeginServerFrame
void ClientBeginServerFrame(edict_t *ent)
Definition: p_client.c:1765
G_Spawn
edict_t * G_Spawn(void)
Definition: g_utils.c:420
sv_maxvelocity
cvar_t * sv_maxvelocity
Definition: g_main.c:52
sv_rollangle
cvar_t * sv_rollangle
Definition: g_main.c:56
G_RunEntity
void G_RunEntity(edict_t *ent)
Definition: g_phys.c:932
filterban
cvar_t * filterban
Definition: g_main.c:50
bob_roll
cvar_t * bob_roll
Definition: g_main.c:65
level_locals_t
Definition: g_local.h:301
CheckNeedPass
void CheckNeedPass(void)
Definition: g_main.c:280
ClientThink
void ClientThink(edict_t *ent, usercmd_t *cmd)
Definition: p_client.c:1580
ClientUserinfoChanged
void ClientUserinfoChanged(edict_t *ent, char *userinfo)
Definition: p_client.c:1371
FRAMETIME
#define FRAMETIME
Definition: g_local.h:73
ClientConnect
qboolean ClientConnect(edict_t *ent, char *userinfo)
Definition: p_client.c:1440
cvar_s::modified
qboolean modified
Definition: q_shared.h:323
game_export_t::Shutdown
void(* Shutdown)(void)
Definition: game.h:189
entity_state_s::origin
vec3_t origin
Definition: q_shared.h:1173
game_export_t::apiversion
int apiversion
Definition: game.h:183
edict_s::linkcount
int linkcount
Definition: g_local.h:971
edict_s::map
char * map
Definition: g_local.h:1059
GetGameAPI
game_export_t * GetGameAPI(game_import_t *import)
Definition: g_main.c:113
cvar_s::string
char * string
Definition: q_shared.h:320
qboolean
qboolean
Definition: q_shared.h:56
edict_s::inuse
qboolean inuse
Definition: g_local.h:970
sv_gravity
cvar_t * sv_gravity
Definition: g_main.c:53
ClientBegin
void ClientBegin(edict_t *ent)
Definition: p_client.c:1305
ExitLevel
void ExitLevel(void)
Definition: g_main.c:351
i
int i
Definition: q_shared.c:305
meansOfDeath
int meansOfDeath
Definition: g_main.c:31
dedicated
cvar_t * dedicated
Definition: g_main.c:48
ClientEndServerFrames
void ClientEndServerFrames(void)
Definition: g_main.c:179
G_Find
edict_t * G_Find(edict_t *from, int fieldofs, char *match)
Definition: g_utils.c:45
game_export_t::Init
void(* Init)(void)
Definition: game.h:188
edict_s::client
struct gclient_s * client
Definition: g_local.h:965
game_export_t::ClientThink
void(* ClientThink)(edict_t *ent, usercmd_t *cmd)
Definition: game.h:211
FOFS
#define FOFS(x)
Definition: g_local.h:504
skill
cvar_t * skill
Definition: g_main.c:38
ShutdownGame
void ShutdownGame(void)
Definition: g_main.c:96
TAG_LEVEL
#define TAG_LEVEL
Definition: g_local.h:77
g_select_empty
cvar_t * g_select_empty
Definition: g_main.c:47
edict_s::groundentity_linkcount
int groundentity_linkcount
Definition: g_local.h:1074
cvar_s
Definition: q_shared.h:317
game_locals_t
Definition: g_local.h:270
password
cvar_t * password
Definition: g_main.c:41
game_export_t::ClientConnect
qboolean(* ClientConnect)(edict_t *ent, char *userinfo)
Definition: game.h:206
game_export_t::ClientDisconnect
void(* ClientDisconnect)(edict_t *ent)
Definition: game.h:209
BeginIntermission
void BeginIntermission(edict_t *targ)
Definition: p_hud.c:73
game_export_t::ClientUserinfoChanged
void(* ClientUserinfoChanged)(edict_t *ent, char *userinfo)
Definition: game.h:208
FL_SWIM
#define FL_SWIM
Definition: g_local.h:58
ClientDisconnect
void ClientDisconnect(edict_t *ent)
Definition: p_client.c:1514
gclient_s::pers
client_persistant_t pers
Definition: g_local.h:884
level_locals_t::current_entity
edict_t * current_entity
Definition: g_local.h:337
va
char * va(char *format,...)
Definition: q_shared.c:1050
msg
cvar_t * msg
Definition: cl_main.c:98
run_roll
cvar_t * run_roll
Definition: g_main.c:62
Sys_Error
void Sys_Error(char *error,...)
Definition: g_main.c:145
game_export_t::ClientCommand
void(* ClientCommand)(edict_t *ent)
Definition: game.h:210
edict_s::svflags
int svflags
Definition: g_local.h:983
gi
game_import_t gi
Definition: g_main.c:25
game_import_t::cvar_set
cvar_t *(* cvar_set)(char *var_name, char *value)
Definition: game.h:163
edict_s::classname
char * classname
Definition: g_local.h:1005
edict_s
Definition: g_local.h:962
gib_on
cvar_t * gib_on
Definition: g_main.c:75
ReadLevel
void ReadLevel(char *filename)
Definition: g_save.c:689
DF_SAME_LEVEL
#define DF_SAME_LEVEL
Definition: q_shared.h:1053
ServerCommand
void ServerCommand(void)
Definition: g_svcmds.c:282
maxclients
cvar_t * maxclients
Definition: g_main.c:44
t
GLdouble t
Definition: qgl_win.c:328
sv_maplist
cvar_t * sv_maplist
Definition: g_main.c:73
SpawnEntities
void SpawnEntities(char *mapname, char *entities, char *spawnpoint)
Definition: g_spawn.c:522
game_locals_t::clients
gclient_t * clients
Definition: g_local.h:277
bob_pitch
cvar_t * bob_pitch
Definition: g_main.c:64
cvar_s::value
float value
Definition: q_shared.h:324
PRINT_HIGH
#define PRINT_HIGH
Definition: q_shared.h:92
game_export_t::ServerCommand
void(* ServerCommand)(void)
Definition: game.h:219
flood_msgs
cvar_t * flood_msgs
Definition: g_main.c:69
edict_t
struct edict_s edict_t
Definition: game.h:52
level_locals_t::nextmap
char nextmap[MAX_QPATH]
Definition: g_local.h:308
NULL
#define NULL
Definition: q_shared.h:60
level
level_locals_t level
Definition: g_main.c:24
g_edicts
edict_t * g_edicts
Definition: g_main.c:33
game_export_t::ReadLevel
void(* ReadLevel)(char *filename)
Definition: game.h:204
gun_y
cvar_t * gun_y
Definition: g_main.c:58
EndDMLevel
void EndDMLevel(void)
Definition: g_main.c:221
Q_stricmp
int Q_stricmp(char *s1, char *s2)
Definition: q_shared.c:1192
snd_fry
int snd_fry
Definition: g_main.c:30
level_locals_t::framenum
int framenum
Definition: g_local.h:303
ERR_FATAL
#define ERR_FATAL
Definition: qcommon.h:735
gun_x
cvar_t * gun_x
Definition: g_main.c:57
M_CheckGround
void M_CheckGround(edict_t *ent)
Definition: g_monster.c:141
level_locals_t::exitintermission
int exitintermission
Definition: g_local.h:313
level_locals_t::time
float time
Definition: g_local.h:304
dmflags
cvar_t * dmflags
Definition: g_main.c:37
game_import_t::error
void(* error)(char *fmt,...)
Definition: game.h:118
CheckDMRules
void CheckDMRules(void)
Definition: g_main.c:306
bob_up
cvar_t * bob_up
Definition: g_main.c:63
VectorCopy
#define VectorCopy(a, b)
Definition: q_shared.h:158
game_export_t
Definition: game.h:181
G_RunFrame
void G_RunFrame(void)
Definition: g_main.c:383
level_locals_t::intermissiontime
float intermissiontime
Definition: g_local.h:311
game_export_t::RunFrame
void(* RunFrame)(void)
Definition: game.h:213
timelimit
cvar_t * timelimit
Definition: g_main.c:40
sv_rollspeed
cvar_t * sv_rollspeed
Definition: g_main.c:55
game_import_t::AddCommandString
void(* AddCommandString)(char *text)
Definition: game.h:173
game_export_t::WriteLevel
void(* WriteLevel)(char *filename)
Definition: game.h:203
CreateTargetChangeLevel
edict_t * CreateTargetChangeLevel(char *map)
Definition: g_main.c:203
level_locals_t::changemap
char * changemap
Definition: g_local.h:312
usercmd_s
Definition: q_shared.h:513
InitGame
void InitGame(void)
Definition: g_save.c:152
TAG_GAME
#define TAG_GAME
Definition: g_local.h:76
ClientEndServerFrame
void ClientEndServerFrame(edict_t *ent)
Definition: p_view.c:958
ClientCommand
void ClientCommand(edict_t *ent)
Definition: g_cmds.c:909
maxentities
cvar_t * maxentities
Definition: g_main.c:46
needpass
cvar_t * needpass
Definition: g_main.c:43
edict_s::flags
int flags
Definition: g_local.h:996
fraglimit
cvar_t * fraglimit
Definition: g_main.c:39
game_import_t::FreeTags
void(* FreeTags)(int tag)
Definition: game.h:159
coop
cvar_t * coop
Definition: g_main.c:36
FL_FLY
#define FL_FLY
Definition: g_local.h:57
ReadGame
void ReadGame(char *filename)
Definition: g_save.c:494
SVF_MONSTER
#define SVF_MONSTER
Definition: game.h:29
st
spawn_temp_t st
Definition: g_main.c:27
run_pitch
cvar_t * run_pitch
Definition: g_main.c:61
maxspectators
cvar_t * maxspectators
Definition: g_main.c:45
game_export_t::ClientBegin
void(* ClientBegin)(edict_t *ent)
Definition: game.h:207
spectator_password
cvar_t * spectator_password
Definition: g_main.c:42
client_persistant_t::max_health
int max_health
Definition: g_local.h:838
flood_persecond
cvar_t * flood_persecond
Definition: g_main.c:70
level_locals_t::mapname
char mapname[MAX_QPATH]
Definition: g_local.h:307
gclient_s
Definition: g_local.h:877
flood_waitdelay
cvar_t * flood_waitdelay
Definition: g_main.c:71
game_export_t::WriteGame
void(* WriteGame)(char *filename, qboolean autosave)
Definition: game.h:198
sv_cheats
cvar_t * sv_cheats
Definition: g_main.c:67
cl
client_state_t cl
Definition: cl_main.c:106
AI_SetSightClient
void AI_SetSightClient(void)
Definition: g_ai.c:50
Com_sprintf
void Com_sprintf(char *dest, int size, char *fmt,...)
Definition: q_shared.c:1236
WriteGame
void WriteGame(char *filename, qboolean autosave)
Definition: g_save.c:467
game_export_t::num_edicts
int num_edicts
Definition: game.h:231
g_local.h
gun_z
cvar_t * gun_z
Definition: g_main.c:59
edict_s::health
int health
Definition: g_local.h:1051
game_import_t::bprintf
void(* bprintf)(int printlevel, char *fmt,...)
Definition: game.h:105