53147a8b836d2b26fd54013826e5dcf1282c7520
[capablanca.git] / lasker-2.2.3 / src / gameproc.c
1 /*
2    Copyright (c) 1993 Richard V. Nash.
3    Copyright (c) 2000 Dan Papasian
4    Copyright (C) Andrew Tridgell 2002
5    
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.
10    
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.
15    
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.
19 */
20
21 #include "includes.h"
22
23 /*Let users know that someone is available (format the message)*/
24 static void getavailmess(int p, char* message)
25 {
26         struct player *pp = &player_globals.parray[p];
27         char titles[100];
28
29         titles [0]='\0';
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.",
33                  pp->name, titles,
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));
39 }
40
41 void getnotavailmess(int p, char* message)
42 {
43         struct player *pp = &player_globals.parray[p];
44         char titles[100];
45
46         titles[0]='\0';
47         AddPlayerLists(p,titles);
48         sprintf (message,"%s%s is no longer available for matches.",
49                  pp->name, titles);
50 }
51
52 void announce_avail(int p)
53 {
54         struct player *pp = &player_globals.parray[p];
55         char avail[200];
56         int p1;
57         if ((pp->game < 0) && (CheckPFlag(p, PFLAG_OPEN))) {
58                 getavailmess (p,avail);
59                 
60                 for (p1 = 0; p1 < player_globals.p_num; p1++) {
61                         if (p == p1)
62                                 continue;
63                         if (player_globals.parray[p1].status != PLAYER_PROMPT)
64                                 continue;
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);
69                 }
70         }
71 }
72
73 void announce_notavail(int p)
74 {
75         struct player *pp = &player_globals.parray[p];
76         char avail[200];
77         int p1;
78         
79         getnotavailmess (p,avail);
80         
81         for (p1 = 0; p1 < player_globals.p_num; p1++) {
82                 if (p == p1)
83                         continue;
84                 if (player_globals.parray[p1].status != PLAYER_PROMPT)
85                         continue;
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);
90         }
91 }
92
93 void game_ended(int g, int winner, int why)
94 {
95   struct game *gg = &game_globals.garray[g];
96   char outstr[200];
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];
101   char tmp[200];
102   int p;
103   int gl = gg->link;
104   int rate_change = 0;
105   int isDraw = 0;
106   int whiteResult;
107   char winSymbol[10];
108   char EndSymbol[10];
109   char *NameOfWinner, *NameOfLoser;
110   int beingplayed = 0;          /* i.e. it wasn't loaded for adjudication */
111   int print_avail = 0;
112
113   avail_white[0] = '\0';
114   avail_black[0] = '\0';
115   avail_bugwhite[0] = '\0';
116   avail_bugblack[0] = '\0';
117
118   beingplayed = (player_globals.parray[gg->black].game == g);
119
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);
123   gg->result = why;
124   gg->winner = winner;
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;
130   } else {
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;
135   }
136   switch (why) {
137   case END_CHECKMATE:
138     sprintf(tmp, "%s checkmated} %s", NameOfLoser, winSymbol);
139     strcpy(EndSymbol, "Mat");
140     rate_change = 1;
141     break;
142   case END_BARE:
143     sprintf(tmp, "%s bared} %s", NameOfLoser, winSymbol);
144     strcpy(EndSymbol, "Bar");
145     rate_change = 1;
146     break;
147   case END_RESIGN:
148     sprintf(tmp, "%s resigns} %s", NameOfLoser, winSymbol);
149     strcpy(EndSymbol, "Res");
150     rate_change = 1;
151     break;
152   case END_FLAG:
153     sprintf(tmp, "%s forfeits on time} %s", NameOfLoser, winSymbol);
154     strcpy(EndSymbol, "Fla");
155     rate_change = 1;
156     break;
157   case END_STALEMATE:
158     sprintf(tmp, "Game drawn by stalemate} 1/2-1/2");
159     isDraw = 1;
160     strcpy(EndSymbol, "Sta");
161     rate_change = 1;
162     whiteResult = RESULT_DRAW;
163     break;
164   case END_AGREEDDRAW:
165     sprintf(tmp, "Game drawn by mutual agreement} 1/2-1/2");
166     isDraw = 1;
167     strcpy(EndSymbol, "Agr");
168     rate_change = 1;
169     whiteResult = RESULT_DRAW;
170     break;
171   case END_BOTHFLAG:
172     sprintf(tmp, "Game drawn because both players ran out of time} 1/2-1/2");
173     isDraw = 1;
174     strcpy(EndSymbol, "Fla");
175     rate_change = 1;
176     whiteResult = RESULT_DRAW;
177     break;
178   case END_REPETITION:
179     sprintf(tmp, "Game drawn by repetition} 1/2-1/2");
180     isDraw = 1;
181     strcpy(EndSymbol, "Rep");
182     rate_change = 1;
183     whiteResult = RESULT_DRAW;
184     break;
185   case END_50MOVERULE:
186     sprintf(tmp, "Game drawn by the 50 move rule} 1/2-1/2");
187     isDraw = 1;
188     strcpy(EndSymbol, "50");
189     rate_change = 1;
190     whiteResult = RESULT_DRAW;
191     break;
192   case END_ADJOURN:
193     if (gl >= 0) {
194       sprintf(tmp, "Bughouse game aborted.} *");
195       whiteResult = RESULT_ABORT;
196     } else {
197     sprintf(tmp, "Game adjourned by mutual agreement} *");
198     game_save(g);
199     }
200     break;
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)
205         && gl < 0) {
206       sprintf(tmp, "adjourned} *");
207       game_save(g);
208     } else
209       sprintf(tmp, "aborted} *");
210     whiteResult = RESULT_ABORT;
211     break;
212   case END_ABORT:
213     sprintf(tmp, "Game aborted by mutual agreement} *");
214     whiteResult = RESULT_ABORT;
215     break;
216   case END_COURTESY:
217     sprintf(tmp, "Game courtesyaborted by %s} *", NameOfWinner);
218     whiteResult = RESULT_ABORT;
219     break;
220   case END_COURTESYADJOURN:
221     if (gl >= 0) {
222       sprintf(tmp, "Bughouse game courtesyaborted by %s.} *", NameOfWinner);
223       whiteResult = RESULT_ABORT;
224     } else {
225     sprintf(tmp, "Game courtesyadjourned by %s} *", NameOfWinner);
226     game_save(g);
227     }
228     break;
229   case END_NOMATERIAL:
230     /* Draw by insufficient material (e.g., lone K vs. lone K) */
231     sprintf(tmp, "Neither player has mating material} 1/2-1/2");
232     isDraw = 1;
233     strcpy(EndSymbol, "NM ");
234     rate_change = 1;
235     whiteResult = RESULT_DRAW;
236     break;
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);
240     isDraw = 1;
241     strcpy(EndSymbol, "TM ");
242     rate_change = 1;
243     whiteResult = RESULT_DRAW;
244     break;
245   case END_ADJWIN:
246     sprintf(tmp, "%s wins by adjudication} %s", NameOfWinner, winSymbol);
247     strcpy(EndSymbol, "Adj");
248     rate_change = 1;
249     break;
250   case END_ADJDRAW:
251     sprintf(tmp, "Game drawn by adjudication} 1/2-1/2");
252     isDraw = 1;
253     strcpy(EndSymbol, "Adj");
254     rate_change = 1;
255     whiteResult = RESULT_DRAW;
256     break;
257   case END_ADJABORT:
258     sprintf(tmp, "Game aborted by adjudication} *");
259     whiteResult = RESULT_ABORT;
260     break;
261   default:
262     sprintf(tmp, "Hmm, the game ended and I don't know why} *");
263     break;
264   }
265   strcat(outstr, tmp);
266
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);
272           strcat(outstr, tmp);
273   }
274
275   strcat(outstr, "\n");
276
277   if (gg->rated && rate_change && gg->type != TYPE_BUGHOUSE)
278     /* Adjust ratings; bughouse gets done later. */
279     rating_update(g, -1);
280
281   if (beingplayed) {
282     int printed = 0;
283     int avail_printed = 0;
284
285     pprintf_noformat(gg->white, outstr);
286     pprintf_noformat(gg->black, outstr);
287     Bell (gg->white);
288     Bell (gg->black);
289
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);
296         print_avail = 1;
297       }
298       if (CheckPFlag(game_globals.garray[gl].black, PFLAG_OPEN)) {
299         getavailmess (game_globals.garray[gl].black, avail_bugblack);
300         print_avail = 1;
301       }
302       if ((gg->rated) && (rate_change)) {
303         /* Adjust ratings */
304         rating_update(g, gl);
305       }
306       game_ended(gl, CToggle(winner), why);
307     }
308
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);
312       print_avail = 1;
313     } else {    /* Part of an ongoing simul!  Let's shrink the array. */
314       
315     }
316     
317     if (CheckPFlag(gg->black, PFLAG_OPEN)) {
318       getavailmess (gg->black,avail_black);
319       print_avail = 1;
320     }
321
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))
325         continue;
326       if (pp->status != PLAYER_PROMPT)
327         continue;
328
329       if (CheckPFlag(p, PFLAG_GIN) || player_is_observe(p, g)) {
330         pprintf_noformat(p, outstr);
331         printed = 1;
332       }
333
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);
337           avail_printed = 1;
338         }
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);
341           avail_printed = 1;
342         }
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);
346             avail_printed = 1;
347           }
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);
350             avail_printed = 1;
351           }
352         }
353         if (avail_printed) {
354           avail_printed = 0;
355           printed = 1; 
356           pprintf (p,"\n");
357         }
358       }
359
360       if (printed) {
361         send_prompt(p);
362         printed = 0;
363       }
364     }
365
366     if (!(gg->rated && rate_change)) {
367       pprintf(gg->white, "No ratings adjustment done.\n");
368       pprintf(gg->black, "No ratings adjustment done.\n");
369     } 
370   }
371
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");
377   }
378   if (CheckPFlag(gg->black, PFLAG_AUTOMAIL)) {
379     pcommand(gg->black, "mailmoves");
380   }
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);
387   }
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);
392   if (beingplayed) {
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);
404   }
405   game_finish(g); 
406 }
407
408 static int was_promoted(struct game *g, int f, int r)
409 {
410 #define BUGHOUSE_PAWN_REVERT 1
411 #ifdef BUGHOUSE_PAWN_REVERT
412   int i;
413
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;
425         }
426       }
427       if (g->moveList[i].fromFile == ALG_DROP)
428         return 0;
429       f = g->moveList[i].fromFile;
430       r = g->moveList[i].fromRank;
431     }
432   }
433 #endif
434   return 0;
435 }
436
437 int pIsPlaying (int p)
438 {
439         struct player *pp = &player_globals.parray[p];
440         int g = pp->game;
441         int p1 = pp->opponent;
442         
443         if (g < 0 || game_globals.garray[g].status != GAME_ACTIVE) {
444                 pprintf (p, "You are not playing a game.\n");
445                 return 0;
446         } 
447
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);
453                 pp->game = -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;
458                 }
459                 return 0;
460         }
461         return 1;
462 }
463
464 /* add clock increments */
465 static void game_add_increment(struct player *pp, struct game *gg)
466 {
467         /* no update on first move */
468         if (gg->game_state.moveNum == 1) return;
469
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
474                                                                                                    tenth secs */
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
478                                                                                                    tenth secs */
479                 }
480         } else {
481                 if (gg->game_state.onMove == BLACK) {
482                         gg->bTime += gg->bIncrement;
483                 }
484                 if (gg->game_state.onMove == WHITE) {
485                         gg->wTime += gg->wIncrement;
486                 }
487         }
488 }
489
490 /* updates clocks for a game with timeseal */
491 void timeseal_update_clocks(struct player *pp, struct game *gg)
492 {
493         /* no update on first move */
494         if (gg->game_state.moveNum == 1) return;
495
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;
505                 } else {
506                         gg->wRealTime -= gg->wTimeWhenMoved - gg->wTimeWhenReceivedMove;
507                 }
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;
517                 } else {
518                         gg->bRealTime -= gg->bTimeWhenMoved - gg->bTimeWhenReceivedMove;
519                 }
520         }
521 }
522
523
524 void process_move(int p, char *command)
525 {
526   struct player *pp = &player_globals.parray[p];
527   struct game *gg;
528   int g, result, len, i;
529   struct move_t move;
530   unsigned now = 0;
531
532   if (pp->game < 0) {
533     pprintf(p, "You are not playing or examining a game.\n");
534     return;
535   }
536   decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
537
538   g = pp->game;
539   gg = &game_globals.garray[g];
540
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");
545     } else
546     send_board_to(g, p); 
547     return;
548   }
549
550   if (gg->status != GAME_EXAMINE) {
551     if (!pIsPlaying(p)) return;
552
553     if (pp->side != gg->game_state.onMove) {
554       pprintf(p, "It is not your move.\n");
555       return;
556     }
557     if (gg->clockStopped) {
558       pprintf(p, "Game clock is paused, use \"unpause\" to resume.\n");
559       return;
560     }
561   }
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])) {
567       case 'n':
568         pp->promote = KNIGHT;
569         break;
570       case 'b':
571         pp->promote = BISHOP;
572         break;
573       case 'r':
574         pp->promote = ROOK;
575         break;
576       case 'a':
577         pp->promote = CARDINAL;
578         break;
579       case 'c':
580         pp->promote = MARSHALL;
581         break;
582       case 'm':
583         pp->promote = MAN;
584         break;
585       case 'q':
586         pp->promote = QUEEN;
587         break;
588       // courier promotion
589       case 'f':
590         pp->promote = FERZ2;
591         break;
592       // Superchess promotions
593       case 'e':
594         pp->promote = EMPRESS;
595         break;
596       case 's':
597         pp->promote = PRINCESS;
598         break;
599       case 'v':
600         pp->promote = CENTAUR;
601         break;
602       case 'w':
603         pp->promote = WOODY;
604         break;
605       case 'o':
606         pp->promote = SQUIRREL;
607         break;
608       case 'g':
609         pp->promote = MASTODON;
610         break;
611       // Shogi promotions
612       case '^':
613       case '+':
614         pp->promote = GOLD;
615         break;
616       case '=':
617         pp->promote = NOPIECE;
618         break;
619       default:
620         pprintf(p, "Don't understand that move.\n");
621         return;
622         break;
623       }
624     }
625   }
626
627   switch (parse_move(command, &gg->game_state, &move, pp->promote)) {
628   case MOVE_ILLEGAL:
629     pprintf(p, "Illegal move.\n");
630     return;
631     break;
632   case MOVE_AMBIGUOUS:
633     pprintf(p, "Ambiguous move.\n");
634     return;
635     break;
636   default:
637     break;
638   }
639
640   if (gg->status == GAME_EXAMINE) {
641     gg->numHalfMoves++;
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);
645     }
646     result = execute_move(&gg->game_state, &move, 1);
647     move.atTime = now;
648     move.tookTime = 0;
649     MakeFENpos(g, move.FENpos);
650     gg->examMoveList[gg->numHalfMoves - 1] = move;
651     /* roll back time */
652     if (gg->game_state.onMove == WHITE) {
653       gg->wTime += (gg->lastDecTime - gg->lastMoveTime);
654     } else {
655       gg->bTime += (gg->lastDecTime - gg->lastMoveTime);
656     }
657     now = tenth_secs();
658     if (gg->numHalfMoves == 0)
659       gg->timeOfStart = now;
660     gg->lastMoveTime = now;
661     gg->lastDecTime = now;
662
663   } else {                      /* real game */
664     i = pp->opponent;
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");
668       return;
669     }
670     if (net_globals.con[pp->socket]->timeseal) {        /* does he use timeseal? */
671             timeseal_update_clocks(pp, &game_globals.garray[g]);
672     }
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
676        the timings */
677     if (pp->side == WHITE) {
678       gg->bTimeWhenReceivedMove = 0;
679     } else {
680       gg->wTimeWhenReceivedMove = 0;
681     }
682
683     game_update_time(g);
684     game_add_increment(pp, gg);
685
686     /* Do the move */
687     gg->numHalfMoves++;
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);
691     }
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
698       if(gg->link < 0) { 
699         partner = g; // pieces stay with current board
700         if(gg->game_state.holdings == -1) victim ^= WHITE|BLACK; // flip color
701       } 
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
704       else
705         update_holding(partner, victim);
706     }
707     now = tenth_secs();
708     move.atTime = now;
709     if (gg->numHalfMoves > 1) {
710       move.tookTime = move.atTime - gg->lastMoveTime;
711     } else {
712       move.tookTime = move.atTime - gg->startTime;
713     }
714     gg->lastMoveTime = now;
715     gg->lastDecTime = now;
716     move.wTime = gg->wTime;
717     move.bTime = gg->bTime;
718
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;
723       } else {
724         move.tookTime = (game_globals.garray[pp->game].bTimeWhenMoved -
725                          game_globals.garray[pp->game].bTimeWhenReceivedMove) / 100;
726       }
727     }
728
729     if (gg->numHalfMoves <= 2) {
730             move.tookTime = 0;
731     }
732
733     MakeFENpos(g, move.FENpos);
734     gg->moveList[gg->numHalfMoves - 1] = move;
735   }
736
737   send_boards(g);
738
739   if (result == MOVE_ILLEGAL) {
740     pprintf(p, "Internal error, illegal move accepted!\n");
741   }
742   if ((result == MOVE_OK) && (gg->status == GAME_EXAMINE)) {
743     int p1;
744
745     for (p1 = 0; p1 < player_globals.p_num; p1++) {
746       if (player_globals.parray[p1].status != PLAYER_PROMPT)
747         continue;
748       if (player_is_observe(p1, g) || player_globals.parray[p1].game == g) {
749         pprintf(p1, "%s moves: %s\n", pp->name, move.algString);
750       }
751     }
752   }
753   if (result == MOVE_CHECKMATE) {
754     if (gg->status == GAME_EXAMINE) {
755       int p1;
756
757       for (p1 = 0; p1 < player_globals.p_num; p1++) {
758         if (player_globals.parray[p1].status != PLAYER_PROMPT)
759           continue;
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");
763         }
764       }
765     } else {
766       game_ended(g, CToggle(gg->game_state.onMove), END_CHECKMATE);
767     }
768   }
769   if (result == MOVE_STALEMATE) {
770     if (gg->status == GAME_EXAMINE) {
771       int p1;
772
773       for (p1 = 0; p1 < player_globals.p_num; p1++) {
774         if (player_globals.parray[p1].status != PLAYER_PROMPT)
775           continue;
776         if (player_is_observe(p1, g) || player_globals.parray[p1].game == g) {
777           pprintf(p1, "Stalemate.\n");
778         }
779       }
780     } else {
781       game_ended(g, CToggle(gg->game_state.onMove), END_STALEMATE);
782     }
783   }
784   if (result == MOVE_NOMATERIAL) {
785     if (gg->status == GAME_EXAMINE) {
786       int p1;
787
788       for (p1 = 0; p1 < player_globals.p_num; p1++) {
789         if (player_globals.parray[p1].status != PLAYER_PROMPT)
790           continue;
791         if (player_is_observe(p1, g) || player_globals.parray[p1].game == g) {
792           pprintf(p1, "No mating material.\n");
793         }
794       }
795     } else {
796       game_ended(g, CToggle(gg->game_state.onMove), END_NOMATERIAL);
797     }
798   }
799   if (result == MOVE_BARE) {
800     if (gg->status == GAME_EXAMINE) {
801       int p1;
802
803       for (p1 = 0; p1 < player_globals.p_num; p1++) {
804         if (player_globals.parray[p1].status != PLAYER_PROMPT)
805           continue;
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");
809         }
810       }
811     } else {
812       game_ended(g, gg->game_state.onMove, END_BARE);
813     }
814   }
815 }
816
817 int com_resign(int p, param_list param)
818 {
819   struct player *pp = &player_globals.parray[p];
820   int g, o, oconnected;
821
822   if (param[0].type == TYPE_NULL) {
823     g = pp->game;
824     if (!pIsPlaying(p))
825       return COM_OK;
826     else {
827       decline_withdraw_offers(p, -1, -1, DO_DECLINE);
828       game_ended(g, (game_globals.garray[g].white == p) ? BLACK : WHITE, END_RESIGN);
829     }
830   } else if (FindPlayer(p, param[0].val.word, &o, &oconnected)) {
831     g = game_new();
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);
835         if (!oconnected)
836           player_remove(o);
837         return COM_OK;
838       } else {
839         game_globals.garray[g].white = o;
840         game_globals.garray[g].black = p;
841       }
842     } else {
843       game_globals.garray[g].white = p;
844       game_globals.garray[g].black = o;
845     }
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);
853     if (!oconnected)
854       player_remove(o);
855   }
856   return COM_OK;
857 }
858
859 static int Check50MoveRule (int p, int g)
860 {
861   int num_reversible = game_globals.garray[g].numHalfMoves;
862
863   if (game_globals.garray[g].game_state.lastIrreversable >= 0) {
864     num_reversible -= game_globals.garray[g].game_state.lastIrreversable;
865   }
866   if (num_reversible > 99) {
867     game_ended(g, (game_globals.garray[g].white == p) ? BLACK : WHITE, END_50MOVERULE);
868     return 1;
869   }
870   return 0;
871 }
872
873 static char *GetFENpos (int g, int half_move)
874 {
875   if (half_move < 0)
876     return game_globals.garray[g].FENstartPos;
877   else return game_globals.garray[g].moveList[half_move].FENpos;
878 }
879
880 static int CheckRepetition (int p, int g)
881 {
882   struct player *pp = &player_globals.parray[p];
883   struct pending* pend;
884   int move_num;
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);
888   char *pos;
889
890   if (game_globals.garray[g].numHalfMoves < 8)  /* can't have three repeats any quicker. */
891     return 0;
892
893   for (move_num = game_globals.garray[g].game_state.lastIrreversable - 1; // [HGM] FEN stored in moveList[numHalfMoves-1] !
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))
897       flag1++;
898     if (strlen(pos2) == strlen(pos) && !strcmp(pos2, pos))
899       flag2++;
900 printf("%2d. %d-%d %s %s %s\n", move_num, flag1, flag2, pos1,pos2,pos);
901   }
902   if (flag1 >= 3 || flag2 >= 3) {
903     if ((pend = find_pend(pp->opponent, p, PEND_DRAW)) != NULL) {
904       delete_pending(pend);
905       decline_withdraw_offers(p, -1, -1,DO_DECLINE);
906     }
907     game_ended(g, (game_globals.garray[g].white == p) ? BLACK : WHITE, END_REPETITION);
908     return 1;
909   }
910   else return 0;
911 }
912
913 int com_draw(int p, param_list param)
914 {
915   struct player *pp = &player_globals.parray[p];
916   struct pending* pend;
917   int p1, g = pp->game;
918
919   if (!pIsPlaying(p)) {
920     return COM_OK;
921   }
922   if (Check50MoveRule (p, g) || CheckRepetition(p, g)) {
923     return COM_OK;
924   }
925   p1 = pp->opponent;
926
927   if ((player_globals.parray[p1].simul_info != NULL) && (player_globals.parray[p1].simul_info->numBoards &&
928         player_globals.parray[p1].simul_info->boards[player_globals.parray[p1].simul_info->onBoard] != g)) {
929     pprintf(p, "You can only make requests when the simul player is at your board.\n");
930     return COM_OK;
931   }
932
933   if ((pend = (find_pend(pp->opponent, p, PEND_DRAW))) != NULL) {
934     delete_pending(pend);
935     decline_withdraw_offers(p, -1, -1,DO_DECLINE);
936     game_ended(g, (game_globals.garray[g].white == p) ? BLACK : WHITE, END_AGREEDDRAW);
937   } else {
938     pprintf(pp->opponent, "\n");
939     pprintf_highlight(pp->opponent, "%s", pp->name);
940     pprintf_prompt(pp->opponent, " offers you a draw.\n");
941     pprintf(p, "Draw request sent.\n");
942     add_request(p, pp->opponent, PEND_DRAW);
943   }
944   return COM_OK;
945 }
946
947 int com_pause(int p, param_list param)
948 {
949   struct player *pp = &player_globals.parray[p];
950   int g, now;
951   struct pending* pend;
952
953   if (!pIsPlaying(p)) {
954     return COM_OK;
955   }
956   g = pp->game;
957   if (game_globals.garray[g].wTime == 0) {
958     pprintf(p, "You can't pause untimed games.\n");
959     return COM_OK;
960   }
961   if (game_globals.garray[g].clockStopped) {
962     pprintf(p, "Game is already paused, use \"unpause\" to resume.\n");
963     return COM_OK;
964   }
965   if ((pend = find_pend(pp->opponent, p, PEND_PAUSE)) != NULL) {
966     delete_pending(pend);
967     game_globals.garray[g].clockStopped = 1;
968     /* Roll back the time */
969     if (game_globals.garray[g].game_state.onMove == WHITE) {
970       game_globals.garray[g].wTime += (game_globals.garray[g].lastDecTime - game_globals.garray[g].lastMoveTime);
971     } else {
972       game_globals.garray[g].bTime += (game_globals.garray[g].lastDecTime - game_globals.garray[g].lastMoveTime);
973     }
974     now = tenth_secs();
975     if (game_globals.garray[g].numHalfMoves == 0)
976       game_globals.garray[g].timeOfStart = now;
977     game_globals.garray[g].lastMoveTime = now;
978     game_globals.garray[g].lastDecTime = now;
979     send_boards(g);
980     pprintf_prompt(pp->opponent, "\n%s accepted pause. Game clock paused.\n",
981                    pp->name);
982     pprintf(p, "Game clock paused.\n");
983   } else {
984     pprintf(pp->opponent, "\n");
985     pprintf_highlight(pp->opponent, "%s", pp->name);
986     pprintf_prompt(pp->opponent, " requests to pause the game.\n");
987     pprintf(p, "Pause request sent.\n");
988     add_request(p, pp->opponent, PEND_PAUSE);
989   }
990   return COM_OK;
991 }
992
993 int com_unpause(int p, param_list param)
994 {
995   struct player *pp = &player_globals.parray[p];
996   int g;
997   int now;
998   struct pending* pend;
999
1000   if (!pIsPlaying(p)) {
1001     return COM_OK;
1002   }
1003
1004   g = pp->game;
1005
1006   if (!game_globals.garray[g].clockStopped) {
1007     pprintf(p, "Game is not paused.\n");
1008     return COM_OK;
1009   }
1010   if ((pend = find_pend(pp->opponent, p, PEND_UNPAUSE)) != NULL) {
1011     delete_pending(pend);
1012     game_globals.garray[g].clockStopped = 0;
1013     now = tenth_secs();
1014     if (game_globals.garray[g].numHalfMoves == 0)
1015       game_globals.garray[g].timeOfStart = now;
1016     game_globals.garray[g].lastMoveTime = now;
1017     game_globals.garray[g].lastDecTime = now;
1018     send_boards(g);
1019     pprintf(p, "Game clock resumed.\n");
1020     pprintf_prompt(pp->opponent, "\nGame clock resumed.\n");
1021   } else {
1022     pprintf(pp->opponent, "\n");
1023     pprintf_highlight(pp->opponent, "%s", pp->name);
1024     pprintf_prompt(pp->opponent, " requests to unpause the game.\n");
1025     pprintf(p, "Unpause request sent.\n");
1026     add_request(p, pp->opponent, PEND_UNPAUSE);
1027   }
1028   return COM_OK;
1029 }
1030
1031 int com_abort(int p, param_list param)
1032 {
1033   struct player *pp = &player_globals.parray[p];
1034   struct pending* pend;
1035   int p1, g, myColor, yourColor, myGTime, yourGTime;
1036   int courtesyOK = 1;
1037
1038   g = pp->game;
1039   if (!pIsPlaying(p))
1040     return COM_OK;
1041
1042   p1 = pp->opponent;
1043   if (p == game_globals.garray[g].white) {
1044     myColor = WHITE;
1045     yourColor = BLACK;
1046     myGTime = game_globals.garray[g].wTime;
1047     yourGTime = game_globals.garray[g].bTime;
1048   } else {
1049     myColor = BLACK;
1050     yourColor = WHITE;
1051     myGTime = game_globals.garray[g].bTime;
1052     yourGTime = game_globals.garray[g].wTime;
1053   }
1054   if ((player_globals.parray[p1].simul_info != NULL) && 
1055      (player_globals.parray[p1].simul_info->numBoards &&
1056         player_globals.parray[p1].simul_info->boards[player_globals.parray[p1].simul_info->onBoard] != g)) {
1057     pprintf(p, "You can only make requests when the simul player is at your board.\n");
1058     return COM_OK;
1059   }
1060   if ((pend = find_pend(p1, p, PEND_ABORT)) != NULL) {
1061     delete_pending(pend);
1062     decline_withdraw_offers(p, -1, -1,DO_DECLINE);
1063     game_ended(g, yourColor, END_ABORT);
1064   } else {
1065     game_update_time(g);
1066
1067     if (net_globals.con[pp->socket]->timeseal
1068         && game_globals.garray[g].game_state.onMove == myColor
1069         && game_globals.garray[g].flag_pending == FLAG_ABORT) {
1070       /* It's my move, opponent has asked for abort; I lagged out,
1071          my timeseal prevented courtesyabort, and I am sending an abort
1072          request before acknowledging (and processing) my opponent's
1073          courtesyabort.  OK, let's abort already :-). */
1074       decline_withdraw_offers(p, -1, -1,DO_DECLINE);
1075       game_ended(g, yourColor, END_ABORT);
1076     }
1077
1078     if (net_globals.con[player_globals.parray[p1].socket]->timeseal) {  /* opp uses timeseal? */
1079
1080       int yourRealTime = (myColor == WHITE  ?  game_globals.garray[g].bRealTime
1081                                             :  game_globals.garray[g].wRealTime);
1082       if (myGTime > 0 && yourGTime <= 0 && yourRealTime > 0) {
1083         /* Override courtesyabort; opponent still has time.  Check for lag. */
1084         courtesyOK = 0;
1085
1086         if (game_globals.garray[g].game_state.onMove != myColor
1087             && game_globals.garray[g].flag_pending != FLAG_CHECKING) {
1088           /* Opponent may be lagging; let's ask. */
1089           game_globals.garray[g].flag_pending = FLAG_ABORT;
1090           game_globals.garray[g].flag_check_time = time(0);
1091           pprintf(p, "Opponent has timeseal; trying to courtesyabort.\n");
1092           pprintf(p1, "\n[G]\n");
1093           return COM_OK;
1094         }
1095       }
1096     }
1097
1098     if (myGTime > 0 && yourGTime <= 0 && courtesyOK) {
1099       /* player wants to abort + opponent is out of time = courtesyabort */
1100       pprintf(p, "Since you have time, and your opponent has none, the game has been aborted.");
1101       pprintf(p1, "Your opponent has aborted the game rather than calling your flag.");
1102       decline_withdraw_offers(p, -1, -1, DO_DECLINE);
1103       game_ended(g, myColor, END_COURTESY);
1104     } else {
1105       pprintf(p1, "\n");
1106       pprintf_highlight(p1, "%s", pp->name);
1107       pprintf(p1, " would like to abort the game; ");
1108       pprintf_prompt(p1, "type \"abort\" to accept.\n");
1109       pprintf(p, "Abort request sent.\n");
1110       add_request(p, p1, PEND_ABORT);
1111     }
1112   }
1113   return COM_OK;
1114 }
1115
1116 static int player_has_mating_material(struct game_state_t *gs, int color)
1117 {
1118   int i, j;
1119   int piece;
1120   int minor_pieces = 0;
1121
1122   for (i = 0; i < gs->files; i++)
1123     for (j = 0; j < gs->ranks; j++) {
1124       piece = gs->board[i][j];
1125       switch (piecetype(piece)) {
1126       case BISHOP:
1127       case KNIGHT:
1128         if (iscolor(piece, color))
1129           minor_pieces++;
1130         break;
1131       case KING:
1132       case NOPIECE:
1133         break;
1134       default:
1135         if (iscolor(piece, color))
1136           return 1;
1137       }
1138     }
1139   return ((minor_pieces > 1) ? 1 : 0);
1140 }
1141
1142 int com_flag(int p, param_list param)
1143 {
1144         struct player *pp = &player_globals.parray[p];
1145         struct game *gg;
1146         int g;
1147         int myColor;
1148
1149         if (!pIsPlaying(p)) {
1150                 return COM_OK;
1151         }
1152         g = pp->game;
1153
1154         gg = &game_globals.garray[g];
1155
1156         myColor = (p == gg->white ? WHITE : BLACK);
1157         if (gg->type == TYPE_UNTIMED) {
1158                 pprintf(p, "You can't flag an untimed game.\n");
1159                 return COM_OK;
1160         }
1161         if (gg->numHalfMoves < 2) {
1162                 pprintf(p, "You cannot flag before both players have moved.\nUse abort instead.\n");
1163                 return COM_OK;
1164         }
1165         game_update_time(g);
1166         
1167         {
1168                 int myTime, yourTime, opp = pp->opponent, serverTime;
1169                 
1170                 if (net_globals.con[pp->socket]->timeseal) {    /* does caller use timeseal? */
1171                         myTime = (myColor==WHITE?gg->wRealTime:gg->bRealTime);
1172                 } else {
1173                         myTime = (myColor == WHITE?gg->wTime:gg->bTime);
1174                 }
1175                 serverTime = (myColor == WHITE?gg->bTime:gg->wTime);
1176                 
1177                 if (net_globals.con[player_globals.parray[opp].socket]->timeseal) {     /* opp uses timeseal? */
1178                         yourTime = (myColor == WHITE?gg->bRealTime:gg->wRealTime);
1179                 } else {
1180                         yourTime = serverTime;
1181                 }
1182
1183                 /* the clocks to compare are now in myTime and yourTime */
1184                 if ((myTime <= 0) && (yourTime <= 0)) {
1185                         decline_withdraw_offers(p, -1, -1,DO_DECLINE);
1186                         game_ended(g, myColor, END_BOTHFLAG);
1187                         return COM_OK;
1188                 }
1189
1190                 if (yourTime > 0) {
1191                         /* Opponent still has time, but if that's only because s/he
1192                          * may be lagging, we should ask for an acknowledgement and then
1193                          * try to call the flag. */
1194                         
1195                         if (serverTime <= 0 && gg->game_state.onMove != myColor
1196                             && gg->flag_pending != FLAG_CHECKING) {                             
1197                                 /* server time thinks opponent is down, but RealTIme disagrees.
1198                                  * ask client to acknowledge it's alive. */                             
1199                                 gg->flag_pending = FLAG_CALLED;
1200                                 gg->flag_check_time = time(0);
1201                                 pprintf(p, "Opponent has timeseal; checking if (s)he's lagging.\n");
1202                                 pprintf (opp, "\n[G]\n");
1203                                 return COM_OK;
1204                         }
1205                         
1206                         /* if we're here, it means one of:
1207                          * 1. the server agrees opponent has time, whether lagging or not.
1208                          * 2. opp. has timeseal (if yourTime != serverTime), had time left
1209                          *    after the last move (yourTime > 0), and it's still your move.
1210                          * 3. we're currently checking a flag call after having receiving
1211                          *    acknowledgement from the other timeseal (and would have reset
1212                          *    yourTime if the flag were down). */
1213                         
1214                         pprintf(p, "Your opponent is not out of time!\n");
1215                         return COM_OK;
1216                 }
1217         }
1218         
1219         decline_withdraw_offers(p, -1, -1,DO_DECLINE);
1220         if (player_has_mating_material(&gg->game_state, myColor))
1221                 game_ended(g, myColor, END_FLAG);
1222         else
1223                 game_ended(g, myColor, END_FLAGNOMATERIAL);
1224         return COM_OK;
1225 }
1226
1227 int com_adjourn(int p, param_list param)
1228 {
1229   struct player *pp = &player_globals.parray[p];
1230   struct pending* pend;
1231   int p1, g, myColor, yourColor;
1232
1233   if (!pIsPlaying(p))
1234     return COM_OK;
1235
1236   p1 = pp->opponent;
1237   g = pp->game;
1238   if (!CheckPFlag(p, PFLAG_REG) || !CheckPFlag(p, PFLAG_REG)) {
1239     pprintf(p, "Both players must be registered to adjourn a game.  Use \"abort\".\n");
1240     return COM_OK;
1241   }
1242   if (game_globals.garray[g].link >= 0) {
1243     pprintf(p, "Bughouse games cannot be adjourned.\n");
1244     return COM_OK;
1245   }
1246   myColor = (p == game_globals.garray[g].white ? WHITE : BLACK);
1247   yourColor = (myColor == WHITE ? BLACK : WHITE);
1248
1249   if ((pend = find_pend(p1, p, PEND_ADJOURN)) != NULL) {
1250     delete_pending(pend);
1251     decline_withdraw_offers(p, -1, -1,DO_DECLINE);
1252     game_ended(pp->game, yourColor, END_ADJOURN);
1253   } else {
1254     game_update_time(g);
1255     if (((myColor == WHITE) && (game_globals.garray[g].wTime > 0) && (game_globals.garray[g].bTime <= 0))
1256         || ((myColor == BLACK) && (game_globals.garray[g].bTime > 0) && (game_globals.garray[g].wTime <= 0))) {
1257 /* player wants to adjourn + opponent is out of time = courtesyadjourn */
1258       pprintf(p, "Since you have time, and your opponent has none, the game has been adjourned.");
1259       pprintf(p1, "Your opponent has adjourned the game rather than calling your flag.");
1260       decline_withdraw_offers(p, -1, -1,DO_DECLINE);
1261       game_ended(g, myColor, END_COURTESYADJOURN);
1262     } else {
1263       pprintf(p1, "\n");
1264       pprintf_highlight(p1, "%s", pp->name);
1265       pprintf(p1, " would like to adjourn the game; ");
1266       pprintf_prompt(p1, "type \"adjourn\" to accept.\n");
1267       pprintf(p, "Adjourn request sent.\n");
1268       add_request(p, p1, PEND_ADJOURN);
1269     }
1270   }
1271   return COM_OK;
1272 }
1273
1274 int com_takeback(int p, param_list param)
1275 {
1276   struct player *pp = &player_globals.parray[p];
1277   int nHalfMoves = 1, g, i, p1, pend_half_moves;
1278   struct pending* from;
1279
1280   if (!pIsPlaying(p))
1281     return COM_OK;
1282
1283   p1 = pp->opponent;
1284   if ((player_globals.parray[p1].simul_info != NULL) && 
1285      (player_globals.parray[p1].simul_info->numBoards &&
1286         player_globals.parray[p1].simul_info->boards[player_globals.parray[p1].simul_info->onBoard] !=
1287         pp->game)) {
1288     pprintf(p, "You can only make requests when the simul player is at your board.\n");
1289     return COM_OK;
1290   }
1291
1292   g = pp->game;
1293   if (game_globals.garray[g].link >= 0) {
1294     pprintf(p, "Takeback not implemented for bughouse games yet.\n");
1295     return COM_OK;
1296   }
1297   if (param[0].type == TYPE_INT) {
1298     nHalfMoves = param[0].val.integer;
1299     if (nHalfMoves <= 0) {
1300       pprintf (p,"You can't takeback less than 1 move.\n");
1301       return COM_OK;
1302     }
1303   }
1304   if ((from = find_pend(pp->opponent, p, PEND_TAKEBACK)) != NULL) {
1305     pend_half_moves = from->wtime;
1306     delete_pending(from);
1307     if (pend_half_moves == nHalfMoves) {
1308       /* Doing the takeback */
1309       decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1310       for (i = 0; i < nHalfMoves; i++) {
1311         if (backup_move(g, REL_GAME) != MOVE_OK) {
1312           pprintf(game_globals.garray[g].white, "Can only backup %d moves\n", i);
1313           pprintf(game_globals.garray[g].black, "Can only backup %d moves\n", i);
1314           break;
1315         }
1316       }
1317
1318       game_globals.garray[g].wTimeWhenReceivedMove = 0;
1319       game_globals.garray[g].bTimeWhenReceivedMove = 0;
1320
1321       send_boards(g);
1322     } else {
1323
1324       if (!game_globals.garray[g].numHalfMoves) {
1325         pprintf(p, "There are no moves in your game.\n");
1326         pprintf_prompt(pp->opponent, "\n%s has declined the takeback request.\n", 
1327                        pp->name);
1328         return COM_OK;
1329       }
1330  
1331       if (game_globals.garray[g].numHalfMoves < nHalfMoves) {
1332         pprintf(p, "There are only %d half moves in your game.\n", game_globals.garray[g].numHalfMoves);
1333         pprintf_prompt(pp->opponent, "\n%s has declined the takeback request.\n", 
1334                        pp->name);
1335         return COM_OK;
1336       }
1337       pprintf(p, "You disagree on the number of half-moves to takeback.\n");
1338       pprintf(p, "Alternate takeback request sent.\n");
1339       pprintf_prompt(pp->opponent, "\n%s proposes a different number (%d) of half-move(s).\n", pp->name, nHalfMoves);
1340       from = add_request(p, pp->opponent, PEND_TAKEBACK);
1341       from->wtime = nHalfMoves;
1342     }
1343   } else {
1344
1345     if (!game_globals.garray[g].numHalfMoves) {
1346       pprintf(p, "There are no moves in your game.\n");
1347       return COM_OK;
1348     }
1349     if (game_globals.garray[g].numHalfMoves < nHalfMoves) {
1350       pprintf(p, "There are only %d half moves in your game.\n", game_globals.garray[g].numHalfMoves);
1351       return COM_OK;
1352     }
1353     pprintf(pp->opponent, "\n");
1354     pprintf_highlight(pp->opponent, "%s", pp->name);
1355     pprintf_prompt(pp->opponent, " would like to take back %d half move(s).\n",
1356            nHalfMoves);
1357     pprintf(p, "Takeback request sent.\n");
1358     from = add_request(p, pp->opponent, PEND_TAKEBACK);
1359     from->wtime = nHalfMoves;
1360   }
1361   return COM_OK;
1362 }
1363
1364
1365 int com_switch(int p, param_list param)
1366 {
1367   struct player *pp = &player_globals.parray[p];
1368   int g = pp->game, tmp, now, p1;
1369   char *strTmp;
1370   struct pending* pend;
1371
1372   if (!pIsPlaying(p))
1373     return COM_OK;
1374
1375   p1 = pp->opponent;
1376   if ((player_globals.parray[p1].simul_info != NULL) && (player_globals.parray[p1].simul_info->numBoards &&
1377         player_globals.parray[p1].simul_info->boards[player_globals.parray[p1].simul_info->onBoard] != g)) {
1378     pprintf(p, "You can only make requests when the simul player is at your board.\n");
1379     return COM_OK;
1380   }
1381
1382   if (game_globals.garray[g].link >= 0) {
1383     pprintf(p, "Switch not implemented for bughouse games.\n");
1384     return COM_OK;
1385   }
1386   if ((pend = find_pend(pp->opponent, p, PEND_SWITCH)) != NULL) {
1387     delete_pending(pend);
1388     /* Doing the switch */
1389     decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1390
1391     tmp = game_globals.garray[g].white;
1392     game_globals.garray[g].white = game_globals.garray[g].black;
1393     game_globals.garray[g].black = tmp;
1394     pp->side = (pp->side == WHITE) ? BLACK : WHITE;
1395     strTmp = strdup(game_globals.garray[g].white_name);
1396     strcpy(game_globals.garray[g].white_name, game_globals.garray[g].black_name);
1397     strcpy(game_globals.garray[g].black_name, strTmp);
1398     free(strTmp);
1399
1400     player_globals.parray[pp->opponent].side =
1401       (player_globals.parray[pp->opponent].side == WHITE) ? BLACK : WHITE;
1402     /* Roll back the time */
1403     if (game_globals.garray[g].game_state.onMove == WHITE) {
1404       game_globals.garray[g].wTime += (game_globals.garray[g].lastDecTime - game_globals.garray[g].lastMoveTime);
1405     } else {
1406       game_globals.garray[g].bTime += (game_globals.garray[g].lastDecTime - game_globals.garray[g].lastMoveTime);
1407     }
1408     now = tenth_secs();
1409     if (game_globals.garray[g].numHalfMoves == 0)
1410       game_globals.garray[g].timeOfStart = now;
1411     game_globals.garray[g].lastMoveTime = now;
1412     game_globals.garray[g].lastDecTime = now;
1413     send_boards(g);
1414     return COM_OK;
1415   }
1416   if (game_globals.garray[g].rated && game_globals.garray[g].numHalfMoves > 0) {
1417     pprintf(p, "You cannot switch sides once a rated game is underway.\n");
1418     return COM_OK;
1419   }
1420   pprintf(pp->opponent, "\n");
1421   pprintf_highlight(pp->opponent, "%s", pp->name);
1422   pprintf_prompt(pp->opponent, " would like to switch sides.\nType \"accept\" to switch sides, or \"decline\" to refuse.\n");
1423   pprintf(p, "Switch request sent.\n");
1424   add_request(p, pp->opponent, PEND_SWITCH);
1425   return COM_OK;
1426 }
1427
1428 int com_time(int p, param_list param)
1429 {
1430   struct player *pp = &player_globals.parray[p];
1431   int p1, g;
1432
1433   if (param[0].type == TYPE_NULL) {
1434     g = pp->game;
1435     if (!pIsPlaying(p))
1436       return COM_OK;
1437   } else {
1438     g = GameNumFromParam(p, &p1, &param[0]);
1439     if (g < 0)
1440       return COM_OK;
1441   }
1442   if ((g < 0) || (g >= game_globals.g_num) || (game_globals.garray[g].status != GAME_ACTIVE)) {
1443     pprintf(p, "There is no such game.\n");
1444     return COM_OK;
1445   }
1446   game_update_time(g);
1447   pprintf(p, "White (%s) : %d mins, %d secs\n",
1448           player_globals.parray[game_globals.garray[g].white].name,
1449           game_globals.garray[g].wTime / 600,
1450           (game_globals.garray[g].wTime - ((game_globals.garray[g].wTime / 600) * 600)) / 10);
1451   pprintf(p, "Black (%s) : %d mins, %d secs\n",
1452           player_globals.parray[game_globals.garray[g].black].name,
1453           game_globals.garray[g].bTime / 600,
1454           (game_globals.garray[g].bTime - ((game_globals.garray[g].bTime / 600) * 600)) / 10);
1455   return COM_OK;
1456 }
1457
1458 int com_ptime(int p, param_list param)
1459 {
1460   struct player *pp = &player_globals.parray[p];
1461   int retval, part = pp->partner;
1462
1463   if (part < 0) {
1464     pprintf(p, "You do not have a partner.\n");
1465     return COM_OK;
1466   }
1467   retval = pcommand (p, "time %s", player_globals.parray[part].name);
1468   if (retval == COM_OK)
1469     return COM_OK_NOPROMPT;
1470   else
1471     return retval;
1472 }
1473
1474 int com_boards(int p, param_list param)
1475 {
1476   char *category = NULL;
1477   char dname[MAX_FILENAME_SIZE];
1478   DIR *dirp;
1479   struct dirent *dp;
1480
1481   if (param[0].type == TYPE_WORD)
1482     category = param[0].val.word;
1483   if (category) {
1484     pprintf(p, "Boards Available For Category %s:\n", category);
1485     sprintf(dname, "%s/%s", BOARD_DIR, category);
1486   } else {
1487     pprintf(p, "Categories Available:\n");
1488     sprintf(dname, "%s", BOARD_DIR);
1489   }
1490   dirp = opendir(dname);
1491   if (!dirp) {
1492     pprintf(p, "No such category %s, try \"boards\".\n", category);
1493     return COM_OK;
1494   }
1495
1496 /* YUK! what a mess, how about printing an ordered directory? - DAV*/
1497
1498   for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
1499     if (!strcmp(dp->d_name, "."))
1500       continue;
1501     if (!strcmp(dp->d_name, ".."))
1502       continue;
1503     pprintf(p, "%s\n", dp->d_name);
1504   }
1505   closedir(dirp);
1506   return COM_OK;
1507 }
1508
1509 int com_simmatch(int p, param_list param)
1510 {
1511   struct player *pp = &player_globals.parray[p];
1512   int p1, g, adjourned;
1513   int num;
1514   char tmp[100];
1515   struct pending* pend;
1516   char* board = NULL;
1517   char* category = NULL;
1518   char fname[MAX_FILENAME_SIZE];
1519
1520   if (pp->game >=0) {
1521     if (game_globals.garray[pp->game].status == GAME_EXAMINE) {
1522       pprintf(p, "You are still examining a game.\n");
1523       return COM_OK;
1524     }
1525     if (game_globals.garray[pp->game].status == GAME_SETUP) {
1526       pprintf(p, "You are still setting up a position.\n");
1527       return COM_OK;
1528     }
1529   } 
1530   p1 = player_find_part_login(param[0].val.word);
1531   if (p1 < 0) {
1532     pprintf(p, "No user named \"%s\" is logged in.\n", param[0].val.word);
1533     return COM_OK;
1534   }
1535   if (p == p1) {
1536     pprintf(p, "You can't simmatch yourself!\n");
1537     return COM_OK;
1538   }
1539   if ((pend = find_pend(p1, p, PEND_SIMUL)) != NULL) {
1540
1541     /* Accepting Simul ! */
1542
1543     if ((pp->simul_info != NULL) && 
1544        (pp->simul_info->numBoards >= MAX_SIMUL)) {
1545       pprintf(p, "You are already playing the maximum of %d boards.\n", MAX_SIMUL);
1546       pprintf(p1, "Simul request removed, boards filled.\n");
1547       delete_pending(pend);
1548       return COM_OK;
1549     }
1550     unobserveAll(p);            /* stop observing when match starts */
1551     unobserveAll(p1);
1552
1553     g = game_new();
1554     adjourned = 0;
1555     if (game_read(g, p, p1) >= 0) {
1556       adjourned = 1;
1557       delete_pending(pend);
1558     }
1559
1560     if (!adjourned) {           /* no adjourned game, so begin a new game */
1561
1562       if ((pend->category != NULL) && (pend->board_type != NULL)) {
1563         board = strdup(pend->category);
1564         category = strdup(pend->board_type);
1565       } 
1566
1567       delete_pending(pend);
1568
1569       if (create_new_match(g,p, p1, 0, 0, 0, 0, 0, ((board == NULL) ? "\0" : board), ((category == NULL) ? "\0" : category), 1,1) == COM_FAILED) {
1570         pprintf(p, "There was a problem creating the new match.\n");
1571         pprintf_prompt(p1, "There was a problem creating the new match.\n");
1572         game_remove(g);
1573
1574         if (board != NULL) {
1575           free (board);
1576           free (category);
1577         }
1578         return COM_OK;
1579       }
1580
1581       if (board != NULL) {
1582         free (board);
1583         free (category);
1584       }
1585
1586     } else {                    /* resume adjourned game */
1587       game_delete(p, p1);
1588
1589       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]);
1590       pprintf(p, tmp);
1591       pprintf(p1, tmp);
1592
1593       game_globals.garray[g].white = p;
1594       game_globals.garray[g].black = p1;
1595       game_globals.garray[g].status = GAME_ACTIVE;
1596       game_globals.garray[g].startTime = tenth_secs();
1597       game_globals.garray[g].lastMoveTime = game_globals.garray[g].startTime;
1598       game_globals.garray[g].lastDecTime = game_globals.garray[g].startTime;
1599       pp->game = g;
1600       pp->opponent = p1;
1601       pp->side = WHITE;
1602       player_globals.parray[p1].game = g;
1603       player_globals.parray[p1].opponent = p;
1604       player_globals.parray[p1].side = BLACK;
1605       send_boards(g);
1606     }
1607
1608     if (pp->simul_info == NULL) {
1609       pp->simul_info = (struct simul_info_t *) malloc(sizeof(struct simul_info_t));
1610       pp->simul_info->numBoards = 0;
1611       pp->simul_info->onBoard = 0;
1612       pp->simul_info->num_wins = pp->simul_info->num_draws
1613         = pp->simul_info->num_losses = 0;
1614     }
1615     num = pp->simul_info->numBoards;
1616     /*    pp->simul_info->results[num] = -1; */
1617     pp->simul_info->boards[num] = pp->game;
1618     pp->simul_info->numBoards++;
1619     if (pp->simul_info->numBoards > 1 &&
1620         pp->simul_info->onBoard >= 0)
1621       player_goto_board(p, pp->simul_info->onBoard);
1622     else
1623       pp->simul_info->onBoard = 0;
1624     return COM_OK;
1625   }
1626   if (find_pend(-1, p, PEND_SIMUL) != NULL) {
1627     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");
1628     return COM_OK;
1629   }
1630   if (pp->simul_info != NULL) {
1631     if (pp->simul_info->numBoards) {
1632       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");
1633       return COM_OK;
1634     }
1635   }
1636   if (pp->game >=0) {
1637     pprintf(p, "You are already playing a game.\n");
1638     return COM_OK;
1639   }
1640   if (!CheckPFlag(p1, PFLAG_SIMOPEN)) {
1641     pprintf_highlight(p, "%s", player_globals.parray[p1].name);
1642     pprintf(p, " is not open to receiving simul requests.\n");
1643     return COM_OK;
1644   }
1645   if ((player_globals.parray[p1].simul_info != NULL) && (player_globals.parray[p1].simul_info->numBoards >= MAX_SIMUL)) {
1646     pprintf_highlight(p, "%s", player_globals.parray[p1].name);
1647     pprintf(p, " is already playing the maximum of %d boards.\n", MAX_SIMUL);
1648     return COM_OK;
1649   }
1650
1651 /* loon: checking for some crazy situations we can't allow :) */
1652
1653   if ((player_globals.parray[p1].simul_info != NULL) && (player_globals.parray[p1].game >=0) && (player_globals.parray[p1].simul_info->numBoards == 0)) {
1654     pprintf_highlight(p, "%s", player_globals.parray[p1].name);
1655     if (player_globals.parray[game_globals.garray[player_globals.parray[p1].game].white].simul_info->numBoards) {
1656       pprintf(p, " is playing in ");
1657       pprintf_highlight(p, "%s", player_globals.parray[player_globals.parray[p1].opponent].name);
1658       pprintf(p, "'s simul, and can't accept.\n");
1659     } else {
1660       pprintf(p, " can't begin a simul while playing a non-simul game.\n");
1661     }
1662     return COM_OK;
1663   }
1664
1665   g = game_new();               /* Check if an adjourned untimed game */
1666   adjourned = ((game_read(g, p, p1) < 0) && (game_read(g, p1, p) < 0)) ? 0 : 1;
1667   if (adjourned) {
1668     if (!(game_globals.garray[g].type == TYPE_UNTIMED))
1669       adjourned = 0;
1670   }
1671   game_remove(g);
1672
1673   pend = add_request(p, p1, PEND_SIMUL);
1674
1675   if ((param[1].type == TYPE_WORD) && (param[2].type == TYPE_WORD)) {
1676
1677     sprintf(fname, "%s/%s/%s", BOARD_DIR, param[1].val.word , param[2].val.word);
1678     if (!file_exists(fname)) {
1679       pprintf(p, "No such category/board: %s/%s\n", param[1].val.word , param[2].val.word);
1680       return COM_OK;
1681     }
1682     pend->category = strdup(param[1].val.word);
1683     pend->board_type = strdup(param[2].val.word);
1684   } else {
1685     pend->category = NULL;
1686     pend->board_type = NULL;
1687   }
1688  
1689   pprintf(p1, "\n");
1690   pprintf_highlight(p1, "%s", pp->name);
1691   if (adjourned) {
1692     pprintf_prompt(p1, " requests to continue an adjourned simul game.\n");
1693     pprintf(p, "Request to resume simul sent. Adjourned game found.\n");
1694   } else {
1695     if (pend->category == NULL)
1696       pprintf_prompt(p1, " requests to join a simul match with you.\n");
1697     else
1698       pprintf_prompt(p1, " requests to join a %s %s simul match with you.\n",
1699                 pend->category,pend->board_type);
1700     pprintf(p, "Simul match request sent.\n");
1701   }
1702   return COM_OK;
1703 }
1704
1705 int com_goboard(int p, param_list param)
1706 {
1707   struct player *pp = &player_globals.parray[p];
1708   int on, g, p1, gamenum;
1709
1710   if (pp->simul_info == NULL) {
1711     pprintf(p, "You are not giving a simul.\n");
1712     return COM_OK;
1713   }
1714
1715   if (!pp->simul_info->numBoards) {
1716     pprintf(p, "You are not giving a simul.\n");
1717     return COM_OK;
1718   }
1719
1720   if (param[0].type == TYPE_WORD) {
1721
1722     p1 = player_find_part_login(param[0].val.word);
1723     if (p1 < 0) {
1724       pprintf(p, "No user named \"%s\" is logged in.\n", param[0].val.word);
1725       return COM_OK;
1726     }
1727     if (p == p1) {
1728       pprintf(p, "You can't goboard yourself!\n");
1729       return COM_OK;
1730     }
1731
1732     gamenum = player_globals.parray[p1].game;
1733     if (gamenum < 0) {
1734       pprintf (p,"%s is not playing a game.\n", player_globals.parray[p1].login);
1735       return COM_OK;
1736     }
1737
1738   } else { 
1739     gamenum = param[0].val.integer - 1;
1740     if (gamenum < 0)
1741       gamenum = 0;
1742   }
1743
1744   on = pp->simul_info->onBoard;
1745   g = pp->simul_info->boards[on];
1746   if (gamenum == g) {
1747     pprintf(p, "You are already at that board!\n");
1748     return COM_OK;
1749   }
1750   if (pp->simul_info->numBoards > 1) {
1751     decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1752     if (player_goto_simulgame_bynum(p, gamenum) !=-1) {
1753       if (g >= 0) {
1754         pprintf(game_globals.garray[g].black, "\n");
1755         pprintf_highlight(game_globals.garray[g].black, "%s", pp->name);
1756         pprintf_prompt(game_globals.garray[g].black, " has moved away from your board.\n");
1757       }
1758     } else
1759     pprintf(p, "You are not playing that game/person.\n");
1760   } else
1761     pprintf(p, "You are only playing one board!\n");
1762   return COM_OK;
1763 }
1764
1765 int com_simnext(int p, param_list param)
1766 {
1767   struct player *pp = &player_globals.parray[p];
1768   int on, g;
1769
1770   if (pp->simul_info == NULL) {
1771     pprintf(p, "You are not giving a simul.\n");
1772     return COM_OK;
1773   }
1774
1775   if (!pp->simul_info->numBoards) {
1776     pprintf(p, "You are not giving a simul.\n");
1777     return COM_OK;
1778   }
1779
1780   if (pp->simul_info->numBoards > 1) {
1781     decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1782     on = pp->simul_info->onBoard;
1783     g = pp->simul_info->boards[on];
1784     if (g >= 0) {
1785       pprintf(game_globals.garray[g].black, "\n");
1786       pprintf_highlight(game_globals.garray[g].black, "%s", pp->name);
1787       pprintf_prompt(game_globals.garray[g].black, " is moving away from your board.\n");
1788       player_goto_next_board(p);
1789     }
1790   } else
1791     pprintf(p, "You are only playing one board!\n");
1792   return COM_OK;
1793 }
1794
1795 int com_simprev(int p, param_list param)
1796 {
1797   struct player *pp = &player_globals.parray[p];
1798   int on, g;
1799
1800   if (pp->simul_info == NULL) {
1801     pprintf(p, "You are not giving a simul.\n");
1802     return COM_OK;
1803   }
1804
1805   if (!pp->simul_info->numBoards) {
1806     pprintf(p, "You are not giving a simul.\n");
1807     return COM_OK;
1808   }
1809   if (pp->simul_info->numBoards > 1) {
1810     decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1811     on = pp->simul_info->onBoard;
1812     g = pp->simul_info->boards[on];
1813     if (g >= 0) {
1814       pprintf(game_globals.garray[g].black, "\n");
1815       pprintf_highlight(game_globals.garray[g].black, "%s", pp->name);
1816       pprintf_prompt(game_globals.garray[g].black, " is moving back to the previous board.\n");
1817     }
1818     player_goto_prev_board(p);
1819   } else
1820     pprintf(p, "You are only playing one board!\n");
1821   return COM_OK;
1822 }
1823
1824 int com_simgames(int p, param_list param)
1825 {
1826   int p1 = p;
1827
1828   if (param[0].type == TYPE_WORD) {
1829     if ((p1 = player_find_part_login(param[0].val.word)) < 0) {
1830       pprintf(p, "No player named %s is logged in.\n", param[0].val.word);
1831       return COM_OK;
1832     }
1833   }
1834   if (p1 == p)
1835     pprintf(p, "You are playing %d simultaneous games.\n",
1836             player_num_active_boards(p1));
1837   else
1838     pprintf(p, "%s is playing %d simultaneous games.\n", player_globals.parray[p1].name,
1839             player_num_active_boards(p1));
1840   return COM_OK;
1841 }
1842
1843 int com_simpass(int p, param_list param)
1844 {
1845   struct player *pp = &player_globals.parray[p];
1846   int g, p1, on;
1847
1848   if (!pIsPlaying(p))
1849     return COM_OK;
1850
1851   g = pp->game;
1852   p1 = game_globals.garray[g].white;
1853
1854   if (player_globals.parray[p1].simul_info == NULL) {
1855     pprintf(p, "You are not participating in a simul.\n");
1856     return COM_OK;
1857   }
1858
1859   if (!player_globals.parray[p1].simul_info->numBoards) {
1860     pprintf(p, "You are not participating in a simul.\n");
1861     return COM_OK;
1862   }
1863   if (p == p1) {
1864     pprintf(p, "You are the simul holder and cannot pass!\n");
1865     return COM_OK;
1866   }
1867   if (player_num_active_boards(p1) == 1) {
1868     pprintf(p, "This is the only game, so passing is futile.\n");
1869     return COM_OK;
1870   }
1871   on = player_globals.parray[p1].simul_info->onBoard;
1872   if (player_globals.parray[p1].simul_info->boards[on] != g) {
1873     pprintf(p, "You cannot pass until the simul holder arrives!\n");
1874     return COM_OK;
1875   }
1876   if (game_globals.garray[g].passes >= MAX_SIMPASS) {
1877     Bell (p);
1878     pprintf(p, "You have reached your maximum of %d pass(es).\n", MAX_SIMPASS);
1879     pprintf(p, "Please move IMMEDIATELY!\n");
1880     pprintf_highlight(p1, "%s", pp->name);
1881     pprintf_prompt(p1, " tried to pass, but is out of passes.\n");
1882     return COM_OK;
1883   }
1884   decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1885
1886   game_globals.garray[g].passes++;
1887   pprintf(p, "You have passed and have %d pass(es) left.\n",
1888           (MAX_SIMPASS - game_globals.garray[g].passes));
1889   pprintf_highlight(p1, "%s", pp->name);
1890   pprintf_prompt(p1, " has decided to pass and has %d pass(es) left.\n",
1891                  (MAX_SIMPASS - game_globals.garray[g].passes));
1892   player_goto_next_board(p1);
1893   return COM_OK;
1894 }
1895
1896 int com_simabort(int p, param_list param)
1897 {
1898   struct player *pp = &player_globals.parray[p];
1899
1900   if (pp->simul_info == NULL) {
1901     pprintf(p, "You are not giving a simul.\n");
1902     return COM_OK;
1903   }
1904
1905   if (!pp->simul_info->numBoards) {
1906     pprintf(p, "You are not giving a simul.\n");
1907     return COM_OK;
1908   }
1909   decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1910   game_ended(pp->simul_info->boards[pp->simul_info->onBoard],
1911              WHITE, END_ABORT);
1912   return COM_OK;
1913 }
1914
1915 int com_simallabort(int p, param_list param)
1916 {
1917   struct player *pp = &player_globals.parray[p];
1918   int i;
1919
1920   if (pp->simul_info == NULL) {
1921     pprintf(p, "You are not giving a simul.\n");
1922     return COM_OK;
1923   }
1924
1925   if (!pp->simul_info->numBoards) {
1926     pprintf(p, "You are not giving a simul.\n");
1927     return COM_OK;
1928   }
1929
1930   decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1931   for (i = 0; i < pp->simul_info->numBoards; i++)
1932     if (pp->simul_info->boards[i] >= 0)
1933       game_ended(pp->simul_info->boards[i],
1934                  WHITE, END_ABORT);
1935
1936   return COM_OK;
1937 }
1938
1939 int com_simadjourn(int p, param_list param)
1940 {
1941   struct player *pp = &player_globals.parray[p];
1942
1943   if (pp->simul_info == NULL) {
1944     pprintf(p, "You are not giving a simul.\n");
1945     return COM_OK;
1946   }
1947
1948   if (!pp->simul_info->numBoards) {
1949     pprintf(p, "You are not giving a simul.\n");
1950     return COM_OK;
1951   }
1952   decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1953   game_ended(pp->simul_info->boards[pp->simul_info->onBoard],
1954              WHITE, END_ADJOURN);
1955   return COM_OK;
1956 }
1957
1958 int com_simalladjourn(int p, param_list param)
1959 {
1960   struct player *pp = &player_globals.parray[p];
1961   int i;
1962
1963   if (pp->simul_info == NULL) {
1964     pprintf(p, "You are not giving a simul.\n");
1965     return COM_OK;
1966   }
1967
1968   if (!pp->simul_info->numBoards) {
1969     pprintf(p, "You are not giving a simul.\n");
1970     return COM_OK;
1971   }
1972   decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1973   for (i = 0; i < pp->simul_info->numBoards; i++)
1974     if (pp->simul_info->boards[i] >= 0)
1975       game_ended(pp->simul_info->boards[i],
1976                  WHITE, END_ADJOURN);
1977
1978   return COM_OK;
1979 }
1980
1981 int com_moretime(int p, param_list param)
1982 {
1983   struct player *pp = &player_globals.parray[p];
1984   int g, increment;
1985
1986   if ((pp->game >=0) &&((game_globals.garray[pp->game].status == GAME_EXAMINE) ||
1987         (game_globals.garray[pp->game].status == GAME_SETUP))) {
1988     pprintf(p, "You cannot use moretime in an examined game.\n");
1989     return COM_OK;
1990   }
1991   increment = param[0].val.integer;
1992   if (increment <= 0) {
1993     pprintf(p, "Moretime requires an integer value greater than zero.\n");
1994     return COM_OK;
1995   }
1996   if (!pIsPlaying(p))
1997     return COM_OK;
1998  
1999   if (increment > 600) {
2000     pprintf(p, "Moretime has a maximum limit of 600 seconds.\n");
2001     increment = 600;
2002   }
2003   g = pp->game;
2004   if (game_globals.garray[g].white == p) {
2005     game_globals.garray[g].bTime += increment * 10;
2006     game_globals.garray[g].bRealTime += increment * 10 * 100;
2007     pprintf(p, "%d seconds were added to your opponents clock\n",
2008             increment);
2009     pprintf_prompt(pp->opponent,
2010                    "\nYour opponent has added %d seconds to your clock.\n",
2011                    increment);
2012   }
2013   if (game_globals.garray[g].black == p) {
2014     game_globals.garray[g].wTime += increment * 10;;
2015     game_globals.garray[g].wRealTime += increment * 10 * 100;
2016     pprintf(p, "%d seconds were added to your opponents clock\n",
2017             increment);
2018     pprintf_prompt(pp->opponent,
2019                    "\nYour opponent has added %d seconds to your clock.\n",
2020                    increment);
2021   }
2022   return COM_OK;
2023 }
2024