#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include "allegro.h"
//#include "jgmod.h"
#include "side.h"


#include "aldumb.h"

#define width   300		//number of tiles wide  (max 500)
#define heigth	200		//number of tiles high  (max 500)

#define tile_w  30
#define tile_h  30

//JGMOD *song1, *song2, *song3, *song4, *song5 ,*song_menu, *song_end;

DUH *song1, *song2, *song3, *song4, *song5 ,*song_menu, *song_end, *current_song;
AL_DUH_PLAYER *mod_player=NULL;

/***************************GROUND TYPES************************************/
//the higher the number the less slippery the ground:
#define ice_ground	.1
#define norm_ground	.4
/***************************************************************************/

void startup(void);
void one_loop(void);
void go_sec(void);

void loader(void);
void draw_tile(BITMAP*bmp,int x,int y,int type, int la);
int get_no(int x, int y);
void draw_map(void);
int diff_x(int x);
int detect(int x, int y);

void draw_bad(int n);
void move_bad(int n);
void detect_bad(int n);

void draw_item(int n);
void detect_item(int n);

void draw_tel(int n);

void counters(void);

int move_right(void);
int move_left(void);
int move_up(void);
int move_down(void);

void menu1(void);
void set_game(void);

void loose(void);
void loose_all(void);
void win(void);
void win_all(void);
int check_col(BITMAP *b1, BITMAP *b2,float x1,float y1,float x2,float y2);

void play_song(DUH *new_song);
void load_scores(void);
void write_scores(void);
void save_scores(void);
void sort_scores(void);

char* get_string(void);
char* get_letter(void);

void how_to_loop(void);
int logo_move(void);

void credits(int win);

volatile int sec;
int cycles=0;
int esc=0;
int out=0;
int gravity=6;
int start_x=0,start_y=-0;

int logo_on=1;

int cc=0, cc2=0;

int number_of_bad=0;
int number_of_items=0;
int number_of_tele=0;

int level=1, old_level=0;

int skip_menu=0;
int menu_on=1;
int level_c;

float stretch=9;
float speed=-.1;
float count=5;

typedef struct hi//hi_score
	{
     char name[20];
     int score;
     int level;
    } hi;//hi_score[11];
//hi_score;

struct p
	{
     float x,y;
     int dir;
     float x_speed, y_speed, accel, slow;
     int jumped, j_count, j_high;
     float max_speed;
     BITMAP *pic;

     float left_x,left_y,right_x,right_y,top_x,top_y,bottom_x,bottom_y;

     int pic_num, frame_wait, num_of_frames;
     int life;
     int after_hit, after_hit_count, after_hit_count_max;
     int score;
     int items;
     int fries;
     int lives;
     BITMAP *pics[10];

    } p;

struct
	{
     int x1,y1;
     int x2,y2;
     int num;
    } tel[20];

struct
	{
     int x,y;
     int type;

     int left_x,right_x,dir;
     int angle;

     BITMAP *pic;
    } bad[100];

struct
      {
      int x,y;
      int type;
      } item[500];

//map vars
typedef struct t
	{
     int type, type2, type3;
     int walk;
    } t;

FILE *fp;
char name[20];
float screen_x=0, screen_y=0;
t tile[width*heigth+width+heigth];
//end map vars

int temp=0;

int how_to_flag=1;

hi hi_score[11];

BITMAP *backgr, *walk, *layer1, *layer2, *shot, *over;
DATAFILE *data;

    PALLETE ppal;
    BITMAP *ppic;
  SAMPLE *sound;

void go_sec()
	{
    sec++;
	}
END_OF_FUNCTION(go_sec);

int main(void)
	{
	int n=100;
     startup();

    ppic=load_bitmap("logo.bmp",ppal);

    sound= load_sample("thund.wav");
    set_pallete(ppal);
    
    
    
    rectfill(screen,0,0,640,480,0);
    fade_out(255);
    draw_sprite(screen,ppic,320-ppic->w/2,240-ppic->h/2);
    
    
    fade_in(ppal,1);
    
    //blit(ppic,screen,320-ppic->w/2,240-ppic->h/2,0,0,0,0);
    //while(n>100){n--;}
    sec=0; cycles=0;
    while(cycles<100)
    {
    while(sec>cycles)     //regulates time
    {
        cycles++;
    }
    }
    
    fade_out(1);
    
    /* while(logo_on==1)
        {
         while(sec>cycles && logo_on==1)
            {
            if(logo_move()==1)
                logo_on=0;
            }

        }*/
     //fade_out(1);

     while (out==0)
      {
       if(skip_menu==0 && temp==0)
       	 menu1();
       else if(temp==0)
       {
         set_game();
         skip_menu=0;
         esc=0;
       }

       temp=0;
       if(menu_on==0)
       {
       while(esc==0)  //when not trying to leave game
         {
          while(sec>cycles*.7 && esc==0)     //regulates time
            {
            if(how_to_flag==0)
            	one_loop();
            else
                how_to_loop();
            
            if(temp==1)
                break;
            }
            if(temp==1)
                break;
            
         }
 
         }
      }
      
      if(mod_player)
      {
        al_stop_duh(mod_player);
        mod_player=NULL;
      }
     //fade_out(1);
    }
END_OF_MAIN();

void startup(void)
     {
     char * noise;
     int x,y,n;
     allegro_init();
     install_timer();
     install_keyboard();
     install_mouse();
     set_gfx_mode(GFX_AUTODETECT,640,480,0,0);

     atexit(&dumb_exit);
     dumb_register_stdfiles();

     //reserve_voices(20,-1);
     if(install_sound(DIGI_AUTODETECT,MIDI_NONE,NULL)<0)
     {
     exit(0);
     }
/*
     if(install_mod(16)<0)
     {
     exit(0);
     }
*/
     /*song1=load_mod("music2/TST.xm");
     song2=load_mod("music2/Forest.xm");
     song3=load_mod("music2/FotF.xm");
     song4=load_mod("music2/TE.xm");
     song5=load_mod("music2/IP.xm");
     song_menu=load_mod("music2/TCV.xm");    //Rtts
     song_end=load_mod("music2/ATB.xm");*/
     
     song1= dumb_load_xm("music2/TST.xm");
     song2= dumb_load_xm("music2/Forest.xm");
     song3= dumb_load_xm("music2/FotF.xm");
     song4= dumb_load_xm("music2/TE.xm");
     song5= dumb_load_xm("music2/IP.xm");
     song_menu= dumb_load_xm("music2/TCV.xm");
     song_end= dumb_load_xm("music2/ATB.xm");
     

     srand(time(0));

     data=load_datafile("side.dat");
     if(!data)
     {
     exit(0);
     }
    // set_mod_volume(190);
     
     set_pallete(data[pal].dat);

     backgr=create_bitmap(SCREEN_W,SCREEN_H);

     walk=create_bitmap(SCREEN_W,SCREEN_H);
     layer1=create_bitmap(SCREEN_W,SCREEN_H);
     layer2=create_bitmap(SCREEN_W,SCREEN_H);
     over=create_bitmap(640,480);
     rectfill(over,0,0,640,480,40);
     //layer3=create_bitmap(SCREEN_W,SCREEN_H);

     LOCK_VARIABLE(sec);
     LOCK_FUNCTION(go_sec);

     text_mode(-1);

     if(( color_map = malloc( sizeof( COLOR_MAP) )) == NULL)
        {
                printf("Unable to allocate colour map!\n");
                exit( 1);
        }
     create_trans_table(color_map,data[pal].dat, 70, 70, 70, NULL);//168

     install_int_ex(go_sec, BPS_TO_TIMER(38));
     }

