/*
    Copyright 2015 Harm Jetten

    This file is part of Moby Dam.

    Moby Dam 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 3 of the License, or
    (at your option) any later version.

    Moby Dam 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 Moby Dam.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "../core/core.h"
#include "pdn.h"
#include "search.h"

/* events that may terminate the search */
typedef union {
    struct {
        /* functionally these are bools, but implemented as */
        /* bytes to avoid read-modify-write instructions for */
        /* safe multi-thread access now that we have separate */
        /* stdin and main threads */
        u8 movenow      : 8;
        u8 connect      : 8;
        u8 gamereq      : 8;
        u8 move         : 8;
        u8 gameend      : 8;
        u8 backreq      : 8;
        u8 cmdexit      : 8;
        u8 cmdend       : 8;
        u8 disconn      : 8;
        u8 hubnewgame   : 8;
        u8 hubpos       : 8;
        u8 hublevel     : 8;
        u8 hubgo        : 8;
        u8 hubponderhit : 8;
    };
    struct {
        u64 any1;
        u64 any2;
    };
} mev;

#define main_event_any ((main_event.any1 | main_event.any2) != 0)

extern mev main_event;

extern char engine_name[];
extern char opponent_name[];

extern u32 move_tick;        /* time of last move made */
extern int playtime_moves;   /* total nr. of moves for game, 0 = unlimited */
extern int playtime_period;  /* time set for game, in milliseconds */
extern int playtime_inc;     /* incremental time per move, in milliseconds */
extern int timeleft_moves;   /* remaining moves */
extern int timeused_period;  /* time used for self, in ms */
extern int timeused_opponent;/* time used for other player, in ms */
extern int oppmoves_first;   /* remaining opponent moves adjustment */
extern u32 move_time;        /* nominal think time for engine */

extern int test_depth;       /* max iterative search depth */
extern u64 test_nodes;       /* max nodes to visit (approximately) */
extern u32 test_time;        /* max search time per move (ms) */

extern int our_side;         /* engine's side in the game */
extern bool game_inprog;     /* game in progress */
extern int side_moving;      /* side to move in current root position */
extern int game_number;      /* game number in current connection */
extern int game_result;      /* from gameend */
extern char pdn_format[];    /* pdn log filename format */
extern bool prot_hub;        /* use hub protocol instead of DamExchange */
extern bool ponder_state;    /* current pondering state */
extern bitboard ponder_move; /* move to ponder upon */
extern bool ponder_hit;      /* opponent did expected move */

extern bool verbose_info;    /* print verbose search info */

void hub_send(char *buf);
void poll_event(int wait);
