~ubuntu-branches/ubuntu/natty/gtkboard/natty

« back to all changes in this revision

Viewing changes to .pc/debian-changes-0.11pre0+cvs.2003.11.02-2/src/engine.c

  • Committer: Bazaar Package Importer
  • Author(s): Barak A. Pearlmutter
  • Date: 2011-02-28 11:25:02 UTC
  • mto: This revision was merged to the branch mainline in revision 10.
  • Revision ID: james.westby@ubuntu.com-20110228112502-e9aah248wxelm7ao
Tags: 0.11pre0+cvs.2003.11.02-2
autotools tweaks, most notably -lSDL to supplement -lSDL_mixer

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  This file is a part of gtkboard, a board games system.
 
2
    Copyright (C) 2003, Arvind Narayanan <arvindn@users.sourceforge.net>
 
3
 
 
4
    This program is free software; you can redistribute it and/or modify
 
5
    it under the terms of the GNU General Public License as published by
 
6
    the Free Software Foundation; either version 2 of the License, or
 
7
    (at your option) any later version.
 
8
 
 
9
    This program is distributed in the hope that it will be useful,
 
10
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
    GNU General Public License for more details.
 
13
 
 
14
    You should have received a copy of the GNU General Public License
 
15
    along with this program; if not, write to the Free Software
 
16
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
 
17
 
 
18
*/
 
19
/** \file engine.c
 
20
 \brief The engine forms the backend that does the number crunching
 
21
 */
 
22
#include "config.h"
 
23
 
 
24
#include "game.h"
 
25
#include "move.h"
 
26
#include "stack.h"
 
27
#include "engine.h"
 
28
 
 
29
#include <signal.h>
 
30
#include <string.h>
 
31
#include <stdlib.h>
 
32
#include <assert.h>
 
33
#include <unistd.h>
 
34
 
 
35
extern int board_wid, board_heit;
 
36
extern int opt_verbose;
 
37
//extern int state_player;
 
38
 
 
39
// FIXME: this is ugly. Code should be refactored.
 
40
extern gboolean engine_flag;
 
41
 
 
42
extern Pos cur_pos;
 
43
 
 
44
extern Game *opt_game, *games[];
 
45
extern int num_games;
 
46
byte * engine_search (Pos *);
 
47
static FILE *engine_fin, *engine_fout;
 
48
 
 
49
//! Eval fn for white (can be NULL, in which case game_eval will be used for both)
 
50
ResultType (*game_eval_white) (Pos *, Player, float *);
 
51
//! Eval fn for black (can be NULL, in which case game_eval will be used for both)
 
52
ResultType (*game_eval_black) (Pos *, Player, float *);
 
53
 
 
54
// FIXME: following 3 extern decls must be removed by refactoring (i.e, move all fns common to client and server to a new file)
 
55
extern void reset_game_params ();
 
56
extern void set_game_params ();
 
57
extern void game_set_init_pos_def (Pos *);
 
58
 
 
59
//! Alpha-beta search function (using depth first iterative deepening).
 
60
extern byte *ab_dfid (Pos *, int);
 
61
 
 
62
//! The input pipe is accessed through a GIOChannel so that we can register a callback for events
 
63
static GIOChannel *channel_in = NULL;
 
64
 
 
65
//! If an event occurs when we are thinking this will be set to TRUE so that we will know to stop thinking
 
66
gboolean engine_stop_search = FALSE;
 
67
 
 
68
//! Indicates whether we have to stop and return the move or stop and cancel the move
 
69
static gboolean cancel_move = FALSE;
 
70
 
 
71
//! Max time per move. alpha-beta will often return earlier than this.
 
72
int time_per_move = 5000;
 
73
 
 
74
gboolean engine_hup_cb ()
 
75
{
 
76
        if (opt_verbose)
 
77
                fprintf (stderr, "engine: Connection broken. Exiting.\n");
 
78
        // FIXME: do we want to free anything here?
 
79
        exit (1);
 
80
}
 
81
 
 
82
ResultType engine_eval (Pos *pos, /*Player player,*/ float *eval)
 
83
{
 
84
        ResultType result;
 
85
        result = pos->player == WHITE ? game_eval_white(pos, pos->player, eval) :
 
86
        game_eval_black (pos, pos->player, eval);
 
87
        return result;
 
88
}
 
89
 
 
90
void engine_set_to_play (char *line)
 
91
{
 
92
        if (!strncasecmp (line, "white", 5))
 
93
                cur_pos.player = WHITE;
 
94
        else if (!strncasecmp (line, "black", 5))
 
95
                cur_pos.player = BLACK;
 
96
}
 
