#include <allegro.h>
#include <vector>
#include "classes.h"
#include "1pmode.h"
#include "2pmode.h"
#include "lasers.h"
#include "ships.h"
#include "bots.h"
#include "collisions.h"
#include "pvpmode.h"


BITMAP *buffer;
BITMAP *credscrn; //local to this file
BITMAP *pship1;
BITMAP *pship2;
BITMAP *bship1;
BITMAP *bship2;
BITMAP *bship3;
BITMAP *bship4;
BITMAP *bship5;
BITMAP *title;
BITMAP *lasim;
BITMAP *part;

SAMPLE *menback;
SAMPLE *gback;
SAMPLE *lassfx;
SAMPLE *expd;
SAMPLE *turbofx;
SAMPLE *pause;
SAMPLE *col;
SAMPLE *lashit;
SAMPLE *screwed;

playership p1, p2;

std::vector<botship> bots;  //only 17ish bots at a time
std::vector<laser> las; //no limit to lasers
std::vector<particle> parts;
std::vector<star> p1_stars;
std::vector<star> p2_stars;

int drawmen = 1; //for menu drawing, prevents mouse being covered
int menu = 1; //for menu nav
int mute;
int mode;
int botc = 0, kb; //count of bots currently alive and killed per player
int fps = 1, tsecs = 1, secs = 1;  //for timer, fps
int secs1, secs2; //for time alive for players
int exec_loop, te; //to keep constant frame rate
int cycles;
bool con = true; //for loops, is false if loop needs to stop
bool ovom = false; //for p. vs. p.    //alters engine, but uses same functions etc

void calc_fps(){
    if (secs>tsecs){
        fps = cycles;
        cycles = 0;
        tsecs = secs;}}

void inc_timer(){
    secs++;}
END_OF_FUNCTION(inc_timer());

void inc_ptim1(){
    secs1++;}
END_OF_FUNCTION(inc_ptim1());

void inc_ptim2(){
    secs2++;}
END_OF_FUNCTION(inc_ptim2());

void inc_exec(){
    exec_loop++;}
END_OF_FUNCTION(inc_exec());

double sqrd(double ntbs){
    ntbs = ntbs * ntbs;
    return ntbs;}


void credits(){
     int scrnh = 480;
     rectfill(credscrn, 0, 0, 640, 700, makecol(0,0,0));
     textout_ex(credscrn, font, "Developed by:", 300, 10, makecol(255,0,0), makecol(0,0,0));
     textout_ex(credscrn, font, "ModShop", 330, 30, makecol(255,0,0), makecol(0,0,0));
     
     textout_ex(credscrn, font, "Game Concept:", 300, 70, makecol(255,0,0), makecol(0,0,0));
     textout_ex(credscrn, font, "Ben Reid", 330, 90, makecol(255,0,0), makecol(0,0,0));
     textout_ex(credscrn, font, "Emily Reid", 330, 110, makecol(255,0,0), makecol(0,0,0));
     
     textout_ex(credscrn, font, "Programming:", 300, 150, makecol(255,0,0), makecol(0,0,0));
     textout_ex(credscrn, font, "Ben Reid", 330, 170, makecol(255,0,0), makecol(0,0,0));
     
     textout_ex(credscrn, font, "Graphics:", 300, 190, makecol(255,0,0), makecol(0,0,0));
     textout_ex(credscrn, font, "Ben Reid", 330, 210, makecol(255,0,0), makecol(0,0,0));
     textout_ex(credscrn, font, "Emily Reid", 330, 225, makecol(255,0,0), makecol(0,0,0));
     textout_ex(credscrn, font, "Ultifinitus", 330, 240, makecol(255,0,0), makecol(0,0,0));
     
     textout_ex(credscrn, font, "Sound Effects:", 300, 270, makecol(255,0,0), makecol(0,0,0));
     textout_ex(credscrn, font, "Creation:", 330, 290, makecol(255,0,0), makecol(0,0,0));
     textout_ex(credscrn, font, "Partners in Rhyme", 340, 310, makecol(255,0,0), makecol(0,0,0));
     textout_ex(credscrn, font, "Editing:", 330, 330, makecol(255,0,0), makecol(0,0,0));
     textout_ex(credscrn, font, "Ben Reid", 340, 350, makecol(255,0,0), makecol(0,0,0));
     
     textout_ex(credscrn, font, "Special Thanks:", 300, 400, makecol(255,0,0), makecol(0,0,0));
     textout_ex(credscrn, font, "Mathew Reid", 330, 420, makecol(255,0,0), makecol(0,0,0));
     textout_ex(credscrn, font, "Emily Reid", 330, 440, makecol(255,0,0), makecol(0,0,0));
     textout_ex(credscrn, font, "Allegro", 330, 460, makecol(255,0,0), makecol(0,0,0));
     textout_ex(credscrn, font, "Dev-Cpp", 330, 480, makecol(255,0,0), makecol(0,0,0));
     textout_ex(credscrn, font, "Ultifinitus", 330, 500, makecol(255,0,0), makecol(0,0,0));
     draw_sprite(credscrn, title, 235, 550);
     textout_ex(credscrn, font, "Thanks for Playing!!!", 300, 700, makecol(255,0,0), makecol(0,0,0));
     
     while (scrnh!=-720){
           draw_sprite(screen, credscrn, 0, scrnh);
           scrnh--;
           if (scrnh==-520)
           rest(2500);
           rest(15);}
     scrnh = 480;}
     
