//pvpmode.cpp                        //contains functions
#include <math.h>
#include <allegro.h>
#include "classes.h"
#include "globals.h"
#include "render.h"
#include "bots.h"
#include "particles.h"
#include "lasers.h"
#include "ships.h"
#include "stars.h"
#include "collisions.h"
#include "2pmode.h"  //for cameras

void pvpmode_init(){
    p1.playership_init(1);
    p1.health = 100;
    p2.playership_init(2);
    p2.health = 100;
    p2.x = 500;
    p2.y = -500;
    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);}}
    
void spawn_killers(){
    if (botc<=50){
    bots.push_back(botship(p1.x+botc*30, p1.y+botc*30, 1));
    bots.push_back(botship(p2.x+botc*30, p2.y+botc*30, 1));}}
    
int check_win(){
    if (p1.health<=0){
        con = false;
        return 2;}
        
    if (p2.health<=0){
        con = false;
        return 1;}
        
    else
    return 0;}
    
void pvp_after_game(int w){
    stop_sample(gback);
    if (mute==1)
    play_sample(menback, 255, 128, 1000, 1);
    rectfill(screen, 0, 0, 640, 480, makecol(60,60,60));
    if (w==1)
    textout_ex(screen, font, "Player 1 WINS!!", 280, 150, makecol(0,0,255), makecol(60,60,60));
    if (w==2)
    textout_ex(screen, font, "Player 2 WINS!!", 280, 150, makecol(0,255,0), makecol(60,60,60));
    textout_ex(screen, font, "Press ESC to go back to the menu", 100, 300, makecol(255,0,0), makecol(60,60,60));
    
    while (!key[KEY_ESC]){
        rest(1);}}
    
void pvpmode_loop(){
    int winner = 0;
    int countd = 1;//for countdown
    fps = 35; //to prevent loop from going too fast at start
    bool play = false; //so screwed only plays once
    
    while (con){
        if (exec_loop-te>=1 || fps<=22 && cycles>150){
    //physics/gameplay
    get_p1_input();
    get_p2_input();
    
    if (botc>0)
    handle_bots();
    if (secs>50){
    if (!play){
    play_sample(screwed, 255, 120, 1000, 1);
    play = true;}
    spawn_killers();} //kills players
    
    handle_parts();
    handle_stars();
    
    pts_4_dead_bots();
    
    handle_las();
    handle_col();
    
    winner = check_win();
    
    //rendering
    tpmode_cam1();
    tpmode_cam2();
    draw_pvp_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();}
    
    con = true;
    ovom = false;
    pvp_after_game(winner);
    drawmen = 1;}
