/* Pepito v1.59  -- file: gentest.c --
   Copyright (c) 2000-02 Carlos del Cacho

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.

   Contact the author: guggenjaimito@yahoo.com
*/

#ifndef UNIQUE_SOURCE_FILE
#  include "datos.h"
#  include <time.h>
#endif

#define DEBUG_LEVEL_1
//#define DEBUG_LEVEL_2

int             TestGen(int ply);
int             LIMIT;

void            Perf()
{
    entero32        i,
                    t,
                    mov_cnt = 0;
    list_jug       *pjug;

    TimeReset();
    for (i = 0; i < 1000000; i++) {
        tab.max_stack[0] = GenCaps(tab.jug_stack);
        tab.max_stack[0] = GenNoCaps(tab.max_stack[0]);
        mov_cnt += tab.max_stack[0] - tab.jug_stack;
    }
    Print("\nTime spent:              %.2f\n", (float) (t = TimeDiff()) / 100);
    Print("Moves generated:         %d\n", mov_cnt);
    Print("Moves per sec:           %.3f\n", (float) mov_cnt / t * 100);

    mov_cnt = 0;
    TimeReset();
    for (i = 0; i < 1000000; i++) {
        tab.max_stack[0] = GenCaps(tab.jug_stack);
        tab.max_stack[0] = GenNoCaps(tab.max_stack[0]);
        mov_cnt += tab.max_stack[0] - tab.jug_stack;
        for (pjug = tab.max_stack[0] - 1; pjug >= tab.jug_stack; pjug--) {
            Mueve(pjug->jug);
            DesMueve();
        }
    }
    Print("\nTime spent:              %.2f\n", (float) (t = TimeDiff()) / 100);
    Print("Moves gen/made/unmade:   %d\n", mov_cnt);
    Print("Moves per sec:           %.3f\n", (float) mov_cnt / t * 100);
}


// las funciones de este archivo se usan para depurar el generador de jugadas
// y probar su velocidad...

void            RunTestGen(int ply, int xb_mode)
{
    char *tellstr = xb_mode ? "telluser ": "";
    tab.nodos_buscados = 0;
    tab.max_stack[0] = tab.jug_stack;
    tab.jaque[1] = Jaque(tab.pos.turno);
    TimeReset();
    LIMIT = ply;
    TestGen(1);
    Print("\n%sPaths searched: %ld",tellstr, tab.nodos_buscados);

    Print("%sTime spent: %0.2f\n", xb_mode ? ";  ":"\n",(float) TimeDiff() / 100);

//   if (!VerificaTab(Turno))
//    exit(1);

}
//-----------------------------------------------------------------------------------
int             TestGen(int ply)
{
    list_jug       *pos_stack;
#ifdef DEBUG_LEVEL_1
    FILE           *pfich;
    long            paths_bkup;
    int             i = 0;
#endif
    if (ply > LIMIT) {
        tab.nodos_buscados++;
        return 0;
    }
    if (tab.jaque[ply])
        tab.max_stack[ply] = GenJaqJugs(tab.max_stack[ply - 1]);
    else {
        tab.max_stack[ply] = GenCaps(tab.max_stack[ply - 1]);
        tab.max_stack[ply] = GenNoCaps(tab.max_stack[ply]);
    }

    for (pos_stack = tab.max_stack[ply - 1]; pos_stack < tab.max_stack[ply]; pos_stack++) {
        Mueve(pos_stack->jug);

        if (Jaque(Turno))
            goto NO_VALIDA;

        CambiaTurno();

        tab.jaque[ply + 1] = Jaque(Turno);

#ifdef DEBUG_LEVEL_2
        {
            desde = pos_stack->jug & 0x3f;
            hacia = (pos_stack->jug >> 6) & 0x3f;
            Print("%d. %c%d-%c%d\n", i, 'a' + (desde & 7), 8 - (desde >> 3), 'a' + (hacia & 7), 8 - (hacia >> 3));
        }
#endif


#ifdef DEBUG_LEVEL_1
        if (ply == 1) {
            OutFEN("root_plys");
            paths_bkup = tab.nodos_buscados;
        }
#endif

        TestGen(ply + 1);

#ifdef DEBUG_LEVEL_1
        if (ply == 1) {
            pfich = fopen("root_plys", "a");
            fprintf(pfich, " %ld", tab.nodos_buscados - paths_bkup);
            fclose(pfich);
        }
#endif
        CambiaTurno();
NO_VALIDA:
        DesMueve();
    }
    return 0;
}