void how_to_loop(void)
	{
     int n=0,n2=0,nn, go_x=0,go_y=0;

     int on_right=0 , on_left=0 , on_up=0;

     cycles++;

     if(p.frame_wait<1)
       p.frame_wait=5;

    rectfill(backgr,0,0,SCREEN_W,SCREEN_H,level_c);//239
    rectfill(walk,0,0,SCREEN_W,SCREEN_H,0);
    rectfill(layer1,0,0,SCREEN_W,SCREEN_H,level_c); //239
    rectfill(layer2,0,0,SCREEN_W,SCREEN_H,0);
    //rectfill(layer3,0,0,SCREEN_W,SCREEN_H,0);
    draw_map();
    blit(layer1,backgr,0,0,0,0,SCREEN_W,SCREEN_H);


    if(p.life<0)
      {
      //esc=1;
       loose();
      }
    for(n=0;n<number_of_bad;n++)
       {
        draw_bad(n);
        move_bad(n);
        detect_bad(n);
        }

    for(n=0;n<number_of_items;n++)
    	{
         draw_item(n);
         detect_item(n);
         }

    for(n=0;n<number_of_tele;n++)
       {
        draw_tel(n);
       }

    counters();

    if(key[KEY_ESC])
    {
       esc=1;
       if(mod_player)
       {
        al_stop_duh(mod_player);
        mod_player=NULL;
        }
    }
//gives the commands to the game to play through the demonstration//////////
    rectfill(backgr,10,90,630,130,0);

    if(cycles>50 && cycles<130)
    	textprintf_centre(backgr,data[f2].dat,320,100,80,"Use the left and right arrow keys to walk");
    if(cycles>50 && cycles<75)
       on_right=1; //walk right
    else if(cycles>75 && cycles<130)
       on_left=1;  //walk left

    if(cycles>140 && cycles<210)
      textprintf_centre(backgr,data[f2].dat,320,100,80,"Use the up arrow to jump");
    if(cycles>160 && cycles<200)
      on_up=1;    //jump

    if(cycles>240 && cycles<340)
      textprintf_centre(backgr,data[f2].dat,320,100,80,"Collect items for points. Get 100 items for a life up");
    if(cycles>260 && cycles<300)
      on_right=1;

    if(cycles>360 && cycles<490)
      textprintf_centre(backgr,data[f2].dat,320,100,80,"Collect enough fries, then step on the exit to beat the level");
 
    if(cycles>410 && cycles<420)
      on_up=1;

    if(cycles>500 && cycles<610)
      textprintf_centre(backgr,data[f2].dat,320,100,80,"Watch out for enemies, touching them will hurt you!");
    if(cycles>510 && cycles <540)
      on_right=1;
    else if(cycles>540 && cycles<670)
      on_left=1;

    if(cycles >640 && cycles<700)
      textprintf_centre(backgr,data[f2].dat,320,100,80,"Step on a teleporter pad to get transported");
    if(cycles>660 && cycles<670)
      on_up=1;

    if(cycles >710 && cycles <780)
      textprintf_centre(backgr,data[f2].dat,320,100,80,"Some tiles are fake");
    if(cycles >720 && cycles <760)
      on_right=1;

    if(cycles>790 && cycles<900)
       textprintf_centre(backgr,data[f2].dat,320,100,80,"A Jump pad will make you jump really high");
    if(cycles >800 && cycles<815)
      {
      on_right=1;
      on_up=1;
      }
    else if(cycles>=820 && cycles<835)
      on_right=1;
    else if(cycles>840 && cycles<890)
      {
      on_up=1;
      on_left=1;
      }
    if(cycles >910 && cycles<1000)
       textprintf_centre(backgr,data[f2].dat,320,100,80,"Be careful, spikes will finish you off instantly!!");
    if((cycles >910 && cycles<930) || (cycles>945 && cycles<1000) )
       on_left=1;
///////////////////////////////////////////////////////////////////////////



    if(on_right==1 &&  p.x_speed<p.max_speed)
      {
      p.x_speed+=p.accel;
      if(p.x_speed<-6 && p.jumped==0)
         play_sample(data[slide].dat,255,128,1000,FALSE);
      p.dir=1;

      if(p.jumped==0)
        {
      	if (p.frame_wait==5)
        	p.pic_num++;
      	p.frame_wait--;
        }
      }
    else if(on_left==1 && p.x_speed>-p.max_speed)
      {
      p.x_speed-=p.accel;
      if(p.x_speed>6 && p.jumped==0)
         play_sample(data[slide].dat,255,128,1000,FALSE);

      p.dir=2;

      if(p.jumped==0)
        {
      	if (p.frame_wait==5)
        	p.pic_num++;
      	p.frame_wait--;
        }
      }
    else
      {
       if(p.x_speed<p.slow && p.x_speed>0)
         p.x_speed=0;
       else if(p.x_speed>-p.slow && p.x_speed<0)
         p.x_speed=0;

       if(p.x_speed>0)
         p.x_speed-=p.slow;
       else if(p.x_speed<0)
         p.x_speed+=p.slow;
      }


    if(on_up==1 && p.jumped==0)
      {
      p.jumped=1;

      if(getpixel(walk,p.bottom_x+1,p.bottom_y)==254)
        {
        play_sample(data[boing].dat,255,128,1000,FALSE);
		p.y_speed= - p.j_high*2;
        }
      else
        p.y_speed= - p.j_high;

      }
    else if (p.y_speed<gravity)
      p.y_speed++;

    if(on_up!=1 && p.y_speed<-2)
      p.y_speed+=2;


     p.right_x=p.x+p.pic->w-screen_x;
     p.right_y=p.y+p.pic->h/2-screen_y;

     p.left_x=p.x-screen_x;
     p.left_y=p.y+p.pic->h/2-screen_y;

     p.bottom_x=p.x-screen_x+ p.pic->w/2;
     p.bottom_y=p.y-screen_y+p.pic->h;

     p.top_x=p.x-screen_x+ p.pic->w/2;
     p.top_y=p.y-screen_y;

    if(p.x_speed>0)
		go_x=move_right();
    else if(p.x_speed<0)
    	go_x=move_left();
    p.x+=go_x;
    screen_x+=go_x;

    if(p.y_speed>0)
    	go_y=move_down();
    else if(p.y_speed<0)
    	go_y=move_up();


    p.y+=go_y;
    screen_y+=go_y;

    if(p.pic_num>p.num_of_frames-1)
      p.pic_num=0;

    p.pic=p.pics[p.pic_num];

    if(p.dir==1)
        draw_sprite(backgr,p.pic,SCREEN_W/2,SCREEN_H/2);
    else if(p.dir==2)
        draw_sprite_h_flip(backgr,p.pic,SCREEN_W/2,SCREEN_H/2);

    if(p.after_hit_count%3==2 && p.dir==1)
        draw_sprite(backgr,data[k_flash].dat,SCREEN_W/2,SCREEN_H/2);
    else if(p.after_hit_count%3==2 && p.dir==2)
        draw_sprite_h_flip(backgr,data[k_flash].dat,SCREEN_W/2,SCREEN_H/2);

    if(p.items<=0)
      {
      p.lives++;
      p.items=70;
      }

    draw_sprite(backgr,layer2,0,0);

    for(n=0;n<p.life;n++)
       draw_sprite(backgr,data[heart].dat,20+n*30,5);

    textprintf(backgr,data[f1].dat,20,30,110,"Lives left: %d",p.lives);

    textprintf_centre(backgr,data[f1].dat,320,0,75,"Score: %d",p.score);
    textprintf_centre(backgr,data[f1].dat,320,60,80,"%d",p.items);
    textprintf_centre(backgr,data[f1].dat,320,30,80,"Fries Left: %d",p.fries);


    if(esc==0)
      {
    /*	if(!key[KEY_Q])
          {
           //draw_trans_sprite(backgr,over,0,0);
    		blit(backgr,screen,0,0,0,0,SCREEN_W,SCREEN_H);
          }
    	else*/
        	blit(walk,screen,0,0,0,0,SCREEN_W,SCREEN_H);

      }
/*
    if(key[KEY_Z])
      {
       win();
      // win_all();
       level++;
       set_game();
      }*/
   }

