g_main.c 7.8 KB

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