g_main.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. #include "g_local.h"
  16. game_locals_t game;
  17. level_locals_t level;
  18. game_import_t gi;
  19. game_export_t globals;
  20. spawn_temp_t st;
  21. int sm_meat_index;
  22. int snd_fry;
  23. int meansOfDeath;
  24. edict_t *g_edicts;
  25. cvar_t *deathmatch;
  26. cvar_t *coop;
  27. cvar_t *dmflags;
  28. cvar_t *skill;
  29. cvar_t *fraglimit;
  30. cvar_t *timelimit;
  31. cvar_t *password;
  32. cvar_t *spectator_password;
  33. cvar_t *maxclients;
  34. cvar_t *maxspectators;
  35. cvar_t *maxentities;
  36. cvar_t *g_select_empty;
  37. cvar_t *dedicated;
  38. cvar_t *filterban;
  39. cvar_t *sv_maxvelocity;
  40. cvar_t *sv_gravity;
  41. cvar_t *sv_rollspeed;
  42. cvar_t *sv_rollangle;
  43. cvar_t *gun_x;
  44. cvar_t *gun_y;
  45. cvar_t *gun_z;
  46. cvar_t *run_pitch;
  47. cvar_t *run_roll;
  48. cvar_t *bob_up;
  49. cvar_t *bob_pitch;
  50. cvar_t *bob_roll;
  51. cvar_t *sv_cheats;
  52. cvar_t *flood_msgs;
  53. cvar_t *flood_persecond;
  54. cvar_t *flood_waitdelay;
  55. cvar_t *sv_maplist;
  56. void SpawnEntities (char *mapname, char *entities, char *spawnpoint);
  57. void ClientThink (edict_t *ent, usercmd_t *cmd);
  58. qboolean ClientConnect (edict_t *ent, char *userinfo);
  59. void ClientUserinfoChanged (edict_t *ent, char *userinfo);
  60. void ClientDisconnect (edict_t *ent);
  61. void ClientBegin (edict_t *ent);
  62. void ClientCommand (edict_t *ent);
  63. void RunEntity (edict_t *ent);
  64. void WriteGame (char *filename, qboolean autosave);
  65. void ReadGame (char *filename);
  66. void WriteLevel (char *filename);
  67. void ReadLevel (char *filename);
  68. void InitGame (void);
  69. void G_RunFrame (void);
  70. //===================================================================
  71. void ShutdownGame (void)
  72. {
  73. gi.dprintf ("==== ShutdownGame ====\n");
  74. gi.FreeTags (TAG_LEVEL);
  75. gi.FreeTags (TAG_GAME);
  76. }
  77. /*
  78. =================
  79. GetGameAPI
  80. Returns a pointer to the structure with all entry points
  81. and global variables
  82. =================
  83. */
  84. game_export_t *GetGameAPI (game_import_t *import)
  85. {
  86. gi = *import;
  87. globals.apiversion = GAME_API_VERSION;
  88. globals.Init = InitGame;
  89. globals.Shutdown = ShutdownGame;
  90. globals.SpawnEntities = SpawnEntities;
  91. globals.WriteGame = WriteGame;
  92. globals.ReadGame = ReadGame;
  93. globals.WriteLevel = WriteLevel;
  94. globals.ReadLevel = ReadLevel;
  95. globals.ClientThink = ClientThink;
  96. globals.ClientConnect = ClientConnect;
  97. globals.ClientUserinfoChanged = ClientUserinfoChanged;
  98. globals.ClientDisconnect = ClientDisconnect;
  99. globals.ClientBegin = ClientBegin;
  100. globals.ClientCommand = ClientCommand;
  101. globals.RunFrame = G_RunFrame;
  102. globals.ServerCommand = ServerCommand;
  103. globals.edict_size = sizeof(edict_t);
  104. return &globals;
  105. }
  106. #ifndef GAME_HARD_LINKED
  107. // this is only here so the functions in q_shared.c and q_shwin.c can link
  108. void Sys_Error (char *error, ...)
  109. {
  110. va_list argptr;
  111. char text[1024];
  112. va_start (argptr, error);
  113. vsprintf (text, error, argptr);
  114. va_end (argptr);
  115. gi.error (ERR_FATAL, "%s", text);
  116. }
  117. void Com_Printf (char *msg, ...)
  118. {
  119. va_list argptr;
  120. char text[1024];
  121. va_start (argptr, msg);
  122. vsprintf (text, msg, argptr);
  123. va_end (argptr);
  124. gi.dprintf ("%s", text);
  125. }
  126. #endif
  127. //======================================================================
  128. /*
  129. =================
  130. ClientEndServerFrames
  131. =================
  132. */
  133. void ClientEndServerFrames (void)
  134. {
  135. int i;
  136. edict_t *ent;
  137. // calc the player views now that all pushing
  138. // and damage has been added
  139. for (i=0 ; i<maxclients->value ; i++)
  140. {
  141. ent = g_edicts + 1 + i;
  142. if (!ent->inuse || !ent->client)
  143. continue;
  144. ClientEndServerFrame (ent);
  145. }
  146. }
  147. /*
  148. =================
  149. CreateTargetChangeLevel
  150. Returns the created target changelevel
  151. =================
  152. */
  153. edict_t *CreateTargetChangeLevel(char *map)
  154. {
  155. edict_t *ent;
  156. ent = G_Spawn ();
  157. ent->classname = "target_changelevel";
  158. Com_sprintf(level.nextmap, sizeof(level.nextmap), "%s", map);
  159. ent->map = level.nextmap;
  160. return ent;
  161. }
  162. /*
  163. =================
  164. EndDMLevel
  165. The timelimit or fraglimit has been exceeded
  166. =================
  167. */
  168. void EndDMLevel (void)
  169. {
  170. edict_t *ent;
  171. char *s, *t, *f;
  172. static const char *seps = " ,\n\r";
  173. // stay on same level flag
  174. if ((int)dmflags->value & DF_SAME_LEVEL)
  175. {
  176. BeginIntermission (CreateTargetChangeLevel (level.mapname) );
  177. return;
  178. }
  179. // see if it's in the map list
  180. if (*sv_maplist->string) {
  181. s = strdup(sv_maplist->string);
  182. f = NULL;
  183. t = strtok(s, seps);
  184. while (t != NULL) {
  185. if (Q_stricmp(t, level.mapname) == 0) {
  186. // it's in the list, go to the next one
  187. t = strtok(NULL, seps);
  188. if (t == NULL) { // end of list, go to first one
  189. if (f == NULL) // there isn't a first one, same level
  190. BeginIntermission (CreateTargetChangeLevel (level.mapname) );
  191. else
  192. BeginIntermission (CreateTargetChangeLevel (f) );
  193. } else
  194. BeginIntermission (CreateTargetChangeLevel (t) );
  195. free(s);
  196. return;
  197. }
  198. if (!f)
  199. f = t;
  200. t = strtok(NULL, seps);
  201. }
  202. free(s);
  203. }
  204. if (level.nextmap[0]) // go to a specific map
  205. BeginIntermission (CreateTargetChangeLevel (level.nextmap) );
  206. else { // search for a changelevel
  207. ent = G_Find (NULL, FOFS(classname), "target_changelevel");
  208. if (!ent)
  209. { // the map designer didn't include a changelevel,
  210. // so create a fake ent that goes back to the same level
  211. BeginIntermission (CreateTargetChangeLevel (level.mapname) );
  212. return;
  213. }
  214. BeginIntermission (ent);
  215. }
  216. }
  217. /*
  218. =================
  219. CheckDMRules
  220. =================
  221. */
  222. void CheckDMRules (void)
  223. {
  224. int i;
  225. gclient_t *cl;
  226. if (level.intermissiontime)
  227. return;
  228. if (!deathmatch->value)
  229. return;
  230. if (timelimit->value)
  231. {
  232. if (level.time >= timelimit->value*60)
  233. {
  234. gi.bprintf (PRINT_HIGH, "Timelimit hit.\n");
  235. EndDMLevel ();
  236. return;
  237. }
  238. }
  239. if (fraglimit->value)
  240. {
  241. for (i=0 ; i<maxclients->value ; i++)
  242. {
  243. cl = game.clients + i;
  244. if (!g_edicts[i+1].inuse)
  245. continue;
  246. if (cl->resp.score >= fraglimit->value)
  247. {
  248. gi.bprintf (PRINT_HIGH, "Fraglimit hit.\n");
  249. EndDMLevel ();
  250. return;
  251. }
  252. }
  253. }
  254. }
  255. /*
  256. =============
  257. ExitLevel
  258. =============
  259. */
  260. void ExitLevel (void)
  261. {
  262. int i;
  263. edict_t *ent;
  264. char command [256];
  265. Com_sprintf (command, sizeof(command), "gamemap \"%s\"\n", level.changemap);
  266. gi.AddCommandString (command);
  267. level.changemap = NULL;
  268. level.exitintermission = 0;
  269. level.intermissiontime = 0;
  270. ClientEndServerFrames ();
  271. // clear some things before going to next level
  272. for (i=0 ; i<maxclients->value ; i++)
  273. {
  274. ent = g_edicts + 1 + i;
  275. if (!ent->inuse)
  276. continue;
  277. if (ent->health > ent->client->pers.max_health)
  278. ent->health = ent->client->pers.max_health;
  279. }
  280. }
  281. /*
  282. ================
  283. G_RunFrame
  284. Advances the world by 0.1 seconds
  285. ================
  286. */
  287. void G_RunFrame (void)
  288. {
  289. int i;
  290. edict_t *ent;
  291. level.framenum++;
  292. level.time = level.framenum*FRAMETIME;
  293. // choose a client for monsters to target this frame
  294. AI_SetSightClient ();
  295. // exit intermissions
  296. if (level.exitintermission)
  297. {
  298. ExitLevel ();
  299. return;
  300. }
  301. //
  302. // treat each object in turn
  303. // even the world gets a chance to think
  304. //
  305. ent = &g_edicts[0];
  306. for (i=0 ; i<globals.num_edicts ; i++, ent++)
  307. {
  308. if (!ent->inuse)
  309. continue;
  310. level.current_entity = ent;
  311. VectorCopy (ent->s.origin, ent->s.old_origin);
  312. // if the ground entity moved, make sure we are still on it
  313. if ((ent->groundentity) && (ent->groundentity->linkcount != ent->groundentity_linkcount))
  314. {
  315. ent->groundentity = NULL;
  316. if ( !(ent->flags & (FL_SWIM|FL_FLY)) && (ent->svflags & SVF_MONSTER) )
  317. {
  318. M_CheckGround (ent);
  319. }
  320. }
  321. if (i > 0 && i <= maxclients->value)
  322. {
  323. ClientBeginServerFrame (ent);
  324. continue;
  325. }
  326. G_RunEntity (ent);
  327. }
  328. // see if it is time to end a deathmatch
  329. CheckDMRules ();
  330. // build the playerstate_t structures for all players
  331. ClientEndServerFrames ();
  332. }