//collision.cpp                         //check_col functions here
#include <allegro.h>
#include <math.h>
#include "ships.h"
#include "1pmode.h"
#include "2pmode.h"
#include "globals.h"
#include "bots.h"
#include "lasers.h"
#include "ships.h"
#include "classes.h"


void ship::fix_col(){
    if (mute==1)
    play_sample(col, 140, 128, 1000, 0);
    int tdir;
    //****************//
    if (dir>=16)  //dir is 0-32
    tdir = dir - 16;
    else
    tdir = dir + 16;
    //****************//
    change_xy(tdir, 7); //send ship backwards pretty far
    health-=5;}

void ship::las_hit(){
    if (mute==1)
    play_sample(lashit, 190, 128, 1000, 0);
    health-=3;
    htim = 150;}

void handle_col(){
    int dis;
    
    if (mode==2){  //for player to player if there is 2 players
        dis = abs( sqrt( sqrd(p1.x-p2.x) + sqrd(p1.y-p2.y)));
        dis-=p1.rad;
        dis-=p2.rad;
        if (dis<=0 && p1.health>0 && p2.health>0){
            if (mute==1)
            play_sample(col, 215, 128, 1000, 0);
            p1.health-=3;
            p1.htim = 150;
            p2.htim = 150;
            p2.fix_col();}}
            
    //***********************************************************************//
    
    //this loop is for player to bots collisions
    if (botc>0){
    for (int c1 = 0; c1<bots.size(); c1++){
        if (bots[c1].x!=0){ //makes sure a bot is in the vector (haven't gotton to erasing dead bots)
        if (bots[c1].health>0){  //so we don't check for dead bots
            dis = abs( sqrt( sqrd(bots[c1].x-p1.x) + sqrd(bots[c1].y-p1.y)));
            dis-=bots[c1].rad;
            dis-=p1.rad;
            if (dis<=0 && p1.health>0){
                p1.health-=3;
                p1.htim = 150;
                bots[c1].fix_col();}}
            
        if (mode==2){
            dis = abs( sqrt( sqrd(bots[c1].x-p2.x) + sqrd(bots[c1].y-p2.y)));
            dis-=p2.rad;
            dis-=bots[c1].rad;
            if (dis<=0 && p2.health>0){
                p2.health-=3;
                p2.htim = 150;
                bots[c1].fix_col();}}}}}
                
          //*********************************************************************// 
               
    if (las.size()>0){//to prevent segfault
    for (int c1 = 0; c1<las.size(); c1++){   //this loop is for lasers to players
        if (p1.health>0){
            dis = abs( sqrt( sqrd(las[c1].x-p1.x) + sqrd(las[c1].y-p1.y)));
            dis-=2;
            dis-=p1.rad;
            if (dis<=0 && p1.health>0){
                las.erase(las.begin()+c1);
                p1.las_hit();}}
                
        if (mode==2){
            dis = abs( sqrt( sqrd(las[c1].x-p2.x) + sqrd(las[c1].y-p2.y)));
            dis-=2;
            dis-=p2.rad;
            if (dis<=0 && p2.health>0){
                las.erase(las.begin()+c1);
                p2.las_hit();}}}}
            
            //************************************************************************//
         
         //these loops are for bot-to-bot collisions   
    for (int c1 = 0; c1<bots.size(); c1++){
        for (int c2 = 0; c2<bots.size(); c2++){
            if (c1!=c2){ //so bots aren't checked against themselves
                if (bots[c1].x!=0 && bots[c2].x!=0){//not checking empty blocks
                    if (bots[c1].health>0 && bots[c2].health>0){//not checking dead bots
                    dis = abs( sqrt( sqrd(bots[c1].x-bots[c2].x) + sqrd(bots[c1].y-bots[c2].y)));
                    dis-=bots[c1].rad;
                    dis-=bots[c2].rad;
                    if (dis<=0){
                        bots[c1].fix_col();
                        bots[c2].fix_col();}}}}}}
                        
    //*****************************************************************************//
    
        //these loops are for laser-to-bots collisions
    for (int c1 = 0; c1<bots.size(); c1++){
        for (int c2 = 0; c2<las.size(); c2++){
            if (bots[c1].x!=0 && las[c2].x!=0){ //so we don't check empty blocks
                if (bots[c1].health>0){//so we don't check for dead bots
                    dis = abs( sqrt( sqrd(bots[c1].x-las[c2].x) + sqrd(bots[c1].y-las[c2].y)));
                    dis-=2;
                    dis-=bots[c1].rad;
                    if (dis<=0){
                        bots[c1].las_hit();
                        las.erase(las.begin()+c2);}}}}}     }
