g_main.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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 *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. if (*level.forcemap) {
  181. BeginIntermission (CreateTargetChangeLevel (level.forcemap) );
  182. return;
  183. }
  184. // see if it's in the map list
  185. if (*sv_maplist->string) {
  186. s = strdup(sv_maplist->string);
  187. f = NULL;
  188. t = strtok(s, seps);
  189. while (t != NULL) {
  190. if (Q_stricmp(t, level.mapname) == 0) {
  191. // it's in the list, go to the next one
  192. t = strtok(NULL, seps);
  193. if (t == NULL) { // end of list, go to first one
  194. if (f == NULL) // there isn't a first one, same level
  195. BeginIntermission (CreateTargetChangeLevel (level.mapname) );
  196. else
  197. BeginIntermission (CreateTargetChangeLevel (f) );
  198. } else
  199. BeginIntermission (CreateTargetChangeLevel (t) );
  200. free(s);
  201. return;
  202. }
  203. if (!f)
  204. f = t;
  205. t = strtok(NULL, seps);
  206. }
  207. free(s);
  208. }
  209. if (level.nextmap[0]) // go to a specific map
  210. BeginIntermission (CreateTargetChangeLevel (level.nextmap) );
  211. else { // search for a changelevel
  212. ent = G_Find (NULL, FOFS(classname), "target_changelevel");
  213. if (!ent)
  214. { // the map designer didn't include a changelevel,
  215. // so create a fake ent that goes back to the same level
  216. BeginIntermission (CreateTargetChangeLevel (level.mapname) );
  217. return;
  218. }
  219. BeginIntermission (ent);
  220. }
  221. }
  222. /*
  223. =================
  224. CheckDMRules
  225. =================
  226. */
  227. void CheckDMRules (void)
  228. {
  229. int i;
  230. gclient_t *cl;
  231. if (level.intermissiontime)
  232. return;
  233. if (!deathmatch->value)
  234. return;
  235. //ZOID
  236. if (ctf->value && CTFCheckRules()) {
  237. EndDMLevel ();
  238. return;
  239. }
  240. if (CTFInMatch())
  241. return; // no checking in match mode
  242. //ZOID
  243. if (timelimit->value)
  244. {
  245. if (level.time >= timelimit->value*60)
  246. {
  247. gi.bprintf (PRINT_HIGH, "Timelimit hit.\n");
  248. EndDMLevel ();
  249. return;
  250. }
  251. }
  252. if (fraglimit->value)
  253. for (i=0 ; i<maxclients->value ; i++)
  254. {
  255. cl = game.clients + i;
  256. if (!g_edicts[i+1].inuse)
  257. continue;
  258. if (cl->resp.score >= fraglimit->value)
  259. {
  260. gi.bprintf (PRINT_HIGH, "Fraglimit hit.\n");
  261. EndDMLevel ();
  262. return;
  263. }
  264. }
  265. }
  266. /*
  267. =============
  268. ExitLevel
  269. =============
  270. */
  271. void ExitLevel (void)
  272. {
  273. int i;
  274. edict_t *ent;
  275. char command [256];
  276. level.exitintermission = 0;
  277. level.intermissiontime = 0;
  278. if (CTFNextMap())
  279. return;
  280. Com_sprintf (command, sizeof(command), "gamemap \"%s\"\n", level.changemap);
  281. gi.AddCommandString (command);
  282. ClientEndServerFrames ();
  283. level.changemap = NULL;
  284. // clear some things before going to next level
  285. for (i=0 ; i<maxclients->value ; i++)
  286. {
  287. ent = g_edicts + 1 + i;
  288. if (!ent->inuse)
  289. continue;
  290. if (ent->health > ent->client->pers.max_health)
  291. ent->health = ent->client->pers.max_health;
  292. }
  293. }
  294. /*
  295. ================
  296. G_RunFrame
  297. Advances the world by 0.1 seconds
  298. ================
  299. */
  300. void G_RunFrame (void)
  301. {
  302. int i;
  303. edict_t *ent;
  304. level.framenum++;
  305. level.time = level.framenum*FRAMETIME;
  306. // choose a client for monsters to target this frame
  307. AI_SetSightClient ();
  308. // exit intermissions
  309. if (level.exitintermission)
  310. {
  311. ExitLevel ();
  312. return;
  313. }
  314. //
  315. // treat each object in turn
  316. // even the world gets a chance to think
  317. //
  318. ent = &g_edicts[0];
  319. for (i=0 ; i<globals.num_edicts ; i++, ent++)
  320. {
  321. if (!ent->inuse)
  322. continue;
  323. level.current_entity = ent;
  324. VectorCopy (ent->s.origin, ent->s.old_origin);
  325. // if the ground entity moved, make sure we are still on it
  326. if ((ent->groundentity) && (ent->groundentity->linkcount != ent->groundentity_linkcount))
  327. {
  328. ent->groundentity = NULL;
  329. if ( !(ent->flags & (FL_SWIM|FL_FLY)) && (ent->svflags & SVF_MONSTER) )
  330. {
  331. M_CheckGround (ent);
  332. }
  333. }
  334. if (i > 0 && i <= maxclients->value)
  335. {
  336. ClientBeginServerFrame (ent);
  337. continue;
  338. }
  339. G_RunEntity (ent);
  340. }
  341. // see if it is time to end a deathmatch
  342. CheckDMRules ();
  343. // build the playerstate_t structures for all players
  344. ClientEndServerFrames ();
  345. }