#include <stdio.h>
#include "ramjet.h"
#include "move.h"
#include "position.h"
#include "stats.h"

// reset the statistics to their initial values
void stat_clear(STATS* stat)
{
    stat->start = time_now();
    stat->nodes = 0;
    stat->depth = 0;
    stat->curr_mv = MV_NULL;
    stat->curr_n = 0;
    stat->stint_ref = 0;
    stat->stint_len = 256;
}

// increment the node counter
void stat_nodes_inc(STATS* stat)
{
    ++(stat->nodes);
}

// increment the depth counter
void stat_depth_inc(STATS* stat)
{
    ++(stat->depth);
}

// set the current move
void stat_set_curr_mv(STATS* stat, MOVE mv, int n)
{
    stat->curr_mv = mv;
    stat->curr_n = n;
}

// set the stint reference to the current node count
void stat_set_stint_ref(STATS* stat)
{
    stat->stint_ref = stat->nodes;
}

// double the stint length
void stat_double_stint_len(STATS* stat)
{
    stat->stint_len *= 2;
}

// check whether enough nodes have been searched
int is_stint_over(STATS* stat)
{
    return (stat->nodes >= stat->stint_ref + stat->stint_len);
}

// write nodes, time elapsed and speed in UCI format
void stat_print_uci_speed(char* uci, const STATS* stat)
{
    MILLISEC ms = time_diff(stat->start);
    float nps = (float) stat->nodes * 1000.0 / ((float) ms + 1);
    sprintf(uci, "nodes %"NC_PRINT" time %"MS_PRINT" nps %.0f", stat->nodes, ms, nps);
}

// write the current depth in UCI format
void stat_print_uci_depth(char* uci, const STATS* stat)
{
    sprintf(uci, "depth %d", stat->depth);
}

// write the current move in UCI format
void stat_print_uci_curr_mv(char* uci, const STATS* stat, const POSITION* pos, int c960)
{
    sprintf(uci, "info currmovenumber %d currmove", stat->curr_n);
    mv_print_uci(append_space(uci), &pos->qb, pos->st, c960, stat->curr_mv);
}
