g_main.c 8.4 KB

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