#include <allegro.h>
#include <fstream>
#include "lariolib.h"
#include "world1.h"
#include "world2.h"
#include "world3.h"
#include "world4.h"

unsigned char *temp;

int cgs(){
    stop_sample(gmus);
    rectfill(screen, 0, 0, 640, 480, makecol(10,10,255));
    if (world>3 ^ world<=0)
    textout_ex(screen, font, "ERROR: 59273", 200, 100, makecol(220,0,0), makecol(10,10,255));
    if (p.x<=0 ^ p.x>140 ^ p.y>40 ^ p.y<6)
    textout_ex(screen, font, "ERROR: 52635", 200, 100, makecol(220,0,0), makecol(10,10,255));
    if (p.dir>2 ^ p.dir<2)
    textout_ex(screen, font, "ERROR: 56345", 200, 100, makecol(220,0,0), makecol(10,10,255));
    if (p.score>50000 ^ p.score<0)
    textout_ex(screen, font, "ERROR: 59384", 200, 100, makecol(220,0,0), makecol(10,10,255));
    if (p.dir==0 && world==0)
    textout_ex(screen, font, "ERROR: 50000", 200, 100, makecol(220,0,0), makecol(10,10,255));
    
    textout_ex(screen, font, "The gamesave is corrupt!", 300, 200, makecol(220,0,0), makecol(10,10,255));
    textout_ex(screen, font, "The game cannot be played!", 290, 230, makecol(220,0,0), makecol(10,10,255));
    textout_ex(screen, font, "Press ESC to quit", 300, 320, makecol(220,0,0), makecol(10,10,255));
    
                       while (!key[KEY_ESC]){
                             rest(5);}
                             
    ofstream savedel;
    savedel.open("Settings/savegame.svg");
    savedel << "The gamesave was corrupt!" << endl;
    savedel << "Your data has been deleted!";
    savedel.close();
    
    return 0;}

void re_alloc_mem() {
     if (world==2)
     delete []world1map;
     else if (world==3)
     delete []world2map; }

void get_cfg(){
     ifstream cfg;
     int mbuf;
     cfg.open("Settings/Lario.cfg");
     cfg >> mbuf;
     cfg.close();
     if (mbuf==7834)
     mute = 1;
     else
     mute = -1;}
     
void make_cfg(){
     ofstream cfg;
     int mbuf;
     if (mute==1)
     mbuf = 7834;
     else
     mbuf = 8734;
     cfg.open("Settings/Lario.cfg", ios::binary);
     cfg << mbuf << endl;
     cfg << "jhfdfchjhcbsdhbhbjchsfdhshhds" << endl;
     cfg << "hddcdhgdhdhjbhbhbdchjbdchjbdh" << endl;
     cfg << "hdghddhjdhjdhjdhjdhjdhjbdhjbdhjb" << endl;
     cfg.close();}

int main_menu() {
     if (mouse_x>300 && mouse_x<420 && mouse_y>40 && mouse_y<100 && mouse_b & 1)
	    menu = 7; //Continue game****************************
     if (mouse_x>300 && mouse_x<420 && mouse_y>150 && mouse_y<210 && mouse_b & 1)
	    menu = 6;  //new game**********************************
     if (mouse_x>300 && mouse_x<420 && mouse_y>230 && mouse_y<290 && mouse_b & 1){
	    instructions();
        drawmen = 1;}
     if (mouse_x>300 && mouse_x<420 && mouse_y>310 && mouse_y<370 && mouse_b & 1){
        rectfill(screen, 0, 0, 640, 480, makecol(0,0,0));
	    credits();
        drawmen = 1;}
     if (mouse_x>300 && mouse_x<420 && mouse_y>390 && mouse_y<450 && mouse_b & 1){
     if (mute==1){
        stop_sample(mmus);
	    mute = -1;}  
     else {
        play_sample(mmus, 255, 128, 1000, 1);
        mute = 1;}
     make_cfg();
     drawmen = 1;}
     if (mouse_x>10 && mouse_x<130 && mouse_y>10 && mouse_y<70 && mouse_b & 1)
	    menu = 0;}

void mendraw() {
     if (drawmen==1 && menu==1){
                    rectfill(screen, 0, 0, 640, 480, makecol(230,230,40));
                    draw_sprite(screen, title, 440, 0);
                    draw_sprite(screen, title, 0, 250);
                    rectfill(screen, 300, 40, 420, 100, makecol(255,0,0));
                    textout_ex(screen, font, "Continue", 340, 55, makecol(0,0,255), makecol(255,0,0));
                    rectfill(screen, 300, 150, 420, 210, makecol(0,255,0));
                    textout_ex(screen, font, "New Game", 320, 165, makecol(255,0,0), makecol(0,255,0));
                    rectfill(screen, 300, 230, 420, 290, makecol(255,0,0));
                    textout_ex(screen, font, "Instructions", 310, 245, makecol(0,0,255), makecol(255,0,0));
                    rectfill(screen, 300, 310, 420, 370, makecol(255,0,0));
                    textout_ex(screen, font, "Credits", 340, 325, makecol(0,0,255), makecol(255,0,0));
                    rectfill(screen, 300, 390, 420, 450, makecol(100,100,100));
                    if (mute==1)
                    textout_ex(screen, font, "Mute", 350, 405, makecol(255,0,0), makecol(100,100,100));
                    else
                    textout_ex(screen, font, "unMute", 340, 405, makecol(255,0,0), makecol(100,100,100));
                    rectfill(screen, 10, 10, 130, 70, makecol(200,0,0));
                    textout_ex(screen, font, "Quit", 50, 30, makecol(0,0,0), makecol(200,0,0));
                    rest(500);
                    drawmen = 0;}}
                    
                    