void one_loop(void)
	{
     int n=0,n2=0,nn, go_x=0,go_y=0;

     cycles++;

    if(mod_player)
    al_poll_duh(mod_player);

     if(p.frame_wait<1)
       p.frame_wait=5;

    rectfill(backgr,0,0,SCREEN_W,SCREEN_H,level_c);//239
    rectfill(walk,0,0,SCREEN_W,SCREEN_H,0);
    rectfill(layer1,0,0,SCREEN_W,SCREEN_H,level_c); //239
    rectfill(layer2,0,0,SCREEN_W,SCREEN_H,0);
    //rectfill(layer3,0,0,SCREEN_W,SCREEN_H,0);
    draw_map();
    blit(layer1,backgr,0,0,0,0,SCREEN_W,SCREEN_H);

    p.pic=p.pics[p.pic_num];

    if(p.life<0)
      {
      //esc=1;
       loose();
      }
    for(n=0;n<number_of_bad;n++)
       {
        draw_bad(n);
        move_bad(n);
        detect_bad(n);
        }

    for(n=0;n<number_of_items;n++)
    	{
         draw_item(n);
         detect_item(n);
         }

    for(n=0;n<number_of_tele;n++)
       {
        draw_tel(n);
       }

    counters();

if(mod_player)
al_poll_duh(mod_player);

    if(key[KEY_ESC])
    {
       esc=1;
       if(mod_player)
       {
        al_stop_duh(mod_player);
        mod_player=NULL;
       }
    }
    
    if(key[KEY_W])
      {
       shot=create_sub_bitmap(screen,0,0,SCREEN_W,SCREEN_H);
       save_bitmap("screenshot.pcx",shot,(RGB*)data[pal].dat);
       destroy_bitmap(shot);
      }

    if(key[KEY_RIGHT] &&  p.x_speed<p.max_speed)
      {
      p.x_speed+=p.accel;
      if(p.x_speed<-6 && p.jumped==0)
         play_sample(data[slide].dat,255,128,1000,FALSE);
      p.dir=1;

      if(p.jumped==0)
        {
      	if (p.frame_wait==5)
        	p.pic_num++;
      	p.frame_wait--;
        }
      }
    else if(key[KEY_LEFT] && p.x_speed>-p.max_speed)
      {
      p.x_speed-=p.accel;
      if(p.x_speed>6 && p.jumped==0)
         play_sample(data[slide].dat,255,128,1000,FALSE);

      p.dir=2;

      if(p.jumped==0)
        {
      	if (p.frame_wait==5)
        	p.pic_num++;
      	p.frame_wait--;
        }
      }
    else
      {
       if(p.x_speed<p.slow && p.x_speed>0)
         p.x_speed=0;
       else if(p.x_speed>-p.slow && p.x_speed<0)
         p.x_speed=0;

       if(p.x_speed>0)
         p.x_speed-=p.slow;
       else if(p.x_speed<0)
         p.x_speed+=p.slow;
      }


    if(key[KEY_UP] && p.jumped==0)
      {
      p.jumped=1;

      if(getpixel(walk,p.bottom_x+1,p.bottom_y)==254)
        {
        play_sample(data[boing].dat,255,128,1000,FALSE);
		p.y_speed= - p.j_high*2;
        }
      else
        p.y_speed= - p.j_high;

      }
    else if (p.y_speed<gravity)
      p.y_speed++;

    if(!key[KEY_UP] && p.y_speed<-2)
      p.y_speed+=2;



     p.right_x=p.x+p.pic->w-screen_x;
     p.right_y=p.y+p.pic->h/2-screen_y;

     p.left_x=p.x-screen_x;
     p.left_y=p.y+p.pic->h/2-screen_y;

     p.bottom_x=p.x-screen_x+ p.pic->w/2;
     p.bottom_y=p.y-screen_y+p.pic->h;

     p.top_x=p.x-screen_x+ p.pic->w/2;
     p.top_y=p.y-screen_y;

    if(p.x_speed>0)
		go_x=move_right();
    else if(p.x_speed<0)
    	go_x=move_left();
    p.x+=go_x;
    screen_x+=go_x;

    if(p.y_speed>0)
    	go_y=move_down();
    else if(p.y_speed<0)
    	go_y=move_up();


    p.y+=go_y;
    screen_y+=go_y;

    if(p.pic_num>p.num_of_frames-1)
      p.pic_num=0;



    if(p.dir==1)
        draw_sprite(backgr,p.pic,SCREEN_W/2,SCREEN_H/2);
    else if(p.dir==2)
        draw_sprite_h_flip(backgr,p.pic,SCREEN_W/2,SCREEN_H/2);

    if(p.after_hit_count%3==2 && p.dir==1)
        draw_sprite(backgr,data[k_flash].dat,SCREEN_W/2,SCREEN_H/2);
    else if(p.after_hit_count%3==2 && p.dir==2)
        draw_sprite_h_flip(backgr,data[k_flash].dat,SCREEN_W/2,SCREEN_H/2);

    if(p.items<=0)
      {
      p.lives++;
      p.items=70;
      }

    draw_sprite(backgr,layer2,0,0);

    for(n=0;n<p.life;n++)
       draw_sprite(backgr,data[heart].dat,20+n*30,5);

    textprintf(backgr,data[f1].dat,20,30,110,"Lives left: %d",p.lives);

    textprintf_centre(backgr,data[f1].dat,320,0,75,"Score: %d",p.score);
    textprintf_centre(backgr,data[f1].dat,320,60,80,"%d",p.items);
    textprintf_centre(backgr,data[f1].dat,320,30,80,"Fries Left: %d",p.fries);

    if(esc==0)
      {
    	if(!key[KEY_Q])
          {
           //draw_trans_sprite(backgr,over,0,0);
    		blit(backgr,screen,0,0,0,0,SCREEN_W,SCREEN_H);
          }
    	else
        	blit(walk,screen,0,0,0,0,SCREEN_W,SCREEN_H);

      }

    if(key[KEY_Z])
      {
       win();
      // win_all();
       level++;
       set_game();
      }
}

