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 resigns} %s", NameOfLoser, winSymbol);
149 strcpy(EndSymbol, "Res");
153 sprintf(tmp, "%s forfeits on time} %s", NameOfLoser, winSymbol);
154 strcpy(EndSymbol, "Fla");
158 sprintf(tmp, "Game drawn by stalemate} 1/2-1/2");
160 strcpy(EndSymbol, "Sta");
162 whiteResult = RESULT_DRAW;
165 sprintf(tmp, "Game drawn by mutual agreement} 1/2-1/2");
167 strcpy(EndSymbol, "Agr");
169 whiteResult = RESULT_DRAW;
172 sprintf(tmp, "Game drawn because both players ran out of time} 1/2-1/2");
174 strcpy(EndSymbol, "Fla");
176 whiteResult = RESULT_DRAW;
179 sprintf(tmp, "Game drawn by repetition} 1/2-1/2");
181 strcpy(EndSymbol, "Rep");
183 whiteResult = RESULT_DRAW;
186 sprintf(tmp, "Game drawn by the 50 move rule} 1/2-1/2");
188 strcpy(EndSymbol, "50");
190 whiteResult = RESULT_DRAW;
194 sprintf(tmp, "Bughouse game aborted.} *");
195 whiteResult = RESULT_ABORT;
197 sprintf(tmp, "Game adjourned by mutual agreement} *");
201 case END_LOSTCONNECTION:
202 sprintf(tmp, "%s lost connection; game ", NameOfWinner);
203 if (CheckPFlag(gg->white, PFLAG_REG)
204 && CheckPFlag(gg->black, PFLAG_REG)
206 sprintf(tmp, "adjourned} *");
209 sprintf(tmp, "aborted} *");
210 whiteResult = RESULT_ABORT;
213 sprintf(tmp, "Game aborted by mutual agreement} *");
214 whiteResult = RESULT_ABORT;
217 sprintf(tmp, "Game courtesyaborted by %s} *", NameOfWinner);
218 whiteResult = RESULT_ABORT;
220 case END_COURTESYADJOURN:
222 sprintf(tmp, "Bughouse game courtesyaborted by %s.} *", NameOfWinner);
223 whiteResult = RESULT_ABORT;
225 sprintf(tmp, "Game courtesyadjourned by %s} *", NameOfWinner);
230 /* Draw by insufficient material (e.g., lone K vs. lone K) */
231 sprintf(tmp, "Neither player has mating material} 1/2-1/2");
233 strcpy(EndSymbol, "NM ");
235 whiteResult = RESULT_DRAW;
237 case END_FLAGNOMATERIAL:
238 sprintf(tmp, "%s ran out of time and %s has no material to mate} 1/2-1/2",
239 NameOfLoser, NameOfWinner);
241 strcpy(EndSymbol, "TM ");
243 whiteResult = RESULT_DRAW;
246 sprintf(tmp, "%s wins by adjudication} %s", NameOfWinner, winSymbol);
247 strcpy(EndSymbol, "Adj");
251 sprintf(tmp, "Game drawn by adjudication} 1/2-1/2");
253 strcpy(EndSymbol, "Adj");
255 whiteResult = RESULT_DRAW;
258 sprintf(tmp, "Game aborted by adjudication} *");
259 whiteResult = RESULT_ABORT;
262 sprintf(tmp, "Hmm, the game ended and I don't know why} *");
267 if (CheckPFlag(gg->white, PFLAG_TOURNEY) &&
268 CheckPFlag(gg->black, PFLAG_TOURNEY)) {
269 /* mamer wants more info */
270 sprintf(tmp," [%d %d %d %d %d]",
271 gg->wInitTime/(60*10), gg->wIncrement/10, gg->rated, gg->private, (int)gg->type);
275 strcat(outstr, "\n");
277 if (gg->rated && rate_change && gg->type != TYPE_BUGHOUSE)
278 /* Adjust ratings; bughouse gets done later. */
279 rating_update(g, -1);
283 int avail_printed = 0;
285 pprintf_noformat(gg->white, outstr);
286 pprintf_noformat(gg->black, outstr);
290 gg->link = -1; /*IanO: avoids recursion */
291 if (gl >= 0 && game_globals.garray[gl].link >= 0) {
292 pprintf_noformat(game_globals.garray[gl].white, outstr);
293 pprintf_noformat(game_globals.garray[gl].black, outstr);
294 if (CheckPFlag(game_globals.garray[gl].white, PFLAG_OPEN)) {
295 getavailmess (game_globals.garray[gl].white, avail_bugwhite);
298 if (CheckPFlag(game_globals.garray[gl].black, PFLAG_OPEN)) {
299 getavailmess (game_globals.garray[gl].black, avail_bugblack);
302 if ((gg->rated) && (rate_change)) {
304 rating_update(g, gl);
306 game_ended(gl, CToggle(winner), why);
309 if ((player_num_active_boards(gg->white) <= 1) /* not a simul or */
310 && CheckPFlag(gg->white, PFLAG_OPEN)) { /* simul is over? */
311 getavailmess (gg->white,avail_white);
313 } else { /* Part of an ongoing simul! Let's shrink the array. */
317 if (CheckPFlag(gg->black, PFLAG_OPEN)) {
318 getavailmess (gg->black,avail_black);
322 for (p = 0; p < player_globals.p_num; p++) {
323 struct player *pp = &player_globals.parray[p];
324 if ((p == gg->white) || (p == gg->black))
326 if (pp->status != PLAYER_PROMPT)
329 if (CheckPFlag(p, PFLAG_GIN) || player_is_observe(p, g)) {
330 pprintf_noformat(p, outstr);
334 if (CheckPFlag(p, PFLAG_AVAIL) && (CheckPFlag(p, PFLAG_OPEN)) && (pp->game < 0) && (print_avail)) {
335 if (((player_globals.parray[gg->white].b_stats.rating <= pp->availmax) && (player_globals.parray[gg->white].b_stats.rating >= pp->availmin)) || (!pp->availmax)) {
336 pprintf (p,"\n%s",avail_white);
339 if (((player_globals.parray[gg->black].b_stats.rating <= pp->availmax) && (player_globals.parray[gg->black].b_stats.rating >= pp->availmin)) || (!pp->availmax)) {
340 pprintf (p,"\n%s",avail_black);
343 if (gl == -1) /* bughouse ? */ {
344 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)) {
345 pprintf (p,"\n%s",avail_bugwhite);
348 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)) {
349 pprintf (p,"\n%s",avail_bugblack);
366 if (!(gg->rated && rate_change)) {
367 pprintf(gg->white, "No ratings adjustment done.\n");
368 pprintf(gg->black, "No ratings adjustment done.\n");
372 if (rate_change && gl < 0)
373 game_write_complete(g, isDraw, EndSymbol);
374 /* Mail off the moves */
375 if (CheckPFlag(gg->white, PFLAG_AUTOMAIL)) {
376 pcommand(gg->white, "mailmoves");
378 if (CheckPFlag(gg->black, PFLAG_AUTOMAIL)) {
379 pcommand(gg->black, "mailmoves");
381 if (!((player_globals.parray[gg->white].simul_info != NULL) &&
382 (player_globals.parray[gg->white].simul_info->numBoards))) {
383 player_globals.parray[gg->white].num_white++;
384 PFlagOFF(gg->white, PFLAG_LASTBLACK);
385 player_globals.parray[gg->black].num_black++;
386 PFlagON(gg->black, PFLAG_LASTBLACK);
388 player_globals.parray[gg->white].last_opponent =
389 strdup(gg->black_name);
390 player_globals.parray[gg->black].last_opponent =
391 strdup(gg->white_name);
393 player_globals.parray[gg->white].game = -1;
394 player_globals.parray[gg->black].game = -1;
395 player_globals.parray[gg->white].opponent = -1;
396 player_globals.parray[gg->black].opponent = -1;
397 if (gg->white != command_globals.commanding_player)
398 send_prompt(gg->white);
399 if (gg->black != command_globals.commanding_player)
400 send_prompt(gg->black);
401 if ((player_globals.parray[gg->white].simul_info != NULL) &&
402 (player_globals.parray[gg->white].simul_info->numBoards))
403 player_simul_over(gg->white, g, whiteResult);
408 static int was_promoted(struct game *g, int f, int r)
410 #define BUGHOUSE_PAWN_REVERT 1
411 #ifdef BUGHOUSE_PAWN_REVERT
414 for (i = g->numHalfMoves-2; i > 0; i -= 2) {
415 if (g->moveList[i].toFile == f && g->moveList[i].toRank == r) {
416 if (g->moveList[i].piecePromotionTo) {
417 switch(g->moveList[i].moveString[0]) { // [HGM] return original piece type rather than just TRUE
418 case 'P': return PAWN;
419 case 'N': return HONORABLEHORSE; // !!! this is Shogi, so no KNIGHT !!!
420 case 'B': return BISHOP;
421 case 'R': return ROOK;
422 case 'L': return LANCE;
423 case 'S': return SILVER;
424 default: return GOLD;
427 if (g->moveList[i].fromFile == ALG_DROP)
429 f = g->moveList[i].fromFile;
430 r = g->moveList[i].fromRank;
437 int pIsPlaying (int p)
439 struct player *pp = &player_globals.parray[p];
441 int p1 = pp->opponent;
443 if (g < 0 || game_globals.garray[g].status != GAME_ACTIVE) {
444 pprintf (p, "You are not playing a game.\n");
448 if (game_globals.garray[g].white != p && game_globals.garray[g].black != p) {
449 /* oh oh; big bad game bug. */
450 d_printf("BUG: Player %s playing game %d according to player_globals.parray,"
451 "\n but not according to game_globals.garray.\n", pp->name, g+1);
452 pprintf (p, "Disconnecting you from game number %d.\n", g+1);
454 if (p1 >= 0 && player_globals.parray[p1].game == g
455 && game_globals.garray[g].white != p1 && game_globals.garray[g].black != p1) {
456 pprintf (p1, "Disconnecting you from game number %d.\n", g+1);
457 player_globals.parray[p1].game = -1;
464 /* add clock increments */
465 static void game_add_increment(struct player *pp, struct game *gg)
467 /* no update on first move */
468 if (gg->game_state.moveNum == 1) return;
470 if (net_globals.con[pp->socket]->timeseal) { /* does he use timeseal? */
471 if (pp->side == WHITE) {
472 gg->wRealTime += gg->wIncrement * 100;
473 gg->wTime = gg->wRealTime / 100; /* remember to conv to
475 } else if (pp->side == BLACK) {
476 gg->bRealTime += gg->bIncrement * 100; /* conv to ms */
477 gg->bTime = gg->bRealTime / 100; /* remember to conv to
481 if (gg->game_state.onMove == BLACK) {
482 gg->bTime += gg->bIncrement;
484 if (gg->game_state.onMove == WHITE) {
485 gg->wTime += gg->wIncrement;
490 /* updates clocks for a game with timeseal */
491 void timeseal_update_clocks(struct player *pp, struct game *gg)
493 /* no update on first move */
494 if (gg->game_state.moveNum == 1) return;
496 if (pp->side == WHITE) {
497 gg->wLastRealTime = gg->wRealTime;
498 gg->wTimeWhenMoved = net_globals.con[pp->socket]->time;
499 if (((gg->wTimeWhenMoved - gg->wTimeWhenReceivedMove) < 0) ||
500 (gg->wTimeWhenReceivedMove == 0)) {
501 /* might seem weird - but could be caused by a person moving BEFORE
502 he receives the board pos (this is possible due to lag) but it's
503 safe to say he moved in 0 secs :-) */
504 gg->wTimeWhenReceivedMove = gg->wTimeWhenMoved;
506 gg->wRealTime -= gg->wTimeWhenMoved - gg->wTimeWhenReceivedMove;
508 } else if (pp->side == BLACK) {
509 gg->bLastRealTime = gg->bRealTime;
510 gg->bTimeWhenMoved = net_globals.con[pp->socket]->time;
511 if (((gg->bTimeWhenMoved - gg->bTimeWhenReceivedMove) < 0) ||
512 (gg->bTimeWhenReceivedMove == 0)) {
513 /* might seem weird - but could be caused by a person moving BEFORE
514 he receives the board pos (this is possible due to lag) but it's
515 safe to say he moved in 0 secs :-) */
516 gg->bTimeWhenReceivedMove = gg->bTimeWhenMoved;
518 gg->bRealTime -= gg->bTimeWhenMoved - gg->bTimeWhenReceivedMove;
524 void process_move(int p, char *command)
526 struct player *pp = &player_globals.parray[p];
528 int g, result, len, i;
533 pprintf(p, "You are not playing or examining a game.\n");
536 decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
539 gg = &game_globals.garray[g];
541 if (gg->status == GAME_SETUP) {
542 if (!attempt_drop(p,g,command)) {
543 pprintf(p, "You are still setting up the position.\n");
544 pprintf(p, "Type: 'setup done' when you are finished editing.\n");
550 if (gg->status != GAME_EXAMINE) {
551 if (!pIsPlaying(p)) return;
553 if (pp->side != gg->game_state.onMove) {
554 pprintf(p, "It is not your move.\n");
557 if (gg->clockStopped) {
558 pprintf(p, "Game clock is paused, use \"unpause\" to resume.\n");
562 pp->promote = NOPIECE; // [HGM] this seemed to be uninitialized, which caused spurious promotion in Shogi
563 if ((len = strlen(command)) > 1) {
564 if (command[len - 2] == '=') {
565 printf("promo '%s'\n", command);
566 switch (tolower(command[strlen(command) - 1])) {
568 pp->promote = KNIGHT;
571 pp->promote = BISHOP;
577 pp->promote = CARDINAL;
580 pp->promote = MARSHALL;
592 // Superchess promotions
594 pp->promote = EMPRESS;
597 pp->promote = PRINCESS;
600 pp->promote = CENTAUR;
606 pp->promote = SQUIRREL;
609 pp->promote = MASTODON;
613 pp->promote = DRAGONHORSE;
616 pp->promote = DRAGONKING;
623 pp->promote = NOPIECE;
626 pprintf(p, "Don't understand that move.\n");
633 switch (parse_move(command, &gg->game_state, &move, pp->promote)) {
635 pprintf(p, "Illegal move.\n");
639 pprintf(p, "Ambiguous move.\n");
646 if (gg->status == GAME_EXAMINE) {
648 if (gg->numHalfMoves > gg->examMoveListSize) {
649 gg->examMoveListSize += 20; /* Allocate 20 moves at a time */
650 gg->examMoveList = (struct move_t *) realloc(gg->examMoveList, sizeof(struct move_t) * gg->examMoveListSize);
652 result = execute_move(&gg->game_state, &move, 1);
655 MakeFENpos(g, move.FENpos);
656 gg->examMoveList[gg->numHalfMoves - 1] = move;
658 if (gg->game_state.onMove == WHITE) {
659 gg->wTime += (gg->lastDecTime - gg->lastMoveTime);
661 gg->bTime += (gg->lastDecTime - gg->lastMoveTime);
664 if (gg->numHalfMoves == 0)
665 gg->timeOfStart = now;
666 gg->lastMoveTime = now;
667 gg->lastDecTime = now;
669 } else { /* real game */
671 if ((player_globals.parray[i].simul_info != NULL) && (player_globals.parray[i].simul_info->numBoards &&
672 (player_globals.parray[i].simul_info->boards[player_globals.parray[i].simul_info->onBoard] != g))) {
673 pprintf(p, "It isn't your turn: wait until the simul giver is at your board.\n");
676 if (net_globals.con[pp->socket]->timeseal) { /* does he use timeseal? */
677 timeseal_update_clocks(pp, &game_globals.garray[g]);
679 /* we need to reset the opp's time for receiving the board since the
680 timeseal decoder only alters the time if it's 0 Otherwise the time
681 would be changed if the player did a refresh which would screw up
683 if (pp->side == WHITE) {
684 gg->bTimeWhenReceivedMove = 0;
686 gg->wTimeWhenReceivedMove = 0;
690 game_add_increment(pp, gg);
694 if (gg->numHalfMoves > gg->moveListSize) {
695 gg->moveListSize += 20; /* Allocate 20 moves at a time */
696 gg->moveList = (struct move_t *) realloc(gg->moveList, sizeof(struct move_t) * gg->moveListSize);
698 result = execute_move(&gg->game_state, &move, 1);
699 if (result == MOVE_OK && (gg->link >= 0 || gg->game_state.holdings) && move.pieceCaptured != NOPIECE) {
700 /* transfer captured piece to partner */
701 /* check if piece reverts to a pawn */
702 int victim = move.pieceCaptured, partner = gg->link, demoted;
703 // [HGM] zh: if not Bughouse, the game_state.holdings field decides what happens
705 partner = g; // pieces stay with current board
706 if(gg->game_state.holdings == -1) victim ^= WHITE|BLACK; // flip color
708 if (demoted = was_promoted(&game_globals.garray[g], move.toFile, move.toRank))
709 update_holding(partner, colorval(victim) | demoted); // [HGM] was_promoted now returns original piece type
711 update_holding(partner, victim);
715 if (gg->numHalfMoves > 1) {
716 move.tookTime = move.atTime - gg->lastMoveTime;
718 move.tookTime = move.atTime - gg->startTime;
720 gg->lastMoveTime = now;
721 gg->lastDecTime = now;
722 move.wTime = gg->wTime;
723 move.bTime = gg->bTime;
725 if (net_globals.con[pp->socket]->timeseal) { /* does he use timeseal? */
726 if (pp->side == WHITE) {
727 move.tookTime = (game_globals.garray[pp->game].wTimeWhenMoved -
728 game_globals.garray[pp->game].wTimeWhenReceivedMove) / 100;
730 move.tookTime = (game_globals.garray[pp->game].bTimeWhenMoved -
731 game_globals.garray[pp->game].bTimeWhenReceivedMove) / 100;
735 if (gg->numHalfMoves <= 2) {
739 MakeFENpos(g, move.FENpos);
740 gg->moveList[gg->numHalfMoves - 1] = move;
745 if (result == MOVE_ILLEGAL) {
746 pprintf(p, "Internal error, illegal move accepted!\n");
748 if ((result == MOVE_OK) && (gg->status == GAME_EXAMINE)) {
751 for (p1 = 0; p1 < player_globals.p_num; p1++) {
752 if (player_globals.parray[p1].status != PLAYER_PROMPT)
754 if (player_is_observe(p1, g) || player_globals.parray[p1].game == g) {
755 pprintf(p1, "%s moves: %s\n", pp->name, move.algString);
759 if (result == MOVE_CHECKMATE) {
760 if (gg->status == GAME_EXAMINE) {
763 for (p1 = 0; p1 < player_globals.p_num; p1++) {
764 if (player_globals.parray[p1].status != PLAYER_PROMPT)
766 if (player_is_observe(p1, g) || player_globals.parray[p1].game == g) {
767 pprintf(p1, "%s has been checkmated.\n",
768 (CToggle(gg->game_state.onMove) == BLACK) ? "White" : "Black");
772 game_ended(g, CToggle(gg->game_state.onMove), END_CHECKMATE);
775 if (result == MOVE_STALEMATE) {
776 if (gg->status == GAME_EXAMINE) {
779 for (p1 = 0; p1 < player_globals.p_num; p1++) {
780 if (player_globals.parray[p1].status != PLAYER_PROMPT)
782 if (player_is_observe(p1, g) || player_globals.parray[p1].game == g) {
783 pprintf(p1, "Stalemate.\n");
787 game_ended(g, CToggle(gg->game_state.onMove), END_STALEMATE);
790 if (result == MOVE_NOMATERIAL) {
791 if (gg->status == GAME_EXAMINE) {
794 for (p1 = 0; p1 < player_globals.p_num; p1++) {
795 if (player_globals.parray[p1].status != PLAYER_PROMPT)
797 if (player_is_observe(p1, g) || player_globals.parray[p1].game == g) {
798 pprintf(p1, "No mating material.\n");
802 game_ended(g, CToggle(gg->game_state.onMove), END_NOMATERIAL);
805 if (result == MOVE_BARE) {
806 if (gg->status == GAME_EXAMINE) {
809 for (p1 = 0; p1 < player_globals.p_num; p1++) {
810 if (player_globals.parray[p1].status != PLAYER_PROMPT)
812 if (player_is_observe(p1, g) || player_globals.parray[p1].game == g) {
813 pprintf(p1, "%s bared.\n",
814 (gg->game_state.onMove == BLACK) ? "White" : "Black");
818 game_ended(g, gg->game_state.onMove, END_BARE);
823 int com_resign(int p, param_list param)
825 struct player *pp = &player_globals.parray[p];
826 int g, o, oconnected;
828 if (param[0].type == TYPE_NULL) {
833 decline_withdraw_offers(p, -1, -1, DO_DECLINE);
834 game_ended(g, (game_globals.garray[g].white == p) ? BLACK : WHITE, END_RESIGN);
836 } else if (FindPlayer(p, param[0].val.word, &o, &oconnected)) {
838 if (game_read(g, p, o) < 0) {
839 if (game_read(g, o, p) < 0) {
840 pprintf(p, "You have no stored game with %s\n", player_globals.parray[o].name);
845 game_globals.garray[g].white = o;
846 game_globals.garray[g].black = p;
849 game_globals.garray[g].white = p;
850 game_globals.garray[g].black = o;
852 pprintf(p, "You resign your stored game with %s\n", player_globals.parray[o].name);
853 pcommand(p, "message %s I have resigned our stored game \"%s vs. %s.\"",
854 player_globals.parray[o].name,
855 player_globals.parray[game_globals.garray[g].white].name,
856 player_globals.parray[game_globals.garray[g].black].name);
857 game_delete(game_globals.garray[g].white, game_globals.garray[g].black);
858 game_ended(g, (game_globals.garray[g].white == p) ? BLACK : WHITE, END_RESIGN);
865 static int Check50MoveRule (int p, int g)
867 int num_reversible = game_globals.garray[g].numHalfMoves;
869 if (game_globals.garray[g].game_state.lastIrreversable >= 0) {
870 num_reversible -= game_globals.garray[g].game_state.lastIrreversable;
872 if (num_reversible > 99) {
873 game_ended(g, (game_globals.garray[g].white == p) ? BLACK : WHITE, END_50MOVERULE);
879 static char *GetFENpos (int g, int half_move)
882 return game_globals.garray[g].FENstartPos;
883 else return game_globals.garray[g].moveList[half_move].FENpos;
886 static int CheckRepetition (int p, int g)
888 struct player *pp = &player_globals.parray[p];
889 struct pending* pend;
891 int flag1 = 1, flag2 = 1;
892 char *pos1 = GetFENpos (g, game_globals.garray[g].numHalfMoves - 1);
893 char *pos2 = GetFENpos (g, game_globals.garray[g].numHalfMoves);
896 if (game_globals.garray[g].numHalfMoves < 8) /* can't have three repeats any quicker. */
899 for (move_num = game_globals.garray[g].game_state.lastIrreversable - 1; // [HGM] FEN stored in moveList[numHalfMoves-1] !
900 move_num < game_globals.garray[g].numHalfMoves - 1; move_num++) {
901 pos = GetFENpos (g, move_num);
902 if (strlen(pos1) == strlen(pos) && !strcmp(pos1, pos))
904 if (strlen(pos2) == strlen(pos) && !strcmp(pos2, pos))
906 printf("%2d. %d-%d %s %s %s\n", move_num, flag1, flag2, pos1,pos2,pos);
908 if (flag1 >= 3 || flag2 >= 3) {
909 if ((pend = find_pend(pp->opponent, p, PEND_DRAW)) != NULL) {
910 delete_pending(pend);
911 decline_withdraw_offers(p, -1, -1,DO_DECLINE);
913 game_ended(g, (game_globals.garray[g].white == p) ? BLACK : WHITE, END_REPETITION);
919 int com_draw(int p, param_list param)
921 struct player *pp = &player_globals.parray[p];
922 struct pending* pend;
923 int p1, g = pp->game;
925 if (!pIsPlaying(p)) {
928 if (Check50MoveRule (p, g) || CheckRepetition(p, g)) {
933 if ((player_globals.parray[p1].simul_info != NULL) && (player_globals.parray[p1].simul_info->numBoards &&
934 player_globals.parray[p1].simul_info->boards[player_globals.parray[p1].simul_info->onBoard] != g)) {
935 pprintf(p, "You can only make requests when the simul player is at your board.\n");
939 if ((pend = (find_pend(pp->opponent, p, PEND_DRAW))) != NULL) {
940 delete_pending(pend);
941 decline_withdraw_offers(p, -1, -1,DO_DECLINE);
942 game_ended(g, (game_globals.garray[g].white == p) ? BLACK : WHITE, END_AGREEDDRAW);
944 pprintf(pp->opponent, "\n");
945 pprintf_highlight(pp->opponent, "%s", pp->name);
946 pprintf_prompt(pp->opponent, " offers you a draw.\n");
947 pprintf(p, "Draw request sent.\n");
948 add_request(p, pp->opponent, PEND_DRAW);
953 int com_pause(int p, param_list param)
955 struct player *pp = &player_globals.parray[p];
957 struct pending* pend;
959 if (!pIsPlaying(p)) {
963 if (game_globals.garray[g].wTime == 0) {
964 pprintf(p, "You can't pause untimed games.\n");
967 if (game_globals.garray[g].clockStopped) {
968 pprintf(p, "Game is already paused, use \"unpause\" to resume.\n");
971 if ((pend = find_pend(pp->opponent, p, PEND_PAUSE)) != NULL) {
972 delete_pending(pend);
973 game_globals.garray[g].clockStopped = 1;
974 /* Roll back the time */
975 if (game_globals.garray[g].game_state.onMove == WHITE) {
976 game_globals.garray[g].wTime += (game_globals.garray[g].lastDecTime - game_globals.garray[g].lastMoveTime);
978 game_globals.garray[g].bTime += (game_globals.garray[g].lastDecTime - game_globals.garray[g].lastMoveTime);
981 if (game_globals.garray[g].numHalfMoves == 0)
982 game_globals.garray[g].timeOfStart = now;
983 game_globals.garray[g].lastMoveTime = now;
984 game_globals.garray[g].lastDecTime = now;
986 pprintf_prompt(pp->opponent, "\n%s accepted pause. Game clock paused.\n",
988 pprintf(p, "Game clock paused.\n");
990 pprintf(pp->opponent, "\n");
991 pprintf_highlight(pp->opponent, "%s", pp->name);
992 pprintf_prompt(pp->opponent, " requests to pause the game.\n");
993 pprintf(p, "Pause request sent.\n");
994 add_request(p, pp->opponent, PEND_PAUSE);
999 int com_unpause(int p, param_list param)
1001 struct player *pp = &player_globals.parray[p];
1004 struct pending* pend;
1006 if (!pIsPlaying(p)) {
1012 if (!game_globals.garray[g].clockStopped) {
1013 pprintf(p, "Game is not paused.\n");
1016 if ((pend = find_pend(pp->opponent, p, PEND_UNPAUSE)) != NULL) {
1017 delete_pending(pend);
1018 game_globals.garray[g].clockStopped = 0;
1020 if (game_globals.garray[g].numHalfMoves == 0)
1021 game_globals.garray[g].timeOfStart = now;
1022 game_globals.garray[g].lastMoveTime = now;
1023 game_globals.garray[g].lastDecTime = now;
1025 pprintf(p, "Game clock resumed.\n");
1026 pprintf_prompt(pp->opponent, "\nGame clock resumed.\n");
1028 pprintf(pp->opponent, "\n");
1029 pprintf_highlight(pp->opponent, "%s", pp->name);
1030 pprintf_prompt(pp->opponent, " requests to unpause the game.\n");
1031 pprintf(p, "Unpause request sent.\n");
1032 add_request(p, pp->opponent, PEND_UNPAUSE);
1037 int com_abort(int p, param_list param)
1039 struct player *pp = &player_globals.parray[p];
1040 struct pending* pend;
1041 int p1, g, myColor, yourColor, myGTime, yourGTime;
1049 if (p == game_globals.garray[g].white) {
1052 myGTime = game_globals.garray[g].wTime;
1053 yourGTime = game_globals.garray[g].bTime;
1057 myGTime = game_globals.garray[g].bTime;
1058 yourGTime = game_globals.garray[g].wTime;
1060 if ((player_globals.parray[p1].simul_info != NULL) &&
1061 (player_globals.parray[p1].simul_info->numBoards &&
1062 player_globals.parray[p1].simul_info->boards[player_globals.parray[p1].simul_info->onBoard] != g)) {
1063 pprintf(p, "You can only make requests when the simul player is at your board.\n");
1066 if ((pend = find_pend(p1, p, PEND_ABORT)) != NULL) {
1067 delete_pending(pend);
1068 decline_withdraw_offers(p, -1, -1,DO_DECLINE);
1069 game_ended(g, yourColor, END_ABORT);
1071 game_update_time(g);
1073 if (net_globals.con[pp->socket]->timeseal
1074 && game_globals.garray[g].game_state.onMove == myColor
1075 && game_globals.garray[g].flag_pending == FLAG_ABORT) {
1076 /* It's my move, opponent has asked for abort; I lagged out,
1077 my timeseal prevented courtesyabort, and I am sending an abort
1078 request before acknowledging (and processing) my opponent's
1079 courtesyabort. OK, let's abort already :-). */
1080 decline_withdraw_offers(p, -1, -1,DO_DECLINE);
1081 game_ended(g, yourColor, END_ABORT);
1084 if (net_globals.con[player_globals.parray[p1].socket]->timeseal) { /* opp uses timeseal? */
1086 int yourRealTime = (myColor == WHITE ? game_globals.garray[g].bRealTime
1087 : game_globals.garray[g].wRealTime);
1088 if (myGTime > 0 && yourGTime <= 0 && yourRealTime > 0) {
1089 /* Override courtesyabort; opponent still has time. Check for lag. */
1092 if (game_globals.garray[g].game_state.onMove != myColor
1093 && game_globals.garray[g].flag_pending != FLAG_CHECKING) {
1094 /* Opponent may be lagging; let's ask. */
1095 game_globals.garray[g].flag_pending = FLAG_ABORT;
1096 game_globals.garray[g].flag_check_time = time(0);
1097 pprintf(p, "Opponent has timeseal; trying to courtesyabort.\n");
1098 pprintf(p1, "\n[G]\n");
1104 if (myGTime > 0 && yourGTime <= 0 && courtesyOK) {
1105 /* player wants to abort + opponent is out of time = courtesyabort */
1106 pprintf(p, "Since you have time, and your opponent has none, the game has been aborted.");
1107 pprintf(p1, "Your opponent has aborted the game rather than calling your flag.");
1108 decline_withdraw_offers(p, -1, -1, DO_DECLINE);
1109 game_ended(g, myColor, END_COURTESY);
1112 pprintf_highlight(p1, "%s", pp->name);
1113 pprintf(p1, " would like to abort the game; ");
1114 pprintf_prompt(p1, "type \"abort\" to accept.\n");
1115 pprintf(p, "Abort request sent.\n");
1116 add_request(p, p1, PEND_ABORT);
1122 static int player_has_mating_material(struct game_state_t *gs, int color)
1126 int minor_pieces = 0;
1128 for (i = 0; i < gs->files; i++)
1129 for (j = 0; j < gs->ranks; j++) {
1130 piece = gs->board[i][j];
1131 switch (piecetype(piece)) {
1134 if (iscolor(piece, color))
1141 if (iscolor(piece, color))
1145 return ((minor_pieces > 1) ? 1 : 0);
1148 int com_flag(int p, param_list param)
1150 struct player *pp = &player_globals.parray[p];
1155 if (!pIsPlaying(p)) {
1160 gg = &game_globals.garray[g];
1162 myColor = (p == gg->white ? WHITE : BLACK);
1163 if (gg->type == TYPE_UNTIMED) {
1164 pprintf(p, "You can't flag an untimed game.\n");
1167 if (gg->numHalfMoves < 2) {
1168 pprintf(p, "You cannot flag before both players have moved.\nUse abort instead.\n");
1171 game_update_time(g);
1174 int myTime, yourTime, opp = pp->opponent, serverTime;
1176 if (net_globals.con[pp->socket]->timeseal) { /* does caller use timeseal? */
1177 myTime = (myColor==WHITE?gg->wRealTime:gg->bRealTime);
1179 myTime = (myColor == WHITE?gg->wTime:gg->bTime);
1181 serverTime = (myColor == WHITE?gg->bTime:gg->wTime);
1183 if (net_globals.con[player_globals.parray[opp].socket]->timeseal) { /* opp uses timeseal? */
1184 yourTime = (myColor == WHITE?gg->bRealTime:gg->wRealTime);
1186 yourTime = serverTime;
1189 /* the clocks to compare are now in myTime and yourTime */
1190 if ((myTime <= 0) && (yourTime <= 0)) {
1191 decline_withdraw_offers(p, -1, -1,DO_DECLINE);
1192 game_ended(g, myColor, END_BOTHFLAG);
1197 /* Opponent still has time, but if that's only because s/he
1198 * may be lagging, we should ask for an acknowledgement and then
1199 * try to call the flag. */
1201 if (serverTime <= 0 && gg->game_state.onMove != myColor
1202 && gg->flag_pending != FLAG_CHECKING) {
1203 /* server time thinks opponent is down, but RealTIme disagrees.
1204 * ask client to acknowledge it's alive. */
1205 gg->flag_pending = FLAG_CALLED;
1206 gg->flag_check_time = time(0);
1207 pprintf(p, "Opponent has timeseal; checking if (s)he's lagging.\n");
1208 pprintf (opp, "\n[G]\n");
1212 /* if we're here, it means one of:
1213 * 1. the server agrees opponent has time, whether lagging or not.
1214 * 2. opp. has timeseal (if yourTime != serverTime), had time left
1215 * after the last move (yourTime > 0), and it's still your move.
1216 * 3. we're currently checking a flag call after having receiving
1217 * acknowledgement from the other timeseal (and would have reset
1218 * yourTime if the flag were down). */
1220 pprintf(p, "Your opponent is not out of time!\n");
1225 decline_withdraw_offers(p, -1, -1,DO_DECLINE);
1226 if (player_has_mating_material(&gg->game_state, myColor))
1227 game_ended(g, myColor, END_FLAG);
1229 game_ended(g, myColor, END_FLAGNOMATERIAL);
1233 int com_adjourn(int p, param_list param)
1235 struct player *pp = &player_globals.parray[p];
1236 struct pending* pend;
1237 int p1, g, myColor, yourColor;
1244 if (!CheckPFlag(p, PFLAG_REG) || !CheckPFlag(p, PFLAG_REG)) {
1245 pprintf(p, "Both players must be registered to adjourn a game. Use \"abort\".\n");
1248 if (game_globals.garray[g].link >= 0) {
1249 pprintf(p, "Bughouse games cannot be adjourned.\n");
1252 myColor = (p == game_globals.garray[g].white ? WHITE : BLACK);
1253 yourColor = (myColor == WHITE ? BLACK : WHITE);
1255 if ((pend = find_pend(p1, p, PEND_ADJOURN)) != NULL) {
1256 delete_pending(pend);
1257 decline_withdraw_offers(p, -1, -1,DO_DECLINE);
1258 game_ended(pp->game, yourColor, END_ADJOURN);
1260 game_update_time(g);
1261 if (((myColor == WHITE) && (game_globals.garray[g].wTime > 0) && (game_globals.garray[g].bTime <= 0))
1262 || ((myColor == BLACK) && (game_globals.garray[g].bTime > 0) && (game_globals.garray[g].wTime <= 0))) {
1263 /* player wants to adjourn + opponent is out of time = courtesyadjourn */
1264 pprintf(p, "Since you have time, and your opponent has none, the game has been adjourned.");
1265 pprintf(p1, "Your opponent has adjourned the game rather than calling your flag.");
1266 decline_withdraw_offers(p, -1, -1,DO_DECLINE);
1267 game_ended(g, myColor, END_COURTESYADJOURN);
1270 pprintf_highlight(p1, "%s", pp->name);
1271 pprintf(p1, " would like to adjourn the game; ");
1272 pprintf_prompt(p1, "type \"adjourn\" to accept.\n");
1273 pprintf(p, "Adjourn request sent.\n");
1274 add_request(p, p1, PEND_ADJOURN);
1280 int com_takeback(int p, param_list param)
1282 struct player *pp = &player_globals.parray[p];
1283 int nHalfMoves = 1, g, i, p1, pend_half_moves;
1284 struct pending* from;
1290 if ((player_globals.parray[p1].simul_info != NULL) &&
1291 (player_globals.parray[p1].simul_info->numBoards &&
1292 player_globals.parray[p1].simul_info->boards[player_globals.parray[p1].simul_info->onBoard] !=
1294 pprintf(p, "You can only make requests when the simul player is at your board.\n");
1299 if (game_globals.garray[g].link >= 0) {
1300 pprintf(p, "Takeback not implemented for bughouse games yet.\n");
1303 if (param[0].type == TYPE_INT) {
1304 nHalfMoves = param[0].val.integer;
1305 if (nHalfMoves <= 0) {
1306 pprintf (p,"You can't takeback less than 1 move.\n");
1310 if ((from = find_pend(pp->opponent, p, PEND_TAKEBACK)) != NULL) {
1311 pend_half_moves = from->wtime;
1312 delete_pending(from);
1313 if (pend_half_moves == nHalfMoves) {
1314 /* Doing the takeback */
1315 decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1316 for (i = 0; i < nHalfMoves; i++) {
1317 if (backup_move(g, REL_GAME) != MOVE_OK) {
1318 pprintf(game_globals.garray[g].white, "Can only backup %d moves\n", i);
1319 pprintf(game_globals.garray[g].black, "Can only backup %d moves\n", i);
1324 game_globals.garray[g].wTimeWhenReceivedMove = 0;
1325 game_globals.garray[g].bTimeWhenReceivedMove = 0;
1330 if (!game_globals.garray[g].numHalfMoves) {
1331 pprintf(p, "There are no moves in your game.\n");
1332 pprintf_prompt(pp->opponent, "\n%s has declined the takeback request.\n",
1337 if (game_globals.garray[g].numHalfMoves < nHalfMoves) {
1338 pprintf(p, "There are only %d half moves in your game.\n", game_globals.garray[g].numHalfMoves);
1339 pprintf_prompt(pp->opponent, "\n%s has declined the takeback request.\n",
1343 pprintf(p, "You disagree on the number of half-moves to takeback.\n");
1344 pprintf(p, "Alternate takeback request sent.\n");
1345 pprintf_prompt(pp->opponent, "\n%s proposes a different number (%d) of half-move(s).\n", pp->name, nHalfMoves);
1346 from = add_request(p, pp->opponent, PEND_TAKEBACK);
1347 from->wtime = nHalfMoves;
1351 if (!game_globals.garray[g].numHalfMoves) {
1352 pprintf(p, "There are no moves in your game.\n");
1355 if (game_globals.garray[g].numHalfMoves < nHalfMoves) {
1356 pprintf(p, "There are only %d half moves in your game.\n", game_globals.garray[g].numHalfMoves);
1359 pprintf(pp->opponent, "\n");
1360 pprintf_highlight(pp->opponent, "%s", pp->name);
1361 pprintf_prompt(pp->opponent, " would like to take back %d half move(s).\n",
1363 pprintf(p, "Takeback request sent.\n");
1364 from = add_request(p, pp->opponent, PEND_TAKEBACK);
1365 from->wtime = nHalfMoves;
1371 int com_switch(int p, param_list param)
1373 struct player *pp = &player_globals.parray[p];
1374 int g = pp->game, tmp, now, p1;
1376 struct pending* pend;
1382 if ((player_globals.parray[p1].simul_info != NULL) && (player_globals.parray[p1].simul_info->numBoards &&
1383 player_globals.parray[p1].simul_info->boards[player_globals.parray[p1].simul_info->onBoard] != g)) {
1384 pprintf(p, "You can only make requests when the simul player is at your board.\n");
1388 if (game_globals.garray[g].link >= 0) {
1389 pprintf(p, "Switch not implemented for bughouse games.\n");
1392 if ((pend = find_pend(pp->opponent, p, PEND_SWITCH)) != NULL) {
1393 delete_pending(pend);
1394 /* Doing the switch */
1395 decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1397 tmp = game_globals.garray[g].white;
1398 game_globals.garray[g].white = game_globals.garray[g].black;
1399 game_globals.garray[g].black = tmp;
1400 pp->side = (pp->side == WHITE) ? BLACK : WHITE;
1401 strTmp = strdup(game_globals.garray[g].white_name);
1402 strcpy(game_globals.garray[g].white_name, game_globals.garray[g].black_name);
1403 strcpy(game_globals.garray[g].black_name, strTmp);
1406 player_globals.parray[pp->opponent].side =
1407 (player_globals.parray[pp->opponent].side == WHITE) ? BLACK : WHITE;
1408 /* Roll back the time */
1409 if (game_globals.garray[g].game_state.onMove == WHITE) {
1410 game_globals.garray[g].wTime += (game_globals.garray[g].lastDecTime - game_globals.garray[g].lastMoveTime);
1412 game_globals.garray[g].bTime += (game_globals.garray[g].lastDecTime - game_globals.garray[g].lastMoveTime);
1415 if (game_globals.garray[g].numHalfMoves == 0)
1416 game_globals.garray[g].timeOfStart = now;
1417 game_globals.garray[g].lastMoveTime = now;
1418 game_globals.garray[g].lastDecTime = now;
1422 if (game_globals.garray[g].rated && game_globals.garray[g].numHalfMoves > 0) {
1423 pprintf(p, "You cannot switch sides once a rated game is underway.\n");
1426 pprintf(pp->opponent, "\n");
1427 pprintf_highlight(pp->opponent, "%s", pp->name);
1428 pprintf_prompt(pp->opponent, " would like to switch sides.\nType \"accept\" to switch sides, or \"decline\" to refuse.\n");
1429 pprintf(p, "Switch request sent.\n");
1430 add_request(p, pp->opponent, PEND_SWITCH);
1434 int com_time(int p, param_list param)
1436 struct player *pp = &player_globals.parray[p];
1439 if (param[0].type == TYPE_NULL) {
1444 g = GameNumFromParam(p, &p1, ¶m[0]);
1448 if ((g < 0) || (g >= game_globals.g_num) || (game_globals.garray[g].status != GAME_ACTIVE)) {
1449 pprintf(p, "There is no such game.\n");
1452 game_update_time(g);
1453 pprintf(p, "White (%s) : %d mins, %d secs\n",
1454 player_globals.parray[game_globals.garray[g].white].name,
1455 game_globals.garray[g].wTime / 600,
1456 (game_globals.garray[g].wTime - ((game_globals.garray[g].wTime / 600) * 600)) / 10);
1457 pprintf(p, "Black (%s) : %d mins, %d secs\n",
1458 player_globals.parray[game_globals.garray[g].black].name,
1459 game_globals.garray[g].bTime / 600,
1460 (game_globals.garray[g].bTime - ((game_globals.garray[g].bTime / 600) * 600)) / 10);
1464 int com_ptime(int p, param_list param)
1466 struct player *pp = &player_globals.parray[p];
1467 int retval, part = pp->partner;
1470 pprintf(p, "You do not have a partner.\n");
1473 retval = pcommand (p, "time %s", player_globals.parray[part].name);
1474 if (retval == COM_OK)
1475 return COM_OK_NOPROMPT;
1480 int com_boards(int p, param_list param)
1482 char *category = NULL;
1483 char dname[MAX_FILENAME_SIZE];
1487 if (param[0].type == TYPE_WORD)
1488 category = param[0].val.word;
1490 pprintf(p, "Boards Available For Category %s:\n", category);
1491 sprintf(dname, "%s/%s", BOARD_DIR, category);
1493 pprintf(p, "Categories Available:\n");
1494 sprintf(dname, "%s", BOARD_DIR);
1496 dirp = opendir(dname);
1498 pprintf(p, "No such category %s, try \"boards\".\n", category);
1502 /* YUK! what a mess, how about printing an ordered directory? - DAV*/
1504 for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
1505 if (!strcmp(dp->d_name, "."))
1507 if (!strcmp(dp->d_name, ".."))
1509 pprintf(p, "%s\n", dp->d_name);
1515 int com_simmatch(int p, param_list param)
1517 struct player *pp = &player_globals.parray[p];
1518 int p1, g, adjourned;
1521 struct pending* pend;
1523 char* category = NULL;
1524 char fname[MAX_FILENAME_SIZE];
1527 if (game_globals.garray[pp->game].status == GAME_EXAMINE) {
1528 pprintf(p, "You are still examining a game.\n");
1531 if (game_globals.garray[pp->game].status == GAME_SETUP) {
1532 pprintf(p, "You are still setting up a position.\n");
1536 p1 = player_find_part_login(param[0].val.word);
1538 pprintf(p, "No user named \"%s\" is logged in.\n", param[0].val.word);
1542 pprintf(p, "You can't simmatch yourself!\n");
1545 if ((pend = find_pend(p1, p, PEND_SIMUL)) != NULL) {
1547 /* Accepting Simul ! */
1549 if ((pp->simul_info != NULL) &&
1550 (pp->simul_info->numBoards >= MAX_SIMUL)) {
1551 pprintf(p, "You are already playing the maximum of %d boards.\n", MAX_SIMUL);
1552 pprintf(p1, "Simul request removed, boards filled.\n");
1553 delete_pending(pend);
1556 unobserveAll(p); /* stop observing when match starts */
1561 if (game_read(g, p, p1) >= 0) {
1563 delete_pending(pend);
1566 if (!adjourned) { /* no adjourned game, so begin a new game */
1568 if ((pend->category != NULL) && (pend->board_type != NULL)) {
1569 board = strdup(pend->category);
1570 category = strdup(pend->board_type);
1573 delete_pending(pend);
1575 if (create_new_match(g,p, p1, 0, 0, 0, 0, 0, ((board == NULL) ? "\0" : board), ((category == NULL) ? "\0" : category), 1,1) == COM_FAILED) {
1576 pprintf(p, "There was a problem creating the new match.\n");
1577 pprintf_prompt(p1, "There was a problem creating the new match.\n");
1580 if (board != NULL) {
1587 if (board != NULL) {
1592 } else { /* resume adjourned game */
1595 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]);
1599 game_globals.garray[g].white = p;
1600 game_globals.garray[g].black = p1;
1601 game_globals.garray[g].status = GAME_ACTIVE;
1602 game_globals.garray[g].startTime = tenth_secs();
1603 game_globals.garray[g].lastMoveTime = game_globals.garray[g].startTime;
1604 game_globals.garray[g].lastDecTime = game_globals.garray[g].startTime;
1608 player_globals.parray[p1].game = g;
1609 player_globals.parray[p1].opponent = p;
1610 player_globals.parray[p1].side = BLACK;
1614 if (pp->simul_info == NULL) {
1615 pp->simul_info = (struct simul_info_t *) malloc(sizeof(struct simul_info_t));
1616 pp->simul_info->numBoards = 0;
1617 pp->simul_info->onBoard = 0;
1618 pp->simul_info->num_wins = pp->simul_info->num_draws
1619 = pp->simul_info->num_losses = 0;
1621 num = pp->simul_info->numBoards;
1622 /* pp->simul_info->results[num] = -1; */
1623 pp->simul_info->boards[num] = pp->game;
1624 pp->simul_info->numBoards++;
1625 if (pp->simul_info->numBoards > 1 &&
1626 pp->simul_info->onBoard >= 0)
1627 player_goto_board(p, pp->simul_info->onBoard);
1629 pp->simul_info->onBoard = 0;
1632 if (find_pend(-1, p, PEND_SIMUL) != NULL) {
1633 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");
1636 if (pp->simul_info != NULL) {
1637 if (pp->simul_info->numBoards) {
1638 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");
1643 pprintf(p, "You are already playing a game.\n");
1646 if (!CheckPFlag(p1, PFLAG_SIMOPEN)) {
1647 pprintf_highlight(p, "%s", player_globals.parray[p1].name);
1648 pprintf(p, " is not open to receiving simul requests.\n");
1651 if ((player_globals.parray[p1].simul_info != NULL) && (player_globals.parray[p1].simul_info->numBoards >= MAX_SIMUL)) {
1652 pprintf_highlight(p, "%s", player_globals.parray[p1].name);
1653 pprintf(p, " is already playing the maximum of %d boards.\n", MAX_SIMUL);
1657 /* loon: checking for some crazy situations we can't allow :) */
1659 if ((player_globals.parray[p1].simul_info != NULL) && (player_globals.parray[p1].game >=0) && (player_globals.parray[p1].simul_info->numBoards == 0)) {
1660 pprintf_highlight(p, "%s", player_globals.parray[p1].name);
1661 if (player_globals.parray[game_globals.garray[player_globals.parray[p1].game].white].simul_info->numBoards) {
1662 pprintf(p, " is playing in ");
1663 pprintf_highlight(p, "%s", player_globals.parray[player_globals.parray[p1].opponent].name);
1664 pprintf(p, "'s simul, and can't accept.\n");
1666 pprintf(p, " can't begin a simul while playing a non-simul game.\n");
1671 g = game_new(); /* Check if an adjourned untimed game */
1672 adjourned = ((game_read(g, p, p1) < 0) && (game_read(g, p1, p) < 0)) ? 0 : 1;
1674 if (!(game_globals.garray[g].type == TYPE_UNTIMED))
1679 pend = add_request(p, p1, PEND_SIMUL);
1681 if ((param[1].type == TYPE_WORD) && (param[2].type == TYPE_WORD)) {
1683 sprintf(fname, "%s/%s/%s", BOARD_DIR, param[1].val.word , param[2].val.word);
1684 if (!file_exists(fname)) {
1685 pprintf(p, "No such category/board: %s/%s\n", param[1].val.word , param[2].val.word);
1688 pend->category = strdup(param[1].val.word);
1689 pend->board_type = strdup(param[2].val.word);
1691 pend->category = NULL;
1692 pend->board_type = NULL;
1696 pprintf_highlight(p1, "%s", pp->name);
1698 pprintf_prompt(p1, " requests to continue an adjourned simul game.\n");
1699 pprintf(p, "Request to resume simul sent. Adjourned game found.\n");
1701 if (pend->category == NULL)
1702 pprintf_prompt(p1, " requests to join a simul match with you.\n");
1704 pprintf_prompt(p1, " requests to join a %s %s simul match with you.\n",
1705 pend->category,pend->board_type);
1706 pprintf(p, "Simul match request sent.\n");
1711 int com_goboard(int p, param_list param)
1713 struct player *pp = &player_globals.parray[p];
1714 int on, g, p1, gamenum;
1716 if (pp->simul_info == NULL) {
1717 pprintf(p, "You are not giving a simul.\n");
1721 if (!pp->simul_info->numBoards) {
1722 pprintf(p, "You are not giving a simul.\n");
1726 if (param[0].type == TYPE_WORD) {
1728 p1 = player_find_part_login(param[0].val.word);
1730 pprintf(p, "No user named \"%s\" is logged in.\n", param[0].val.word);
1734 pprintf(p, "You can't goboard yourself!\n");
1738 gamenum = player_globals.parray[p1].game;
1740 pprintf (p,"%s is not playing a game.\n", player_globals.parray[p1].login);
1745 gamenum = param[0].val.integer - 1;
1750 on = pp->simul_info->onBoard;
1751 g = pp->simul_info->boards[on];
1753 pprintf(p, "You are already at that board!\n");
1756 if (pp->simul_info->numBoards > 1) {
1757 decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1758 if (player_goto_simulgame_bynum(p, gamenum) !=-1) {
1760 pprintf(game_globals.garray[g].black, "\n");
1761 pprintf_highlight(game_globals.garray[g].black, "%s", pp->name);
1762 pprintf_prompt(game_globals.garray[g].black, " has moved away from your board.\n");
1765 pprintf(p, "You are not playing that game/person.\n");
1767 pprintf(p, "You are only playing one board!\n");
1771 int com_simnext(int p, param_list param)
1773 struct player *pp = &player_globals.parray[p];
1776 if (pp->simul_info == NULL) {
1777 pprintf(p, "You are not giving a simul.\n");
1781 if (!pp->simul_info->numBoards) {
1782 pprintf(p, "You are not giving a simul.\n");
1786 if (pp->simul_info->numBoards > 1) {
1787 decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1788 on = pp->simul_info->onBoard;
1789 g = pp->simul_info->boards[on];
1791 pprintf(game_globals.garray[g].black, "\n");
1792 pprintf_highlight(game_globals.garray[g].black, "%s", pp->name);
1793 pprintf_prompt(game_globals.garray[g].black, " is moving away from your board.\n");
1794 player_goto_next_board(p);
1797 pprintf(p, "You are only playing one board!\n");
1801 int com_simprev(int p, param_list param)
1803 struct player *pp = &player_globals.parray[p];
1806 if (pp->simul_info == NULL) {
1807 pprintf(p, "You are not giving a simul.\n");
1811 if (!pp->simul_info->numBoards) {
1812 pprintf(p, "You are not giving a simul.\n");
1815 if (pp->simul_info->numBoards > 1) {
1816 decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1817 on = pp->simul_info->onBoard;
1818 g = pp->simul_info->boards[on];
1820 pprintf(game_globals.garray[g].black, "\n");
1821 pprintf_highlight(game_globals.garray[g].black, "%s", pp->name);
1822 pprintf_prompt(game_globals.garray[g].black, " is moving back to the previous board.\n");
1824 player_goto_prev_board(p);
1826 pprintf(p, "You are only playing one board!\n");
1830 int com_simgames(int p, param_list param)
1834 if (param[0].type == TYPE_WORD) {
1835 if ((p1 = player_find_part_login(param[0].val.word)) < 0) {
1836 pprintf(p, "No player named %s is logged in.\n", param[0].val.word);
1841 pprintf(p, "You are playing %d simultaneous games.\n",
1842 player_num_active_boards(p1));
1844 pprintf(p, "%s is playing %d simultaneous games.\n", player_globals.parray[p1].name,
1845 player_num_active_boards(p1));
1849 int com_simpass(int p, param_list param)
1851 struct player *pp = &player_globals.parray[p];
1858 p1 = game_globals.garray[g].white;
1860 if (player_globals.parray[p1].simul_info == NULL) {
1861 pprintf(p, "You are not participating in a simul.\n");
1865 if (!player_globals.parray[p1].simul_info->numBoards) {
1866 pprintf(p, "You are not participating in a simul.\n");
1870 pprintf(p, "You are the simul holder and cannot pass!\n");
1873 if (player_num_active_boards(p1) == 1) {
1874 pprintf(p, "This is the only game, so passing is futile.\n");
1877 on = player_globals.parray[p1].simul_info->onBoard;
1878 if (player_globals.parray[p1].simul_info->boards[on] != g) {
1879 pprintf(p, "You cannot pass until the simul holder arrives!\n");
1882 if (game_globals.garray[g].passes >= MAX_SIMPASS) {
1884 pprintf(p, "You have reached your maximum of %d pass(es).\n", MAX_SIMPASS);
1885 pprintf(p, "Please move IMMEDIATELY!\n");
1886 pprintf_highlight(p1, "%s", pp->name);
1887 pprintf_prompt(p1, " tried to pass, but is out of passes.\n");
1890 decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1892 game_globals.garray[g].passes++;
1893 pprintf(p, "You have passed and have %d pass(es) left.\n",
1894 (MAX_SIMPASS - game_globals.garray[g].passes));
1895 pprintf_highlight(p1, "%s", pp->name);
1896 pprintf_prompt(p1, " has decided to pass and has %d pass(es) left.\n",
1897 (MAX_SIMPASS - game_globals.garray[g].passes));
1898 player_goto_next_board(p1);
1902 int com_simabort(int p, param_list param)
1904 struct player *pp = &player_globals.parray[p];
1906 if (pp->simul_info == NULL) {
1907 pprintf(p, "You are not giving a simul.\n");
1911 if (!pp->simul_info->numBoards) {
1912 pprintf(p, "You are not giving a simul.\n");
1915 decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1916 game_ended(pp->simul_info->boards[pp->simul_info->onBoard],
1921 int com_simallabort(int p, param_list param)
1923 struct player *pp = &player_globals.parray[p];
1926 if (pp->simul_info == NULL) {
1927 pprintf(p, "You are not giving a simul.\n");
1931 if (!pp->simul_info->numBoards) {
1932 pprintf(p, "You are not giving a simul.\n");
1936 decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1937 for (i = 0; i < pp->simul_info->numBoards; i++)
1938 if (pp->simul_info->boards[i] >= 0)
1939 game_ended(pp->simul_info->boards[i],
1945 int com_simadjourn(int p, param_list param)
1947 struct player *pp = &player_globals.parray[p];
1949 if (pp->simul_info == NULL) {
1950 pprintf(p, "You are not giving a simul.\n");
1954 if (!pp->simul_info->numBoards) {
1955 pprintf(p, "You are not giving a simul.\n");
1958 decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1959 game_ended(pp->simul_info->boards[pp->simul_info->onBoard],
1960 WHITE, END_ADJOURN);
1964 int com_simalladjourn(int p, param_list param)
1966 struct player *pp = &player_globals.parray[p];
1969 if (pp->simul_info == NULL) {
1970 pprintf(p, "You are not giving a simul.\n");
1974 if (!pp->simul_info->numBoards) {
1975 pprintf(p, "You are not giving a simul.\n");
1978 decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1979 for (i = 0; i < pp->simul_info->numBoards; i++)
1980 if (pp->simul_info->boards[i] >= 0)
1981 game_ended(pp->simul_info->boards[i],
1982 WHITE, END_ADJOURN);
1987 int com_moretime(int p, param_list param)
1989 struct player *pp = &player_globals.parray[p];
1992 if ((pp->game >=0) &&((game_globals.garray[pp->game].status == GAME_EXAMINE) ||
1993 (game_globals.garray[pp->game].status == GAME_SETUP))) {
1994 pprintf(p, "You cannot use moretime in an examined game.\n");
1997 increment = param[0].val.integer;
1998 if (increment <= 0) {
1999 pprintf(p, "Moretime requires an integer value greater than zero.\n");
2005 if (increment > 600) {
2006 pprintf(p, "Moretime has a maximum limit of 600 seconds.\n");
2010 if (game_globals.garray[g].white == p) {
2011 game_globals.garray[g].bTime += increment * 10;
2012 game_globals.garray[g].bRealTime += increment * 10 * 100;
2013 pprintf(p, "%d seconds were added to your opponents clock\n",
2015 pprintf_prompt(pp->opponent,
2016 "\nYour opponent has added %d seconds to your clock.\n",
2019 if (game_globals.garray[g].black == p) {
2020 game_globals.garray[g].wTime += increment * 10;;
2021 game_globals.garray[g].wRealTime += increment * 10 * 100;
2022 pprintf(p, "%d seconds were added to your opponents clock\n",
2024 pprintf_prompt(pp->opponent,
2025 "\nYour opponent has added %d seconds to your clock.\n",