//render.cpp                       //contains ALL drawing code
#include <allegro.h>
#include "classes.h"
#include "1pmode.h"
#include "2pmode.h"
#include "globals.h"
#include "lasers.h"
#include "ships.h"
#include "bots.h"

void draw_bots(){
    for (int c1 = 0; c1<bots.size(); c1++){
        if (bots[c1].health>0){//so we dont draw dead bots
        
        if (bots[c1].type==1){
        pivot_sprite(buffer, bship1, bots[c1].sx1, bots[c1].sy1, 10, 15, itofix(bots[c1].dir*8));
        if (mode==2)
        pivot_sprite(buffer, bship1, bots[c1].sx2, bots[c1].sy2, 10, 15, itofix(bots[c1].dir*8));}
        
        if (bots[c1].type==2){
        pivot_sprite(buffer, bship2, bots[c1].sx1, bots[c1].sy1, 17, 16, itofix(bots[c1].dir*8));
        if (mode==2)
        pivot_sprite(buffer, bship2, bots[c1].sx2, bots[c1].sy2, 17, 16, itofix(bots[c1].dir*8));}
        
        if (bots[c1].type==3){
        pivot_sprite(buffer, bship3, bots[c1].sx1, bots[c1].sy1, 30, 30, itofix(bots[c1].dir*8));
        if (mode==2)
        pivot_sprite(buffer, bship3, bots[c1].sx2, bots[c1].sy2, 30, 30, itofix(bots[c1].dir*8));}
        
        if (bots[c1].type==4){
        pivot_sprite(buffer, bship4, bots[c1].sx1, bots[c1].sy1, 55, 55, itofix(bots[c1].dir*8));
        if (mode==2)
        pivot_sprite(buffer, bship4, bots[c1].sx2, bots[c1].sy2, 55, 55, itofix(bots[c1].dir*8));}
        
        if (bots[c1].type==5){
        pivot_sprite(buffer, bship5, bots[c1].sx1, bots[c1].sy1, 70, 70, itofix(bots[c1].dir*8));
        if (mode==2)
        pivot_sprite(buffer, bship5, bots[c1].sx2, bots[c1].sy2, 70, 70, itofix(bots[c1].dir*8));}}}}

void draw_las(){
    for (int c1 = 0; c1<las.size(); c1++){
        if (las[c1].x!=0){//attempt to not draw unshot lasers
            draw_sprite(buffer, lasim, las[c1].sx1, las[c1].sy1);
            if (mode==2)
            draw_sprite(buffer, lasim, las[c1].sx2, las[c1].sy2);}}}

void draw_players(){
    if (mode==1)
    pivot_sprite(buffer, pship1, 320, 240, 30, 30, itofix(p1.dir*8));
    if (mode==2){
    if (p1.health>0){
    pivot_sprite(buffer, pship1, 320, 120, 30, 30, itofix(p1.dir*8));
    pivot_sprite(buffer, pship1, p1.sx1, p1.sy1, 30, 30, itofix(p1.dir*8));}
    else
    textout_ex(buffer, font, "DEAD", 320, 120, makecol(255,0,0), makecol(0,0,0));
    if (p2.health>0){
    pivot_sprite(buffer, pship2, 320, 360, 30, 30, itofix(p2.dir*8));
    pivot_sprite(buffer, pship2, p2.sx1, p2.sy1, 30, 30, itofix(p2.dir*8));}
    else
    textout_ex(buffer, font, "DEAD", 320, 360, makecol(255,0,0), makecol(0,0,0));}}
    