void loader(void)
	{
     int x,y,no=0;
     //clear(screen);

     fp=fopen(name,"r");
     if(!fp)
     {
     exit(0);
     }

     fscanf(fp,"%d",&number_of_bad);
     fscanf(fp,"%d",&number_of_items);
     fscanf(fp,"%d",&number_of_tele);

     fscanf(fp,"%d",&p.fries);

     for(x=0;x<width;x++)
        for(y=0;y<heigth;y++)
        	{
            fscanf(fp,"%d",&tile[no].type);
            //fscanf(fp,"%d",&tile[no].walk);

            fscanf(fp,"%d ",&tile[no].type2);
            //fscanf(fp,"%d ",&tile[no].type3);
            no++;
        	}
        fscanf(fp,"%d ",&start_x);
        fscanf(fp,"%d ",&start_y);

        for(x=0;x<number_of_bad;x++)
           {
            fscanf(fp,"%d ",&bad[x].x);
            fscanf(fp,"%d ",&bad[x].y);
            fscanf(fp,"%d ",&bad[x].type);
            fscanf(fp,"%d ",&bad[x].left_x);
            fscanf(fp,"%d ",&bad[x].right_x);
           }

        for(x=0;x<number_of_items;x++)
		   {
            fscanf(fp,"%d ",&item[x].x);
            fscanf(fp,"%d ",&item[x].y);
            fscanf(fp,"%d ",&item[x].type);
           }

        for(x=0;x<number_of_tele;x++)
           {
            fscanf(fp,"%d ",&tel[x].x1);
            fscanf(fp,"%d ",&tel[x].y1);
            fscanf(fp,"%d ",&tel[x].x2);
            fscanf(fp,"%d ",&tel[x].y2);
           }
      fclose(fp);
	}

void draw_map(void)
	{
     int xx,yy,n;
     draw_sprite(layer1,data[cloud2].dat,1000-screen_x-cycles/4,-300-screen_y);
     for(yy=screen_y-(tile_h*2) ; yy<screen_y+SCREEN_H+tile_h ; yy+=tile_h)
     {
      if(yy>-tile_h)
       {
       for(xx=screen_x-(tile_w*2) ; xx<screen_x+SCREEN_W+tile_w ; xx+=tile_w)
          {
          n=get_no(xx,yy);

          if(xx<width*tile_w && yy<heigth*tile_h)
            {
          	draw_tile(layer1,xx-diff_x(xx),yy-diff_x(yy),tile[n].type,1);
          	draw_tile(layer2,xx-diff_x(xx),yy-diff_x(yy),tile[n].type2,2);
          	//draw_tile(layer3,xx-diff_x(xx),yy-diff_x(yy),tile[n].type3,3);
            }
          }
       }
       }
    }

int diff_x(int x)
	{
     int x2;

     x2=x;

     if(x2>=tile_w)
     	{
        while(x2>=tile_w)
          x2-=tile_w;
        }
     return x2;
    }

void draw_tile(BITMAP*bmp,int x, int y, int type, int la)
	{
     if(type==1)
     	draw_sprite(bmp,data[g_mid].dat,x-screen_x,y-screen_y);
     else if(type==2)
        draw_sprite(bmp,data[cliff1].dat,x-screen_x,y-screen_y);
     else if(type==3)
     	draw_sprite(bmp,data[g_l_edge].dat,x-screen_x,y-screen_y);
     else if(type==4)
     	draw_sprite(bmp,data[g_r_edge].dat,x-screen_x,y-screen_y);
     else if(type==5)
     	draw_sprite(bmp,data[underground1].dat,x-screen_x,y-screen_y);
     else if(type==6)
     	draw_sprite(bmp,data[tree1].dat,x-screen_x,y-screen_y+7);
     else if(type==7)
     	draw_sprite(bmp,data[tree2].dat,x-screen_x,y-screen_y+7);
     else if(type==8)
     	draw_sprite(bmp,data[spikes1].dat,x-screen_x,y-screen_y);

     else if(type==9)
     	draw_sprite(bmp,data[arr_up].dat,x-screen_x,y-screen_y);
     else if(type==10)
     	draw_sprite(bmp,data[arr_right].dat,x-screen_x,y-screen_y);
     else if(type==11)
     	draw_sprite(bmp,data[arr_down].dat,x-screen_x,y-screen_y);
     else if(type==12)
     	draw_sprite(bmp,data[arr_left].dat,x-screen_x,y-screen_y);
     else if(type==13)
     	draw_sprite(bmp,data[exit1].dat,x-screen_x,y-screen_y);
     else if(type==14)
     	draw_sprite(bmp,data[spring].dat,x-screen_x,y-screen_y);
     else if(type==15)
     	draw_sprite(bmp,data[back_tile1].dat,x-screen_x,y-screen_y);
     else if(type==16)
     	draw_sprite(bmp,data[vine_tile1].dat,x-screen_x,y-screen_y);
     else if(type==17)
     	draw_sprite(bmp,data[vine_tile2].dat,x-screen_x,y-screen_y);

     if(la==1)
       {
     	if(type==1)
     		draw_sprite(walk,data[g_mid_walk].dat,x-screen_x,y-screen_y);
     	else if(type==2)
        	draw_sprite(walk,data[cliff1_walk].dat,x-screen_x,y-screen_y);

     	else if(type==3)
     		draw_sprite(walk,data[g_l_edge_walk].dat,x-screen_x,y-screen_y);
     	else if(type==4)
     		draw_sprite(walk,data[g_r_edge_walk].dat,x-screen_x,y-screen_y);
        else if(type==5)
     		draw_sprite(walk,data[underground1_walk].dat,x-screen_x,y-screen_y);
        else if(type==8)
     		draw_sprite(walk,data[spikes1_walk].dat,x-screen_x,y-screen_y);
        else if(type==13)
            draw_sprite(walk,data[exit1_walk].dat,x-screen_x,y-screen_y);
        else if(type==14)
            draw_sprite(walk,data[spring_walk].dat,x-screen_x,y-screen_y);
        }
    }

int get_no(int x, int y)
	{
     int no;
     x/=tile_w;
     y/=tile_h;
     x++; y++;

     no=x+(width*y)-width;

     return no;
   }

int detect(int x, int y)
	{
     if(getpixel(walk, x-screen_x+p.pic->w/2, y-screen_y+p.pic->h)==136)
     	return 1;
     else
        return 0;
     }

