#include <allegro.h>
#include <math.h>
#include "classes.h"
#include "2pmode.h"
#include "globals.h"
#include "lasers.h"
#include "ships.h"
#include "bots.h"
#include "render.h"
#include "collisions.h"
#include "particles.h"
#include "stars.h"

void tpmode_init(){
    srand (time(NULL));
    int typebuf;
    p1.playership_init(1);
    p2.playership_init(2);
    create_stars(1);
    create_stars(2);
    secs1 = 0;
    secs2 = 0;
    botc = 0;
    secs = 0;
    kb = 0;
    for (int c = 0; c<parts.size(); c++){
    parts.erase(parts.begin()+c);}
    for (int c = 0; c<bots.size(); c++){
    bots.erase(bots.begin()+c);}
    for (int c = 0; c<20; c++){
    typebuf = rand() % 5 + 1;
    bots.push_back(botship(c*120, c*100, typebuf));} }
    
void tpmode_cam1(){
    //for player 2
    if (p2.x>p1.x)
        p2.sx1 = 320 + (p2.x - p1.x);
        else
        p2.sx1 = 320 - (p1.x - p2.x);
        if (abs(p2.y-p1.y)<100){//so bots from player 1's screen aren't drawn on p2's screen
        if (p2.y>p1.y)
        p2.sy1 = 120 + (p2.y - p1.y);
        else
        p2.sy1 = 120 - (p1.y - p2.y);}
        else
        p2.sy1 = -500; //off screen
    
    //this loop is for the bots
    for (int c1 = 0; c1<bots.size(); c1++){
        if (bots[c1].health>0 && bots[c1].x!=0){//so we don't draw dead bots
            if (bots[c1].x>p1.x)
            bots[c1].sx1 = 320 + (bots[c1].x - p1.x);
            else
            bots[c1].sx1 = 320 - (p1.x - bots[c1].x);
            if (abs(bots[c1].y-p1.y)<100){
            if (bots[c1].y>p1.y)
            bots[c1].sy1 = 120 + (bots[c1].y - p1.y);
            else
            bots[c1].sy1 = 120 - (p1.y - bots[c1].y);}
            else
            bots[c1].sy1 = -500;}}
            
    //this loop is for the lasers
    for (int c1 = 0; c1<las.size(); c1++){
        if (las[c1].x!=0){//so we don't draw unshot lasers
            if (las[c1].x>p1.x)
            las[c1].sx1 = 320 + (las[c1].x - p1.x);
            else
            las[c1].sx1 = 320 - (p1.x - las[c1].x);
            if (abs(las[c1].y-p1.y)<100){
            if (las[c1].y>p1.y)
            las[c1].sy1 = 120 + (las[c1].y - p1.y);
            else
            las[c1].sy1 = 120 - (p1.y - las[c1].y);}
            else
            las[c1].sy1 = -500;}}
            
    //this loop is for the particles
    for (int c1 = 0; c1<parts.size(); c1++){
        if (parts[c1].x!=0){//to be safe
            if (parts[c1].x>p1.x)
            parts[c1].sx1 = 320 + (parts[c1].x - p1.x);
            else
            parts[c1].sx1 = 320 - (p1.x - parts[c1].x);
            if (abs(parts[c1].y-p1.y)<120){
            if (parts[c1].y>p1.y)
            parts[c1].sy1 = 120 + (parts[c1].y - p1.y);
            else
            parts[c1].sy1 = 120 - (p1.y - parts[c1].y);}
            else
            parts[c1].sy1 = -500;}}
            
        //this loop is for the stars
    for (int c1 = 0; c1<p1_stars.size(); c1++){
        if (abs(p1_stars[c1].y-p1.y<120)){
        if (p1_stars[c1].x>p1.x)
        p1_stars[c1].sx1 = 320 + (p1_stars[c1].x - p1.x);
        else
        p1_stars[c1].sx1 = 320 - (p1.x - p1_stars[c1].x);
        if (p1_stars[c1].y>p1.y)
        p1_stars[c1].sy1 = 120 + (p1_stars[c1].y - p1.y);
        else
        p1_stars[c1].sy1 = 120 - (p1.y - p1_stars[c1].y);}
        
        else
        p1_stars[c1].sy1 = -500;}}
            
