home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Games / Arashi 1.1 / Game Source / jam src / GameLoop.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-08  |  7.9 KB  |  366 lines  |  [TEXT/KAHL]

  1. /*/
  2.      Project Arashi: GameLoop.c
  3.      Major release: Version 1.1, 7/22/92
  4.  
  5.      Last modification: Thursday, July 8, 1993, 23:11
  6.      Created: Sunday, February 5, 1989, 2:21
  7.  
  8.      Copyright © 1989-1993, Juri Munkki
  9. /*/
  10.  
  11. #include "VA.h"
  12. #include "STORM.h"
  13. #include "Shuddup.h"
  14. #include "PlayOptions.h"
  15. #include "GamePause.h"
  16. #include "HighScoresToBeat.h"        /* mz */
  17. #include <GestaltEqu.h>
  18. #include <OSUtils.h>
  19. #include <Types.h>
  20. #include "heroflags.h"
  21.  
  22. /* #define DEBUGGING_ROUTINES */
  23. #define    UNNATURALDEATH    0
  24. #define NATURALDEATH    1
  25.  
  26. static     int    highestLastLevel=9;            /* (mz) */
  27.         int    FinalDeathMode=NATURALDEATH;
  28.         
  29. extern    Player    Hero;
  30. extern    int        OldSound;                /*    Flag for Sound Kit        */
  31.  
  32.  
  33. Initials    *HiScoresToBeat;
  34. int         ScoretoBeatIndex;
  35.  
  36. EventRecord        Event;
  37. WindowPtr        BackdropWind;
  38.  
  39.  
  40. void    GameLoop(Options)
  41. int        Options;
  42. {
  43.     int            i;
  44.     long int    levelStBonus=0;
  45.     long int    levelBonus=0;
  46.     int            levelComplete=0;
  47.     Handle        ScoreHand;
  48.     int            DoomsdayDeath = 0;
  49.     
  50.     /* read scores from resources  for top right corner */
  51.     if (PlayOptions->restart == 0)
  52.         ScoreHand=GetResource('SCOR',128);
  53.     else 
  54.         ScoreHand=GetResource('SCOR',129);
  55.     HLock(ScoreHand);
  56.     HiScoresToBeat = (Initials *)*ScoreHand;
  57.     ScoretoBeatIndex = 50;        /* start at the bottom */
  58.     
  59.     VAEraseBuffer();
  60.  
  61.     /*    Flush out some events and let MultiFinder update windows.    */
  62.     for(i=0;i<10;i++)
  63.         GameEvent();
  64.  
  65.     ZeroScore();
  66.     
  67.     if(Options == NormalPlay)
  68.     {    StartNormalPlay();
  69.     }
  70.     else
  71.     {    Hero.lives = 0;
  72.         if(Options == PlaybackPlay)
  73.             ThisLevel.lvNext = TickCount() & 7;
  74.  
  75.         LoadPlayRecord();
  76.         if(Options == RecordPlay)
  77.         {    
  78.             StartPlayRecord();
  79.         }
  80.         else
  81.         if(Options == PlaybackPlay)
  82.         {    
  83.             StartPlayback();
  84.         }
  85.     }
  86.  
  87.     DisplayLives();
  88.  
  89.     do
  90.     {    VACatchUp();
  91.  
  92.         DisplayScore();
  93.  
  94.         if(Hero.state != HeroDead)
  95.         {    AddLife();
  96.             if (levelComplete == 1)
  97.             {
  98.                 /* check to see if this was first level.  If so give STBonus.        */
  99.                 if (!(Hero.Flags & STBonusGivenMask)){
  100.                     
  101.                     /* set bonus granted and show zap flags                         */
  102.                     
  103.                     Hero.Flags |= (ShowSuperZapMsgMask + STBonusGivenMask ); 
  104.                     IncreaseScore(levelStBonus); /* give starting bonus                */
  105.                 } 
  106.                 
  107.                 Hero.Flags |= (ShowSuperZapMsgMask);
  108.                 
  109.                 /* if  a saved game and start level was already done then turn on    */
  110.                 /* superzap msg flag */
  111.                 
  112.                 IncreaseScore(levelBonus);
  113.                 DisplayScore();            
  114.             }
  115.             else
  116.                 if(!(Hero.Flags & SavedgameFirstLVLCompleteMask))
  117.                     Hero.Flags &= (FLAGSMASK - ShowSuperZapMsgMask);
  118.             levelComplete = 0;
  119.             levelBonus = 0;
  120.             levelStBonus = 0;
  121.             STLoadLevel(); 
  122.             CreateFit();
  123.         }
  124.         else if(DoomsdayDeath)
  125.         {
  126.             int    numzaps=ThisLevel.plSuperZaps;
  127.             int    lvl =     ThisLevel.lvNumber;
  128.             
  129.             STLoadLevel(); 
  130.             ThisLevel.plSuperZaps = numzaps;    /* Keep old # of zaps                 */
  131.             ThisLevel.lvNumber = lvl; 
  132.             ThisLevel.lvNext = lvl + 1;
  133.             Hero.Flags &= (FLAGSMASK - ShowSuperZapMsgMask); /* unset re-zap msg    */
  134.             CreateFit();
  135.             Hero.Flags |= ShowSuperZapMsgMask;    /* set show re-zap msg                 */
  136.             DoomsdayDeath = 0;
  137.             
  138.         }
  139.         else
  140.         {    VA.color=0;
  141.             RedrawField();
  142.         }
  143.         
  144.         RemoveLife();
  145.         STInitialize();
  146.     
  147.         do
  148.         {    if(Options != PlaybackPlay)
  149.             {    GameEvent();
  150.                 switch(Event.what)
  151.                 {    case keyDown:
  152.                     case autoKey:
  153.                         switch(Event.message & 0xFF)
  154.                         {    case 'q':
  155.                             case 'Q':
  156.                             case 27:
  157.                             case 13:    Hero.state=HeroDead;
  158.                                         Hero.lives=0;                
  159.                                         FinalDeathMode=UNNATURALDEATH;
  160.                                         break;
  161.                             case ' ':    /* Handled in player routine    */
  162.                                                                     break;
  163.                             case 'p':
  164.                             case 'P':    GamePause(GameRunning);     
  165.                                         break;
  166.     #ifdef DEBUGGING_ROUTINES
  167.                             case 26:    DumpThisLevel();            break;
  168.                             case '1':    AddLife();                    break;
  169.                             case '2':    RemoveLife();                break;
  170.  
  171.                             case 'z':    CreateNewFlipper();            break;
  172.                             case 'x':    CreateNewTanker(0);            break;
  173.                             case 's':    CreateNewTanker(1);            break;
  174.                             case 'd':    CreateNewTanker(2);            break;
  175.                             case 'c':    CreateNewPulsar();            break;
  176.                             case 'v':    CreateNewBall();            break;
  177.                             case 'o':    ThisLevel.totalCount = 0;    break;
  178.  
  179.                         /*    case '@':    Hero.state=HeroFlying;
  180.                                         DemoMode();                    break;
  181.                             case 'k':    TestZoomRect();                break;
  182.                             case 'l':    DoLevelSelect();            break; */
  183.                             default:    Hero.state=HeroFlying;        break;
  184.      #endif
  185.                         }
  186.                         break;
  187.                 }
  188.             }
  189.             else    /*    Playback active.    */
  190.             {    if(GamePeekEvent())
  191.                 {    Hero.lives = 0;
  192.                     Hero.state = HeroDead;
  193.                 }
  194.                 Event.what = 0;
  195.             }
  196.             if(ThisLevel.activeCount<ThisLevel.boredomCount)
  197.                 ThisLevel.probIncrease=ThisLevel.boredProb;
  198.             else
  199.                 ThisLevel.probIncrease=0;
  200.             ThisLevel.activeCount=0;
  201.  
  202.             STUpdate();
  203.  
  204.             VA.segmscale=5;
  205.  
  206.             VA.color = BG2; 
  207.             if (!(PlayOptions->restart == 0))    /* only show count if Practice mode */
  208.             {
  209.                 if (ThisLevel.lvColor == 5)        /* check for invis lvl where BG2 is */
  210.                     VA.color=BG1;                /* also invis. (mz)                 */
  211.                 VADrawPadNumber(ThisLevel.totalCount,VA.frame.right-10,60,3);
  212.                 VADrawPadNumber(ThisLevel.edgeCount,VA.frame.right-10,80,3);
  213.                 VA.color = BG2;
  214.             }
  215.             VAStep();
  216.             
  217.             if(Hero.state == HeroPlaying)
  218.             {    if(ThisLevel.edgeCount==ThisLevel.totalCount)
  219.                 {    Hero.endtimer--;
  220.                     if(Hero.endtimer<=0)
  221.                     {    Hero.state=HeroFlying;
  222.                         Hero.flydepth=0;
  223.                         levelComplete=1;
  224.                         levelStBonus=ThisLevel.lvStBonus;
  225.                         levelBonus=ThisLevel.lvBonus;
  226.                     }
  227.                 }
  228.             }
  229.         
  230.         } while(Hero.state != HeroDead && Hero.state != HeroFlying);
  231.  
  232.         if(Hero.state == HeroFlying)
  233.             STFlyThruLoop(Options);
  234.             
  235.         if( (ThisLevel.Doomsday==1) && (Hero.lives > 0)) /* player has died in Doomsday level */
  236.         {    DoomsdayDeath = 1;
  237.             VA.color=-1;    /*    Black                    */
  238.             RedrawField();
  239.             RedrawSpikes();
  240.         }
  241.     
  242.     } while(Hero.lives>0 || Hero.state != HeroDead);
  243.     if(FinalDeathMode  && (Options == NormalPlay))
  244.         STFlyOutLoop();                                /* Fly out at end */
  245.     
  246.     FinalDeathMode = NATURALDEATH;
  247.     highestLastLevel = ThisLevel.lvNumber - 1;    /* save highest level *completed*    */
  248.     
  249.     /* Make sure saved game flag does not carry into next game.            
  250.     Hero.Flags &= (FLAGSMASK - SavedGameStartMask); */
  251.     
  252.     Hero.Flags = NOFLAGSMASK;
  253.     
  254.     if(Options != NormalPlay)
  255.     {    if(Options == RecordPlay)
  256.         {    WriteRecordedPlay();
  257.         }
  258.         UnloadPlayRecord();
  259.     }    
  260. }
  261.  
  262.  
  263. int        StormStart()
  264. {
  265.     int                zero=0;
  266.     int                i;
  267.     Handle            TheDevice;
  268.     long            myFeature;
  269.     
  270.     DoInits();
  271.     
  272.     if(installASHI())
  273.         return -1;
  274.         
  275.     DisableSuperClock(); 
  276.     
  277.     /* Now do check for Color Quickdraw. Any version above simple 8 bit will do. */
  278.     if(Gestalt(gestaltQuickdrawVersion, &myFeature))
  279.     {
  280.         ParamText("\pARASHI encountered an unrecoverable _GESTALT error.",nil,nil,nil);
  281.         Alert(131,0);
  282.         ExitToShell();
  283.     }
  284.     if (myFeature < gestalt8BitQD)
  285.     {
  286.         ParamText("\pARASHI requires Color Quickdraw and has not detected it in your System.",nil,nil,nil);
  287.         Alert(131,0);
  288.         ExitToShell();
  289.     }
  290.     
  291.     
  292.     randSeed= Ticks;
  293.     VARandSeed= Ticks;
  294.  
  295.     TheDevice=ScreenSelect();
  296.     if(TheDevice==0)    return -1;
  297.     
  298.     if(PlayOptions->sys607Sound)
  299.     {    OldSound=0;
  300.     }
  301.     else
  302.     {    OldSound=1;
  303.     }
  304.     if(!PlayOptions->soundOff)
  305.         InitSoundKit();
  306.         
  307.     if(PlayOptions->blankUnused)
  308.         BackdropWind = NewWindow(0,&((*GrayRgn)->rgnBBox),&zero,-1,plainDBox,0,0,0);
  309.     else
  310.     {    Rect    SmallRect = { 0,0,10,10};
  311.         BackdropWind = NewWindow(0,&SmallRect,&zero,-1,plainDBox,0,0,0);
  312.     }    
  313.     VAInit(TheDevice);
  314.                     
  315.     if(PlayOptions->verticalGame)
  316.     {    i=VA.frame.bottom*4L/5;
  317.         if(VA.frame.right>i)
  318.             VA.frame.right=i;
  319.     }
  320.  
  321.     VAInitFractalLines();
  322.     STAllocate();
  323.  
  324.     /*    Flush out some events and let MultiFinder update windows.    */
  325.     for(i=0;i<10;i++)    GameEvent();
  326.     
  327.     return 0;
  328. }
  329.  
  330. long    PlayGame(HighScore,Options)
  331. long    HighScore;
  332. int        Options;
  333. {
  334.     int        i;
  335.     
  336.     Hero.score = -1;
  337.     i=DoLevelSelect(highestLastLevel);
  338.     if(Hero.Flags & SavedGameStartMask)
  339.         Hero.Flags &= (FLAGSMASK - SavedgameFirstLVLCompleteMask);
  340.     if(i>=0)
  341.     {    VA.FrameSpeed=3;
  342.         ThisLevel.lvNext=i;
  343.         GameLoop(Options);
  344.     }
  345.     else
  346.         Hero.score = -1;    
  347.     return    Hero.score;
  348. }
  349.  
  350. void    PlayDemoGame()
  351. {
  352.     long    seeder;
  353.     
  354.     seeder = VARandSeed;
  355.     GameLoop(PlaybackPlay);
  356.     VARandSeed = seeder;
  357. }
  358.  
  359. void    RecordDemoGame()
  360. {
  361.     static    demoLevelStart = 10;
  362.  
  363.     ThisLevel.lvNext = demoLevelStart;
  364.     demoLevelStart+=2;
  365.     GameLoop(RecordPlay);
  366. }