97
 
 
98
void engine_take_move (char *line)
 
99
{
 
100
        byte *move = move_read (line);
 
101
        movstack_trunc ();
 
102
        movstack_push (cur_pos.board, move);
 
103
        if (game_stateful)
 
104
        {
 
105
                void *newstate = game_newstate (&cur_pos, move);
 
106
                statestack_push (newstate);
 
107
                cur_pos.state = statestack_peek ();
 
108
        }
 
109
        move_apply (cur_pos.board, move);
 
110
        cur_pos.num_moves++;
 
111
        cur_pos.player = cur_pos.player == WHITE ? BLACK : WHITE;
 
112
}
 
113
 
 
114
void engine_make_move ()
 
115
{
 
116
        byte *move;
 
117
        movstack_trunc ();
 
118
        cancel_move = FALSE;
 
119
        move = engine_search (&cur_pos);
 
120
        if (cancel_move)
 
121
                return;
 
122
        if (!move)
 
123
        {
 
124
                move_fwrite_nak ("Nice try", engine_fout);
 
125
                return;
 
126
        }
 
127
        movstack_push (cur_pos.board, move);
 
128
        if (game_stateful)
 
129
        {
 
130
                void *newstate = game_newstate (&cur_pos, move);
 
131
                statestack_push (newstate);
 
132
                cur_pos.state = statestack_peek ();
 
133
        }
 
134
        move_apply (cur_pos.board, move);
 
135
        cur_pos.num_moves++;
 
136
        move_fwrite_ack (move, engine_fout);
 
137
        cur_pos.player = cur_pos.player == WHITE ? BLACK : WHITE;
 
138
}
 
139
 
 
140
void engine_new_game (char *gamename)
 
141
{
 
142
        int i, len;
 
143
        Game *old_game = opt_game;
 
144
        opt_game = NULL;
 
145
        // strip trailing newline
 
146
        if (gamename[len = strlen(gamename) - 1] == '\n')
 
147
                gamename[len] = 0;
 
148
        for (i=0; i<num_games; i++)
 
149
                if (!strcmp (games[i]->name, gamename))
 
150
                {
 
151
                        opt_game = games[i];
 
152
                        break;
 
153
                }
 
154
        if (!opt_game)
 
155
        {
 
156
                // FIXME: isn't there a more elegant way to do this?
 
157
                GameLevel *level = game_levels;
 
158
                assert (level);
 
159
                while (level->name)
 
160
                {
 
161
                        if (!strcmp (level->game->name, gamename))
 
162
                        {
 
163
                                opt_game = level->game;
 
164
                                break;
 
165
                        }
 
166
                        level++;
 
167
                }
 
168
        }
 
169
        if (!opt_game)
 
170
        {
 
171
                fprintf (stderr, "engine: unknown game: %s\n", gamename);
 
172
                exit(1);
 
173
        }
 
174
        reset_game_params ();
 
175
        if (opt_game->game_init)
 
176
                opt_game->game_init(opt_game);
 
177
        set_game_params ();
 
178
        if (game_set_init_pos != game_set_init_pos_def) 
 
179
        {
 
180
                fwrite (cur_pos.board, board_wid * board_heit, 1, engine_fout);
 
181
                fflush (engine_fout);
 
182
        }
 
183
        stack_free ();
 
184
}
 
185
 
 
186
void engine_reset_game ()
 
187
{
 
188
        stack_free ();
 
189
        // FIXME : there should be a separate reset_game_params() for engine
 
190
        cur_pos.player = WHITE;
 
191
        cur_pos.state = NULL;
 
192
        cur_pos.num_moves = 0;
 
193
        game_set_init_pos (&cur_pos);
 
194
}
 
195
 
 
196
void engine_back_move ()
 
197
{
 
198
        byte *move = movstack_back ();
 
199
        if (game_stateful) cur_pos.state = statestack_back ();
 
200
        if (!move)
 
201
        {
 
202
                move_fwrite_nak (NULL, engine_fout);
 
203
                return;
 
204
        }
 
205
        move_apply (cur_pos.board, move);
 
206
        cur_pos.num_moves--;
 
207
        cur_pos.player = cur_pos.player == WHITE ? BLACK : WHITE;
 
208
        move_fwrite_ack (move, engine_fout);
 
209
}
 
210
 
 
211
void engine_forw_move ()
 