void tpmode_cam2(){
    //for player 1
    if (p1.x>p2.x)
        p1.sx1 = 320 + (p1.x - p2.x);
        else
        p1.sx1 = 320 - (p2.x - p1.x);
        if (abs(p2.y-p1.y)<100){//so bots from player 1's screen aren't drawn on p2's screen
        if (p2.y>p1.y)
        p1.sy1 = 360 + (p1.y - p2.y);
        else
        p1.sy1 = 360 - (p2.y - p1.y);}
        
        else
        p1.sy1 = -500; //off screen
    
    //this loop is for the bots
    for (int c1 = 0; c1<bots.size(); c1++){
        if (bots[c1].health>0 && bots[c1].x!=0){//so we don't draw dead bots
            if (bots[c1].x>p2.x)
            bots[c1].sx2 = 320 + (bots[c1].x - p2.x);
            else
            bots[c1].sx2 = 320 - (p2.x - bots[c1].x);
            if (abs(bots[c1].y-p2.y)<100){
            if (bots[c1].y>p2.y)
            bots[c1].sy2 = 360 + (bots[c1].y - p2.y);
            else
            bots[c1].sy2 = 360 - (p2.y - bots[c1].y);}
            
            else
            bots[c1].sy2 = -500;}}
            
    //this loop is for the lasers
    for (int c1 = 0; c1<las.size(); c1++){
        if (las[c1].x!=0){//so we don't draw unshot lasers
            if (las[c1].x>p2.x)
            las[c1].sx2 = 320 + (las[c1].x - p2.x);
            else
            las[c1].sx2 = 320 - (p2.x - las[c1].x);
            if (abs(las[c1].y-p2.y)<100){
            if (las[c1].y>p2.y)
            las[c1].sy2 = 360 + (las[c1].y - p2.y);
            else
            las[c1].sy2 = 360 - (p2.y - las[c1].y);}
            
            else
            las[c1].sy2 = -500;}}
            
    //this loop is for the particles
    for (int c1 = 0; c1<parts.size(); c1++){
        if (parts[c1].x!=0){//to be safe
            if (parts[c1].x>p2.x)
            parts[c1].sx2 = 320 + (parts[c1].x - p2.x);
            else
            parts[c1].sx2 = 320 - (p2.x - parts[c1].x);
            if (abs(parts[c1].y-p2.y)<120){
            if (parts[c1].y>p2.y)
            parts[c1].sy2 = 360 + (parts[c1].y - p2.y);
            else
            parts[c1].sy2 = 360 - (p2.y - parts[c1].y);}
            
            else
            parts[c1].sy2 = -500;}}
            
    //this loop is for the stars
    for (int c1 = 0; c1<p2_stars.size(); c1++){
        if (abs(p2_stars[c1].y-p2.y<120)){
            if (p2_stars[c1].x>p2.x)
            p2_stars[c1].sx2 = 320 + (p2_stars[c1].x - p2.x);
            else
            p2_stars[c1].sx2 = 320 - (p2.x - p2_stars[c1].x);
            if (p2_stars[c1].y>p1.y)
            p2_stars[c1].sy2 = 360 + (p2_stars[c1].y - p2.y);
            else
            p2_stars[c1].sy2 = 360 - (p2.y - p2_stars[c1].y);}
            
            if (p2_stars[c1].sy2<240 || p2_stars[c1].sy2>470) //had to do it differently for these
            p2_stars[c1].sy2 = -500;}}
            
    