void draw_bad(int n)
	{
     if(bad[n].type==1)
          {
          rotate_sprite(backgr,data[tomato1].dat,bad[n].x-screen_x,bad[n].y-screen_y
                        ,itofix(bad[n].angle));
          }
     }

void move_bad(int n)
	{
     if(bad[n].type==1)
          {
          if(bad[n].dir==1)
            {
             if(bad[n].x<bad[n].right_x)
               bad[n].x+=2;
             else
               bad[n].dir=2;
             bad[n].angle+=10;
            }
          if(bad[n].dir==2)
            {

             if(bad[n].x>bad[n].left_x)
               bad[n].x-=2;
             else
               bad[n].dir=1;
               bad[n].angle-=10;
             }
          }
     }

void detect_bad(int n)
{
    if(bad[n].x-screen_x>0 && bad[n].x-screen_x<640 &&
        bad[n].y-screen_y>0 && bad[n].y-screen_y<480)
    {
        if(bad[n].type==1)
        {
            if (check_col(p.pic,data[tomato1].dat,p.x,p.y,bad[n].x,bad[n].y)==1
                && p.after_hit==FALSE)
            {
                p.after_hit=TRUE;
                p.life--;
                play_sample(data[hurt].dat,255,128,1000,FALSE);
                p.after_hit_count=p.after_hit_count_max;

            }
        }
    }
}

void draw_item(int n)
{
     if(item[n].type==1)
     	draw_sprite(backgr,data[rose1].dat,item[n].x-screen_x,item[n].y-screen_y);
     else if(item[n].type==2)
        draw_sprite(backgr,data[star1].dat,item[n].x-screen_x,item[n].y-screen_y);
     else if(item[n].type==3)
     	draw_sprite(backgr,data[cheese1].dat,item[n].x-screen_x,item[n].y-screen_y);
     else if(item[n].type==4)
     	draw_sprite(backgr,data[fry1].dat,item[n].x-screen_x,item[n].y-screen_y);
     else if(item[n].type==5)
     	draw_sprite(backgr,data[lett1].dat,item[n].x-screen_x,item[n].y-screen_y);
	 else if(item[n].type==6)
     	draw_sprite(backgr,data[onion1].dat,item[n].x-screen_x,item[n].y-screen_y);
     else if(item[n].type==7)
     	draw_sprite(backgr,data[cone1].dat,item[n].x-screen_x,item[n].y-screen_y);
     else if(item[n].type==8)
     	draw_sprite(backgr,data[melon1].dat,item[n].x-screen_x,item[n].y-screen_y);
	else if(item[n].type==9)
     	draw_sprite(backgr,data[pepper1].dat,item[n].x-screen_x,item[n].y-screen_y);
}

void detect_item(int n)
{
    if(item[n].x-screen_x>0 && item[n].x-screen_x<640 &&
       item[n].y-screen_y>0 && item[n].y-screen_y<480){
    if (check_col(p.pic,data[rose1].dat,p.x,p.y,item[n].x,item[n].y)==1
    	&& item[n].type==1)
    	{
        item[n].type=0;
        p.score+=50;
        p.items--;
        }

 	else if (check_col(p.pic,data[star1].dat,p.x,p.y,item[n].x,item[n].y)==1
             && item[n].type==2)
    	{
        item[n].type=0;
        p.after_hit=TRUE;
        p.after_hit_count=175;
        }

    else if (check_col(p.pic,data[cheese1].dat,p.x,p.y,item[n].x,item[n].y)==1
             && item[n].type==3)
        {
        item[n].type=0;
        p.score+=100;
        p.items-=2;
        }
    else if (check_col(p.pic,data[fry1].dat,p.x,p.y,item[n].x,item[n].y)==1
             && item[n].type==4)
        {
        item[n].type=0;
        p.score+=200;
        p.fries--;
        play_sample(data[rescue].dat,255,128,1000,FALSE);
        return;
        }
    else if (check_col(p.pic,data[lett1].dat,p.x,p.y,item[n].x,item[n].y)==1
             && item[n].type==5)
        {
        item[n].type=0;
        p.score+=150;
        p.items--;
        }
    else if (check_col(p.pic,data[onion1].dat,p.x,p.y,item[n].x,item[n].y)==1
             && item[n].type==6)
        {
        item[n].type=0;
        p.score+=200;
        p.items--;
        }
    else if (check_col(p.pic,data[onion1].dat,p.x,p.y,item[n].x,item[n].y)==1
             && item[n].type==7)
        {
        item[n].type=0;
        p.score+=250;
        p.items--;
        }
    else if (check_col(p.pic,data[onion1].dat,p.x,p.y,item[n].x,item[n].y)==1
             && item[n].type==8)
        {
        item[n].type=0;
        p.score+=300;
        p.items--;
        }
    else if (check_col(p.pic,data[onion1].dat,p.x,p.y,item[n].x,item[n].y)==1
             && item[n].type==9)
        {
        item[n].type=0;
        p.score+=350;
        p.items--;
        }
    else
      return;

    play_sample(data[collect].dat,255,128,1000,FALSE);
    }
}

void counters(void)
	{
    if(p.after_hit_count>0)
      p.after_hit_count--;
    else
      p.after_hit=FALSE;
    }

int move_right(void)
	{
     int n,n2,nn;
     int go_x=0;
          go_x=p.x_speed;
     nn=0;
     for(n=0;n<p.x_speed;n++)
     	{
        for(n2=-p.pic->h/2;n2<p.pic->h/2;n2++)
        	{
        	if(getpixel(walk,p.right_x+n,p.right_y+n2)==0)
          		{
                nn=n;
          		}
            else
            	{
            	go_x=nn;
            	p.x_speed=0;
                p.x--;
                screen_x--;
            	break;
            	}
            }

		}
     return go_x;
	}

int move_left(void)
	{
     int n,n2,nn;
     int go_x=0;
     go_x=p.x_speed;
     nn=0;
     for(n=0;n>p.x_speed;n--)
       		{
            for(n2=-p.pic->h/2;n2<p.pic->h/2;n2++)
               {
        		if(getpixel(walk,p.left_x+n,p.left_y+n2)==0)
          			{
                	nn=n;
          			}
            	else
              		{
              		go_x=nn;
              		p.x_speed=0;
                    p.x++;
                    screen_x++;
              		break;
              	}
               }

            }
      return go_x;
     }

int move_down(void)
	{
     int n,n2,nn;
     int go_y=0;
     go_y=p.y_speed;
     nn=0;
     if(p.y_speed>0)
      {
    	for(n=0;n<p.y_speed;n++)
       		{
            for(n2=-p.pic->w/2;n2<p.pic->w/2+1;n2++)
               {
                if(getpixel(walk,p.bottom_x+n2,p.bottom_y+n)==0)
          			{
                	nn=n;
          			}
                else if(getpixel(walk,p.bottom_x+-n2,p.bottom_y+n)==108 && p.after_hit==FALSE)
                    {
                     loose();
                     break;
                    }
                else if(getpixel(walk,p.bottom_x+-n2,p.bottom_y+n)==40 && p.fries==0)
                    {
                     //winning
                     win();
                     level++;
                     set_game();
                    }
            	else
              		{
                     go_y=nn;
                     p.y_speed=0;
              		 p.jumped=0;
             		 break;
              		}
                }
             }
        }
      return go_y;
      }

