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

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

#define tile_w	30     //width of tiles  (in pixels)
#define tile_h  30     //height of tiles (in pixels)

typedef struct t
	{
     //int act;
     int type, type2, type3;
     int walk;  //0=walk 1=no walk
    } t;

struct
	{
     int x,y;
     int type;
     int right_x,left_x;
    } bad[100];

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

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

int start_x=0,start_y=0;

void startup(void);
void one_loop(void);
void go_sec(void);
int get_no(int x, int y);

void draw_tile(int x,int y,int type, int la);
void draw_trans_tile(int x,int y,int type);


void save(void);
void loader(void);
void define_range(void);

int choose_tile(int number);

int diff_x(int x);
int fries=0;

volatile int sec;
int cycles=0;
int esc=0;
int layer=1;
int out=0;

int x=0,y=0;
int screen_x=0, screen_y=0;
int type=1;
char name[20];

int display=0;

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

t tile[width*heigth+width+heigth];

FILE *fp;

BITMAP *backgr;
DATAFILE *data;
RGB *p;


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

int main(void)
	{
     printf("Please enter a name to save the map by, or a map to load: \n");
     scanf("%s",name);
     strcat(name,".map");
     startup();

     while (out==0)
      {
       //menu, etc.
       if(esc==1)
         out=1;
       while(esc==0)  //when not trying to leave game
         {
          while(sec>cycles && esc==0)      //regulates time
            one_loop();
         // {}
         }
      }
    }

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

     data=load_datafile("side.dat");

     set_pallete(data[pal].dat);

     backgr=create_bitmap(SCREEN_W,SCREEN_H);
     rectfill(backgr,0,0,640,480,0);

     for(x=0;x<width*heigth+width+heigth;x++)
        tile[x].type=0;

     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


   //  for(x=0;x<number_of_

     LOCK_VARIABLE(sec);
     LOCK_FUNCTION(go_sec);
     install_int_ex(go_sec, BPS_TO_TIMER(20));


     }

