FS#12756 by Marek Salaba - update Czech translation
[maemo-rb.git] / apps / plugins / doom / d_main.c
blobbe45e6a37ef0192c214fac17323ff6186b3c669f
1 /* Emacs style mode select -*- C++ -*-
2 *-----------------------------------------------------------------------------
5 * PrBoom a Doom port merged with LxDoom and LSDLDoom
6 * based on BOOM, a modified and improved DOOM engine
7 * Copyright (C) 1999 by
8 * id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman
9 * Copyright (C) 1999-2000 by
10 * Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 * 02111-1307, USA.
27 * DESCRIPTION:
28 * DOOM main program (D_DoomMain) and game loop (D_DoomLoop),
29 * plus functions to determine game mode (shareware, registered),
30 * parse command line parameters, configure game parameters (turbo),
31 * and call the startup functions.
33 *-----------------------------------------------------------------------------
37 #include "rockmacros.h"
39 #include "doomdef.h"
40 #include "doomtype.h"
41 #include "doomstat.h"
42 #include "dstrings.h"
43 #include "sounds.h"
44 #include "z_zone.h"
45 #include "w_wad.h"
46 #include "s_sound.h"
47 #include "v_video.h"
48 #include "f_finale.h"
49 #include "f_wipe.h"
50 #include "m_argv.h"
51 #include "m_misc.h"
52 #include "m_menu.h"
53 #include "i_system.h"
54 #include "i_sound.h"
55 #include "i_video.h"
56 #include "g_game.h"
57 #include "hu_stuff.h"
58 #include "wi_stuff.h"
59 #include "st_stuff.h"
60 #include "am_map.h"
61 #include "p_setup.h"
62 #include "r_draw.h"
63 #include "r_main.h"
64 #include "d_main.h"
65 #include "d_deh.h" // Ty 04/08/98 - Externalizations
66 #include "m_swap.h"
68 // DEHacked support - Ty 03/09/97 // CPhipps - const char*'s
69 void ProcessDehFile(const char *filename, const char *outfilename, int lumpnum);
71 // CPhipps - removed wadfiles[] stuff
73 boolean devparm; // started game with -devparm
75 // jff 1/24/98 add new versions of these variables to remember command line
76 boolean clnomonsters; // checkparm of -nomonsters
77 boolean clrespawnparm; // checkparm of -respawn
78 boolean clfastparm; // checkparm of -fast
79 // jff 1/24/98 end definition of command line version of play mode switches
81 boolean nomonsters; // working -nomonsters
82 boolean respawnparm; // working -respawn
83 boolean fastparm; // working -fast
84 boolean dehout=true;
86 boolean singletics = false; // debug flag to cancel adaptiveness
88 bool doomexit;
90 //jff 1/22/98 parms for disabling music and sound
91 boolean nomusicparm=0;
93 //jff 4/18/98
94 extern boolean inhelpscreens;
96 skill_t startskill;
97 int startepisode;
98 int startmap;
99 boolean autostart;
100 int debugfile;
101 int ffmap;
103 boolean advancedemo;
105 extern boolean timingdemo, singledemo, demoplayback, fastdemo; // killough
107 int basetic;
109 void D_DoAdvanceDemo (void);
112 * D_PostEvent - Event handling
114 * Called by I/O functions when an event is received.
115 * Try event handlers for each code area in turn.
118 void D_PostEvent(event_t *ev)
120 /* cph - suppress all input events at game start
121 * FIXME: This is a lousy kludge */
122 if (gametic < 3)
123 return;
125 if(!M_Responder(ev)) {
126 if(gamestate == GS_LEVEL && (
127 HU_Responder(ev) ||
128 ST_Responder(ev) ||
129 AM_Responder(ev)
131 return;
132 else
133 G_Responder(ev);
138 // D_Wipe
140 // CPhipps - moved the screen wipe code from D_Display to here
141 // The screens to wipe between are already stored, this just does the timing
142 // and screen updating
144 static void D_Wipe(void)
146 boolean done;
147 int wipestart = I_GetTime () - 1;
151 int nowtime, tics;
154 //I_uSleep(5000); // CPhipps - don't thrash cpu in this loop
155 nowtime = I_GetTime();
156 tics = nowtime - wipestart;
158 while (!tics);
159 wipestart = nowtime;
161 done = wipe_ScreenWipe(0,0,SCREENWIDTH,SCREENHEIGHT,tics);
162 M_Drawer(); // menu is drawn even on top of wipes
163 I_FinishUpdate(); // page flip or blit buffer
165 while (!done);
169 // D_Display
170 // draw current display, possibly wiping it from the previous
173 // wipegamestate can be set to -1 to force a wipe on the next draw
174 gamestate_t wipegamestate = GS_DEMOSCREEN;
175 extern boolean setsizeneeded;
176 extern int showMessages;
178 void D_Display (void)
180 static boolean isborderstate IDATA_ATTR= false;
181 static boolean borderwillneedredraw IDATA_ATTR= false;
182 static gamestate_t oldgamestate IDATA_ATTR= -1;
183 boolean wipe;
184 boolean viewactive = false, isborder = false;
186 if (nodrawers) // for comparative timing / profiling
187 return;
189 // save the current screen if about to wipe
190 if ((wipe = gamestate != wipegamestate))
191 wipe_StartScreen(0, 0, SCREENWIDTH, SCREENHEIGHT);
193 if (gamestate != GS_LEVEL) { // Not a level
194 switch (oldgamestate) {
195 case (gamestate_t)-1:
196 case GS_LEVEL:
197 V_SetPalette(0); // cph - use default (basic) palette
198 default:
199 break;
202 switch (gamestate) {
203 case GS_INTERMISSION:
204 WI_Drawer();
205 break;
206 case GS_FINALE:
207 F_Drawer();
208 break;
209 case GS_DEMOSCREEN:
210 D_PageDrawer();
211 break;
212 default:
213 break;
215 } else if (gametic != basetic) { // In a level
216 boolean redrawborderstuff;
218 HU_Erase();
220 if (setsizeneeded) { // change the view size if needed
221 R_ExecuteSetViewSize();
222 oldgamestate = -1; // force background redraw
225 // Work out if the player view is visible, and if there is a border
226 viewactive = (!(automapmode & am_active) || (automapmode & am_overlay)) && !inhelpscreens;
227 isborder = viewactive ? (viewheight != SCREENHEIGHT) : (!inhelpscreens && (automapmode & am_active));
229 if (oldgamestate != GS_LEVEL) {
230 R_FillBackScreen (); // draw the pattern into the back screen
231 redrawborderstuff = isborder;
232 } else {
233 // CPhipps -
234 // If there is a border, and either there was no border last time,
235 // or the border might need refreshing, then redraw it.
236 redrawborderstuff = isborder && (!isborderstate || borderwillneedredraw);
237 // The border may need redrawing next time if the border surrounds the screen,
238 // and there is a menu being displayed
239 borderwillneedredraw = menuactive && isborder && viewactive && (viewwidth != SCREENWIDTH);
242 if (redrawborderstuff)
243 R_DrawViewBorder();
245 // Now do the drawing
246 if (viewactive)
247 R_RenderPlayerView (&players[displayplayer]);
248 if (automapmode & am_active)
249 AM_Drawer();
250 ST_Drawer((viewheight != SCREENHEIGHT) || ((automapmode & am_active) && !(automapmode & am_overlay)), redrawborderstuff);
251 R_DrawViewBorder();
253 HU_Drawer();
256 isborderstate = isborder;
257 oldgamestate = wipegamestate = gamestate;
259 // draw pause pic
260 if (paused) {
261 static int x;
263 if (!x) { // Cache results of x pos calc
264 int lump = W_GetNumForName("M_PAUSE");
265 const patch_t* p = W_CacheLumpNum(lump);
266 x = (320 - SHORT(p->width))/2;
267 W_UnlockLumpNum(lump);
270 // CPhipps - updated for new patch drawing
271 V_DrawNamePatch(x, (!(automapmode & am_active) || (automapmode & am_overlay))
272 ? 4+(viewwindowy*200/SCREENHEIGHT) : 4, // cph - Must un-stretch viewwindowy
273 0, "M_PAUSE", CR_DEFAULT, VPT_STRETCH);
276 // menus go directly to the screen
277 M_Drawer(); // menu is drawn even on top of everything
278 D_BuildNewTiccmds();
280 // normal update
281 if (!wipe)
282 I_FinishUpdate (); // page flip or blit buffer
283 else {
284 // wipe update
285 wipe_EndScreen(0, 0, SCREENWIDTH, SCREENHEIGHT);
286 D_Wipe();
291 // D_DoomLoop()
293 // Not a globally visible function,
294 // just included for source reference,
295 // called by D_DoomMain, never exits.
296 // Manages timing and IO,
297 // calls all ?_Responder, ?_Ticker, and ?_Drawer,
298 // calls I_GetTime, I_StartFrame, and I_StartTic
301 extern boolean demorecording;
303 static void D_DoomLoop (void)
305 basetic = gametic;
307 I_SubmitSound();
309 while (!doomexit)
311 // process one or more tics
312 if (singletics)
314 I_StartTic ();
315 G_BuildTiccmd (&netcmds[consoleplayer][maketic%BACKUPTICS]);
316 if (advancedemo)
317 D_DoAdvanceDemo ();
318 M_Ticker ();
319 G_Ticker ();
320 gametic++;
321 maketic++;
323 else
324 TryRunTics (); // will run at least one tic
326 // killough 3/16/98: change consoleplayer to displayplayer
327 if (players[displayplayer].mo) // cph 2002/08/10
328 S_UpdateSounds(players[displayplayer].mo);// move positional sounds
330 // Update display, next frame, with current state.
331 D_Display();
333 // Give the system some time
334 rb->yield();
339 // DEMO LOOP
342 static int demosequence; // killough 5/2/98: made static
343 static int pagetic;
344 static const char *pagename; // CPhipps - const
347 // D_PageTicker
348 // Handles timing for warped projection
350 void D_PageTicker(void)
352 if (--pagetic < 0)
353 D_AdvanceDemo();
357 // D_PageDrawer
359 void D_PageDrawer(void)
361 // CPhipps - updated for new patch drawing
362 V_DrawNamePatch(0, 0, 0, pagename, CR_DEFAULT, VPT_STRETCH);
366 // D_AdvanceDemo
367 // Called after each demo or intro demosequence finishes
369 void D_AdvanceDemo (void)
371 advancedemo = true;
374 /* killough 11/98: functions to perform demo sequences
375 * cphipps 10/99: constness fixes
378 static void D_SetPageName(const char *name)
380 pagename = name;
383 static void D_DrawTitle1(const char *name)
385 S_StartMusic(mus_intro);
386 pagetic = (TICRATE*170)/35;
387 D_SetPageName(name);
390 static void D_DrawTitle2(const char *name)
392 S_StartMusic(mus_dm2ttl);
393 D_SetPageName(name);
396 /* killough 11/98: tabulate demo sequences
399 static struct
401 void (*func)(const char *);
402 const char *name;
403 } const demostates[][4] =
406 {D_DrawTitle1, "TITLEPIC"},
407 {D_DrawTitle1, "TITLEPIC"},
408 {D_DrawTitle2, "TITLEPIC"},
409 {D_DrawTitle1, "TITLEPIC"},
413 {G_DeferedPlayDemo, "demo1"},
414 {G_DeferedPlayDemo, "demo1"},
415 {G_DeferedPlayDemo, "demo1"},
416 {G_DeferedPlayDemo, "demo1"},
419 {D_SetPageName, "CREDIT"},
420 {D_SetPageName, "CREDIT"},
421 {D_SetPageName, "CREDIT"},
422 {D_SetPageName, "CREDIT"},
426 {G_DeferedPlayDemo, "demo2"},
427 {G_DeferedPlayDemo, "demo2"},
428 {G_DeferedPlayDemo, "demo2"},
429 {G_DeferedPlayDemo, "demo2"},
433 {D_SetPageName, "HELP2"},
434 {D_SetPageName, "HELP2"},
435 {D_SetPageName, "CREDIT"},
436 {D_DrawTitle1, "TITLEPIC"},
440 {G_DeferedPlayDemo, "demo3"},
441 {G_DeferedPlayDemo, "demo3"},
442 {G_DeferedPlayDemo, "demo3"},
443 {G_DeferedPlayDemo, "demo3"},
447 {NULL,0},
448 {NULL,0},
449 {NULL,0},
450 {D_SetPageName, "CREDIT"},
454 {NULL,0},
455 {NULL,0},
456 {NULL,0},
457 {G_DeferedPlayDemo, "demo4"},
461 {NULL,0},
462 {NULL,0},
463 {NULL,0},
464 {NULL,0},
469 * This cycles through the demo sequences.
470 * killough 11/98: made table-driven
473 void D_DoAdvanceDemo(void)
475 players[consoleplayer].playerstate = PST_LIVE; /* not reborn */
476 advancedemo = usergame = paused = false;
477 gameaction = ga_nothing;
479 pagetic = TICRATE * 11; /* killough 11/98: default behavior */
480 gamestate = GS_DEMOSCREEN;
482 if (netgame && !demoplayback) {
483 demosequence = 0;
484 } else
485 if (!demostates[++demosequence][gamemode].func)
486 demosequence = 0;
487 demostates[demosequence][gamemode].func(demostates[demosequence][gamemode].name);
491 // D_StartTitle
493 void D_StartTitle (void)
495 gameaction = ga_nothing;
496 demosequence = -1;
497 D_AdvanceDemo();
501 // D_AddFile
503 // Rewritten by Lee Killough
505 // Ty 08/29/98 - add source parm to indicate where this came from
506 // CPhipps - static, const char* parameter
507 // - source is an enum
508 // - modified to allocate & use new wadfiles array
509 void D_AddFile (const char *file, wad_source_t source)
511 wadfiles = realloc(wadfiles, sizeof(*wadfiles)*(numwadfiles+1));
512 wadfiles[numwadfiles].name =
513 AddDefaultExtension(strcpy(malloc(strlen(file)+5), file), ".wad");
514 wadfiles[numwadfiles].src = source; // Ty 08/29/98
515 numwadfiles++;
519 // CheckIWAD
521 // Verify a file is indeed tagged as an IWAD
522 // Scan its lumps for levelnames and return gamemode as indicated
523 // Detect missing wolf levels in DOOM II
525 // The filename to check is passed in iwadname, the gamemode detected is
526 // returned in gmode, hassec returns the presence of secret levels
528 // jff 4/19/98 Add routine to test IWAD for validity and determine
529 // the gamemode from it. Also note if DOOM II, whether secret levels exist
530 // CPhipps - const char* for iwadname, made static
531 #if 0
532 static void CheckIWAD(const char *iwadname,GameMode_t *gmode,boolean *hassec)
534 if ( !fileexists (iwadname) )
536 int ud=0,rg=0,sw=0,cm=0,sc=0;
537 int handle;
539 // Identify IWAD correctly
540 if ( (handle = open (iwadname,O_RDONLY)) != -1)
542 wadinfo_t header;
544 // read IWAD header
545 read (handle, &header, sizeof(header));
546 if (!strncmp(header.identification,"IWAD",4))
548 size_t length;
549 filelump_t *fileinfo;
551 // read IWAD directory
552 header.numlumps = LONG(header.numlumps);
553 header.infotableofs = LONG(header.infotableofs);
554 length = header.numlumps;
555 fileinfo = malloc(length*sizeof(filelump_t));
556 lseek (handle, header.infotableofs, SEEK_SET);
557 read (handle, fileinfo, length*sizeof(filelump_t));
558 close(handle);
560 // scan directory for levelname lumps
561 while (length--)
562 if (fileinfo[length].name[0] == 'E' &&
563 fileinfo[length].name[2] == 'M' &&
564 fileinfo[length].name[4] == 0)
566 if (fileinfo[length].name[1] == '4')
567 ++ud;
568 else if (fileinfo[length].name[1] == '3')
569 ++rg;
570 else if (fileinfo[length].name[1] == '2')
571 ++rg;
572 else if (fileinfo[length].name[1] == '1')
573 ++sw;
575 else if (fileinfo[length].name[0] == 'M' &&
576 fileinfo[length].name[1] == 'A' &&
577 fileinfo[length].name[2] == 'P' &&
578 fileinfo[length].name[5] == 0)
580 ++cm;
581 if (fileinfo[length].name[3] == '3')
582 if (fileinfo[length].name[4] == '1' ||
583 fileinfo[length].name[4] == '2')
584 ++sc;
587 free(fileinfo);
589 else // missing IWAD tag in header
590 I_Error("CheckIWAD: IWAD tag %s not present", iwadname);
592 else // error from open call
593 I_Error("CheckIWAD: Can't open IWAD %s", iwadname);
595 // Determine game mode from levels present
596 // Must be a full set for whichever mode is present
597 // Lack of wolf-3d levels also detected here
599 *gmode = indetermined;
600 *hassec = false;
601 if (cm>=30)
603 *gmode = commercial;
604 *hassec = sc>=2;
606 else if (ud>=9)
607 *gmode = retail;
608 else if (rg>=18)
609 *gmode = registered;
610 else if (sw>=9)
611 *gmode = shareware;
613 else // error from access call
614 I_Error("CheckIWAD: IWAD %s not readable", iwadname);
616 #endif
617 void D_DoomMainSetup(void)
619 int p;
621 nomonsters = M_CheckParm ("-nomonsters");
622 respawnparm = M_CheckParm ("-respawn");
623 fastparm = M_CheckParm ("-fast");
624 devparm = M_CheckParm ("-devparm");
625 if (M_CheckParm ("-altdeath"))
626 deathmatch = 2;
627 else if (M_CheckParm ("-deathmatch"))
628 deathmatch = 1;
630 printf("Welcome to Rockdoom\n");
632 switch ( gamemode )
634 case retail:
635 printf ("The Ultimate DOOM Startup v%d.%d\n",DVERSION/100,DVERSION%100);
636 break;
637 case shareware:
638 printf ("DOOM Shareware Startup v%d.%d\n",DVERSION/100,DVERSION%100);
639 break;
640 case registered:
641 printf ("DOOM Registered Startup v%d.%d\n",DVERSION/100,DVERSION%100);
642 break;
643 case commercial:
644 switch (gamemission)
646 case pack_plut:
647 printf ("DOOM 2: Plutonia Experiment v%d.%d\n",DVERSION/100,DVERSION%100);
648 break;
649 case pack_tnt:
650 printf ("DOOM 2: TNT - Evilution v%d.%d\n",DVERSION/100,DVERSION%100);
651 break;
652 default:
653 printf ("DOOM 2: Hell on Earth v%d.%d\n",DVERSION/100,DVERSION%100);
654 break;
656 break;
657 default:
658 printf ("Public DOOM v%d.%d\n",DVERSION/100,DVERSION%100);
659 break;
662 if (devparm)
663 printf(D_DEVSTR);
665 // turbo option
666 if ((p=M_CheckParm ("-turbo")))
668 int scale = 200;
669 extern int forwardmove[2];
670 extern int sidemove[2];
672 if (p<myargc-1)
673 scale = atoi (myargv[p+1]);
674 if (scale < 10)
675 scale = 10;
676 if (scale > 400)
677 scale = 400;
678 printf ("turbo scale: %d%%\n",scale);
679 forwardmove[0] = forwardmove[0]*scale/100;
680 forwardmove[1] = forwardmove[1]*scale/100;
681 sidemove[0] = sidemove[0]*scale/100;
682 sidemove[1] = sidemove[1]*scale/100;
685 // get skill / episode / map from parms
686 startskill = sk_medium;
687 startepisode = 1;
688 startmap = 1;
689 autostart = false;
691 p = M_CheckParm ("-skill");
692 if (p && p < myargc-1)
694 startskill = myargv[p+1][0]-'1';
695 autostart = true;
698 p = M_CheckParm ("-episode");
699 if (p && p < myargc-1)
701 startepisode = myargv[p+1][0]-'0';
702 startmap = 1;
703 autostart = true;
706 p = M_CheckParm ("-warp");
707 if (p && p < myargc-1)
709 if (gamemode == commercial)
710 startmap = atoi (myargv[p+1]);
711 else
713 startepisode = myargv[p+1][0]-'0';
714 startmap = myargv[p+2][0]-'0';
716 autostart = true;
719 // CPhipps - move up netgame init
720 printf("D_InitNetGame: Checking for network game.\n");
721 D_InitNetGame();
723 // init subsystems
724 printf ("V_Init: allocate screens.\n");
725 V_Init ();
727 printf ("W_Init: Init WADfiles.\n");
728 W_Init();
730 if ((p = W_CheckNumForName("DEHACKED")) != -1) // cph - add dehacked-in-a-wad support
731 ProcessDehFile(NULL, dehout ? "/dehlog.txt" : NULL, p);
733 V_InitColorTranslation(); //jff 4/24/98 load color translation lumps
735 // Check for -file in shareware
736 if (modifiedgame)
738 // These are the lumps that will be checked in IWAD,
739 // if any one is not present, execution will be aborted.
740 const char name[23][8]=
742 "e2m1","e2m2","e2m3","e2m4","e2m5","e2m6","e2m7","e2m8","e2m9",
743 "e3m1","e3m3","e3m3","e3m4","e3m5","e3m6","e3m7","e3m8","e3m9",
744 "dphoof","bfgga0","heada1","cybra1","spida1d1"
746 int i;
748 if ( gamemode == shareware)
749 I_Error("\nYou cannot -file with the shareware version. Register!\n");
751 // Check for fake IWAD with right name,
752 // but w/o all the lumps of the registered version.
753 if (gamemode == registered)
754 for (i = 0;i < 23; i++)
755 if (W_CheckNumForName(name[i])<0)
756 I_Error("This is not the registered version.\n");
759 // Iff additonal PWAD files are used, print modified banner
760 if (modifiedgame)
761 printf ("ATTENTION: This version of DOOM has been modified.\n");
763 // Check and print which version is executed.
764 switch ( gamemode )
766 case shareware:
767 case indetermined:
768 printf ("Shareware!\n");
769 break;
770 case registered:
771 case retail:
772 case commercial:
773 printf ("Commercial product - do not distribute!\n");
774 break;
775 default:
776 // Ouch.
777 break;
780 printf ("M_Init: Init miscellaneous info.\n");
781 M_Init ();
783 printf ("R_Init: Init DOOM refresh daemon - ");
784 R_Init ();
786 printf ("P_Init: Init Playloop state.\n");
787 P_Init ();
789 printf ("I_Init: Setting up machine state.\n");
790 I_Init ();
792 printf ("S_Init: Setting up sound.\n");
793 S_Init (snd_SfxVolume /* *8 */, snd_MusicVolume /* *8*/ );
795 printf ("HU_Init: Setting up heads up display.\n");
796 HU_Init ();
798 I_InitGraphics ();
800 printf ("ST_Init: Init status bar.\n");
801 ST_Init ();
803 // check for a driver that wants intermission stats
804 p = M_CheckParm ("-statcopy");
805 if (p && p<myargc-1)
807 // for statistics driver
808 extern void* statcopy;
810 statcopy = (void*)(long)atoi(myargv[p+1]);
811 printf ("External statistics registered.\n");
814 // start the apropriate game based on parms
815 p = M_CheckParm ("-record");
816 if (p && p < myargc-1)
818 G_RecordDemo (myargv[p+1]);
819 autostart = true;
822 p = M_CheckParm ("-loadgame");
823 if (p && p < myargc-1)
824 G_LoadGame (atoi(myargv[p+1]), false);
826 if ( gameaction != ga_loadgame )
828 if (!singledemo) { /* killough 12/98 */
829 if (autostart || netgame)
830 G_InitNew (startskill, startepisode, startmap);
831 else
832 D_StartTitle (); // start up intro loop
838 // D_DoomMain
840 void D_DoomMain (void)
842 D_DoomMainSetup(); // get this crap off the stack
844 D_DoomLoop (); // never returns