
//===========================================================================
//
// Name:			bb_cg_bothud.c
//===========================================================================

#include "cg_local.h"

#include "bb_cg_hud.h"
#include "bb_cg_bothud.h"

#define  BB_BOTHUD_MAX_FIELD_WIDTH		15

typedef enum
{
    INFO_BA,
    INFO_BB,
    INFO_BC,
    INFO_BD,
    INFO_BE,
    INFO_BF,
    INFO_BG,
    INFO_BH,

    INFO_MAX
} info_t;

struct {
    char *key;
    char *label;
}

meeleInfo[INFO_MAX] =  {
                           { "ba",		"Enemy     = " },
                           { "bb",		"Behavior  = " },
                           { "bc",		"Enemy Hght= " },
                           { "bd",		"Aggr C/B  = " },
                           { "be",		"Bot Org   = " },
                           { "bf",		"SelfP C/B = " },
                           { "bg",		"AI Node   = " },
                           { "bh",		"Enemy Dist= " }
                       };

struct {
    char *key;
    char *label;
}

goalInfo[INFO_MAX] =  {
                          { "ba",		"Close Item= " },
                          { "bb",		"Close Dist= " },
                          { "bc",		"Far Item  = " },
                          { "bd",		"Far Dist  = " },
                          { "be",		"Goal      = " },
                          { "bf",		"Goal Dist = " },
                          { "bg",		"AI Node   = " },
                          { "bh",		"Goal Org  = " }
                      };



/*
 
INCIN -- After running this thru a few games i was starting to get 
command overflows from this even before the game loaded..
You might want to do this before new map reloads, go thru all 
clients and force set cg_drawbothud to 0
 
  Also put in protection to make sure client viewing is a spectator, 
even tho i was in the game as a player at game end on map reload with cg_drawbothud 1
when the first bots loaded i was getting Config string updates while i wasnt speccing a bot at all.
 
*/

// draw the bot hud
void BB_BOTHUD_DrawBotHud (void) {
    char		buff[60];
    char		info[INFO_MAX][MAX_INFO_VALUE];
    int			i, skill;
    float		x, y;
    vec4_t		color;
    const char	*rawInfo;



    // don't draw any status if dead
    if ( cg.snap->ps.stats[STAT_HEALTH] <= 0 )
        return;

    // if not a bot, skill = 0
    rawInfo = CG_ConfigString( CS_PLAYERS + cg.snap->ps.clientNum );
    skill = atoi(Info_ValueForKey(  rawInfo, "skill" ));

    // don't draw if not following a bot
    if (!cg_drawBothud.integer || !skill) //INCIN --- ADD and spectating
        return;

    //if(cg.time < 10000)
    //	Com_Printf("%i\n", cg.time);

    // process the data
    rawInfo = CG_ConfigString( BB_BOTHUD_CS_BOTS + cg.snap->ps.clientNum );
    if (strlen (rawInfo)) {
        // parse the config string
        for (i = 0; i < INFO_MAX; i++) {

            if (cg_drawBothud.integer == 1) {
                // get some data
                //strcpy (info[i], Info_ValueForKey(rawInfo, meeleInfo[i].key));
                Q_strncpyz(info[i], Info_ValueForKey(rawInfo, meeleInfo[i].key), sizeof(info[i]));

                // limit the size of the data
                if (strlen (info[i]) > BB_BOTHUD_MAX_FIELD_WIDTH)
                    info[i][BB_BOTHUD_MAX_FIELD_WIDTH] = 0;

                // make it look fancy
                if (i == INFO_BB)
                    if (atoi(info[i]))
                        Q_strncpyz(info[i], "aggressive", sizeof(info[i]));
                    else
                        Q_strncpyz(info[i], "passive", sizeof(info[i]));

            } else if (cg_drawBothud.integer == 2) {

                // get some data
                Q_strncpyz (info[i], Info_ValueForKey(rawInfo, goalInfo[i].key), sizeof(info[i]));

                // limit the size of the data
                if (strlen (info[i]) > BB_BOTHUD_MAX_FIELD_WIDTH)
                    info[i][BB_BOTHUD_MAX_FIELD_WIDTH] = 0;

            }
        }

        // set the color to blue
        color[0] = 0.0;
        color[1] = 0.0;
        color[2] = 1.0;
        color[3] = 1.0;

        // starting vertical position
        y = 450 - ICON_SIZE - (INFO_MAX * TINYCHAR_HEIGHT) + 5;

        if (cg_drawBothud.integer == 1) {

            x = 200 - ((CG_DrawStrlen( "Meele Data" ) * TINYCHAR_WIDTH) / 2);
            BB_HUD_DrawStringExt( 310, y, "Meele Data", color, qtrue, qtrue,
                                  TINYCHAR_WIDTH, TINYCHAR_HEIGHT, 0 );
        } else if (cg_drawBothud.integer == 2) {

            x = 200 - ((CG_DrawStrlen( "Goal Data" ) * TINYCHAR_WIDTH) / 2);
            BB_HUD_DrawStringExt( 310, y, "Goal Data", color, qtrue, qtrue,
                                  TINYCHAR_WIDTH, TINYCHAR_HEIGHT, 0 );
        }

        // set the color to white
        color[0] = 1.0;
        color[1] = 1.0;
        color[2] = 1.0;
        color[3] = 1.0;

        // starting vertical position
        y = 460 - ICON_SIZE - (INFO_MAX * TINYCHAR_HEIGHT) + 5;

        // print the information
        for (i = 0; i < INFO_MAX; i+=2) {
            // build a row
            // - must set minimum field size to BB_BOTHUD_MAX_FIELD_WIDTH

            if (cg_drawBothud.integer == 1) {

                Com_sprintf (buff, sizeof(buff), "%s%15s | %s%15s",
                             meeleInfo[i].label, info[i],
                             meeleInfo[i+1].label, info[i+1]);

            } else if (cg_drawBothud.integer == 2) {

                Com_sprintf (buff, sizeof(buff), "%s%15s | %s%15s",
                             goalInfo[i].label, info[i],
                             goalInfo[i+1].label, info[i+1]);

            }
            // calculate the horizontal start position 320
            x = 320 - ((CG_DrawStrlen( buff ) * TINYCHAR_WIDTH) / 2);
            // print the data
            BB_HUD_DrawStringExt( x, y, buff, color, qtrue, qtrue,
                                  TINYCHAR_WIDTH, TINYCHAR_HEIGHT, 0 );
            // go to the next row
            y += TINYCHAR_HEIGHT;
        }
    } // if (strlen (rawInfo))
    else {
        // starting vertical position
        y = 460 - ICON_SIZE - (INFO_MAX * TINYCHAR_HEIGHT) + 5;
        // bot hud not enabled on the server
        x = 320 - ((CG_DrawStrlen( "No Data!" ) * TINYCHAR_WIDTH) / 2);
        BB_HUD_DrawStringExt( x, y, "No data!", color, qtrue, qtrue,
                              TINYCHAR_WIDTH, TINYCHAR_HEIGHT, 0 );
    } // if (strlen (rawInfo)) else
} //BB_BOTHUD_DrawBotHud ()
