summaryrefslogtreecommitdiffstats
path: root/knights/proto_xboard.cpp
blob: 1c2fbd01959111c3aa3c89dd6b06bc7eecc1c6ef (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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
/***************************************************************************
                          proto_xboard.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 <tdelocale.h>
#include <tqtimer.h>
#include <tqstringlist.h>
#include <tqregexp.h>

#include "proto_xboard.moc"
#include "definitions.h"

proto_xboard::proto_xboard( const int ID ) : proto_base( ID )
{
	FEATURE_Analyze			= TRUE;
	FEATURE_Colors			= TRUE;
	FEATURE_Draw				= TRUE;
	FEATURE_ICS					= FALSE;
	FEATURE_Name				= TRUE;
	FEATURE_Pause				= FALSE;
	FEATURE_Ping				= FALSE;
	FEATURE_PlayOther		= FALSE;
	FEATURE_Reuse				= TRUE;
	FEATURE_SetBoard		= FALSE;
	FEATURE_SAN					= FALSE;
	FEATURE_SIGINT			= TRUE;
	FEATURE_SIGTERM			= TRUE;
	FEATURE_Time				= TRUE;
	FEATURE_UserMove		= FALSE;
	FEATURE_Variants		= 0;
	FEATURE_Level				= 1;
	FEATURE_BookEngine	= 0;

	CMDList = new CommandList;
	InitTimer = new TQTimer( this );
	connect( InitTimer, TQT_SIGNAL( timeout() ), this, TQT_SLOT( releaseBuffer() ) );

	Check								= TRUE;
	Team								= FALSE;
	JustMoved						= FALSE;
	AcceptIllegal				= FALSE;
	DelayedGo						= FALSE;
	Forced							= TRUE;
	Turn								= WHITE;
}
proto_xboard::~proto_xboard()
{
	delete InitTimer;
	delete CMDList;
}
///////////////////////////////////////
//
//	proto_xboard::parse( const Command &command )
//
///////////////////////////////////////
void proto_xboard::parse( const Command &command )
{
	TQString temp;
	Command cmd = command;

	if( InitTimer->isActive() )
	{
		/*
				Store Command for later use..
				we're still waiting for the engine to finish
		*/
		CMDList->append( command );
		return;
	}

	switch( cmd.getCommand() )
	{
		/* Command: Init */
		case CMD_Init:
			emit output( "xboard\nprotover 3" );
			InitTimer->start( 2000, TRUE );
			break;

		/* Command: New Game */
		case CMD_NewGame:
			emit output( "new\nrandom" );
			break;

		/* Command: Exit */
		case CMD_Exit:
			emit output( "quit" );
			if( FEATURE_SIGTERM )
				emit output( Command( myID, CMD_Send_SIGTERM ) );
			break;

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

		/* Command: Move */
		case CMD_Move:
			if( JustMoved )
			{
				JustMoved = FALSE;
				break;
			}
			AcceptIllegal = TRUE;
			myMove = cmd.getMove();
			/* Send Time */
			if( FEATURE_Time )
			{
				if( Army == WHITE )
				{
					emit output( TQString( "time %1" ).arg( cmd.getWhiteTime() ) );
					emit output( TQString( "otim %1" ).arg( cmd.getBlackTime() ) );
				}
				else
				{
					emit output( TQString( "time %1" ).arg( cmd.getBlackTime() ) );
					emit output( TQString( "otim %1" ).arg( cmd.getWhiteTime() ) );
				}
			}
			if( FEATURE_UserMove )
				temp = "usermove ";
			if( FEATURE_SAN )
				temp += myMove.SAN;
			else
				temp += myMove.CAN;
			if( DelayedGo )
			{
				DelayedGo = FALSE;
				temp += "\ngo";
				Forced = FALSE;
			}
			emit output( temp );
			break;

		/* Command: Pause */
		case CMD_Pause:
			if( FEATURE_Pause )
				emit output( "pause" );
			else
			{
				emit output( "force" );
				Forced = TRUE;
			}
			break;

		/* Command: Resume */
		case CMD_Resume:
			if( FEATURE_Pause )
				emit output( "resume" );
			else
			{
				if( Turn == Army )
				{
					emit output( "go" );
					Forced = FALSE;
				}
				else
					DelayedGo = TRUE;
			}
			break;

		/* Command: Play White */
		case CMD_Play_White:
			Army = WHITE;
			if( Turn == BLACK )
			{
				if( FEATURE_PlayOther )
				{
					emit output( "playother" );
				}
				else if( FEATURE_Colors )
				{
					emit output( "black" );
				}
				else
				{
					emit output( "force" );
					Forced = TRUE;
					DelayedGo = TRUE;
				}
				break;
			}
			else
			{
				temp = "force";
				if( FEATURE_Colors )
					temp += "\nwhite";
				temp += "\ngo";
				emit output( temp );
				break;
			}
			break;

		/* Command: Play Black */
		case CMD_Play_Black:
			Army = BLACK;
			if( Turn == WHITE )
			{
				if( FEATURE_PlayOther )
				{
					emit output( "playother" );
				}
				else if( FEATURE_Colors )
				{
					emit output( "white" );
				}
				else
				{
					emit output( "force" );
					Forced = TRUE;
					DelayedGo = TRUE;
				}
				break;
			}
			else
			{
				temp = "force";
				if( FEATURE_Colors )
					temp += "\nblack";
				temp += "\ngo";
				emit output( temp );
				break;
			}
			break;

		/* Command: Result White */
		case CMD_Result_White:
			emit output( "result 1-0 {White Mates}" );
			AcceptIllegal = FALSE;
//			if( Army == WHITE )
//				(*IT).Wins++;
//			else
//				(*IT).Losses++;
			break;

		/* Command: Result Black */
		case CMD_Result_Black:
			emit output( "result 0-1 {Black Mates}" );
			AcceptIllegal = FALSE;
//			if( Army == BLACK )
//				(*IT).Wins++;
//			else
//				(*IT).Losses++;
			break;

		/* Command: Result Draw */
		case CMD_Result_Draw:
			emit output( "result 1/2-1/2 {Draw Game}" );
			AcceptIllegal = FALSE;
//			(*IT).Draws++;
			break;

		/* Command: Your Time */
		case CMD_Your_Time:
			if( FEATURE_Time )
			{
				if( Army == WHITE )
					emit output( TQString( "time %1" ).arg( cmd.getWhiteTime() ) );
				else
					emit output( TQString( "time %1" ).arg( cmd.getBlackTime() ) );
			}
			break;

		/* Command: Enemy Time */
		case CMD_Enemy_Time:
			if( FEATURE_Time )
			{
				if( Army == WHITE )
					emit output( TQString( "otim %1" ).arg( cmd.getBlackTime() ) );
				else
					emit output( TQString( "otim %1" ).arg( cmd.getWhiteTime() ) );
			}
			break;

		/* Command: Offer Draw */
		case CMD_Offer_Draw:
			if( FEATURE_Draw )
				emit output( "draw" );
			break;

		/* Command: Book Mode */
		case CMD_Book_Mode:
			if( FEATURE_BookEngine != 0 )
				emit output( "bookengine" );
			break;

		/* Command: Check Book */
		case CMD_Check_Book:
			emit output( "go" );
			Forced = FALSE;
			break;

		/* Command: Ponder */
		case CMD_Ponder:
			emit output( "hard" );
			break;

		/* Command: No Pondering */
		case CMD_No_Pondering:
			emit output( "hard\neasy" );
			break;

		/* Command: Retract Move */
		case CMD_Retract_Move:
			emit output( "remove" );
			break;

		/* Command: Hint */
		case CMD_Hint:
			emit output( "hint" );
			break;

		/* Command: Listen */
		case CMD_Listen:
			emit output( "force" );
			Forced = TRUE;
			DelayedGo = FALSE;
			break;

		/* Command: Play */
		case CMD_Play:
			if( Army == Turn )
			{
				emit output( "go" );
				Forced = FALSE;
			}
			else
				DelayedGo = TRUE;
			break;

		/* Command: White Resign */
		case CMD_White_Resign:
			parse( Command( 0, CMD_Result_White ) );
			break;

		/* Command: Black Resign */
		case CMD_Black_Resign:
			parse( Command( 0, CMD_Result_Black ) );
			break;

		/* Command: Set Board */
		case CMD_Set_Board:
			if( FEATURE_SetBoard == TRUE )
			{
				emit output( TQString( "setboard %1" ).arg( cmd.getData() ) );
			}
			else
			{
				// Convert FEN to edit commands here and feed them to engine
			}
			break;

		/* Command: Set Difficulty */
		case CMD_Set_Difficulty:
			emit output( "sd " + cmd.getData() );
			// emit output( "st " + TQString::number( cmd.getData().toInt() >> 1 ) );
			break;

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

	if( ( FEATURE_SIGINT ) && ( !Forced ) )
		emit output( Command( myID, CMD_Send_SIGINT ) );

	/* Illegal */
	if( strList[0].contains( "illegal", FALSE ) )
	{
		if( AcceptIllegal )
		{
			if( strIn.contains("(no matching move)board") )
				return;
			if( strIn.contains("(no matching move)protover") )
				return;
			if( strIn.contains("(no matching move)sd") )
				return;
			AcceptIllegal = FALSE;
			emit output( Command( myID, CMD_Illegal ) );
		}
		return;
	}

	/* A Move */
	if( strList[0] == "move" )
	{
		if( strList.count() < 2 )
		{
			kdWarning() << "proto_xboard::parse: Incomplete Move command" << endl;
			return;
		}
		Command::clearMove( &myMove );
		strcpy( myMove.SAN, strList[1].latin1() );
		strcpy( myMove.CAN, strList[1].latin1() );
		if( Team )
		{
			emit output( "force" );
			Forced = TRUE;
		}
		JustMoved = TRUE;
		emit output( Command( myID, CMD_Move, 0, 0, myMove ) );
		return;
	}

	/* A Move ( Old Variation ) */
	if( ( strList[0].contains( TQRegExp("\\d+\\.") ) ) && ( strList[1] == "..." ) )
	{
		Command::clearMove( &myMove );
		strcpy( myMove.SAN, strList[2].latin1() );
		strcpy( myMove.CAN, strList[2].latin1() );
		if( Team )
		{
			emit output( "force" );
			Forced = TRUE;
		}
		JustMoved = TRUE;
		emit output( Command( myID, CMD_Move, 0, 0, myMove ) );
		return;
	}

	/* Hint */
	if( strList[0] == "Hint:" )
	{
		emit output( Command( myID, CMD_Hint, i18n( "%1 suggests this move:\n%2" ).arg( FEATURE_MyName ).arg( strList[1] ) ) );
		return;
	}

	/* Offer Draw */
	if( ( strList[0] == "offer" ) && ( strList[1] == "draw" ) )
	{
		emit output( Command( myID, CMD_Offer_Draw ) );
	}

	/* Out of Book */
	if( strList[0] == "outofbook" )
	{
		emit output( "force" );
		Forced = TRUE;
		emit output( Command( myID, CMD_Out_Of_Book ) );
		return;
	}

	/* Tell User */
	if( strList[0] == "telluser" )
	{
		emit output( Command( myID, CMD_Tell_User, i18n( "%1 tells you:\n%2" ).arg( FEATURE_MyName ).arg( strIn.right( strIn.length() - 9 ) ) ) );
		return;
	}

	/* Tell User Error */
	if( strList[0] == "tellusererror" )
	{
		emit output( Command( myID, CMD_Tell_User_Error, i18n( "%1 tells you:\n%2" ).arg( FEATURE_MyName ).arg( strIn.right( strIn.length() - 14 ) ) ) );
		return;
	}

	/* Error */
	if( strList[0] == "Error" )
	{
		emit output( Command( myID, CMD_Tell_User_Error, i18n( "%1 tells you:\n%2" ).arg( FEATURE_MyName ).arg( strIn.right( strIn.length() ) ) ) );
		return;
	}

	/* Tell Opponent */
	if( strList[0] == "tellopponent" )
	{
		emit output( Command( myID, CMD_Tell_Opponent, TQString( "%1" ).arg( strIn.right( strIn.length() - 13 ) ) ) );
		return;
	}

	/* Tell Others */
	if( strList[0] == "tellothers" )
	{
		emit output( Command( myID, CMD_Tell_Others, TQString( "%1" ).arg( strIn.right( strIn.length() - 11 ) ) ) );
		return;
	}

	/* Tell All */
	if( strList[0] == "tellall" )
	{
		emit output( Command( myID, CMD_Tell_All, TQString( "%1" ).arg( strIn.right( strIn.length() - 8 ) ) ) );
		return;
	}

	/* Tell ICS */
	if( strList[0] == "tellics" )
	{
		emit output( Command( myID, CMD_Tell_ICS, TQString( "%1" ).arg( strIn.right( strIn.length() - 8 ) ) ) );
		return;
	}

	/* Tell ICS No Alias */
	if( strList[0] == "tellicsnoalias" )
	{
		emit output( Command( myID, CMD_Tell_ICS, TQString( "%1" ).arg( strIn.right( strIn.length() - 15 ) ) ) );
		return;
	}

	/* Resign */
	if( strIn.contains( "resign", FALSE ) )
	{
		if( Army == WHITE )
			emit output( Command( myID, CMD_White_Resign ) );
		else
			emit output( Command( myID, CMD_Black_Resign ) );
		return;
	}

	/*
			Parse Features
	*/
	if( !InitTimer->isActive() )
		return;
	for( unsigned int loop = 0; loop < strList.count(); loop++ )
	{
		if( strList[loop] == "done=1" )
		{
			InitTimer->stop();
			releaseBuffer();
			continue;
		}
		/* This buys you 10 minutes */
		if( strList[loop] == "done=0" )
		{
			InitTimer->changeInterval( 600000 );
			continue;
		}
		if( strList[loop] == "feature" )
		{
			continue;
		}
		if( strList[loop].left(3) == "ics" )
		{
			FEATURE_ICS = TQString( strList[loop].right(1) ).toInt();
			emit output( "accepted ics" );
			continue;
		}
		if( strList[loop].left(3) == "san" )
		{
			FEATURE_SAN = TQString( strList[loop].right(1) ).toInt();
			emit output( "accepted san" );
			continue;
		}
		if( strList[loop].left(4) == "draw" )
		{
			FEATURE_Draw = TQString( strList[loop].right(1) ).toInt();
			emit output( "accepted draw" );
			continue;
		}
		if( strList[loop].left(4) == "name" )
		{
			FEATURE_Name = TQString( strList[loop].right(1) ).toInt();
			emit output( "accepted name" );
			continue;
		}
		if( strList[loop].left(4) == "ping" )
		{
			FEATURE_Ping = TQString( strList[loop].right(1) ).toInt();
			emit output( "accepted ping" );
			continue;
		}
		if( strList[loop].left(4) == "time" )
		{
			FEATURE_Time = TQString( strList[loop].right(1) ).toInt();
			emit output( "accepted time" );
			continue;
		}
		if( strList[loop].left(5) == "level" )
		{
			FEATURE_Level = TQString( strList[loop].right( strList[loop].length() - 6 ) ).toInt();
			emit output( "accepted level" );
			continue;
		}
		if( strList[loop].left(5) == "pause" )
		{
			FEATURE_Pause = TQString( strList[loop].right(1) ).toInt();
			emit output( "accepted pause" );
			continue;
		}
		if( strList[loop].left(5) == "reuse" )
		{
			FEATURE_Reuse = TQString( strList[loop].right(1) ).toInt();
			emit output( "accepted reuse" );
			continue;
		}
		if( strList[loop].left(6) == "colors" )
		{
			FEATURE_Colors = TQString( strList[loop].right(1) ).toInt();
			emit output( "accepted colors" );
			continue;
		}
		if( strList[loop].left(6) == "myname" )
		{
			FEATURE_MyName = TQString( strList[loop].right( strList[loop].length() - 8 ) );
			emit output( "accepted myname" );
			continue;
		}
		if( strList[loop].left(6) == "sigint" )
		{
			FEATURE_SIGINT = TQString( strList[loop].right(1) ).toInt();
			emit output( "accepted sigint" );
			continue;
		}
		if( strList[loop].left(7) == "analyze" )
		{
			FEATURE_Analyze = TQString( strList[loop].right(1) ).toInt();
			emit output( "accepted analyze" );
			continue;
		}
		if( strList[loop].left(7) == "sigterm" )
		{
			FEATURE_SIGTERM = TQString( strList[loop].right(1) ).toInt();
			emit output( "accepted sigterm" );
			continue;
		}
		if( strList[loop].left(8) == "setboard" )
		{
			FEATURE_SetBoard = TQString( strList[loop].right(1) ).toInt();
			emit output( "accepted setboard" );
			continue;
		}
		if( strList[loop].left(8) == "usermove" )
		{
			FEATURE_UserMove = TQString( strList[loop].right(1) ).toInt();
			emit output( "accepted usermove" );
			continue;
		}
		if( strList[loop].left(8) == "variants" )
		{
//			FIFO_Tmp = strList[loop].right( strList[loop].length() - 10 );
			// This must be finished
			emit output( "accepted variants" );
			continue;
		}
		if( strList[loop].left(9) == "playother" )
		{
			FEATURE_PlayOther = TQString( strList[loop].right(1) ).toInt();
			emit output( "accepted playother" );
			continue;
		}
		if( strList[loop].left(10) == "bookengine" )
		{
			FEATURE_BookEngine = TQString( strList[loop].right( strList[loop].length() - 11 ) ).toInt();
			emit output( "accepted bookengine" );
			continue;
		}
	}
}
///////////////////////////////////////
//
//	proto_xboard::releaseBuffer
//
///////////////////////////////////////
void proto_xboard::releaseBuffer( void )
{
	CommandList::Iterator it;
	for( it = CMDList->begin(); it != CMDList->end(); it++ )
	{
		parse( *it );
	}
	CMDList->clear();
}