void tpmode_after_game(){
    stop_sample(gback);
    if (mute==1)
    play_sample(menback, 255, 128, 1000, 1);
    
    rectfill(screen, 0, 0, 640, 480, makecol(60,60,60));
    textout_ex(screen, font, "GAME OVER", 320, 100, makecol(255,0,0), makecol(60,60,60));
    
    //player 1 stats
    rect(screen, 215, 225, 360, 340, makecol(255,0,0));
    textout_ex(screen, font, "PLAYER 1", 220, 230, makecol(255,0,0), makecol(60,60,60));
    textout_ex(screen, font, "Score:", 220, 250, makecol(255,0,0), makecol(60,60,60));
    textprintf(screen ,font, 270, 250, makecol(255,0,0), "%i", p1.score);
    textout_ex(screen, font, "Bots killed:", 220, 280, makecol(255,0,0), makecol(60,60,60));
    textprintf(screen ,font, 316, 280, makecol(255,0,0), "%i", kb);
    textout_ex(screen, font, "Time survived:", 220, 320, makecol(255,0,0), makecol(60,60,60));
    textprintf(screen ,font, 332, 320, makecol(255,0,0), "%i", secs1);
    
    //player 2 stats
    rect(screen, 415, 225, 560, 340, makecol(255,0,0));
    textout_ex(screen, font, "PLAYER 2", 420, 230, makecol(255,0,0), makecol(60,60,60));
    textout_ex(screen, font, "Score:", 420, 250, makecol(255,0,0), makecol(60,60,60));
    textprintf(screen ,font, 470, 250, makecol(255,0,0), "%i", p2.score);
    textout_ex(screen, font, "Bots killed:", 420, 280, makecol(255,0,0), makecol(60,60,60));
    textprintf(screen ,font, 516, 280, makecol(255,0,0), "%i", kb);
    textout_ex(screen, font, "Time survived:", 420, 320, makecol(255,0,0), makecol(60,60,60));
    textprintf(screen ,font, 532, 320, makecol(255,0,0), "%i", secs2);
    
    textout_ex(screen, font, "Press ESC to go back to the menu", 100, 400, makecol(255,0,0), makecol(60,60,60));
    rest(1000);
        while (1==1){
            if (key[KEY_ESC])
            return;}}
    
bool tpmode_pause(){
    if (mute==1)
    play_sample(pause, 255, 128, 1000, 0);
    remove_int(*inc_ptim1);
    remove_int(*inc_ptim2);
    rectfill(screen, 280, 190, 415, 290, makecol(60,60,60));
    textout_ex(screen, font, "PAUSED:", 310, 200, makecol(255,0,0), makecol(60,60,60));
    textout_ex(screen, font, "1.Continue", 310, 220, makecol(255,0,0), makecol(60,60,60));
    textout_ex(screen, font, "2.Quit", 310, 240, makecol(255,0,0), makecol(60,60,60));
    while (1==1){
    if (key[KEY_1]){
    install_int(*inc_ptim1, 1000);
    install_int(*inc_ptim2, 1000);
    return true;}
    if (key[KEY_2])
    return false;}}
    
void tpmode_countdown(){
    draw_sprite(screen, buffer, 0, 0);
    textout_ex(screen, font, "3", 320, 80, makecol(255,0,0), makecol(0,0,0));
    textout_ex(screen, font, "3", 320, 320, makecol(255,0,0), makecol(0,0,0));
    rest(1000);
    textout_ex(screen, font, "2", 320, 80, makecol(255,0,0), makecol(0,0,0));
    textout_ex(screen, font, "2", 320, 320, makecol(255,0,0), makecol(0,0,0));
    rest(1000);
    textout_ex(screen, font, "1", 320, 80, makecol(255,0,0), makecol(0,0,0));
    textout_ex(screen, font, "1", 320, 320, makecol(255,0,0), makecol(0,0,0));
    rest(1000);
    textout_ex(screen, font, "GO!!", 320, 80, makecol(0,255,0), makecol(0,0,0));
    textout_ex(screen, font, "GO!!", 320, 320, makecol(0,255,0), makecol(0,0,0));
    rest(1000);}
    
void tpmode_loop(){
    int countd = 1;//for countdown
    fps = 35; //to prevent loop from going too fast at start
    
    while (con){
        if (exec_loop-te>=1 || fps<=22 && cycles>150){
    //physics/gameplay
    get_p1_input();
    get_p2_input();
    handle_bots();
    handle_parts();
    handle_stars();
    pts_4_dead_bots();
    respawn_dead_bots();
    handle_las();
    handle_col();
    
    //rendering
    tpmode_cam1();
    tpmode_cam2();
    draw_2p_screen();
    
    //help prevent lag
    cycles++;
    
    //top-level input
    if (key[KEY_ENTER])
    con = tpmode_pause();
    
    if (countd==1){
    tpmode_countdown(); //3,2,1 stuff
    countd = 2;}
    
    te = exec_loop;}
    calc_fps();}
    //**************END OF LOOP**********************//
    
    p1.score+= secs1 * 500;
    p2.score+= secs2 * 500;
    
    //debreif
    tpmode_after_game();
    drawmen = 1;
    
    //reset for replay
    con = true;}
