//bots.cpp                          //contains artificial inteligence stuff
#include <math.h>
#include <allegro.h>
#include <cstdlib>
#include <time.h>
#include "classes.h"
#include "1pmode.h"
#include "globals.h"
#include "lasers.h"
#include "ships.h"
#include "collisions.h"

#define pie 3.14159265

void pts_4_dead_bots(){
    for (int c1 = 0; c1<bots.size(); c1++){
        if (bots[c1].health<=0 && bots[c1].isdead==false){
            bots[c1].isdead = true;
            botc--;
            kb++;
            if (bots[c1].type==5){
            p1.score+=500;
            p2.score+=500;}
            if (bots[c1].type==4){
            p1.score+=400;
            p2.score+=400;}
            if (bots[c1].type==3){
            p1.score+=300;
            p2.score+=300;}
            if (bots[c1].type==2){
            p1.score+=200;
            p2.score+=200;}
            if (bots[c1].type==1){
            p1.score+=100;
            p2.score+=100;}}}}
            
void respawn_dead_bots(){
    if (!ovom){
    int t;
    srand(time(NULL));
    for (int c1 = bots.size(); c1>0; c1--){
        if (bots[c1].isdead==true && botc<15){
            bots.erase(bots.begin()+c1);
            while (botc<30){
            t = rand() % 5 + 1;
            if (mode==2 && p2.health>0)
            bots.push_back(botship(botc*100 + p2.x, botc*100 + p2.y, t));
            else
            bots.push_back(botship(botc*100 + p1.x, botc*100 + p1.y, t));}
            return;}}}}

botship::botship(int xx, int yy, int tp){
    x = xx;
    y = yy;
    dir = 5;
    isdead = false;
    srand(time(NULL));
    
    if (!ovom){
    if (p1.x==-500){//so this only happens once at start
        if (botc==1)
        type = 5;
        else if (botc<4)
        type = 4;
        else if (botc<7)
        type = 3;
        else if (botc<13)
        type = 2;
        else 
        type = 1;}
        
    else
        type = tp;} //!ovom end
        
    if (ovom)
    type = 1;
        
    if (type==1){
        health = 10;
        tim = 1;
        rad = 11;}
    if (type==2){
        health = 20;
        tim = 3;
        rad = 17;}
    if (type==3){
        health = 40;
        tim = 4;
        rad = 28;}
    if (type==4){
        health = 70;
        tim = 6;
        rad = 50;}
    if (type==5){
        health = 100;
        tim = 8;  //goes once per 8 loops
        rad = 55;}
        
    botc++;}
    
    
void botship::action(int cx, int cy){
    double angle, cangle, dis; //angle is needed angle, cangle is current-angle
    int dx, dy; //delta x,y
    
    dis = abs( sqrt( sqrd(cx-x) + sqrd(cy-y)));
    
    dx = abs(x - cx);
    dy = abs(y - cy);
    
    if (y>cy && x>cx)
    angle = atan2(dy,dx) * 180 / pie + 270;
    else
    angle = atan2(dy,dx) * 180 / pie + 90;
    
    cangle = dir * 11.25;
    
    if (dis-p1.rad-rad>5) //checks how far away bot is, then moves
    change_xy(dir, 1);
    
    if (cangle-angle>8)  //rotation
    dir--;
    if (cangle-angle<-8)
    dir++;
    
    //shooting
    if (cangle-angle>-5 && cangle-angle<5 && dis<400){
    if (type==1){
        if (shooting==0 && dis<200){
        if (mute==1)
        play_sample(lassfx, 225, 128, 1000, 0);
        shoot();
        shooting = 3;}}
    else {
        if (mute==1)
        play_sample(lassfx, 225, 128, 1000, 0);
        shoot();}}
    
    if (shooting>0)
    shooting--;
    if (dir>32) //reset dir
    dir = 0;
    if (dir<0)
    dir = 32;}
    
void handle_bots(){
    int dis1, dis2; //distance from player 1 and 2
    //finds closer player and calls action()
    for (int c1 = 0; c1<bots.size(); c1++){
        if (bots[c1].health>0){//not moving dead bots
        
        if (mode==2){//finds closer player
        
            if (p1.health>0)//not dead
            dis1 = abs( sqrt( sqrd(bots[c1].x-p1.x) + sqrd(bots[c1].y-p1.y)));
            else if (p1.health<=0)//dead
            dis1 = 10000;
            
            if (p2.health>0) //not dead
            dis2 = abs( sqrt( sqrd(bots[c1].x-p2.x) + sqrd(bots[c1].y-p2.y)));
            else if (p2.health<=0)//dead
            dis2 = 10000;
            
            if (bots[c1].tim>0)
            bots[c1].tim--;
            
            if (bots[c1].tim==0 || bots[c1].type==1){
                
            if (dis1>dis2)
                bots[c1].action(p2.x, p2.y);
            else if (dis1<dis2)
                bots[c1].action(p1.x, p1.y);
                
            if (bots[c1].type==1)
            bots[c1].tim = 0;
            if (bots[c1].type==2)
            bots[c1].tim = 2;
            if (bots[c1].type==3)
            bots[c1].tim = 4;
            if (bots[c1].type==4)
            bots[c1].tim = 6;
            else
            bots[c1].tim = 8;}}
                
        else { //mode 1
            bots[c1].tim--;
            if (bots[c1].tim==0 || bots[c1].type==1){
            bots[c1].action(p1.x, p1.y);
            
            if (bots[c1].type==1)
            bots[c1].tim = 1;
            if (bots[c1].type==2)
            bots[c1].tim = 3;
            if (bots[c1].type==3)
            bots[c1].tim = 4;
            if (bots[c1].type==4)
            bots[c1].tim = 6;
            else
            bots[c1].tim = 8;}}}}}