void one_loop(void)
	{
    int xx,yy,dx,dy;
    int no, n=0;
    fries=0;
    cycles++;
    rectfill(backgr,0,0,640,480,0);

    rect(backgr,0-screen_x,0-screen_y,
         (width)*tile_w-screen_x, (heigth)*tile_h-screen_y,12);

    if(key[KEY_ESC])
       esc=1;

    if(key[KEY_UP] && (y-screen_y>0 || screen_y>0) )
      {
       y-=tile_h;
       if(y-screen_y<0)
       	screen_y-=tile_h;
      }
    else if(key[KEY_DOWN] && y<(heigth-1)*(tile_h))
      {
       y+=tile_h;
       if(y-screen_y>=400)
       	screen_y+=tile_h;
      }
    if(key[KEY_LEFT] && (x-screen_x>0 || screen_x>0) )
      {
       x-=tile_w;
       if(x-screen_x<0)
       	screen_x-=tile_w;
      }
    else if(key[KEY_RIGHT] && x<(width-1)*tile_w)
      {
       x+=tile_w;
       if(x-screen_x>=600)
       	screen_x+=tile_w;
      }

    if(key[KEY_1])
      {
        layer=1;
        type=1;
        }
      else if(key[KEY_2])
        layer=2;
      else if(key[KEY_3])
        {
        layer=3;
        type=-1;
        }

    no=get_no(x,y);

    if(key[KEY_ENTER])
      {
      if(layer==1)
      	tile[no].type=type;
      if(layer==2)
      	tile[no].type2=type;
      /*if(layer==3)
      	tile[no].type3=type;   */
        if(layer==3)
          {
           if(type==-1)
             {
              start_x=x-SCREEN_W/2;
              start_y=y-SCREEN_H/2;

             }
           else if( type==-2)
             {
             define_range();
             }
           else if( type==-3)
             {
              item[number_of_items].x=x;
              item[number_of_items].y=y;
              item[number_of_items].type=1;
              number_of_items++;
              }
           else if( type==-4)
             {
              item[number_of_items].x=x;
              item[number_of_items].y=y;
              item[number_of_items].type=2;
              number_of_items++;
              }
           else if( type==-5)
             {
              item[number_of_items].x=x;
              item[number_of_items].y=y;
              item[number_of_items].type=3;
              number_of_items++;
              }
           else if( type==-6)
             {
              item[number_of_items].x=x;
              item[number_of_items].y=y;
              item[number_of_items].type=4;

              number_of_items++;
              }
           else if( type==-7)
             {
              tel[number_of_tele].x1=x;
              tel[number_of_tele].y1=y;
              tel[number_of_tele].num=number_of_tele;

              //number_of_tele++;
              }
           else if(type==-8)
              {
               tel[number_of_tele].x2=x;
               tel[number_of_tele].y2=y;
               //tel[number_of_tele].num=number_of_tele;
               number_of_tele++;
              }
           else if( type==-9)
             {
              item[number_of_items].x=x;
              item[number_of_items].y=y;
              item[number_of_items].type=5;
              number_of_items++;
              }
           else if( type==-10)
             {
              item[number_of_items].x=x;
              item[number_of_items].y=y;
              item[number_of_items].type=6;
              number_of_items++;
              }
          else if( type==-11)
             {
              item[number_of_items].x=x;
              item[number_of_items].y=y;
              item[number_of_items].type=7;
              number_of_items++;
              }
          else if( type==-12)
             {
              item[number_of_items].x=x;
              item[number_of_items].y=y;
              item[number_of_items].type=8;
              number_of_items++;
              }
          else if( type==-13)
             {
              item[number_of_items].x=x;
              item[number_of_items].y=y;
              item[number_of_items].type=9;
              number_of_items++;
              }
           }

    //  if(layer==3)


      if(type==1 || type==2)
      	tile[no].walk=0;
      else if(type==3 || type==4)
        tile[no].walk=1;
      }

    if(key[KEY_E])
      {
       if(layer==1)
      	tile[no].type=0;
      if(layer==2)
      	tile[no].type2=0;
      if(layer==3)
        {
         for(n=0;n<number_of_bad;n++)
            {
             if(bad[n].x==x && bad[n].y==y)
               {
               bad[n].x=0;
               bad[n].y=0;
               bad[n].type=0;
               }
             }
         for(n=0;n<number_of_items;n++)
            {
             if(item[n].x==x && item[n].y==y)
               {
               item[n].x=0;
               item[n].y=0;
               item[n].type=0;
               }
             }
         for(n=0;n<number_of_tele;n++)
            {
             if((tel[n].x1==x && tel[n].y1==y)||(tel[n].x2==x && tel[n].y2==y))
               {
                tel[n].x1=0;
                tel[n].y1=0;
                tel[n].x2=0;
                tel[n].y2=0;

                }
            }
         }
      }

    if(key[KEY_A])
      screen_x++;



    if(key[KEY_SPACE])
      type=choose_tile(1);

    if(key[KEY_M])
      {
       if(display==0)
         display=1;
       else if(display==1)
         display=0;
       rest(200);
      }

    /*if(key[KEY_X] && layer<3)
      layer++;
    else if(key[KEY_Z] && layer>1)
      layer--;  */


    if(key[KEY_O] && type>0 && layer!=3)
      type--;
    else if(key[KEY_P] && layer!=3)
      type++;

    if(key[KEY_O] && layer==3)
      type--;
    else if(key[KEY_P] && layer==3 && type<-1)
      type++;


    for(yy=screen_y ; yy<screen_y+SCREEN_H ; yy+=tile_h)
       for(xx=screen_x ; xx<screen_x+SCREEN_W ; xx+=tile_w)
          {
          n=get_no(xx,yy);
          draw_tile(xx-diff_x(xx),yy-diff_x(yy),tile[n].type,1);
          draw_tile(xx-diff_x(xx),yy-diff_x(yy),tile[n].type2,2);
          draw_tile(xx-diff_x(xx),yy-diff_x(yy),tile[n].type3,3);
          }

    rect(backgr,x-screen_x, y-screen_y, x-screen_x+(tile_w), y-screen_y+(tile_h),1);

    if(display==1)
      {
    	text_mode(-1);
    	rectfill(backgr,0,5,360+tile_w,10+tile_h,23);
    	textprintf(backgr,font,2,
               	5,0,"screen_x: %5d screen_y: %5d",screen_x,screen_y);
    	textprintf(backgr,font,2,15,0,"cursor_x: %5d cursor_y: %5d",x,y);
    	textprintf(backgr,font,2,25,0,"index number of tile selected: %7d",no);
    	textprintf(backgr,font,2,35,0,"type number of tile selected:  %7d",tile[get_no(x,y)].type);
        textprintf(backgr,font,2,45,0,"walk  %7d",tile[get_no(x,y)].walk);


        draw_tile(350+screen_x,7+screen_y,type,0);
        //draw_tile(350+screen_x,7+screen_y,type2);

        if(type==0)
          rectfill(backgr,350,7,350+tile_w,7+tile_h,0);
    	text_mode(0);
      }

    draw_sprite(backgr,data[k_walk1].dat,start_x+SCREEN_W/2-screen_x,start_y+SCREEN_H/2-screen_y);

    for(n=0;n<number_of_bad;n++)
       {
        if(bad[n].type==1)
          {
        	draw_sprite(backgr,data[tomato1].dat,bad[n].x-screen_x,bad[n].y-screen_y);
            circlefill(backgr,bad[n].right_x-screen_x,bad[n].y-screen_y,5,85);
            circlefill(backgr,bad[n].left_x-screen_x,bad[n].y-screen_y,5,85);
          }
      }

    for(n=0;n<number_of_items;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);
          fries++;
          }
        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);
  

       }

    for(n=0;n<number_of_tele+1;n++)
       	{
         textprintf(backgr,font,tel[n].x1-screen_x,tel[n].y1-screen_y,75,"In, %d",n);
       	 draw_sprite(backgr,data[tele1].dat,tel[n].x1-screen_x,tel[n].y1-screen_y);

         if(key[KEY_T])
           {
         if(tel[n].x1==x && tel[n].y1==y)
         	{
             dx=-x+tel[n].x2;
         	 dy=-y+tel[n].y2;

             x=tel[n].x2;
             y=tel[n].y2;

             screen_x+=dx;
             screen_y+=dy;
             }
         else if(tel[n].x2==x && tel[n].y2==y)
         	{
             dx=-x+tel[n].x1;
         	 dy=-y+tel[n].y1;

             x=tel[n].x1;
             y=tel[n].y1;

             screen_x+=dx;
             screen_y+=dy;
             }
             }

         textprintf(backgr,font,tel[n].x2-screen_x,tel[n].y2-screen_y,75,"Out, %d",n);
         draw_sprite(backgr,data[tele1].dat,tel[n].x2-screen_x,tel[n].y2-screen_y);


        }


    textprintf(backgr,font,2,SCREEN_H-80,75,"lx %d rx %d",bad[0].left_x, bad[0].right_x);
    textprintf(backgr,font,2,SCREEN_H-70,75,"NOB  %d",number_of_bad);
    textprintf(backgr,font,2,SCREEN_H-60,75,"NOI  %d",number_of_items);
    textprintf(backgr,font,2,SCREEN_H-60,75,"Fries  %d",fries);

    textprintf(backgr,font,2,SCREEN_H-30,75,"Tile  %d",type);
    textprintf(backgr,font,2,SCREEN_H-20,85,"Layer  %d",layer);
    textprintf(backgr,font,0,SCREEN_H-10,52,"Hit Space to Choose a Tile, S to Save, L to Load, or M to Toggle Display");

    draw_trans_tile(x,y,type);


  /*  for(n=0;n<number_of_bad;n++)
       if(bad[n].type==0)
         number_of_bad=n;

    for(n=0;n<number_of_items;n++)
       if(item[n].type==0)
         number_of_items=n;   */

    if(key[KEY_S])
      {
      save();
      save();
      }

    else if(key[KEY_L])
      loader();

    blit(backgr,screen,0,0,0,0,SCREEN_W,SCREEN_H);
    rest(100);
    }

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

     no=x+(width*y)-width;
    // no=x+(heigth*y)-th;

     return no;
   }


