summaryrefslogtreecommitdiffstats
path: root/knights/proto_uci.cpp
blob: 8aa39a113f9565da2b8956d220dcaa5bb8782874 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/***************************************************************************
                          proto_uci.cpp  -  description
                             -------------------
    begin                : Sat Oct 26 2002
    copyright            : (C) 2003 by Troy Corbin Jr.
    email                : tcorbin@users.sf.net
 ***************************************************************************/

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

#include <tqstringlist.h>
#include <tqregexp.h>

#include "proto_uci.moc"
#include "definitions.h"

proto_uci::proto_uci( const int ID ) : proto_base( ID )
{
	hintMode = FALSE;
	initMode = FALSE;
	JustMoved = FALSE;
	myArmy = BLACK;
	Turn = WHITE;
	CMDList = new CommandList;
}
proto_uci::~proto_uci()
{
	delete CMDList;
}
///////////////////////////////////////
//
//	proto_uci::parse( const Command &command )
//
///////////////////////////////////////
void proto_uci::parse( const Command &command )
{
	Command cmd = command;

	if( initMode )
	{
		CMDList->append( command );
		return;
	}

	switch( cmd.getCommand() )
	{
		/* Command: Init */
		case CMD_Init:
			emit output( "uci" );
			initMode = TRUE;
			break;

		/* Command: New Game */
		case CMD_NewGame:
			emit output( "isready" );
			break;

		/* Command: Exit */
		case CMD_Exit:
			emit output( "quit" );
			break;

		/* Command: Move */
		case CMD_Move:
			if( JustMoved )
			{
				JustMoved = FALSE;
				break;
			}
			if( FEN.isEmpty() )
			{
				emit output( TQString("position startpos moves %1").arg( cmd.getData() ) );
			}
			else
			{
				emit output( TQString("position fen %1 moves %2").arg( FEN ).arg( cmd.getData() ) );
			}
			emit output( TQString("go wtime %1 btime %2 depth %3").arg( cmd.getWhiteTime() ).arg( cmd.getBlackTime() ).arg( difficulty ) );
			break;

		/* Command: Set Difficulty */
		case CMD_Set_Difficulty:
			difficulty = cmd.getData();
			break;

		/* Command: Set Board */
		case CMD_Set_Board:
			FEN = cmd.getData();
			break;

		/* Command: Move Now */
		case CMD_MoveNow:
			emit output( "stop" );
			break;

		/* Command: UCI Hint */
		case CMD_UCI_Hint:
			hintMode = TRUE;
			if( FEN.isEmpty() )
			{
				emit output( TQString("position startpos moves %1").arg( cmd.getData() ) );
			}
			else
			{
				emit output( TQString("position fen %1 moves %2").arg( FEN ).arg( cmd.getData() ) );
			}
			emit output( TQString("go wtime %1 btime %2 depth %3").arg( cmd.getWhiteTime() ).arg( cmd.getBlackTime() ).arg( difficulty ) );
			break;

		/* Command: Play White */
		case CMD_Play_White:
			myArmy = WHITE;
			break;

		/* Command: Play Black */
		case CMD_Play_Black:
			myArmy = BLACK;
			break;

		/* Command: Set Name */
		case CMD_Set_Name:
			engineName = cmd.getData();
			break;
	}
}
///////////////////////////////////////
//
//	proto_uci::parse( const TQString &string )
//
///////////////////////////////////////
void proto_uci::parse( const TQString &string )
{
	TQString strIn = string;
	TQStringList strList( TQStringList::split( ' ', strIn ) );

	/* Bestmove */
	if( strList[0] == "bestmove" )
	{
		if( strList.count() < 2 )
		{
			kdWarning() << "proto_uci::parse: Incomplete Bestmove command" << endl;
			return;
		}
		Command::clearMove( &myMove );
		strcpy( myMove.CAN, strList[1].latin1() );
		if( hintMode )
		{
			/* The Engine Gives a Hint */
			hintMode = FALSE;
//			emit output( Command( myID, CMD_Hint, i18n( "%1 suggests this move:\n%2" ).arg( engineName ).arg( strList[1] ) ) );
			emit output( Command( myID, CMD_Hint, TQString( "%1 suggests this move:\n%2" ).arg( engineName ).arg( strList[1] ) ) );
		}
		else
		{
			/* The Engine Moves */
			JustMoved = TRUE;
			emit output( Command( myID, CMD_Move, 0, 0, myMove ) );
		}
	}

	/* UCIOK */
	if( strList[0] == "uciok" )
	{
		initMode = FALSE;
		releaseBuffer();
	}

	/* READYOK */
	if( strList[0] == "readyok" )
	{
		initMode = FALSE;
		releaseBuffer();
	}

	/* ID */
	if( strList[0] == "id" )
	{
		if( strList[1] == "name" )
		{
			engineName = strIn.right( strIn.length() - 8 );
		}
		if( strList[1] == "author" )
		{
			engineAuthor = strIn.right( strIn.length() - 10 );
		}
	}

	/* COPYPROTECTION */
	if( strList[0] == "copyprotection" )
	{
		if( strList[1] == "error" )
		{
//			emit output( Command( myID, CMD_Tell_User_Error, i18n( "%1 did not pass it's copy protection check." ).arg( engineName ) ) );
			emit output( Command( myID, CMD_Tell_User_Error, TQString( "%1 did not pass it's copy protection check." ).arg( engineName ) ) );
		}
	}
}
///////////////////////////////////////
//
//	proto_uci::releaseBuffer
//
///////////////////////////////////////
void proto_uci::releaseBuffer( void )
{
	CommandList::Iterator it;
	for( it = CMDList->begin(); it != CMDList->end(); it++ )
	{
		parse( *it );
	}
	CMDList->clear();
}