Subversion Repositories Kolibri OS

Rev

Rev 8557 | Rev 8655 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. // WL_MAIN.C
  2.  
  3. #ifdef _WIN32
  4.     #include <io.h>
  5. #else
  6.     #include <unistd.h>
  7. #endif
  8.  
  9. #include "wl_def.h"
  10. #pragma hdrstop
  11. #include "wl_atmos.h"
  12. #include <SDL_syswm.h>
  13.  
  14. /*
  15. =============================================================================
  16.  
  17.                              WOLFENSTEIN 3-D
  18.  
  19.                         An Id Software production
  20.  
  21.                              by John Carmack
  22.  
  23. =============================================================================
  24. */
  25.  
  26. extern byte signon[];
  27. extern void kolibri_set_win_center();
  28. /*
  29. =============================================================================
  30.  
  31.                              LOCAL CONSTANTS
  32.  
  33. =============================================================================
  34. */
  35.  
  36.  
  37. #define FOCALLENGTH     (0x5700l)               // in global coordinates
  38. #define VIEWGLOBAL      0x10000                 // globals visable flush to wall
  39.  
  40. #define VIEWWIDTH       256                     // size of view window
  41. #define VIEWHEIGHT      144
  42.  
  43. /*
  44. =============================================================================
  45.  
  46.                             GLOBAL VARIABLES
  47.  
  48. =============================================================================
  49. */
  50.  
  51. char    str[80];
  52. int     dirangle[9] = {0,ANGLES/8,2*ANGLES/8,3*ANGLES/8,4*ANGLES/8,
  53.                        5*ANGLES/8,6*ANGLES/8,7*ANGLES/8,ANGLES};
  54.  
  55. //
  56. // proejection variables
  57. //
  58. fixed    focallength;
  59. unsigned screenofs;
  60. int      viewscreenx, viewscreeny;
  61. int      viewwidth;
  62. int      viewheight;
  63. short    centerx;
  64. int      shootdelta;           // pixels away from centerx a target can be
  65. fixed    scale;
  66. int32_t  heightnumerator;
  67.  
  68.  
  69. void    Quit (const char *error,...);
  70.  
  71. boolean startgame;
  72. boolean loadedgame;
  73. int     mouseadjustment;
  74.  
  75. char    configdir[256] = "";
  76. char    configname[13] = "config.";
  77.  
  78. //
  79. // Command line parameter variables
  80. //
  81. boolean param_debugmode = false;
  82. boolean param_nowait = false;
  83. int     param_difficulty = 1;           // default is "normal"
  84. int     param_tedlevel = -1;            // default is not to start a level
  85. int     param_joystickindex = 0;
  86.  
  87. #if defined(_arch_dreamcast)
  88. int     param_joystickhat = 0;
  89. int     param_samplerate = 11025;       // higher samplerates result in "out of memory"
  90. int     param_audiobuffer = 4096 / (44100 / param_samplerate);
  91. #elif defined(GP2X_940)
  92. int     param_joystickhat = -1;
  93. int     param_samplerate = 11025;       // higher samplerates result in "out of memory"
  94. int     param_audiobuffer = 128;
  95. #else
  96. int     param_joystickhat = -1;
  97. int     param_samplerate = 44100;
  98. int     param_audiobuffer = 2048 / (44100 / param_samplerate);
  99. #endif
  100.  
  101. int     param_mission = 0;
  102. boolean param_goodtimes = false;
  103. boolean param_ignorenumchunks = false;
  104.  
  105. /*
  106. =============================================================================
  107.  
  108.                             LOCAL VARIABLES
  109.  
  110. =============================================================================
  111. */
  112.  
  113.  
  114. /*
  115. ====================
  116. =
  117. = ReadConfig
  118. =
  119. ====================
  120. */
  121.  
  122. void ReadConfig(void)
  123. {
  124.     SDMode  sd;
  125.     SMMode  sm;
  126.     SDSMode sds;
  127.  
  128.     char configpath[300];
  129.  
  130. #ifdef _arch_dreamcast
  131.     DC_LoadFromVMU(configname);
  132. #endif
  133.  
  134.     if(configdir[0])
  135.         snprintf(configpath, sizeof(configpath), "%s/%s", configdir, configname);
  136.     else
  137.         strcpy(configpath, configname);
  138.  
  139.     const int file = open(configpath, O_RDONLY | O_BINARY);
  140.     if (file != -1)
  141.     {
  142.         //
  143.         // valid config file
  144.         //
  145.         word tmp;
  146.         read(file,&tmp,sizeof(tmp));
  147.         if(tmp!=0xfefa)
  148.         {
  149.             close(file);
  150.             goto noconfig;
  151.         }
  152.         read(file,Scores,sizeof(HighScore) * MaxScores);
  153.  
  154.         read(file,&sd,sizeof(sd));
  155.         read(file,&sm,sizeof(sm));
  156.         read(file,&sds,sizeof(sds));
  157.  
  158.         read(file,&mouseenabled,sizeof(mouseenabled));
  159.         read(file,&joystickenabled,sizeof(joystickenabled));
  160.         boolean dummyJoypadEnabled;
  161.         read(file,&dummyJoypadEnabled,sizeof(dummyJoypadEnabled));
  162.         boolean dummyJoystickProgressive;
  163.         read(file,&dummyJoystickProgressive,sizeof(dummyJoystickProgressive));
  164.         int dummyJoystickPort = 0;
  165.         read(file,&dummyJoystickPort,sizeof(dummyJoystickPort));
  166.  
  167.         read(file,dirscan,sizeof(dirscan));
  168.         read(file,buttonscan,sizeof(buttonscan));
  169.         read(file,buttonmouse,sizeof(buttonmouse));
  170.         read(file,buttonjoy,sizeof(buttonjoy));
  171.  
  172.         read(file,&viewsize,sizeof(viewsize));
  173.         read(file,&mouseadjustment,sizeof(mouseadjustment));
  174.  
  175.         close(file);
  176.  
  177.         if ((sd == sdm_AdLib || sm == smm_AdLib) && !AdLibPresent
  178.                 && !SoundBlasterPresent)
  179.         {
  180.             sd = sdm_PC;
  181.             sm = smm_Off;
  182.         }
  183.  
  184.         if ((sds == sds_SoundBlaster && !SoundBlasterPresent))
  185.             sds = sds_Off;
  186.  
  187.         // make sure values are correct
  188.  
  189.         if(mouseenabled) mouseenabled=true;
  190.         if(joystickenabled) joystickenabled=true;
  191.  
  192.         if (!MousePresent)
  193.             mouseenabled = false;
  194.         if (!IN_JoyPresent())
  195.             joystickenabled = false;
  196.  
  197.         if(mouseadjustment<0) mouseadjustment=0;
  198.         else if(mouseadjustment>9) mouseadjustment=9;
  199.  
  200.         if(viewsize<4) viewsize=4;
  201.         else if(viewsize>21) viewsize=21;
  202.  
  203.         MainMenu[6].active=1;
  204.         MainItems.curpos=0;
  205.     }
  206.     else
  207.     {
  208.         //
  209.         // no config file, so select by hardware
  210.         //
  211. noconfig:
  212.         if (SoundBlasterPresent || AdLibPresent)
  213.         {
  214.             sd = sdm_AdLib;
  215.             sm = smm_AdLib;
  216.         }
  217.         else
  218.         {
  219.             sd = sdm_PC;
  220.             sm = smm_Off;
  221.         }
  222.  
  223.         if (SoundBlasterPresent)
  224.             sds = sds_SoundBlaster;
  225.         else
  226.             sds = sds_Off;
  227.  
  228.         if (MousePresent)
  229.             mouseenabled = true;
  230.  
  231.         if (IN_JoyPresent())
  232.             joystickenabled = true;
  233.  
  234.         viewsize = 19;                          // start with a good size
  235.         mouseadjustment=5;
  236.     }
  237.  
  238.     SD_SetMusicMode (sm);
  239.     SD_SetSoundMode (sd);
  240.     SD_SetDigiDevice (sds);
  241. }
  242.  
  243. /*
  244. ====================
  245. =
  246. = WriteConfig
  247. =
  248. ====================
  249. */
  250.  
  251. void WriteConfig(void)
  252. {
  253.     char configpath[300];
  254.  
  255. #ifdef _arch_dreamcast
  256.     fs_unlink(configname);
  257. #endif
  258.  
  259.     if(configdir[0])
  260.         snprintf(configpath, sizeof(configpath), "%s/%s", configdir, configname);
  261.     else
  262.         strcpy(configpath, configname);
  263.  
  264.     const int file = open(configpath, O_CREAT | O_WRONLY | O_BINARY, 0644);
  265.     if (file != -1)
  266.     {
  267.         word tmp=0xfefa;
  268.         write(file,&tmp,sizeof(tmp));
  269.         write(file,Scores,sizeof(HighScore) * MaxScores);
  270.  
  271.         write(file,&SoundMode,sizeof(SoundMode));
  272.         write(file,&MusicMode,sizeof(MusicMode));
  273.         write(file,&DigiMode,sizeof(DigiMode));
  274.  
  275.         write(file,&mouseenabled,sizeof(mouseenabled));
  276.         write(file,&joystickenabled,sizeof(joystickenabled));
  277.         boolean dummyJoypadEnabled = false;
  278.         write(file,&dummyJoypadEnabled,sizeof(dummyJoypadEnabled));
  279.         boolean dummyJoystickProgressive = false;
  280.         write(file,&dummyJoystickProgressive,sizeof(dummyJoystickProgressive));
  281.         int dummyJoystickPort = 0;
  282.         write(file,&dummyJoystickPort,sizeof(dummyJoystickPort));
  283.  
  284.         write(file,dirscan,sizeof(dirscan));
  285.         write(file,buttonscan,sizeof(buttonscan));
  286.         write(file,buttonmouse,sizeof(buttonmouse));
  287.         write(file,buttonjoy,sizeof(buttonjoy));
  288.  
  289.         write(file,&viewsize,sizeof(viewsize));
  290.         write(file,&mouseadjustment,sizeof(mouseadjustment));
  291.  
  292.         close(file);
  293.     }
  294. #ifdef _arch_dreamcast
  295.     DC_SaveToVMU(configname, NULL);
  296. #endif
  297. }
  298.  
  299.  
  300. //===========================================================================
  301.  
  302. /*
  303. =====================
  304. =
  305. = NewGame
  306. =
  307. = Set up new game to start from the beginning
  308. =
  309. =====================
  310. */
  311.  
  312. void NewGame (int difficulty,int episode)
  313. {
  314.     memset (&gamestate,0,sizeof(gamestate));
  315.     gamestate.difficulty = difficulty;
  316.     gamestate.weapon = gamestate.bestweapon
  317.             = gamestate.chosenweapon = wp_pistol;
  318.     gamestate.health = 100;
  319.     gamestate.ammo = STARTAMMO;
  320.     gamestate.lives = 3;
  321.     gamestate.nextextra = EXTRAPOINTS;
  322.     gamestate.episode=episode;
  323.  
  324.     startgame = true;
  325. }
  326.  
  327. //===========================================================================
  328.  
  329. void DiskFlopAnim(int x,int y)
  330. {
  331.     static int8_t which=0;
  332.     if (!x && !y)
  333.         return;
  334.     VWB_DrawPic(x,y,C_DISKLOADING1PIC+which);
  335.     VW_UpdateScreen();
  336.     which^=1;
  337. }
  338.  
  339.  
  340. int32_t DoChecksum(byte *source,unsigned size,int32_t checksum)
  341. {
  342.     unsigned i;
  343.  
  344.     for (i=0;i<size-1;i++)
  345.     checksum += source[i]^source[i+1];
  346.  
  347.     return checksum;
  348. }
  349.  
  350.  
  351. /*
  352. ==================
  353. =
  354. = SaveTheGame
  355. =
  356. ==================
  357. */
  358.  
  359. extern statetype s_grdstand;
  360. extern statetype s_player;
  361.  
  362. boolean SaveTheGame(FILE *file,int x,int y)
  363. {
  364. //    struct diskfree_t dfree;
  365. //    int32_t avail,size,checksum;
  366.     int checksum;
  367.     objtype *ob;
  368.     objtype nullobj;
  369.     statobj_t nullstat;
  370.  
  371. /*    if (_dos_getdiskfree(0,&dfree))
  372.         Quit("Error in _dos_getdiskfree call");
  373.  
  374.     avail = (int32_t)dfree.avail_clusters *
  375.                   dfree.bytes_per_sector *
  376.                   dfree.sectors_per_cluster;
  377.  
  378.     size = 0;
  379.     for (ob = player; ob ; ob=ob->next)
  380.         size += sizeof(*ob);
  381.     size += sizeof(nullobj);
  382.  
  383.     size += sizeof(gamestate) +
  384.             sizeof(LRstruct)*LRpack +
  385.             sizeof(tilemap) +
  386.             sizeof(actorat) +
  387.             sizeof(laststatobj) +
  388.             sizeof(statobjlist) +
  389.             sizeof(doorposition) +
  390.             sizeof(pwallstate) +
  391.             sizeof(pwalltile) +
  392.             sizeof(pwallx) +
  393.             sizeof(pwally) +
  394.             sizeof(pwalldir) +
  395.             sizeof(pwallpos);
  396.  
  397.     if (avail < size)
  398.     {
  399.         Message(STR_NOSPACE1"\n"STR_NOSPACE2);
  400.         return false;
  401.     }*/
  402.  
  403.     checksum = 0;
  404.  
  405.     DiskFlopAnim(x,y);
  406.     fwrite(&gamestate,sizeof(gamestate),1,file);
  407.     checksum = DoChecksum((byte *)&gamestate,sizeof(gamestate),checksum);
  408.  
  409.     DiskFlopAnim(x,y);
  410.     fwrite(&LevelRatios[0],sizeof(LRstruct)*LRpack,1,file);
  411.     checksum = DoChecksum((byte *)&LevelRatios[0],sizeof(LRstruct)*LRpack,checksum);
  412.  
  413.     DiskFlopAnim(x,y);
  414.     fwrite(tilemap,sizeof(tilemap),1,file);
  415.     checksum = DoChecksum((byte *)tilemap,sizeof(tilemap),checksum);
  416.     DiskFlopAnim(x,y);
  417.  
  418.     int i;
  419.     for(i=0;i<MAPSIZE;i++)
  420.     {
  421.         for(int j=0;j<MAPSIZE;j++)
  422.         {
  423.             word actnum;
  424.             objtype *objptr=actorat[i][j];
  425.             if(ISPOINTER(objptr))
  426.                 actnum=0x8000 | (word)(objptr-objlist);
  427.             else
  428.                 actnum=(word)(uintptr_t)objptr;
  429.             fwrite(&actnum,sizeof(actnum),1,file);
  430.             checksum = DoChecksum((byte *)&actnum,sizeof(actnum),checksum);
  431.         }
  432.     }
  433.  
  434.     fwrite (areaconnect,sizeof(areaconnect),1,file);
  435.     fwrite (areabyplayer,sizeof(areabyplayer),1,file);
  436.  
  437.     // player object needs special treatment as it's in WL_AGENT.CPP and not in
  438.     // WL_ACT2.CPP which could cause problems for the relative addressing
  439.  
  440.     ob = player;
  441.     DiskFlopAnim(x,y);
  442.     memcpy(&nullobj,ob,sizeof(nullobj));
  443.     nullobj.state=(statetype *) ((uintptr_t)nullobj.state-(uintptr_t)&s_player);
  444.     fwrite(&nullobj,sizeof(nullobj),1,file);
  445.     ob = ob->next;
  446.  
  447.     DiskFlopAnim(x,y);
  448.     for (; ob ; ob=ob->next)
  449.     {
  450.         memcpy(&nullobj,ob,sizeof(nullobj));
  451.         nullobj.state=(statetype *) ((uintptr_t)nullobj.state-(uintptr_t)&s_grdstand);
  452.         fwrite(&nullobj,sizeof(nullobj),1,file);
  453.     }
  454.     nullobj.active = ac_badobject;          // end of file marker
  455.     DiskFlopAnim(x,y);
  456.     fwrite(&nullobj,sizeof(nullobj),1,file);
  457.  
  458.     DiskFlopAnim(x,y);
  459.     word laststatobjnum=(word) (laststatobj-statobjlist);
  460.     fwrite(&laststatobjnum,sizeof(laststatobjnum),1,file);
  461.     checksum = DoChecksum((byte *)&laststatobjnum,sizeof(laststatobjnum),checksum);
  462.  
  463.     DiskFlopAnim(x,y);
  464.     for(i=0;i<MAXSTATS;i++)
  465.     {
  466.         memcpy(&nullstat,statobjlist+i,sizeof(nullstat));
  467.         nullstat.visspot=(byte *) ((uintptr_t) nullstat.visspot-(uintptr_t)spotvis);
  468.         fwrite(&nullstat,sizeof(nullstat),1,file);
  469.         checksum = DoChecksum((byte *)&nullstat,sizeof(nullstat),checksum);
  470.     }
  471.  
  472.     DiskFlopAnim(x,y);
  473.     fwrite (doorposition,sizeof(doorposition),1,file);
  474.     checksum = DoChecksum((byte *)doorposition,sizeof(doorposition),checksum);
  475.     DiskFlopAnim(x,y);
  476.     fwrite (doorobjlist,sizeof(doorobjlist),1,file);
  477.     checksum = DoChecksum((byte *)doorobjlist,sizeof(doorobjlist),checksum);
  478.  
  479.     DiskFlopAnim(x,y);
  480.     fwrite (&pwallstate,sizeof(pwallstate),1,file);
  481.     checksum = DoChecksum((byte *)&pwallstate,sizeof(pwallstate),checksum);
  482.     fwrite (&pwalltile,sizeof(pwalltile),1,file);
  483.     checksum = DoChecksum((byte *)&pwalltile,sizeof(pwalltile),checksum);
  484.     fwrite (&pwallx,sizeof(pwallx),1,file);
  485.     checksum = DoChecksum((byte *)&pwallx,sizeof(pwallx),checksum);
  486.     fwrite (&pwally,sizeof(pwally),1,file);
  487.     checksum = DoChecksum((byte *)&pwally,sizeof(pwally),checksum);
  488.     fwrite (&pwalldir,sizeof(pwalldir),1,file);
  489.     checksum = DoChecksum((byte *)&pwalldir,sizeof(pwalldir),checksum);
  490.     fwrite (&pwallpos,sizeof(pwallpos),1,file);
  491.     checksum = DoChecksum((byte *)&pwallpos,sizeof(pwallpos),checksum);
  492.  
  493.     //
  494.     // WRITE OUT CHECKSUM
  495.     //
  496.     fwrite (&checksum,sizeof(checksum),1,file);
  497.  
  498.     fwrite (&lastgamemusicoffset,sizeof(lastgamemusicoffset),1,file);
  499.  
  500.     return(true);
  501. }
  502.  
  503. //===========================================================================
  504.  
  505. /*
  506. ==================
  507. =
  508. = LoadTheGame
  509. =
  510. ==================
  511. */
  512.  
  513. boolean LoadTheGame(FILE *file,int x,int y)
  514. {
  515.     int32_t checksum,oldchecksum;
  516.     objtype nullobj;
  517.     statobj_t nullstat;
  518.  
  519.     checksum = 0;
  520.  
  521.     DiskFlopAnim(x,y);
  522.     fread (&gamestate,sizeof(gamestate),1,file);
  523.     checksum = DoChecksum((byte *)&gamestate,sizeof(gamestate),checksum);
  524.  
  525.     DiskFlopAnim(x,y);
  526.     fread (&LevelRatios[0],sizeof(LRstruct)*LRpack,1,file);
  527.     checksum = DoChecksum((byte *)&LevelRatios[0],sizeof(LRstruct)*LRpack,checksum);
  528.  
  529.     DiskFlopAnim(x,y);
  530.     SetupGameLevel ();
  531.  
  532.     DiskFlopAnim(x,y);
  533.     fread (tilemap,sizeof(tilemap),1,file);
  534.     checksum = DoChecksum((byte *)tilemap,sizeof(tilemap),checksum);
  535.  
  536.     DiskFlopAnim(x,y);
  537.  
  538.     int actnum=0, i;
  539.     for(i=0;i<MAPSIZE;i++)
  540.     {
  541.         for(int j=0;j<MAPSIZE;j++)
  542.         {
  543.             fread (&actnum,sizeof(word),1,file);
  544.             checksum = DoChecksum((byte *) &actnum,sizeof(word),checksum);
  545.             if(actnum&0x8000)
  546.                 actorat[i][j]=objlist+(actnum&0x7fff);
  547.             else
  548.                 actorat[i][j]=(objtype *)(uintptr_t) actnum;
  549.         }
  550.     }
  551.  
  552.     fread (areaconnect,sizeof(areaconnect),1,file);
  553.     fread (areabyplayer,sizeof(areabyplayer),1,file);
  554.  
  555.     InitActorList ();
  556.     DiskFlopAnim(x,y);
  557.     fread (player,sizeof(*player),1,file);
  558.     player->state=(statetype *) ((uintptr_t)player->state+(uintptr_t)&s_player);
  559.  
  560.     while (1)
  561.     {
  562.         DiskFlopAnim(x,y);
  563.         fread (&nullobj,sizeof(nullobj),1,file);
  564.         if (nullobj.active == ac_badobject)
  565.             break;
  566.         GetNewActor ();
  567.         nullobj.state=(statetype *) ((uintptr_t)nullobj.state+(uintptr_t)&s_grdstand);
  568.         // don't copy over the links
  569.         memcpy (newobj,&nullobj,sizeof(nullobj)-8);
  570.     }
  571.  
  572.     DiskFlopAnim(x,y);
  573.     word laststatobjnum;
  574.     fread (&laststatobjnum,sizeof(laststatobjnum),1,file);
  575.     laststatobj=statobjlist+laststatobjnum;
  576.     checksum = DoChecksum((byte *)&laststatobjnum,sizeof(laststatobjnum),checksum);
  577.  
  578.     DiskFlopAnim(x,y);
  579.     for(i=0;i<MAXSTATS;i++)
  580.     {
  581.         fread(&nullstat,sizeof(nullstat),1,file);
  582.         checksum = DoChecksum((byte *)&nullstat,sizeof(nullstat),checksum);
  583.         nullstat.visspot=(byte *) ((uintptr_t)nullstat.visspot+(uintptr_t)spotvis);
  584.         memcpy(statobjlist+i,&nullstat,sizeof(nullstat));
  585.     }
  586.  
  587.     DiskFlopAnim(x,y);
  588.     fread (doorposition,sizeof(doorposition),1,file);
  589.     checksum = DoChecksum((byte *)doorposition,sizeof(doorposition),checksum);
  590.     DiskFlopAnim(x,y);
  591.     fread (doorobjlist,sizeof(doorobjlist),1,file);
  592.     checksum = DoChecksum((byte *)doorobjlist,sizeof(doorobjlist),checksum);
  593.  
  594.     DiskFlopAnim(x,y);
  595.     fread (&pwallstate,sizeof(pwallstate),1,file);
  596.     checksum = DoChecksum((byte *)&pwallstate,sizeof(pwallstate),checksum);
  597.     fread (&pwalltile,sizeof(pwalltile),1,file);
  598.     checksum = DoChecksum((byte *)&pwalltile,sizeof(pwalltile),checksum);
  599.     fread (&pwallx,sizeof(pwallx),1,file);
  600.     checksum = DoChecksum((byte *)&pwallx,sizeof(pwallx),checksum);
  601.     fread (&pwally,sizeof(pwally),1,file);
  602.     checksum = DoChecksum((byte *)&pwally,sizeof(pwally),checksum);
  603.     fread (&pwalldir,sizeof(pwalldir),1,file);
  604.     checksum = DoChecksum((byte *)&pwalldir,sizeof(pwalldir),checksum);
  605.     fread (&pwallpos,sizeof(pwallpos),1,file);
  606.     checksum = DoChecksum((byte *)&pwallpos,sizeof(pwallpos),checksum);
  607.  
  608.     if (gamestate.secretcount)      // assign valid floorcodes under moved pushwalls
  609.     {
  610.         word *map, *obj; word tile, sprite;
  611.         map = mapsegs[0]; obj = mapsegs[1];
  612.         for (y=0;y<mapheight;y++)
  613.             for (x=0;x<mapwidth;x++)
  614.             {
  615.                 tile = *map++; sprite = *obj++;
  616.                 if (sprite == PUSHABLETILE && !tilemap[x][y]
  617.                     && (tile < AREATILE || tile >= (AREATILE+NUMMAPS)))
  618.                 {
  619.                     if (*map >= AREATILE)
  620.                         tile = *map;
  621.                     if (*(map-1-mapwidth) >= AREATILE)
  622.                         tile = *(map-1-mapwidth);
  623.                     if (*(map-1+mapwidth) >= AREATILE)
  624.                         tile = *(map-1+mapwidth);
  625.                     if ( *(map-2) >= AREATILE)
  626.                         tile = *(map-2);
  627.  
  628.                     *(map-1) = tile; *(obj-1) = 0;
  629.                 }
  630.             }
  631.     }
  632.  
  633.     Thrust(0,0);    // set player->areanumber to the floortile you're standing on
  634.  
  635.     fread (&oldchecksum,sizeof(oldchecksum),1,file);
  636.  
  637.     fread (&lastgamemusicoffset,sizeof(lastgamemusicoffset),1,file);
  638.     if(lastgamemusicoffset<0) lastgamemusicoffset=0;
  639.  
  640.  
  641.     if (oldchecksum != checksum)
  642.     {
  643.         Message(STR_SAVECHT1"\n"
  644.                 STR_SAVECHT2"\n"
  645.                 STR_SAVECHT3"\n"
  646.                 STR_SAVECHT4);
  647.  
  648.         IN_ClearKeysDown();
  649.         IN_Ack();
  650.  
  651.         gamestate.oldscore = gamestate.score = 0;
  652.         gamestate.lives = 1;
  653.         gamestate.weapon =
  654.             gamestate.chosenweapon =
  655.             gamestate.bestweapon = wp_pistol;
  656.         gamestate.ammo = 8;
  657.     }
  658.  
  659.     return true;
  660. }
  661.  
  662. //===========================================================================
  663.  
  664. /*
  665. ==========================
  666. =
  667. = ShutdownId
  668. =
  669. = Shuts down all ID_?? managers
  670. =
  671. ==========================
  672. */
  673.  
  674. void ShutdownId (void)
  675. {
  676.     US_Shutdown ();         // This line is completely useless...
  677.     SD_Shutdown ();
  678.     PM_Shutdown ();
  679.     IN_Shutdown ();
  680.     VW_Shutdown ();
  681.     CA_Shutdown ();
  682. #if defined(GP2X_940)
  683.     GP2X_Shutdown();
  684. #endif
  685. }
  686.  
  687.  
  688. //===========================================================================
  689.  
  690. /*
  691. ==================
  692. =
  693. = BuildTables
  694. =
  695. = Calculates:
  696. =
  697. = scale                 projection constant
  698. = sintable/costable     overlapping fractional tables
  699. =
  700. ==================
  701. */
  702.  
  703. const float radtoint = (float)(FINEANGLES/2/PI);
  704.  
  705. void BuildTables (void)
  706. {
  707.     //
  708.     // calculate fine tangents
  709.     //
  710.  
  711.     int i;
  712.     for(i=0;i<FINEANGLES/8;i++)
  713.     {
  714.         double tang=tan((i+0.5)/radtoint);
  715.         finetangent[i]=(int32_t)(tang*GLOBAL1);
  716.         finetangent[FINEANGLES/4-1-i]=(int32_t)((1/tang)*GLOBAL1);
  717.     }
  718.  
  719.     //
  720.     // costable overlays sintable with a quarter phase shift
  721.     // ANGLES is assumed to be divisable by four
  722.     //
  723.  
  724.     float angle=0;
  725.     float anglestep=(float)(PI/2/ANGLEQUAD);
  726.     for(i=0; i<ANGLEQUAD; i++)
  727.     {
  728.         fixed value=(int32_t)(GLOBAL1*sin(angle));
  729.         sintable[i]=sintable[i+ANGLES]=sintable[ANGLES/2-i]=value;
  730.         sintable[ANGLES-i]=sintable[ANGLES/2+i]=-value;
  731.         angle+=anglestep;
  732.     }
  733.     sintable[ANGLEQUAD] = 65536;
  734.     sintable[3*ANGLEQUAD] = -65536;
  735.  
  736. #if defined(USE_STARSKY) || defined(USE_RAIN) || defined(USE_SNOW)
  737.     Init3DPoints();
  738. #endif
  739. }
  740.  
  741. //===========================================================================
  742.  
  743.  
  744. /*
  745. ====================
  746. =
  747. = CalcProjection
  748. =
  749. = Uses focallength
  750. =
  751. ====================
  752. */
  753.  
  754. void CalcProjection (int32_t focal)
  755. {
  756.     int     i;
  757.     int    intang;
  758.     float   angle;
  759.     double  tang;
  760.     int     halfview;
  761.     double  facedist;
  762.  
  763.     focallength = focal;
  764.     facedist = focal+MINDIST;
  765.     halfview = viewwidth/2;                                 // half view in pixels
  766.  
  767.     //
  768.     // calculate scale value for vertical height calculations
  769.     // and sprite x calculations
  770.     //
  771.     scale = (fixed) (halfview*facedist/(VIEWGLOBAL/2));
  772.  
  773.     //
  774.     // divide heightnumerator by a posts distance to get the posts height for
  775.     // the heightbuffer.  The pixel height is height>>2
  776.     //
  777.     heightnumerator = (TILEGLOBAL*scale)>>6;
  778.  
  779.     //
  780.     // calculate the angle offset from view angle of each pixel's ray
  781.     //
  782.  
  783.     for (i=0;i<halfview;i++)
  784.     {
  785.         // start 1/2 pixel over, so viewangle bisects two middle pixels
  786.         tang = (int32_t)i*VIEWGLOBAL/viewwidth/facedist;
  787.         angle = (float) atan(tang);
  788.         intang = (int) (angle*radtoint);
  789.         pixelangle[halfview-1-i] = intang;
  790.         pixelangle[halfview+i] = -intang;
  791.     }
  792. }
  793.  
  794.  
  795.  
  796. //===========================================================================
  797.  
  798. /*
  799. ===================
  800. =
  801. = SetupWalls
  802. =
  803. = Map tile values to scaled pics
  804. =
  805. ===================
  806. */
  807.  
  808. void SetupWalls (void)
  809. {
  810.     int     i;
  811.  
  812.     horizwall[0]=0;
  813.     vertwall[0]=0;
  814.  
  815.     for (i=1;i<MAXWALLTILES;i++)
  816.     {
  817.         horizwall[i]=(i-1)*2;
  818.         vertwall[i]=(i-1)*2+1;
  819.     }
  820. }
  821.  
  822. //===========================================================================
  823.  
  824. /*
  825. ==========================
  826. =
  827. = SignonScreen
  828. =
  829. ==========================
  830. */
  831.  
  832. void SignonScreen (void)                        // VGA version
  833. {
  834.     VL_SetVGAPlaneMode ();
  835.  
  836.     VL_MungePic (signon,320,200);
  837.     VL_MemToScreen (signon,320,200,0,0);
  838. }
  839.  
  840.  
  841. /*
  842. ==========================
  843. =
  844. = FinishSignon
  845. =
  846. ==========================
  847. */
  848.  
  849. void FinishSignon (void)
  850. {
  851. #ifndef SPEAR
  852.     VW_Bar (0,189,300,11,VL_GetPixel(0,0));
  853.     WindowX = 0;
  854.     WindowW = 320;
  855.     PrintY = 190;
  856.  
  857.     #ifndef JAPAN
  858.     SETFONTCOLOR(14,4);
  859.  
  860.     #ifdef SPANISH
  861.     US_CPrint ("Oprima una tecla");
  862.     #else
  863.     US_CPrint ("Press a key");
  864.     #endif
  865.  
  866.     #endif
  867.  
  868.     VH_UpdateScreen();
  869.  
  870.     if (!param_nowait)
  871.         IN_Ack ();
  872.  
  873.     #ifndef JAPAN
  874.     VW_Bar (0,189,300,11,VL_GetPixel(0,0));
  875.  
  876.     PrintY = 190;
  877.     SETFONTCOLOR(10,4);
  878.  
  879.     #ifdef SPANISH
  880.     US_CPrint ("pensando...");
  881.     #else
  882.     US_CPrint ("Working...");
  883.     #endif
  884.  
  885.     VH_UpdateScreen();
  886.     #endif
  887.  
  888.     SETFONTCOLOR(0,15);
  889. #else
  890.     VH_UpdateScreen();
  891.  
  892.     if (!param_nowait)
  893.         VW_WaitVBL(3*70);
  894. #endif
  895. }
  896.  
  897. //===========================================================================
  898.  
  899. /*
  900. =====================
  901. =
  902. = InitDigiMap
  903. =
  904. =====================
  905. */
  906.  
  907. // channel mapping:
  908. //  -1: any non reserved channel
  909. //   0: player weapons
  910. //   1: boss weapons
  911.  
  912. static int wolfdigimap[] =
  913.     {
  914.         // These first sounds are in the upload version
  915. #ifndef SPEAR
  916.         HALTSND,                0,  -1,
  917.         DOGBARKSND,             1,  -1,
  918.         CLOSEDOORSND,           2,  -1,
  919.         OPENDOORSND,            3,  -1,
  920.         ATKMACHINEGUNSND,       4,   0,
  921.         ATKPISTOLSND,           5,   0,
  922.         ATKGATLINGSND,          6,   0,
  923.         SCHUTZADSND,            7,  -1,
  924.         GUTENTAGSND,            8,  -1,
  925.         MUTTISND,               9,  -1,
  926.         BOSSFIRESND,            10,  1,
  927.         SSFIRESND,              11, -1,
  928.         DEATHSCREAM1SND,        12, -1,
  929.         DEATHSCREAM2SND,        13, -1,
  930.         DEATHSCREAM3SND,        13, -1,
  931.         TAKEDAMAGESND,          14, -1,
  932.         PUSHWALLSND,            15, -1,
  933.  
  934.         LEBENSND,               20, -1,
  935.         NAZIFIRESND,            21, -1,
  936.         SLURPIESND,             22, -1,
  937.  
  938.         YEAHSND,                32, -1,
  939.  
  940. #ifndef UPLOAD
  941.         // These are in all other episodes
  942.         DOGDEATHSND,            16, -1,
  943.         AHHHGSND,               17, -1,
  944.         DIESND,                 18, -1,
  945.         EVASND,                 19, -1,
  946.  
  947.         TOT_HUNDSND,            23, -1,
  948.         MEINGOTTSND,            24, -1,
  949.         SCHABBSHASND,           25, -1,
  950.         HITLERHASND,            26, -1,
  951.         SPIONSND,               27, -1,
  952.         NEINSOVASSND,           28, -1,
  953.         DOGATTACKSND,           29, -1,
  954.         LEVELDONESND,           30, -1,
  955.         MECHSTEPSND,            31, -1,
  956.  
  957.         SCHEISTSND,             33, -1,
  958.         DEATHSCREAM4SND,        34, -1,         // AIIEEE
  959.         DEATHSCREAM5SND,        35, -1,         // DEE-DEE
  960.         DONNERSND,              36, -1,         // EPISODE 4 BOSS DIE
  961.         EINESND,                37, -1,         // EPISODE 4 BOSS SIGHTING
  962.         ERLAUBENSND,            38, -1,         // EPISODE 6 BOSS SIGHTING
  963.         DEATHSCREAM6SND,        39, -1,         // FART
  964.         DEATHSCREAM7SND,        40, -1,         // GASP
  965.         DEATHSCREAM8SND,        41, -1,         // GUH-BOY!
  966.         DEATHSCREAM9SND,        42, -1,         // AH GEEZ!
  967.         KEINSND,                43, -1,         // EPISODE 5 BOSS SIGHTING
  968.         MEINSND,                44, -1,         // EPISODE 6 BOSS DIE
  969.         ROSESND,                45, -1,         // EPISODE 5 BOSS DIE
  970.  
  971. #endif
  972. #else
  973. //
  974. // SPEAR OF DESTINY DIGISOUNDS
  975. //
  976.         HALTSND,                0,  -1,
  977.         CLOSEDOORSND,           2,  -1,
  978.         OPENDOORSND,            3,  -1,
  979.         ATKMACHINEGUNSND,       4,   0,
  980.         ATKPISTOLSND,           5,   0,
  981.         ATKGATLINGSND,          6,   0,
  982.         SCHUTZADSND,            7,  -1,
  983.         BOSSFIRESND,            8,   1,
  984.         SSFIRESND,              9,  -1,
  985.         DEATHSCREAM1SND,        10, -1,
  986.         DEATHSCREAM2SND,        11, -1,
  987.         TAKEDAMAGESND,          12, -1,
  988.         PUSHWALLSND,            13, -1,
  989.         AHHHGSND,               15, -1,
  990.         LEBENSND,               16, -1,
  991.         NAZIFIRESND,            17, -1,
  992.         SLURPIESND,             18, -1,
  993.         LEVELDONESND,           22, -1,
  994.         DEATHSCREAM4SND,        23, -1,         // AIIEEE
  995.         DEATHSCREAM3SND,        23, -1,         // DOUBLY-MAPPED!!!
  996.         DEATHSCREAM5SND,        24, -1,         // DEE-DEE
  997.         DEATHSCREAM6SND,        25, -1,         // FART
  998.         DEATHSCREAM7SND,        26, -1,         // GASP
  999.         DEATHSCREAM8SND,        27, -1,         // GUH-BOY!
  1000.         DEATHSCREAM9SND,        28, -1,         // AH GEEZ!
  1001.         GETGATLINGSND,          38, -1,         // Got Gat replacement
  1002.  
  1003. #ifndef SPEARDEMO
  1004.         DOGBARKSND,             1,  -1,
  1005.         DOGDEATHSND,            14, -1,
  1006.         SPIONSND,               19, -1,
  1007.         NEINSOVASSND,           20, -1,
  1008.         DOGATTACKSND,           21, -1,
  1009.         TRANSSIGHTSND,          29, -1,         // Trans Sight
  1010.         TRANSDEATHSND,          30, -1,         // Trans Death
  1011.         WILHELMSIGHTSND,        31, -1,         // Wilhelm Sight
  1012.         WILHELMDEATHSND,        32, -1,         // Wilhelm Death
  1013.         UBERDEATHSND,           33, -1,         // Uber Death
  1014.         KNIGHTSIGHTSND,         34, -1,         // Death Knight Sight
  1015.         KNIGHTDEATHSND,         35, -1,         // Death Knight Death
  1016.         ANGELSIGHTSND,          36, -1,         // Angel Sight
  1017.         ANGELDEATHSND,          37, -1,         // Angel Death
  1018.         GETSPEARSND,            39, -1,         // Got Spear replacement
  1019. #endif
  1020. #endif
  1021.         LASTSOUND
  1022.     };
  1023.  
  1024.  
  1025. void InitDigiMap (void)
  1026. {
  1027.     int *map;
  1028.  
  1029.     for (map = wolfdigimap; *map != LASTSOUND; map += 3)
  1030.     {
  1031.         DigiMap[map[0]] = map[1];
  1032.         DigiChannel[map[1]] = map[2];
  1033.         SD_PrepareSound(map[1]);
  1034.     }
  1035. }
  1036.  
  1037. #ifndef SPEAR
  1038. CP_iteminfo MusicItems={CTL_X,CTL_Y,6,0,32};
  1039. CP_itemtype MusicMenu[]=
  1040.     {
  1041.         {1,"Get Them!",0},
  1042.         {1,"Searching",0},
  1043.         {1,"P.O.W.",0},
  1044.         {1,"Suspense",0},
  1045.         {1,"War March",0},
  1046.         {1,"Around The Corner!",0},
  1047.  
  1048.         {1,"Nazi Anthem",0},
  1049.         {1,"Lurking...",0},
  1050.         {1,"Going After Hitler",0},
  1051.         {1,"Pounding Headache",0},
  1052.         {1,"Into the Dungeons",0},
  1053.         {1,"Ultimate Conquest",0},
  1054.  
  1055.         {1,"Kill the S.O.B.",0},
  1056.         {1,"The Nazi Rap",0},
  1057.         {1,"Twelfth Hour",0},
  1058.         {1,"Zero Hour",0},
  1059.         {1,"Ultimate Conquest",0},
  1060.         {1,"Wolfpack",0}
  1061.     };
  1062. #else
  1063. CP_iteminfo MusicItems={CTL_X,CTL_Y-20,9,0,32};
  1064. CP_itemtype MusicMenu[]=
  1065.     {
  1066.         {1,"Funky Colonel Bill",0},
  1067.         {1,"Death To The Nazis",0},
  1068.         {1,"Tiptoeing Around",0},
  1069.         {1,"Is This THE END?",0},
  1070.         {1,"Evil Incarnate",0},
  1071.         {1,"Jazzin' Them Nazis",0},
  1072.         {1,"Puttin' It To The Enemy",0},
  1073.         {1,"The SS Gonna Get You",0},
  1074.         {1,"Towering Above",0}
  1075.     };
  1076. #endif
  1077.  
  1078. #ifndef SPEARDEMO
  1079. void DoJukebox(void)
  1080. {
  1081.     int which,lastsong=-1;
  1082.     unsigned start;
  1083.     unsigned songs[]=
  1084.         {
  1085. #ifndef SPEAR
  1086.             GETTHEM_MUS,
  1087.             SEARCHN_MUS,
  1088.             POW_MUS,
  1089.             SUSPENSE_MUS,
  1090.             WARMARCH_MUS,
  1091.             CORNER_MUS,
  1092.  
  1093.             NAZI_OMI_MUS,
  1094.             PREGNANT_MUS,
  1095.             GOINGAFT_MUS,
  1096.             HEADACHE_MUS,
  1097.             DUNGEON_MUS,
  1098.             ULTIMATE_MUS,
  1099.  
  1100.             INTROCW3_MUS,
  1101.             NAZI_RAP_MUS,
  1102.             TWELFTH_MUS,
  1103.             ZEROHOUR_MUS,
  1104.             ULTIMATE_MUS,
  1105.             PACMAN_MUS
  1106. #else
  1107.             XFUNKIE_MUS,             // 0
  1108.             XDEATH_MUS,              // 2
  1109.             XTIPTOE_MUS,             // 4
  1110.             XTHEEND_MUS,             // 7
  1111.             XEVIL_MUS,               // 17
  1112.             XJAZNAZI_MUS,            // 18
  1113.             XPUTIT_MUS,              // 21
  1114.             XGETYOU_MUS,             // 22
  1115.             XTOWER2_MUS              // 23
  1116. #endif
  1117.         };
  1118.  
  1119.     IN_ClearKeysDown();
  1120.     if (!AdLibPresent && !SoundBlasterPresent)
  1121.         return;
  1122.  
  1123.     MenuFadeOut();
  1124.  
  1125. #ifndef SPEAR
  1126. #ifndef UPLOAD
  1127.     start = ((SDL_GetTicks()/10)%3)*6;
  1128. #else
  1129.     start = 0;
  1130. #endif
  1131. #else
  1132.     start = 0;
  1133. #endif
  1134.  
  1135.     CA_CacheGrChunk (STARTFONT+1);
  1136. #ifdef SPEAR
  1137.     CacheLump (BACKDROP_LUMP_START,BACKDROP_LUMP_END);
  1138. #else
  1139.     CacheLump (CONTROLS_LUMP_START,CONTROLS_LUMP_END);
  1140. #endif
  1141.     CA_LoadAllSounds ();
  1142.  
  1143.     fontnumber=1;
  1144.     ClearMScreen ();
  1145.     VWB_DrawPic(112,184,C_MOUSELBACKPIC);
  1146.     DrawStripes (10);
  1147.     SETFONTCOLOR (TEXTCOLOR,BKGDCOLOR);
  1148.  
  1149. #ifndef SPEAR
  1150.     DrawWindow (CTL_X-2,CTL_Y-6,280,13*7,BKGDCOLOR);
  1151. #else
  1152.     DrawWindow (CTL_X-2,CTL_Y-26,280,13*10,BKGDCOLOR);
  1153. #endif
  1154.  
  1155.     DrawMenu (&MusicItems,&MusicMenu[start]);
  1156.  
  1157.     SETFONTCOLOR (READHCOLOR,BKGDCOLOR);
  1158.     PrintY=15;
  1159.     WindowX = 0;
  1160.     WindowY = 320;
  1161.     US_CPrint ("Robert's Jukebox");
  1162.  
  1163.     SETFONTCOLOR (TEXTCOLOR,BKGDCOLOR);
  1164.     VW_UpdateScreen();
  1165.     MenuFadeIn();
  1166.  
  1167.     do
  1168.     {
  1169.         which = HandleMenu(&MusicItems,&MusicMenu[start],NULL);
  1170.         if (which>=0)
  1171.         {
  1172.             if (lastsong >= 0)
  1173.                 MusicMenu[start+lastsong].active = 1;
  1174.  
  1175.             StartCPMusic(songs[start + which]);
  1176.             MusicMenu[start+which].active = 2;
  1177.             DrawMenu (&MusicItems,&MusicMenu[start]);
  1178.             VW_UpdateScreen();
  1179.             lastsong = which;
  1180.         }
  1181.     } while(which>=0);
  1182.  
  1183.     MenuFadeOut();
  1184.     IN_ClearKeysDown();
  1185. #ifdef SPEAR
  1186.     UnCacheLump (BACKDROP_LUMP_START,BACKDROP_LUMP_END);
  1187. #else
  1188.     UnCacheLump (CONTROLS_LUMP_START,CONTROLS_LUMP_END);
  1189. #endif
  1190. }
  1191. #endif
  1192.  
  1193. /*
  1194. ==========================
  1195. =
  1196. = InitGame
  1197. =
  1198. = Load a few things right away
  1199. =
  1200. ==========================
  1201. */
  1202.  
  1203. static void InitGame()
  1204. {
  1205. #ifndef SPEARDEMO
  1206.     boolean didjukebox=false;
  1207. #endif
  1208.  
  1209.     // initialize SDL
  1210. #if defined _WIN32
  1211.     putenv("SDL_VIDEODRIVER=directx");
  1212. #endif
  1213.     if(SDL_Init(SDL_INIT_VIDEO /*| SDL_INIT_AUDIO | SDL_INIT_JOYSTICK*/) < 0)
  1214.     {
  1215.         printf("Unable to init SDL: %s\n", SDL_GetError());
  1216.         exit(1);
  1217.     }
  1218.     atexit(SDL_Quit);
  1219.  
  1220.     int numJoysticks = SDL_NumJoysticks();
  1221.     if(param_joystickindex && (param_joystickindex < -1 || param_joystickindex >= numJoysticks))
  1222.     {
  1223.         if(!numJoysticks)
  1224.             printf("No joysticks are available to SDL!\n");
  1225.         else
  1226.             printf("The joystick index must be between -1 and %i!\n", numJoysticks - 1);
  1227.         exit(1);
  1228.     }
  1229.  
  1230. #if defined(GP2X_940)
  1231.     GP2X_MemoryInit();
  1232. #endif
  1233.    
  1234.     SignonScreen ();
  1235.     kolibri_set_win_center();
  1236.    
  1237. #if defined _WIN32
  1238.     if(!fullscreen)
  1239.     {
  1240.         struct SDL_SysWMinfo wmInfo;
  1241.         SDL_VERSION(&wmInfo.version);
  1242.  
  1243.         if(SDL_GetWMInfo(&wmInfo) != -1)
  1244.         {
  1245.             HWND hwndSDL = wmInfo.window;
  1246.             DWORD style = GetWindowLong(hwndSDL, GWL_STYLE) & ~WS_SYSMENU;
  1247.             SetWindowLong(hwndSDL, GWL_STYLE, style);
  1248.             SetWindowPos(hwndSDL, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
  1249.         }
  1250.     }
  1251. #endif
  1252.         VW_UpdateScreen();
  1253.  
  1254.     VH_Startup ();
  1255.     IN_Startup ();
  1256.     PM_Startup ();
  1257.     SD_Startup ();
  1258.     CA_Startup ();
  1259.     US_Startup ();
  1260.  
  1261.     // TODO: Will any memory checking be needed someday??
  1262. #ifdef NOTYET
  1263. #ifndef SPEAR
  1264.     if (mminfo.mainmem < 235000L)
  1265. #else
  1266.     if (mminfo.mainmem < 257000L && !MS_CheckParm("debugmode"))
  1267. #endif
  1268.     {
  1269.         byte *screen;
  1270.  
  1271.         CA_CacheGrChunk (ERRORSCREEN);
  1272.         screen = grsegs[ERRORSCREEN];
  1273.         ShutdownId();
  1274. /*        memcpy((byte *)0xb8000,screen+7+7*160,17*160);
  1275.         gotoxy (1,23);*/
  1276.         exit(1);
  1277.     }
  1278. #endif
  1279.  
  1280.  
  1281. //
  1282. // build some tables
  1283. //
  1284.     InitDigiMap ();
  1285.  
  1286.     ReadConfig ();
  1287.  
  1288.     SetupSaveGames();
  1289.  
  1290. //
  1291. // HOLDING DOWN 'M' KEY?
  1292. //
  1293.         IN_ProcessEvents();
  1294.  
  1295. #ifndef SPEARDEMO
  1296.     if (Keyboard[sc_M])
  1297.     {
  1298.         DoJukebox();
  1299.         didjukebox=true;
  1300.     }
  1301.     else
  1302. #endif
  1303.  
  1304. //
  1305. // draw intro screen stuff
  1306. //
  1307.     IntroScreen ();
  1308.  
  1309. #ifdef _arch_dreamcast
  1310.     //TODO: VMU Selection Screen
  1311. #endif
  1312.  
  1313. //
  1314. // load in and lock down some basic chunks
  1315. //
  1316.  
  1317.     CA_CacheGrChunk(STARTFONT);
  1318.     CA_CacheGrChunk(STATUSBARPIC);
  1319.  
  1320.     LoadLatchMem ();
  1321.     BuildTables ();          // trig tables
  1322.     SetupWalls ();
  1323.  
  1324.     NewViewSize (viewsize);
  1325.  
  1326. //
  1327. // initialize variables
  1328. //
  1329.     InitRedShifts ();
  1330. #ifndef SPEARDEMO
  1331.     if(!didjukebox)
  1332. #endif
  1333.         FinishSignon();
  1334.  
  1335. #ifdef NOTYET
  1336.     vdisp = (byte *) (0xa0000+PAGE1START);
  1337.     vbuf = (byte *) (0xa0000+PAGE2START);
  1338. #endif
  1339. }
  1340.  
  1341. //===========================================================================
  1342.  
  1343. /*
  1344. ==========================
  1345. =
  1346. = SetViewSize
  1347. =
  1348. ==========================
  1349. */
  1350.  
  1351. boolean SetViewSize (unsigned width, unsigned height)
  1352. {
  1353.     viewwidth = width&~15;                  // must be divisable by 16
  1354.     viewheight = height&~1;                 // must be even
  1355.     centerx = viewwidth/2-1;
  1356.     shootdelta = viewwidth/10;
  1357.     if((unsigned) viewheight == screenHeight)
  1358.         viewscreenx = viewscreeny = screenofs = 0;
  1359.     else
  1360.     {
  1361.         viewscreenx = (screenWidth-viewwidth) / 2;
  1362.         viewscreeny = (screenHeight-scaleFactor*STATUSLINES-viewheight)/2;
  1363.         screenofs = viewscreeny*screenWidth+viewscreenx;
  1364.     }
  1365.  
  1366. //
  1367. // calculate trace angles and projection constants
  1368. //
  1369.     CalcProjection (FOCALLENGTH);
  1370.  
  1371.     return true;
  1372. }
  1373.  
  1374.  
  1375. void ShowViewSize (int width)
  1376. {
  1377.     int oldwidth,oldheight;
  1378.  
  1379.     oldwidth = viewwidth;
  1380.     oldheight = viewheight;
  1381.  
  1382.     if(width == 21)
  1383.     {
  1384.         viewwidth = screenWidth;
  1385.         viewheight = screenHeight;
  1386.         VWB_BarScaledCoord (0, 0, screenWidth, screenHeight, 0);
  1387.     }
  1388.     else if(width == 20)
  1389.     {
  1390.         viewwidth = screenWidth;
  1391.         viewheight = screenHeight - scaleFactor*STATUSLINES;
  1392.         DrawPlayBorder ();
  1393.     }
  1394.     else
  1395.     {
  1396.         viewwidth = width*16*screenWidth/320;
  1397.         viewheight = (int) (width*16*HEIGHTRATIO*screenHeight/200);
  1398.         DrawPlayBorder ();
  1399.     }
  1400.  
  1401.     viewwidth = oldwidth;
  1402.     viewheight = oldheight;
  1403. }
  1404.  
  1405.  
  1406. void NewViewSize (int width)
  1407. {
  1408.     viewsize = width;
  1409.     if(viewsize == 21)
  1410.         SetViewSize(screenWidth, screenHeight);
  1411.     else if(viewsize == 20)
  1412.         SetViewSize(screenWidth, screenHeight - scaleFactor * STATUSLINES);
  1413.     else
  1414.         SetViewSize(width*16*screenWidth/320, (unsigned) (width*16*HEIGHTRATIO*screenHeight/200));
  1415. }
  1416.  
  1417.  
  1418.  
  1419. //===========================================================================
  1420.  
  1421. /*
  1422. ==========================
  1423. =
  1424. = Quit
  1425. =
  1426. ==========================
  1427. */
  1428.  
  1429. void Quit (const char *errorStr, ...)
  1430. {
  1431. #ifdef NOTYET
  1432.     byte *screen;
  1433. #endif
  1434.     char error[256];
  1435.     if(errorStr != NULL)
  1436.     {
  1437.         va_list vlist;
  1438.         va_start(vlist, errorStr);
  1439.         vsprintf(error, errorStr, vlist);
  1440.         va_end(vlist);
  1441.     }
  1442.     else error[0] = 0;
  1443.  
  1444.     if (!pictable)  // don't try to display the red box before it's loaded
  1445.     {
  1446.         ShutdownId();
  1447.         if (error && *error)
  1448.         {
  1449. #ifdef NOTYET
  1450.             SetTextCursor(0,0);
  1451. #endif
  1452.             puts(error);
  1453. #ifdef NOTYET
  1454.             SetTextCursor(0,2);
  1455. #endif
  1456.             VW_WaitVBL(100);
  1457.         }
  1458.         exit(1);
  1459.     }
  1460.  
  1461.     if (!error || !*error)
  1462.     {
  1463. #ifdef NOTYET
  1464.         #ifndef JAPAN
  1465.         CA_CacheGrChunk (ORDERSCREEN);
  1466.         screen = grsegs[ORDERSCREEN];
  1467.         #endif
  1468. #endif
  1469.         WriteConfig ();
  1470.     }
  1471. #ifdef NOTYET
  1472.     else
  1473.     {
  1474.         CA_CacheGrChunk (ERRORSCREEN);
  1475.         screen = grsegs[ERRORSCREEN];
  1476.     }
  1477. #endif
  1478.  
  1479.     ShutdownId ();
  1480.  
  1481.     if (error && *error)
  1482.     {
  1483. #ifdef NOTYET
  1484.         memcpy((byte *)0xb8000,screen+7,7*160);
  1485.         SetTextCursor(9,3);
  1486. #endif
  1487.         puts(error);
  1488. #ifdef NOTYET
  1489.         SetTextCursor(0,7);
  1490. #endif
  1491.         VW_WaitVBL(200);
  1492.         exit(1);
  1493.     }
  1494.     else
  1495.     if (!error || !(*error))
  1496.     {
  1497. #ifdef NOTYET
  1498.         #ifndef JAPAN
  1499.         memcpy((byte *)0xb8000,screen+7,24*160); // 24 for SPEAR/UPLOAD compatibility
  1500.         #endif
  1501.         SetTextCursor(0,23);
  1502. #endif
  1503.     }
  1504.  
  1505.     exit(0);
  1506. }
  1507.  
  1508. //===========================================================================
  1509.  
  1510.  
  1511.  
  1512. /*
  1513. =====================
  1514. =
  1515. = DemoLoop
  1516. =
  1517. =====================
  1518. */
  1519.  
  1520.  
  1521. static void DemoLoop()
  1522. {
  1523.     int LastDemo = 0;
  1524.  
  1525. //
  1526. // check for launch from ted
  1527. //
  1528.     if (param_tedlevel != -1)
  1529.     {
  1530.         param_nowait = true;
  1531.         EnableEndGameMenuItem();
  1532.         NewGame(param_difficulty,0);
  1533.  
  1534. #ifndef SPEAR
  1535.         gamestate.episode = param_tedlevel/10;
  1536.         gamestate.mapon = param_tedlevel%10;
  1537. #else
  1538.         gamestate.episode = 0;
  1539.         gamestate.mapon = param_tedlevel;
  1540. #endif
  1541.         GameLoop();
  1542.         Quit (NULL);
  1543.     }
  1544.  
  1545.  
  1546. //
  1547. // main game cycle
  1548. //
  1549.  
  1550. #ifndef DEMOTEST
  1551.  
  1552.     #ifndef UPLOAD
  1553.  
  1554.         #ifndef GOODTIMES
  1555.         #ifndef SPEAR
  1556.         #ifndef JAPAN
  1557.         if (!param_nowait)
  1558.             NonShareware();
  1559.         #endif
  1560.         #else
  1561.             #ifndef GOODTIMES
  1562.             #ifndef SPEARDEMO
  1563.             extern void CopyProtection(void);
  1564.             if(!param_goodtimes)
  1565.                 CopyProtection();
  1566.             #endif
  1567.             #endif
  1568.         #endif
  1569.         #endif
  1570.     #endif
  1571.  
  1572.     StartCPMusic(INTROSONG);
  1573.  
  1574. #ifndef JAPAN
  1575.     if (!param_nowait)
  1576.         PG13 ();
  1577. #endif
  1578.  
  1579. #endif
  1580.  
  1581.     while (1)
  1582.     {
  1583.         while (!param_nowait)
  1584.         {
  1585. //
  1586. // title page
  1587. //
  1588. #ifndef DEMOTEST
  1589.  
  1590. #ifdef SPEAR
  1591.             SDL_Color pal[256];
  1592.             CA_CacheGrChunk (TITLEPALETTE);
  1593.             VL_ConvertPalette(grsegs[TITLEPALETTE], pal, 256);
  1594.  
  1595.             CA_CacheGrChunk (TITLE1PIC);
  1596.             VWB_DrawPic (0,0,TITLE1PIC);
  1597.             UNCACHEGRCHUNK (TITLE1PIC);
  1598.  
  1599.             CA_CacheGrChunk (TITLE2PIC);
  1600.             VWB_DrawPic (0,80,TITLE2PIC);
  1601.             UNCACHEGRCHUNK (TITLE2PIC);
  1602.             VW_UpdateScreen ();
  1603.             VL_FadeIn(0,255,pal,30);
  1604.  
  1605.             UNCACHEGRCHUNK (TITLEPALETTE);
  1606. #else
  1607.             CA_CacheScreen (TITLEPIC);
  1608.             VW_UpdateScreen ();
  1609.             VW_FadeIn();
  1610. #endif
  1611.             if (IN_UserInput(TickBase*15))
  1612.                 break;
  1613.             VW_FadeOut();
  1614. //
  1615. // credits page
  1616. //
  1617.             CA_CacheScreen (CREDITSPIC);
  1618.             VW_UpdateScreen();
  1619.             VW_FadeIn ();
  1620.             if (IN_UserInput(TickBase*10))
  1621.                 break;
  1622.             VW_FadeOut ();
  1623. //
  1624. // high scores
  1625. //
  1626.             DrawHighScores ();
  1627.             VW_UpdateScreen ();
  1628.             VW_FadeIn ();
  1629.  
  1630.             if (IN_UserInput(TickBase*10))
  1631.                 break;
  1632. #endif
  1633. //
  1634. // demo
  1635. //
  1636.  
  1637.             #ifndef SPEARDEMO
  1638.             PlayDemo (LastDemo++%4);
  1639.             #else
  1640.             PlayDemo (0);
  1641.             #endif
  1642.  
  1643.             if (playstate == ex_abort)
  1644.                 break;
  1645.             VW_FadeOut();
  1646.             if(screenHeight % 200 != 0)
  1647.                 VL_ClearScreen(0);
  1648.             StartCPMusic(INTROSONG);
  1649.         }
  1650.  
  1651.         VW_FadeOut ();
  1652.  
  1653. #ifdef DEBUGKEYS
  1654.         if (Keyboard[sc_Tab] && param_debugmode)
  1655.             RecordDemo ();
  1656.         else
  1657.             US_ControlPanel (0);
  1658. #else
  1659.         US_ControlPanel (0);
  1660. #endif
  1661.  
  1662.         if (startgame || loadedgame)
  1663.         {
  1664.             GameLoop ();
  1665.             if(!param_nowait)
  1666.             {
  1667.                 VW_FadeOut();
  1668.                 StartCPMusic(INTROSONG);
  1669.             }
  1670.         }
  1671.     }
  1672. }
  1673.  
  1674.  
  1675. //===========================================================================
  1676.  
  1677. #define IFARG(str) if(!strcmp(arg, (str)))
  1678.  
  1679. void CheckParameters(int argc, char *argv[])
  1680. {
  1681.     bool hasError = false, showHelp = false;
  1682.     bool sampleRateGiven = false, audioBufferGiven = false;
  1683.     int defaultSampleRate = param_samplerate;
  1684.  
  1685.     for(int i = 1; i < argc; i++)
  1686.     {
  1687.         char *arg = argv[i];
  1688. #ifndef SPEAR
  1689.         IFARG("--goobers")
  1690. #else
  1691.         IFARG("--debugmode")
  1692. #endif
  1693.             param_debugmode = true;
  1694.         else IFARG("--baby")
  1695.             param_difficulty = 0;
  1696.         else IFARG("--easy")
  1697.             param_difficulty = 1;
  1698.         else IFARG("--normal")
  1699.             param_difficulty = 2;
  1700.         else IFARG("--hard")
  1701.             param_difficulty = 3;
  1702.         else IFARG("--nowait")
  1703.             param_nowait = true;
  1704.         else IFARG("--tedlevel")
  1705.         {
  1706.             if(++i >= argc)
  1707.             {
  1708.                 printf("The tedlevel option is missing the level argument!\n");
  1709.                 hasError = true;
  1710.             }
  1711.             else param_tedlevel = atoi(argv[i]);
  1712.         }
  1713.         else IFARG("--windowed")
  1714.             fullscreen = false;
  1715.         else IFARG("--windowed-mouse")
  1716.         {
  1717.             fullscreen = false;
  1718.             forcegrabmouse = true;
  1719.         }
  1720.         else IFARG("--res")
  1721.         {
  1722.             if(i + 2 >= argc)
  1723.             {
  1724.                 printf("The res option needs the width and/or the height argument!\n");
  1725.                 hasError = true;
  1726.             }
  1727.             else
  1728.             {
  1729.                 screenWidth = atoi(argv[++i]);
  1730.                 screenHeight = atoi(argv[++i]);
  1731.                 unsigned factor = screenWidth / 320;
  1732.                 if(screenWidth % 320 || screenHeight != 200 * factor && screenHeight != 240 * factor)
  1733.                     printf("Screen size must be a multiple of 320x200 or 320x240!\n"), hasError = true;
  1734.             }
  1735.         }
  1736.         else IFARG("--resf")
  1737.         {
  1738.             if(i + 2 >= argc)
  1739.             {
  1740.                 printf("The resf option needs the width and/or the height argument!\n");
  1741.                 hasError = true;
  1742.             }
  1743.             else
  1744.             {
  1745.                 screenWidth = atoi(argv[++i]);
  1746.                 screenHeight = atoi(argv[++i]);
  1747.                 if(screenWidth < 320)
  1748.                     printf("Screen width must be at least 320!\n"), hasError = true;
  1749.                 if(screenHeight < 200)
  1750.                     printf("Screen height must be at least 200!\n"), hasError = true;
  1751.             }
  1752.         }
  1753.         else IFARG("--bits")
  1754.         {
  1755.             if(++i >= argc)
  1756.             {
  1757.                 printf("The bits option is missing the color depth argument!\n");
  1758.                 hasError = true;
  1759.             }
  1760.             else
  1761.             {
  1762.                 screenBits = atoi(argv[i]);
  1763.                 switch(screenBits)
  1764.                 {
  1765.                     case 8:
  1766.                     case 16:
  1767.                     case 24:
  1768.                     case 32:
  1769.                         break;
  1770.  
  1771.                     default:
  1772.                         printf("Screen color depth must be 8, 16, 24, or 32!\n");
  1773.                         hasError = true;
  1774.                         break;
  1775.                 }
  1776.             }
  1777.         }
  1778.         else IFARG("--nodblbuf")
  1779.             usedoublebuffering = false;
  1780.         else IFARG("--extravbls")
  1781.         {
  1782.             if(++i >= argc)
  1783.             {
  1784.                 printf("The extravbls option is missing the vbls argument!\n");
  1785.                 hasError = true;
  1786.             }
  1787.             else
  1788.             {
  1789.                 extravbls = atoi(argv[i]);
  1790.                 if(extravbls < 0)
  1791.                 {
  1792.                     printf("Extravbls must be positive!\n");
  1793.                     hasError = true;
  1794.                 }
  1795.             }
  1796.         }
  1797.         else IFARG("--joystick")
  1798.         {
  1799.             if(++i >= argc)
  1800.             {
  1801.                 printf("The joystick option is missing the index argument!\n");
  1802.                 hasError = true;
  1803.             }
  1804.             else param_joystickindex = atoi(argv[i]);   // index is checked in InitGame
  1805.         }
  1806.         else IFARG("--joystickhat")
  1807.         {
  1808.             if(++i >= argc)
  1809.             {
  1810.                 printf("The joystickhat option is missing the index argument!\n");
  1811.                 hasError = true;
  1812.             }
  1813.             else param_joystickhat = atoi(argv[i]);
  1814.         }
  1815.         else IFARG("--samplerate")
  1816.         {
  1817.             if(++i >= argc)
  1818.             {
  1819.                 printf("The samplerate option is missing the rate argument!\n");
  1820.                 hasError = true;
  1821.             }
  1822.             else param_samplerate = atoi(argv[i]);
  1823.             sampleRateGiven = true;
  1824.         }
  1825.         else IFARG("--audiobuffer")
  1826.         {
  1827.             if(++i >= argc)
  1828.             {
  1829.                 printf("The audiobuffer option is missing the size argument!\n");
  1830.                 hasError = true;
  1831.             }
  1832.             else param_audiobuffer = atoi(argv[i]);
  1833.             audioBufferGiven = true;
  1834.         }
  1835.         else IFARG("--mission")
  1836.         {
  1837.             if(++i >= argc)
  1838.             {
  1839.                 printf("The mission option is missing the mission argument!\n");
  1840.                 hasError = true;
  1841.             }
  1842.             else
  1843.             {
  1844.                 param_mission = atoi(argv[i]);
  1845.                 if(param_mission < 0 || param_mission > 3)
  1846.                 {
  1847.                     printf("The mission option must be between 0 and 3!\n");
  1848.                     hasError = true;
  1849.                 }
  1850.             }
  1851.         }
  1852.         else IFARG("--configdir")
  1853.         {
  1854.             if(++i >= argc)
  1855.             {
  1856.                 printf("The configdir option is missing the dir argument!\n");
  1857.                 hasError = true;
  1858.             }
  1859.             else
  1860.             {
  1861.                 size_t len = strlen(argv[i]);
  1862.                 if(len + 2 > sizeof(configdir))
  1863.                 {
  1864.                     printf("The config directory is too long!\n");
  1865.                     hasError = true;
  1866.                 }
  1867.                 else
  1868.                 {
  1869.                     strcpy(configdir, argv[i]);
  1870.                     if(argv[i][len] != '/' && argv[i][len] != '\\')
  1871.                         strcat(configdir, "/");
  1872.                 }
  1873.             }
  1874.         }
  1875.         else IFARG("--goodtimes")
  1876.             param_goodtimes = true;
  1877.         else IFARG("--ignorenumchunks")
  1878.             param_ignorenumchunks = true;
  1879.         else IFARG("--help")
  1880.             showHelp = true;
  1881.         else hasError = true;
  1882.     }
  1883.     if(hasError || showHelp)
  1884.     {
  1885.         if(hasError) printf("\n");
  1886.         printf(
  1887.             "Wolf4SDL v1.7 ($Revision$)\n"
  1888.             "Ported by Chaos-Software (http://www.chaos-software.de.vu)\n"
  1889.             "Original Wolfenstein 3D by id Software\n\n"
  1890.             "Usage: Wolf4SDL [options]\n"
  1891.             "Options:\n"
  1892.             " --help                 This help page\n"
  1893.             " --tedlevel <level>     Starts the game in the given level\n"
  1894.             " --baby                 Sets the difficulty to baby for tedlevel\n"
  1895.             " --easy                 Sets the difficulty to easy for tedlevel\n"
  1896.             " --normal               Sets the difficulty to normal for tedlevel\n"
  1897.             " --hard                 Sets the difficulty to hard for tedlevel\n"
  1898.             " --nowait               Skips intro screens\n"
  1899.             " --windowed[-mouse]     Starts the game in a window [and grabs mouse]\n"
  1900.             " --res <width> <height> Sets the screen resolution\n"
  1901.             "                        (must be multiple of 320x200 or 320x240)\n"
  1902.             " --resf <w> <h>         Sets any screen resolution >= 320x200\n"
  1903.             "                        (which may result in graphic errors)\n"
  1904.             " --bits <b>             Sets the screen color depth\n"
  1905.             "                        (use this when you have palette/fading problems\n"
  1906.             "                        allowed: 8, 16, 24, 32, default: \"best\" depth)\n"
  1907.             " --nodblbuf             Don't use SDL's double buffering\n"
  1908.             " --extravbls <vbls>     Sets a delay after each frame, which may help to\n"
  1909.             "                        reduce flickering (unit is currently 8 ms, default: 0)\n"
  1910.             " --joystick <index>     Use the index-th joystick if available\n"
  1911.             "                        (-1 to disable joystick, default: 0)\n"
  1912.             " --joystickhat <index>  Enables movement with the given coolie hat\n"
  1913.             " --samplerate <rate>    Sets the sound sample rate (given in Hz, default: %i)\n"
  1914.             " --audiobuffer <size>   Sets the size of the audio buffer (-> sound latency)\n"
  1915.             "                        (given in bytes, default: 2048 / (44100 / samplerate))\n"
  1916.             " --ignorenumchunks      Ignores the number of chunks in VGAHEAD.*\n"
  1917.             "                        (may be useful for some broken mods)\n"
  1918.             " --configdir <dir>      Directory where config file and save games are stored\n"
  1919. #if defined(_arch_dreamcast) || defined(_WIN32)
  1920.             "                        (default: current directory)\n"
  1921. #else
  1922.             "                        (default: $HOME/.wolf4sdl)\n"
  1923. #endif
  1924. #if defined(SPEAR) && !defined(SPEARDEMO)
  1925.             " --mission <mission>    Mission number to play (0-3)\n"
  1926.             "                        (default: 0 -> .sod, 1-3 -> .sd*)\n"
  1927.             " --goodtimes            Disable copy protection quiz\n"
  1928. #endif
  1929.             , defaultSampleRate
  1930.         );
  1931.         exit(1);
  1932.     }
  1933.  
  1934.     if(sampleRateGiven && !audioBufferGiven)
  1935.         param_audiobuffer = 2048 / (44100 / param_samplerate);
  1936. }
  1937.  
  1938. /*
  1939. ==========================
  1940. =
  1941. = main
  1942. =
  1943. ==========================
  1944. */
  1945.  
  1946. int main (int argc, char *argv[])
  1947. {
  1948. #if defined(_arch_dreamcast)
  1949.     DC_Init();
  1950. #else
  1951.     CheckParameters(argc, argv);
  1952. #endif
  1953.     CheckForEpisodes();
  1954.     InitGame();
  1955.     DemoLoop();
  1956.     Quit("Demo loop exited???");
  1957.     return 1;
  1958. }
  1959.