int move_up(void)
	{
    int n,n2,nn;
    int go_y=0;
    go_y=p.y_speed;
    nn=0;
    if(p.y_speed<0)
      {
       for(n=0;n>p.y_speed;n--)
       		{
            for(n2=-p.pic->w/2;n2<p.pic->w/2;n2++)
               {
                if(getpixel(walk,p.top_x+n2,p.top_y+n)==0)
          			{
                	nn=n;
                    //go_y=n;
          		}
            	else
              		{
                     go_y=nn;
                     p.y_speed=0;
             		 break;
              		}
              //  nn=n;
                }
             }

         }
       return go_y;
       }

void draw_tel(int n)
	{
     int d_x,d_y;
     if(tel[n].x1-screen_x>0 && tel[n].x1-screen_x<640 &&
        tel[n].y1-screen_y>0 && tel[n].y1-screen_y<480)
       {

     draw_sprite(backgr,data[tele1].dat,tel[n].x1-screen_x,tel[n].y1-screen_y);

     draw_sprite(walk,data[tele1_walk].dat,tel[n].x1-screen_x,tel[n].y1-screen_y);

    if (check_col(p.pic,data[tele1].dat,p.x,p.y+1,tel[n].x1,tel[n].y1)==1)
      	{

         d_x=-p.x+tel[n].x2;
         d_y=-p.y+tel[n].y2-p.pic->h;

         p.x=tel[n].x2;
         p.y=tel[n].y2-p.pic->h;

         screen_x+=d_x;
         screen_y+=d_y;
         }

         //go_y=n;
         }
     }

void menu1(void)
	{

     int ax=255,ay=250,hit=0, num=1,y;
     //set_mod_volume(256);
     play_song(song_menu);
     menu_on=1;
    //mod_player=al_start_duh(song_menu/*song*/, 2/*sterio*/, 0/**/,0.7f/*volume*/, 512/*buffer*/, 11025/*frequency*/);


     rectfill(backgr,0,0,SCREEN_W,SCREEN_H,0);

     fade_out(63);
     blit(data[title].dat,screen,0,0,0,0,640,480);
     fade_in(data[title_pal].dat,1);
//    set_pallete(data[title_pal].dat);
     clear_keybuf();
     for(y=-480;y<0;y+=2)
        {
        if(mod_player)
        al_poll_duh(mod_player);

        
        blit(data[title].dat,backgr,0,0,0,0,640,480);
        textprintf(backgr,font,0,450,10,"Music by: Gopher");
        textprintf(backgr,font,0,460,10,"Some graphics are form Ari Feldman's sprite lib.");
        textprintf(backgr,font,0,470,10,"Licence number: 216.183.128.2-978214690");
        textprintf(backgr,data[f3].dat,280-(y/2),250+y,154,"Play");
        textprintf(backgr,data[f3].dat,280+1-(y/2),250+1+y,0,"Play");

        textprintf(backgr,data[f3].dat,280+(y/2),300,154,"Training");
        textprintf(backgr,data[f3].dat,280+1+(y/2),300+1,0,"Training");

        textprintf(backgr,data[f3].dat,280-(y/2),350,154,"Scores");
        textprintf(backgr,data[f3].dat,280+1-(y/2),350+1,0,"Scores");

     	textprintf(backgr,data[f3].dat,280+(y/2),400+y,154,"Exit");
        textprintf(backgr,data[f3].dat,280+1+(y/2),400+1+y,0,"Exit");

        blit(backgr,screen,0,0,0,0,640,480);

        if(keypressed())
          break;
        }
     clear_keybuf();
     cycles=0; sec=0;
     while(hit==0)
     {
     while(sec>cycles*3 && hit==0)
     {
        cycles++;
        if(mod_player)
        al_poll_duh(mod_player);

        rectfill(backgr,0,0,SCREEN_W,SCREEN_H,239);
        blit(data[title].dat,backgr,0,0,0,0,640,480);
        textprintf(backgr,font,0,450,10,"Music by: Gopher");
        textprintf(backgr,font,0,460,10,"Some graphics are form Ari Feldman's sprite lib.");
        textprintf(backgr,font,0,470,10,"Licence number: 216.183.128.2-978214690");
     	textprintf(backgr,data[f3].dat,280,250,154,"Play");
        textprintf(backgr,data[f3].dat,280+1,250+1,0,"Play");

        textprintf(backgr,data[f3].dat,280,300,154,"Training");
        textprintf(backgr,data[f3].dat,280+1,300+1,0,"Training");

        textprintf(backgr,data[f3].dat,280,350,154,"Scores");
        textprintf(backgr,data[f3].dat,280+1,350+1,0,"Scores");

     	textprintf(backgr,data[f3].dat,280,400,154,"Exit");
        textprintf(backgr,data[f3].dat,280+1,400+1,0,"Exit");

     	draw_sprite(backgr,data[arrow_title].dat,ax,ay+10);

        if(mod_player)
        al_poll_duh(mod_player);
        if(key[KEY_DOWN] && num<4)
          {
           num++;
           ay+=50;
           play_sample(data[collect].dat,255,128,2000,FALSE);
          }
        else if(key[KEY_UP] && num>1)
          {
           num--;
           ay-=50;
           play_sample(data[collect].dat,255,128,2000,FALSE);
          }


        if(key[KEY_ENTER])
          {
          hit=1;
          play_sample(data[collect].dat,255,128,500,FALSE);
          }

        //rest(75);
        blit(backgr,screen,0,0,0,0,640,480);
       }
     }
     //set_mod_volume(190);
     if(mod_player)
     {
        al_stop_duh(mod_player);
        mod_player=NULL;
     }
     if(num==1)
       {
    //    stop_mod();
        fade_out(1);
        level=1;
        old_level=0;
        p.score=0;
        set_game();
        p.lives=3;
        esc=0;
        how_to_flag=0;
        
        menu_on=0;
        }
     else if(num==2)
       {
      //  stop_mod();
        fade_out(1);
        level=-1;
        old_level=0;
        p.score=0;
        set_game();
        p.lives=0;
        esc=0;
        how_to_flag=1;
        
        menu_on=0;
       }
     else if(num==3)
       {
        fade_out(1);
        load_scores();
        sort_scores();
        write_scores();
        esc=0;
        out=0;
        menu_on=1;
       }
     else if(num==4)
       {
       fade_out(1);
       esc=1;
       out=1;
       menu_on=0;
       }
     p.after_hit=FALSE;

     }