212
{
 
213
        byte *move = movstack_forw ();
 
214
        void *state;
 
215
        if (game_stateful && (state = statestack_forw ()))
 
216
                cur_pos.state = state;
 
217
        if (!move)
 
218
        {
 
219
                move_fwrite_nak (NULL, engine_fout);
 
220
                return;
 
221
        }
 
222
        move_apply (cur_pos.board, move);
 
223
        cur_pos.num_moves++;
 
224
        cur_pos.player = cur_pos.player == WHITE ? BLACK : WHITE;
 
225
        move_fwrite_ack (move, engine_fout);
 
226
}
 
227
 
 
228
void engine_msec_per_move (char *line)
 
229
{
 
230
        if (!line) return;
 
231
        time_per_move = atoi (line);
 
232
        if (time_per_move < 0)
 
233
                time_per_move = 3000;
 
234
}
 
235
 
 
236
void engine_who_won (char *line)
 
237
{
 
238
        int who;
 
239
        char *msg = NULL;
 
240
        char *who_str;
 
241
        if (!game_who_won)
 
242
        {
 
243
                move_fwrite_nak (NULL, engine_fout);
 
244
                return;
 
245
        }
 
246
        who = game_who_won (&cur_pos, cur_pos.player, &msg);
 
247
        switch(who)
 
248
        {
 
249
                case RESULT_WHITE: who_str = "WHITE"; break;
 
250
                case RESULT_BLACK: who_str = "BLACK"; break;
 
251
                case RESULT_TIE  : who_str = "TIE"  ; break;
 
252
                case RESULT_WON  : who_str = "WON" ; break;
 
253
                case RESULT_LOST : who_str = "LOST" ; break;
 
254
                case RESULT_MISC : who_str = "MISC" ; break;
 
255
                case RESULT_NOTYET:
 
256
                default:
 
257
                                                who_str = "NYET"; break; // ;^)
 
258
        }
 
259
        
 
260
        if (msg)
 
261
                fprintf (engine_fout, "ACK %s %s\n", who_str, msg);
 
262
        else
 
263
                fprintf (engine_fout, "ACK %s\n", who_str);
 
264
        fflush (engine_fout);
 
265
 
 
266
}
 
267
 
 
268
void engine_move_now (char *line)
 
269
{
 
270
        engine_stop_search = TRUE;
 
271
}
 
272
 
 
273
int engine_timeout_cb ()
 
274
{
 
275
        engine_stop_search = TRUE;
 
276
        return FALSE;
 
277
}
 
278
 
 
279
void engine_cancel_move (char *line)
 
280
{
 
281
        engine_stop_search = TRUE;
 
282
        cancel_move = TRUE;
 
283
}
 
284
 
 
285
 
 
286
//! This structure defines the protocol
 
287
Command commands[] = 
 
288
{
 
289
        { "MSEC_PER_MOVE"  , 1 , engine_msec_per_move},
 
290
        { "SUGGEST_MOVE"    , 0 , NULL},
 
291
        { "TAKE_MOVE"       , 1 , engine_take_move},
 
292
        { "BACK_MOVE"       , 1 , engine_back_move},
 
293
        { "FORW_MOVE"       , 1 , engine_forw_move},
 
294
        { "MAKE_MOVE"       , 1 , engine_make_move},
 
295
        { "MOVE_NOW"            , 1 , engine_move_now },
 
296
        { "CANCEL_MOVE"         , 1 , engine_cancel_move },
 
297
        { "END_GAME"        , 0 , NULL},
 
298
        { "RESET_GAME"      , 1 , engine_reset_game},
 
299
        { "TO_PLAY"         , 1 , engine_set_to_play},
 
300
        { "SET_POSITION"    , 0 , NULL},
 
301
        { "NEW_GAME"        , 1 , engine_new_game},
 
302
        { "GET_EVAL"        , 0 , NULL},
 
303
        { "SET_HEUR"        , 0 , NULL},
 
304
        { "SET_STRATEGY"    , 0 , NULL},
 
305
        { "WHO_WON"                     , 1 , engine_who_won},
 
306
};
 
307
 
 
308
#define NUM_COMMANDS (sizeof (commands) / sizeof (commands[0]))
 
309
 
 
310
//! Parse the command and pass control to the corresponding function pointer
 
311
static void execute_command (char *line)
 
