RaaA: Fix write locations of plugins
[maemo-rb.git] / apps / plugins / chessbox / chessbox.c
blob48762312072215f2757e3f178f806b064aed46d4
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006 Miguel A. Arévalo
11 * Color graphics from eboard
12 * GNUChess v2 chess engine Copyright (c) 1988 John Stanback
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
22 ****************************************************************************/
24 #include "plugin.h"
26 #if (MEMORYSIZE > 8) /* Lowmem doesn't have playback in chessbox */
27 #define HAVE_PLAYBACK_CONTROL
28 #endif
31 #ifdef HAVE_PLAYBACK_CONTROL
32 #include "lib/playback_control.h"
33 #endif
35 #include "gnuchess.h"
36 #include "opening.h"
37 #include "chessbox_pgn.h"
39 /* type definitions */
40 struct cb_command {
41 int type;
42 char mv_s[5];
43 unsigned short mv;
46 /* External bitmaps */
47 extern const fb_data chessbox_pieces[];
52 /* Tile size defined by the assigned bitmap */
53 #include "pluginbitmaps/chessbox_pieces.h"
54 #define TILE_WIDTH BMPWIDTH_chessbox_pieces
55 #define TILE_HEIGHT (BMPHEIGHT_chessbox_pieces/26)
57 /* Calculate Offsets */
58 #define XOFS ((LCD_WIDTH-8*TILE_WIDTH)/2)
59 #define YOFS ((LCD_HEIGHT-8*TILE_HEIGHT)/2)
61 /* save files */
62 #define SAVE_FILE PLUGIN_GAMES_DATA_DIR "/chessbox.save"
64 /* commands enum */
65 #define COMMAND_NOP 0
66 #define COMMAND_MOVE 1
67 #define COMMAND_PLAY 2
68 #define COMMAND_LEVEL 3
69 #define COMMAND_RESTART 4
70 #define COMMAND_QUIT 5
71 #define COMMAND_MENU 6
72 #define COMMAND_SAVE 7
73 #define COMMAND_RESTORE 8
74 #define COMMAND_RESUME 9
75 #define COMMAND_SELECT 10
76 #define COMMAND_NEXT 11
77 #define COMMAND_PREV 12
79 short plugin_mode;
81 /* level+1's string */
82 const char *level_string[] = { "Level 1: 60 moves / 5 min" ,
83 "Level 2: 60 moves / 15 min" ,
84 "Level 3: 60 moves / 30 min" ,
85 "Level 4: 40 moves / 30 min" ,
86 "Level 5: 40 moves / 60 min" ,
87 "Level 6: 40 moves / 120 min" ,
88 "Level 7: 40 moves / 240 min" ,
89 "Level 8: 1 move / 15 min" ,
90 "Level 9: 1 move / 60 min" ,
91 "Level 10: 1 move / 600 min" };
93 /* "While thinking" command */
94 int wt_command = COMMAND_NOP;
96 /* ---- Get the board column and row (e2 f.e.) for a physical x y ---- */
97 void xy2cr ( short x, short y, short *c, short *r ) {
98 if (computer == black ) {
99 *c = x ;
100 *r = y ;
101 } else {
102 *c = 7 - x ;
103 *r = 7 - y ;
107 /* ---- get physical x y for a board column and row (e2 f.e.) ---- */
108 void cr2xy ( short c, short r, short *x, short *y ) {
109 if ( computer == black ) {
110 *x = c ;
111 *y = r ;
112 } else {
113 *x = 7 - c ;
114 *y = 7 - r ;
118 /* ---- Draw a complete board ---- */
119 static void cb_drawboard (void) {
120 short r , c , x , y ;
121 short l , piece , p_color ;
122 int b_color=1;
124 rb->lcd_clear_display();
126 for (r = 0; r < 8; r++) {
127 for (c = 0; c < 8; c++) {
128 l = locn[r][c];
129 piece = board[l] ;
130 p_color = color[l] ;
131 cr2xy ( c , r , &x , &y );
132 if ( piece == no_piece ) {
133 rb->lcd_bitmap_part ( chessbox_pieces , 0 ,
134 TILE_HEIGHT * b_color ,
135 STRIDE( SCREEN_MAIN,
136 BMPWIDTH_chessbox_pieces,
137 BMPHEIGHT_chessbox_pieces) ,
138 XOFS + x*TILE_WIDTH ,
139 YOFS + ( 7 - y )*TILE_HEIGHT ,
140 TILE_WIDTH ,
141 TILE_HEIGHT );
142 } else {
143 rb->lcd_bitmap_part ( chessbox_pieces ,
145 2 * TILE_HEIGHT +
146 4 * TILE_HEIGHT * ( piece - 1 ) +
147 2 * TILE_HEIGHT * p_color +
148 TILE_HEIGHT * b_color ,
149 STRIDE( SCREEN_MAIN,
150 BMPWIDTH_chessbox_pieces,
151 BMPHEIGHT_chessbox_pieces) ,
152 XOFS + x*TILE_WIDTH ,
153 YOFS + (7 - y)*TILE_HEIGHT ,
154 TILE_WIDTH ,
155 TILE_HEIGHT );
157 b_color = (b_color == 1) ? 0 : 1 ;
159 b_color = (b_color == 1) ? 0 : 1 ;
162 /* draw board limits */
163 #if (LCD_WIDTH > TILE_WIDTH*8) && (LCD_HEIGHT > TILE_HEIGHT*8)
164 rb->lcd_drawrect(XOFS - 1, YOFS - 1, TILE_WIDTH*8 + 2, TILE_HEIGHT*8 + 2);
165 #elif LCD_WIDTH > TILE_WIDTH*8
166 rb->lcd_vline(XOFS - 1, 0, LCD_HEIGHT - 1);
167 rb->lcd_vline(XOFS + 8*TILE_WIDTH, 0, LCD_HEIGHT - 1);
168 #elif LCD_HEIGHT > TILE_HEIGHT*8
169 rb->lcd_hline(0, LCD_WIDTH - 1, YOFS - 1);
170 rb->lcd_hline(0, LCD_WIDTH - 1, YOFS + TILE_HEIGHT*8);
171 #endif
173 rb->lcd_update();
176 /* ---- Switch mark on board ---- */
177 void cb_switch ( short x , short y ) {
178 rb->lcd_set_drawmode ( DRMODE_COMPLEMENT );
179 rb->lcd_drawrect ( XOFS + x*TILE_WIDTH + 1 ,
180 YOFS + ( 7 - y )*TILE_HEIGHT +1 ,
181 TILE_WIDTH-2 , TILE_HEIGHT-2 );
182 rb->lcd_update();
183 rb->lcd_set_drawmode ( DRMODE_SOLID );
186 /* ---- callback for capturing interaction while thinking ---- */
187 void cb_wt_callback ( void ) {
188 int button = BUTTON_NONE;
190 wt_command = COMMAND_NOP;
191 button = rb->button_get(false);
192 switch (button) {
193 #ifdef CB_RC_QUIT
194 case CB_RC_QUIT:
195 wt_command = COMMAND_QUIT;
196 timeout = true;
197 break;
198 #endif
199 case CB_MENU:
200 wt_command = COMMAND_MENU;
201 timeout = true;
202 break;
203 case CB_PLAY:
204 wt_command = COMMAND_PLAY;
205 timeout = true;
206 break;
210 /* ---- set playing parameters depending on level ---- */
211 void cb_setlevel ( int lev ) {
212 Level = (lev > 7) ? 7 : ( (lev < 1) ? 1 : lev ) ;
213 switch (Level) {
214 case 1 :
215 TCmoves = 60;
216 TCminutes = 5;
217 break;
218 case 2 :
219 TCmoves = 60;
220 TCminutes = 15;
221 break;
222 case 3 :
223 TCmoves = 60;
224 TCminutes = 30;
225 break;
226 case 4 :
227 TCmoves = 40;
228 TCminutes = 30;
229 break;
230 case 5 :
231 TCmoves = 40;
232 TCminutes = 60;
233 break;
234 case 6 :
235 TCmoves = 40;
236 TCminutes = 120;
237 break;
238 case 7 :
239 TCmoves = 40;
240 TCminutes = 240;
241 break;
242 case 8 :
243 TCmoves = 1;
244 TCminutes = 15;
245 break;
246 case 9 :
247 TCmoves = 1;
248 TCminutes = 60;
249 break;
250 case 10 :
251 TCmoves = 1;
252 TCminutes = 600;
253 break;
255 TCflag = (TCmoves > 1);
256 SetTimeControl();
259 /* ---- increase playing level ---- */
260 void cb_levelup ( void ) {
261 if ( Level == 7 )
262 cb_setlevel ( 1 );
263 else
264 cb_setlevel ( Level+1 );
265 rb->splash ( HZ/2 , level_string[Level-1] );
268 /* ---- Save current position ---- */
269 void cb_saveposition ( void ) {
270 int fd;
271 short sq,i,c;
272 unsigned short temp;
274 rb->splash ( 0 , "Saving position" );
276 fd = rb->open(SAVE_FILE, O_WRONLY|O_CREAT, 0666);
278 computer++; rb->write(fd, &(computer), sizeof(computer)); computer--;
279 opponent++; rb->write(fd, &(opponent), sizeof(opponent)); opponent--;
280 rb->write(fd, &(Game50), sizeof(Game50));
282 rb->write(fd, &(castld[white]), sizeof(castld[white]));
283 rb->write(fd, &(castld[black]), sizeof(castld[black]));
284 rb->write(fd, &(kingmoved[white]), sizeof(kingmoved[white]));
285 rb->write(fd, &(kingmoved[black]), sizeof(kingmoved[black]));
287 rb->write(fd, &(withbook), sizeof(withbook));
288 rb->write(fd, &(Level), sizeof(Level));
289 rb->write(fd, &(TCflag), sizeof(TCflag));
290 rb->write(fd, &(OperatorTime), sizeof(OperatorTime));
292 rb->write(fd, &(TimeControl.clock[white]),
293 sizeof(TimeControl.clock[white]) );
294 rb->write(fd, &(TimeControl.clock[black]),
295 sizeof(TimeControl.clock[black]) );
296 rb->write(fd, &(TimeControl.moves[white]),
297 sizeof(TimeControl.moves[white]) );
298 rb->write(fd, &(TimeControl.moves[black]),
299 sizeof(TimeControl.moves[black]) );
300 for (sq = 0; sq < 64; sq++) {
301 if (color[sq] == neutral) c = 0; else c = color[sq]+1;
302 temp = 256*board[sq] + c ;
303 rb->write(fd, &(temp), sizeof(temp));
305 for (i = 0; i <= GameCnt; i++) {
306 if (GameList[i].color == neutral)
307 c = 0;
308 else
309 c = GameList[i].color + 1;
310 rb->write(fd, &(GameList[i].gmove), sizeof(GameList[i].gmove));
311 rb->write(fd, &(GameList[i].score), sizeof(GameList[i].score));
312 rb->write(fd, &(GameList[i].depth), sizeof(GameList[i].depth));
313 rb->write(fd, &(GameList[i].nodes), sizeof(GameList[i].nodes));
314 rb->write(fd, &(GameList[i].time), sizeof(GameList[i].time));
315 rb->write(fd, &(GameList[i].piece), sizeof(GameList[i].piece));
316 rb->write(fd, &(c), sizeof(c));
318 rb->close(fd);
321 /* ---- Restore saved position ---- */
322 void cb_restoreposition ( void ) {
323 int fd;
324 int c;
325 short sq;
326 unsigned short m;
328 if ( (fd = rb->open(SAVE_FILE, O_RDONLY)) >= 0 ) {
329 rb->splash ( 0 , "Loading position" );
330 rb->read(fd, &(computer), sizeof(computer));
331 rb->read(fd, &(opponent), sizeof(opponent));
332 rb->read(fd, &(Game50), sizeof(Game50));
334 rb->read(fd, &(castld[white]), sizeof(castld[white]));
335 rb->read(fd, &(castld[black]), sizeof(castld[black]));
336 rb->read(fd, &(kingmoved[white]), sizeof(kingmoved[white]));
337 rb->read(fd, &(kingmoved[black]), sizeof(kingmoved[black]));
339 rb->read(fd, &(withbook), sizeof(withbook));
340 rb->read(fd, &(Level), sizeof(Level));
341 rb->read(fd, &(TCflag), sizeof(TCflag));
342 rb->read(fd, &(OperatorTime), sizeof(OperatorTime));
344 rb->read(fd, &(TimeControl.clock[white]),
345 sizeof(TimeControl.clock[white]));
346 rb->read(fd, &(TimeControl.clock[black]),
347 sizeof(TimeControl.clock[black]));
348 rb->read(fd, &(TimeControl.moves[white]),
349 sizeof(TimeControl.moves[white]));
350 rb->read(fd, &(TimeControl.moves[black]),
351 sizeof(TimeControl.moves[black]));
352 for (sq = 0; sq < 64; sq++) {
353 rb->read(fd, &(m), sizeof(m));
354 board[sq] = (m >> 8); color[sq] = (m & 0xFF);
355 if (color[sq] == 0)
356 color[sq] = neutral;
357 else
358 --color[sq];
360 GameCnt = -1; c = '?';
361 while (rb->read(fd, &(GameList[++GameCnt].gmove),
362 sizeof(GameList[GameCnt].gmove)) > 0) {
363 rb->read(fd, &(GameList[GameCnt].score),
364 sizeof(GameList[GameCnt].score));
365 rb->read(fd, &(GameList[GameCnt].depth),
366 sizeof(GameList[GameCnt].depth));
367 rb->read(fd, &(GameList[GameCnt].nodes),
368 sizeof(GameList[GameCnt].nodes));
369 rb->read(fd, &(GameList[GameCnt].time),
370 sizeof(GameList[GameCnt].time));
371 rb->read(fd, &(GameList[GameCnt].piece),
372 sizeof(GameList[GameCnt].piece));
373 rb->read(fd, &(GameList[GameCnt].color),
374 sizeof(GameList[GameCnt].color));
375 if (GameList[GameCnt].color == 0)
376 GameList[GameCnt].color = neutral;
377 else
378 --GameList[GameCnt].color;
380 GameCnt--;
381 if (TimeControl.clock[white] > 0)
382 TCflag = true;
383 computer--; opponent--;
385 rb->close(fd);
386 cb_setlevel(Level);
387 InitializeStats();
388 Sdepth = 0;
391 /* ---- show menu in viewer mode---- */
392 static int cb_menu_viewer(void)
394 int selection;
395 int result = 0;
396 bool menu_quit = false;
398 MENUITEM_STRINGLIST(menu,"Chessbox Menu",NULL,"Restart Game",
399 "Select Other Game", "Quit");
401 while(!menu_quit)
403 switch(rb->do_menu(&menu, &selection, NULL, false))
405 case 0:
406 menu_quit = true;
407 result = COMMAND_RESTART;
408 break;
409 case 1:
410 result = COMMAND_SELECT;
411 menu_quit = true;
412 break;
413 case 2:
414 result = COMMAND_QUIT;
415 menu_quit = true;
416 break;
419 return result;
422 /* ---- get a command in game mode ---- */
423 struct cb_command cb_get_viewer_command (void) {
424 int button;
425 struct cb_command result = { 0, {0,0,0,0,0}, 0 };
427 /* main loop */
428 while ( true ) {
429 button = rb->button_get(true);
430 switch (button) {
431 #ifdef CB_RC_QUIT
432 case CB_RC_QUIT:
433 result.type = COMMAND_QUIT;
434 return result;
435 #endif
436 case CB_MENU:
437 result.type = cb_menu_viewer();
438 return result;
439 case CB_LEFT:
440 result.type = COMMAND_PREV;
441 return result;
442 case CB_RIGHT:
443 result.type = COMMAND_NEXT;
444 return result;
450 /* ---- viewer main loop ---- */
451 void cb_start_viewer(char* filename){
452 struct pgn_game_node *first_game, *selected_game;
453 struct pgn_ply_node *curr_ply;
454 bool exit_game = false;
455 bool exit_viewer = false;
456 struct cb_command command;
458 first_game = pgn_list_games(filename);
459 if (first_game == NULL){
460 rb->splash ( HZ*2 , "No games found !" );
461 return;
464 do {
465 selected_game = pgn_show_game_list(first_game);
466 if (selected_game == NULL){
467 break;
470 pgn_parse_game(filename, selected_game);
471 if (selected_game->first_ply != NULL) {
473 /* init board */
474 GNUChess_Initialize();
476 /* draw the board */
477 cb_drawboard();
479 curr_ply = selected_game->first_ply;
480 exit_game = false;
482 do {
483 command = cb_get_viewer_command ();
484 switch (command.type) {
485 case COMMAND_PREV:
486 /* unapply the previous ply */
487 if (curr_ply->prev_node != NULL){
488 curr_ply = curr_ply->prev_node;
489 } else {
490 rb->splash ( HZ*2 , "At the begining of the game" );
491 break;
493 board[locn[curr_ply->row_from][curr_ply->column_from]]
494 = board[locn[curr_ply->row_to][curr_ply->column_to]];
495 color[locn[curr_ply->row_from][curr_ply->column_from]]
496 = color[locn[curr_ply->row_to][curr_ply->column_to]];
497 board[locn[curr_ply->row_to][curr_ply->column_to]] = no_piece;
498 color[locn[curr_ply->row_to][curr_ply->column_to]] = neutral;
499 if (curr_ply->taken_piece != no_piece && !curr_ply->enpassant){
500 board[locn[curr_ply->row_to][curr_ply->column_to]]
501 = curr_ply->taken_piece;
502 color[locn[curr_ply->row_to][curr_ply->column_to]]
503 = ((curr_ply->player==white)?black:white);
505 if (curr_ply->castle){
506 if (curr_ply->column_to == 6){
507 /* castling kingside */
508 board[locn[curr_ply->row_to][7]] = rook;
509 color[locn[curr_ply->row_to][7]] = curr_ply->player;
510 board[locn[curr_ply->row_to][5]] = no_piece;
511 color[locn[curr_ply->row_to][5]] = neutral;
512 } else {
513 /* castling queenside */
514 board[locn[curr_ply->row_to][0]] = rook;
515 color[locn[curr_ply->row_to][0]] = curr_ply->player;
516 board[locn[curr_ply->row_to][3]] = no_piece;
517 color[locn[curr_ply->row_to][3]] = neutral;
520 if (curr_ply->enpassant){
521 board[locn[curr_ply->row_from][curr_ply->column_to]] = pawn;
522 color[locn[curr_ply->row_from][curr_ply->column_to]]
523 = ((curr_ply->player==white)?black:white);
525 if (curr_ply->promotion){
526 board[locn[curr_ply->row_from][curr_ply->column_from]] = pawn;
527 color[locn[curr_ply->row_from][curr_ply->column_from]]
528 = curr_ply->player;
531 cb_drawboard();
532 break;
533 case COMMAND_NEXT:
534 /* apply the current move */
535 if (curr_ply->player == neutral){
536 rb->splash ( HZ*2 , "At the end of the game" );
537 break;
539 board[locn[curr_ply->row_to][curr_ply->column_to]]
540 = board[locn[curr_ply->row_from][curr_ply->column_from]];
541 color[locn[curr_ply->row_to][curr_ply->column_to]]
542 = color[locn[curr_ply->row_from][curr_ply->column_from]];
543 board[locn[curr_ply->row_from][curr_ply->column_from]] = no_piece;
544 color[locn[curr_ply->row_from][curr_ply->column_from]] = neutral;
545 if (curr_ply->castle){
546 if (curr_ply->column_to == 6){
547 /* castling kingside */
548 board[locn[curr_ply->row_to][5]] = rook;
549 color[locn[curr_ply->row_to][5]] = curr_ply->player;
550 board[locn[curr_ply->row_to][7]] = no_piece;
551 color[locn[curr_ply->row_to][7]] = neutral;
552 } else {
553 /* castling queenside */
554 board[locn[curr_ply->row_to][3]] = rook;
555 color[locn[curr_ply->row_to][3]] = curr_ply->player;
556 board[locn[curr_ply->row_to][0]] = no_piece;
557 color[locn[curr_ply->row_to][0]] = neutral;
560 if (curr_ply->enpassant){
561 board[locn[curr_ply->row_from][curr_ply->column_to]] = no_piece;
562 color[locn[curr_ply->row_from][curr_ply->column_to]] = neutral;
564 if (curr_ply->promotion){
565 board[locn[curr_ply->row_to][curr_ply->column_to]]
566 = curr_ply->promotion_piece;
567 color[locn[curr_ply->row_to][curr_ply->column_to]]
568 = curr_ply->player;
570 if (curr_ply->next_node != NULL){
571 curr_ply = curr_ply->next_node;
573 cb_drawboard();
574 break;
575 case COMMAND_RESTART:
576 GNUChess_Initialize();
577 cb_drawboard();
578 curr_ply = selected_game->first_ply;
579 case COMMAND_SELECT:
580 exit_game = true;
581 break;
582 case COMMAND_QUIT:
583 exit_viewer = true;
584 break;
586 } while (!exit_game && !exit_viewer);
587 } else {
588 rb->splash ( HZ*2 , "Error parsing game !");
590 } while (!exit_viewer);
593 /* ---- show menu ---- */
594 static int cb_menu(void)
596 int selection;
597 int result = 0;
598 bool menu_quit = false;
600 MENUITEM_STRINGLIST(menu,"Chessbox Menu",NULL,"New Game","Resume Game",
601 "Save Game", "Restore Game",
602 #ifdef HAVE_PLAYBACK_CONTROL
603 "Playback Control",
604 #endif
605 "Quit");
607 while(!menu_quit)
609 switch(rb->do_menu(&menu, &selection, NULL, false))
611 case 0:
612 menu_quit = true;
613 result = COMMAND_RESTART;
614 break;
615 case 1:
616 result = COMMAND_RESUME;
617 menu_quit = true;
618 break;
619 case 2:
620 result = COMMAND_SAVE;
621 menu_quit = true;
622 break;
623 case 3:
624 result = COMMAND_RESTORE;
625 menu_quit = true;
626 break;
627 case 4:
628 #ifdef HAVE_PLAYBACK_CONTROL
629 playback_control(NULL);
630 break;
631 case 5:
632 #endif
633 result = COMMAND_QUIT;
634 menu_quit = true;
635 break;
638 return result;
641 /* ---- get a command in game mode ---- */
642 struct cb_command cb_getcommand (void) {
643 static short x = 4 , y = 3 ;
644 short c , r , l;
645 int button, lastbutton = BUTTON_NONE;
646 int marked = false , from_marked = false ;
647 short marked_x = 0 , marked_y = 0 ;
648 struct cb_command result = { 0, {0,0,0,0,0}, 0 };
650 cb_switch ( x , y );
651 /* main loop */
652 while ( true ) {
653 button = rb->button_get(true);
654 switch (button) {
655 #ifdef CB_RC_QUIT
656 case CB_RC_QUIT:
657 result.type = COMMAND_QUIT;
658 return result;
659 #endif
660 case CB_MENU:
661 result.type = cb_menu();
662 return result;
663 case CB_LEVEL:
664 result.type = COMMAND_LEVEL;
665 return result;
666 case CB_PLAY:
667 #ifdef CB_PLAY_PRE
668 if (lastbutton != CB_PLAY_PRE)
669 break;
670 #endif
671 result.type = COMMAND_PLAY;
672 return result;
673 case CB_UP:
674 if ( !from_marked ) cb_switch ( x , y );
675 y++;
676 if ( y == 8 ) {
677 y = 0;
678 x--;
679 if ( x < 0 ) x = 7;
681 if ( marked && ( marked_x == x ) && ( marked_y == y ) ) {
682 from_marked = true ;
683 } else {
684 from_marked = false ;
685 cb_switch ( x , y );
687 break;
688 case CB_DOWN:
689 if ( !from_marked ) cb_switch ( x , y );
690 y--;
691 if ( y < 0 ) {
692 y = 7;
693 x++;
694 if ( x == 8 ) x = 0;
696 if ( marked && ( marked_x == x ) && ( marked_y == y ) ) {
697 from_marked = true ;
698 } else {
699 from_marked = false ;
700 cb_switch ( x , y );
702 break;
703 case CB_LEFT:
704 if ( !from_marked ) cb_switch ( x , y );
705 x--;
706 if ( x < 0 ) {
707 x = 7;
708 y++;
709 if ( y == 8 ) y = 0;
711 if ( marked && ( marked_x == x ) && ( marked_y == y ) ) {
712 from_marked = true ;
713 } else {
714 from_marked = false ;
715 cb_switch ( x , y );
717 break;
718 case CB_RIGHT:
719 if ( !from_marked ) cb_switch ( x , y );
720 x++;
721 if ( x == 8 ) {
722 x = 0;
723 y--;
724 if ( y < 0 ) y = 7;
726 if ( marked && ( marked_x == x ) && ( marked_y == y ) ) {
727 from_marked = true ;
728 } else {
729 from_marked = false ;
730 cb_switch ( x , y );
732 break;
733 case CB_SELECT:
734 #ifdef CB_SELECT_PRE
735 if (lastbutton != CB_SELECT_PRE)
736 break;
737 #endif
738 if ( !marked ) {
739 xy2cr ( x , y , &c , &r );
740 l = locn[r][c];
741 if ( ( color[l]!=computer ) && ( board[l]!=no_piece ) ) {
742 marked = true;
743 from_marked = true ;
744 marked_x = x;
745 marked_y = y;
747 } else {
748 if ( ( marked_x == x ) && ( marked_y == y ) ) {
749 marked = false;
750 from_marked = false;
751 } else {
752 xy2cr ( marked_x , marked_y , &c , &r );
753 result.mv_s[0] = 'a' + c;
754 result.mv_s[1] = '1' + r;
755 xy2cr ( x , y , &c , &r );
756 result.mv_s[2] = 'a' + c;
757 result.mv_s[3] = '1' + r;
758 result.mv_s[4] = '\00';
759 result.type = COMMAND_MOVE;
760 return result;
763 break;
765 if (button != BUTTON_NONE)
766 lastbutton = button;
771 /* ---- game main loop ---- */
772 void cb_play_game(void) {
773 struct cb_command command;
774 struct pgn_game_node *game;
775 char move_buffer[20];
777 /* init status */
778 bool exit = false;
780 /* load opening book, soon */
782 /* init board */
783 GNUChess_Initialize();
785 /* init PGN history data structures */
786 game = pgn_init_game();
788 /* restore saved position, if saved */
789 cb_restoreposition();
790 /* TODO: save/restore the PGN history of unfinished games */
792 /* draw the board */
793 /* I don't like configscreens, start game inmediatly */
794 cb_drawboard();
796 while (!exit) {
797 if ( mate ) {
798 rb->splash ( HZ*3 , "Checkmate!" );
799 rb->button_get(true);
800 pgn_store_game(game);
801 GNUChess_Initialize();
802 game = pgn_init_game();
803 cb_drawboard();
805 command = cb_getcommand ();
806 switch (command.type) {
807 case COMMAND_MOVE:
808 if ( ! VerifyMove (opponent, command.mv_s , 0 , &command.mv, move_buffer ) ) {
809 rb->splash ( HZ/2 , "Illegal move!" );
810 cb_drawboard();
811 } else {
812 cb_drawboard();
814 /* Add the ply to the PGN history (in algebraic notation) */
815 pgn_append_ply(game, opponent, move_buffer, mate);
817 rb->splash ( 0 , "Thinking..." );
818 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
819 rb->cpu_boost ( true );
820 #endif
821 SelectMove ( computer , 0 , cb_wt_callback, move_buffer);
822 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
823 rb->cpu_boost ( false );
824 #endif
825 /* Add the ply to the PGN history (in algebraic notation) and check
826 * for the result of the game which is only calculated in SelectMove
828 if (move_buffer[0] != '\0'){
829 pgn_append_ply(game, computer, move_buffer, mate);
830 } else {
831 pgn_set_result(game, mate);
834 if ( wt_command == COMMAND_QUIT ) {
835 exit = true;
836 break;
838 cb_drawboard();
840 break;
841 #ifdef COMMAND_RESTART
842 case COMMAND_RESTART:
843 GNUChess_Initialize();
844 game = pgn_init_game();
845 cb_drawboard();
846 break;
847 #endif
848 case COMMAND_RESUME:
849 cb_drawboard();
850 break;
851 case COMMAND_SAVE:
852 cb_saveposition();
853 cb_drawboard();
854 break;
855 case COMMAND_RESTORE:
856 /* watch out, it will reset the game if no previous game was saved! */
858 /* init board */
859 GNUChess_Initialize();
861 /* init PGN history data structures */
862 game = pgn_init_game();
864 /* restore saved position, if saved */
865 cb_restoreposition();
867 cb_drawboard();
868 break;
869 case COMMAND_PLAY:
870 if (opponent == white) {
871 opponent = black;
872 computer = white;
873 } else {
874 opponent = white;
875 computer = black;
877 rb->splash ( 0 , "Thinking..." );
878 ElapsedTime(1);
879 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
880 rb->cpu_boost ( true );
881 #endif
882 SelectMove ( computer , 0 , cb_wt_callback , move_buffer );
883 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
884 rb->cpu_boost ( false );
885 #endif
887 /* Add the ply to the PGN history (in algebraic notation) and check
888 * for the result of the game which is only calculated in SelectMove
890 if (move_buffer[0] != '\0'){
891 pgn_append_ply(game, computer, move_buffer, mate);
892 } else {
893 pgn_set_result(game, mate);
896 if ( wt_command == COMMAND_QUIT ) {
897 exit = true;
898 break;
900 cb_drawboard();
901 break;
902 case COMMAND_LEVEL:
903 cb_levelup ( );
904 cb_drawboard();
905 break;
906 case COMMAND_QUIT:
907 exit = true;
908 break;
912 cb_saveposition();
913 /* TODO: save/restore the PGN history of unfinished games */
914 rb->lcd_setfont(FONT_UI);
918 /*****************************************************************************
919 * plugin entry point.
920 ******************************************************************************/
921 enum plugin_status plugin_start(const void* parameter) {
923 /* plugin init */
925 #if LCD_DEPTH > 1
926 rb->lcd_set_backdrop(NULL);
927 #endif
929 /* end of plugin init */
931 /* if the plugin was invoked as a viewer, parse the file and show the game list
932 * else, start playing a game
934 if (parameter != NULL) {
935 cb_start_viewer((char *)parameter);
936 } else {
937 cb_play_game();
940 return PLUGIN_OK;