void draw_1pm_hud(){
    //health display
    textout_ex(buffer, font, "Health", 345, 10, makecol(255,0,0), makecol(0,0,0));
    rectfill(buffer, 270, 25, 270 + p1.health * 3, 50, makecol(0,255,0));
    rect(buffer, 269, 24, 480, 51, makecol(255,0,0));
    
    //timer
    rect(buffer, 518, 8, 639, 25, makecol(255,0,0));
    textout_ex(buffer, font, "Time alive:", 520, 10, makecol(255,0,0), makecol(0,0,0));
    textprintf(buffer ,font, 610, 10, makecol(223,238,45), "%i", secs1);
    
    //bot numbers
    rect(buffer, 138, 438, 240, 479, makecol(255,0,0));
    textout_ex(buffer, font, "Bots Alive:", 150, 440, makecol(255,0,0), makecol(0,0,0));
    textprintf(buffer ,font, 170, 460, makecol(255,0,0), "%i", botc);
    
    rect(buffer, 438, 438, 560, 479, makecol(0,255,0));
    textout_ex(buffer, font, "Bots killed:", 450, 440, makecol(0,255,0), makecol(0,0,0));
    textprintf(buffer ,font, 465, 460, makecol(0,255,0), "%i", kb);
    
    //fps
    rect(buffer, 88, 8, 122, 45, makecol(255,0,0));
    textout_ex(buffer, font, "FPS:", 90, 10, makecol(255,0,0), makecol(0,0,0));
    textprintf(buffer ,font, 95, 27, makecol(223,238,45), "%i", fps);
    
    //score
    rect(buffer, 138, 8, 190, 45, makecol(230,230,40));
    textout_ex(buffer, font, "Score:", 140, 10, makecol(230,230,40), makecol(0,0,0));
    textprintf(buffer ,font, 150, 27, makecol(230,230,40), "%i", p1.score);
    
    //position
    rect(buffer, 8, 8, 75, 40, makecol(255,0,0));
    textout_ex(buffer, font, "Position", 10, 10, makecol(255,0,0), makecol(0,0,0));
    textprintf(buffer ,font, 10, 25, makecol(223,238,45), "%i", p1.x);
    textprintf(buffer ,font, 42, 25, makecol(223,238,45), "%i", p1.y);}
    
void draw_2pm_hud(){
    //*********************player 1 HUD**************************//
    //health display
    if (!ovom){
    textout_ex(buffer, font, "Health", 300, 10, makecol(255,0,0), makecol(0,0,0));
    if (p1.health>0)
    rectfill(buffer, 270, 25, 270 + p1.health * 3, 50, makecol(0,255,0));
    rect(buffer, 269, 24, 420, 51, makecol(255,0,0));}
    
    if (ovom){
    textout_ex(buffer, font, "Health", 270, 10, makecol(255,0,0), makecol(0,0,0));
    if (p1.health>0)
    rectfill(buffer, 140, 25, 140 + p1.health * 3, 50, makecol(0,255,0));
    rect(buffer, 139, 24, 441, 51, makecol(255,0,0));}
    
    //timer
    rect(buffer, 518, 8, 639, 25, makecol(255,0,0));
    textout_ex(buffer, font, "Time alive:", 520, 10, makecol(255,0,0), makecol(0,0,0));
    textprintf(buffer ,font, 610, 10, makecol(223,238,45), "%i", secs1);
    
    if (!ovom){
    //score
    rect(buffer, 138, 8, 190, 45, makecol(230,230,40));
    textout_ex(buffer, font, "Score:", 140, 10, makecol(230,230,40), makecol(0,0,0));
    textprintf(buffer ,font, 150, 27, makecol(230,230,40), "%i", p1.score);}
    
    //position
    rect(buffer, 8, 8, 75, 40, makecol(255,0,0));
    textout_ex(buffer, font, "Position", 10, 10, makecol(255,0,0), makecol(0,0,0));
    textprintf(buffer ,font, 10, 25, makecol(223,238,45), "%i", p1.x);
    textprintf(buffer ,font, 42, 25, makecol(223,238,45), "%i", p1.y);
    
    //**************************Player 2 HUD*********************************//
    //health display
    if (!ovom){
    textout_ex(buffer, font, "Health", 300, 260, makecol(255,0,0), makecol(0,0,0));
    if (p2.health>0)
    rectfill(buffer, 270, 275, 270 + p2.health * 3, 300, makecol(0,255,0));
    rect(buffer, 269, 274, 420, 301, makecol(255,0,0));}
    
    if (ovom){
    textout_ex(buffer, font, "Health", 270, 260, makecol(255,0,0), makecol(0,0,0));
    if (p2.health>0)
    rectfill(buffer, 140, 275, 140 + p2.health * 3, 300, makecol(0,255,0));
    rect(buffer, 139, 274, 441, 301, makecol(255,0,0));}
    
    //timer
    rect(buffer, 1, 260, 122, 277, makecol(255,0,0));
    textout_ex(buffer, font, "Time alive:", 3, 262, makecol(255,0,0), makecol(0,0,0));
    textprintf(buffer ,font, 93, 262, makecol(223,238,45), "%i", secs2);
    
    if (!ovom){
    //score
    rect(buffer, 138, 263, 190, 300, makecol(230,230,40));
    textout_ex(buffer, font, "Score:", 140, 265, makecol(230,230,40), makecol(0,0,0));
    textprintf(buffer ,font, 150, 282, makecol(230,230,40), "%i", p2.score);}
    
    //position
    rect(buffer, 8, 437, 75, 479, makecol(255,0,0));
    textout_ex(buffer, font, "Position", 10, 449, makecol(255,0,0), makecol(0,0,0));
    textprintf(buffer ,font, 10, 464, makecol(223,238,45), "%i", p2.x);
    textprintf(buffer ,font, 42, 464, makecol(223,238,45), "%i", p2.y);
    
    //**************************both players******************************//
    if (!ovom){
    //bot numbers
    rect(buffer, 538, 180, 639, 221, makecol(255,0,0));
    textout_ex(buffer, font, "Bots Alive:", 550, 182, makecol(255,0,0), makecol(0,0,0));
    textprintf(buffer ,font, 570, 202, makecol(255,0,0), "%i", botc);
    
    rect(buffer, 518, 260, 639, 301, makecol(0,255,0));
    textout_ex(buffer, font, "Bots killed:", 530, 262, makecol(0,255,0), makecol(0,0,0));
    textprintf(buffer ,font, 545, 282, makecol(0,255,0), "%i", kb);}
    
    //fps
    rect(buffer, 1, 179, 40, 220, makecol(255,0,0));
    textout_ex(buffer, font, "FPS:", 5, 181, makecol(255,0,0), makecol(0,0,0));
    textprintf(buffer ,font, 17, 201, makecol(223,238,45), "%i", fps);}