int load_save(){    
    ifstream save;
    save.open("Settings/savegame.svg");
    save >> wbuf;
    save >> xbuf;
    save >> ybuf;
    save >> dirbuf;
    save >> sbuf;
    save.close();
    decrypt();}
    

int main() 
{	
	allegro_init();
       
	if (install_keyboard()!=0){
       allegro_message("ERROR: 09374", allegro_error);
       return 0;}
       
	install_mouse();
	
	if (install_sound(MIDI_AUTODETECT, DIGI_AUTODETECT, 0)!=0){
       allegro_message("ERROR: 05843", allegro_message);
       return 0;}
	
	set_color_depth(32);
	
	if (set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0)!=0){
       allegro_message("ERROR: 00435", allegro_error);
       return 0;}
       
    temp = (unsigned char *)malloc(sizeof(unsigned char) * SCREEN_W);
         if (!temp){
                    allegro_message("ERROR: 00001");
                    return 0;}

    g1.ds = 1;
    g1.type = 1;
    g2.ds = 2;
    g2.type = 1;
    t1.ds = 3;
    t1.type = 2;
    g3.ds = 4;
    g3.type = 1;
    t2.ds = 5;
    t2.type = 2;

    start:
    menu = 1;
    get_cfg();
	show_mouse(screen);
	
	credscrn = create_bitmap(640, 700);
	buffer = create_bitmap(640, 480);
	title = load_bitmap("Graphics/title.bmp", NULL);
	lar1 = load_bitmap("Graphics/lario1.bmp", NULL);
	lar2 = load_bitmap("Graphics/lario2.bmp", NULL);
	jump = load_bitmap("Graphics/larioj.bmp", NULL);
	gm1 = load_bitmap("Graphics/go1.bmp", NULL);
    gm2 = load_bitmap("Graphics/go2.bmp", NULL);
    tt1 = load_bitmap("Graphics/tt1.bmp", NULL);
    tt2 = load_bitmap("Graphics/tt2.bmp", NULL);
    finzon = load_bitmap("Graphics/fz.bmp", NULL);
    sky = load_bitmap("Graphics/sky.bmp", NULL);
    brick = load_bitmap("Graphics/brick.bmp", NULL);
    spike = load_bitmap("Graphics/spike.bmp", NULL);
    grass = load_bitmap("Graphics/grass.bmp", NULL);
    cloud = load_bitmap("Graphics/cloud.bmp", NULL);
    fballi = load_bitmap("Graphics/fball.bmp", NULL);
    fbex = load_bitmap("Graphics/fball2.bmp", NULL);
    
    ouch = load_sample("Sound_FX/ouch.wav");
    gmus = load_sample("Sound_FX/gback.wav");
    bkill = load_sample("Sound_FX/bkill.wav");
    die1 = load_sample("Sound_FX/die.wav");
    die2 = load_sample("Sound_FX/die2.wav");
    mmus = load_sample("Sound_FX/mback.wav");
    gwin = load_sample("Sound_FX/wback.wav");
    levwin = load_sample("Sound_FX/wl.wav");
    j = load_sample("Sound_FX/jump.wav");
    fballs = load_sample("Sound_FX/fball.wav");
    fbhw = load_sample("Sound_FX/fbhw.wav");
	
	stop_sample(gwin);
	stop_sample(gmus);
	
	if (mute==1)
	play_sample(mmus, 255, 128, 1000, 1);
	
	while (menu==1){//7 is for continue //6 is for new game  //1 is for main menu
	main_menu();
	mendraw();
    rest(50);}
	
	stop_sample(mmus);
	
	if (menu==0)
	return 0;
	
	if (menu==7){
	load_save();
    if (world>4 ^ world<=0 ^ world==0 ^ p.x >140 ^ p.x<=0 ^ p.y<6 ^ p.y>40 ^ p.score>120000 ^ p.score<0 ^ p.dir<1 ^ p.dir>2 ^ world==0)
    world = cgs();}
	else if (menu==6)
	world = 1;
	
	make_cfg();
	re_alloc_mem();
	
	show_mouse(NULL);
	
	if (world==1){
	world = world1_init();
	p.score+=5000;}
	
	if (world==2){
	caller = 2;
	make_save();}
	
	if (world==5)
	goto start;
	
	re_alloc_mem();
	
	if (world==2){
	world = world2_init();
	p.score+=7000;}
	
	if (world==3){
    caller = 3;
    make_save();}
	
	if (world==5)
	goto start;
	
	re_alloc_mem();
	
	if (world==3){
       world = world3_init();
       p.score+=10000;}
       
    if (world==5)
    goto start;
       
    re_alloc_mem();
    
    if (world==4){
        caller = 4;
        make_save();}
    
    if (world==4){
        world = world4_init();
        p.score+=15000;}
    
    if (world==4){
       winner();
       goto start;}
       
    if (world==5)
    goto start;
	
	return 0;
}
END_OF_MAIN();
