
/*
    STB Generator
    Copyright (C) 2006 Gian-Carlo Pascutto
   
    File: iterate.c
    Purpose: iteration building loops
*/

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>

#include "global.h"
#include "utils.h"
#include "board.h"
#include "sfile.h"
#include "index.h"
#include "moves.h"
#include "make.h"

INDEX gen(sfile_s *sf, matsig_s *ms) {
    INDEX maxpos;
    INDEX curpos;
    int changed;
    state_s s;
    move_s moves[MOVE_BUFF];
    int num_moves;
    int legals;
    int score;

    maxpos = index_maxpos(ms);
    
    printf("\tactual_gen: ");
    print_sig(s);
    printf("\n");       
    printf("\t\tSize:  2 * %lld =  %lld\n", maxpos, maxpos * 2);
    
    curpos = 0;
    changed = 0;
    
    
    /*
        initialize conversions
    */
    for (curpos = 0; curpos < maxpos; curpos++) {
    
        index_to_board(curpos, &s, ms);        
        s.white_to_move = 0;
     
        if (!is_broken(s, ms)) {     
            num_moves = movegen(s, moves);
            
            legals = 0;
            
            for (i = 0; i < num_moves; i++) {
                make(s, ms, &moves[i]);
                
                if (check_legal(s, &moves[i]) {
                    legals++;
                    
                    /*
                        probe here if it's a conversion
                    */
                }
                
                unmake(s, ms, &moves[i]);
            }
        } else {
            score = BROKEN;
        }
        
        if (legals == 0) {
            if (in_check(s)) {
                score = -MATE;
            } else {
                score = 0;
            }
        } 
        
    }

/*    
    s.white_to_move = 1;
        
    do {
        for (curpos = 0; curpos < maxpos; curpos++) {
            
        }
    } while (changed);
*/    

    return maxpos * 2;
}
