vkQuake2 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 
75 void SpawnEntities (char *mapname, char *entities, char *spawnpoint);
76 void ClientThink (edict_t *ent, usercmd_t *cmd);
77 qboolean ClientConnect (edict_t *ent, char *userinfo);
78 void ClientUserinfoChanged (edict_t *ent, char *userinfo);
79 void ClientDisconnect (edict_t *ent);
80 void ClientBegin (edict_t *ent);
81 void ClientCommand (edict_t *ent);
82 void RunEntity (edict_t *ent);
83 void WriteGame (char *filename, qboolean autosave);
84 void ReadGame (char *filename);
85 void WriteLevel (char *filename);
86 void ReadLevel (char *filename);
87 void InitGame (void);
88 void G_RunFrame (void);
89 
90 
91 //===================================================================
92 
93 
94 void ShutdownGame (void)
95 {
96  gi.dprintf ("==== ShutdownGame ====\n");
97 
100 }
101 
102 
103 /*
104 =================
105 GetGameAPI
106 
107 Returns a pointer to the structure with all entry points
108 and global variables
109 =================
110 */
112 {
113  gi = *import;
114 
119 
124 
131 
133 
135 
136  globals.edict_size = sizeof(edict_t);
137 
138  return &globals;
139 }
140 
141 #ifndef GAME_HARD_LINKED
142 // this is only here so the functions in q_shared.c and q_shwin.c can link
143 void Sys_Error (char *error, ...)
144 {
145  va_list argptr;
146  char text[1024];
147 
148  va_start (argptr, error);
149  vsnprintf (text, 1024, error, argptr);
150  va_end (argptr);
151 
152  gi.error (ERR_FATAL, "%s", text);
153 }
154 
155 void Com_Printf (char *msg, ...)
156 {
157  va_list argptr;
158  char text[1024];
159 
160  va_start (argptr, msg);
161  vsnprintf (text, 1024, msg, argptr);
162  va_end (argptr);
163 
164  gi.dprintf ("%s", text);
165 }
166 
167 #endif
168 
169 //======================================================================
170 
171 
172 /*
173 =================
174 ClientEndServerFrames
175 =================
176 */
178 {
179  int i;
180  edict_t *ent;
181 
182  // calc the player views now that all pushing
183  // and damage has been added
184  for (i=0 ; i<maxclients->value ; i++)
185  {
186  ent = g_edicts + 1 + i;
187  if (!ent->inuse || !ent->client)
188  continue;
189  ClientEndServerFrame (ent);
190  }
191 
192 }
193 
194 /*
195 =================
196 CreateTargetChangeLevel
197 
198 Returns the created target changelevel
199 =================
200 */
202 {
203  edict_t *ent;
204 
205  ent = G_Spawn ();
206  ent->classname = "target_changelevel";
207  Com_sprintf(level.nextmap, sizeof(level.nextmap), "%s", map);
208  ent->map = level.nextmap;
209  return ent;
210 }
211 
212 /*
213 =================
214 EndDMLevel
215 
216 The timelimit or fraglimit has been exceeded
217 =================
218 */
219 void EndDMLevel (void)
220 {
221  edict_t *ent;
222  char *s, *t, *f;
223  static const char *seps = " ,\n\r";
224 
225  // stay on same level flag
226  if ((int)dmflags->value & DF_SAME_LEVEL)
227  {
229  return;
230  }
231 
232  // see if it's in the map list
233  if (*sv_maplist->string) {
234  s = strdup(sv_maplist->string);
235  f = NULL;
236  t = strtok(s, seps);
237  while (t != NULL) {
238  if (Q_stricmp(t, level.mapname) == 0) {
239  // it's in the list, go to the next one
240  t = strtok(NULL, seps);
241  if (t == NULL) { // end of list, go to first one
242  if (f == NULL) // there isn't a first one, same level
244  else
246  } else
248  free(s);
249  return;
250  }
251  if (!f)
252  f = t;
253  t = strtok(NULL, seps);
254  }
255  free(s);
256  }
257 
258  if (level.nextmap[0]) // go to a specific map
260  else { // search for a changelevel
261  ent = G_Find (NULL, FOFS(classname), "target_changelevel");
262  if (!ent)
263  { // the map designer didn't include a changelevel,
264  // so create a fake ent that goes back to the same level
266  return;
267  }
268  BeginIntermission (ent);
269  }
270 }
271 
272 
273 /*
274 =================
275 CheckNeedPass
276 =================
277 */
278 void CheckNeedPass (void)
279 {
280  int need;
281 
282  // if password or spectator_password has changed, update needpass
283  // as needed
285  {
287 
288  need = 0;
289 
290  if (*password->string && Q_stricmp(password->string, "none"))
291  need |= 1;
293  need |= 2;
294 
295  gi.cvar_set("needpass", va("%d", need));
296  }
297 }
298 
299 /*
300 =================
301 CheckDMRules
302 =================
303 */
304 void CheckDMRules (void)
305 {
306  int i;
307  gclient_t *cl;
308 
310  return;
311 
312  if (!deathmatch->value)
313  return;
314 
315  if (timelimit->value)
316  {
317  if (level.time >= timelimit->value*60)
318  {
319  gi.bprintf (PRINT_HIGH, "Timelimit hit.\n");
320  EndDMLevel ();
321  return;
322  }
323  }
324 
325  if (fraglimit->value)
326  {
327  for (i=0 ; i<maxclients->value ; i++)
328  {
329  cl = game.clients + i;
330  if (!g_edicts[i+1].inuse)
331  continue;
332 
333  if (cl->resp.score >= fraglimit->value)
334  {
335  gi.bprintf (PRINT_HIGH, "Fraglimit hit.\n");
336  EndDMLevel ();
337  return;
338  }
339  }
340  }
341 }
342 
343 
344 /*
345 =============
346 ExitLevel
347 =============
348 */
349 void ExitLevel (void)
350 {
351  int i;
352  edict_t *ent;
353  char command [256];
354 
355  Com_sprintf (command, sizeof(command), "gamemap \"%s\"\n", level.changemap);
356  gi.AddCommandString (command);
357  level.changemap = NULL;
361 
362  // clear some things before going to next level
363  for (i=0 ; i<maxclients->value ; i++)
364  {
365  ent = g_edicts + 1 + i;
366  if (!ent->inuse)
367  continue;
368  if (ent->health > ent->client->pers.max_health)
369  ent->health = ent->client->pers.max_health;
370  }
371 
372 }
373 
374 /*
375 ================
376 G_RunFrame
377 
378 Advances the world by 0.1 seconds
379 ================
380 */
381 void G_RunFrame (void)
382 {
383  int i;
384  edict_t *ent;
385 
386  level.framenum++;
388 
389  // choose a client for monsters to target this frame
391 
392  // exit intermissions
393 
395  {
396  ExitLevel ();
397  return;
398  }
399 
400  //
401  // treat each object in turn
402  // even the world gets a chance to think
403  //
404  ent = &g_edicts[0];
405  for (i=0 ; i<globals.num_edicts ; i++, ent++)
406  {
407  if (!ent->inuse)
408  continue;
409 
410  level.current_entity = ent;
411 
412  VectorCopy (ent->s.origin, ent->s.old_origin);
413 
414  // if the ground entity moved, make sure we are still on it
415  if ((ent->groundentity) && (ent->groundentity->linkcount != ent->groundentity_linkcount))
416  {
417  ent->groundentity = NULL;
418  if ( !(ent->flags & (FL_SWIM|FL_FLY)) && (ent->svflags & SVF_MONSTER) )
419  {
420  M_CheckGround (ent);
421  }
422  }
423 
424  if (i > 0 && i <= maxclients->value)
425  {
427  continue;
428  }
429 
430  G_RunEntity (ent);
431  }
432 
433  // see if it is time to end a deathmatch
434  CheckDMRules ();
435 
436  // see if needpass needs updated
437  CheckNeedPass ();
438 
439  // build the playerstate_t structures for all players
441 }
442 
entity_state_s::old_origin
vec3_t old_origin
Definition: q_shared.h:1151
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:632
game_import_t::dprintf
void(* dprintf)(char *fmt,...)
Definition: game.h:106
edict_s::s
entity_state_t s
Definition: g_local.h:970
edict_s::groundentity
edict_t * groundentity
Definition: g_local.h:1079
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:349
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:155
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:303
CheckNeedPass
void CheckNeedPass(void)
Definition: g_main.c:278
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:75
ClientConnect
qboolean ClientConnect(edict_t *ent, char *userinfo)
Definition: p_client.c:1440
cvar_s::modified
qboolean modified
Definition: q_shared.h:330
game_export_t::Shutdown
void(* Shutdown)(void)
Definition: game.h:189
entity_state_s::origin
vec3_t origin
Definition: q_shared.h:1149
game_export_t::apiversion
int apiversion
Definition: game.h:183
edict_s::linkcount
int linkcount
Definition: g_local.h:977
edict_s::map
char * map
Definition: g_local.h:1065
GetGameAPI
game_export_t * GetGameAPI(game_import_t *import)
Definition: g_main.c:111
cvar_s::string
char * string
Definition: q_shared.h:327
qboolean
qboolean
Definition: q_shared.h:63
edict_s::inuse
qboolean inuse
Definition: g_local.h:976
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:349
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:177
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:971
game_export_t::ClientThink
void(* ClientThink)(edict_t *ent, usercmd_t *cmd)
Definition: game.h:211
FOFS
#define FOFS(x)
Definition: g_local.h:510
skill
cvar_t * skill
Definition: g_main.c:38
ShutdownGame
void ShutdownGame(void)
Definition: g_main.c:94
TAG_LEVEL
#define TAG_LEVEL
Definition: g_local.h:79
g_select_empty
cvar_t * g_select_empty
Definition: g_main.c:47
edict_s::groundentity_linkcount
int groundentity_linkcount
Definition: g_local.h:1080
cvar_s
Definition: q_shared.h:324
game_locals_t
Definition: g_local.h:272
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:74
game_export_t::ClientUserinfoChanged
void(* ClientUserinfoChanged)(edict_t *ent, char *userinfo)
Definition: game.h:208
FL_SWIM
#define FL_SWIM
Definition: g_local.h:60
ClientDisconnect
void ClientDisconnect(edict_t *ent)
Definition: p_client.c:1514
gclient_s::pers
client_persistant_t pers
Definition: g_local.h:890
level_locals_t::current_entity
edict_t * current_entity
Definition: g_local.h:339
va
char * va(char *format,...)
Definition: q_shared.c:1050
msg
cvar_t * msg
Definition: cl_main.c:83
run_roll
cvar_t * run_roll
Definition: g_main.c:62
Sys_Error
void Sys_Error(char *error,...)
Definition: g_main.c:143
game_export_t::ClientCommand
void(* ClientCommand)(edict_t *ent)
Definition: game.h:210
edict_s::svflags
int svflags
Definition: g_local.h:989
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:1011
edict_s
Definition: g_local.h:968
ReadLevel
void ReadLevel(char *filename)
Definition: g_save.c:686
DF_SAME_LEVEL
#define DF_SAME_LEVEL
Definition: q_shared.h:1029
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:279
bob_pitch
cvar_t * bob_pitch
Definition: g_main.c:64
cvar_s::value
float value
Definition: q_shared.h:331
PRINT_HIGH
#define PRINT_HIGH
Definition: q_shared.h:99
error
static int error(vorb *f, enum STBVorbisError e)
Definition: stb_vorbis.c:865
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:310
NULL
#define NULL
Definition: q_shared.h:67
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:219
Q_stricmp
int Q_stricmp(char *s1, char *s2)
Definition: q_shared.c:1180
snd_fry
int snd_fry
Definition: g_main.c:30
level_locals_t::framenum
int framenum
Definition: g_local.h:305
ERR_FATAL
#define ERR_FATAL
Definition: qcommon.h:743
gun_x
cvar_t * gun_x
Definition: g_main.c:57
M_CheckGround
void M_CheckGround(edict_t *ent)
Definition: g_monster.c:141
s
static fixed16_t s
Definition: r_scan.c:30
level_locals_t::exitintermission
int exitintermission
Definition: g_local.h:315
level_locals_t::time
float time
Definition: g_local.h:306
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:304
bob_up
cvar_t * bob_up
Definition: g_main.c:63
VectorCopy
#define VectorCopy(a, b)
Definition: q_shared.h:165
game_export_t
Definition: game.h:181
G_RunFrame
void G_RunFrame(void)
Definition: g_main.c:381
level_locals_t::intermissiontime
float intermissiontime
Definition: g_local.h:313
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:201
level_locals_t::changemap
char * changemap
Definition: g_local.h:314
usercmd_s
Definition: q_shared.h:517
InitGame
void InitGame(void)
Definition: g_save.c:153
TAG_GAME
#define TAG_GAME
Definition: g_local.h:78
ClientEndServerFrame
void ClientEndServerFrame(edict_t *ent)
Definition: p_view.c:958
ClientCommand
void ClientCommand(edict_t *ent)
Definition: g_cmds.c:908
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:1002
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:59
ReadGame
void ReadGame(char *filename)
Definition: g_save.c:491
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:844
flood_persecond
cvar_t * flood_persecond
Definition: g_main.c:70
level_locals_t::mapname
char mapname[MAX_QPATH]
Definition: g_local.h:309
gclient_s
Definition: g_local.h:883
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:91
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:1223
WriteGame
void WriteGame(char *filename, qboolean autosave)
Definition: g_save.c:464
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:1057
game_import_t::bprintf
void(* bprintf)(int printlevel, char *fmt,...)
Definition: game.h:105