void instructions(){
    int a = 0;
    rectfill(screen, 0, 0, 640, 480, makecol(0,0,0));
    draw_sprite(screen, title, 150, 0);
    
    rect(screen, 0, 0, 128, 100, makecol(255,255,255));
    textout_ex(screen, font, "NOTE:", 25, 1, makecol(255,255,255), makecol(0,0,0));
    textout_ex(screen, font, "If you can go", 1, 20, makecol(255,255,255), makecol(0,0,0));
    textout_ex(screen, font, "5s without tak-", 1, 40, makecol(255,255,255), makecol(0,0,0));
    textout_ex(screen, font, "ing damage, your", 1, 60, makecol(255,255,255), makecol(0,0,0));
    textout_ex(screen, font, "health refills", 1, 80, makecol(255,255,255), makecol(0,0,0));
    
    rect(screen, 8, 108, 212, 300, makecol(0,0,255));
    textout_ex(screen, font, "How to play:", 40, 110, makecol(0,0,255), makecol(0,0,0));
    textout_ex(screen, font, "Go all out Rambo or", 10, 130, makecol(0,0,255), makecol(0,0,0));
    textout_ex(screen, font, "team up with a freind", 10, 150, makecol(0,0,255), makecol(0,0,0));
    textout_ex(screen, font, "to survive as long as", 10, 170, makecol(0,0,255), makecol(0,0,0));
    textout_ex(screen, font, "possible to get a high", 10, 190, makecol(0,0,255), makecol(0,0,0));
    textout_ex(screen, font, "score. Goodluck!", 10, 210, makecol(0,0,255), makecol(0,0,0));
    textout_ex(screen, font, "(p vs. p is to kill your", 10, 230, makecol(0,0,255), makecol(0,0,0));
    textout_ex(screen, font, "friend if he makes you", 10, 250, makecol(0,0,255), makecol(0,0,0));
    textout_ex(screen, font, "lose...which is every", 10, 270, makecol(0,0,255), makecol(0,0,0));
    textout_ex(screen, font, "time of course)", 10, 290, makecol(0,0,255), makecol(0,0,0));
    
    rect(screen, 378, 8, 620, 295, makecol(255,0,0));
    textout_ex(screen, font, "Enemies:", 400, 10, makecol(255,0,0), makecol(0,0,0));
    textout_ex(screen, font, "The enemy ships range in", 380, 30, makecol(255,0,0), makecol(0,0,0));
    textout_ex(screen, font, "size from 1/4 to 6 times", 380, 50, makecol(255,0,0), makecol(0,0,0));
    textout_ex(screen, font, "that of yours", 380, 70, makecol(255,0,0), makecol(0,0,0));
    textout_ex(screen, font, "Which are a greater threat?", 380, 90, makecol(255,0,0), makecol(0,0,0));
    textout_ex(screen, font, "You decide (you'll be shocked)", 380, 110, makecol(255,0,0), makecol(0,0,0));
    draw_sprite(screen, bship1, 540, 140);
    draw_sprite(screen, bship2, 540, 190);
    draw_sprite(screen, bship5, 390, 130);
    
    rect(screen, 258, 108, 350, 305, makecol(0,255,0));
    textout_ex(screen, font, "Ships:", 260, 110, makecol(0,255,0), makecol(0,0,0));
    textout_ex(screen, font, "Player 1:", 270, 130, makecol(0,255,0), makecol(0,0,0));
    draw_sprite(screen, pship1, 290, 150);
    textout_ex(screen, font, "Player 2:", 270, 220, makecol(0,255,0), makecol(0,0,0));
    draw_sprite(screen, pship2, 290, 240);
    
    rect(screen, 8, 318, 450, 460, makecol(255,0,0));
    textout_ex(screen, font, "Controls:", 40, 320, makecol(255,0,0), makecol(0,0,0));
    textout_ex(screen, font, "Player 1", 140, 320, makecol(0,0,255), makecol(0,0,0));
    textout_ex(screen, font, "Player 2", 240, 320, makecol(0,255,0), makecol(0,0,0));
    
    textout_ex(screen, font, "Rotate:", 40, 340, makecol(255,0,0), makecol(0,0,0));
    textout_ex(screen, font, "Shoot:", 40, 360, makecol(255,0,0), makecol(0,0,0));
    textout_ex(screen, font, "Move:", 40, 380, makecol(255,0,0), makecol(0,0,0));
    textout_ex(screen, font, "Turbo:", 40, 400, makecol(255,0,0), makecol(0,0,0));
    textout_ex(screen, font, "Pause:", 40, 420, makecol(255,0,0), makecol(0,0,0));
    
    textout_ex(screen, font, "X/C", 140, 340, makecol(0,0,255), makecol(0,0,0));
    textout_ex(screen, font, "B", 140, 360, makecol(0,0,255), makecol(0,0,0));
    textout_ex(screen, font, "SPACE", 140, 380, makecol(0,0,255), makecol(0,0,0));
    textout_ex(screen, font, "N", 140, 400, makecol(0,0,255), makecol(0,0,0));
    textout_ex(screen, font, "ENTER", 180, 420, makecol(255,0,0), makecol(0,0,0));
    
    textout_ex(screen, font, "R/L ARROW KEYS", 240, 340, makecol(0,255,0), makecol(0,0,0));
    textout_ex(screen, font, "SLASH", 240, 360, makecol(0,255,0), makecol(0,0,0));
    textout_ex(screen, font, "R CTRL", 240, 380, makecol(0,255,0), makecol(0,0,0));
    textout_ex(screen, font, "R SHIFT", 240, 400, makecol(0,255,0), makecol(0,0,0));
    
    rectfill(screen, 500, 400, 620, 470, makecol(255,0,0));
    textout_ex(screen, font, "Back", 550, 430, makecol(0,0,255), makecol(255,0,0));
    
                    while (a==0){
                        if (mouse_x>=500 && mouse_x<=620 && mouse_y>=400 && mouse_y<=470 && mouse_b&1)
                        return;}
                    }