void set_game(void)
	{
     int n;
     
     if(mod_player)
     {
        al_stop_duh(mod_player);
        mod_player=NULL;
     }
     
     clear_keybuf();
     blit(data[water_drops].dat,screen,0,0,0,0,640,480);
     if(level!=6)
	   {
        if(level!=-1)
     	    textprintf_centre(screen,data[f2].dat,320,240,80,"Prepare for level %d",level);
        else
            textprintf_centre(screen,data[f2].dat,320,240,80,"Prepare for ketchup training 101");

        textprintf_centre(screen,font,320,270,90,"Hit the ENTER key");
     	fade_in(data[pal].dat,1);
     	while(!key[KEY_ENTER])
       		{}
     	fade_out(1);
        rectfill(screen,0,0,640,480,0);
        fade_in(data[pal].dat,64);
        set_pallete(data[pal].dat);
       }
     if(level==1)
       {
     	strcpy(name,"side5.map");
        level_c=239;
        //if(old_level!=level)
        	 play_song(song1);
        old_level=level;
       }
     else if(level==2)
       {
        strcpy(name,"side6.map");
        level_c=249;

        //if(old_level!=level)
        	 play_song(song2);
        old_level=level;
       }
     else if(level==3)
       {
        strcpy(name,"side10.map");
        level_c=254;
        //if(old_level!=level)
        	 play_song(song3);
        old_level=level;
       }
     else if(level==4)
       {
        strcpy(name,"side7.map");
        level_c=30;
        //if(old_level!=level)
        	 play_song(song4);
        old_level=level;
       }
     else if(level==5)
       {
        strcpy(name,"side11.map");
        level_c=0;
        //if(old_level!=level)
        	 play_song(song5);
        old_level=level;
       }
     else if(level==6)
       {
        level--;
        win_all();
        esc=1;
       }
     else if(level==-1)
       {
     	strcpy(name,"side1.map");
        level_c=239;
        //if(old_level!=level)
        	play_song(song1);
        old_level=level;
       }

    temp=1;
    menu_on=0;

     loader();
     start_x+=6;
     start_y-=6;
     p.x=SCREEN_W/2+start_x;
     p.y=SCREEN_H/2+start_y;
     p.x_speed=0;
     p.y_speed=0;
     p.accel=.25;
     p.slow=norm_ground;
     p.max_speed=7;
     p.dir=1;

     p.jumped=0;
     p.j_count=0;
     p.j_high=15;

     p.frame_wait=5;
     p.pic_num=0;
     p.num_of_frames=2;

     p.pics[0]=data[k_walk1].dat;
     p.pics[1]=data[k_walk2].dat;

     p.life=5;
     p.after_hit=FALSE;
     p.after_hit_count_max=50;
     p.after_hit_count=0;

     p.items=100;

     screen_x=0+start_x;
     screen_y=0+start_y;

     for(n=0;n<number_of_bad;n++)
    {
     	bad[n].dir=2;
     	bad[n].angle=0;
    }

     sec=0;
     cycles=0;
 }



void loose(void)
	{
     int temp_score= p.score;

    /*if(mod_player)
    {
        al_stop_duh(mod_player);
        mod_player=NULL;
    }*/
    al_pause_duh(mod_player);

     temp_score-=6000;

     if(temp_score < p.score/2)
       temp_score=p.score/2;

     if(p.lives>0)
       {
       p.lives--;
       do
         {
          if(p.score>temp_score+30)
          	p.score-=30;
          else
            p.score=temp_score;
          play_sample(data[collect].dat,255,128,1000,FALSE);
          //rectfill(backgr,0,0,640,480,0);
          blit(data[water_drops].dat,backgr,0,0,0,0,640,480);
          textprintf_centre(backgr,data[f2].dat,320,240,80,"You have lost. You now have %d lives left",p.lives);
          textprintf_centre(backgr,font,320,270,90,"Hit the ENTER key");
          textprintf_centre(backgr,data[f2].dat,320,200,80,"Score: %d",p.score);
          blit(backgr,screen,0,0,0,0,640,480);
         
         }
       while(p.score>temp_score && !key[KEY_ENTER]);

       while(!key[KEY_ENTER])
      	    {}
       esc=1;
       skip_menu=1;
       }
     else
       {
        if(how_to_flag==0)
        	loose_all();
        else
            fade_out(1);
        esc=1;
       }
     }

int check_col(BITMAP *b1, BITMAP *b2,float x1,float y1,float x2,float y2)
     {
      if(x1+b1->w>x2 && x1 < x2+b2->w
      && y1+b1->h>y2 && y1 < y2+b2->h)
         return 1;
      else
         return 0;
      }

void loose_all(void)
	{
     int n=480;
     char word[20];
     char *temp;

    if(mod_player)
    {
        al_stop_duh(mod_player);
        mod_player=NULL;
        }
    

     fade_out(1);
  //   stop_mod();
     blit(data[loose_pic].dat,screen,0,0,0,0,640,480);
     fade_in(data[loose_pal].dat,1);

     //set_pallete(data[loose_pal].dat);
     play_sample(data[loose_laugh].dat,255,128,1000,FALSE);
     while(!key[KEY_ENTER])
       {

     	blit(data[loose_pic].dat,backgr,0,0,0,0,640,480);
        draw_sprite(backgr,data[loose_text].dat,77,20+n);

        blit(backgr,screen,0,0,0,0,640,480);

        if(n>0)
          n--;
        if(n==0)
          textprintf_centre(screen,data[f2].dat,320,200,180,"Your final score was %d",p.score);
       }
     fade_out(1);

     play_song(song_end);

     load_scores();
     if(p.score>=hi_score[9].score)
       {
        temp=get_string();
     	strcpy(hi_score[9].name,temp);
        hi_score[9].score=p.score;
        hi_score[9].level=level;
       }
     sort_scores();
     save_scores();

     credits(0);

     write_scores();
	}

void win(void)
	{
     int hearts= p.life;
     int n;
     
     sec=0;
     cycles=0;
    
     /*if(mod_player)
     { 
        al_stop_duh(mod_player);
        mod_player=0;
     }*/
     al_pause_duh(mod_player);
      
     while(hearts>0)
       {
       while (sec>cycles*4 && hearts>0)
       {
        cycles++;
        hearts--;
        p.score+=1000;
        play_sample(data[collect].dat,255,128,1000,FALSE);
        play_sample(data[collect].dat,255,128,1000,FALSE);
     	//rectfill(backgr,0,0,640,480,0);
        blit(data[water_drops].dat,backgr,0,0,0,0,640,480);
     	for(n=0;n<hearts;n++)
       	    draw_sprite(backgr,data[heart].dat,250+n*30,180);
     	textprintf_centre(backgr,data[f2].dat,320,240,80,"Great job! You have beat level %d",level);
     	textprintf_centre(backgr,font,320,270,90,"Hit the ENTER key");
     	textprintf_centre(backgr,data[f2].dat,320,200,80,"Score: %d",p.score);
     	blit(backgr,screen,0,0,0,0,640,480);
        //rest(300);
        }
        }
       while(!key[KEY_ENTER])
         {}
}
void load_scores(void)
	{
     int x,y,no=0;
     //clear(screen);

     fp=fopen("highscores.hc","r");
     if(!fp)
     {
     exit(0);
     }

    for(no=0;no<10;no++)
    	{
     	fscanf(fp,"%s",hi_score[no].name);
     	fscanf(fp,"%d",&hi_score[no].score);
        fscanf(fp,"%d",&hi_score[no].level);
        }

     fclose(fp);
	}

