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