#include "maze.h"

#include <iostream.h>
#include <allegro.h>

//#define linux
#define dos

#define maxx 800
#define maxy 600

#ifdef linux
#define GFX_CARD GFX_XWINDOWS
#endif

#ifdef dos
#define GFX_CARD GFX_AUTODETECT
#endif

void load_graphics();
void main_menu();
void game(int level);

BITMAP *menu,*m_game,*win,*lose;

int main()
{

  cout<<"Installing Allegro interface..."<<endl;

  allegro_init();

  install_keyboard();
  install_timer();
  install_mouse();

  set_color_depth(32);
  if(set_gfx_mode(GFX_CARD, maxx,maxy, 0,0)<0)
  {
    cout<<"Error setting graphics: "<<allegro_error<<endl;
    return -1;
  }

  show_mouse(screen);
  set_mouse_speed(1,1);
  scare_mouse();

  cout<<"Initialization comleted..."<<endl;
  cout<<"Loading graphics..."<<endl;
  load_graphics();

  cout<<"Starting main menu..."<<endl;
  main_menu();

  cout<<"Shutting down..."<<endl;
  destroy_bitmap(menu);
  destroy_bitmap(m_game);
  destroy_bitmap(win);
  destroy_bitmap(lose);

  allegro_exit();

  return 0;
}

#ifdef linux
END_OF_MAIN();
#endif

void load_graphics()
{

  menu=create_bitmap(maxx,maxy);
  m_game=create_bitmap(maxx,maxy);
  win=create_bitmap(maxx,maxy);
  lose=create_bitmap(maxx,maxy);
  clear(menu);
  clear(m_game);
  clear(win);
  clear(lose);

  int c=makecol32(255,255,255);

  textout(win,font,"YOU WIN",maxx/2,maxy/2,c);
  textout(lose,font,"YOU LOSE",maxx/2,maxy/2,c);
	
  int x = 2*(maxx/5) + 5,
	  y = (maxy/10);

  textout(menu,font,"MAZE",x,y*2+10,c);
  textout(menu,font,"Easy",x,5*y+10,c);
  textout(menu,font,"Medium",x,6*y+10,c);
  textout(menu,font,"Hard",x,7*y+10,c);
  textout(menu,font,"EXIT",x,8*y+10,c);


}

void main_menu()
{

  blit(menu,screen,0,0,0,0,maxx,maxy);

  unscare_mouse();

  int quit(0);

  int x = (maxx/5),
      y = (maxy/10);

#ifdef dos
#define KEY_E 18
#define KEY_M 50
#define KEY_H 35
#define KEY_ESC 1
#endif
	


  while(!quit)
  {
    if(key[KEY_E])
    {
      scare_mouse();
      game(20);
      blit(menu,screen,0,0,0,0,maxx,maxy);
      unscare_mouse();
    }
    if(key[KEY_M])
    {
      scare_mouse();
      game(50);
      blit(menu,screen,0,0,0,0,maxx,maxy);
      unscare_mouse();
    }
    if(key[KEY_H])
    {
      scare_mouse();
      game(100);
      blit(menu,screen,0,0,0,0,maxx,maxy);
      unscare_mouse();
    }
    if(key[KEY_ESC]) quit=1;
    if(mouse_b & 1)
      if(mouse_x>(2*x) && mouse_x<(3*x))
        if(mouse_y>(5*y))
        {
          if(mouse_y<(6*y))
          {
            scare_mouse();
            game(20);
            blit(menu,screen,0,0,0,0,maxx,maxy);
            unscare_mouse();
          }
          else if(mouse_y<(7*y))
          {
            scare_mouse();
            game(50);
            blit(menu,screen,0,0,0,0,maxx,maxy);
            unscare_mouse();
          }
          else if(mouse_y<(8*y))
          {
            scare_mouse();
            game(100);
            blit(menu,screen,0,0,0,0,maxx,maxy);
            unscare_mouse();
          }
          else if(mouse_y<(9*y)) quit=1;
        }
  }

  scare_mouse();

}

void game(int level)
{
  cout<<"Entering game at level: "<<level<<endl;
  maze_t Maze(level);

  coord pos,t;

  Maze.generate();

  pos=Maze.beg;

  clear(m_game);

  Maze.draw(m_game,pos);
  blit(m_game,screen,0,0,0,0,maxx,maxy);


#ifdef dos
#define KEY_UP 72
#define KEY_DOWN 80
#define KEY_LEFT 75
#define KEY_RIGHT 77
#endif

  int quit(0),won(0);

  while(!quit)
  {
    if(key[KEY_ESC]) quit=1;
    if(pos==Maze.end)
    {
      won=1;
      quit=1;
    }
    if(key[KEY_UP])
    {
      t=pos.ret(0,-1);
      if(Maze[t]==floor)
        pos=t;
      Maze.draw(m_game,pos);
      blit(m_game,screen,0,0,0,0,maxx,maxy);
      rest(100);
    }
    if(key[KEY_DOWN])
    {
      t=pos.ret(0,1);
      if(Maze[t]==floor)
        pos=t;
      Maze.draw(m_game,pos);
      blit(m_game,screen,0,0,0,0,maxx,maxy);
      rest(100);
    }
    if(key[KEY_LEFT])
    {
      t=pos.ret(-1,0);
      if(Maze[t]==floor)
        pos=t;
      Maze.draw(m_game,pos);
      blit(m_game,screen,0,0,0,0,maxx,maxy);
      rest(100);
    }
    if(key[KEY_RIGHT])
    {
      t=pos.ret(1,0);
      if(Maze[t]==floor)
        pos=t;
      Maze.draw(m_game,pos);
      blit(m_game,screen,0,0,0,0,maxx,maxy);
      rest(100);
    }
  }

  if(won)
  {
    blit(win,screen,0,0,0,0,maxx,maxy);
  }
  else
  {
    blit(lose,screen,0,0,0,0,maxx,maxy);
  }

  clear_keybuf();
  while(!keypressed());
  clear_keybuf();
}