312
{
 
313
        char *tail;
 
314
        int i;
 
315
        tail = strpbrk (line, " \t");
 
316
        if (tail)
 
317
                *tail = 0;
 
318
        for (i=0; i<NUM_COMMANDS; i++)
 
319
                if (!strcmp (line, commands[i].proto_str))
 
320
                {
 
321
                        if (!commands[i].isimpl)
 
322
                        {
 
323
                                fprintf (stderr, "warning: command %s not yet implemented\n",
 
324
                                                commands[i].proto_str);
 
325
                                return;
 
326
                        }
 
327
                        commands[i].impl_func (tail ? (tail+1) : 0);
 
328
                        return;
 
329
                }
 
330
        fprintf (stderr, "warning: unknown command \"%s\" \n", line);
 
331
}
 
332
 
 
333
 
 
334
static GSList *command_list = NULL;
 
335
static gboolean process_line ()
 
336
{
 
337
        char *line;
 
338
        line = (char *) g_slist_nth_data (command_list, 0);
 
339
        if (!line) return FALSE;
 
340
        command_list = g_slist_remove (command_list, line);
 
341
        execute_command (line);
 
342
        g_free (line);
 
343
        return TRUE;
 
344
}
 
345
 
 
346
static gboolean channel_process_input ()
 
347
{
 
348
        static char linebuf[4096];
 
349
        char *linep = linebuf;
 
350
        char *line;
 
351
        int bytes_read;
 
352
#if GLIB_MAJOR_VERSION > 1
 
353
        // we need to call this again because we will get new events before returning
 
354
        // from this function
 
355
        // semantics of add_watch silently changing between glib versions!!!!
 
356
        g_io_add_watch (channel_in, G_IO_IN, (GIOFunc) channel_process_input, NULL);
 
357
#endif
 
358
        g_io_channel_read (channel_in, linebuf, 4096, &bytes_read);
 
359
        linebuf[bytes_read] = '\0';
 
360
        while (*linep != '\0')
 
361
        {
 
362
                line = linep;
 
363
                while (*linep++ != '\n')
 
364
                        g_assert (linep[-1] != '\0');
 
365
                linep[-1] = '\0';
 
366
                if (opt_verbose) printf ("engine got command \"%s\"\n", line);
 
367
                command_list = g_slist_append (command_list, g_strdup (line));
 
368
        }       
 
369
        while (process_line ())
 
370
                ;
 
371
#if GLIB_MAJOR_VERSION == 1
 
372
        return TRUE;
 
373
#else
 
374
        return FALSE;
 
375
#endif
 
376
}
 
377
 
 
378
void engine_poll ()
 
379
{
 
380
        static int poll_count = 0;
 
381
        if (++poll_count == 100)
 
382
        {
 
383
                poll_count = 0;
 
384
                // listen for input in the pipe
 
385
                while (g_main_iteration (FALSE))
 
386
                        ;
 
387
                // execute pending commands
 
388
                // we execute only ONE command so that if there is a CANCEL_MOVE followed by a MAKE_MOVE we won't start on the new move before finishing this one
 
389
                process_line ();
 
390
        }
 
391
}
 
392
 
 
393
static void ignore () {}
 
394
 
 
395
//! This is the main function for the engine
 
396
void engine_main (int infd, int outfd)
 
397
{
 
398
        GMainLoop *loop;
 
399
        char *line;
 
400
        engine_flag = TRUE;
 
401
        signal (SIGHUP, ignore);
 
402
        signal (SIGINT, ignore);
 
403
        engine_fin = fdopen (infd, "r");
 
404
        engine_fout = fdopen (outfd, "w");
 
405
        assert (engine_fin);
 
406
        assert (engine_fout);
 
407
        channel_in = g_io_channel_unix_new (infd);
 
408
        g_io_add_watch (channel_in, G_IO_IN, (GIOFunc) channel_process_input, NULL);
 
409
        g_io_add_watch (channel_in, G_IO_HUP | G_IO_NVAL, (GIOFunc) engine_hup_cb, NULL);
 
410
#if GLIB_MAJOR_VERSION > 1
 
411
        loop = g_main_loop_new (NULL, TRUE);
 
412
#else
 
413
        loop = g_main_new (TRUE);
 
414
#endif
 
415
        g_main_run (loop);
 
416
}
 
417
 
 
418
byte * engine_search (Pos *pos/*, int player*/)
 
419
{
 
420
        int tag;
 
421
        byte *move;
 
422
        engine_stop_search = FALSE;
 
423
        if (game_search)
 
424
                game_search (pos, &move);
 
425
        else if (game_single_player)
 
426
                move = NULL;
 
427
        else
 
428
        {
 
429
                tag = g_timeout_add (2 * time_per_move, engine_timeout_cb, NULL);
 
430
                move = ab_dfid (pos, pos->player);
 
431
                g_source_remove (tag);
 
432
        }
 
433
        return move;
 
434
}
 
435