g_main.c 8.7 KB

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