File: GAME.C | Size: 7,559 bytes | Download file | Back to directory listing | BWPOW's homepage
#include <math.h>
#include <all312.h>
#include "game.h"
#include "invasion.h"
 
short astx[20],asty[20];
char astt[20],asto[20],astto[20];
 
extern short tim;
short ax,ay;
char ap;
char end=0;
short lowest;
 
char alien[10][6];
char rot[10][6];
char howrot[10][6];
short now;
 
typedef struct{
  short x,y;
  char speed;
}STRELA;
 
STRELA s_dole[160];
STRELA s_hore[20];
STRELA expl[160];
 
short num_dole,num_hore,num_exp;
 
short hx,hy;
char hs=2;
char he=0;
 
void zabili_ma()
{
  hx=320;
  hy=520;
  num_dole=0;
  user.lives--;
}
 
void urob_screen()
{
  char x,y,i;
 
  if(icfg.fx==1){
 
  for(i=0;i<20;i++){
    if(icfg.fx_a==1)
      draw_rle_sprite(actual,data[ASTA01_BMP+astt[i]*15+asto[i]].dat,astx[i],asty[i]);
    else
      putpixel(actual,astx[i],asty[i],makecol(255,255,255));
  }
 
  }
 
  for(i=0;i<num_dole;i++){
    draw_rle_sprite(actual,data[ROCKET_BMP].dat,s_dole[i].x-8,s_dole[i].y);
  }
  for(i=0;i<num_hore;i++){
    draw_rle_sprite(actual,data[ROCKET_BMP].dat,s_hore[i].x-8,s_hore[i].y);
  }
 
  for(y=0;y<6;y++){
    for(x=0;x<10;x++){
      if(alien[x][y]){
      draw_rle_sprite(actual,data[UFO0000_BMP+rot[x][y]].dat,
                          x*45+ax,y*45+ay);
      rot[x][y]+=howrot[x][y];
      if(rot[x][y]<0) rot[x][y]=31;
      if(rot[x][y]>31) rot[x][y]=0;
      }
    }
  }
 
  for(i=0;i<num_exp;i++){
    draw_sprite(actual,data[EXPL01_BMP+expl[i].speed].dat,expl[i].x-32,expl[i].y-32);
  }
 
  draw_rle_sprite(actual,data[ENGINE1_BMP+he].dat,hx-6,hy+20);
  draw_rle_sprite(actual,data[SHIP1_BMP+hs].dat,hx-40,hy-24);
  he++;
  if(he>=7) he=0;
}
 
char je_tam_ufo(short sx,short sy)
{
   if(sx<=ax) return 0;
   if(sx>=ax+450) return 0;
   if(sy<=ay) return 0;
   if(sy>=ay+270) return 0;
 
   return alien[(sx-ax)/45][(sy-ay)/45];
}
 
void find_lowest()
{
  char x,y,yy;
  short yl;
 
  lowest=-1;
 
  for(x=0;x<10;x++){
    yy=-1;
    for(y=0;y<6;y++)
      if(alien[x][y]==1) yy=y;
    if(yy>=0){
      yl=ay+yy*45+45;
      if(yl>lowest) lowest=yl;
    }
  }
 
}
 
void znic_ufo(short sx,short sy)
{
  alien[(sx-ax)/45][(sy-ay)/45]=0;
  play_sample(data[BOOM_SPL].dat,255,sx*0.39,1000,0);
  user.points+=5;
}
 
void zrazil_sa()
{
  char ano=0;
 
  if(je_tam_ufo(hx-40,hy-25)){
    znic_ufo(hx-40,hy-25);
    ano=1;
  }
 
  if(je_tam_ufo(hx+40,hy-25)){
    znic_ufo(hx+40,hy-25);
    ano=1;
  }
  if(je_tam_ufo(hx-40,hy+25)){
    znic_ufo(hx-40,hy+25);
    ano=1;
  }
  if(je_tam_ufo(hx+40,hy+25)){
    znic_ufo(hx+40,hy+25);
    ano=1;
  }
  if(ano==1){
        expl[num_exp].x=hx;
        expl[num_exp].y=hy;
        expl[num_exp].speed=0;
        num_exp++;
        play_sample(data[BOOM_SPL].dat,255,hx*0.39,1000,0);
        zabili_ma();
  }
 
}
 
