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 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;
617 pp->promote = NOPIECE;
620 pprintf(p, "Don't understand that move.\n");
627 switch (parse_move(command, &gg->game_state, &move, pp->promote)) {
629 pprintf(p, "Illegal move.\n");
633 pprintf(p, "Ambiguous move.\n");
640 if (gg->status == GAME_EXAMINE) {
642 if (gg->numHalfMoves > gg->examMoveListSize) {
643 gg->examMoveListSize += 20; /* Allocate 20 moves at a time */
644 gg->examMoveList = (struct move_t *) realloc(gg->examMoveList, sizeof(struct move_t) * gg->examMoveListSize);
646 result = execute_move(&gg->game_state, &move, 1);
649 MakeFENpos(g, move.FENpos);
650 gg->examMoveList[gg->numHalfMoves - 1] = move;
652 if (gg->game_state.onMove == WHITE) {
653 gg->wTime += (gg->lastDecTime - gg->lastMoveTime);
655 gg->bTime += (gg->lastDecTime - gg->lastMoveTime);
658 if (gg->numHalfMoves == 0)
659 gg->timeOfStart = now;
660 gg->lastMoveTime = now;
661 gg->lastDecTime = now;
663 } else { /* real game */
665 if ((player_globals.parray[i].simul_info != NULL) && (player_globals.parray[i].simul_info->numBoards &&
666 (player_globals.parray[i].simul_info->boards[player_globals.parray[i].simul_info->onBoard] != g))) {
667 pprintf(p, "It isn't your turn: wait until the simul giver is at your board.\n");
670 if (net_globals.con[pp->socket]->timeseal) { /* does he use timeseal? */
671 timeseal_update_clocks(pp, &game_globals.garray[g]);
673 /* we need to reset the opp's time for receiving the board since the
674 timeseal decoder only alters the time if it's 0 Otherwise the time
675 would be changed if the player did a refresh which would screw up
677 if (pp->side == WHITE) {
678 gg->bTimeWhenReceivedMove = 0;
680 gg->wTimeWhenReceivedMove = 0;
684 game_add_increment(pp, gg);
688 if (gg->numHalfMoves > gg->moveListSize) {
689 gg->moveListSize += 20; /* Allocate 20 moves at a time */
690 gg->moveList = (struct move_t *) realloc(gg->moveList, sizeof(struct move_t) * gg->moveListSize);
692 result = execute_move(&gg->game_state, &move, 1);
693 if (result == MOVE_OK && (gg->link >= 0 || gg->game_state.holdings) && move.pieceCaptured != NOPIECE) {
694 /* transfer captured piece to partner */
695 /* check if piece reverts to a pawn */
696 int victim = move.pieceCaptured, partner = gg->link, demoted;
697 // [HGM] zh: if not Bughouse, the game_state.holdings field decides what happens
699 partner = g; // pieces stay with current board
700 if(gg->game_state.holdings == -1) victim ^= WHITE|BLACK; // flip color
702 if (demoted = was_promoted(&game_globals.garray[g], move.toFile, move.toRank))
703 update_holding(partner, colorval(victim) | demoted); // [HGM] was_promoted now returns original piece type
705 update_holding(partner, victim);
709 if (gg->numHalfMoves > 1) {
710 move.tookTime = move.atTime - gg->lastMoveTime;
712 move.tookTime = move.atTime - gg->startTime;
714 gg->lastMoveTime = now;
715 gg->lastDecTime = now;
716 move.wTime = gg->wTime;
717 move.bTime = gg->bTime;
719 if (net_globals.con[pp->socket]->timeseal) { /* does he use timeseal? */
720 if (pp->side == WHITE) {
721 move.tookTime = (game_globals.garray[pp->game].wTimeWhenMoved -
722 game_globals.garray[pp->game].wTimeWhenReceivedMove) / 100;
724 move.tookTime = (game_globals.garray[pp->game].bTimeWhenMoved -
725 game_globals.garray[pp->game].bTimeWhenReceivedMove) / 100;
729 if (gg->numHalfMoves <= 2) {
733 MakeFENpos(g, move.FENpos);
734 gg->moveList[gg->numHalfMoves - 1] = move;
739 if (result == MOVE_ILLEGAL) {
740 pprintf(p, "Internal error, illegal move accepted!\n");
742 if ((result == MOVE_OK) && (gg->status == GAME_EXAMINE)) {
745 for (p1 = 0; p1 < player_globals.p_num; p1++) {
746 if (player_globals.parray[p1].status != PLAYER_PROMPT)
748 if (player_is_observe(p1, g) || player_globals.parray[p1].game == g) {
749 pprintf(p1, "%s moves: %s\n", pp->name, move.algString);
753 if (result == MOVE_CHECKMATE) {
754 if (gg->status == GAME_EXAMINE) {
757 for (p1 = 0; p1 < player_globals.p_num; p1++) {
758 if (player_globals.parray[p1].status != PLAYER_PROMPT)
760 if (player_is_observe(p1, g) || player_globals.parray[p1].game == g) {
761 pprintf(p1, "%s has been checkmated.\n",
762 (CToggle(gg->game_state.onMove) == BLACK) ? "White" : "Black");
766 game_ended(g, CToggle(gg->game_state.onMove), END_CHECKMATE);
769 if (result == MOVE_STALEMATE) {
770 if (gg->status == GAME_EXAMINE) {
773 for (p1 = 0; p1 < player_globals.p_num; p1++) {
774 if (player_globals.parray[p1].status != PLAYER_PROMPT)
776 if (player_is_observe(p1, g) || player_globals.parray[p1].game == g) {
777 pprintf(p1, "Stalemate.\n");
781 game_ended(g, CToggle(gg->game_state.onMove), END_STALEMATE);
784 if (result == MOVE_NOMATERIAL) {
785 if (gg->status == GAME_EXAMINE) {
788 for (p1 = 0; p1 < player_globals.p_num; p1++) {
789 if (player_globals.parray[p1].status != PLAYER_PROMPT)
791 if (player_is_observe(p1, g) || player_globals.parray[p1].game == g) {
792 pprintf(p1, "No mating material.\n");
796 game_ended(g, CToggle(gg->game_state.onMove), END_NOMATERIAL);
799 if (result == MOVE_BARE) {
800 if (gg->status == GAME_EXAMINE) {
803 for (p1 = 0; p1 < player_globals.p_num; p1++) {
804 if (player_globals.parray[p1].status != PLAYER_PROMPT)
806 if (player_is_observe(p1, g) || player_globals.parray[p1].game == g) {
807 pprintf(p1, "%s bared.\n",
808 (gg->game_state.onMove == BLACK) ? "White" : "Black");
812 game_ended(g, gg->game_state.onMove, END_BARE);
817 int com_resign(int p, param_list param)
819 struct player *pp = &player_globals.parray[p];
820 int g, o, oconnected;
822 if (param[0].type == TYPE_NULL) {
827 decline_withdraw_offers(p, -1, -1, DO_DECLINE);
828 game_ended(g, (game_globals.garray[g].white == p) ? BLACK : WHITE, END_RESIGN);
830 } else if (FindPlayer(p, param[0].val.word, &o, &oconnected)) {
832 if (game_read(g, p, o) < 0) {
833 if (game_read(g, o, p) < 0) {
834 pprintf(p, "You have no stored game with %s\n", player_globals.parray[o].name);
839 game_globals.garray[g].white = o;
840 game_globals.garray[g].black = p;
843 game_globals.garray[g].white = p;
844 game_globals.garray[g].black = o;
846 pprintf(p, "You resign your stored game with %s\n", player_globals.parray[o].name);
847 pcommand(p, "message %s I have resigned our stored game \"%s vs. %s.\"",
848 player_globals.parray[o].name,
849 player_globals.parray[game_globals.garray[g].white].name,
850 player_globals.parray[game_globals.garray[g].black].name);
851 game_delete(game_globals.garray[g].white, game_globals.garray[g].black);
852 game_ended(g, (game_globals.garray[g].white == p) ? BLACK : WHITE, END_RESIGN);
859 static int Check50MoveRule (int p, int g)
861 int num_reversible = game_globals.garray[g].numHalfMoves;
863 if (game_globals.garray[g].game_state.lastIrreversable >= 0) {
864 num_reversible -= game_globals.garray[g].game_state.lastIrreversable;
866 if (num_reversible > 99) {
867 game_ended(g, (game_globals.garray[g].white == p) ? BLACK : WHITE, END_50MOVERULE);
873 static char *GetFENpos (int g, int half_move)
876 return game_globals.garray[g].FENstartPos;
877 else return game_globals.garray[g].moveList[half_move].FENpos;
880 static int CheckRepetition (int p, int g)
882 struct player *pp = &player_globals.parray[p];
883 struct pending* pend;
885 int flag1 = 1, flag2 = 1;
886 char *pos1 = GetFENpos (g, game_globals.garray[g].numHalfMoves - 1);
887 char *pos2 = GetFENpos (g, game_globals.garray[g].numHalfMoves);
890 if (game_globals.garray[g].numHalfMoves < 8) /* can't have three repeats any quicker. */
893 for (move_num = game_globals.garray[g].game_state.lastIrreversable;
894 move_num < game_globals.garray[g].numHalfMoves - 1; move_num++) {
895 pos = GetFENpos (g, move_num);
896 if (strlen(pos1) == strlen(pos) && !strcmp(pos1, pos))
898 if (strlen(pos2) == strlen(pos) && !strcmp(pos2, pos))
901 if (flag1 >= 3 || flag2 >= 3) {
902 if ((pend = find_pend(pp->opponent, p, PEND_DRAW)) != NULL) {
903 delete_pending(pend);
904 decline_withdraw_offers(p, -1, -1,DO_DECLINE);
906 game_ended(g, (game_globals.garray[g].white == p) ? BLACK : WHITE, END_REPETITION);
912 int com_draw(int p, param_list param)
914 struct player *pp = &player_globals.parray[p];
915 struct pending* pend;
916 int p1, g = pp->game;
918 if (!pIsPlaying(p)) {
921 if (Check50MoveRule (p, g) || CheckRepetition(p, g)) {
926 if ((player_globals.parray[p1].simul_info != NULL) && (player_globals.parray[p1].simul_info->numBoards &&
927 player_globals.parray[p1].simul_info->boards[player_globals.parray[p1].simul_info->onBoard] != g)) {
928 pprintf(p, "You can only make requests when the simul player is at your board.\n");
932 if ((pend = (find_pend(pp->opponent, p, PEND_DRAW))) != NULL) {
933 delete_pending(pend);
934 decline_withdraw_offers(p, -1, -1,DO_DECLINE);
935 game_ended(g, (game_globals.garray[g].white == p) ? BLACK : WHITE, END_AGREEDDRAW);
937 pprintf(pp->opponent, "\n");
938 pprintf_highlight(pp->opponent, "%s", pp->name);
939 pprintf_prompt(pp->opponent, " offers you a draw.\n");
940 pprintf(p, "Draw request sent.\n");
941 add_request(p, pp->opponent, PEND_DRAW);
946 int com_pause(int p, param_list param)
948 struct player *pp = &player_globals.parray[p];
950 struct pending* pend;
952 if (!pIsPlaying(p)) {
956 if (game_globals.garray[g].wTime == 0) {
957 pprintf(p, "You can't pause untimed games.\n");
960 if (game_globals.garray[g].clockStopped) {
961 pprintf(p, "Game is already paused, use \"unpause\" to resume.\n");
964 if ((pend = find_pend(pp->opponent, p, PEND_PAUSE)) != NULL) {
965 delete_pending(pend);
966 game_globals.garray[g].clockStopped = 1;
967 /* Roll back the time */
968 if (game_globals.garray[g].game_state.onMove == WHITE) {
969 game_globals.garray[g].wTime += (game_globals.garray[g].lastDecTime - game_globals.garray[g].lastMoveTime);
971 game_globals.garray[g].bTime += (game_globals.garray[g].lastDecTime - game_globals.garray[g].lastMoveTime);
974 if (game_globals.garray[g].numHalfMoves == 0)
975 game_globals.garray[g].timeOfStart = now;
976 game_globals.garray[g].lastMoveTime = now;
977 game_globals.garray[g].lastDecTime = now;
979 pprintf_prompt(pp->opponent, "\n%s accepted pause. Game clock paused.\n",
981 pprintf(p, "Game clock paused.\n");
983 pprintf(pp->opponent, "\n");
984 pprintf_highlight(pp->opponent, "%s", pp->name);
985 pprintf_prompt(pp->opponent, " requests to pause the game.\n");
986 pprintf(p, "Pause request sent.\n");
987 add_request(p, pp->opponent, PEND_PAUSE);
992 int com_unpause(int p, param_list param)
994 struct player *pp = &player_globals.parray[p];
997 struct pending* pend;
999 if (!pIsPlaying(p)) {
1005 if (!game_globals.garray[g].clockStopped) {
1006 pprintf(p, "Game is not paused.\n");
1009 if ((pend = find_pend(pp->opponent, p, PEND_UNPAUSE)) != NULL) {
1010 delete_pending(pend);
1011 game_globals.garray[g].clockStopped = 0;
1013 if (game_globals.garray[g].numHalfMoves == 0)
1014 game_globals.garray[g].timeOfStart = now;
1015 game_globals.garray[g].lastMoveTime = now;
1016 game_globals.garray[g].lastDecTime = now;
1018 pprintf(p, "Game clock resumed.\n");
1019 pprintf_prompt(pp->opponent, "\nGame clock resumed.\n");
1021 pprintf(pp->opponent, "\n");
1022 pprintf_highlight(pp->opponent, "%s", pp->name);
1023 pprintf_prompt(pp->opponent, " requests to unpause the game.\n");
1024 pprintf(p, "Unpause request sent.\n");
1025 add_request(p, pp->opponent, PEND_UNPAUSE);
1030 int com_abort(int p, param_list param)
1032 struct player *pp = &player_globals.parray[p];
1033 struct pending* pend;
1034 int p1, g, myColor, yourColor, myGTime, yourGTime;
1042 if (p == game_globals.garray[g].white) {
1045 myGTime = game_globals.garray[g].wTime;
1046 yourGTime = game_globals.garray[g].bTime;
1050 myGTime = game_globals.garray[g].bTime;
1051 yourGTime = game_globals.garray[g].wTime;
1053 if ((player_globals.parray[p1].simul_info != NULL) &&
1054 (player_globals.parray[p1].simul_info->numBoards &&
1055 player_globals.parray[p1].simul_info->boards[player_globals.parray[p1].simul_info->onBoard] != g)) {
1056 pprintf(p, "You can only make requests when the simul player is at your board.\n");
1059 if ((pend = find_pend(p1, p, PEND_ABORT)) != NULL) {
1060 delete_pending(pend);
1061 decline_withdraw_offers(p, -1, -1,DO_DECLINE);
1062 game_ended(g, yourColor, END_ABORT);
1064 game_update_time(g);
1066 if (net_globals.con[pp->socket]->timeseal
1067 && game_globals.garray[g].game_state.onMove == myColor
1068 && game_globals.garray[g].flag_pending == FLAG_ABORT) {
1069 /* It's my move, opponent has asked for abort; I lagged out,
1070 my timeseal prevented courtesyabort, and I am sending an abort
1071 request before acknowledging (and processing) my opponent's
1072 courtesyabort. OK, let's abort already :-). */
1073 decline_withdraw_offers(p, -1, -1,DO_DECLINE);
1074 game_ended(g, yourColor, END_ABORT);
1077 if (net_globals.con[player_globals.parray[p1].socket]->timeseal) { /* opp uses timeseal? */
1079 int yourRealTime = (myColor == WHITE ? game_globals.garray[g].bRealTime
1080 : game_globals.garray[g].wRealTime);
1081 if (myGTime > 0 && yourGTime <= 0 && yourRealTime > 0) {
1082 /* Override courtesyabort; opponent still has time. Check for lag. */
1085 if (game_globals.garray[g].game_state.onMove != myColor
1086 && game_globals.garray[g].flag_pending != FLAG_CHECKING) {
1087 /* Opponent may be lagging; let's ask. */
1088 game_globals.garray[g].flag_pending = FLAG_ABORT;
1089 game_globals.garray[g].flag_check_time = time(0);
1090 pprintf(p, "Opponent has timeseal; trying to courtesyabort.\n");
1091 pprintf(p1, "\n[G]\n");
1097 if (myGTime > 0 && yourGTime <= 0 && courtesyOK) {
1098 /* player wants to abort + opponent is out of time = courtesyabort */
1099 pprintf(p, "Since you have time, and your opponent has none, the game has been aborted.");
1100 pprintf(p1, "Your opponent has aborted the game rather than calling your flag.");
1101 decline_withdraw_offers(p, -1, -1, DO_DECLINE);
1102 game_ended(g, myColor, END_COURTESY);
1105 pprintf_highlight(p1, "%s", pp->name);
1106 pprintf(p1, " would like to abort the game; ");
1107 pprintf_prompt(p1, "type \"abort\" to accept.\n");
1108 pprintf(p, "Abort request sent.\n");
1109 add_request(p, p1, PEND_ABORT);
1115 static int player_has_mating_material(struct game_state_t *gs, int color)
1119 int minor_pieces = 0;
1121 for (i = 0; i < gs->files; i++)
1122 for (j = 0; j < gs->ranks; j++) {
1123 piece = gs->board[i][j];
1124 switch (piecetype(piece)) {
1127 if (iscolor(piece, color))
1134 if (iscolor(piece, color))
1138 return ((minor_pieces > 1) ? 1 : 0);
1141 int com_flag(int p, param_list param)
1143 struct player *pp = &player_globals.parray[p];
1148 if (!pIsPlaying(p)) {
1153 gg = &game_globals.garray[g];
1155 myColor = (p == gg->white ? WHITE : BLACK);
1156 if (gg->type == TYPE_UNTIMED) {
1157 pprintf(p, "You can't flag an untimed game.\n");
1160 if (gg->numHalfMoves < 2) {
1161 pprintf(p, "You cannot flag before both players have moved.\nUse abort instead.\n");
1164 game_update_time(g);
1167 int myTime, yourTime, opp = pp->opponent, serverTime;
1169 if (net_globals.con[pp->socket]->timeseal) { /* does caller use timeseal? */
1170 myTime = (myColor==WHITE?gg->wRealTime:gg->bRealTime);
1172 myTime = (myColor == WHITE?gg->wTime:gg->bTime);
1174 serverTime = (myColor == WHITE?gg->bTime:gg->wTime);
1176 if (net_globals.con[player_globals.parray[opp].socket]->timeseal) { /* opp uses timeseal? */
1177 yourTime = (myColor == WHITE?gg->bRealTime:gg->wRealTime);
1179 yourTime = serverTime;
1182 /* the clocks to compare are now in myTime and yourTime */
1183 if ((myTime <= 0) && (yourTime <= 0)) {
1184 decline_withdraw_offers(p, -1, -1,DO_DECLINE);
1185 game_ended(g, myColor, END_BOTHFLAG);
1190 /* Opponent still has time, but if that's only because s/he
1191 * may be lagging, we should ask for an acknowledgement and then
1192 * try to call the flag. */
1194 if (serverTime <= 0 && gg->game_state.onMove != myColor
1195 && gg->flag_pending != FLAG_CHECKING) {
1196 /* server time thinks opponent is down, but RealTIme disagrees.
1197 * ask client to acknowledge it's alive. */
1198 gg->flag_pending = FLAG_CALLED;
1199 gg->flag_check_time = time(0);
1200 pprintf(p, "Opponent has timeseal; checking if (s)he's lagging.\n");
1201 pprintf (opp, "\n[G]\n");
1205 /* if we're here, it means one of:
1206 * 1. the server agrees opponent has time, whether lagging or not.
1207 * 2. opp. has timeseal (if yourTime != serverTime), had time left
1208 * after the last move (yourTime > 0), and it's still your move.
1209 * 3. we're currently checking a flag call after having receiving
1210 * acknowledgement from the other timeseal (and would have reset
1211 * yourTime if the flag were down). */
1213 pprintf(p, "Your opponent is not out of time!\n");
1218 decline_withdraw_offers(p, -1, -1,DO_DECLINE);
1219 if (player_has_mating_material(&gg->game_state, myColor))
1220 game_ended(g, myColor, END_FLAG);
1222 game_ended(g, myColor, END_FLAGNOMATERIAL);
1226 int com_adjourn(int p, param_list param)
1228 struct player *pp = &player_globals.parray[p];
1229 struct pending* pend;
1230 int p1, g, myColor, yourColor;
1237 if (!CheckPFlag(p, PFLAG_REG) || !CheckPFlag(p, PFLAG_REG)) {
1238 pprintf(p, "Both players must be registered to adjourn a game. Use \"abort\".\n");
1241 if (game_globals.garray[g].link >= 0) {
1242 pprintf(p, "Bughouse games cannot be adjourned.\n");
1245 myColor = (p == game_globals.garray[g].white ? WHITE : BLACK);
1246 yourColor = (myColor == WHITE ? BLACK : WHITE);
1248 if ((pend = find_pend(p1, p, PEND_ADJOURN)) != NULL) {
1249 delete_pending(pend);
1250 decline_withdraw_offers(p, -1, -1,DO_DECLINE);
1251 game_ended(pp->game, yourColor, END_ADJOURN);
1253 game_update_time(g);
1254 if (((myColor == WHITE) && (game_globals.garray[g].wTime > 0) && (game_globals.garray[g].bTime <= 0))
1255 || ((myColor == BLACK) && (game_globals.garray[g].bTime > 0) && (game_globals.garray[g].wTime <= 0))) {
1256 /* player wants to adjourn + opponent is out of time = courtesyadjourn */
1257 pprintf(p, "Since you have time, and your opponent has none, the game has been adjourned.");
1258 pprintf(p1, "Your opponent has adjourned the game rather than calling your flag.");
1259 decline_withdraw_offers(p, -1, -1,DO_DECLINE);
1260 game_ended(g, myColor, END_COURTESYADJOURN);
1263 pprintf_highlight(p1, "%s", pp->name);
1264 pprintf(p1, " would like to adjourn the game; ");
1265 pprintf_prompt(p1, "type \"adjourn\" to accept.\n");
1266 pprintf(p, "Adjourn request sent.\n");
1267 add_request(p, p1, PEND_ADJOURN);
1273 int com_takeback(int p, param_list param)
1275 struct player *pp = &player_globals.parray[p];
1276 int nHalfMoves = 1, g, i, p1, pend_half_moves;
1277 struct pending* from;
1283 if ((player_globals.parray[p1].simul_info != NULL) &&
1284 (player_globals.parray[p1].simul_info->numBoards &&
1285 player_globals.parray[p1].simul_info->boards[player_globals.parray[p1].simul_info->onBoard] !=
1287 pprintf(p, "You can only make requests when the simul player is at your board.\n");
1292 if (game_globals.garray[g].link >= 0) {
1293 pprintf(p, "Takeback not implemented for bughouse games yet.\n");
1296 if (param[0].type == TYPE_INT) {
1297 nHalfMoves = param[0].val.integer;
1298 if (nHalfMoves <= 0) {
1299 pprintf (p,"You can't takeback less than 1 move.\n");
1303 if ((from = find_pend(pp->opponent, p, PEND_TAKEBACK)) != NULL) {
1304 pend_half_moves = from->wtime;
1305 delete_pending(from);
1306 if (pend_half_moves == nHalfMoves) {
1307 /* Doing the takeback */
1308 decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1309 for (i = 0; i < nHalfMoves; i++) {
1310 if (backup_move(g, REL_GAME) != MOVE_OK) {
1311 pprintf(game_globals.garray[g].white, "Can only backup %d moves\n", i);
1312 pprintf(game_globals.garray[g].black, "Can only backup %d moves\n", i);
1317 game_globals.garray[g].wTimeWhenReceivedMove = 0;
1318 game_globals.garray[g].bTimeWhenReceivedMove = 0;
1323 if (!game_globals.garray[g].numHalfMoves) {
1324 pprintf(p, "There are no moves in your game.\n");
1325 pprintf_prompt(pp->opponent, "\n%s has declined the takeback request.\n",
1330 if (game_globals.garray[g].numHalfMoves < nHalfMoves) {
1331 pprintf(p, "There are only %d half moves in your game.\n", game_globals.garray[g].numHalfMoves);
1332 pprintf_prompt(pp->opponent, "\n%s has declined the takeback request.\n",
1336 pprintf(p, "You disagree on the number of half-moves to takeback.\n");
1337 pprintf(p, "Alternate takeback request sent.\n");
1338 pprintf_prompt(pp->opponent, "\n%s proposes a different number (%d) of half-move(s).\n", pp->name, nHalfMoves);
1339 from = add_request(p, pp->opponent, PEND_TAKEBACK);
1340 from->wtime = nHalfMoves;
1344 if (!game_globals.garray[g].numHalfMoves) {
1345 pprintf(p, "There are no moves in your game.\n");
1348 if (game_globals.garray[g].numHalfMoves < nHalfMoves) {
1349 pprintf(p, "There are only %d half moves in your game.\n", game_globals.garray[g].numHalfMoves);
1352 pprintf(pp->opponent, "\n");
1353 pprintf_highlight(pp->opponent, "%s", pp->name);
1354 pprintf_prompt(pp->opponent, " would like to take back %d half move(s).\n",
1356 pprintf(p, "Takeback request sent.\n");
1357 from = add_request(p, pp->opponent, PEND_TAKEBACK);
1358 from->wtime = nHalfMoves;
1364 int com_switch(int p, param_list param)
1366 struct player *pp = &player_globals.parray[p];
1367 int g = pp->game, tmp, now, p1;
1369 struct pending* pend;
1375 if ((player_globals.parray[p1].simul_info != NULL) && (player_globals.parray[p1].simul_info->numBoards &&
1376 player_globals.parray[p1].simul_info->boards[player_globals.parray[p1].simul_info->onBoard] != g)) {
1377 pprintf(p, "You can only make requests when the simul player is at your board.\n");
1381 if (game_globals.garray[g].link >= 0) {
1382 pprintf(p, "Switch not implemented for bughouse games.\n");
1385 if ((pend = find_pend(pp->opponent, p, PEND_SWITCH)) != NULL) {
1386 delete_pending(pend);
1387 /* Doing the switch */
1388 decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1390 tmp = game_globals.garray[g].white;
1391 game_globals.garray[g].white = game_globals.garray[g].black;
1392 game_globals.garray[g].black = tmp;
1393 pp->side = (pp->side == WHITE) ? BLACK : WHITE;
1394 strTmp = strdup(game_globals.garray[g].white_name);
1395 strcpy(game_globals.garray[g].white_name, game_globals.garray[g].black_name);
1396 strcpy(game_globals.garray[g].black_name, strTmp);
1399 player_globals.parray[pp->opponent].side =
1400 (player_globals.parray[pp->opponent].side == WHITE) ? BLACK : WHITE;
1401 /* Roll back the time */
1402 if (game_globals.garray[g].game_state.onMove == WHITE) {
1403 game_globals.garray[g].wTime += (game_globals.garray[g].lastDecTime - game_globals.garray[g].lastMoveTime);
1405 game_globals.garray[g].bTime += (game_globals.garray[g].lastDecTime - game_globals.garray[g].lastMoveTime);
1408 if (game_globals.garray[g].numHalfMoves == 0)
1409 game_globals.garray[g].timeOfStart = now;
1410 game_globals.garray[g].lastMoveTime = now;
1411 game_globals.garray[g].lastDecTime = now;
1415 if (game_globals.garray[g].rated && game_globals.garray[g].numHalfMoves > 0) {
1416 pprintf(p, "You cannot switch sides once a rated game is underway.\n");
1419 pprintf(pp->opponent, "\n");
1420 pprintf_highlight(pp->opponent, "%s", pp->name);
1421 pprintf_prompt(pp->opponent, " would like to switch sides.\nType \"accept\" to switch sides, or \"decline\" to refuse.\n");
1422 pprintf(p, "Switch request sent.\n");
1423 add_request(p, pp->opponent, PEND_SWITCH);
1427 int com_time(int p, param_list param)
1429 struct player *pp = &player_globals.parray[p];
1432 if (param[0].type == TYPE_NULL) {
1437 g = GameNumFromParam(p, &p1, ¶m[0]);
1441 if ((g < 0) || (g >= game_globals.g_num) || (game_globals.garray[g].status != GAME_ACTIVE)) {
1442 pprintf(p, "There is no such game.\n");
1445 game_update_time(g);
1446 pprintf(p, "White (%s) : %d mins, %d secs\n",
1447 player_globals.parray[game_globals.garray[g].white].name,
1448 game_globals.garray[g].wTime / 600,
1449 (game_globals.garray[g].wTime - ((game_globals.garray[g].wTime / 600) * 600)) / 10);
1450 pprintf(p, "Black (%s) : %d mins, %d secs\n",
1451 player_globals.parray[game_globals.garray[g].black].name,
1452 game_globals.garray[g].bTime / 600,
1453 (game_globals.garray[g].bTime - ((game_globals.garray[g].bTime / 600) * 600)) / 10);
1457 int com_ptime(int p, param_list param)
1459 struct player *pp = &player_globals.parray[p];
1460 int retval, part = pp->partner;
1463 pprintf(p, "You do not have a partner.\n");
1466 retval = pcommand (p, "time %s", player_globals.parray[part].name);
1467 if (retval == COM_OK)
1468 return COM_OK_NOPROMPT;
1473 int com_boards(int p, param_list param)
1475 char *category = NULL;
1476 char dname[MAX_FILENAME_SIZE];
1480 if (param[0].type == TYPE_WORD)
1481 category = param[0].val.word;
1483 pprintf(p, "Boards Available For Category %s:\n", category);
1484 sprintf(dname, "%s/%s", BOARD_DIR, category);
1486 pprintf(p, "Categories Available:\n");
1487 sprintf(dname, "%s", BOARD_DIR);
1489 dirp = opendir(dname);
1491 pprintf(p, "No such category %s, try \"boards\".\n", category);
1495 /* YUK! what a mess, how about printing an ordered directory? - DAV*/
1497 for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
1498 if (!strcmp(dp->d_name, "."))
1500 if (!strcmp(dp->d_name, ".."))
1502 pprintf(p, "%s\n", dp->d_name);
1508 int com_simmatch(int p, param_list param)
1510 struct player *pp = &player_globals.parray[p];
1511 int p1, g, adjourned;
1514 struct pending* pend;
1516 char* category = NULL;
1517 char fname[MAX_FILENAME_SIZE];
1520 if (game_globals.garray[pp->game].status == GAME_EXAMINE) {
1521 pprintf(p, "You are still examining a game.\n");
1524 if (game_globals.garray[pp->game].status == GAME_SETUP) {
1525 pprintf(p, "You are still setting up a position.\n");
1529 p1 = player_find_part_login(param[0].val.word);
1531 pprintf(p, "No user named \"%s\" is logged in.\n", param[0].val.word);
1535 pprintf(p, "You can't simmatch yourself!\n");
1538 if ((pend = find_pend(p1, p, PEND_SIMUL)) != NULL) {
1540 /* Accepting Simul ! */
1542 if ((pp->simul_info != NULL) &&
1543 (pp->simul_info->numBoards >= MAX_SIMUL)) {
1544 pprintf(p, "You are already playing the maximum of %d boards.\n", MAX_SIMUL);
1545 pprintf(p1, "Simul request removed, boards filled.\n");
1546 delete_pending(pend);
1549 unobserveAll(p); /* stop observing when match starts */
1554 if (game_read(g, p, p1) >= 0) {
1556 delete_pending(pend);
1559 if (!adjourned) { /* no adjourned game, so begin a new game */
1561 if ((pend->category != NULL) && (pend->board_type != NULL)) {
1562 board = strdup(pend->category);
1563 category = strdup(pend->board_type);
1566 delete_pending(pend);
1568 if (create_new_match(g,p, p1, 0, 0, 0, 0, 0, ((board == NULL) ? "\0" : board), ((category == NULL) ? "\0" : category), 1,1) == COM_FAILED) {
1569 pprintf(p, "There was a problem creating the new match.\n");
1570 pprintf_prompt(p1, "There was a problem creating the new match.\n");
1573 if (board != NULL) {
1580 if (board != NULL) {
1585 } else { /* resume adjourned game */
1588 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]);
1592 game_globals.garray[g].white = p;
1593 game_globals.garray[g].black = p1;
1594 game_globals.garray[g].status = GAME_ACTIVE;
1595 game_globals.garray[g].startTime = tenth_secs();
1596 game_globals.garray[g].lastMoveTime = game_globals.garray[g].startTime;
1597 game_globals.garray[g].lastDecTime = game_globals.garray[g].startTime;
1601 player_globals.parray[p1].game = g;
1602 player_globals.parray[p1].opponent = p;
1603 player_globals.parray[p1].side = BLACK;
1607 if (pp->simul_info == NULL) {
1608 pp->simul_info = (struct simul_info_t *) malloc(sizeof(struct simul_info_t));
1609 pp->simul_info->numBoards = 0;
1610 pp->simul_info->onBoard = 0;
1611 pp->simul_info->num_wins = pp->simul_info->num_draws
1612 = pp->simul_info->num_losses = 0;
1614 num = pp->simul_info->numBoards;
1615 /* pp->simul_info->results[num] = -1; */
1616 pp->simul_info->boards[num] = pp->game;
1617 pp->simul_info->numBoards++;
1618 if (pp->simul_info->numBoards > 1 &&
1619 pp->simul_info->onBoard >= 0)
1620 player_goto_board(p, pp->simul_info->onBoard);
1622 pp->simul_info->onBoard = 0;
1625 if (find_pend(-1, p, PEND_SIMUL) != NULL) {
1626 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");
1629 if (pp->simul_info != NULL) {
1630 if (pp->simul_info->numBoards) {
1631 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 pprintf(p, "You are already playing a game.\n");
1639 if (!CheckPFlag(p1, PFLAG_SIMOPEN)) {
1640 pprintf_highlight(p, "%s", player_globals.parray[p1].name);
1641 pprintf(p, " is not open to receiving simul requests.\n");
1644 if ((player_globals.parray[p1].simul_info != NULL) && (player_globals.parray[p1].simul_info->numBoards >= MAX_SIMUL)) {
1645 pprintf_highlight(p, "%s", player_globals.parray[p1].name);
1646 pprintf(p, " is already playing the maximum of %d boards.\n", MAX_SIMUL);
1650 /* loon: checking for some crazy situations we can't allow :) */
1652 if ((player_globals.parray[p1].simul_info != NULL) && (player_globals.parray[p1].game >=0) && (player_globals.parray[p1].simul_info->numBoards == 0)) {
1653 pprintf_highlight(p, "%s", player_globals.parray[p1].name);
1654 if (player_globals.parray[game_globals.garray[player_globals.parray[p1].game].white].simul_info->numBoards) {
1655 pprintf(p, " is playing in ");
1656 pprintf_highlight(p, "%s", player_globals.parray[player_globals.parray[p1].opponent].name);
1657 pprintf(p, "'s simul, and can't accept.\n");
1659 pprintf(p, " can't begin a simul while playing a non-simul game.\n");
1664 g = game_new(); /* Check if an adjourned untimed game */
1665 adjourned = ((game_read(g, p, p1) < 0) && (game_read(g, p1, p) < 0)) ? 0 : 1;
1667 if (!(game_globals.garray[g].type == TYPE_UNTIMED))
1672 pend = add_request(p, p1, PEND_SIMUL);
1674 if ((param[1].type == TYPE_WORD) && (param[2].type == TYPE_WORD)) {
1676 sprintf(fname, "%s/%s/%s", BOARD_DIR, param[1].val.word , param[2].val.word);
1677 if (!file_exists(fname)) {
1678 pprintf(p, "No such category/board: %s/%s\n", param[1].val.word , param[2].val.word);
1681 pend->category = strdup(param[1].val.word);
1682 pend->board_type = strdup(param[2].val.word);
1684 pend->category = NULL;
1685 pend->board_type = NULL;
1689 pprintf_highlight(p1, "%s", pp->name);
1691 pprintf_prompt(p1, " requests to continue an adjourned simul game.\n");
1692 pprintf(p, "Request to resume simul sent. Adjourned game found.\n");
1694 if (pend->category == NULL)
1695 pprintf_prompt(p1, " requests to join a simul match with you.\n");
1697 pprintf_prompt(p1, " requests to join a %s %s simul match with you.\n",
1698 pend->category,pend->board_type);
1699 pprintf(p, "Simul match request sent.\n");
1704 int com_goboard(int p, param_list param)
1706 struct player *pp = &player_globals.parray[p];
1707 int on, g, p1, gamenum;
1709 if (pp->simul_info == NULL) {
1710 pprintf(p, "You are not giving a simul.\n");
1714 if (!pp->simul_info->numBoards) {
1715 pprintf(p, "You are not giving a simul.\n");
1719 if (param[0].type == TYPE_WORD) {
1721 p1 = player_find_part_login(param[0].val.word);
1723 pprintf(p, "No user named \"%s\" is logged in.\n", param[0].val.word);
1727 pprintf(p, "You can't goboard yourself!\n");
1731 gamenum = player_globals.parray[p1].game;
1733 pprintf (p,"%s is not playing a game.\n", player_globals.parray[p1].login);
1738 gamenum = param[0].val.integer - 1;
1743 on = pp->simul_info->onBoard;
1744 g = pp->simul_info->boards[on];
1746 pprintf(p, "You are already at that board!\n");
1749 if (pp->simul_info->numBoards > 1) {
1750 decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1751 if (player_goto_simulgame_bynum(p, gamenum) !=-1) {
1753 pprintf(game_globals.garray[g].black, "\n");
1754 pprintf_highlight(game_globals.garray[g].black, "%s", pp->name);
1755 pprintf_prompt(game_globals.garray[g].black, " has moved away from your board.\n");
1758 pprintf(p, "You are not playing that game/person.\n");
1760 pprintf(p, "You are only playing one board!\n");
1764 int com_simnext(int p, param_list param)
1766 struct player *pp = &player_globals.parray[p];
1769 if (pp->simul_info == NULL) {
1770 pprintf(p, "You are not giving a simul.\n");
1774 if (!pp->simul_info->numBoards) {
1775 pprintf(p, "You are not giving a simul.\n");
1779 if (pp->simul_info->numBoards > 1) {
1780 decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1781 on = pp->simul_info->onBoard;
1782 g = pp->simul_info->boards[on];
1784 pprintf(game_globals.garray[g].black, "\n");
1785 pprintf_highlight(game_globals.garray[g].black, "%s", pp->name);
1786 pprintf_prompt(game_globals.garray[g].black, " is moving away from your board.\n");
1787 player_goto_next_board(p);
1790 pprintf(p, "You are only playing one board!\n");
1794 int com_simprev(int p, param_list param)
1796 struct player *pp = &player_globals.parray[p];
1799 if (pp->simul_info == NULL) {
1800 pprintf(p, "You are not giving a simul.\n");
1804 if (!pp->simul_info->numBoards) {
1805 pprintf(p, "You are not giving a simul.\n");
1808 if (pp->simul_info->numBoards > 1) {
1809 decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1810 on = pp->simul_info->onBoard;
1811 g = pp->simul_info->boards[on];
1813 pprintf(game_globals.garray[g].black, "\n");
1814 pprintf_highlight(game_globals.garray[g].black, "%s", pp->name);
1815 pprintf_prompt(game_globals.garray[g].black, " is moving back to the previous board.\n");
1817 player_goto_prev_board(p);
1819 pprintf(p, "You are only playing one board!\n");
1823 int com_simgames(int p, param_list param)
1827 if (param[0].type == TYPE_WORD) {
1828 if ((p1 = player_find_part_login(param[0].val.word)) < 0) {
1829 pprintf(p, "No player named %s is logged in.\n", param[0].val.word);
1834 pprintf(p, "You are playing %d simultaneous games.\n",
1835 player_num_active_boards(p1));
1837 pprintf(p, "%s is playing %d simultaneous games.\n", player_globals.parray[p1].name,
1838 player_num_active_boards(p1));
1842 int com_simpass(int p, param_list param)
1844 struct player *pp = &player_globals.parray[p];
1851 p1 = game_globals.garray[g].white;
1853 if (player_globals.parray[p1].simul_info == NULL) {
1854 pprintf(p, "You are not participating in a simul.\n");
1858 if (!player_globals.parray[p1].simul_info->numBoards) {
1859 pprintf(p, "You are not participating in a simul.\n");
1863 pprintf(p, "You are the simul holder and cannot pass!\n");
1866 if (player_num_active_boards(p1) == 1) {
1867 pprintf(p, "This is the only game, so passing is futile.\n");
1870 on = player_globals.parray[p1].simul_info->onBoard;
1871 if (player_globals.parray[p1].simul_info->boards[on] != g) {
1872 pprintf(p, "You cannot pass until the simul holder arrives!\n");
1875 if (game_globals.garray[g].passes >= MAX_SIMPASS) {
1877 pprintf(p, "You have reached your maximum of %d pass(es).\n", MAX_SIMPASS);
1878 pprintf(p, "Please move IMMEDIATELY!\n");
1879 pprintf_highlight(p1, "%s", pp->name);
1880 pprintf_prompt(p1, " tried to pass, but is out of passes.\n");
1883 decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1885 game_globals.garray[g].passes++;
1886 pprintf(p, "You have passed and have %d pass(es) left.\n",
1887 (MAX_SIMPASS - game_globals.garray[g].passes));
1888 pprintf_highlight(p1, "%s", pp->name);
1889 pprintf_prompt(p1, " has decided to pass and has %d pass(es) left.\n",
1890 (MAX_SIMPASS - game_globals.garray[g].passes));
1891 player_goto_next_board(p1);
1895 int com_simabort(int p, param_list param)
1897 struct player *pp = &player_globals.parray[p];
1899 if (pp->simul_info == NULL) {
1900 pprintf(p, "You are not giving a simul.\n");
1904 if (!pp->simul_info->numBoards) {
1905 pprintf(p, "You are not giving a simul.\n");
1908 decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1909 game_ended(pp->simul_info->boards[pp->simul_info->onBoard],
1914 int com_simallabort(int p, param_list param)
1916 struct player *pp = &player_globals.parray[p];
1919 if (pp->simul_info == NULL) {
1920 pprintf(p, "You are not giving a simul.\n");
1924 if (!pp->simul_info->numBoards) {
1925 pprintf(p, "You are not giving a simul.\n");
1929 decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1930 for (i = 0; i < pp->simul_info->numBoards; i++)
1931 if (pp->simul_info->boards[i] >= 0)
1932 game_ended(pp->simul_info->boards[i],
1938 int com_simadjourn(int p, param_list param)
1940 struct player *pp = &player_globals.parray[p];
1942 if (pp->simul_info == NULL) {
1943 pprintf(p, "You are not giving a simul.\n");
1947 if (!pp->simul_info->numBoards) {
1948 pprintf(p, "You are not giving a simul.\n");
1951 decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1952 game_ended(pp->simul_info->boards[pp->simul_info->onBoard],
1953 WHITE, END_ADJOURN);
1957 int com_simalladjourn(int p, param_list param)
1959 struct player *pp = &player_globals.parray[p];
1962 if (pp->simul_info == NULL) {
1963 pprintf(p, "You are not giving a simul.\n");
1967 if (!pp->simul_info->numBoards) {
1968 pprintf(p, "You are not giving a simul.\n");
1971 decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1972 for (i = 0; i < pp->simul_info->numBoards; i++)
1973 if (pp->simul_info->boards[i] >= 0)
1974 game_ended(pp->simul_info->boards[i],
1975 WHITE, END_ADJOURN);
1980 int com_moretime(int p, param_list param)
1982 struct player *pp = &player_globals.parray[p];
1985 if ((pp->game >=0) &&((game_globals.garray[pp->game].status == GAME_EXAMINE) ||
1986 (game_globals.garray[pp->game].status == GAME_SETUP))) {
1987 pprintf(p, "You cannot use moretime in an examined game.\n");
1990 increment = param[0].val.integer;
1991 if (increment <= 0) {
1992 pprintf(p, "Moretime requires an integer value greater than zero.\n");
1998 if (increment > 600) {
1999 pprintf(p, "Moretime has a maximum limit of 600 seconds.\n");
2003 if (game_globals.garray[g].white == p) {
2004 game_globals.garray[g].bTime += increment * 10;
2005 game_globals.garray[g].bRealTime += increment * 10 * 100;
2006 pprintf(p, "%d seconds were added to your opponents clock\n",
2008 pprintf_prompt(pp->opponent,
2009 "\nYour opponent has added %d seconds to your clock.\n",
2012 if (game_globals.garray[g].black == p) {
2013 game_globals.garray[g].wTime += increment * 10;;
2014 game_globals.garray[g].wRealTime += increment * 10 * 100;
2015 pprintf(p, "%d seconds were added to your opponents clock\n",
2017 pprintf_prompt(pp->opponent,
2018 "\nYour opponent has added %d seconds to your clock.\n",