void draw_tile(int x, int y, int type, int la)
	{
    /* if(type==1)
     	blit(data[roadv1].dat,backgr,0,0,x-screen_x,y-screen_y,tile_w,tile_h);
     else if(type==2)
     	blit(data[roadv2].dat,backgr,0,0,x-screen_x,y-screen_y,tile_w,tile_h);
     else if(type==3)
     	blit(data[roadh1].dat,backgr,0,0,x-screen_x,y-screen_y,tile_w,tile_h);
    */
     if(type==1)
     	draw_sprite(backgr,data[g_mid].dat,x-screen_x,y-screen_y);
     else if(type==2)
        draw_sprite(backgr,data[cliff1].dat,x-screen_x,y-screen_y);
     else if(type==3)
     	draw_sprite(backgr,data[g_l_edge].dat,x-screen_x,y-screen_y);
     else if(type==4)
     	draw_sprite(backgr,data[g_r_edge].dat,x-screen_x,y-screen_y);
     else if(type==5)
     	draw_sprite(backgr,data[underground1].dat,x-screen_x,y-screen_y);
     else if(type==6)
     	draw_sprite(backgr,data[tree1].dat,x-screen_x,y-screen_y);
     else if(type==7)
     	draw_sprite(backgr,data[tree2].dat,x-screen_x,y-screen_y);
     else if(type==8)
     	draw_sprite(backgr,data[spikes1].dat,x-screen_x,y-screen_y);
     else if(type==9)
     	draw_sprite(backgr,data[arr_up].dat,x-screen_x,y-screen_y);
     else if(type==10)
     	draw_sprite(backgr,data[arr_right].dat,x-screen_x,y-screen_y);
     else if(type==11)
     	draw_sprite(backgr,data[arr_down].dat,x-screen_x,y-screen_y);
     else if(type==12)
     	draw_sprite(backgr,data[arr_left].dat,x-screen_x,y-screen_y);
     else if(type==13)
     	draw_sprite(backgr,data[exit1].dat,x-screen_x,y-screen_y);
     else if(type==14)
     	draw_sprite(backgr,data[spring].dat,x-screen_x,y-screen_y);
     else if(type==15)
     	draw_sprite(backgr,data[back_tile1].dat,x-screen_x,y-screen_y);
     else if(type==16)
     	draw_sprite(backgr,data[vine_tile1].dat,x-screen_x,y-screen_y);
     else if(type==17)
     	draw_sprite(backgr,data[vine_tile2].dat,x-screen_x,y-screen_y);

  /*   else if(type==-1)
        draw_sprite(backgr,data[k_walk1].dat,x-screen_x,y-screen_y);    */


     if(la==1 && type>0)
       rect(backgr,x-screen_x,y-screen_y,x-screen_x+tile_w,y-screen_y+tile_h,80);
     else if(la==2 && type>0)
       rect(backgr,x-screen_x,y-screen_y,x-screen_x+tile_w,y-screen_y+tile_h,75);
    /* else if(type==3)
     	draw_sprite(backgr,data[g_tile3].dat,x-screen_x,y-screen_y);
     else if(type==4)
     	draw_sprite(backgr,data[g_tile4].dat,x-screen_x,y-screen_y);
     else if(type==5)
     	draw_sprite(backgr,data[g_tile5].dat,x-screen_x,y-screen_y);
     else if(type==6)
     	draw_sprite(backgr,data[g_tile6].dat,x-screen_x,y-screen_y);
     else if(type==7)
     	draw_sprite(backgr,data[g_tile7].dat,x-screen_x,y-screen_y);
     else if(type==8)
     	draw_sprite(backgr,data[g_tile8].dat,x-screen_x,y-screen_y);
     else if(type==9)
     	draw_sprite(backgr,data[g_tile9].dat,x-screen_x,y-screen_y);
   */


    }