void save_scores(void)
	{
     int x,y,no=0;

     fp=fopen("highscores.hc","w");
     if(!fp)
     {
     exit(0);
     }
    for(no=0;no<10;no++)
    	{
     	fprintf(fp,"%s\n",hi_score[no].name);
     	fprintf(fp,"%d\n",hi_score[no].score);
        fprintf(fp,"%d\n",hi_score[no].level);
        }
    }

void write_scores(void)
	{
     int n;
     int x=-250,y;
     //rectfill(backgr,0,0,640,480,0);
     blit(data[water_drops].dat,backgr,0,0,0,0,640,480);
     blit(backgr,screen,0,0,0,0,640,480);
     fade_in(data[pal].dat,1);
     for(y=-300;y<100;y+=2)
     	{
         blit(data[water_drops].dat,backgr,0,0,0,0,640,480);
         x+=2;
         textprintf(backgr,data[f1].dat,x,y-30,90,"%18s %8s %11s",
                    "Name","Score","Level");
     	 for(n=0;n<10;n++)
         	textprintf(backgr,data[f2].dat,x,y+n*30,80,"%20s %10d %10d",
                       hi_score[n].name,hi_score[n].score,hi_score[n].level);

        textprintf(backgr,data[f3].dat,x-130,y+100,90,"High Scores");
        textprintf(backgr,data[f3].dat,x-130+1,y+100+1,90,"High Scores");
        textprintf(backgr,data[f3].dat,x-130+2,y+100+2,80,"High Scores");

     	blit(backgr,screen,0,0,0,0,640,480);
     	}
     blit(backgr,screen,0,0,0,0,640,480);

     while(!key[KEY_ENTER])
       {/*al_poll_duh(mod_player);*/}
     fade_out(1);
	}

char* get_string(void)
	{
    static char word[20]="";

    DIALOG input_dlg[] = {
    	{ d_edit_proc , 300, 240, 100, 50, 90, 80, 0, D_EXIT, 20, 0, word},
        { NULL },
       };

    textprintf_centre(screen,data[f2].dat,320,100,80,"Please Enter Your Name");

    do_dialog(input_dlg,0);

    return word;
	}

void win_all(void)
	{
     char word[20];
     char *temp;

     play_song(song_end);

     load_scores();
     if(p.score>=hi_score[9].score)
       {
        temp=get_string();
     	strcpy(hi_score[9].name,temp);
        hi_score[9].score=p.score;
        hi_score[9].level=level;
       }
     sort_scores();
     save_scores();

     credits(1);

     write_scores();
    }

void sort_scores(void)
	{
     int x,y;
     hi temp;
     for(x=0;x<10;x++)
        {
         for(y=0;y<9;y++)
            {
             if(hi_score[y+1].score>hi_score[y].score)
               {
                temp = hi_score[y];
                hi_score[y]=hi_score[y+1];
                hi_score[y+1]=temp;
               }


             }
        }
     }

void credits(int win)
	{
     int y=482,n;
     clear_keybuf();

     blit(data[water_drops].dat,backgr,0,0,0,0,640,480);
     blit(backgr,screen,0,0,0,0,640,480);
     fade_in(data[pal].dat,1);

     if(win==1)
       {
	   textprintf_centre(screen,data[f3].dat,320,230,0,"YOU WIN!!!!");
       textprintf_centre(screen,data[f3].dat,321,231,80,"YOU WIN!!!!");
       textprintf_centre(screen,data[f3].dat,322,232,0,"YOU WIN!!!!");
       textprintf_centre(screen,data[f3].dat,323,233,80,"YOU WIN!!!!");
       rest(1000);
       }

     fade_out(1);
     blit(backgr,screen,0,0,0,0,640,480);
     fade_in(data[pal].dat,1);

     for(n=0;n<39;n++)
        {
        blit(data[water_drops].dat,backgr,0,0,0,0,640,480);
     	textprintf_centre(backgr,data[f2].dat,320,230,39-n,"Thank's for Playing!");
        blit(backgr,screen,0,0,0,0,640,480);
        rest(100);
        }
     for(n=38;n>0;n--)
        {
        blit(data[water_drops].dat,backgr,0,0,0,0,640,480);
     	textprintf_centre(backgr,data[f2].dat,320,230,39-n,"Thank's for Playing!");
        blit(backgr,screen,0,0,0,0,640,480);
        rest(100);
        }

     while(y>75)
       {
        blit(data[water_drops].dat,backgr,0,0,0,0,640,480);
        textprintf_centre(backgr,data[f1].dat,320,y,80,"Game By: Richard Cassan");
        textprintf_centre(backgr,data[f1].dat,320,y+70,80,"Music By: Kevin Chow (Gopher) www.traxinspace.com (search Gopher)");
        textprintf_centre(backgr,data[f1].dat,320,y+140,80,"Graphics By: Richard Cassan and Ari Feldman");
        textprintf_centre(backgr,data[f1].dat,320,y+170,80,"Sprite Lib Licence number: 216.183.128.2-978214690");
     	textprintf_centre(backgr,data[f1].dat,320,y+240,80,"This game was programmed using the Allegro Library");
        blit(backgr,screen,0,0,0,0,640,480);
        y-=2;
        rest(20);
       }
     if(win==1)
       {
     	for(n=0;n<39;n++)
        	{
     		textprintf_centre(backgr,data[f2].dat,320,400,39-n,"Congratulations");
        	blit(backgr,screen,0,0,0,0,640,480);
        	rest(100);
        	}
        }
      rest(3000);
      fade_out(1);
     }




int logo_move(void)
{


    rectfill(backgr,0,0,640,480,0);

    //static int sw=640,sh=480;
    //static float x=640/2,y=480/2;


    //

    if(count>0)
    	{
    	if(stretch>4.6)
    		speed-=.05;
    	else
      		speed+=.05;


     	stretch+=.05*speed;
    	}

    if(stretch>.9 && stretch<1)
    	{
        count--;
       	}

     stretch_blit(ppic,backgr,0,0,ppic->w,ppic->h,(int)(320-(ppic->w*stretch)/2),(int)(240-(ppic->h*stretch)/2),(int)(ppic->w*stretch),(int)(ppic->h*stretch));

     if(count==0)
       play_sample(sound,255,128,1000,FALSE);

     if(count==0 || count ==-2)
    	rectfill(backgr,0,0,640,48,255);

    blit(backgr,screen,0,0,0,0,640,480);

    if(count<-35)
      return 1;
    else
      return 0;
}

void play_song(DUH *new_song)
{
    //if(current_song)
    //    unload_duh(current_song);
    //current_song= dumb_load_it(filename);
    current_song = new_song;
    
    if(mod_player)
    {
        al_stop_duh(mod_player);
        mod_player=NULL;
    }
    
    mod_player=al_start_duh(current_song/*new_song*//*song*/, 2/*sterio*/, 0/**/,0.7f/*volume*/, 512/*buffer*/, 11025/*frequency*/);
}
