コード例 #1
0
ファイル: dreamchess.c プロジェクト: dreamchess/dreamchess
void game_retract_move(void) {
	/* Make sure a user is on move and we can undo two moves. */
	if (config->player[history->last->board->turn] != PLAYER_UI)
		return;
	if (!history->last->prev || !history->last->prev->prev)
		return;

	game_undo();
	game_undo();
	comm_send("remove\n");
}
コード例 #2
0
ファイル: get_move.c プロジェクト: konker/dreamchess
int get_move()
{
    int retval = -1;
    int mousex, mousey;
    static Sint16 rotx, roty;
    SDL_Event event;
    Uint8 *keystate = SDL_GetKeyState(NULL);
    Uint8 mousestate = SDL_GetMouseState(&mousex, &mousey);

    if( mousestate & SDL_BUTTON_MIDDLE )
    {
        move_camera( -(mousey-last_mousey)*MOUSE_CAM_SPEED, -(mousex-last_mousex)*MOUSE_CAM_SPEED);
        last_mousex=mousex;
        last_mousey=mousey;
    }
    else
    {
        last_mousex=mousex;
        last_mousey=mousey;
    }

    if (keystate[SDLK_LCTRL])
    {
        if (keystate[SDLK_DOWN])
            move_camera(-KEYBOARD_CAM_SPEED, 0.0f);
        if (keystate[SDLK_LEFT])
            move_camera(0.0f, -KEYBOARD_CAM_SPEED);
        if (keystate[SDLK_RIGHT])
            move_camera(0.0f, KEYBOARD_CAM_SPEED);
        if (keystate[SDLK_UP])
            move_camera(KEYBOARD_CAM_SPEED, 0.0f);

        while (SDL_PollEvent( &event ))
            if (event.type == SDL_QUIT)
                /* FIXME */
                exit(0);
    }

    if ((roty < -3000) || (roty > 3000))
        move_camera(-roty / (float) 32768 * KEYBOARD_CAM_SPEED, 0.0f);

    if ((rotx < -3000) || (rotx > 3000))
        move_camera(0.0f, -rotx / (float) 32768 * KEYBOARD_CAM_SPEED);

    while ( SDL_PollEvent( &event ) )
    {
        gg_event_t gg_event;

        if (event.type == SDL_QUIT)
            /* FIXME */
            exit(0);

        if (event.type == SDL_MOUSEMOTION)
            set_mouse_pos( event.motion.x, event.motion.y );

        if (!gg_dialog_get_active() && event.type == SDL_MOUSEBUTTONDOWN &&
                event.button.button == SDL_BUTTON_LEFT)
        {
            retval = get_mouse_square();
            if (retval != -1)
                select_piece(retval);

            continue;
        }

        if (!gg_dialog_get_active() && event.type == SDL_MOUSEBUTTONDOWN &&
                event.button.button == SDL_BUTTON_RIGHT)
        {
            gg_dialog_open(dialog_system_create());

            continue;
        }

        if ((event.type == SDL_JOYAXISMOTION) && (event.jaxis.axis == AXIS_VIEW_X))
        {
            rotx = event.jaxis.value;
            continue;
        }

        if ((event.type == SDL_JOYAXISMOTION) && (event.jaxis.axis == AXIS_VIEW_Y))
        {
            roty = event.jaxis.value;
            continue;
        }

        if ( (event.type == SDL_KEYDOWN && event.key.keysym.mod & KMOD_ALT &&
            event.key.keysym.sym == SDLK_RETURN) || (event.type == SDL_KEYDOWN &&
            event.key.keysym.sym == SDLK_F11) )
        {
            DBG_LOG( "toggled fullscreen" );
            toggle_fullscreen();
            continue;
        }

        gg_event = convert_event(&event);

        if (gg_event.type == GG_EVENT_NONE)
            continue;

        if (gg_dialog_get_active())
            gg_dialog_input_current(gg_event);

        /* In the promote dialog */
        else if (gg_event.type == GG_EVENT_KEY)
            switch (gg_event.key)
            {
            case GG_KEY_LEFT:
                move_selector(SELECTOR_LEFT);
                break;
            case GG_KEY_RIGHT:
                move_selector(SELECTOR_RIGHT);
                break;
            case GG_KEY_UP:
                move_selector(SELECTOR_UP);
                break;
            case GG_KEY_DOWN:
                move_selector(SELECTOR_DOWN);
                break;
            case GG_KEY_ACTION:
                retval = get_selector();
                select_piece(retval);
                break;
            case GG_KEY_ESCAPE:
                gg_dialog_open(dialog_system_create());
                break;
            case 'g':
            case GG_KEY_EXTRA3:
                /* gg_dialog_open(dialog_ingame_create()); */
                break;
            case 'p':
                game_view_prev();
                break;
            case 'n':
                game_view_next();
                break;
            case 'u':
                game_undo();
                break;
            case 's':
                fen_encode(get_board());
                /* game_save(); */
                break;
            case 'l':
                /* load_game(); */
                break;
            default:
                break;
            }
    }
    return retval;
}
コード例 #3
0
ファイル: dreamchess.c プロジェクト: dreamchess/dreamchess
int main(int argc, char **argv) {
	cl_options_t cl_options = {0};

	dbg_init();
	DBG_LOG("Version %s", g_version);

	ui = &ui_sdlgl;

	printf("DreamChess %s\n", g_version);

	parse_options(argc, argv, &ui, &cl_options);
	config_init();
	set_cl_options(&cl_options);

	if (!ui) {
		DBG_ERROR("Failed to find a user interface driver");
		exit(1);
	}

	ui->init();

	init_resolution();

	while (1) {
		board_t board;
		int pgn_slot;
		option_t *option;

		if (!(config = ui->config(&pgn_slot)))
			break;

		ch_userdir();
		option = config_get_option("first_engine");

#ifdef __APPLE__
		char temp1[200];
		char temp2[200];

		if (!strcmp(option->string, "dreamer") || !strcmp(option->string, "Dreamer")) {
			CFBundleRef mainBundle = CFBundleGetMainBundle();

			CFURLRef bundledir = CFBundleCopyBundleURL(mainBundle);
			CFStringRef stringref = CFURLCopyFileSystemPath(bundledir, kCFURLPOSIXPathStyle);
			CFStringGetCString(stringref, temp1, 200, kCFStringEncodingMacRoman);

			snprintf(temp2, sizeof(temp2), "%s/contents/MacOS/dreamer", temp1);

			game_set_engine_error(comm_init(temp2));
		} else
			game_set_engine_error(comm_init(option->string));
#else
		game_set_engine_error(comm_init(option->string));
#endif

		comm_send("xboard\n");

		comm_send("new\n");
		comm_send("random\n");

		comm_send("sd %i\n", config->cpu_level);
		comm_send("depth %i\n", config->cpu_level);

		if (config->difficulty == 0)
			comm_send("noquiesce\n");

		if (config->player[WHITE] == PLAYER_UI && config->player[BLACK] == PLAYER_UI)
			comm_send("force\n");

		if (config->player[WHITE] == PLAYER_ENGINE)
			comm_send("go\n");

		in_game = 1;
		board_setup(&board);
		history = history_init(&board);
		move_list_init(&san_list);
		move_list_init(&fan_list);
		move_list_init(&fullalg_list);

		if (pgn_slot >= 0)
			if (game_load(pgn_slot)) {
				DBG_ERROR("Failed to load savegame in slot %i", pgn_slot);
				exit(1);
			}

		ui->update(history->view->board, NULL);
		while (in_game) {
			char *s;

			if ((s = comm_poll())) {
				DBG_LOG("Message from engine: '%s'", s);
				if (!history->result) {
					if ((!strncmp(s, "move ", 4) || strstr(s, "... ")) &&
						config->player[history->last->board->turn] == PLAYER_ENGINE) {
						char *move_str = strrchr(s, ' ') + 1;
						board_t new_board = *history->last->board;
						move_t *engine_move;

						DBG_LOG("Parsing move string '%s'", move_str);

						engine_move = san_to_move(&new_board, move_str);
						if (!engine_move)
							engine_move = fullalg_to_move(&new_board, move_str);
						if (engine_move) {
							audio_play_sound(AUDIO_MOVE);
							do_move(engine_move, 1);
							free(engine_move);
						} else
							DBG_ERROR("Failed to parse move string '%s'", move_str);
					} else if (strstr(s, "llegal move"))
						game_undo();
					/* Ignore result message if we've already determined a result ourselves. */
					else {
						char *start = strchr(s, '{');
						char *end = strchr(s, '}');

						if (start && end && end > start) {
							char *comment = malloc(end - start);
							history->result = malloc(sizeof(result_t));
							strncpy(comment, start + 1, end - start - 1);
							comment[end - start - 1] = '\0';
							history->result->reason = comment;
							if (strstr(s, "1-0")) {
								history->result->code = RESULT_WHITE_WINS;
								ui->show_result(history->result);
							} else if (strstr(s, "1/2-1/2")) {
								history->result->code = RESULT_DRAW;
								ui->show_result(history->result);
							} else if (strstr(s, "0-1")) {
								history->result->code = RESULT_BLACK_WINS;
								ui->show_result(history->result);
							} else {
								free(history->result->reason);
								free(history->result);
								history->result = NULL;
							}
						}
					}
				}

				free(s);
			}
			ui->poll();
		}
		comm_send("quit\n");
		comm_exit();
		history_exit(history);
		move_list_exit(&san_list);
		move_list_exit(&fan_list);
		move_list_exit(&fullalg_list);
	}
	ui->exit();
	dbg_exit();
	return 0;
}