void draw_trans_tile(int x, int y, int type)
	{

     if(type==1)
     	draw_trans_sprite(backgr,data[g_mid].dat,x-screen_x,y-screen_y);
     else if(type==2)
        draw_trans_sprite(backgr,data[cliff1].dat,x-screen_x,y-screen_y);
     else if(type==3)
     	draw_trans_sprite(backgr,data[g_l_edge].dat,x-screen_x,y-screen_y);
     else if(type==4)
     	draw_trans_sprite(backgr,data[g_r_edge].dat,x-screen_x,y-screen_y);
     else if(type==5)
     	draw_trans_sprite(backgr,data[underground1].dat,x-screen_x,y-screen_y);
     else if(type==6)
     	draw_trans_sprite(backgr,data[tree1].dat,x-screen_x,y-screen_y);
     else if(type==7)
     	draw_trans_sprite(backgr,data[tree2].dat,x-screen_x,y-screen_y);
     else if(type==8)
     	draw_trans_sprite(backgr,data[spikes1].dat,x-screen_x,y-screen_y);
     else if(type==9)
     	draw_trans_sprite(backgr,data[arr_up].dat,x-screen_x,y-screen_y);
     else if(type==10)
     	draw_trans_sprite(backgr,data[arr_right].dat,x-screen_x,y-screen_y);
     else if(type==11)
     	draw_trans_sprite(backgr,data[arr_down].dat,x-screen_x,y-screen_y);
     else if(type==12)
     	draw_trans_sprite(backgr,data[arr_left].dat,x-screen_x,y-screen_y);
     else if(type==13)
     	draw_trans_sprite(backgr,data[exit1].dat,x-screen_x,y-screen_y);
     else if(type==14)
     	draw_trans_sprite(backgr,data[spring].dat,x-screen_x,y-screen_y);
     else if(type==15)
     	draw_trans_sprite(backgr,data[back_tile1].dat,x-screen_x,y-screen_y);
     else if(type==16)
     	draw_trans_sprite(backgr,data[vine_tile1].dat,x-screen_x,y-screen_y);
     else if(type==17)
     	draw_trans_sprite(backgr,data[vine_tile2].dat,x-screen_x,y-screen_y);



     else if(type==-1)
        draw_trans_sprite(backgr,data[k_walk1].dat,x-screen_x,y-screen_y);
     else if(type==-2)
        draw_trans_sprite(backgr,data[tomato1].dat,x-screen_x,y-screen_y);
     else if(type==-3)
     	draw_trans_sprite(backgr,data[rose1].dat,x-screen_x,y-screen_y);
     else if(type==-4)
     	draw_trans_sprite(backgr,data[star1].dat,x-screen_x,y-screen_y);
     else if(type==-5)
     	draw_trans_sprite(backgr,data[cheese1].dat,x-screen_x,y-screen_y);
     else if(type==-6)
     	draw_trans_sprite(backgr,data[fry1].dat,x-screen_x,y-screen_y);
     else if(type==-7)
     	draw_trans_sprite(backgr,data[tele1].dat,x-screen_x,y-screen_y);
     else if(type==-8)
     	draw_trans_sprite(backgr,data[tele1].dat,x-screen_x,y-screen_y);
     else if(type==-9)
     	draw_trans_sprite(backgr,data[lett1].dat,x-screen_x,y-screen_y);
     else if(type==-10)
     	draw_trans_sprite(backgr,data[onion1].dat,x-screen_x,y-screen_y);
     else if(type==-11)
     	draw_trans_sprite(backgr,data[cone1].dat,x-screen_x,y-screen_y);
     else if(type==-12)
     	draw_trans_sprite(backgr,data[melon1].dat,x-screen_x,y-screen_y);
     else if(type==-13)
     	draw_trans_sprite(backgr,data[pepper1].dat,x-screen_x,y-screen_y);
    /* else if(type==6)
     	draw_trans_sprite(backgr,data[g_tile6].dat,x-screen_x,y-screen_y);
     else if(type==7)
     	draw_trans_sprite(backgr,data[g_tile7].dat,x-screen_x,y-screen_y);
     else if(type==8)
     	draw_trans_sprite(backgr,data[g_tile8].dat,x-screen_x,y-screen_y);
     else if(type==9)
     	draw_trans_sprite(backgr,data[g_tile9].dat,x-screen_x,y-screen_y);*/
    }


