home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 28 / amigaformatcd28.iso / -screenplay- / otherstuff / adoomppc_src / g_game.c < prev    next >
C/C++ Source or Header  |  1998-04-23  |  42KB  |  1,695 lines

  1. // Emacs style mode select   -*- C++ -*- 
  2. //-----------------------------------------------------------------------------
  3. //
  4. // $Id:$
  5. //
  6. // Copyright (C) 1993-1996 by id Software, Inc.
  7. //
  8. // This source is available for distribution and/or modification
  9. // only under the terms of the DOOM Source Code License as
  10. // published by id Software. All rights reserved.
  11. //
  12. // The source is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // FITNESS FOR A PARTICULAR PURPOSE. See the DOOM Source Code License
  15. // for more details.
  16. //
  17. // $Log:$
  18. //
  19. // DESCRIPTION:  none
  20. //
  21. //-----------------------------------------------------------------------------
  22.  
  23.  
  24. static const char
  25. rcsid[] = "$Id: g_game.c,v 1.8 1997/02/03 22:45:09 b1 Exp $";
  26.  
  27. #include <string.h>
  28. #include <stdlib.h>
  29.  
  30. #include "doomdef.h" 
  31. #include "doomstat.h"
  32.  
  33. #include "z_zone.h"
  34. #include "f_finale.h"
  35. #include "m_argv.h"
  36. #include "m_misc.h"
  37. #include "m_menu.h"
  38. #include "m_random.h"
  39. #include "i_system.h"
  40.  
  41. #include "p_setup.h"
  42. #include "p_saveg.h"
  43. #include "p_tick.h"
  44.  
  45. #include "d_main.h"
  46.  
  47. #include "wi_stuff.h"
  48. #include "hu_stuff.h"
  49. #include "st_stuff.h"
  50. #include "am_map.h"
  51.  
  52. // Needs access to LFB.
  53. #include "v_video.h"
  54.  
  55. #include "w_wad.h"
  56.  
  57. #include "p_local.h" 
  58.  
  59. #include "s_sound.h"
  60.  
  61. // Data.
  62. #include "dstrings.h"
  63. #include "sounds.h"
  64.  
  65. // SKY handling - still the wrong place.
  66. #include "r_data.h"
  67. #include "r_sky.h"
  68.  
  69.  
  70.  
  71. #include "g_game.h"
  72.  
  73.  
  74. //#define SAVEGAMESIZE  0x2c000
  75. #define SAVEGAMESIZE    0x50000
  76. #define SAVESTRINGSIZE  24
  77.  
  78.  
  79.  
  80. boolean G_CheckDemoStatus (void); 
  81. void    G_ReadDemoTiccmd (ticcmd_t* cmd); 
  82. void    G_WriteDemoTiccmd (ticcmd_t* cmd); 
  83. void    G_PlayerReborn (int player); 
  84. void    G_InitNew (skill_t skill, int episode, int map); 
  85.  
  86. void    G_DoReborn (int playernum); 
  87.  
  88. void    G_DoLoadLevel (void); 
  89. void    G_DoNewGame (void); 
  90. void    G_DoLoadGame (void); 
  91. void    G_DoPlayDemo (void); 
  92. void    G_DoCompleted (void); 
  93. void    G_DoVictory (void); 
  94. void    G_DoWorldDone (void); 
  95. void    G_DoSaveGame (void); 
  96.  
  97.  
  98. gameaction_t    gameaction; 
  99. gamestate_t     gamestate; 
  100. skill_t         gameskill; 
  101. boolean         respawnmonsters;
  102. int             gameepisode; 
  103. int             gamemap; 
  104.  
  105. boolean         paused; 
  106. boolean         sendpause;              // send a pause event next tic 
  107. boolean         sendsave;               // send a save event next tic 
  108. boolean         usergame;               // ok to save / end game 
  109.  
  110. boolean         timingdemo;             // if true, exit with report on completion 
  111. boolean         nodrawers;              // for comparative timing purposes 
  112. boolean         noblit;                 // for comparative timing purposes 
  113. int             starttime;              // for comparative timing purposes       
  114.  
  115. boolean         viewactive; 
  116.  
  117. boolean         deathmatch;             // only if started as net death 
  118. boolean         netgame;                // only true if packets are broadcast 
  119. boolean         playeringame[MAXPLAYERS]; 
  120. player_t        players[MAXPLAYERS]; 
  121.  
  122. int             consoleplayer;          // player taking events and displaying 
  123. int             displayplayer;          // view being displayed 
  124. int             gametic; 
  125. int             levelstarttic;          // gametic at level start 
  126. int             totalkills, totalitems, totalsecret;    // for intermission 
  127.  
  128. char            demoname[32]; 
  129. boolean         demorecording; 
  130. boolean         demoplayback; 
  131. boolean         netdemo; 
  132. byte*           demobuffer;
  133. byte*           demo_p;
  134. byte*           demoend; 
  135. boolean         singledemo;             // quit after playing a demo from cmdline 
  136.  
  137. boolean         precache = true;        // if true, load all graphics at start 
  138.  
  139. wbstartstruct_t wminfo;                 // parms for world map / intermission 
  140.  
  141. short           consistancy[MAXPLAYERS][BACKUPTICS]; 
  142.  
  143. byte*           savebuffer;
  144.  
  145. // Dehacked
  146. int initial_health=100;
  147. int initial_bullets=50;
  148.  
  149. // 
  150. // controls (have defaults) 
  151. // 
  152. int             key_right;
  153. int             key_left;
  154.  
  155. int             key_up;
  156. int             key_down; 
  157. int             key_strafeleft;
  158. int             key_straferight; 
  159. int             key_fire;
  160. int             key_use;
  161. int             key_strafe;
  162. int             key_speed; 
  163.  
  164. int             mousebfire; 
  165. int             mousebstrafe; 
  166. int             mousebforward; 
  167.  
  168. int             joybfire; 
  169. int             joybstrafe; 
  170. int             joybuse; 
  171. int             joybspeed; 
  172.  
  173.  
  174.  
  175. #define MAXPLMOVE               (forwardmove[1]) 
  176.  
  177. #define TURBOTHRESHOLD  0x32
  178.  
  179. fixed_t         forwardmove[2] = {0x19, 0x32}; 
  180. fixed_t         sidemove[2] = {0x18, 0x28}; 
  181. fixed_t         angleturn[3] = {640, 1280, 320};        // + slow turn 
  182.  
  183. #define SLOWTURNTICS    6 
  184.  
  185. #define NUMKEYS         256 
  186.  
  187. boolean         gamekeydown[NUMKEYS]; 
  188. int             turnheld;                               // for accelerative turning 
  189.  
  190. boolean         mousearray[4]; 
  191. boolean*        mousebuttons = &mousearray[1];          // allow [-1]
  192.  
  193. // mouse values are used once 
  194. int             mousex;
  195. int             mousey;         
  196.  
  197. int             dclicktime;
  198. int             dclickstate;
  199. int             dclicks; 
  200. int             dclicktime2;
  201. int             dclickstate2;
  202. int             dclicks2;
  203.  
  204. // joystick values are repeated 
  205. int             joyxmove;
  206. int             joyymove;
  207. boolean         joyarray[5]; 
  208. boolean*        joybuttons = &joyarray[1];              // allow [-1] 
  209.  
  210. int             savegameslot; 
  211. char            savedescription[32]; 
  212.  
  213.  
  214. #define BODYQUESIZE     32
  215.  
  216. mobj_t*         bodyque[BODYQUESIZE]; 
  217. int             bodyqueslot; 
  218.  
  219. void*           statcopy;                               // for statistics driver
  220.  
  221.  
  222.  
  223. int G_CmdChecksum (ticcmd_t* cmd) 
  224.     int         i;
  225.     int         sum = 0; 
  226.          
  227.     for (i=0 ; i< sizeof(*cmd)/4 - 1 ; i++) 
  228.         sum += ((int *)cmd)[i]; 
  229.                  
  230.     return sum; 
  231.  
  232.  
  233. //
  234. // G_BuildTiccmd
  235. // Builds a ticcmd from all of the available inputs
  236. // or reads it from the demo buffer. 
  237. // If recording a demo, write it out 
  238. // 
  239. void G_BuildTiccmd (ticcmd_t* cmd) 
  240.     int         i; 
  241.     boolean     strafe;
  242.     boolean     bstrafe; 
  243.     int         speed;
  244.     int         tspeed; 
  245.     int         forward;
  246.     int         side;
  247.     
  248.     ticcmd_t*   base;
  249.  
  250.     base = I_BaseTiccmd ();             // empty, or external driver
  251.     memcpy (cmd,base,sizeof(*cmd)); 
  252.         
  253.     cmd->consistancy = 
  254.         consistancy[consoleplayer][maketic%BACKUPTICS]; 
  255.  
  256.  
  257.     strafe = gamekeydown[key_strafe] || mousebuttons[mousebstrafe] 
  258.         || joybuttons[joybstrafe]; 
  259.     speed = gamekeydown[key_speed] || joybuttons[joybspeed];
  260.  
  261.     forward = side = 0;
  262.     
  263.     // use two stage accelerative turning
  264.     // on the keyboard and joystick
  265.     if (joyxmove < 0
  266.         || joyxmove > 0  
  267.         || gamekeydown[key_right]
  268.         || gamekeydown[key_left]) 
  269.         turnheld += ticdup; 
  270.     else 
  271.         turnheld = 0; 
  272.  
  273.     if (turnheld < SLOWTURNTICS) 
  274.         tspeed = 2;             // slow turn 
  275.     else 
  276.         tspeed = speed;
  277.     
  278.     // let movement keys cancel each other out
  279.     if (strafe) 
  280.     { 
  281.         if (gamekeydown[key_right]) 
  282.         {
  283.             // fprintf(stderr, "strafe right\n");
  284.             side += sidemove[speed]; 
  285.         }
  286.         if (gamekeydown[key_left]) 
  287.         {
  288.             //  fprintf(stderr, "strafe left\n");
  289.             side -= sidemove[speed]; 
  290.         }
  291.         if (joyxmove > 0) 
  292.             side += sidemove[speed]; 
  293.         if (joyxmove < 0) 
  294.             side -= sidemove[speed]; 
  295.  
  296.     } 
  297.     else 
  298.     { 
  299.         if (gamekeydown[key_right]) 
  300.             cmd->angleturn -= angleturn[tspeed]; 
  301.         if (gamekeydown[key_left]) 
  302.             cmd->angleturn += angleturn[tspeed]; 
  303.         if (joyxmove > 0) 
  304.             cmd->angleturn -= angleturn[tspeed]; 
  305.         if (joyxmove < 0) 
  306.             cmd->angleturn += angleturn[tspeed]; 
  307.     } 
  308.  
  309.     if (gamekeydown[key_up]) 
  310.     {
  311.         // fprintf(stderr, "up\n");
  312.         forward += forwardmove[speed]; 
  313.     }
  314.     if (gamekeydown[key_down]) 
  315.     {
  316.         // fprintf(stderr, "down\n");
  317.         forward -= forwardmove[speed]; 
  318.     }
  319.     if (joyymove < 0) 
  320.         forward += forwardmove[speed]; 
  321.     if (joyymove > 0) 
  322.         forward -= forwardmove[speed]; 
  323.     if (gamekeydown[key_straferight]) 
  324.         side += sidemove[speed]; 
  325.     if (gamekeydown[key_strafeleft]) 
  326.         side -= sidemove[speed];
  327.     
  328.     // buttons
  329.     cmd->chatchar = HU_dequeueChatChar(); 
  330.  
  331.     if (gamekeydown[key_fire] || mousebuttons[mousebfire] 
  332.         || joybuttons[joybfire]) 
  333.         cmd->buttons |= BT_ATTACK; 
  334.  
  335.     if (gamekeydown[key_use] || joybuttons[joybuse] ) 
  336.     { 
  337.         cmd->buttons |= BT_USE;
  338.         // clear double clicks if hit use button 
  339.         dclicks = 0;                   
  340.     } 
  341.  
  342.     // chainsaw overrides 
  343.     for (i=0 ; i<NUMWEAPONS-1 ; i++)        
  344.         if (gamekeydown['1'+i]) 
  345.         { 
  346.             cmd->buttons |= BT_CHANGE; 
  347.             cmd->buttons |= i<<BT_WEAPONSHIFT; 
  348.             break; 
  349.         }
  350.     
  351.     // mouse
  352.     if (mousebuttons[mousebforward]) 
  353.         forward += forwardmove[speed];
  354.     
  355.     // forward double click
  356.     if (mousebuttons[mousebforward] != dclickstate && dclicktime > 1 ) 
  357.     { 
  358.         dclickstate = mousebuttons[mousebforward]; 
  359.         if (dclickstate) 
  360.             dclicks++; 
  361.         if (dclicks == 2) 
  362.         { 
  363.             cmd->buttons |= BT_USE; 
  364.             dclicks = 0; 
  365.         } 
  366.         else 
  367.             dclicktime = 0; 
  368.     } 
  369.     else 
  370.     { 
  371.         dclicktime += ticdup; 
  372.         if (dclicktime > 20) 
  373.         { 
  374.             dclicks = 0; 
  375.             dclickstate = 0; 
  376.         } 
  377.     }
  378.     
  379.     // strafe double click
  380.     bstrafe =
  381.         mousebuttons[mousebstrafe] 
  382.         || joybuttons[joybstrafe]; 
  383.     if (bstrafe != dclickstate2 && dclicktime2 > 1 ) 
  384.     { 
  385.         dclickstate2 = bstrafe; 
  386.         if (dclickstate2) 
  387.             dclicks2++; 
  388.         if (dclicks2 == 2) 
  389.         { 
  390.             cmd->buttons |= BT_USE; 
  391.             dclicks2 = 0; 
  392.         } 
  393.         else 
  394.             dclicktime2 = 0; 
  395.     } 
  396.     else 
  397.     { 
  398.         dclicktime2 += ticdup; 
  399.         if (dclicktime2 > 20) 
  400.         { 
  401.             dclicks2 = 0; 
  402.             dclickstate2 = 0; 
  403.         } 
  404.     } 
  405.  
  406.     forward += mousey; 
  407.     if (strafe) 
  408.         side += mousex*2; 
  409.     else 
  410.         cmd->angleturn -= mousex*0x8; 
  411.  
  412.     mousex = mousey = 0; 
  413.          
  414.     if (forward > MAXPLMOVE) 
  415.         forward = MAXPLMOVE; 
  416.     else if (forward < -MAXPLMOVE) 
  417.         forward = -MAXPLMOVE; 
  418.     if (side > MAXPLMOVE) 
  419.         side = MAXPLMOVE; 
  420.     else if (side < -MAXPLMOVE) 
  421.         side = -MAXPLMOVE; 
  422.  
  423.     cmd->forwardmove += forward; 
  424.     cmd->sidemove += side;
  425.     
  426.     // special buttons
  427.     if (sendpause) 
  428.     { 
  429.         sendpause = false; 
  430.         cmd->buttons = BT_SPECIAL | BTS_PAUSE; 
  431.     } 
  432.  
  433.     if (sendsave) 
  434.     { 
  435.         sendsave = false; 
  436.         cmd->buttons = BT_SPECIAL | BTS_SAVEGAME | (savegameslot<<BTS_SAVESHIFT); 
  437.     } 
  438.  
  439.  
  440. //
  441. // G_DoLoadLevel 
  442. //
  443. extern  gamestate_t     wipegamestate; 
  444.  
  445. void G_DoLoadLevel (void) 
  446.     int             i; 
  447.  
  448.     // Set the sky map.
  449.     // First thing, we have a dummy sky texture name,
  450.     //  a flat. The data is in the WAD only because
  451.     //  we look for an actual index, instead of simply
  452.     //  setting one.
  453.     skyflatnum = R_FlatNumForName ( SKYFLATNAME );
  454.  
  455.     // DOOM determines the sky texture to be used
  456.     // depending on the current episode, and the game version.
  457.     if ( (gamemode == commercial)
  458.          || ( gamemode == pack_tnt )
  459.          || ( gamemode == pack_plut ) )
  460.     {
  461.         skytexture = R_TextureNumForName ("SKY3");
  462.         if (gamemap < 12)
  463.             skytexture = R_TextureNumForName ("SKY1");
  464.         else
  465.             if (gamemap < 21)
  466.                 skytexture = R_TextureNumForName ("SKY2");
  467.     }
  468.  
  469.     levelstarttic = gametic;        // for time calculation
  470.     
  471.     if (wipegamestate == GS_LEVEL) 
  472.         wipegamestate = -1;             // force a wipe 
  473.  
  474.     gamestate = GS_LEVEL; 
  475.  
  476.     for (i=0 ; i<MAXPLAYERS ; i++) 
  477.     { 
  478.         if (playeringame[i] && players[i].playerstate == PST_DEAD) 
  479.             players[i].playerstate = PST_REBORN; 
  480.         memset (players[i].frags,0,sizeof(players[i].frags)); 
  481.     } 
  482.                  
  483.     P_SetupLevel (gameepisode, gamemap, 0, gameskill);    
  484.     displayplayer = consoleplayer;              // view the guy you are playing    
  485.     starttime = I_GetTime (); 
  486.     gameaction = ga_nothing; 
  487.     Z_CheckHeap ();
  488.     
  489.     // clear cmd building stuff
  490.     memset (gamekeydown, 0, sizeof(gamekeydown)); 
  491.     joyxmove = joyymove = 0; 
  492.     mousex = mousey = 0; 
  493.     sendpause = sendsave = paused = false; 
  494.     memset (mousebuttons, 0, sizeof(mousebuttons)); 
  495.     memset (joybuttons, 0, sizeof(joybuttons)); 
  496.  
  497.  
  498. //
  499. // G_Responder  
  500. // Get info needed to make ticcmd_ts for the players.
  501. // 
  502. boolean G_Responder (event_t* ev) 
  503.     // allow spy mode changes even during the demo
  504.     if (gamestate == GS_LEVEL && ev->type == ev_keydown 
  505.         && ev->data1 == KEY_F12 && (singledemo || !deathmatch) )
  506.     {
  507.         // spy mode 
  508.         do 
  509.         { 
  510.             displayplayer++; 
  511.             if (displayplayer == MAXPLAYERS) 
  512.                 displayplayer = 0; 
  513.         } while (!playeringame[displayplayer] && displayplayer != consoleplayer); 
  514.         return true; 
  515.     }
  516.     
  517.     // any other key pops up menu if in demos
  518.     if (gameaction == ga_nothing && !singledemo && 
  519.         (demoplayback || gamestate == GS_DEMOSCREEN) 
  520.         ) 
  521.     { 
  522.         if (ev->type == ev_keydown ||  
  523.             (ev->type == ev_mouse && ev->data1) || 
  524.             (ev->type == ev_joystick && ev->data1) ) 
  525.         { 
  526.             M_StartControlPanel (); 
  527.             return true; 
  528.         } 
  529.         return false; 
  530.     } 
  531.  
  532.     if (gamestate == GS_LEVEL) 
  533.     { 
  534. #if 0 
  535.         if (devparm && ev->type == ev_keydown && ev->data1 == ';') 
  536.         { 
  537.             G_DeathMatchSpawnPlayer (0); 
  538.             return true; 
  539.         } 
  540. #endif 
  541.         if (HU_Responder (ev)) 
  542.             return true;        // chat ate the event 
  543.         if (ST_Responder (ev)) 
  544.             return true;        // status window ate it 
  545.         if (AM_Responder (ev)) 
  546.             return true;        // automap ate it 
  547.     } 
  548.          
  549.     if (gamestate == GS_FINALE) 
  550.     { 
  551.         if (F_Responder (ev)) 
  552.             return true;        // finale ate the event 
  553.     } 
  554.          
  555.     switch (ev->type) 
  556.     { 
  557.       case ev_keydown: 
  558.         if (ev->data1 == KEY_PAUSE) 
  559.         { 
  560.             sendpause = true; 
  561.             return true; 
  562.         } 
  563.         if (ev->data1 <NUMKEYS) 
  564.             gamekeydown[ev->data1] = true; 
  565.         return true;    // eat key down events 
  566.  
  567.       case ev_keyup: 
  568.         if (ev->data1 <NUMKEYS) 
  569.             gamekeydown[ev->data1] = false; 
  570.         return false;   // always let key up events filter down 
  571.                  
  572.       case ev_mouse: 
  573.         mousebuttons[0] = ev->data1 & 1; 
  574.         mousebuttons[1] = ev->data1 & 2; 
  575.         mousebuttons[2] = ev->data1 & 4; 
  576.         mousex = ev->data2*(mouseSensitivity+5)/10; 
  577.         mousey = ev->data3*(mouseSensitivity+5)/10; 
  578.         return true;    // eat events 
  579.  
  580.       case ev_joystick: 
  581.         joybuttons[0] = ev->data1 & 1; 
  582.         joybuttons[1] = ev->data1 & 2; 
  583.         joybuttons[2] = ev->data1 & 4; 
  584.         joybuttons[3] = ev->data1 & 8; 
  585.         joyxmove = ev->data2; 
  586.         joyymove = ev->data3; 
  587.         return true;    // eat events 
  588.  
  589.       default: 
  590.         break; 
  591.     } 
  592.  
  593.     return false; 
  594.  
  595.  
  596.  
  597. //
  598. // G_Ticker
  599. // Make ticcmd_ts for the players.
  600. //
  601. void G_Ticker (void) 
  602.     int         i;
  603.     int         buf; 
  604.     ticcmd_t*   cmd;
  605.     
  606.     // do player reborns if needed
  607.     for (i=0 ; i<MAXPLAYERS ; i++) 
  608.         if (playeringame[i] && players[i].playerstate == PST_REBORN) 
  609.             G_DoReborn (i);
  610.     
  611.     // do things to change the game state
  612.     while (gameaction != ga_nothing) 
  613.     { 
  614.         switch (gameaction) 
  615.         { 
  616.           case ga_loadlevel: 
  617.             G_DoLoadLevel (); 
  618.             break; 
  619.           case ga_newgame: 
  620.             G_DoNewGame (); 
  621.             break; 
  622.           case ga_loadgame: 
  623.             G_DoLoadGame (); 
  624.             break; 
  625.           case ga_savegame: 
  626.             G_DoSaveGame (); 
  627.             break; 
  628.           case ga_playdemo: 
  629.             G_DoPlayDemo (); 
  630.             break; 
  631.           case ga_completed: 
  632.             G_DoCompleted (); 
  633.             break; 
  634.           case ga_victory: 
  635.             F_StartFinale (); 
  636.             break; 
  637.           case ga_worlddone: 
  638.             G_DoWorldDone (); 
  639.             break; 
  640.           case ga_screenshot: 
  641.             M_ScreenShot (); 
  642.             gameaction = ga_nothing; 
  643.             break; 
  644.           case ga_nothing: 
  645.             break; 
  646.         } 
  647.     }
  648.     
  649.     // get commands, check consistancy,
  650.     // and build new consistancy check
  651.     buf = (gametic/ticdup)%BACKUPTICS; 
  652.  
  653.     for (i=0 ; i<MAXPLAYERS ; i++)
  654.     {
  655.         if (playeringame[i]) 
  656.         { 
  657.             cmd = &players[i].cmd; 
  658.  
  659.             memcpy (cmd, &netcmds[i][buf], sizeof(ticcmd_t)); 
  660.  
  661.             if (demoplayback) 
  662.                 G_ReadDemoTiccmd (cmd); 
  663.             if (demorecording) 
  664.                 G_WriteDemoTiccmd (cmd);
  665.             
  666.             // check for turbo cheats
  667.             if (cmd->forwardmove > TURBOTHRESHOLD 
  668.                 && !(gametic&31) && ((gametic>>5)&3) == i )
  669.             {
  670.                 static char turbomessage[80];
  671.                 extern char *player_names[4];
  672.                 sprintf (turbomessage, "%s is turbo!",player_names[i]);
  673.                 players[consoleplayer].message = turbomessage;
  674.             }
  675.                         
  676.             if (netgame && !netdemo && !(gametic%ticdup) ) 
  677.             { 
  678.                 if (gametic > BACKUPTICS 
  679.                     && consistancy[i][buf] != cmd->consistancy) 
  680.                 { 
  681.                     I_Error ("consistency failure (%i should be %i)",
  682.                              cmd->consistancy, consistancy[i][buf]); 
  683.                 } 
  684.                 if (players[i].mo) 
  685.                     consistancy[i][buf] = players[i].mo->x; 
  686.                 else 
  687.                     consistancy[i][buf] = rndindex; 
  688.             } 
  689.         }
  690.     }
  691.     
  692.     // check for special buttons
  693.     for (i=0 ; i<MAXPLAYERS ; i++)
  694.     {
  695.         if (playeringame[i]) 
  696.         { 
  697.             if (players[i].cmd.buttons & BT_SPECIAL) 
  698.             { 
  699.                 switch (players[i].cmd.buttons & BT_SPECIALMASK) 
  700.                 { 
  701.                   case BTS_PAUSE: 
  702.                     paused ^= 1; 
  703.                     if (paused) 
  704.                         S_PauseSound (); 
  705.                     else 
  706.                         S_ResumeSound (); 
  707.                     break; 
  708.                                          
  709.                   case BTS_SAVEGAME: 
  710.                     if (!savedescription[0]) 
  711.                         strcpy (savedescription, "NET GAME"); 
  712.                     savegameslot =  
  713.                         (players[i].cmd.buttons & BTS_SAVEMASK)>>BTS_SAVESHIFT; 
  714.                     gameaction = ga_savegame; 
  715.                     break; 
  716.                 } 
  717.             } 
  718.         }
  719.     }
  720.     
  721.     // do main actions
  722.     switch (gamestate) 
  723.     { 
  724.       case GS_LEVEL: 
  725.         P_Ticker (); 
  726.         ST_Ticker (); 
  727.         AM_Ticker (); 
  728.         HU_Ticker ();            
  729.         break; 
  730.          
  731.       case GS_INTERMISSION: 
  732.         WI_Ticker (); 
  733.         break; 
  734.                          
  735.       case GS_FINALE: 
  736.         F_Ticker (); 
  737.         break; 
  738.  
  739.       case GS_DEMOSCREEN: 
  740.         D_PageTicker (); 
  741.         break; 
  742.     }        
  743.  
  744.  
  745. //
  746. // PLAYER STRUCTURE FUNCTIONS
  747. // also see P_SpawnPlayer in P_Things
  748. //
  749.  
  750. //
  751. // G_InitPlayer 
  752. // Called at the start.
  753. // Called by the game initialization functions.
  754. //
  755. void G_InitPlayer (int player) 
  756.     player_t*   p; 
  757.  
  758.     // set up the saved info         
  759.     p = &players[player]; 
  760.          
  761.     // clear everything else to defaults 
  762.     G_PlayerReborn (player); 
  763.          
  764.  
  765.  
  766.  
  767. //
  768. // G_PlayerFinishLevel
  769. // Can when a player completes a level.
  770. //
  771. void G_PlayerFinishLevel (int player) 
  772.     player_t*   p; 
  773.          
  774.     p = &players[player]; 
  775.          
  776.     memset (p->powers, 0, sizeof (p->powers)); 
  777.     memset (p->cards, 0, sizeof (p->cards)); 
  778.     p->mo->flags &= ~MF_SHADOW;         // cancel invisibility 
  779.     p->extralight = 0;                  // cancel gun flashes 
  780.     p->fixedcolormap = 0;               // cancel ir gogles 
  781.     p->damagecount = 0;                 // no palette changes 
  782.     p->bonuscount = 0; 
  783.  
  784.  
  785. //
  786. // G_PlayerReborn
  787. // Called after a player dies 
  788. // almost everything is cleared and initialized 
  789. //
  790. void G_PlayerReborn (int player) 
  791.     player_t*   p; 
  792.     int         i; 
  793.     int         frags[MAXPLAYERS]; 
  794.     int         killcount;
  795.     int         itemcount;
  796.     int         secretcount; 
  797.          
  798.     memcpy (frags,players[player].frags,sizeof(frags)); 
  799.     killcount = players[player].killcount; 
  800.     itemcount = players[player].itemcount; 
  801.     secretcount = players[player].secretcount; 
  802.          
  803.     p = &players[player]; 
  804.     memset (p, 0, sizeof(*p)); 
  805.  
  806.     memcpy (players[player].frags, frags, sizeof(players[player].frags)); 
  807.     players[player].killcount = killcount; 
  808.     players[player].itemcount = itemcount; 
  809.     players[player].secretcount = secretcount; 
  810.  
  811.     p->usedown = p->attackdown = true;  // don't do anything immediately 
  812.     p->playerstate = PST_LIVE;       
  813.     p->health = initial_health; // Dehacked
  814.     p->readyweapon = p->pendingweapon = wp_pistol; 
  815.     p->weaponowned[wp_fist] = true; 
  816.     p->weaponowned[wp_pistol] = true; 
  817.     p->ammo[am_clip] = initial_bullets; // Dehacked
  818.          
  819.     for (i=0 ; i<NUMAMMO ; i++) 
  820.         p->maxammo[i] = maxammo[i]; 
  821.                  
  822. }
  823.  
  824. //
  825. // G_CheckSpot  
  826. // Returns false if the player cannot be respawned
  827. // at the given mapthing_t spot  
  828. // because something is occupying it 
  829. //
  830. void P_SpawnPlayer (mapthing_t* mthing); 
  831.  
  832. boolean
  833. G_CheckSpot
  834. ( int           playernum,
  835.   mapthing_t*   mthing ) 
  836.     fixed_t             x;
  837.     fixed_t             y; 
  838.     subsector_t*        ss; 
  839.     unsigned            an; 
  840.     mobj_t*             mo; 
  841.     int                 i;
  842.         
  843.     if (!players[playernum].mo)
  844.     {
  845.         // first spawn of level, before corpses
  846.         for (i=0 ; i<playernum ; i++)
  847.             if (players[i].mo->x == mthing->x << FRACBITS
  848.                 && players[i].mo->y == mthing->y << FRACBITS)
  849.                 return false;   
  850.         return true;
  851.     }
  852.                 
  853.     x = mthing->x << FRACBITS; 
  854.     y = mthing->y << FRACBITS; 
  855.          
  856.     if (!P_CheckPosition (players[playernum].mo, x, y) ) 
  857.         return false; 
  858.  
  859.     // flush an old corpse if needed 
  860.     if (bodyqueslot >= BODYQUESIZE) 
  861.         P_RemoveMobj (bodyque[bodyqueslot%BODYQUESIZE]); 
  862.     bodyque[bodyqueslot%BODYQUESIZE] = players[playernum].mo; 
  863.     bodyqueslot++; 
  864.         
  865.     // spawn a teleport fog 
  866.     ss = R_PointInSubsector (x,y); 
  867.     an = ( ANG45 * (mthing->angle/45) ) >> ANGLETOFINESHIFT; 
  868.  
  869.     mo = P_SpawnMobj (x+20*finecosine[an], y+20*finesine[an] 
  870.                       , ss->sector->floorheight 
  871.                       , MT_TFOG); 
  872.          
  873.     if (players[consoleplayer].viewz != 1) 
  874.         S_StartSound (mo, sfx_telept);  // don't start sound on first frame 
  875.  
  876.     return true; 
  877.  
  878.  
  879. //
  880. // G_DeathMatchSpawnPlayer 
  881. // Spawns a player at one of the random death match spots 
  882. // called at level load and each death 
  883. //
  884. void G_DeathMatchSpawnPlayer (int playernum) 
  885.     int             i,j; 
  886.     int                         selections; 
  887.          
  888.     selections = deathmatch_p - deathmatchstarts; 
  889.     if (selections < 4) 
  890.         I_Error ("Only %i deathmatch spots, 4 required", selections); 
  891.  
  892.     for (j=0 ; j<20 ; j++) 
  893.     { 
  894.         i = P_Random() % selections; 
  895.         if (G_CheckSpot (playernum, &deathmatchstarts[i]) ) 
  896.         { 
  897.             deathmatchstarts[i].type = playernum+1; 
  898.             P_SpawnPlayer (&deathmatchstarts[i]); 
  899.             return; 
  900.         } 
  901.     } 
  902.  
  903.     // no good spot, so the player will probably get stuck 
  904.     P_SpawnPlayer (&playerstarts[playernum]); 
  905.  
  906. //
  907. // G_DoReborn 
  908. // 
  909. void G_DoReborn (int playernum) 
  910.     int                             i; 
  911.          
  912.     if (!netgame)
  913.     {
  914.         // reload the level from scratch
  915.         gameaction = ga_loadlevel;  
  916.     }
  917.     else 
  918.     {
  919.         // respawn at the start
  920.  
  921.         // first dissasociate the corpse 
  922.         players[playernum].mo->player = NULL;   
  923.                  
  924.         // spawn at random spot if in death match 
  925.         if (deathmatch) 
  926.         { 
  927.             G_DeathMatchSpawnPlayer (playernum); 
  928.             return; 
  929.         } 
  930.                  
  931.         if (G_CheckSpot (playernum, &playerstarts[playernum]) ) 
  932.         { 
  933.             P_SpawnPlayer (&playerstarts[playernum]); 
  934.             return; 
  935.         }
  936.         
  937.         // try to spawn at one of the other players spots 
  938.         for (i=0 ; i<MAXPLAYERS ; i++)
  939.         {
  940.             if (G_CheckSpot (playernum, &playerstarts[i]) ) 
  941.             { 
  942.                 playerstarts[i].type = playernum+1;     // fake as other player 
  943.                 P_SpawnPlayer (&playerstarts[i]); 
  944.                 playerstarts[i].type = i+1;             // restore 
  945.                 return; 
  946.             }       
  947.             // he's going to be inside something.  Too bad.
  948.         }
  949.         P_SpawnPlayer (&playerstarts[playernum]); 
  950.     } 
  951.  
  952.  
  953. void G_ScreenShot (void) 
  954.     gameaction = ga_screenshot; 
  955.  
  956.  
  957.  
  958. // DOOM Par Times
  959. int pars[4][10] = 
  960.     {0}, 
  961.     {0,30,75,120,90,165,180,180,30,165}, 
  962.     {0,90,90,90,120,90,360,240,30,170}, 
  963.     {0,90,45,90,150,90,90,165,30,135} 
  964. }; 
  965.  
  966. // DOOM II Par Times
  967. int cpars[32] =
  968. {
  969.     30,90,120,120,90,150,120,120,270,90,        //  1-10
  970.     210,150,150,150,210,150,420,150,210,150,    // 11-20
  971.     240,150,180,150,150,300,330,420,300,180,    // 21-30
  972.     120,30                                      // 31-32
  973. };
  974.  
  975.  
  976. //
  977. // G_DoCompleted 
  978. //
  979. boolean         secretexit; 
  980. extern char*    pagename; 
  981.  
  982. void G_ExitLevel (void) 
  983.     secretexit = false; 
  984.     gameaction = ga_completed; 
  985.  
  986. // Here's for the german edition.
  987. void G_SecretExitLevel (void) 
  988.     // IF NO WOLF3D LEVELS, NO SECRET EXIT!
  989.     if ( (gamemode == commercial)
  990.       && (W_CheckNumForName("map31")<0))
  991.         secretexit = false;
  992.     else
  993.         secretexit = true; 
  994.     gameaction = ga_completed; 
  995.  
  996. void G_DoCompleted (void) 
  997.     int             i; 
  998.          
  999.     gameaction = ga_nothing; 
  1000.  
  1001.     for (i=0 ; i<MAXPLAYERS ; i++) 
  1002.         if (playeringame[i]) 
  1003.             G_PlayerFinishLevel (i);        // take away cards and stuff 
  1004.          
  1005.     if (automapactive) 
  1006.         AM_Stop (); 
  1007.         
  1008.     if ( gamemode != commercial)
  1009.         switch(gamemap)
  1010.         {
  1011.           case 8:
  1012.             gameaction = ga_victory;
  1013.             return;
  1014.           case 9: 
  1015.             for (i=0 ; i<MAXPLAYERS ; i++) 
  1016.                 players[i].didsecret = true; 
  1017.             break;
  1018.         }
  1019.                 
  1020. //#if 0  Hmmm - why?
  1021.     if ( (gamemap == 8)
  1022.          && (gamemode != commercial) ) 
  1023.     {
  1024.         // victory 
  1025.         gameaction = ga_victory; 
  1026.         return; 
  1027.     } 
  1028.          
  1029.     if ( (gamemap == 9)
  1030.          && (gamemode != commercial) ) 
  1031.     {
  1032.         // exit secret level 
  1033.         for (i=0 ; i<MAXPLAYERS ; i++) 
  1034.             players[i].didsecret = true; 
  1035.     } 
  1036. //#endif
  1037.     
  1038.          
  1039.     wminfo.didsecret = players[consoleplayer].didsecret; 
  1040.     wminfo.epsd = gameepisode -1; 
  1041.     wminfo.last = gamemap -1;
  1042.     
  1043.     // wminfo.next is 0 biased, unlike gamemap
  1044.     if ( gamemode == commercial)
  1045.     {
  1046.         if (secretexit)
  1047.             switch(gamemap)
  1048.             {
  1049.               case 15: wminfo.next = 30; break;
  1050.               case 31: wminfo.next = 31; break;
  1051.             }
  1052.         else
  1053.             switch(gamemap)
  1054.             {
  1055.               case 31:
  1056.               case 32: wminfo.next = 15; break;
  1057.               default: wminfo.next = gamemap;
  1058.             }
  1059.     }
  1060.     else
  1061.     {
  1062.         if (secretexit) 
  1063.             wminfo.next = 8;    // go to secret level 
  1064.         else if (gamemap == 9) 
  1065.         {
  1066.             // returning from secret level 
  1067.             switch (gameepisode) 
  1068.             { 
  1069.               case 1: 
  1070.                 wminfo.next = 3; 
  1071.                 break; 
  1072.               case 2: 
  1073.                 wminfo.next = 5; 
  1074.                 break; 
  1075.               case 3: 
  1076.                 wminfo.next = 6; 
  1077.                 break; 
  1078.               case 4:
  1079.                 wminfo.next = 2;
  1080.                 break;
  1081.             }                
  1082.         } 
  1083.         else 
  1084.             wminfo.next = gamemap;          // go to next level 
  1085.     }
  1086.                  
  1087.     wminfo.maxkills = totalkills; 
  1088.     wminfo.maxitems = totalitems; 
  1089.     wminfo.maxsecret = totalsecret; 
  1090.     wminfo.maxfrags = 0; 
  1091.     if ( gamemode == commercial )
  1092.         wminfo.partime = 35*cpars[gamemap-1]; 
  1093.     else
  1094.         wminfo.partime = 35*pars[gameepisode][gamemap]; 
  1095.     wminfo.pnum = consoleplayer; 
  1096.  
  1097.     for (i=0 ; i<MAXPLAYERS ; i++) 
  1098.     { 
  1099.         wminfo.plyr[i].in = playeringame[i]; 
  1100.         wminfo.plyr[i].skills = players[i].killcount; 
  1101.         wminfo.plyr[i].sitems = players[i].itemcount; 
  1102.         wminfo.plyr[i].ssecret = players[i].secretcount; 
  1103.         wminfo.plyr[i].stime = leveltime; 
  1104.         memcpy (wminfo.plyr[i].frags, players[i].frags 
  1105.                 , sizeof(wminfo.plyr[i].frags)); 
  1106.     } 
  1107.  
  1108.     gamestate = GS_INTERMISSION; 
  1109.     viewactive = false; 
  1110.     automapactive = false; 
  1111.  
  1112.     if (statcopy)
  1113.         memcpy (statcopy, &wminfo, sizeof(wminfo));
  1114.         
  1115.     WI_Start (&wminfo); 
  1116.  
  1117.  
  1118. //
  1119. // G_WorldDone 
  1120. //
  1121. void G_WorldDone (void) 
  1122.     gameaction = ga_worlddone; 
  1123.  
  1124.     if (secretexit) 
  1125.         players[consoleplayer].didsecret = true; 
  1126.  
  1127.     if ( gamemode == commercial )
  1128.     {
  1129.         switch (gamemap)
  1130.         {
  1131.           case 15:
  1132.           case 31:
  1133.             if (!secretexit)
  1134.                 break;
  1135.           case 6:
  1136.           case 11:
  1137.           case 20:
  1138.           case 30:
  1139.             F_StartFinale ();
  1140.             break;
  1141.         }
  1142.     }
  1143.  
  1144. void G_DoWorldDone (void) 
  1145. {        
  1146.     gamestate = GS_LEVEL; 
  1147.     gamemap = wminfo.next+1; 
  1148.     G_DoLoadLevel (); 
  1149.     gameaction = ga_nothing; 
  1150.     viewactive = true; 
  1151.  
  1152.  
  1153.  
  1154. //
  1155. // G_InitFromSavegame
  1156. // Can be called by the startup code or the menu task. 
  1157. //
  1158. extern boolean setsizeneeded;
  1159. void R_ExecuteSetViewSize (void);
  1160.  
  1161. char    savename[256];
  1162.  
  1163. void G_LoadGame (char* name) 
  1164.     strcpy (savename, name); 
  1165.     gameaction = ga_loadgame; 
  1166.  
  1167. #define VERSIONSIZE             16 
  1168.  
  1169.  
  1170. void G_DoLoadGame (void) 
  1171.     int         length; 
  1172.     int         i; 
  1173.     int         a,b,c; 
  1174.     char        vcheck[VERSIONSIZE]; 
  1175.          
  1176.     gameaction = ga_nothing; 
  1177.          
  1178.     length = M_ReadFile (savename, &savebuffer); 
  1179.     save_p = savebuffer + SAVESTRINGSIZE;
  1180.     
  1181.     // skip the description field 
  1182.     memset (vcheck,0,sizeof(vcheck)); 
  1183.     sprintf (vcheck,"version %i",VERSION); 
  1184.     if (strcmp (save_p, vcheck)) 
  1185.         return;                         // bad version 
  1186.     save_p += VERSIONSIZE; 
  1187.                          
  1188.     gameskill = *save_p++; 
  1189.     gameepisode = *save_p++; 
  1190.     gamemap = *save_p++; 
  1191.     for (i=0 ; i<MAXPLAYERS ; i++) 
  1192.         playeringame[i] = *save_p++; 
  1193.  
  1194.     // load a base level 
  1195.     G_InitNew (gameskill, gameepisode, gamemap); 
  1196.  
  1197.     // get the times 
  1198.     a = *save_p++; 
  1199.     b = *save_p++; 
  1200.     c = *save_p++; 
  1201.     leveltime = (a<<16) + (b<<8) + c; 
  1202.          
  1203.     // dearchive all the modifications
  1204.     P_UnArchivePlayers (); 
  1205.     P_UnArchiveWorld (); 
  1206.     P_UnArchiveThinkers (); 
  1207.     P_UnArchiveSpecials (); 
  1208.  
  1209.     if (*save_p != 0x1d) 
  1210.         I_Error ("Bad savegame");
  1211.     
  1212.     // done 
  1213.     Z_Free (savebuffer); 
  1214.  
  1215.     if (setsizeneeded)
  1216.         R_ExecuteSetViewSize ();
  1217.     
  1218.     // draw the pattern into the back screen
  1219.     R_FillBackScreen ();   
  1220.  
  1221.  
  1222. //
  1223. // G_SaveGame
  1224. // Called by the menu task.
  1225. // Description is a 24 byte text string 
  1226. //
  1227. void
  1228. G_SaveGame
  1229. ( int   slot,
  1230.   char* description ) 
  1231.     savegameslot = slot; 
  1232.     strcpy (savedescription, description); 
  1233.     sendsave = true; 
  1234.  
  1235. void G_DoSaveGame (void) 
  1236.     char        name[100]; 
  1237.     char        name2[VERSIONSIZE]; 
  1238.     char*       description; 
  1239.     int         length; 
  1240.     int         i; 
  1241.         
  1242.     if (M_CheckParm("-cdrom"))
  1243.         sprintf(name,"c:\\doomdata\\"SAVEGAMENAME"%d.dsg",savegameslot);
  1244.     else
  1245.         sprintf (name,SAVEGAMENAME"%d.dsg",savegameslot); 
  1246.     description = savedescription; 
  1247.          
  1248.     save_p = savebuffer = screens[1]+0x4000; 
  1249.          
  1250.     memcpy (save_p, description, SAVESTRINGSIZE); 
  1251.     save_p += SAVESTRINGSIZE; 
  1252.     memset (name2,0,sizeof(name2)); 
  1253.     sprintf (name2,"version %i",VERSION); 
  1254.     memcpy (save_p, name2, VERSIONSIZE); 
  1255.     save_p += VERSIONSIZE; 
  1256.          
  1257.     *save_p++ = gameskill; 
  1258.     *save_p++ = gameepisode; 
  1259.     *save_p++ = gamemap; 
  1260.     for (i=0 ; i<MAXPLAYERS ; i++) 
  1261.         *save_p++ = playeringame[i]; 
  1262.     *save_p++ = leveltime>>16; 
  1263.     *save_p++ = leveltime>>8; 
  1264.     *save_p++ = leveltime; 
  1265.  
  1266.     P_ArchivePlayers (); 
  1267.     P_ArchiveWorld (); 
  1268.     P_ArchiveThinkers (); 
  1269.     P_ArchiveSpecials (); 
  1270.          
  1271.     *save_p++ = 0x1d;           // consistancy marker 
  1272.          
  1273.     length = save_p - savebuffer; 
  1274.     if (length > SAVEGAMESIZE) 
  1275.         I_Error ("Savegame buffer overrun"); 
  1276.     M_WriteFile (name, savebuffer, length); 
  1277.     gameaction = ga_nothing; 
  1278.     savedescription[0] = 0;              
  1279.          
  1280.     players[consoleplayer].message = GGSAVED; 
  1281.  
  1282.     // draw the pattern into the back screen
  1283.     R_FillBackScreen ();        
  1284.  
  1285.  
  1286. //
  1287. // G_InitNew
  1288. // Can be called by the startup code or the menu task,
  1289. // consoleplayer, displayplayer, playeringame[] should be set. 
  1290. //
  1291. skill_t d_skill; 
  1292. int     d_episode; 
  1293. int     d_map; 
  1294.  
  1295. void
  1296. G_DeferedInitNew
  1297. ( skill_t       skill,
  1298.   int           episode,
  1299.   int           map) 
  1300.     d_skill = skill; 
  1301.     d_episode = episode; 
  1302.     d_map = map; 
  1303.     gameaction = ga_newgame; 
  1304.  
  1305.  
  1306. void G_DoNewGame (void) 
  1307. {
  1308.     demoplayback = false; 
  1309.     netdemo = false;
  1310.     netgame = false;
  1311.     deathmatch = false;
  1312.     playeringame[1] = playeringame[2] = playeringame[3] = 0;
  1313.     respawnparm = false;
  1314.     fastparm = false;
  1315.     nomonsters = false;
  1316.     consoleplayer = 0;
  1317.     G_InitNew (d_skill, d_episode, d_map); 
  1318.     gameaction = ga_nothing; 
  1319.  
  1320. // The sky texture to be used instead of the F_SKY1 dummy.
  1321. extern  int     skytexture; 
  1322.  
  1323.  
  1324. void
  1325. G_InitNew
  1326. ( skill_t       skill,
  1327.   int           episode,
  1328.   int           map ) 
  1329.     int             i; 
  1330.          
  1331.     if (paused) 
  1332.     { 
  1333.         paused = false; 
  1334.         S_ResumeSound (); 
  1335.     } 
  1336.         
  1337.  
  1338.     if (skill > sk_nightmare) 
  1339.         skill = sk_nightmare;
  1340.  
  1341.  
  1342.     // This was quite messy with SPECIAL and commented parts.
  1343.     // Supposedly hacks to make the latest edition work.
  1344.     // It might not work properly.
  1345.     if (episode < 1)
  1346.       episode = 1; 
  1347.  
  1348.     if ( gamemode == retail )
  1349.     {
  1350.       if (episode > 4)
  1351.         episode = 4;
  1352.     }
  1353.     else if ( gamemode == shareware )
  1354.     {
  1355.       if (episode > 1) 
  1356.            episode = 1; // only start episode 1 on shareware
  1357.     }  
  1358.     else
  1359.     {
  1360.       if (episode > 3)
  1361.         episode = 3;
  1362.     }
  1363.     
  1364.  
  1365.   
  1366.     if (map < 1) 
  1367.         map = 1;
  1368.     
  1369.     if ( (map > 9)
  1370.          && ( gamemode != commercial) )
  1371.       map = 9; 
  1372.                  
  1373.     M_ClearRandom (); 
  1374.          
  1375.     if (skill == sk_nightmare || respawnparm )
  1376.         respawnmonsters = true;
  1377.     else
  1378.         respawnmonsters = false;
  1379.                 
  1380.     if (fastparm || (skill == sk_nightmare && gameskill != sk_nightmare) )
  1381.     { 
  1382.         for (i=S_SARG_RUN1 ; i<=S_SARG_PAIN2 ; i++) 
  1383.             states[i].tics >>= 1; 
  1384.         mobjinfo[MT_BRUISERSHOT].speed = 20*FRACUNIT; 
  1385.         mobjinfo[MT_HEADSHOT].speed = 20*FRACUNIT; 
  1386.         mobjinfo[MT_TROOPSHOT].speed = 20*FRACUNIT; 
  1387.     } 
  1388.     else if (skill != sk_nightmare && gameskill == sk_nightmare) 
  1389.     { 
  1390.         for (i=S_SARG_RUN1 ; i<=S_SARG_PAIN2 ; i++) 
  1391.             states[i].tics <<= 1; 
  1392.         mobjinfo[MT_BRUISERSHOT].speed = 15*FRACUNIT; 
  1393.         mobjinfo[MT_HEADSHOT].speed = 10*FRACUNIT; 
  1394.         mobjinfo[MT_TROOPSHOT].speed = 10*FRACUNIT; 
  1395.     } 
  1396.          
  1397.                          
  1398.     // force players to be initialized upon first level load         
  1399.     for (i=0 ; i<MAXPLAYERS ; i++) 
  1400.         players[i].playerstate = PST_REBORN; 
  1401.  
  1402.     usergame = true;                // will be set false if a demo 
  1403.     paused = false; 
  1404.     demoplayback = false; 
  1405.     automapactive = false; 
  1406.     viewactive = true; 
  1407.     gameepisode = episode; 
  1408.     gamemap = map; 
  1409.     gameskill = skill; 
  1410.  
  1411.     viewactive = true;
  1412.     
  1413.     // set the sky map for the episode
  1414.     if ( gamemode == commercial)
  1415.     {
  1416.         skytexture = R_TextureNumForName ("SKY3");
  1417.         if (gamemap < 12)
  1418.             skytexture = R_TextureNumForName ("SKY1");
  1419.         else
  1420.             if (gamemap < 21)
  1421.                 skytexture = R_TextureNumForName ("SKY2");
  1422.     }
  1423.     else
  1424.         switch (episode) 
  1425.         { 
  1426.           case 1: 
  1427.             skytexture = R_TextureNumForName ("SKY1"); 
  1428.             break; 
  1429.           case 2: 
  1430.             skytexture = R_TextureNumForName ("SKY2"); 
  1431.             break; 
  1432.           case 3: 
  1433.             skytexture = R_TextureNumForName ("SKY3"); 
  1434.             break; 
  1435.           case 4:       // Special Edition sky
  1436.             skytexture = R_TextureNumForName ("SKY4");
  1437.             break;
  1438.         } 
  1439.  
  1440.     G_DoLoadLevel (); 
  1441.  
  1442.  
  1443. //
  1444. // DEMO RECORDING 
  1445. // 
  1446. #define DEMOMARKER              0x80
  1447.  
  1448.  
  1449. void G_ReadDemoTiccmd (ticcmd_t* cmd) 
  1450.     if (*demo_p == DEMOMARKER) 
  1451.     {
  1452.         // end of demo data stream 
  1453.         G_CheckDemoStatus (); 
  1454.         return; 
  1455.     } 
  1456.     cmd->forwardmove = ((signed char)*demo_p++); 
  1457.     cmd->sidemove = ((signed char)*demo_p++); 
  1458.     cmd->angleturn = ((unsigned char)*demo_p++)<<8; 
  1459.     cmd->buttons = (unsigned char)*demo_p++; 
  1460.  
  1461.  
  1462. void G_WriteDemoTiccmd (ticcmd_t* cmd) 
  1463.     if (gamekeydown['q'])           // press q to end demo recording 
  1464.         G_CheckDemoStatus (); 
  1465.     *demo_p++ = cmd->forwardmove; 
  1466.     *demo_p++ = cmd->sidemove; 
  1467.     *demo_p++ = (cmd->angleturn+128)>>8; 
  1468.     *demo_p++ = cmd->buttons; 
  1469.     demo_p -= 4; 
  1470.     if (demo_p > demoend - 16)
  1471.     {
  1472.         // no more space 
  1473.         G_CheckDemoStatus (); 
  1474.         return; 
  1475.     } 
  1476.         
  1477.     G_ReadDemoTiccmd (cmd);         // make SURE it is exactly the same 
  1478.  
  1479.  
  1480.  
  1481. //
  1482. // G_RecordDemo 
  1483. // 
  1484. void G_RecordDemo (char* name) 
  1485.     int             i; 
  1486.     int                         maxsize;
  1487.         
  1488.     usergame = false; 
  1489.     strcpy (demoname, name); 
  1490.     strcat (demoname, ".lmp"); 
  1491.     maxsize = 0x20000;
  1492.     i = M_CheckParm ("-maxdemo");
  1493.     if (i && i<myargc-1)
  1494.         maxsize = atoi(myargv[i+1])*1024;
  1495.     demobuffer = Z_Malloc (maxsize,PU_STATIC,NULL); 
  1496.     demoend = demobuffer + maxsize;
  1497.         
  1498.     demorecording = true; 
  1499.  
  1500.  
  1501. void G_BeginRecording (void) 
  1502.     int             i; 
  1503.                 
  1504.     demo_p = demobuffer;
  1505.         
  1506.     *demo_p++ = VERSION;
  1507.     *demo_p++ = gameskill; 
  1508.     *demo_p++ = gameepisode; 
  1509.     *demo_p++ = gamemap; 
  1510.     *demo_p++ = deathmatch; 
  1511.     *demo_p++ = respawnparm;
  1512.     *demo_p++ = fastparm;
  1513.     *demo_p++ = nomonsters;
  1514.     *demo_p++ = consoleplayer;
  1515.          
  1516.     for (i=0 ; i<MAXPLAYERS ; i++) 
  1517.         *demo_p++ = playeringame[i];             
  1518.  
  1519.  
  1520. //
  1521. // G_PlayDemo 
  1522. //
  1523.  
  1524. char*   defdemoname; 
  1525.  
  1526. void G_DeferedPlayDemo (char* name) 
  1527.     defdemoname = name; 
  1528.     gameaction = ga_playdemo; 
  1529.  
  1530. void G_DoPlayDemo (void) 
  1531.     skill_t skill; 
  1532.     int             i, episode, map; 
  1533.          
  1534.     gameaction = ga_nothing; 
  1535.     demobuffer = demo_p = W_CacheLumpName (defdemoname, PU_STATIC); 
  1536.     if ( *demo_p++ != VERSION && !M_CheckParm("-forcedemo"))
  1537.     {
  1538.       fprintf( stderr, "Demo is from a different game version!\n");
  1539.       gameaction = ga_nothing;
  1540.       return;
  1541.     }
  1542.     
  1543.     skill = *demo_p++; 
  1544.     episode = *demo_p++; 
  1545.     map = *demo_p++; 
  1546.     deathmatch = *demo_p++;
  1547.     respawnparm = *demo_p++;
  1548.     fastparm = *demo_p++;
  1549.     nomonsters = *demo_p++;
  1550.     consoleplayer = *demo_p++;
  1551.         
  1552.     for (i=0 ; i<MAXPLAYERS ; i++) 
  1553.         playeringame[i] = *demo_p++; 
  1554.     if (playeringame[1]) 
  1555.     { 
  1556.         netgame = true; 
  1557.         netdemo = true; 
  1558.     }
  1559.  
  1560.     // don't spend a lot of time in loadlevel 
  1561.     precache = false;
  1562.     G_InitNew (skill, episode, map); 
  1563.     precache = true; 
  1564.  
  1565.     usergame = false; 
  1566.     demoplayback = true; 
  1567.  
  1568. //
  1569. // G_TimeDemo 
  1570. //
  1571. void G_TimeDemo (char* name) 
  1572. {        
  1573.     nodrawers = M_CheckParm ("-nodraw"); 
  1574.     noblit = M_CheckParm ("-noblit"); 
  1575.     timingdemo = true; 
  1576.     singletics = true; 
  1577.  
  1578.     defdemoname = name; 
  1579.     gameaction = ga_playdemo; 
  1580.  
  1581.  
  1582. /* 
  1583. =================== 
  1584. = G_CheckDemoStatus 
  1585. = Called after a death or level completion to allow demos to be cleaned up 
  1586. = Returns true if a new demo loop action will take place 
  1587. =================== 
  1588. */ 
  1589.  
  1590. boolean G_CheckDemoStatus (void) 
  1591.     int             endtime; 
  1592.          
  1593.     if (timingdemo) 
  1594.     { 
  1595.         endtime = I_GetTime (); 
  1596.         I_Error ("timed %i gametics in %i realtics",gametic 
  1597.                  , endtime-starttime); 
  1598.     } 
  1599.          
  1600.     if (demoplayback) 
  1601.     { 
  1602.         if (singledemo) 
  1603.             I_Quit (); 
  1604.                          
  1605.         Z_ChangeTag (demobuffer, PU_CACHE); 
  1606.         demoplayback = false; 
  1607.         netdemo = false;
  1608.         netgame = false;
  1609.         deathmatch = false;
  1610.         playeringame[1] = playeringame[2] = playeringame[3] = 0;
  1611.         respawnparm = false;
  1612.         fastparm = false;
  1613.         nomonsters = false;
  1614.         consoleplayer = 0;
  1615.         D_AdvanceDemo (); 
  1616.         return true; 
  1617.     } 
  1618.  
  1619.     if (demorecording) 
  1620.     { 
  1621.         *demo_p++ = DEMOMARKER; 
  1622.         M_WriteFile (demoname, demobuffer, demo_p - demobuffer); 
  1623.         Z_Free (demobuffer); 
  1624.         demorecording = false; 
  1625.         I_Error ("Demo %s recorded",demoname); 
  1626.     } 
  1627.          
  1628.     return false; 
  1629.  
  1630.  
  1631.  
  1632.