void move_all(char level)
{
  char nap,i;
  nap=ap;
 
  now++;
 
  if(icfg.fx==1){
  for(i=0;i<20;i++){
    asty[i]+=5;
    asto[i]+=astto[i];
    if(asto[i]>=15) asto[i]=0;
    if(asto[i]<0) asto[i]=14;
    if(asty[i]>=480){
      astx[i]=random()%640;
      asty[i]=-80;
      astto[i]=random()%2?1:-1;
      astt[i]=random()%3;
    }
  }
  }
 
  if(ap==0){
    ax+=(level+1);
    if(ax>=180) nap=1;
  }
  if(ap==1){
    ay+=(level+1)+16;
    find_lowest();
    nap=2;
  }
  if(ap==2){
    ax-=(level+1);
    if(ax<=10) nap=3;
  }
  if(ap==3){
    ay+=(level+1)+16;
    find_lowest();
    nap=0;
  }
 
  ap=nap;
 
  for(i=0;i<num_dole;i++){
    s_dole[i].y+=s_dole[i].speed;
    if(s_dole[i].speed<4)
      s_dole[i].speed*=2;
 
    if(abs(s_dole[i].x-hx)<30&&abs(s_dole[i].y-hy)<24&&hy<=450){
      s_dole[i]=s_dole[num_dole-1];
      i--;
      num_dole--;
      expl[num_exp].x=hx;
      expl[num_exp].y=hy;
      expl[num_exp].speed=0;
      num_exp++;
 
      play_sample(data[BOOM_SPL].dat,255,hx*0.39,1000,0);
      zabili_ma();
    }
 
    if(s_dole[i].y>480){
      s_dole[i]=s_dole[num_dole-1];
      i--;
      num_dole--;
    }
 
  }
 
  for(i=0;i<num_hore;i++){
    s_hore[i].y+=s_hore[i].speed;
    if(s_hore[i].speed>-8)
      s_hore[i].speed*=2;
 
    if(je_tam_ufo(s_hore[i].x,s_hore[i].y)){
      znic_ufo(s_hore[i].x,s_hore[i].y);
 
      expl[num_exp].x=s_hore[i].x;
      expl[num_exp].y=s_hore[i].y-22;
      expl[num_exp].speed=0;
      num_exp++;
 
      s_hore[i]=s_hore[num_hore-1];
      num_hore--;
      i--;
    }
 
    if(s_hore[i].y<0){
      s_hore[i]=s_hore[num_hore-1];
      num_hore--;
      i--;
    }
  }
 
  for(i=0;i<num_exp;i++){
    expl[i].speed++;
    if(expl[i].speed>=10){
      expl[i]=expl[num_exp-1];
      i--;
      num_exp--;
    }
  }
 
}
 
void pusti_dole()
{
  char x,y,yy,a=0;
 
  for(x=0;x<10;x++){
    yy=-1;
    for(y=0;y<6;y++)
      if(alien[x][y]==1) yy=y;
    if(yy>=0){
      s_dole[num_dole].x=ax+x*45+22;
      s_dole[num_dole].y=ay+yy*45+25;
      s_dole[num_dole].speed=1;
      num_dole++;
      a=1;
    }
  }
  if(a==0&&num_dole==0) end=1;
//  play_sample(data[BER_SPL].dat,255,128,1000,0);
 
}
 
 
char game(char level)
{
  char x,y,i;
  short pusti=0,how_pusti;
  char strela=0;
 
  ax=10;
  ay=5;
 
  hx=320;
  hy=520;
  ap=0;
  end=0;
 
  lowest=0;
 
  num_dole=0;
  num_hore=0;
 
  for(x=0;x<10;x++)
  for(y=0;y<6;y++){
    howrot[x][y]=(random()%6-3);
    if(howrot[x][y]==0) howrot[x][y]=1;
  }
 
  for(i=0;i<20;i++){
    astx[i]=random()%640;
    asty[i]=random()%480;
    asto[i]=0;
    astt[i]=random()%3;
    astto[i]=random()%2?1:-1;
  }
 
  memset(alien,1,60);
 
  how_pusti=100-level*5;
  if(how_pusti<10) how_pusti=10;
 
  while(1){
    if(tim>2){
      tim=0;
      pusti++;
      if(pusti>how_pusti){
        pusti_dole();
        pusti=0;
      }
      move_all(level);
 
      if(icfg.back==1)
        blit(background,actual,0,0,0,0,640,480);
      else
        clear(actual);
 
      urob_screen();
      blit(actual,screen,0,0,0,0,640,480);
 
      if(end==0){
 
      if(lowest==-1){
        num_dole=0;
        num_hore=0;
        end=1;
      }
 
      if(hy<=450){
      if(lowest>=480){
        expl[num_exp].x=hx;
        expl[num_exp].y=hy;
        expl[num_exp].speed=0;
        num_exp++;
        play_sample(data[BOOM_SPL].dat,255,hx*0.39,1000,0);
        zabili_ma();
      }
 
      if(key[KEY_LEFT]&&hx>45){
        hx-=10;
        hs-=2;
        if(hs<-1) hs=-1;
      }
      if(key[KEY_RIGHT]&&hx<595){
        hx+=10;
        hs+=2;
        if(hs>5) hs=5;
      }
      if(key[KEY_UP]&&hy>30){
        hy-=10;
      }
      if(key[KEY_DOWN]){
        hy+=10;
        if(hy>450) hy=450;
      }
      if(key[KEY_SPACE]&&strela==0){
        s_hore[num_hore].x=hx;
        s_hore[num_hore].y=hy-24;
        s_hore[num_hore].speed=-1;
        num_hore++;
        strela=5;
        play_sample(data[SHOT_SPL].dat,255,hx*0.39,1000,0);
      }
 
      zrazil_sa();
 
      if(strela>0) strela--;
 
      }
 
      if(hy>450){
        if(user.lives==-1) return 0;
        hy-=4;
      }
 
      }
      else{
        if(hx>330){
          hx-=10;
          hs-=2;
          if(hs<-1) hs=-1;
        }
        if(hx<310){
          hx+=10;
          hs+=2;
          if(hs>5) hs=5;
        }
 
 
        hy-=end;
        end+=2;
        if(hy<-20){
          user.points*=(level+1);
          return 1;
        }
      }
      if(hs<2) hs++;
      if(hs>2) hs--;
 
    }
 
 
    if(key[KEY_ESC]) return -1;
  }
}