2 Copyright (c) 1993 Richard V. Nash.
3 Copyright (c) 2000 Dan Papasian
4 Copyright (C) Andrew Tridgell 2002
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 /*Let users know that someone is available (format the message)*/
24 static void getavailmess(int p, char* message)
26 struct player *pp = &player_globals.parray[p];
30 AddPlayerLists(p,titles);
31 sprintf (message,"%s%s Blitz (%s), Std (%s), Wild (%s), Light(%s), Bug(%s)\n"
32 " is now available for matches.",
34 ratstrii(pp->b_stats.rating, p),
35 ratstrii(pp->s_stats.rating, p),
36 ratstrii(pp->w_stats.rating, p),
37 ratstrii(pp->l_stats.rating, p),
38 ratstrii(pp->bug_stats.rating, p));
41 void getnotavailmess(int p, char* message)
43 struct player *pp = &player_globals.parray[p];
47 AddPlayerLists(p,titles);
48 sprintf (message,"%s%s is no longer available for matches.",
52 void announce_avail(int p)
54 struct player *pp = &player_globals.parray[p];
57 if ((pp->game < 0) && (CheckPFlag(p, PFLAG_OPEN))) {
58 getavailmess (p,avail);
60 for (p1 = 0; p1 < player_globals.p_num; p1++) {
63 if (player_globals.parray[p1].status != PLAYER_PROMPT)
65 if (CheckPFlag(p1, PFLAG_AVAIL) && CheckPFlag(p1, PFLAG_OPEN)
66 && (player_globals.parray[p1].game < 0))
67 if (((pp->b_stats.rating <= player_globals.parray[p1].availmax) && (pp->b_stats.rating >= player_globals.parray[p1].availmin)) || (!player_globals.parray[p1].availmax))
68 pprintf_prompt (p1,"\n%s\n",avail);
73 void announce_notavail(int p)
75 struct player *pp = &player_globals.parray[p];
79 getnotavailmess (p,avail);
81 for (p1 = 0; p1 < player_globals.p_num; p1++) {
84 if (player_globals.parray[p1].status != PLAYER_PROMPT)
86 if (CheckPFlag(p1, PFLAG_AVAIL) && CheckPFlag(p1, PFLAG_OPEN)
87 && (player_globals.parray[p1].game < 0))
88 if (((pp->b_stats.rating <= player_globals.parray[p1].availmax) && (pp->b_stats.rating >= player_globals.parray[p1].availmin)) || (!player_globals.parray[p1].availmax))
89 pprintf_prompt (p1,"\n%s\n",avail);
93 void game_ended(int g, int winner, int why)
95 struct game *gg = &game_globals.garray[g];
97 char avail_black[200]; /* for announcing white/black avail */
98 char avail_white[200];
99 char avail_bugwhite[200];
100 char avail_bugblack[200];
109 char *NameOfWinner, *NameOfLoser;
110 int beingplayed = 0; /* i.e. it wasn't loaded for adjudication */
113 avail_white[0] = '\0';
114 avail_black[0] = '\0';
115 avail_bugwhite[0] = '\0';
116 avail_bugblack[0] = '\0';
118 beingplayed = (player_globals.parray[gg->black].game == g);
120 sprintf(outstr, "\n{Game %d (%s vs. %s) ", g + 1,
121 player_globals.parray[gg->white].name,
122 player_globals.parray[gg->black].name);
125 if (winner == WHITE) {
126 whiteResult = RESULT_WIN;
127 strcpy(winSymbol, "1-0");
128 NameOfWinner = player_globals.parray[gg->white].name;
129 NameOfLoser = player_globals.parray[gg->black].name;
131 whiteResult = RESULT_LOSS;
132 strcpy(winSymbol, "0-1");
133 NameOfWinner = player_globals.parray[gg->black].name;
134 NameOfLoser = player_globals.parray[gg->white].name;
138 sprintf(tmp, "%s checkmated} %s", NameOfLoser, winSymbol);
139 strcpy(EndSymbol, "Mat");
143 sprintf(tmp, "%s bared} %s", NameOfLoser, winSymbol);
144 strcpy(EndSymbol, "Bar");
148 sprintf(tmp, "%s perpetually checking} %s", NameOfLoser, winSymbol);
149 strcpy(EndSymbol, "Per");
153 sprintf(tmp, "%s resigns} %s", NameOfLoser, winSymbol);
154 strcpy(EndSymbol, "Res");
158 sprintf(tmp, "%s forfeits on time} %s", NameOfLoser, winSymbol);
159 strcpy(EndSymbol, "Fla");
163 sprintf(tmp, "Game drawn by stalemate} 1/2-1/2");
165 strcpy(EndSymbol, "Sta");
167 whiteResult = RESULT_DRAW;
170 sprintf(tmp, "Game drawn by mutual agreement} 1/2-1/2");
172 strcpy(EndSymbol, "Agr");
174 whiteResult = RESULT_DRAW;
177 sprintf(tmp, "Game drawn because both players ran out of time} 1/2-1/2");
179 strcpy(EndSymbol, "Fla");
181 whiteResult = RESULT_DRAW;
184 sprintf(tmp, "Game drawn by repetition} 1/2-1/2");
186 strcpy(EndSymbol, "Rep");
188 whiteResult = RESULT_DRAW;
191 sprintf(tmp, "Game drawn by the 50 move rule} 1/2-1/2");
193 strcpy(EndSymbol, "50");
195 whiteResult = RESULT_DRAW;
199 sprintf(tmp, "Bughouse game aborted.} *");
200 whiteResult = RESULT_ABORT;
202 sprintf(tmp, "Game adjourned by mutual agreement} *");
206 case END_LOSTCONNECTION:
207 sprintf(tmp, "%s lost connection; game ", NameOfWinner);
208 if (CheckPFlag(gg->white, PFLAG_REG)
209 && CheckPFlag(gg->black, PFLAG_REG)
211 sprintf(tmp, "adjourned} *");
214 sprintf(tmp, "aborted} *");
215 whiteResult = RESULT_ABORT;
218 sprintf(tmp, "Game aborted by mutual agreement} *");
219 whiteResult = RESULT_ABORT;
222 sprintf(tmp, "Game courtesyaborted by %s} *", NameOfWinner);
223 whiteResult = RESULT_ABORT;
225 case END_COURTESYADJOURN:
227 sprintf(tmp, "Bughouse game courtesyaborted by %s.} *", NameOfWinner);
228 whiteResult = RESULT_ABORT;
230 sprintf(tmp, "Game courtesyadjourned by %s} *", NameOfWinner);
235 /* Draw by insufficient material (e.g., lone K vs. lone K) */
236 sprintf(tmp, "Neither player has mating material} 1/2-1/2");
238 strcpy(EndSymbol, "NM ");
240 whiteResult = RESULT_DRAW;
242 case END_FLAGNOMATERIAL:
243 sprintf(tmp, "%s ran out of time and %s has no material to mate} 1/2-1/2",
244 NameOfLoser, NameOfWinner);
246 strcpy(EndSymbol, "TM ");
248 whiteResult = RESULT_DRAW;
251 sprintf(tmp, "%s wins by adjudication} %s", NameOfWinner, winSymbol);
252 strcpy(EndSymbol, "Adj");
256 sprintf(tmp, "Game drawn by adjudication} 1/2-1/2");
258 strcpy(EndSymbol, "Adj");
260 whiteResult = RESULT_DRAW;
263 sprintf(tmp, "Game aborted by adjudication} *");
264 whiteResult = RESULT_ABORT;
267 sprintf(tmp, "Hmm, the game ended and I don't know why} *");
272 if (CheckPFlag(gg->white, PFLAG_TOURNEY) &&
273 CheckPFlag(gg->black, PFLAG_TOURNEY)) {
274 /* mamer wants more info */
275 sprintf(tmp," [%d %d %d %d %d]",
276 gg->wInitTime/(60*10), gg->wIncrement/10, gg->rated, gg->private, (int)gg->type);
280 strcat(outstr, "\n");
282 if (gg->rated && rate_change && gg->type != TYPE_BUGHOUSE)
283 /* Adjust ratings; bughouse gets done later. */
284 rating_update(g, -1);
288 int avail_printed = 0;
290 pprintf_noformat(gg->white, outstr);
291 pprintf_noformat(gg->black, outstr);
295 gg->link = -1; /*IanO: avoids recursion */
296 if (gl >= 0 && game_globals.garray[gl].link >= 0) {
297 pprintf_noformat(game_globals.garray[gl].white, outstr);
298 pprintf_noformat(game_globals.garray[gl].black, outstr);
299 if (CheckPFlag(game_globals.garray[gl].white, PFLAG_OPEN)) {
300 getavailmess (game_globals.garray[gl].white, avail_bugwhite);
303 if (CheckPFlag(game_globals.garray[gl].black, PFLAG_OPEN)) {
304 getavailmess (game_globals.garray[gl].black, avail_bugblack);
307 if ((gg->rated) && (rate_change)) {
309 rating_update(g, gl);
311 game_ended(gl, CToggle(winner), why);
314 if ((player_num_active_boards(gg->white) <= 1) /* not a simul or */
315 && CheckPFlag(gg->white, PFLAG_OPEN)) { /* simul is over? */
316 getavailmess (gg->white,avail_white);
318 } else { /* Part of an ongoing simul! Let's shrink the array. */
322 if (CheckPFlag(gg->black, PFLAG_OPEN)) {
323 getavailmess (gg->black,avail_black);
327 for (p = 0; p < player_globals.p_num; p++) {
328 struct player *pp = &player_globals.parray[p];
329 if ((p == gg->white) || (p == gg->black))
331 if (pp->status != PLAYER_PROMPT)
334 if (CheckPFlag(p, PFLAG_GIN) || player_is_observe(p, g)) {
335 pprintf_noformat(p, outstr);
339 if (CheckPFlag(p, PFLAG_AVAIL) && (CheckPFlag(p, PFLAG_OPEN)) && (pp->game < 0) && (print_avail)) {
340 if (((player_globals.parray[gg->white].b_stats.rating <= pp->availmax) && (player_globals.parray[gg->white].b_stats.rating >= pp->availmin)) || (!pp->availmax)) {
341 pprintf (p,"\n%s",avail_white);
344 if (((player_globals.parray[gg->black].b_stats.rating <= pp->availmax) && (player_globals.parray[gg->black].b_stats.rating >= pp->availmin)) || (!pp->availmax)) {
345 pprintf (p,"\n%s",avail_black);
348 if (gl == -1) /* bughouse ? */ {
349 if (((player_globals.parray[game_globals.garray[gl].white].b_stats.rating <= pp->availmax) && (player_globals.parray[game_globals.garray[gl].white].b_stats.rating >= pp->availmin)) || (!pp->availmax)) {
350 pprintf (p,"\n%s",avail_bugwhite);
353 if (((player_globals.parray[game_globals.garray[gl].black].b_stats.rating <= pp->availmax) && (player_globals.parray[game_globals.garray[gl].black].b_stats.rating >= pp->availmin)) || (!pp->availmax)) {
354 pprintf (p,"\n%s",avail_bugblack);
371 if (!(gg->rated && rate_change)) {
372 pprintf(gg->white, "No ratings adjustment done.\n");
373 pprintf(gg->black, "No ratings adjustment done.\n");
377 if (rate_change && gl < 0)
378 game_write_complete(g, isDraw, EndSymbol);
379 /* Mail off the moves */
380 if (CheckPFlag(gg->white, PFLAG_AUTOMAIL)) {
381 pcommand(gg->white, "mailmoves");
383 if (CheckPFlag(gg->black, PFLAG_AUTOMAIL)) {
384 pcommand(gg->black, "mailmoves");
386 if (!((player_globals.parray[gg->white].simul_info != NULL) &&
387 (player_globals.parray[gg->white].simul_info->numBoards))) {
388 player_globals.parray[gg->white].num_white++;
389 PFlagOFF(gg->white, PFLAG_LASTBLACK);
390 player_globals.parray[gg->black].num_black++;
391 PFlagON(gg->black, PFLAG_LASTBLACK);
393 player_globals.parray[gg->white].last_opponent =
394 strdup(gg->black_name);
395 player_globals.parray[gg->black].last_opponent =
396 strdup(gg->white_name);
398 player_globals.parray[gg->white].game = -1;
399 player_globals.parray[gg->black].game = -1;
400 player_globals.parray[gg->white].opponent = -1;
401 player_globals.parray[gg->black].opponent = -1;
402 if (gg->white != command_globals.commanding_player)
403 send_prompt(gg->white);
404 if (gg->black != command_globals.commanding_player)
405 send_prompt(gg->black);
406 if ((player_globals.parray[gg->white].simul_info != NULL) &&
407 (player_globals.parray[gg->white].simul_info->numBoards))
408 player_simul_over(gg->white, g, whiteResult);
413 static int was_promoted(struct game *g, int f, int r)
415 #define BUGHOUSE_PAWN_REVERT 1
416 #ifdef BUGHOUSE_PAWN_REVERT
419 for (i = g->numHalfMoves-2; i > 0; i -= 2) {
420 if (g->moveList[i].toFile == f && g->moveList[i].toRank == r) {
421 if (g->moveList[i].piecePromotionTo) {
422 switch(g->moveList[i].moveString[0]) { // [HGM] return original piece type rather than just TRUE
423 case 'P': return PAWN;
424 case 'N': return HONORABLEHORSE; // !!! this is Shogi, so no KNIGHT !!!
425 case 'B': return BISHOP;
426 case 'R': return ROOK;
427 case 'L': return LANCE;
428 case 'S': return SILVER;
429 default: return GOLD;
432 if (g->moveList[i].fromFile == ALG_DROP)
434 f = g->moveList[i].fromFile;
435 r = g->moveList[i].fromRank;
442 int pIsPlaying (int p)
444 struct player *pp = &player_globals.parray[p];
446 int p1 = pp->opponent;
448 if (g < 0 || game_globals.garray[g].status != GAME_ACTIVE) {
449 pprintf (p, "You are not playing a game.\n");
453 if (game_globals.garray[g].white != p && game_globals.garray[g].black != p) {
454 /* oh oh; big bad game bug. */
455 d_printf("BUG: Player %s playing game %d according to player_globals.parray,"
456 "\n but not according to game_globals.garray.\n", pp->name, g+1);
457 pprintf (p, "Disconnecting you from game number %d.\n", g+1);
459 if (p1 >= 0 && player_globals.parray[p1].game == g
460 && game_globals.garray[g].white != p1 && game_globals.garray[g].black != p1) {
461 pprintf (p1, "Disconnecting you from game number %d.\n", g+1);
462 player_globals.parray[p1].game = -1;
469 /* add clock increments */
470 static void game_add_increment(struct player *pp, struct game *gg)
472 /* no update on first move */
473 if (gg->game_state.moveNum == 1) return;
475 if (net_globals.con[pp->socket]->timeseal) { /* does he use timeseal? */
476 if (pp->side == WHITE) {
477 gg->wRealTime += gg->wIncrement * 100;
478 gg->wTime = gg->wRealTime / 100; /* remember to conv to
480 } else if (pp->side == BLACK) {
481 gg->bRealTime += gg->bIncrement * 100; /* conv to ms */
482 gg->bTime = gg->bRealTime / 100; /* remember to conv to
486 if (gg->game_state.onMove == BLACK) {
487 gg->bTime += gg->bIncrement;
489 if (gg->game_state.onMove == WHITE) {
490 gg->wTime += gg->wIncrement;
495 /* updates clocks for a game with timeseal */
496 void timeseal_update_clocks(struct player *pp, struct game *gg)
498 /* no update on first move */
499 if (gg->game_state.moveNum == 1) return;
501 if (pp->side == WHITE) {
502 gg->wLastRealTime = gg->wRealTime;
503 gg->wTimeWhenMoved = net_globals.con[pp->socket]->time;
504 if (((gg->wTimeWhenMoved - gg->wTimeWhenReceivedMove) < 0) ||
505 (gg->wTimeWhenReceivedMove == 0)) {
506 /* might seem weird - but could be caused by a person moving BEFORE
507 he receives the board pos (this is possible due to lag) but it's
508 safe to say he moved in 0 secs :-) */
509 gg->wTimeWhenReceivedMove = gg->wTimeWhenMoved;
511 gg->wRealTime -= gg->wTimeWhenMoved - gg->wTimeWhenReceivedMove;
513 } else if (pp->side == BLACK) {
514 gg->bLastRealTime = gg->bRealTime;
515 gg->bTimeWhenMoved = net_globals.con[pp->socket]->time;
516 if (((gg->bTimeWhenMoved - gg->bTimeWhenReceivedMove) < 0) ||
517 (gg->bTimeWhenReceivedMove == 0)) {
518 /* might seem weird - but could be caused by a person moving BEFORE
519 he receives the board pos (this is possible due to lag) but it's
520 safe to say he moved in 0 secs :-) */
521 gg->bTimeWhenReceivedMove = gg->bTimeWhenMoved;
523 gg->bRealTime -= gg->bTimeWhenMoved - gg->bTimeWhenReceivedMove;
529 void process_move(int p, char *command)
531 struct player *pp = &player_globals.parray[p];
533 int g, result, len, i, f;
538 pprintf(p, "You are not playing or examining a game.\n");
541 decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
544 gg = &game_globals.garray[g];
546 if (gg->status == GAME_SETUP) {
547 if (!attempt_drop(p,g,command)) {
548 pprintf(p, "You are still setting up the position.\n");
549 pprintf(p, "Type: 'setup done' when you are finished editing.\n");
555 if (gg->status != GAME_EXAMINE) {
556 if (!pIsPlaying(p)) return;
558 if (pp->side != gg->game_state.onMove) {
559 pprintf(p, "It is not your move.\n");
562 if (gg->clockStopped) {
563 pprintf(p, "Game clock is paused, use \"unpause\" to resume.\n");
567 pp->promote = NOPIECE; // [HGM] this seemed to be uninitialized, which caused spurious promotion in Shogi
568 if ((len = strlen(command)) > 1) {
569 if (command[len - 2] == '=' || gg->game_state.drops == 2 && command[len - 2] == '/') { // [HGM] encode gating as promotion
570 printf("promo '%s'\n", command);
571 switch (tolower(command[len - 1])) {
573 pp->promote = KNIGHT;
576 pp->promote = BISHOP;
582 pp->promote = CARDINAL;
585 pp->promote = MARSHALL;
597 // Superchess promotions
599 pp->promote = EMPRESS;
602 pp->promote = PRINCESS;
605 pp->promote = CENTAUR;
611 pp->promote = SQUIRREL;
614 pp->promote = MASTODON;
617 pp->promote = LIEUTENANT;
624 pp->promote = DRAGONHORSE;
627 pp->promote = DRAGONKING;
634 pp->promote = NOPIECE;
637 pprintf(p, "Don't understand that move.\n");
644 switch (parse_move(command, &gg->game_state, &move, pp->promote)) {
646 pprintf(p, "Illegal move.\n");
650 pprintf(p, "Ambiguous move.\n");
657 if (gg->status == GAME_EXAMINE) {
659 if (gg->numHalfMoves > gg->examMoveListSize) {
660 gg->examMoveListSize += 20; /* Allocate 20 moves at a time */
661 gg->examMoveList = (struct move_t *) realloc(gg->examMoveList, sizeof(struct move_t) * gg->examMoveListSize);
663 result = execute_move(&gg->game_state, &move, 1);
666 MakeFENpos(g, move.FENpos);
667 gg->examMoveList[gg->numHalfMoves - 1] = move;
669 if (gg->game_state.onMove == WHITE) {
670 gg->wTime += (gg->lastDecTime - gg->lastMoveTime);
672 gg->bTime += (gg->lastDecTime - gg->lastMoveTime);
675 if (gg->numHalfMoves == 0)
676 gg->timeOfStart = now;
677 gg->lastMoveTime = now;
678 gg->lastDecTime = now;
680 } else { /* real game */
682 if ((player_globals.parray[i].simul_info != NULL) && (player_globals.parray[i].simul_info->numBoards &&
683 (player_globals.parray[i].simul_info->boards[player_globals.parray[i].simul_info->onBoard] != g))) {
684 pprintf(p, "It isn't your turn: wait until the simul giver is at your board.\n");
687 if (net_globals.con[pp->socket]->timeseal) { /* does he use timeseal? */
688 timeseal_update_clocks(pp, &game_globals.garray[g]);
690 /* we need to reset the opp's time for receiving the board since the
691 timeseal decoder only alters the time if it's 0 Otherwise the time
692 would be changed if the player did a refresh which would screw up
694 if (pp->side == WHITE) {
695 gg->bTimeWhenReceivedMove = 0;
697 gg->wTimeWhenReceivedMove = 0;
701 game_add_increment(pp, gg);
705 if (gg->numHalfMoves > gg->moveListSize) {
706 gg->moveListSize += 20; /* Allocate 20 moves at a time */
707 gg->moveList = (struct move_t *) realloc(gg->moveList, sizeof(struct move_t) * gg->moveListSize);
709 result = execute_move(&gg->game_state, &move, 1);
710 if (result == MOVE_OK && (gg->link >= 0 || gg->game_state.holdings) && move.pieceCaptured != NOPIECE) {
711 /* transfer captured piece to partner */
712 /* check if piece reverts to a pawn */
713 int victim = move.pieceCaptured, partner = gg->link, demoted;
714 // [HGM] zh: if not Bughouse, the game_state.holdings field decides what happens
716 partner = g; // pieces stay with current board
717 if(gg->game_state.holdings == -1) victim ^= WHITE|BLACK; // flip color
719 if (demoted = was_promoted(&game_globals.garray[g], move.toFile, move.toRank))
720 update_holding(partner, colorval(victim) | demoted); // [HGM] was_promoted now returns original piece type
722 update_holding(partner, victim);
726 if (gg->numHalfMoves > 1) {
727 move.tookTime = move.atTime - gg->lastMoveTime;
729 move.tookTime = move.atTime - gg->startTime;
731 gg->lastMoveTime = now;
732 gg->lastDecTime = now;
733 move.wTime = gg->wTime;
734 move.bTime = gg->bTime;
736 if (net_globals.con[pp->socket]->timeseal) { /* does he use timeseal? */
737 if (pp->side == WHITE) {
738 move.tookTime = (game_globals.garray[pp->game].wTimeWhenMoved -
739 game_globals.garray[pp->game].wTimeWhenReceivedMove) / 100;
741 move.tookTime = (game_globals.garray[pp->game].bTimeWhenMoved -
742 game_globals.garray[pp->game].bTimeWhenReceivedMove) / 100;
746 if (gg->numHalfMoves <= 2) {
750 MakeFENpos(g, move.FENpos);
751 gg->moveList[gg->numHalfMoves - 1] = move;
756 if (result == MOVE_ILLEGAL) {
757 pprintf(p, "Internal error, illegal move accepted!\n");
759 if ((result == MOVE_OK) && (gg->status == GAME_EXAMINE)) {
762 for (p1 = 0; p1 < player_globals.p_num; p1++) {
763 if (player_globals.parray[p1].status != PLAYER_PROMPT)
765 if (player_is_observe(p1, g) || player_globals.parray[p1].game == g) {
766 pprintf(p1, "%s moves: %s\n", pp->name, move.algString);
770 if (result == MOVE_CHECKMATE) {
771 if (gg->status == GAME_EXAMINE) {
774 for (p1 = 0; p1 < player_globals.p_num; p1++) {
775 if (player_globals.parray[p1].status != PLAYER_PROMPT)
777 if (player_is_observe(p1, g) || player_globals.parray[p1].game == g) {
778 pprintf(p1, "%s has been checkmated.\n",
779 (CToggle(gg->game_state.onMove) == BLACK) ? "White" : "Black");
783 game_ended(g, CToggle(gg->game_state.onMove), END_CHECKMATE);
786 if (result == MOVE_STALEMATE) {
787 if (gg->status == GAME_EXAMINE) {
790 for (p1 = 0; p1 < player_globals.p_num; p1++) {
791 if (player_globals.parray[p1].status != PLAYER_PROMPT)
793 if (player_is_observe(p1, g) || player_globals.parray[p1].game == g) {
794 pprintf(p1, "Stalemate.\n");
798 game_ended(g, CToggle(gg->game_state.onMove), END_STALEMATE);
801 if (result == MOVE_NOMATERIAL) {
802 if (gg->status == GAME_EXAMINE) {
805 for (p1 = 0; p1 < player_globals.p_num; p1++) {
806 if (player_globals.parray[p1].status != PLAYER_PROMPT)
808 if (player_is_observe(p1, g) || player_globals.parray[p1].game == g) {
809 pprintf(p1, "No mating material.\n");
813 game_ended(g, CToggle(gg->game_state.onMove), END_NOMATERIAL);
816 if (result == MOVE_BARE) {
817 if (gg->status == GAME_EXAMINE) {
820 for (p1 = 0; p1 < player_globals.p_num; p1++) {
821 if (player_globals.parray[p1].status != PLAYER_PROMPT)
823 if (player_is_observe(p1, g) || player_globals.parray[p1].game == g) {
824 pprintf(p1, "%s bared.\n",
825 (gg->game_state.onMove == BLACK) ? "White" : "Black");
829 game_ended(g, gg->game_state.onMove, END_BARE);
834 int com_resign(int p, param_list param)
836 struct player *pp = &player_globals.parray[p];
837 int g, o, oconnected;
839 if (param[0].type == TYPE_NULL) {
844 decline_withdraw_offers(p, -1, -1, DO_DECLINE);
845 game_ended(g, (game_globals.garray[g].white == p) ? BLACK : WHITE, END_RESIGN);
847 } else if (FindPlayer(p, param[0].val.word, &o, &oconnected)) {
849 if (game_read(g, p, o) < 0) {
850 if (game_read(g, o, p) < 0) {
851 pprintf(p, "You have no stored game with %s\n", player_globals.parray[o].name);
856 game_globals.garray[g].white = o;
857 game_globals.garray[g].black = p;
860 game_globals.garray[g].white = p;
861 game_globals.garray[g].black = o;
863 pprintf(p, "You resign your stored game with %s\n", player_globals.parray[o].name);
864 pcommand(p, "message %s I have resigned our stored game \"%s vs. %s.\"",
865 player_globals.parray[o].name,
866 player_globals.parray[game_globals.garray[g].white].name,
867 player_globals.parray[game_globals.garray[g].black].name);
868 game_delete(game_globals.garray[g].white, game_globals.garray[g].black);
869 game_ended(g, (game_globals.garray[g].white == p) ? BLACK : WHITE, END_RESIGN);
876 static int Check50MoveRule (int p, int g)
878 int num_reversible = game_globals.garray[g].numHalfMoves;
880 if (game_globals.garray[g].game_state.lastIrreversable >= 0) {
881 num_reversible -= game_globals.garray[g].game_state.lastIrreversable;
883 if (num_reversible > 99) {
884 game_ended(g, (game_globals.garray[g].white == p) ? BLACK : WHITE, END_50MOVERULE);
890 static int perp_check(struct game g, int first, int third)
892 struct game_state_t gs = g.game_state; // current position, both first and last of loop
893 int half_move, no_perp = 0;
894 printf("perp %d %d\n",first,third);
895 for(half_move=first+1; half_move<third; half_move++) {
896 gs.onMove = CToggle(gs.onMove);
897 if(!in_check(&gs)) no_perp |= (half_move&1) + 1; // 1 = white not in check, 2 = black not in check
898 gs.onMove = CToggle(gs.onMove);
899 printf("move%d, p=%d\n",half_move,no_perp);
900 if(no_perp == 3) break;
901 execute_move(&gs, &g.moveList[half_move], 0);
903 if(no_perp == (third&1) + 1) return END_NOTENDED; // stm was checking, other not: defer judgement
904 if(no_perp == 2 - (third&1)) return END_PERPETUAL; // stm was not checking, other was: stm wins
905 if(no_perp == 0) return END_REPETITION; // mutual perpertual check, draw
906 // here we should check for chasing
907 return END_REPETITION;
910 static char *GetFENpos (int g, int half_move)
913 return game_globals.garray[g].FENstartPos;
914 else return game_globals.garray[g].moveList[half_move].FENpos;
917 static int CheckRepetition (int p, int g)
919 struct player *pp = &player_globals.parray[p];
920 struct pending* pend;
921 int move_num, s1, s2, result = END_REPETITION;
922 int flag1 = 1, flag2 = 1;
923 int numPly = game_globals.garray[g].numHalfMoves;
924 char *pos1 = GetFENpos (g, numPly - 1); // current position
928 if (numPly < 8) /* can't have three repeats any quicker. */
931 if((game_globals.garray[g].white == p) != (numPly&1)) { // claimer has the move
933 pos1 = GetFENpos (g, numPly - 2); // also check position before opponent's move (which could have pre-empted him)
934 } // pos1 is now always a position where the opponent has the move
936 for (move_num = numPly - 2; // [HGM] FEN stored in moveList[numHalfMoves-1] !
937 move_num >= game_globals.garray[g].game_state.lastIrreversable - 1; move_num--) {
938 pos = GetFENpos (g, move_num);
939 if (strlen(pos1) == strlen(pos) && !strcmp(pos1, pos))
940 flag1++ == 2 && (s1 = move_num);
941 if (strlen(pos2) == strlen(pos) && !strcmp(pos2, pos))
942 flag2++ == 2 && (s2 = move_num); // remember start of last two loops
943 printf("%2d. %d-%d '%s' '%s' '%s'\n", move_num, flag1, flag2, pos1,pos2,pos);
945 if (flag1 >= 3 || flag2 >= 3) {
946 if ((pend = find_pend(pp->opponent, p, PEND_DRAW)) != NULL) {
947 delete_pending(pend);
948 decline_withdraw_offers(p, -1, -1,DO_DECLINE);
950 if(game_globals.garray[g].game_state.palace) { // [HGM] in Xiangqi we have to test for perpetuals to determine the outcome
951 if(flag2 >= 3) result = perp_check(game_globals.garray[g], s2, numPly);
952 else result = perp_check(game_globals.garray[g], s1, numPly - (pos2[0] != 0));
953 if(result == END_NOTENDED) {
954 pprintf(p, "Perpetuals can be claimed only during the turn of the winner\n");
957 game_ended(g, (numPly&1) ? BLACK : WHITE, result); // stm wins
960 game_ended(g, (game_globals.garray[g].white == p) ? BLACK : WHITE, result);
966 int com_draw(int p, param_list param)
968 struct player *pp = &player_globals.parray[p];
969 struct pending* pend;
970 int p1, g = pp->game;
972 if (!pIsPlaying(p)) {
975 if (Check50MoveRule (p, g) || CheckRepetition(p, g)) {
980 if ((player_globals.parray[p1].simul_info != NULL) && (player_globals.parray[p1].simul_info->numBoards &&
981 player_globals.parray[p1].simul_info->boards[player_globals.parray[p1].simul_info->onBoard] != g)) {
982 pprintf(p, "You can only make requests when the simul player is at your board.\n");
986 if ((pend = (find_pend(pp->opponent, p, PEND_DRAW))) != NULL) {
987 delete_pending(pend);
988 decline_withdraw_offers(p, -1, -1,DO_DECLINE);
989 game_ended(g, (game_globals.garray[g].white == p) ? BLACK : WHITE, END_AGREEDDRAW);
991 pprintf(pp->opponent, "\n");
992 pprintf_highlight(pp->opponent, "%s", pp->name);
993 pprintf_prompt(pp->opponent, " offers you a draw.\n");
994 pprintf(p, "Draw request sent.\n");
995 add_request(p, pp->opponent, PEND_DRAW);
1000 int com_pause(int p, param_list param)
1002 struct player *pp = &player_globals.parray[p];
1004 struct pending* pend;
1006 if (!pIsPlaying(p)) {
1010 if (game_globals.garray[g].wTime == 0) {
1011 pprintf(p, "You can't pause untimed games.\n");
1014 if (game_globals.garray[g].clockStopped) {
1015 pprintf(p, "Game is already paused, use \"unpause\" to resume.\n");
1018 if ((pend = find_pend(pp->opponent, p, PEND_PAUSE)) != NULL) {
1019 delete_pending(pend);
1020 game_globals.garray[g].clockStopped = 1;
1021 /* Roll back the time */
1022 if (game_globals.garray[g].game_state.onMove == WHITE) {
1023 game_globals.garray[g].wTime += (game_globals.garray[g].lastDecTime - game_globals.garray[g].lastMoveTime);
1025 game_globals.garray[g].bTime += (game_globals.garray[g].lastDecTime - game_globals.garray[g].lastMoveTime);
1028 if (game_globals.garray[g].numHalfMoves == 0)
1029 game_globals.garray[g].timeOfStart = now;
1030 game_globals.garray[g].lastMoveTime = now;
1031 game_globals.garray[g].lastDecTime = now;
1033 pprintf_prompt(pp->opponent, "\n%s accepted pause. Game clock paused.\n",
1035 pprintf(p, "Game clock paused.\n");
1037 pprintf(pp->opponent, "\n");
1038 pprintf_highlight(pp->opponent, "%s", pp->name);
1039 pprintf_prompt(pp->opponent, " requests to pause the game.\n");
1040 pprintf(p, "Pause request sent.\n");
1041 add_request(p, pp->opponent, PEND_PAUSE);
1046 int com_unpause(int p, param_list param)
1048 struct player *pp = &player_globals.parray[p];
1051 struct pending* pend;
1053 if (!pIsPlaying(p)) {
1059 if (!game_globals.garray[g].clockStopped) {
1060 pprintf(p, "Game is not paused.\n");
1063 if ((pend = find_pend(pp->opponent, p, PEND_UNPAUSE)) != NULL) {
1064 delete_pending(pend);
1065 game_globals.garray[g].clockStopped = 0;
1067 if (game_globals.garray[g].numHalfMoves == 0)
1068 game_globals.garray[g].timeOfStart = now;
1069 game_globals.garray[g].lastMoveTime = now;
1070 game_globals.garray[g].lastDecTime = now;
1072 pprintf(p, "Game clock resumed.\n");
1073 pprintf_prompt(pp->opponent, "\nGame clock resumed.\n");
1075 pprintf(pp->opponent, "\n");
1076 pprintf_highlight(pp->opponent, "%s", pp->name);
1077 pprintf_prompt(pp->opponent, " requests to unpause the game.\n");
1078 pprintf(p, "Unpause request sent.\n");
1079 add_request(p, pp->opponent, PEND_UNPAUSE);
1084 int com_abort(int p, param_list param)
1086 struct player *pp = &player_globals.parray[p];
1087 struct pending* pend;
1088 int p1, g, myColor, yourColor, myGTime, yourGTime;
1096 if (p == game_globals.garray[g].white) {
1099 myGTime = game_globals.garray[g].wTime;
1100 yourGTime = game_globals.garray[g].bTime;
1104 myGTime = game_globals.garray[g].bTime;
1105 yourGTime = game_globals.garray[g].wTime;
1107 if ((player_globals.parray[p1].simul_info != NULL) &&
1108 (player_globals.parray[p1].simul_info->numBoards &&
1109 player_globals.parray[p1].simul_info->boards[player_globals.parray[p1].simul_info->onBoard] != g)) {
1110 pprintf(p, "You can only make requests when the simul player is at your board.\n");
1113 if ((pend = find_pend(p1, p, PEND_ABORT)) != NULL) {
1114 delete_pending(pend);
1115 decline_withdraw_offers(p, -1, -1,DO_DECLINE);
1116 game_ended(g, yourColor, END_ABORT);
1118 game_update_time(g);
1120 if (net_globals.con[pp->socket]->timeseal
1121 && game_globals.garray[g].game_state.onMove == myColor
1122 && game_globals.garray[g].flag_pending == FLAG_ABORT) {
1123 /* It's my move, opponent has asked for abort; I lagged out,
1124 my timeseal prevented courtesyabort, and I am sending an abort
1125 request before acknowledging (and processing) my opponent's
1126 courtesyabort. OK, let's abort already :-). */
1127 decline_withdraw_offers(p, -1, -1,DO_DECLINE);
1128 game_ended(g, yourColor, END_ABORT);
1131 if (net_globals.con[player_globals.parray[p1].socket]->timeseal) { /* opp uses timeseal? */
1133 int yourRealTime = (myColor == WHITE ? game_globals.garray[g].bRealTime
1134 : game_globals.garray[g].wRealTime);
1135 if (myGTime > 0 && yourGTime <= 0 && yourRealTime > 0) {
1136 /* Override courtesyabort; opponent still has time. Check for lag. */
1139 if (game_globals.garray[g].game_state.onMove != myColor
1140 && game_globals.garray[g].flag_pending != FLAG_CHECKING) {
1141 /* Opponent may be lagging; let's ask. */
1142 game_globals.garray[g].flag_pending = FLAG_ABORT;
1143 game_globals.garray[g].flag_check_time = time(0);
1144 pprintf(p, "Opponent has timeseal; trying to courtesyabort.\n");
1145 pprintf(p1, "\n[G]\n");
1151 if (myGTime > 0 && yourGTime <= 0 && courtesyOK) {
1152 /* player wants to abort + opponent is out of time = courtesyabort */
1153 pprintf(p, "Since you have time, and your opponent has none, the game has been aborted.");
1154 pprintf(p1, "Your opponent has aborted the game rather than calling your flag.");
1155 decline_withdraw_offers(p, -1, -1, DO_DECLINE);
1156 game_ended(g, myColor, END_COURTESY);
1159 pprintf_highlight(p1, "%s", pp->name);
1160 pprintf(p1, " would like to abort the game; ");
1161 pprintf_prompt(p1, "type \"abort\" to accept.\n");
1162 pprintf(p, "Abort request sent.\n");
1163 add_request(p, p1, PEND_ABORT);
1169 static int player_has_mating_material(struct game_state_t *gs, int color)
1173 int minor_pieces = 0;
1175 for (i = 0; i < gs->files; i++)
1176 for (j = 0; j < gs->ranks; j++) {
1177 piece = gs->board[i][j];
1178 switch (piecetype(piece)) {
1181 if (iscolor(piece, color))
1188 if (iscolor(piece, color))
1192 return ((minor_pieces > 1) ? 1 : 0);
1195 int com_flag(int p, param_list param)
1197 struct player *pp = &player_globals.parray[p];
1202 if (!pIsPlaying(p)) {
1207 gg = &game_globals.garray[g];
1209 myColor = (p == gg->white ? WHITE : BLACK);
1210 if (gg->type == TYPE_UNTIMED) {
1211 pprintf(p, "You can't flag an untimed game.\n");
1214 if (gg->numHalfMoves < 2) {
1215 pprintf(p, "You cannot flag before both players have moved.\nUse abort instead.\n");
1218 game_update_time(g);
1221 int myTime, yourTime, opp = pp->opponent, serverTime;
1223 if (net_globals.con[pp->socket]->timeseal) { /* does caller use timeseal? */
1224 myTime = (myColor==WHITE?gg->wRealTime:gg->bRealTime);
1226 myTime = (myColor == WHITE?gg->wTime:gg->bTime);
1228 serverTime = (myColor == WHITE?gg->bTime:gg->wTime);
1230 if (net_globals.con[player_globals.parray[opp].socket]->timeseal) { /* opp uses timeseal? */
1231 yourTime = (myColor == WHITE?gg->bRealTime:gg->wRealTime);
1233 yourTime = serverTime;
1236 /* the clocks to compare are now in myTime and yourTime */
1237 if ((myTime <= 0) && (yourTime <= 0)) {
1238 decline_withdraw_offers(p, -1, -1,DO_DECLINE);
1239 game_ended(g, myColor, END_BOTHFLAG);
1244 /* Opponent still has time, but if that's only because s/he
1245 * may be lagging, we should ask for an acknowledgement and then
1246 * try to call the flag. */
1248 if (serverTime <= 0 && gg->game_state.onMove != myColor
1249 && gg->flag_pending != FLAG_CHECKING) {
1250 /* server time thinks opponent is down, but RealTIme disagrees.
1251 * ask client to acknowledge it's alive. */
1252 gg->flag_pending = FLAG_CALLED;
1253 gg->flag_check_time = time(0);
1254 pprintf(p, "Opponent has timeseal; checking if (s)he's lagging.\n");
1255 pprintf (opp, "\n[G]\n");
1259 /* if we're here, it means one of:
1260 * 1. the server agrees opponent has time, whether lagging or not.
1261 * 2. opp. has timeseal (if yourTime != serverTime), had time left
1262 * after the last move (yourTime > 0), and it's still your move.
1263 * 3. we're currently checking a flag call after having receiving
1264 * acknowledgement from the other timeseal (and would have reset
1265 * yourTime if the flag were down). */
1267 pprintf(p, "Your opponent is not out of time!\n");
1272 decline_withdraw_offers(p, -1, -1,DO_DECLINE);
1273 if (player_has_mating_material(&gg->game_state, myColor))
1274 game_ended(g, myColor, END_FLAG);
1276 game_ended(g, myColor, END_FLAGNOMATERIAL);
1280 int com_adjourn(int p, param_list param)
1282 struct player *pp = &player_globals.parray[p];
1283 struct pending* pend;
1284 int p1, g, myColor, yourColor;
1291 if (!CheckPFlag(p, PFLAG_REG) || !CheckPFlag(p, PFLAG_REG)) {
1292 pprintf(p, "Both players must be registered to adjourn a game. Use \"abort\".\n");
1295 if (game_globals.garray[g].link >= 0) {
1296 pprintf(p, "Bughouse games cannot be adjourned.\n");
1299 myColor = (p == game_globals.garray[g].white ? WHITE : BLACK);
1300 yourColor = (myColor == WHITE ? BLACK : WHITE);
1302 if ((pend = find_pend(p1, p, PEND_ADJOURN)) != NULL) {
1303 delete_pending(pend);
1304 decline_withdraw_offers(p, -1, -1,DO_DECLINE);
1305 game_ended(pp->game, yourColor, END_ADJOURN);
1307 game_update_time(g);
1308 if (((myColor == WHITE) && (game_globals.garray[g].wTime > 0) && (game_globals.garray[g].bTime <= 0))
1309 || ((myColor == BLACK) && (game_globals.garray[g].bTime > 0) && (game_globals.garray[g].wTime <= 0))) {
1310 /* player wants to adjourn + opponent is out of time = courtesyadjourn */
1311 pprintf(p, "Since you have time, and your opponent has none, the game has been adjourned.");
1312 pprintf(p1, "Your opponent has adjourned the game rather than calling your flag.");
1313 decline_withdraw_offers(p, -1, -1,DO_DECLINE);
1314 game_ended(g, myColor, END_COURTESYADJOURN);
1317 pprintf_highlight(p1, "%s", pp->name);
1318 pprintf(p1, " would like to adjourn the game; ");
1319 pprintf_prompt(p1, "type \"adjourn\" to accept.\n");
1320 pprintf(p, "Adjourn request sent.\n");
1321 add_request(p, p1, PEND_ADJOURN);
1327 int com_takeback(int p, param_list param)
1329 struct player *pp = &player_globals.parray[p];
1330 int nHalfMoves = 1, g, i, p1, pend_half_moves;
1331 struct pending* from;
1337 if ((player_globals.parray[p1].simul_info != NULL) &&
1338 (player_globals.parray[p1].simul_info->numBoards &&
1339 player_globals.parray[p1].simul_info->boards[player_globals.parray[p1].simul_info->onBoard] !=
1341 pprintf(p, "You can only make requests when the simul player is at your board.\n");
1346 if (game_globals.garray[g].link >= 0) {
1347 pprintf(p, "Takeback not implemented for bughouse games yet.\n");
1350 if (param[0].type == TYPE_INT) {
1351 nHalfMoves = param[0].val.integer;
1352 if (nHalfMoves <= 0) {
1353 pprintf (p,"You can't takeback less than 1 move.\n");
1357 if ((from = find_pend(pp->opponent, p, PEND_TAKEBACK)) != NULL) {
1358 pend_half_moves = from->wtime;
1359 delete_pending(from);
1360 if (pend_half_moves == nHalfMoves) {
1361 /* Doing the takeback */
1362 decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1363 for (i = 0; i < nHalfMoves; i++) {
1364 if (backup_move(g, REL_GAME) != MOVE_OK) {
1365 pprintf(game_globals.garray[g].white, "Can only backup %d moves\n", i);
1366 pprintf(game_globals.garray[g].black, "Can only backup %d moves\n", i);
1371 game_globals.garray[g].wTimeWhenReceivedMove = 0;
1372 game_globals.garray[g].bTimeWhenReceivedMove = 0;
1377 if (!game_globals.garray[g].numHalfMoves) {
1378 pprintf(p, "There are no moves in your game.\n");
1379 pprintf_prompt(pp->opponent, "\n%s has declined the takeback request.\n",
1384 if (game_globals.garray[g].numHalfMoves < nHalfMoves) {
1385 pprintf(p, "There are only %d half moves in your game.\n", game_globals.garray[g].numHalfMoves);
1386 pprintf_prompt(pp->opponent, "\n%s has declined the takeback request.\n",
1390 pprintf(p, "You disagree on the number of half-moves to takeback.\n");
1391 pprintf(p, "Alternate takeback request sent.\n");
1392 pprintf_prompt(pp->opponent, "\n%s proposes a different number (%d) of half-move(s).\n", pp->name, nHalfMoves);
1393 from = add_request(p, pp->opponent, PEND_TAKEBACK);
1394 from->wtime = nHalfMoves;
1398 if (!game_globals.garray[g].numHalfMoves) {
1399 pprintf(p, "There are no moves in your game.\n");
1402 if (game_globals.garray[g].numHalfMoves < nHalfMoves) {
1403 pprintf(p, "There are only %d half moves in your game.\n", game_globals.garray[g].numHalfMoves);
1406 pprintf(pp->opponent, "\n");
1407 pprintf_highlight(pp->opponent, "%s", pp->name);
1408 pprintf_prompt(pp->opponent, " would like to take back %d half move(s).\n",
1410 pprintf(p, "Takeback request sent.\n");
1411 from = add_request(p, pp->opponent, PEND_TAKEBACK);
1412 from->wtime = nHalfMoves;
1418 int com_switch(int p, param_list param)
1420 struct player *pp = &player_globals.parray[p];
1421 int g = pp->game, tmp, now, p1;
1423 struct pending* pend;
1429 if ((player_globals.parray[p1].simul_info != NULL) && (player_globals.parray[p1].simul_info->numBoards &&
1430 player_globals.parray[p1].simul_info->boards[player_globals.parray[p1].simul_info->onBoard] != g)) {
1431 pprintf(p, "You can only make requests when the simul player is at your board.\n");
1435 if (game_globals.garray[g].link >= 0) {
1436 pprintf(p, "Switch not implemented for bughouse games.\n");
1439 if ((pend = find_pend(pp->opponent, p, PEND_SWITCH)) != NULL) {
1440 delete_pending(pend);
1441 /* Doing the switch */
1442 decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1444 tmp = game_globals.garray[g].white;
1445 game_globals.garray[g].white = game_globals.garray[g].black;
1446 game_globals.garray[g].black = tmp;
1447 pp->side = (pp->side == WHITE) ? BLACK : WHITE;
1448 strTmp = strdup(game_globals.garray[g].white_name);
1449 strcpy(game_globals.garray[g].white_name, game_globals.garray[g].black_name);
1450 strcpy(game_globals.garray[g].black_name, strTmp);
1453 player_globals.parray[pp->opponent].side =
1454 (player_globals.parray[pp->opponent].side == WHITE) ? BLACK : WHITE;
1455 /* Roll back the time */
1456 if (game_globals.garray[g].game_state.onMove == WHITE) {
1457 game_globals.garray[g].wTime += (game_globals.garray[g].lastDecTime - game_globals.garray[g].lastMoveTime);
1459 game_globals.garray[g].bTime += (game_globals.garray[g].lastDecTime - game_globals.garray[g].lastMoveTime);
1462 if (game_globals.garray[g].numHalfMoves == 0)
1463 game_globals.garray[g].timeOfStart = now;
1464 game_globals.garray[g].lastMoveTime = now;
1465 game_globals.garray[g].lastDecTime = now;
1469 if (game_globals.garray[g].rated && game_globals.garray[g].numHalfMoves > 0) {
1470 pprintf(p, "You cannot switch sides once a rated game is underway.\n");
1473 pprintf(pp->opponent, "\n");
1474 pprintf_highlight(pp->opponent, "%s", pp->name);
1475 pprintf_prompt(pp->opponent, " would like to switch sides.\nType \"accept\" to switch sides, or \"decline\" to refuse.\n");
1476 pprintf(p, "Switch request sent.\n");
1477 add_request(p, pp->opponent, PEND_SWITCH);
1481 int com_time(int p, param_list param)
1483 struct player *pp = &player_globals.parray[p];
1486 if (param[0].type == TYPE_NULL) {
1491 g = GameNumFromParam(p, &p1, ¶m[0]);
1495 if ((g < 0) || (g >= game_globals.g_num) || (game_globals.garray[g].status != GAME_ACTIVE)) {
1496 pprintf(p, "There is no such game.\n");
1499 game_update_time(g);
1500 pprintf(p, "White (%s) : %d mins, %d secs\n",
1501 player_globals.parray[game_globals.garray[g].white].name,
1502 game_globals.garray[g].wTime / 600,
1503 (game_globals.garray[g].wTime - ((game_globals.garray[g].wTime / 600) * 600)) / 10);
1504 pprintf(p, "Black (%s) : %d mins, %d secs\n",
1505 player_globals.parray[game_globals.garray[g].black].name,
1506 game_globals.garray[g].bTime / 600,
1507 (game_globals.garray[g].bTime - ((game_globals.garray[g].bTime / 600) * 600)) / 10);
1511 int com_ptime(int p, param_list param)
1513 struct player *pp = &player_globals.parray[p];
1514 int retval, part = pp->partner;
1517 pprintf(p, "You do not have a partner.\n");
1520 retval = pcommand (p, "time %s", player_globals.parray[part].name);
1521 if (retval == COM_OK)
1522 return COM_OK_NOPROMPT;
1527 int com_boards(int p, param_list param)
1529 char *category = NULL;
1530 char dname[MAX_FILENAME_SIZE];
1534 if (param[0].type == TYPE_WORD)
1535 category = param[0].val.word;
1537 pprintf(p, "Boards Available For Category %s:\n", category);
1538 sprintf(dname, "%s/%s", BOARD_DIR, category);
1540 pprintf(p, "Categories Available:\n");
1541 sprintf(dname, "%s", BOARD_DIR);
1543 dirp = opendir(dname);
1545 pprintf(p, "No such category %s, try \"boards\".\n", category);
1549 /* YUK! what a mess, how about printing an ordered directory? - DAV*/
1551 for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
1552 if (!strcmp(dp->d_name, "."))
1554 if (!strcmp(dp->d_name, ".."))
1556 pprintf(p, "%s\n", dp->d_name);
1562 int com_simmatch(int p, param_list param)
1564 struct player *pp = &player_globals.parray[p];
1565 int p1, g, adjourned;
1568 struct pending* pend;
1570 char* category = NULL;
1571 char fname[MAX_FILENAME_SIZE];
1574 if (game_globals.garray[pp->game].status == GAME_EXAMINE) {
1575 pprintf(p, "You are still examining a game.\n");
1578 if (game_globals.garray[pp->game].status == GAME_SETUP) {
1579 pprintf(p, "You are still setting up a position.\n");
1583 p1 = player_find_part_login(param[0].val.word);
1585 pprintf(p, "No user named \"%s\" is logged in.\n", param[0].val.word);
1589 pprintf(p, "You can't simmatch yourself!\n");
1592 if ((pend = find_pend(p1, p, PEND_SIMUL)) != NULL) {
1594 /* Accepting Simul ! */
1596 if ((pp->simul_info != NULL) &&
1597 (pp->simul_info->numBoards >= MAX_SIMUL)) {
1598 pprintf(p, "You are already playing the maximum of %d boards.\n", MAX_SIMUL);
1599 pprintf(p1, "Simul request removed, boards filled.\n");
1600 delete_pending(pend);
1603 unobserveAll(p); /* stop observing when match starts */
1608 if (game_read(g, p, p1) >= 0) {
1610 delete_pending(pend);
1613 if (!adjourned) { /* no adjourned game, so begin a new game */
1615 if ((pend->category != NULL) && (pend->board_type != NULL)) {
1616 board = strdup(pend->category);
1617 category = strdup(pend->board_type);
1620 delete_pending(pend);
1622 if (create_new_match(g,p, p1, 0, 0, 0, 0, 0, ((board == NULL) ? "\0" : board), ((category == NULL) ? "\0" : category), 1,1) == COM_FAILED) {
1623 pprintf(p, "There was a problem creating the new match.\n");
1624 pprintf_prompt(p1, "There was a problem creating the new match.\n");
1627 if (board != NULL) {
1634 if (board != NULL) {
1639 } else { /* resume adjourned game */
1642 sprintf(tmp, "{Game %d (%s vs. %s) Continuing %s %s simul.}\n", g + 1, pp->name, player_globals.parray[p1].name, rstr[game_globals.garray[g].rated], bstr[game_globals.garray[g].type]);
1646 game_globals.garray[g].white = p;
1647 game_globals.garray[g].black = p1;
1648 game_globals.garray[g].status = GAME_ACTIVE;
1649 game_globals.garray[g].startTime = tenth_secs();
1650 game_globals.garray[g].lastMoveTime = game_globals.garray[g].startTime;
1651 game_globals.garray[g].lastDecTime = game_globals.garray[g].startTime;
1655 player_globals.parray[p1].game = g;
1656 player_globals.parray[p1].opponent = p;
1657 player_globals.parray[p1].side = BLACK;
1661 if (pp->simul_info == NULL) {
1662 pp->simul_info = (struct simul_info_t *) malloc(sizeof(struct simul_info_t));
1663 pp->simul_info->numBoards = 0;
1664 pp->simul_info->onBoard = 0;
1665 pp->simul_info->num_wins = pp->simul_info->num_draws
1666 = pp->simul_info->num_losses = 0;
1668 num = pp->simul_info->numBoards;
1669 /* pp->simul_info->results[num] = -1; */
1670 pp->simul_info->boards[num] = pp->game;
1671 pp->simul_info->numBoards++;
1672 if (pp->simul_info->numBoards > 1 &&
1673 pp->simul_info->onBoard >= 0)
1674 player_goto_board(p, pp->simul_info->onBoard);
1676 pp->simul_info->onBoard = 0;
1679 if (find_pend(-1, p, PEND_SIMUL) != NULL) {
1680 pprintf(p, "You cannot be the simul giver and request to join another simul.\nThat would just be too confusing for me and you.\n");
1683 if (pp->simul_info != NULL) {
1684 if (pp->simul_info->numBoards) {
1685 pprintf(p, "You cannot be the simul giver and request to join another simul.\nThat would just be too confusing for me and you.\n");
1690 pprintf(p, "You are already playing a game.\n");
1693 if (!CheckPFlag(p1, PFLAG_SIMOPEN)) {
1694 pprintf_highlight(p, "%s", player_globals.parray[p1].name);
1695 pprintf(p, " is not open to receiving simul requests.\n");
1698 if ((player_globals.parray[p1].simul_info != NULL) && (player_globals.parray[p1].simul_info->numBoards >= MAX_SIMUL)) {
1699 pprintf_highlight(p, "%s", player_globals.parray[p1].name);
1700 pprintf(p, " is already playing the maximum of %d boards.\n", MAX_SIMUL);
1704 /* loon: checking for some crazy situations we can't allow :) */
1706 if ((player_globals.parray[p1].simul_info != NULL) && (player_globals.parray[p1].game >=0) && (player_globals.parray[p1].simul_info->numBoards == 0)) {
1707 pprintf_highlight(p, "%s", player_globals.parray[p1].name);
1708 if (player_globals.parray[game_globals.garray[player_globals.parray[p1].game].white].simul_info->numBoards) {
1709 pprintf(p, " is playing in ");
1710 pprintf_highlight(p, "%s", player_globals.parray[player_globals.parray[p1].opponent].name);
1711 pprintf(p, "'s simul, and can't accept.\n");
1713 pprintf(p, " can't begin a simul while playing a non-simul game.\n");
1718 g = game_new(); /* Check if an adjourned untimed game */
1719 adjourned = ((game_read(g, p, p1) < 0) && (game_read(g, p1, p) < 0)) ? 0 : 1;
1721 if (!(game_globals.garray[g].type == TYPE_UNTIMED))
1726 pend = add_request(p, p1, PEND_SIMUL);
1728 if ((param[1].type == TYPE_WORD) && (param[2].type == TYPE_WORD)) {
1730 sprintf(fname, "%s/%s/%s", BOARD_DIR, param[1].val.word , param[2].val.word);
1731 if (!file_exists(fname)) {
1732 pprintf(p, "No such category/board: %s/%s\n", param[1].val.word , param[2].val.word);
1735 pend->category = strdup(param[1].val.word);
1736 pend->board_type = strdup(param[2].val.word);
1738 pend->category = NULL;
1739 pend->board_type = NULL;
1743 pprintf_highlight(p1, "%s", pp->name);
1745 pprintf_prompt(p1, " requests to continue an adjourned simul game.\n");
1746 pprintf(p, "Request to resume simul sent. Adjourned game found.\n");
1748 if (pend->category == NULL)
1749 pprintf_prompt(p1, " requests to join a simul match with you.\n");
1751 pprintf_prompt(p1, " requests to join a %s %s simul match with you.\n",
1752 pend->category,pend->board_type);
1753 pprintf(p, "Simul match request sent.\n");
1758 int com_goboard(int p, param_list param)
1760 struct player *pp = &player_globals.parray[p];
1761 int on, g, p1, gamenum;
1763 if (pp->simul_info == NULL) {
1764 pprintf(p, "You are not giving a simul.\n");
1768 if (!pp->simul_info->numBoards) {
1769 pprintf(p, "You are not giving a simul.\n");
1773 if (param[0].type == TYPE_WORD) {
1775 p1 = player_find_part_login(param[0].val.word);
1777 pprintf(p, "No user named \"%s\" is logged in.\n", param[0].val.word);
1781 pprintf(p, "You can't goboard yourself!\n");
1785 gamenum = player_globals.parray[p1].game;
1787 pprintf (p,"%s is not playing a game.\n", player_globals.parray[p1].login);
1792 gamenum = param[0].val.integer - 1;
1797 on = pp->simul_info->onBoard;
1798 g = pp->simul_info->boards[on];
1800 pprintf(p, "You are already at that board!\n");
1803 if (pp->simul_info->numBoards > 1) {
1804 decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1805 if (player_goto_simulgame_bynum(p, gamenum) !=-1) {
1807 pprintf(game_globals.garray[g].black, "\n");
1808 pprintf_highlight(game_globals.garray[g].black, "%s", pp->name);
1809 pprintf_prompt(game_globals.garray[g].black, " has moved away from your board.\n");
1812 pprintf(p, "You are not playing that game/person.\n");
1814 pprintf(p, "You are only playing one board!\n");
1818 int com_simnext(int p, param_list param)
1820 struct player *pp = &player_globals.parray[p];
1823 if (pp->simul_info == NULL) {
1824 pprintf(p, "You are not giving a simul.\n");
1828 if (!pp->simul_info->numBoards) {
1829 pprintf(p, "You are not giving a simul.\n");
1833 if (pp->simul_info->numBoards > 1) {
1834 decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1835 on = pp->simul_info->onBoard;
1836 g = pp->simul_info->boards[on];
1838 pprintf(game_globals.garray[g].black, "\n");
1839 pprintf_highlight(game_globals.garray[g].black, "%s", pp->name);
1840 pprintf_prompt(game_globals.garray[g].black, " is moving away from your board.\n");
1841 player_goto_next_board(p);
1844 pprintf(p, "You are only playing one board!\n");
1848 int com_simprev(int p, param_list param)
1850 struct player *pp = &player_globals.parray[p];
1853 if (pp->simul_info == NULL) {
1854 pprintf(p, "You are not giving a simul.\n");
1858 if (!pp->simul_info->numBoards) {
1859 pprintf(p, "You are not giving a simul.\n");
1862 if (pp->simul_info->numBoards > 1) {
1863 decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1864 on = pp->simul_info->onBoard;
1865 g = pp->simul_info->boards[on];
1867 pprintf(game_globals.garray[g].black, "\n");
1868 pprintf_highlight(game_globals.garray[g].black, "%s", pp->name);
1869 pprintf_prompt(game_globals.garray[g].black, " is moving back to the previous board.\n");
1871 player_goto_prev_board(p);
1873 pprintf(p, "You are only playing one board!\n");
1877 int com_simgames(int p, param_list param)
1881 if (param[0].type == TYPE_WORD) {
1882 if ((p1 = player_find_part_login(param[0].val.word)) < 0) {
1883 pprintf(p, "No player named %s is logged in.\n", param[0].val.word);
1888 pprintf(p, "You are playing %d simultaneous games.\n",
1889 player_num_active_boards(p1));
1891 pprintf(p, "%s is playing %d simultaneous games.\n", player_globals.parray[p1].name,
1892 player_num_active_boards(p1));
1896 int com_simpass(int p, param_list param)
1898 struct player *pp = &player_globals.parray[p];
1905 p1 = game_globals.garray[g].white;
1907 if (player_globals.parray[p1].simul_info == NULL) {
1908 pprintf(p, "You are not participating in a simul.\n");
1912 if (!player_globals.parray[p1].simul_info->numBoards) {
1913 pprintf(p, "You are not participating in a simul.\n");
1917 pprintf(p, "You are the simul holder and cannot pass!\n");
1920 if (player_num_active_boards(p1) == 1) {
1921 pprintf(p, "This is the only game, so passing is futile.\n");
1924 on = player_globals.parray[p1].simul_info->onBoard;
1925 if (player_globals.parray[p1].simul_info->boards[on] != g) {
1926 pprintf(p, "You cannot pass until the simul holder arrives!\n");
1929 if (game_globals.garray[g].passes >= MAX_SIMPASS) {
1931 pprintf(p, "You have reached your maximum of %d pass(es).\n", MAX_SIMPASS);
1932 pprintf(p, "Please move IMMEDIATELY!\n");
1933 pprintf_highlight(p1, "%s", pp->name);
1934 pprintf_prompt(p1, " tried to pass, but is out of passes.\n");
1937 decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1939 game_globals.garray[g].passes++;
1940 pprintf(p, "You have passed and have %d pass(es) left.\n",
1941 (MAX_SIMPASS - game_globals.garray[g].passes));
1942 pprintf_highlight(p1, "%s", pp->name);
1943 pprintf_prompt(p1, " has decided to pass and has %d pass(es) left.\n",
1944 (MAX_SIMPASS - game_globals.garray[g].passes));
1945 player_goto_next_board(p1);
1949 int com_simabort(int p, param_list param)
1951 struct player *pp = &player_globals.parray[p];
1953 if (pp->simul_info == NULL) {
1954 pprintf(p, "You are not giving a simul.\n");
1958 if (!pp->simul_info->numBoards) {
1959 pprintf(p, "You are not giving a simul.\n");
1962 decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1963 game_ended(pp->simul_info->boards[pp->simul_info->onBoard],
1968 int com_simallabort(int p, param_list param)
1970 struct player *pp = &player_globals.parray[p];
1973 if (pp->simul_info == NULL) {
1974 pprintf(p, "You are not giving a simul.\n");
1978 if (!pp->simul_info->numBoards) {
1979 pprintf(p, "You are not giving a simul.\n");
1983 decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1984 for (i = 0; i < pp->simul_info->numBoards; i++)
1985 if (pp->simul_info->boards[i] >= 0)
1986 game_ended(pp->simul_info->boards[i],
1992 int com_simadjourn(int p, param_list param)
1994 struct player *pp = &player_globals.parray[p];
1996 if (pp->simul_info == NULL) {
1997 pprintf(p, "You are not giving a simul.\n");
2001 if (!pp->simul_info->numBoards) {
2002 pprintf(p, "You are not giving a simul.\n");
2005 decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
2006 game_ended(pp->simul_info->boards[pp->simul_info->onBoard],
2007 WHITE, END_ADJOURN);
2011 int com_simalladjourn(int p, param_list param)
2013 struct player *pp = &player_globals.parray[p];
2016 if (pp->simul_info == NULL) {
2017 pprintf(p, "You are not giving a simul.\n");
2021 if (!pp->simul_info->numBoards) {
2022 pprintf(p, "You are not giving a simul.\n");
2025 decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
2026 for (i = 0; i < pp->simul_info->numBoards; i++)
2027 if (pp->simul_info->boards[i] >= 0)
2028 game_ended(pp->simul_info->boards[i],
2029 WHITE, END_ADJOURN);
2034 int com_moretime(int p, param_list param)
2036 struct player *pp = &player_globals.parray[p];
2039 if ((pp->game >=0) &&((game_globals.garray[pp->game].status == GAME_EXAMINE) ||
2040 (game_globals.garray[pp->game].status == GAME_SETUP))) {
2041 pprintf(p, "You cannot use moretime in an examined game.\n");
2044 increment = param[0].val.integer;
2045 if (increment <= 0) {
2046 pprintf(p, "Moretime requires an integer value greater than zero.\n");
2052 if (increment > 600) {
2053 pprintf(p, "Moretime has a maximum limit of 600 seconds.\n");
2057 if (game_globals.garray[g].white == p) {
2058 game_globals.garray[g].bTime += increment * 10;
2059 game_globals.garray[g].bRealTime += increment * 10 * 100;
2060 pprintf(p, "%d seconds were added to your opponents clock\n",
2062 pprintf_prompt(pp->opponent,
2063 "\nYour opponent has added %d seconds to your clock.\n",
2066 if (game_globals.garray[g].black == p) {
2067 game_globals.garray[g].wTime += increment * 10;;
2068 game_globals.garray[g].wRealTime += increment * 10 * 100;
2069 pprintf(p, "%d seconds were added to your opponents clock\n",
2071 pprintf_prompt(pp->opponent,
2072 "\nYour opponent has added %d seconds to your clock.\n",