int diff_x(int x)
	{
     int x2;

     x2=x;

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

    // textprintf(backgr,font,0,50,1,"%d",x2);

     return x2;
    }

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

     fp=fopen(name,"w");
     /*fprintf(fp,"%d\n",width);
     fprintf(fp,"%d\n",heigth);  */
     fprintf(fp,"%d ",number_of_bad);
     fprintf(fp,"%d ",number_of_items);
     fprintf(fp,"%d ",number_of_tele);

     fprintf(fp,"%d ",fries);

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

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

     for(x=0;x<number_of_bad;x++)
        {
         fprintf(fp,"%d ",bad[x].x);
         fprintf(fp,"%d ",bad[x].y);
         fprintf(fp,"%d ",bad[x].type);

         fprintf(fp,"%d ",bad[x].left_x);
         fprintf(fp,"%d ",bad[x].right_x);
         }
      for(x=0;x<number_of_items;x++)
        {
         fprintf(fp,"%d ",item[x].x);
         fprintf(fp,"%d ",item[x].y);
         fprintf(fp,"%d ",item[x].type);
         }
      for(x=0;x<number_of_tele;x++)
         {
         fprintf(fp,"%d ",tel[x].x1);
         fprintf(fp,"%d ",tel[x].y1);
         fprintf(fp,"%d ",tel[x].x2);
         fprintf(fp,"%d ",tel[x].y2);
         //fprintf(fp,"%d ",item[x].type);

          }
	}

