home *** CD-ROM | disk | FTP | other *** search
/ ISV Strong Games / ISV_STRONG_GAMES.iso / shootemup / hive / !Hive / c / game < prev    next >
Text File  |  2000-11-27  |  8KB  |  296 lines

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. #include "DeskLib:h.Error"
  6. #include "DeskLib:h.File"
  7. #include "DeskLib:h.GFX"
  8. #include "DeskLib:h.Hourglass"
  9. #include "DeskLib:h.Kbd"
  10. #include "DeskLib:h.SWI"
  11.  
  12. #include "Popcorn:h.Popcorn"
  13.  
  14. #include "h.sound"
  15. #include "h.useful"
  16. #include "h.waves"
  17.  
  18. #define random(limit) (rand() % (limit))
  19.  
  20. struct object_table     *ship_table, *missile_table, *alien_table,
  21.             *fx_table, *alien_swoop_table, *alien_missile_table,
  22.             *token_table;
  23. void            **ship_sprite[9], **alphabet_sprite[57];
  24. void            *backdrop;
  25. BOOL            quit = FALSE, dirty_rotten_cheat = FALSE,
  26.             hit_bottom = FALSE;
  27. int            fired = 0, max_fired = 10;
  28. int            wave = 0, aliens_left = 0, last_killed_at = -1;
  29. int            ship_pos = 28, fire_freq = 50, fire_again = 0,
  30.             special = 0, invulnerable = 0, ship_x;
  31. int            score, hi_score=0, lives, next_life_time = -1;
  32. int            screenshot_number = 0;
  33.  
  34. void exit_handler(void)
  35. {
  36.   kill_sound();
  37.   SWI(2, 0, SWI_OS_Byte, 112, 1);
  38.   SWI(2, 0, SWI_OS_Byte, 113, 1);
  39.   SWI(0, 0, 256+12);
  40.   SWI(0, 0, SWI_OS_RestoreCursors);
  41.   SWI(2, 0, SWI_OS_Byte, 229, 0);
  42. }
  43.  
  44. void title_handler(struct game_object *obj, union object_flags reason, void *data)
  45. {
  46.   if (reason.bits.attn_every)
  47.   {
  48.     if (Kbd_KeyDown(-99))
  49.     {
  50.       Popcorn_DeleteObject(obj);
  51.       next_life_time = 0;
  52.       aliens_left = 0;
  53.       wave = 1; wave_initialise(wave);
  54.       hit_bottom = FALSE;
  55.       score = 0; lives = 6;
  56.     }
  57.   }
  58. }
  59.  
  60. void misc_every_handler(struct game_object *obj, union object_flags reason, void *data)
  61. {
  62.   void **special_sprite;
  63.   char string[64];
  64.   int  time;
  65.  
  66.   if (reason.bits.attn_plot)
  67.   {
  68.     switch (wave)
  69.     {
  70.       case 0 :
  71.         print_message("PRESS SPACE TO PLAY", 25, 150, TRUE);
  72.         sprintf(string, "BEST SCORE... %06d", hi_score);
  73.         print_message(string, 20, 236, FALSE);
  74.         break;
  75.       case -1 :
  76.         print_message("GAME OVER", 100, 100, TRUE);
  77.         break;
  78.       default :
  79.         if (last_killed_at == -1)
  80.         {
  81.           sprintf(string, "%06d", score);
  82.           print_message(string, 10, 10, TRUE);
  83.           sprintf(string, "%d", lives);
  84.           print_message(string, 10, 236, FALSE);
  85.           switch (special)
  86.           {
  87.             case 1 :
  88.               special_sprite = Popcorn_FindResource("<Hive$Dir>.Graphics.spiral.0");
  89.               Popcorn_PlotSprite(*special_sprite, 300, 226);
  90.               break;
  91.             case 2 :
  92.               special_sprite = Popcorn_FindResource("<Hive$Dir>.Graphics.msl.0");
  93.               Popcorn_PlotSprite(*special_sprite, 300, 226);
  94.               break;
  95.           }
  96.         }
  97.         else
  98.         {
  99.           sprintf(string, "WAVE %d\0", wave+1);
  100.           print_message(string, 120, 100, TRUE);
  101.         }
  102.         break;
  103.     }
  104.   }
  105.  
  106.   check_newwave();
  107.  
  108.   sine_value = sine_value + 5;
  109.   if (sine_value > 359)
  110.     sine_value = sine_value - 360;
  111.  
  112.   if (dirty_rotten_cheat && special == 0)
  113.   {
  114.     if (Kbd_KeyDown(-49))
  115.     {
  116.       special = 1;
  117.       make_sound("<Hive$Dir>.Sounds.Token");
  118.     }
  119.     if (Kbd_KeyDown(-72))
  120.     {
  121.       special = 2;
  122.       make_sound("<Hive$Dir>.Sounds.Token");
  123.     }
  124.   }
  125.  
  126.   if (Kbd_KeyDown(-114)) dirty_rotten_cheat = TRUE;
  127.   if (Kbd_KeyDown(-115)) dirty_rotten_cheat = FALSE;
  128.  
  129.   if (wave != 0 && next_life_time != -1)
  130.   {
  131.     SWI(0, 1, SWI_OS_ReadMonotonicTime, &time);
  132.     if (time - next_life_time > 250)
  133.     {
  134.       Popcorn_NewPrototype(ship_table, 'PIHS', 160<<12, 255<<12);
  135.       fire_freq = 50;
  136.       next_life_time = -1;
  137.     }
  138.   }
  139. }
  140.  
  141. void game_finish(void)
  142. {
  143.   FILE   *hi_score_file;
  144.  
  145.   if (score > hi_score)
  146.   {
  147.     hi_score = score;
  148.     hi_score_file = fopen("<Hive$Dir>.Data.HiScore", "wb");
  149.     if (hi_score_file == NULL)
  150.       return;
  151.     fprintf(hi_score_file, "%d", hi_score);
  152.     fclose(hi_score_file);
  153.   }
  154. }
  155.  
  156. void game_initialise(void)
  157. {
  158.   FILE   *hi_score_file;
  159.   int      c, rand_seed;
  160.   char      temp_filename[255];
  161.  
  162.   Hourglass_On();
  163.  
  164.   ship_table           = Popcorn_NewTable(10, TRUE);
  165.   missile_table        = Popcorn_NewTable(50, TRUE);
  166.   alien_table          = Popcorn_NewTable(100, TRUE);
  167.   alien_swoop_table    = Popcorn_NewTable(50, TRUE);
  168.   alien_missile_table  = Popcorn_NewTable(50, TRUE);
  169.   token_table          = Popcorn_NewTable(50, TRUE);
  170.   fx_table             = Popcorn_NewTable(200, TRUE);
  171.   alien_missile_table->grav_y = 512;
  172.   token_table->grav_y         = 1024;
  173.  
  174.   game_window.x0 = game_window.y0 = 0;
  175.   game_window.x1 = 320; game_window.y1 = 256;
  176.   user_window.x0 = user_window.y0 = -50;
  177.   user_window.x1 = 370; user_window.y1 = 306;
  178.  
  179.   strcpy(symbol_table_filename, "<Hive$Dir>.Data.Symbols");
  180.   Error_CheckFatal(Popcorn_LoadResourceFile("<Hive$Dir>.Data.Resources"));
  181.   Error_CheckFatal(Popcorn_LoadPrototypes("<Hive$Dir>.Data.Prototypes"));
  182.   Error_CheckFatal(Popcorn_LoadGroup('LTIT'));
  183.   Error_CheckFatal(Popcorn_LoadGroup('EROC'));
  184.   Error_CheckFatal(Popcorn_LoadGroup('NILA'));
  185. /*   Error_CheckFatal(Popcorn_LoadGroup('2DAB')); */
  186. /*   Error_CheckFatal(Popcorn_LoadGroup('NOIS')); */
  187.   Error_CheckFatal(Popcorn_LoadGroup('HPLA'));
  188.   Error_CheckFatal(Popcorn_LoadGroup('NUOS'));
  189.  
  190.   backdrop = malloc(File_Size("<Hive$Dir>.Graphics.back.1"));
  191.   init_sound();
  192.   SWI(2, 0, SWI_OS_Byte, 229, 1);
  193.   atexit(exit_handler);
  194.   load_scoring();
  195.   wave_initialise(0);
  196.   Popcorn_NewPrototype(fx_table, 'CSIM', 0, 0);
  197.  
  198.   for (c=0; c!=9; c++)
  199.   {
  200.     sprintf(temp_filename, "<Hive$Dir>.Graphics.ship.%d", c);
  201.     ship_sprite[c] = Popcorn_FindResource(temp_filename);
  202.   }
  203.  
  204.   for (c=32; c!=91; c++)
  205.   {
  206.     sprintf(temp_filename, "<Hive$Dir>.Graphics.alphabet.%d", c);
  207.     alphabet_sprite[c-32] = Popcorn_FindResource(temp_filename);
  208.   }
  209.  
  210.   Error_CheckFatal(File_LoadTo("<Hive$Dir>.Data.Sine", sine_table, NULL));
  211.  
  212.   hi_score_file = fopen("<Hive$Dir>.Data.HiScore", "rb");
  213.   if (hi_score_file == NULL)
  214.   {
  215.     hi_score = 1000;
  216.   }
  217.   else
  218.   {
  219.     fscanf(hi_score_file, "%d", &hi_score);
  220.     fclose(hi_score_file);
  221.   }
  222.  
  223.   SWI(0, 1, SWI_OS_ReadMonotonicTime, &rand_seed);
  224.   srand(rand_seed);
  225.  
  226.   Hourglass_Off();
  227.  
  228.   SWI(2, 0, SWI_OS_Byte, 114, 0);
  229.   GFX_Mode(13);
  230.   SWI(2, 0, SWI_OS_Byte, 114, 1);
  231.   SWI(0, 0, SWI_OS_RemoveCursors);
  232.   Popcorn_ReadScreenDetails();
  233.   Popcorn_ClearScreen(0);
  234. }
  235.  
  236. BOOL check_pause(void)
  237. {
  238.   char cmd[256];
  239.  
  240.   if (Kbd_KeyDown(-56))
  241.   {
  242.     while (Kbd_KeyDown(-56));
  243.     while (!Kbd_KeyDown(-56))
  244.     {
  245.       if (Kbd_KeyDown(-99))
  246.       {
  247.         while (!Kbd_KeyDown(-99));
  248.         sprintf(cmd, "Screensave $.Screen%d", screenshot_number);
  249.         system(cmd);
  250.         screenshot_number++;
  251.       }
  252.     }
  253.     while (Kbd_KeyDown(-56));
  254.     return(TRUE);
  255.   }
  256.   return(FALSE);
  257. }
  258.  
  259. int main(void)
  260. {
  261.   int  start_time, end_time, frames=1;
  262.  
  263.   game_initialise();
  264.  
  265.   do {
  266.     SWI(0, 1, SWI_OS_ReadMonotonicTime, &start_time);
  267.     if (check_pause())
  268.       SWI(0, 1, SWI_OS_ReadMonotonicTime, &start_time);
  269.     Popcorn_PlotBackdrop(backdrop);
  270.     Popcorn_Process(missile_table, TRUE, frames);
  271.     Popcorn_Process(fx_table, TRUE, frames);
  272.     Popcorn_Process(ship_table, TRUE, frames);
  273.     Popcorn_Process(alien_table, TRUE, frames);
  274.     Popcorn_Process(alien_swoop_table, TRUE, frames);
  275.     Popcorn_Process(alien_missile_table, TRUE, frames);
  276.     Popcorn_Process(token_table, TRUE, frames);
  277.     Popcorn_CollisionCheck(missile_table, alien_table);
  278.     Popcorn_CollisionCheck(missile_table, alien_swoop_table);
  279.     Popcorn_CollisionCheck(token_table, ship_table);
  280.     if (!dirty_rotten_cheat)
  281.     {
  282.       Popcorn_CollisionCheck(ship_table, alien_missile_table);
  283.       Popcorn_CollisionCheck(ship_table, alien_swoop_table);
  284.     }
  285.     SWI(1, 0, SWI_OS_Byte, 19);
  286.     Popcorn_SwapBanks();
  287.     do
  288.     {
  289.       SWI(0, 1, SWI_OS_ReadMonotonicTime, &end_time);
  290.       frames = (end_time - start_time) >> 1;
  291.     } while (frames == 0);
  292.   } while (!(Kbd_KeyDown(-113) && Kbd_KeyDown(-2)));
  293.  
  294.   return(0);
  295. }
  296.