]> git.lizzy.rs Git - plan9front.git/blob - sys/src/games/doom/g_game.c
merge
[plan9front.git] / sys / src / games / doom / g_game.c
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 "doomdef.h" 
28 #include "doomstat.h"
29
30 #include "z_zone.h"
31 #include "f_finale.h"
32 #include "m_argv.h"
33 #include "m_misc.h"
34 #include "m_menu.h"
35 #include "m_random.h"
36 #include "i_system.h"
37
38 #include "p_setup.h"
39 #include "p_saveg.h"
40 #include "p_tick.h"
41
42 #include "d_main.h"
43
44 #include "wi_stuff.h"
45 #include "hu_stuff.h"
46 #include "st_stuff.h"
47 #include "am_map.h"
48
49 // Needs access to LFB.
50 #include "v_video.h"
51
52 #include "w_wad.h"
53
54 #include "p_local.h" 
55
56 #include "s_sound.h"
57
58 // Data.
59 #include "dstrings.h"
60 #include "sounds.h"
61
62 // SKY handling - still the wrong place.
63 #include "r_data.h"
64 #include "r_sky.h"
65
66
67
68 #include "g_game.h"
69
70
71 #define SAVEGAMESIZE    0x2c000
72 #define SAVESTRINGSIZE  24
73
74
75
76 boolean G_CheckDemoStatus (void); 
77 void    G_ReadDemoTiccmd (ticcmd_t* cmd); 
78 void    G_WriteDemoTiccmd (ticcmd_t* cmd); 
79 void    G_PlayerReborn (int player); 
80 void    G_InitNew (skill_t skill, int episode, int map); 
81  
82 void    G_DoReborn (int playernum); 
83  
84 void    G_DoLoadLevel (void); 
85 void    G_DoNewGame (void); 
86 void    G_DoLoadGame (void); 
87 void    G_DoPlayDemo (void); 
88 void    G_DoCompleted (void); 
89 void    G_DoVictory (void); 
90 void    G_DoWorldDone (void); 
91 void    G_DoSaveGame (void); 
92  
93  
94 gameaction_t    gameaction; 
95 gamestate_t     gamestate; 
96 skill_t         gameskill; 
97 boolean         respawnmonsters;
98 int             gameepisode; 
99 int             gamemap; 
100  
101 boolean         paused; 
102 boolean         sendpause;              // send a pause event next tic 
103 boolean         sendsave;               // send a save event next tic 
104 boolean         usergame;               // ok to save / end game 
105  
106 boolean         timingdemo;             // if true, exit with report on completion 
107 boolean         nodrawers;              // for comparative timing purposes 
108 boolean         noblit;                 // for comparative timing purposes 
109 int             starttime;              // for comparative timing purposes       
110  
111 boolean         viewactive; 
112  
113 boolean         deathmatch;             // only if started as net death 
114 boolean         netgame;                // only true if packets are broadcast 
115 boolean         playeringame[MAXPLAYERS]; 
116 player_t        players[MAXPLAYERS]; 
117  
118 int             consoleplayer;          // player taking events and displaying 
119 int             displayplayer;          // view being displayed 
120 int             gametic; 
121 int             levelstarttic;          // gametic at level start 
122 int             totalkills, totalitems, totalsecret;    // for intermission 
123  
124 char            demoname[32]; 
125 boolean         demorecording; 
126 boolean         demoplayback; 
127 boolean         netdemo; 
128 byte*           demobuffer;
129 byte*           demo_p;
130 byte*           demoend; 
131 boolean         singledemo;             // quit after playing a demo from cmdline 
132  
133 boolean         precache = true;        // if true, load all graphics at start 
134  
135 wbstartstruct_t wminfo;                 // parms for world map / intermission 
136  
137 short           consistancy[MAXPLAYERS][BACKUPTICS]; 
138  
139 byte*           savebuffer;
140  
141  
142 // 
143 // controls (have defaults) 
144 // 
145 int             key_right;
146 int             key_left;
147
148 int             key_up;
149 int             key_down; 
150 int             key_strafeleft;
151 int             key_straferight; 
152 int             key_fire;
153 int             key_use;
154 int             key_strafe;
155 int             key_speed; 
156  
157 int             mousebfire; 
158 int             mousebstrafe; 
159 int             mousebforward; 
160  
161 int             joybfire; 
162 int             joybstrafe; 
163 int             joybuse; 
164 int             joybspeed; 
165  
166  
167  
168 #define MAXPLMOVE               (forwardmove[1]) 
169  
170 #define TURBOTHRESHOLD  0x32
171
172 fixed_t         forwardmove[2] = {0x19, 0x32}; 
173 fixed_t         sidemove[2] = {0x18, 0x28}; 
174 fixed_t         angleturn[3] = {640, 1280, 320};        // + slow turn 
175
176 #define SLOWTURNTICS    6 
177  
178 #define NUMKEYS         256 
179
180 boolean         gamekeydown[NUMKEYS]; 
181 int             turnheld;                               // for accelerative turning 
182  
183 boolean         mousearray[4]; 
184 boolean*        mousebuttons = &mousearray[1];          // allow [-1]
185
186 // mouse values are used once 
187 int             mousex;
188 int             mousey;         
189
190 int             dclicktime;
191 int             dclickstate;
192 int             dclicks; 
193 int             dclicktime2;
194 int             dclickstate2;
195 int             dclicks2;
196
197 // joystick values are repeated 
198 int             joyxmove;
199 int             joyymove;
200 boolean         joyarray[5]; 
201 boolean*        joybuttons = &joyarray[1];              // allow [-1] 
202  
203 int             savegameslot; 
204 char            savedescription[32]; 
205  
206  
207 #define BODYQUESIZE     32
208
209 mobj_t*         bodyque[BODYQUESIZE]; 
210 int             bodyqueslot; 
211  
212 void*           statcopy;                               // for statistics driver
213  
214  
215  
216 int G_CmdChecksum (ticcmd_t* cmd) 
217
218     int         i;
219     int         sum = 0; 
220          
221     for (i=0 ; i< sizeof(*cmd)/4 - 1 ; i++) 
222         sum += ((int *)cmd)[i]; 
223                  
224     return sum; 
225
226  
227
228 //
229 // G_BuildTiccmd
230 // Builds a ticcmd from all of the available inputs
231 // or reads it from the demo buffer. 
232 // If recording a demo, write it out 
233 // 
234 void G_BuildTiccmd (ticcmd_t* cmd) 
235
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 //
438 // G_DoLoadLevel 
439 //
440 extern  gamestate_t     wipegamestate; 
441  
442 void G_DoLoadLevel (void) 
443
444     int             i; 
445
446     // Set the sky map.
447     // First thing, we have a dummy sky texture name,
448     //  a flat. The data is in the WAD only because
449     //  we look for an actual index, instead of simply
450     //  setting one.
451     skyflatnum = R_FlatNumForName ( SKYFLATNAME );
452
453     // DOOM determines the sky texture to be used
454     // depending on the current episode, and the game version.
455     if ( (gamemode == commercial)
456          || ( gamemode == pack_tnt )
457          || ( gamemode == pack_plut ) )
458     {
459         skytexture = R_TextureNumForName ("SKY3");
460         if (gamemap < 12)
461             skytexture = R_TextureNumForName ("SKY1");
462         else
463             if (gamemap < 21)
464                 skytexture = R_TextureNumForName ("SKY2");
465     }
466
467     levelstarttic = gametic;        // for time calculation
468     
469     if (wipegamestate == GS_LEVEL) 
470         wipegamestate = -1;             // force a wipe 
471
472     gamestate = GS_LEVEL; 
473
474     for (i=0 ; i<MAXPLAYERS ; i++) 
475     { 
476         if (playeringame[i] && players[i].playerstate == PST_DEAD) 
477             players[i].playerstate = PST_REBORN; 
478         memset (players[i].frags,0,sizeof(players[i].frags)); 
479     } 
480                  
481     P_SetupLevel (gameepisode, gamemap);
482     displayplayer = consoleplayer;              // view the guy you are playing    
483     starttime = I_GetTime (); 
484     gameaction = ga_nothing; 
485     Z_CheckHeap ();
486     
487     // clear cmd building stuff
488     memset (gamekeydown, 0, sizeof(gamekeydown)); 
489     joyxmove = joyymove = 0; 
490     mousex = mousey = 0; 
491     sendpause = sendsave = paused = false; 
492     memset (mousebuttons, 0, sizeof(mousebuttons)); 
493     memset (joybuttons, 0, sizeof(joybuttons)); 
494
495  
496  
497 //
498 // G_Responder  
499 // Get info needed to make ticcmd_ts for the players.
500 // 
501 boolean G_Responder (event_t* ev) 
502
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 (HU_Responder (ev)) 
535             return true;        // chat ate the event 
536         if (ST_Responder (ev)) 
537             return true;        // status window ate it 
538         if (AM_Responder (ev)) 
539             return true;        // automap ate it 
540     } 
541          
542     if (gamestate == GS_FINALE) 
543     { 
544         if (F_Responder (ev)) 
545             return true;        // finale ate the event 
546     } 
547          
548     switch (ev->type) 
549     { 
550       case ev_keydown: 
551         if (ev->data1 == KEY_PAUSE) 
552         { 
553             sendpause = true; 
554             return true; 
555         } 
556         if (ev->data1 <NUMKEYS) 
557             gamekeydown[ev->data1] = true; 
558         return true;    // eat key down events 
559  
560       case ev_keyup: 
561         if (ev->data1 <NUMKEYS) 
562             gamekeydown[ev->data1] = false; 
563         return false;   // always let key up events filter down 
564                  
565       case ev_mouse: 
566         mousebuttons[0] = ev->data1 & 1; 
567         mousebuttons[1] = ev->data1 & 2; 
568         mousebuttons[2] = ev->data1 & 4; 
569         mousex = ev->data2*(mouseSensitivity+5)/10; 
570         mousey = ev->data3*(mouseSensitivity+5)/10; 
571         return true;    // eat events 
572  
573       case ev_joystick: 
574         joybuttons[0] = ev->data1 & 1; 
575         joybuttons[1] = ev->data1 & 2; 
576         joybuttons[2] = ev->data1 & 4; 
577         joybuttons[3] = ev->data1 & 8; 
578         joyxmove = ev->data2; 
579         joyymove = ev->data3; 
580         return true;    // eat events 
581  
582       default: 
583         break; 
584     } 
585  
586     return false; 
587
588  
589  
590  
591 //
592 // G_Ticker
593 // Make ticcmd_ts for the players.
594 //
595 void G_Ticker (void) 
596
597     int         i;
598     int         buf; 
599     ticcmd_t*   cmd;
600     
601     // do player reborns if needed
602     for (i=0 ; i<MAXPLAYERS ; i++) 
603         if (playeringame[i] && players[i].playerstate == PST_REBORN) 
604             G_DoReborn (i);
605     
606     // do things to change the game state
607     while (gameaction != ga_nothing) 
608     { 
609         switch (gameaction) 
610         { 
611           case ga_loadlevel: 
612             G_DoLoadLevel (); 
613             break; 
614           case ga_newgame: 
615             G_DoNewGame (); 
616             break; 
617           case ga_loadgame: 
618             G_DoLoadGame (); 
619             break; 
620           case ga_savegame: 
621             G_DoSaveGame (); 
622             break; 
623           case ga_playdemo: 
624             G_DoPlayDemo (); 
625             break; 
626           case ga_completed: 
627             G_DoCompleted (); 
628             break; 
629           case ga_victory: 
630             F_StartFinale (); 
631             break; 
632           case ga_worlddone: 
633             G_DoWorldDone (); 
634             break; 
635           case ga_screenshot: 
636             M_ScreenShot (); 
637             gameaction = ga_nothing; 
638             break; 
639           case ga_nothing: 
640             break; 
641         } 
642     }
643     
644     // get commands, check consistancy,
645     // and build new consistancy check
646     buf = (gametic/ticdup)%BACKUPTICS; 
647  
648     for (i=0 ; i<MAXPLAYERS ; i++)
649     {
650         if (playeringame[i]) 
651         { 
652             cmd = &players[i].cmd; 
653  
654             memcpy (cmd, &netcmds[i][buf], sizeof(ticcmd_t)); 
655  
656             if (demoplayback) 
657                 G_ReadDemoTiccmd (cmd); 
658             if (demorecording) 
659                 G_WriteDemoTiccmd (cmd);
660             
661             // check for turbo cheats
662             if (cmd->forwardmove > TURBOTHRESHOLD 
663                 && !(gametic&31) && ((gametic>>5)&3) == i )
664             {
665                 static char turbomessage[80];
666                 extern char *player_names[4];
667                 sprintf (turbomessage, "%s is turbo!",player_names[i]);
668                 players[consoleplayer].message = turbomessage;
669             }
670                         
671             if (netgame && !netdemo && !(gametic%ticdup) ) 
672             { 
673                 if (gametic > BACKUPTICS 
674                     && consistancy[i][buf] != cmd->consistancy) 
675                 { 
676                     I_Error ("consistency failure (%i should be %i)",
677                              cmd->consistancy, consistancy[i][buf]); 
678                 } 
679                 if (players[i].mo) 
680                     consistancy[i][buf] = players[i].mo->x; 
681                 else 
682                     consistancy[i][buf] = rndindex; 
683             } 
684         }
685     }
686     
687     // check for special buttons
688     for (i=0 ; i<MAXPLAYERS ; i++)
689     {
690         if (playeringame[i]) 
691         { 
692             if (players[i].cmd.buttons & BT_SPECIAL) 
693             { 
694                 switch (players[i].cmd.buttons & BT_SPECIALMASK) 
695                 { 
696                   case BTS_PAUSE: 
697                     paused ^= 1; 
698                     if (paused) 
699                         S_PauseSound (); 
700                     else 
701                         S_ResumeSound (); 
702                     break; 
703                                          
704                   case BTS_SAVEGAME: 
705                     if (!savedescription[0]) 
706                         strcpy (savedescription, "NET GAME"); 
707                     savegameslot =  
708                         (players[i].cmd.buttons & BTS_SAVEMASK)>>BTS_SAVESHIFT; 
709                     gameaction = ga_savegame; 
710                     break; 
711                 } 
712             } 
713         }
714     }
715     
716     // do main actions
717     switch (gamestate) 
718     { 
719       case GS_LEVEL: 
720         P_Ticker (); 
721         ST_Ticker (); 
722         AM_Ticker (); 
723         HU_Ticker ();            
724         break; 
725          
726       case GS_INTERMISSION: 
727         WI_Ticker (); 
728         break; 
729                          
730       case GS_FINALE: 
731         F_Ticker (); 
732         break; 
733  
734       case GS_DEMOSCREEN: 
735         D_PageTicker (); 
736         break; 
737     }        
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 {
753     // clear everything else to defaults 
754     G_PlayerReborn (player); 
755 }
756  
757  
758
759 //
760 // G_PlayerFinishLevel
761 // Can when a player completes a level.
762 //
763 void G_PlayerFinishLevel (int player) 
764
765     player_t*   p; 
766          
767     p = &players[player]; 
768          
769     memset (p->powers, 0, sizeof (p->powers)); 
770     memset (p->cards, 0, sizeof (p->cards)); 
771     p->mo->flags &= ~MF_SHADOW;         // cancel invisibility 
772     p->extralight = 0;                  // cancel gun flashes 
773     p->fixedcolormap = 0;               // cancel ir gogles 
774     p->damagecount = 0;                 // no palette changes 
775     p->bonuscount = 0; 
776
777  
778
779 //
780 // G_PlayerReborn
781 // Called after a player dies 
782 // almost everything is cleared and initialized 
783 //
784 void G_PlayerReborn (int player) 
785
786     player_t*   p; 
787     int         i; 
788     int         frags[MAXPLAYERS]; 
789     int         killcount;
790     int         itemcount;
791     int         secretcount; 
792          
793     memcpy (frags,players[player].frags,sizeof(frags)); 
794     killcount = players[player].killcount; 
795     itemcount = players[player].itemcount; 
796     secretcount = players[player].secretcount; 
797          
798     p = &players[player]; 
799     memset (p, 0, sizeof(*p)); 
800  
801     memcpy (players[player].frags, frags, sizeof(players[player].frags)); 
802     players[player].killcount = killcount; 
803     players[player].itemcount = itemcount; 
804     players[player].secretcount = secretcount; 
805  
806     p->usedown = p->attackdown = true;  // don't do anything immediately 
807     p->playerstate = PST_LIVE;       
808     p->health = MAXHEALTH; 
809     p->readyweapon = p->pendingweapon = wp_pistol; 
810     p->weaponowned[wp_fist] = true; 
811     p->weaponowned[wp_pistol] = true; 
812     p->ammo[am_clip] = 50; 
813          
814     for (i=0 ; i<NUMAMMO ; i++) 
815         p->maxammo[i] = maxammo[i]; 
816                  
817 }
818
819 //
820 // G_CheckSpot  
821 // Returns false if the player cannot be respawned
822 // at the given mapthing_t spot  
823 // because something is occupying it 
824 //
825 void P_SpawnPlayer (mapthing_t* mthing); 
826  
827 boolean
828 G_CheckSpot
829 ( int           playernum,
830   mapthing_t*   mthing ) 
831
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 //
877 // G_DeathMatchSpawnPlayer 
878 // Spawns a player at one of the random death match spots 
879 // called at level load and each death 
880 //
881 void G_DeathMatchSpawnPlayer (int playernum) 
882
883     int             i,j; 
884     int                         selections; 
885          
886     selections = deathmatch_p - deathmatchstarts; 
887     if (selections < 4) 
888         I_Error ("Only %i deathmatch spots, 4 required", selections); 
889  
890     for (j=0 ; j<20 ; j++) 
891     { 
892         i = P_Random() % selections; 
893         if (G_CheckSpot (playernum, &deathmatchstarts[i]) ) 
894         { 
895             deathmatchstarts[i].type = playernum+1; 
896             P_SpawnPlayer (&deathmatchstarts[i]); 
897             return; 
898         } 
899     } 
900  
901     // no good spot, so the player will probably get stuck 
902     P_SpawnPlayer (&playerstarts[playernum]); 
903
904
905 //
906 // G_DoReborn 
907 // 
908 void G_DoReborn (int playernum) 
909
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  
954 void G_ScreenShot (void) 
955
956     gameaction = ga_screenshot; 
957
958  
959
960
961 // DOOM Par Times
962 int pars[4][10] = 
963
964     {0}, 
965     {0,30,75,120,90,165,180,180,30,165}, 
966     {0,90,90,90,120,90,360,240,30,170}, 
967     {0,90,45,90,150,90,90,165,30,135} 
968 }; 
969
970 // DOOM II Par Times
971 int cpars[32] =
972 {
973     30,90,120,120,90,150,120,120,270,90,        //  1-10
974     210,150,150,150,210,150,420,150,210,150,    // 11-20
975     240,150,180,150,150,300,330,420,300,180,    // 21-30
976     120,30                                      // 31-32
977 };
978  
979
980 //
981 // G_DoCompleted 
982 //
983 boolean         secretexit; 
984 extern char*    pagename; 
985  
986 void G_ExitLevel (void) 
987
988     secretexit = false; 
989     gameaction = ga_completed; 
990
991
992 // Here's for the german edition.
993 void G_SecretExitLevel (void) 
994
995     // IF NO WOLF3D LEVELS, NO SECRET EXIT!
996     if ( (gamemode == commercial)
997       && (W_CheckNumForName("map31")<0))
998         secretexit = false;
999     else
1000         secretexit = true; 
1001     gameaction = ga_completed; 
1002
1003  
1004 void G_DoCompleted (void) 
1005
1006     int             i; 
1007          
1008     gameaction = ga_nothing; 
1009  
1010     for (i=0 ; i<MAXPLAYERS ; i++) 
1011         if (playeringame[i]) 
1012             G_PlayerFinishLevel (i);        // take away cards and stuff 
1013          
1014     if (automapactive) 
1015         AM_Stop (); 
1016         
1017     if ( gamemode != commercial)
1018         switch(gamemap)
1019         {
1020           case 8:
1021             gameaction = ga_victory;
1022             return;
1023           case 9: 
1024             for (i=0 ; i<MAXPLAYERS ; i++) 
1025                 players[i].didsecret = true; 
1026             break;
1027         }
1028                 
1029 //#if 0  Hmmm - why?
1030     if ( (gamemap == 8)
1031          && (gamemode != commercial) ) 
1032     {
1033         // victory 
1034         gameaction = ga_victory; 
1035         return; 
1036     } 
1037          
1038     if ( (gamemap == 9)
1039          && (gamemode != commercial) ) 
1040     {
1041         // exit secret level 
1042         for (i=0 ; i<MAXPLAYERS ; i++) 
1043             players[i].didsecret = true; 
1044     } 
1045 //#endif
1046     
1047          
1048     wminfo.didsecret = players[consoleplayer].didsecret; 
1049     wminfo.epsd = gameepisode -1; 
1050     wminfo.last = gamemap -1;
1051     
1052     // wminfo.next is 0 biased, unlike gamemap
1053     if ( gamemode == commercial)
1054     {
1055         if (secretexit)
1056             switch(gamemap)
1057             {
1058               case 15: wminfo.next = 30; break;
1059               case 31: wminfo.next = 31; break;
1060             }
1061         else
1062             switch(gamemap)
1063             {
1064               case 31:
1065               case 32: wminfo.next = 15; break;
1066               default: wminfo.next = gamemap;
1067             }
1068     }
1069     else
1070     {
1071         if (secretexit) 
1072             wminfo.next = 8;    // go to secret level 
1073         else if (gamemap == 9) 
1074         {
1075             // returning from secret level 
1076             switch (gameepisode) 
1077             { 
1078               case 1: 
1079                 wminfo.next = 3; 
1080                 break; 
1081               case 2: 
1082                 wminfo.next = 5; 
1083                 break; 
1084               case 3: 
1085                 wminfo.next = 6; 
1086                 break; 
1087               case 4:
1088                 wminfo.next = 2;
1089                 break;
1090             }                
1091         } 
1092         else 
1093             wminfo.next = gamemap;          // go to next level 
1094     }
1095                  
1096     wminfo.maxkills = totalkills; 
1097     wminfo.maxitems = totalitems; 
1098     wminfo.maxsecret = totalsecret; 
1099     wminfo.maxfrags = 0; 
1100     if ( gamemode == commercial )
1101         wminfo.partime = 35*cpars[gamemap-1]; 
1102     else
1103         wminfo.partime = 35*pars[gameepisode][gamemap]; 
1104     wminfo.pnum = consoleplayer; 
1105  
1106     for (i=0 ; i<MAXPLAYERS ; i++) 
1107     { 
1108         wminfo.plyr[i].in = playeringame[i]; 
1109         wminfo.plyr[i].skills = players[i].killcount; 
1110         wminfo.plyr[i].sitems = players[i].itemcount; 
1111         wminfo.plyr[i].ssecret = players[i].secretcount; 
1112         wminfo.plyr[i].stime = leveltime; 
1113         memcpy (wminfo.plyr[i].frags, players[i].frags 
1114                 , sizeof(wminfo.plyr[i].frags)); 
1115     } 
1116  
1117     gamestate = GS_INTERMISSION; 
1118     viewactive = false; 
1119     automapactive = false; 
1120  
1121     if (statcopy)
1122         memcpy (statcopy, &wminfo, sizeof(wminfo));
1123         
1124     WI_Start (&wminfo); 
1125
1126
1127
1128 //
1129 // G_WorldDone 
1130 //
1131 void G_WorldDone (void) 
1132
1133     gameaction = ga_worlddone; 
1134
1135     if (secretexit) 
1136         players[consoleplayer].didsecret = true; 
1137
1138     if ( gamemode == commercial )
1139     {
1140         switch (gamemap)
1141         {
1142           case 15:
1143           case 31:
1144             if (!secretexit)
1145                 break;
1146           case 6:
1147           case 11:
1148           case 20:
1149           case 30:
1150             F_StartFinale ();
1151             break;
1152         }
1153     }
1154
1155  
1156 void G_DoWorldDone (void) 
1157 {        
1158     gamestate = GS_LEVEL; 
1159     gamemap = wminfo.next+1; 
1160     G_DoLoadLevel (); 
1161     gameaction = ga_nothing; 
1162     viewactive = true; 
1163
1164  
1165
1166
1167 //
1168 // G_InitFromSavegame
1169 // Can be called by the startup code or the menu task. 
1170 //
1171 extern boolean setsizeneeded;
1172 void R_ExecuteSetViewSize (void);
1173
1174 char    savename[256];
1175
1176 void G_LoadGame (char* name) 
1177
1178     strcpy (savename, name); 
1179     gameaction = ga_loadgame; 
1180
1181  
1182 #define VERSIONSIZE             16 
1183
1184
1185 void G_DoLoadGame (void) 
1186
1187     int         i; 
1188     int         a,b,c; 
1189     char        vcheck[VERSIONSIZE]; 
1190          
1191     gameaction = ga_nothing; 
1192          
1193     M_ReadFile (savename, &savebuffer); 
1194     save_p = savebuffer + SAVESTRINGSIZE;
1195     
1196     // skip the description field 
1197     memset (vcheck,0,sizeof(vcheck)); 
1198     sprintf (vcheck,"version %i",VERSION); 
1199     if (strcmp ((char*)save_p, vcheck)) 
1200         return;                         // bad version 
1201     save_p += VERSIONSIZE; 
1202                          
1203     gameskill = *save_p++; 
1204     gameepisode = *save_p++; 
1205     gamemap = *save_p++; 
1206     for (i=0 ; i<MAXPLAYERS ; i++) 
1207         playeringame[i] = *save_p++; 
1208
1209     // load a base level 
1210     G_InitNew (gameskill, gameepisode, gamemap); 
1211  
1212     // get the times 
1213     a = *save_p++; 
1214     b = *save_p++; 
1215     c = *save_p++; 
1216     leveltime = (a<<16) + (b<<8) + c; 
1217          
1218     // dearchive all the modifications
1219     P_UnArchivePlayers (); 
1220     P_UnArchiveWorld (); 
1221     P_UnArchiveThinkers (); 
1222     P_UnArchiveSpecials (); 
1223  
1224     if (*save_p != 0x1d) 
1225         I_Error ("Bad savegame");
1226     
1227     // done 
1228     Z_Free (savebuffer); 
1229  
1230     if (setsizeneeded)
1231         R_ExecuteSetViewSize ();
1232     
1233     // draw the pattern into the back screen
1234     R_FillBackScreen ();   
1235
1236  
1237
1238 //
1239 // G_SaveGame
1240 // Called by the menu task.
1241 // Description is a 24 byte text string 
1242 //
1243 void
1244 G_SaveGame
1245 ( int   slot,
1246   char* description ) 
1247
1248     savegameslot = slot; 
1249     strcpy (savedescription, description); 
1250     sendsave = true; 
1251
1252  
1253 void G_DoSaveGame (void) 
1254
1255     char        name[100]; 
1256     char        name2[VERSIONSIZE]; 
1257     char*       description; 
1258     int         length; 
1259     int         i; 
1260         
1261     if (M_CheckParm("-cdrom"))
1262         sprintf(name,"c:\\doomdata\\"SAVEGAMENAME"%d.dsg",savegameslot);
1263     else
1264         sprintf (name,SAVEGAMENAME"%d.dsg",savegameslot); 
1265     description = savedescription; 
1266          
1267     save_p = savebuffer = screens[1]+0x4000; 
1268          
1269     memcpy (save_p, description, SAVESTRINGSIZE); 
1270     save_p += SAVESTRINGSIZE; 
1271     memset (name2,0,sizeof(name2)); 
1272     sprintf (name2,"version %i",VERSION); 
1273     memcpy (save_p, name2, VERSIONSIZE); 
1274     save_p += VERSIONSIZE; 
1275          
1276     *save_p++ = gameskill; 
1277     *save_p++ = gameepisode; 
1278     *save_p++ = gamemap; 
1279     for (i=0 ; i<MAXPLAYERS ; i++) 
1280         *save_p++ = playeringame[i]; 
1281     *save_p++ = leveltime>>16; 
1282     *save_p++ = leveltime>>8; 
1283     *save_p++ = leveltime; 
1284  
1285     P_ArchivePlayers (); 
1286     P_ArchiveWorld (); 
1287     P_ArchiveThinkers (); 
1288     P_ArchiveSpecials (); 
1289          
1290     *save_p++ = 0x1d;           // consistancy marker 
1291          
1292     length = save_p - savebuffer; 
1293     if (length > SAVEGAMESIZE) 
1294         I_Error ("Savegame buffer overrun"); 
1295     M_WriteFile (name, savebuffer, length); 
1296     gameaction = ga_nothing; 
1297     savedescription[0] = 0;              
1298          
1299     players[consoleplayer].message = GGSAVED; 
1300
1301     // draw the pattern into the back screen
1302     R_FillBackScreen ();        
1303
1304  
1305
1306 //
1307 // G_InitNew
1308 // Can be called by the startup code or the menu task,
1309 // consoleplayer, displayplayer, playeringame[] should be set. 
1310 //
1311 skill_t d_skill; 
1312 int     d_episode; 
1313 int     d_map; 
1314  
1315 void
1316 G_DeferedInitNew
1317 ( skill_t       skill,
1318   int           episode,
1319   int           map) 
1320
1321     d_skill = skill; 
1322     d_episode = episode; 
1323     d_map = map; 
1324     gameaction = ga_newgame; 
1325
1326
1327
1328 void G_DoNewGame (void) 
1329 {
1330     demoplayback = false; 
1331     netdemo = false;
1332     netgame = false;
1333     deathmatch = false;
1334     playeringame[1] = playeringame[2] = playeringame[3] = 0;
1335     respawnparm = false;
1336     fastparm = false;
1337     nomonsters = false;
1338     consoleplayer = 0;
1339     G_InitNew (d_skill, d_episode, d_map); 
1340     gameaction = ga_nothing; 
1341
1342
1343 // The sky texture to be used instead of the F_SKY1 dummy.
1344 extern  int     skytexture; 
1345
1346
1347 void
1348 G_InitNew
1349 ( skill_t       skill,
1350   int           episode,
1351   int           map ) 
1352
1353     int             i; 
1354          
1355     if (paused) 
1356     { 
1357         paused = false; 
1358         S_ResumeSound (); 
1359     } 
1360         
1361
1362     if (skill > sk_nightmare) 
1363         skill = sk_nightmare;
1364
1365
1366     // This was quite messy with SPECIAL and commented parts.
1367     // Supposedly hacks to make the latest edition work.
1368     // It might not work properly.
1369     if (episode < 1)
1370       episode = 1; 
1371
1372     if ( gamemode == retail )
1373     {
1374       if (episode > 4)
1375         episode = 4;
1376     }
1377     else if ( gamemode == shareware )
1378     {
1379       if (episode > 1) 
1380            episode = 1; // only start episode 1 on shareware
1381     }  
1382     else
1383     {
1384       if (episode > 3)
1385         episode = 3;
1386     }
1387     
1388
1389   
1390     if (map < 1) 
1391         map = 1;
1392     
1393     if ( (map > 9)
1394          && ( gamemode != commercial) )
1395       map = 9; 
1396                  
1397     M_ClearRandom (); 
1398          
1399     if (skill == sk_nightmare || respawnparm )
1400         respawnmonsters = true;
1401     else
1402         respawnmonsters = false;
1403                 
1404     if (fastparm || (skill == sk_nightmare && gameskill != sk_nightmare) )
1405     { 
1406         for (i=S_SARG_RUN1 ; i<=S_SARG_PAIN2 ; i++) 
1407             states[i].tics >>= 1; 
1408         mobjinfo[MT_BRUISERSHOT].speed = 20*FRACUNIT; 
1409         mobjinfo[MT_HEADSHOT].speed = 20*FRACUNIT; 
1410         mobjinfo[MT_TROOPSHOT].speed = 20*FRACUNIT; 
1411     } 
1412     else if (skill != sk_nightmare && gameskill == sk_nightmare) 
1413     { 
1414         for (i=S_SARG_RUN1 ; i<=S_SARG_PAIN2 ; i++) 
1415             states[i].tics <<= 1; 
1416         mobjinfo[MT_BRUISERSHOT].speed = 15*FRACUNIT; 
1417         mobjinfo[MT_HEADSHOT].speed = 10*FRACUNIT; 
1418         mobjinfo[MT_TROOPSHOT].speed = 10*FRACUNIT; 
1419     } 
1420          
1421                          
1422     // force players to be initialized upon first level load         
1423     for (i=0 ; i<MAXPLAYERS ; i++) 
1424         players[i].playerstate = PST_REBORN; 
1425  
1426     usergame = true;                // will be set false if a demo 
1427     paused = false; 
1428     demoplayback = false; 
1429     automapactive = false; 
1430     viewactive = true;
1431     gameepisode = episode; 
1432     gamemap = map; 
1433     gameskill = skill; 
1434  
1435     
1436     // set the sky map for the episode
1437     if ( gamemode == commercial)
1438     {
1439         skytexture = R_TextureNumForName ("SKY3");
1440         if (gamemap < 12)
1441             skytexture = R_TextureNumForName ("SKY1");
1442         else
1443             if (gamemap < 21)
1444                 skytexture = R_TextureNumForName ("SKY2");
1445     }
1446     else
1447         switch (episode) 
1448         { 
1449           case 1: 
1450             skytexture = R_TextureNumForName ("SKY1"); 
1451             break; 
1452           case 2: 
1453             skytexture = R_TextureNumForName ("SKY2"); 
1454             break; 
1455           case 3: 
1456             skytexture = R_TextureNumForName ("SKY3"); 
1457             break; 
1458           case 4:       // Special Edition sky
1459             skytexture = R_TextureNumForName ("SKY4");
1460             break;
1461         } 
1462  
1463     G_DoLoadLevel (); 
1464
1465  
1466
1467 //
1468 // DEMO RECORDING 
1469 // 
1470 #define DEMOMARKER              0x80
1471
1472
1473 void G_ReadDemoTiccmd (ticcmd_t* cmd) 
1474
1475     if (*demo_p == DEMOMARKER) 
1476     {
1477         // end of demo data stream 
1478         G_CheckDemoStatus (); 
1479         return; 
1480     } 
1481     cmd->forwardmove = ((signed char)*demo_p++); 
1482     cmd->sidemove = ((signed char)*demo_p++); 
1483     cmd->angleturn = ((unsigned char)*demo_p++)<<8; 
1484     cmd->buttons = (unsigned char)*demo_p++; 
1485
1486
1487
1488 void G_WriteDemoTiccmd (ticcmd_t* cmd) 
1489
1490     if (gamekeydown['q'])           // press q to end demo recording 
1491         G_CheckDemoStatus (); 
1492     *demo_p++ = cmd->forwardmove; 
1493     *demo_p++ = cmd->sidemove; 
1494     *demo_p++ = (cmd->angleturn+128)>>8; 
1495     *demo_p++ = cmd->buttons; 
1496     demo_p -= 4; 
1497     if (demo_p > demoend - 16)
1498     {
1499         // no more space 
1500         G_CheckDemoStatus (); 
1501         return; 
1502     } 
1503         
1504     G_ReadDemoTiccmd (cmd);         // make SURE it is exactly the same 
1505
1506  
1507  
1508  
1509 //
1510 // G_RecordDemo 
1511 // 
1512 void G_RecordDemo (char* name) 
1513
1514     int             i; 
1515     int                         maxsize;
1516         
1517     usergame = false; 
1518     strcpy (demoname, name); 
1519     strcat (demoname, ".lmp"); 
1520     maxsize = 0x20000;
1521     i = M_CheckParm ("-maxdemo");
1522     if (i && i<myargc-1)
1523         maxsize = atoi(myargv[i+1])*1024;
1524     demobuffer = Z_Malloc (maxsize,PU_STATIC,NULL); 
1525     demoend = demobuffer + maxsize;
1526         
1527     demorecording = true; 
1528
1529  
1530  
1531 void G_BeginRecording (void) 
1532
1533     int             i; 
1534                 
1535     demo_p = demobuffer;
1536         
1537     *demo_p++ = VERSION;
1538     *demo_p++ = gameskill; 
1539     *demo_p++ = gameepisode; 
1540     *demo_p++ = gamemap; 
1541     *demo_p++ = deathmatch; 
1542     *demo_p++ = respawnparm;
1543     *demo_p++ = fastparm;
1544     *demo_p++ = nomonsters;
1545     *demo_p++ = consoleplayer;
1546          
1547     for (i=0 ; i<MAXPLAYERS ; i++) 
1548         *demo_p++ = playeringame[i];             
1549
1550  
1551
1552 //
1553 // G_PlayDemo 
1554 //
1555
1556 char*   defdemoname; 
1557  
1558 void G_DeferedPlayDemo (char* name) 
1559
1560     defdemoname = name; 
1561     gameaction = ga_playdemo; 
1562
1563  
1564 void G_DoPlayDemo (void) 
1565
1566     skill_t skill; 
1567     int             i, episode, map; 
1568          
1569     gameaction = ga_nothing; 
1570     demobuffer = demo_p = W_CacheLumpName (defdemoname, PU_STATIC); 
1571     switch (*demo_p++) {
1572     case VERSION:
1573         break;
1574     default:
1575         fprintf(stderr, "Demo is from a different game version!\n");
1576         fprintf(stderr, "VERSION=%d demover=%d\n", VERSION, *demo_p);
1577         break;
1578 /*
1579         gameaction = ga_nothing;
1580         return;
1581 */
1582     }
1583
1584     skill = *demo_p++; 
1585     episode = *demo_p++; 
1586     map = *demo_p++; 
1587     deathmatch = *demo_p++;
1588     respawnparm = *demo_p++;
1589     fastparm = *demo_p++;
1590     nomonsters = *demo_p++;
1591     consoleplayer = *demo_p++;
1592
1593     // PORTME TODO : MAXPLAYERS assumes 4 in demo format
1594     for (i=0 ; i<MAXPLAYERS ; i++) 
1595         playeringame[i] = *demo_p++; 
1596     if (playeringame[1]) 
1597     { 
1598         netgame = true; 
1599         netdemo = true; 
1600     }
1601
1602     // don't spend a lot of time in loadlevel 
1603     precache = false;
1604     G_InitNew (skill, episode, map); 
1605     precache = true; 
1606
1607     usergame = false; 
1608     demoplayback = true; 
1609 }
1610
1611 //
1612 // G_TimeDemo 
1613 //
1614 void G_TimeDemo (char* name) 
1615 {        
1616     nodrawers = M_CheckParm ("-nodraw"); 
1617     noblit = M_CheckParm ("-noblit"); 
1618     timingdemo = true; 
1619     singletics = true; 
1620
1621     defdemoname = name; 
1622     gameaction = ga_playdemo; 
1623
1624  
1625  
1626 /* 
1627 =================== 
1628
1629 = G_CheckDemoStatus 
1630
1631 = Called after a death or level completion to allow demos to be cleaned up 
1632 = Returns true if a new demo loop action will take place 
1633 =================== 
1634 */ 
1635  
1636 boolean G_CheckDemoStatus (void) 
1637
1638     int             endtime; 
1639          
1640     if (timingdemo) 
1641     { 
1642         endtime = I_GetTime (); 
1643         I_Error ("timed %i gametics in %i realtics",gametic 
1644                  , endtime-starttime); 
1645     } 
1646          
1647     if (demoplayback) 
1648     { 
1649         if (singledemo) 
1650             I_Quit (); 
1651                          
1652         Z_ChangeTag (demobuffer, PU_CACHE); 
1653         demoplayback = false; 
1654         netdemo = false;
1655         netgame = false;
1656         deathmatch = false;
1657         playeringame[1] = playeringame[2] = playeringame[3] = 0;
1658         respawnparm = false;
1659         fastparm = false;
1660         nomonsters = false;
1661         consoleplayer = 0;
1662         D_AdvanceDemo (); 
1663         return true; 
1664     } 
1665  
1666     if (demorecording) 
1667     { 
1668         *demo_p++ = DEMOMARKER; 
1669         M_WriteFile (demoname, demobuffer, demo_p - demobuffer); 
1670         Z_Free (demobuffer); 
1671         demorecording = false; 
1672         I_Error ("Demo %s recorded",demoname); 
1673     } 
1674          
1675     return false; 
1676
1677  
1678  
1679