void loader(void)
	{
     int x,y,no=0;
     clear(screen);
     //strcat(name,".map");
     fp=fopen(name,"r");
     /*fscanf(fp,"%d\n",&width);
     fscanf(fp,"%d\n",&heigth); */
     fscanf(fp,"%d ",&number_of_bad);
     fscanf(fp,"%d ",&number_of_items);
     fscanf(fp,"%d ",&number_of_tele);

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

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

            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);
         }
	}

int choose_tile(int number)
	{
     int x, count=0,no=0;
     BITMAP *bmp;

     while(!key[KEY_ENTER])
       {
        rest(100);
        rectfill(backgr,0,0,SCREEN_W,SCREEN_H,0);
        textprintf_centre(backgr,font,370,100,51,"Choose you tile, ENTER to finish");

        if(key[KEY_LEFT] && no>0)
          no--;
        else if(key[KEY_RIGHT])
       	  no++;

        textprintf_centre(backgr,font,370,400,51,"Tile #%d",no);

        if(no>0)
     		draw_tile(320-(tile_w*2)+screen_x , 240-(tile_h)+screen_y,no-1,0);
     	draw_tile(320+screen_x , 240-(tile_h)+screen_y,no,0);
     	draw_tile(320+(tile_w*2)+screen_x , 240-(tile_h)+screen_y,no+1,0);

     	rect(backgr,SCREEN_W,SCREEN_H-(tile_h),SCREEN_W+(tile_w*2),SCREEN_H-(tile_h)+(tile_h*2),51);
        blit(backgr,screen,0,0,0,0,SCREEN_W,SCREEN_H);
       }
     clear_keybuf();
     rest(100);
     sec=0;
     cycles=0;
     return no;

     }

void define_range(void)
	{
     int xx;

     bad[number_of_bad].x=x;
     bad[number_of_bad].y=y;
     bad[number_of_bad].type=1;
     xx=x;
     while(!key[KEY_Q])
     	{
        //blit(backgr,screen,0,0,0,0,640,480);
        rectfill(screen,0,y-screen_y-5,640,y-screen_y+5,75);
        textprintf(screen,font,200,200,65,"Please define it's leftward range of motion");
        circlefill(screen,xx-screen_x,y-screen_y,5,85);
        if(key[KEY_LEFT])
        	xx--;
        else if(key[KEY_RIGHT] && xx<x)
            xx++;

            rest(30);
        }
        bad[number_of_bad].left_x=xx;
        clear_keybuf();

        xx=x;
        while(!key[KEY_W])
        	{
            //blit(backgr,screen,0,0,0,0,640,480);
            rectfill(screen,0,y-screen_y-5,640,y-screen_y+5,75);
            textprintf(screen,font,200,200,65,"Please define it's leftward range of motion");
            circlefill(screen,xx-screen_x,y-screen_y,5,85);
            if(key[KEY_LEFT] && xx>x)
            	xx--;
            else if(key[KEY_RIGHT])
                xx++;

            rest(30);
            }
   		bad[number_of_bad].right_x=xx;
     	number_of_bad++;
	}