void draw_bots_health_bars(){
    for (int c1 = 0; c1<bots.size(); c1++){
        if (bots[c1].health>0){
            rectfill(buffer, bots[c1].sx1-bots[c1].rad, bots[c1].sy1-bots[c1].rad, bots[c1].sx1+bots[c1].health*2-bots[c1].rad, bots[c1].sy1+8-bots[c1].rad, makecol(255,0,0));
            if (mode==2)
            rectfill(buffer, bots[c1].sx2-bots[c1].rad, bots[c1].sy2-bots[c1].rad, bots[c1].sx2+bots[c1].health*2-bots[c1].rad, bots[c1].sy2+8-bots[c1].rad, makecol(255,0,0));}}}
            
void draw_parts(){
    for (int c1 = 0; c1<parts.size(); c1++){
        pivot_sprite(buffer, part, parts[c1].sx1, parts[c1].sy1, 15, 7, itofix(parts[c1].dir*8));
        if (mode==2)
        pivot_sprite(buffer, part, parts[c1].sx2, parts[c1].sy2, 15, 7, itofix(parts[c1].dir*8));}}
        
void draw_stars(){
    for (int c1 = 0; c1<p1_stars.size(); c1++){
        putpixel(buffer, p1_stars[c1].sx1, p1_stars[c1].sy1, makecol(255,255,255));}
        
    if (mode==2){
        for (int c1 = 0; c1<p2_stars.size(); c1++){
            putpixel(buffer, p2_stars[c1].sx2, p2_stars[c1].sy2, makecol(255,255,255));}}}


void draw_1p_screen(){
    rectfill(buffer, 0, 0, 640, 480, makecol(0,0,0));
    draw_stars();
    draw_bots();
    draw_las();
    draw_parts();
    draw_bots_health_bars();
    draw_players();
    draw_1pm_hud();
    draw_sprite(screen, buffer, 0, 0);}
    
void draw_2p_screen(){
    rectfill(buffer, 0, 0, 640, 480, makecol(0,0,0));
    draw_stars();
    draw_bots();
    draw_las();
    draw_parts();
    draw_bots_health_bars();
    draw_players();
    draw_2pm_hud();
    rectfill(buffer, 0, 237, 640, 243, makecol(0,0,255)); //draws divider
    draw_sprite(screen, buffer, 0, 0);}
    
void draw_pvp_screen(){
    rectfill(buffer, 0, 0, 640, 480, makecol(0,0,0));
    draw_stars();
    if (botc>0){
    draw_bots();
    draw_bots_health_bars();}
    draw_las();
    draw_parts();
    draw_players();
    draw_2pm_hud(); //execution is modified by ovom
    rectfill(buffer, 0, 237, 640, 243, makecol(0,0,255));
    draw_sprite(screen, buffer, 0, 0);}