int main_menu() {
     if (mouse_x>300 && mouse_x<420 && mouse_y>40 && mouse_y<100 && mouse_b & 1)
	    menu = 7; //1 player game***********************************
     if (mouse_x>300 && mouse_x<420 && mouse_y>150 && mouse_y<210 && mouse_b & 1)
	    menu = 6;  //2 player game**********************************
     if (mouse_x>450 && mouse_x<570 && mouse_y>150 && mouse_y<210 && mouse_b & 1)
        menu = 9;  //player vs. player******************************
     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){
	    mute = -1;
        stop_sample(menback);}  
     else {
        mute = 1;
        play_sample(menback, 255, 128, 1000, 1);}
     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(0,0,0));
                    draw_sprite(screen, title, 0, 200);
                    draw_sprite(screen, title, 430, 0);
                    draw_sprite(screen, title, 430, 300);
                    rectfill(screen, 300, 40, 420, 100, makecol(255,0,0));
                    textout_ex(screen, font, "1-Player Game", 305, 55, makecol(0,0,255), makecol(255,0,0));
                    rectfill(screen, 300, 150, 420, 210, makecol(0,255,0));
                    textout_ex(screen, font, "2-Player Game", 305, 165, makecol(255,0,0), makecol(0,255,0));
                    rectfill(screen, 450, 150, 570, 210, makecol(0,255,0));
                    textout_ex(screen, font, "P. vs P.", 485, 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 main() 
{	
    LOCK_FUNCTION(inc_timer());
    LOCK_VARIABLE(secs);
    LOCK_FUNCTION(inc_exec());
    LOCK_VARIABLE(exec_loop);
    LOCK_FUNCTION(inc_ptim1());
    LOCK_FUNCTION(inc_ptim2());
    LOCK_VARIABLE(secs1);
    LOCK_VARIABLE(secs2);
    
	allegro_init();
	install_keyboard();
    install_mouse();
    install_sound(MIDI_AUTODETECT, DIGI_AUTODETECT, 0);
	
	set_color_depth(32);
	set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0);
	
	credscrn = create_bitmap(640, 720);
	buffer = create_bitmap(640, 480);
	pship1 = load_bitmap("Graphics/p1s.bmp", NULL);
	pship2 = load_bitmap("Graphics/p2s.bmp", NULL);
	bship1 = load_bitmap("Graphics/l1bt.bmp", NULL);
	bship2 = load_bitmap("Graphics/l2bt.bmp", NULL);
	bship3 = load_bitmap("Graphics/l3bt.bmp", NULL);
	bship4 = load_bitmap("Graphics/l4bt.bmp", NULL);
	bship5 = load_bitmap("Graphics/l5bt.bmp", NULL);
	title = load_bitmap("Graphics/title.bmp", NULL);
	lasim = load_bitmap("Graphics/las.bmp", NULL);
	part = load_bitmap("Graphics/particle.bmp", NULL);
	
	menback = load_sample("Sound_FX/menu.wav");
	gback = load_sample("Sound_FX/back.wav");
	lassfx = load_sample("Sound_FX/laser.wav");
	turbofx = load_sample("Sound_FX/turbo.wav");
	expd = load_sample("Sound_FX/exp.wav");
	pause = load_sample("Sound_FX/pause.wav");
	col = load_sample("Sound_FX/colship.wav");
	lashit = load_sample("Sound_FX/lascol.wav");
	screwed = load_sample("Sound_FX/screwed.wav");
	
	mute = 1;
	
do {
    stop_sample(gback);    //to be sure
    stop_sample(lassfx);
    stop_sample(turbofx);
    stop_sample(expd);
    stop_sample(screwed);
    
    if (mute==1 && p1.x==0) //so sound plays for menu only once
    play_sample(menback, 255, 128, 1000, 1);
    menu = 1;
    ovom = false;
    show_mouse(screen);
	
	while (menu==1){
        main_menu();
        mendraw();
        rest(50);}
	
	show_mouse(NULL);
	stop_sample(menback);
	
	if (mute==1)
	play_sample(gback, 255, 128, 1000, 1);
	
	if (menu==7){
        menu = 1;
        mode = 1;}
    
    if (menu==6){
        menu = 1;
        mode = 2;}
        
    if (menu==9){
        menu = 1;
        mode = 2; //so engine functions can be reused
        ovom = true;}
        
    if (menu==0)
    goto end;
    
    if (mode==1){
        install_int(*inc_timer, 1000);
        install_int(*inc_exec, 29); //about 35 times per sec
        install_int(*inc_ptim1, 1000);
        
        opmode_init();  //set variables, init vectors, start timer countdown
        opmode_loop();}
        
    
    if (mode==2 && ovom==false){
        install_int(*inc_timer, 1000);
        install_int(*inc_exec, 29);
        install_int(*inc_ptim1, 1000);
        install_int(*inc_ptim2, 1000);
        
        tpmode_init();
        tpmode_loop();}
        
    if (mode==2 && ovom==true){
        install_int(*inc_timer, 1000);
        install_int(*inc_exec, 29);
        install_int(*inc_ptim1, 1000);
        install_int(*inc_ptim2, 1000);
        
        pvpmode_init();
        pvpmode_loop();}

} while (menu==1);
    end:
	return 0;
}
END_OF_MAIN();
