e6e59bdd02639ea846e4e1280cfbf6afd5d79e2d
[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, f;
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] == '=' || gg->game_state.drops == 2 && command[len - 2] == '/') { // [HGM] encode gating as promotion
565 printf("promo '%s'\n", command);
566       switch (tolower(command[len - 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 'h':
613         pp->promote = DRAGONHORSE;
614         break;
615       case 'd':
616         pp->promote = DRAGONKING;
617         break;
618       case '^':
619       case '+':
620         pp->promote = GOLD;
621         break;
622       case '=':
623         pp->promote = NOPIECE;
624         break;
625       default:
626         pprintf(p, "Don't understand that move.\n");
627         return;
628         break;
629       }
630     }
631   }
632
633   switch (parse_move(command, &gg->game_state, &move, pp->promote)) {
634   case MOVE_ILLEGAL:
635     pprintf(p, "Illegal move.\n");
636     return;
637     break;
638   case MOVE_AMBIGUOUS:
639     pprintf(p, "Ambiguous move.\n");
640     return;
641     break;
642   default:
643     break;
644   }
645
646   if (gg->status == GAME_EXAMINE) {
647     gg->numHalfMoves++;
648     if (gg->numHalfMoves > gg->examMoveListSize) {
649       gg->examMoveListSize += 20;       /* Allocate 20 moves at a time */
650       gg->examMoveList = (struct move_t *) realloc(gg->examMoveList, sizeof(struct move_t) * gg->examMoveListSize);
651     }
652     result = execute_move(&gg->game_state, &move, 1);
653     move.atTime = now;
654     move.tookTime = 0;
655     MakeFENpos(g, move.FENpos);
656     gg->examMoveList[gg->numHalfMoves - 1] = move;
657     /* roll back time */
658     if (gg->game_state.onMove == WHITE) {
659       gg->wTime += (gg->lastDecTime - gg->lastMoveTime);
660     } else {
661       gg->bTime += (gg->lastDecTime - gg->lastMoveTime);
662     }
663     now = tenth_secs();
664     if (gg->numHalfMoves == 0)
665       gg->timeOfStart = now;
666     gg->lastMoveTime = now;
667     gg->lastDecTime = now;
668
669   } else {                      /* real game */
670     i = pp->opponent;
671     if ((player_globals.parray[i].simul_info != NULL) && (player_globals.parray[i].simul_info->numBoards &&
672          (player_globals.parray[i].simul_info->boards[player_globals.parray[i].simul_info->onBoard] != g))) {
673       pprintf(p, "It isn't your turn: wait until the simul giver is at your board.\n");
674       return;
675     }
676     if (net_globals.con[pp->socket]->timeseal) {        /* does he use timeseal? */
677             timeseal_update_clocks(pp, &game_globals.garray[g]);
678     }
679     /* we need to reset the opp's time for receiving the board since the
680        timeseal decoder only alters the time if it's 0 Otherwise the time
681        would be changed if the player did a refresh which would screw up
682        the timings */
683     if (pp->side == WHITE) {
684       gg->bTimeWhenReceivedMove = 0;
685     } else {
686       gg->wTimeWhenReceivedMove = 0;
687     }
688
689     game_update_time(g);
690     game_add_increment(pp, gg);
691
692     /* Do the move */
693     gg->numHalfMoves++;
694     if (gg->numHalfMoves > gg->moveListSize) {
695       gg->moveListSize += 20;   /* Allocate 20 moves at a time */
696       gg->moveList = (struct move_t *) realloc(gg->moveList, sizeof(struct move_t) * gg->moveListSize);
697     }
698     result = execute_move(&gg->game_state, &move, 1);
699     if (result == MOVE_OK && (gg->link >= 0 || gg->game_state.holdings) && move.pieceCaptured != NOPIECE) {
700       /* transfer captured piece to partner */
701       /* check if piece reverts to a pawn */
702       int victim = move.pieceCaptured, partner = gg->link, demoted;
703       // [HGM] zh: if not Bughouse, the game_state.holdings field decides what happens
704       if(gg->link < 0) { 
705         partner = g; // pieces stay with current board
706         if(gg->game_state.holdings == -1) victim ^= WHITE|BLACK; // flip color
707       } 
708       if (demoted = was_promoted(&game_globals.garray[g], move.toFile, move.toRank))
709         update_holding(partner, colorval(victim) | demoted); // [HGM] was_promoted now returns original piece type
710       else
711         update_holding(partner, victim);
712     }
713     now = tenth_secs();
714     move.atTime = now;
715     if (gg->numHalfMoves > 1) {
716       move.tookTime = move.atTime - gg->lastMoveTime;
717     } else {
718       move.tookTime = move.atTime - gg->startTime;
719     }
720     gg->lastMoveTime = now;
721     gg->lastDecTime = now;
722     move.wTime = gg->wTime;
723     move.bTime = gg->bTime;
724
725     if (net_globals.con[pp->socket]->timeseal) {        /* does he use timeseal? */
726       if (pp->side == WHITE) {
727         move.tookTime = (game_globals.garray[pp->game].wTimeWhenMoved -
728                          game_globals.garray[pp->game].wTimeWhenReceivedMove) / 100;
729       } else {
730         move.tookTime = (game_globals.garray[pp->game].bTimeWhenMoved -
731                          game_globals.garray[pp->game].bTimeWhenReceivedMove) / 100;
732       }
733     }
734
735     if (gg->numHalfMoves <= 2) {
736             move.tookTime = 0;
737     }
738
739     MakeFENpos(g, move.FENpos);
740     gg->moveList[gg->numHalfMoves - 1] = move;
741   }
742
743   send_boards(g);
744
745   if (result == MOVE_ILLEGAL) {
746     pprintf(p, "Internal error, illegal move accepted!\n");
747   }
748   if ((result == MOVE_OK) && (gg->status == GAME_EXAMINE)) {
749     int p1;
750
751     for (p1 = 0; p1 < player_globals.p_num; p1++) {
752       if (player_globals.parray[p1].status != PLAYER_PROMPT)
753         continue;
754       if (player_is_observe(p1, g) || player_globals.parray[p1].game == g) {
755         pprintf(p1, "%s moves: %s\n", pp->name, move.algString);
756       }
757     }
758   }
759   if (result == MOVE_CHECKMATE) {
760     if (gg->status == GAME_EXAMINE) {
761       int p1;
762
763       for (p1 = 0; p1 < player_globals.p_num; p1++) {
764         if (player_globals.parray[p1].status != PLAYER_PROMPT)
765           continue;
766         if (player_is_observe(p1, g) || player_globals.parray[p1].game == g) {
767           pprintf(p1, "%s has been checkmated.\n",
768                   (CToggle(gg->game_state.onMove) == BLACK) ? "White" : "Black");
769         }
770       }
771     } else {
772       game_ended(g, CToggle(gg->game_state.onMove), END_CHECKMATE);
773     }
774   }
775   if (result == MOVE_STALEMATE) {
776     if (gg->status == GAME_EXAMINE) {
777       int p1;
778
779       for (p1 = 0; p1 < player_globals.p_num; p1++) {
780         if (player_globals.parray[p1].status != PLAYER_PROMPT)
781           continue;
782         if (player_is_observe(p1, g) || player_globals.parray[p1].game == g) {
783           pprintf(p1, "Stalemate.\n");
784         }
785       }
786     } else {
787       game_ended(g, CToggle(gg->game_state.onMove), END_STALEMATE);
788     }
789   }
790   if (result == MOVE_NOMATERIAL) {
791     if (gg->status == GAME_EXAMINE) {
792       int p1;
793
794       for (p1 = 0; p1 < player_globals.p_num; p1++) {
795         if (player_globals.parray[p1].status != PLAYER_PROMPT)
796           continue;
797         if (player_is_observe(p1, g) || player_globals.parray[p1].game == g) {
798           pprintf(p1, "No mating material.\n");
799         }
800       }
801     } else {
802       game_ended(g, CToggle(gg->game_state.onMove), END_NOMATERIAL);
803     }
804   }
805   if (result == MOVE_BARE) {
806     if (gg->status == GAME_EXAMINE) {
807       int p1;
808
809       for (p1 = 0; p1 < player_globals.p_num; p1++) {
810         if (player_globals.parray[p1].status != PLAYER_PROMPT)
811           continue;
812         if (player_is_observe(p1, g) || player_globals.parray[p1].game == g) {
813           pprintf(p1, "%s bared.\n",
814                   (gg->game_state.onMove == BLACK) ? "White" : "Black");
815         }
816       }
817     } else {
818       game_ended(g, gg->game_state.onMove, END_BARE);
819     }
820   }
821 }
822
823 int com_resign(int p, param_list param)
824 {
825   struct player *pp = &player_globals.parray[p];
826   int g, o, oconnected;
827
828   if (param[0].type == TYPE_NULL) {
829     g = pp->game;
830     if (!pIsPlaying(p))
831       return COM_OK;
832     else {
833       decline_withdraw_offers(p, -1, -1, DO_DECLINE);
834       game_ended(g, (game_globals.garray[g].white == p) ? BLACK : WHITE, END_RESIGN);
835     }
836   } else if (FindPlayer(p, param[0].val.word, &o, &oconnected)) {
837     g = game_new();
838     if (game_read(g, p, o) < 0) {
839       if (game_read(g, o, p) < 0) {
840         pprintf(p, "You have no stored game with %s\n", player_globals.parray[o].name);
841         if (!oconnected)
842           player_remove(o);
843         return COM_OK;
844       } else {
845         game_globals.garray[g].white = o;
846         game_globals.garray[g].black = p;
847       }
848     } else {
849       game_globals.garray[g].white = p;
850       game_globals.garray[g].black = o;
851     }
852     pprintf(p, "You resign your stored game with %s\n", player_globals.parray[o].name);
853     pcommand(p, "message %s I have resigned our stored game \"%s vs. %s.\"",
854              player_globals.parray[o].name,
855              player_globals.parray[game_globals.garray[g].white].name,
856              player_globals.parray[game_globals.garray[g].black].name);
857     game_delete(game_globals.garray[g].white, game_globals.garray[g].black);
858     game_ended(g, (game_globals.garray[g].white == p) ? BLACK : WHITE, END_RESIGN);
859     if (!oconnected)
860       player_remove(o);
861   }
862   return COM_OK;
863 }
864
865 static int Check50MoveRule (int p, int g)
866 {
867   int num_reversible = game_globals.garray[g].numHalfMoves;
868
869   if (game_globals.garray[g].game_state.lastIrreversable >= 0) {
870     num_reversible -= game_globals.garray[g].game_state.lastIrreversable;
871   }
872   if (num_reversible > 99) {
873     game_ended(g, (game_globals.garray[g].white == p) ? BLACK : WHITE, END_50MOVERULE);
874     return 1;
875   }
876   return 0;
877 }
878
879 static char *GetFENpos (int g, int half_move)
880 {
881   if (half_move < 0)
882     return game_globals.garray[g].FENstartPos;
883   else return game_globals.garray[g].moveList[half_move].FENpos;
884 }
885
886 static int CheckRepetition (int p, int g)
887 {
888   struct player *pp = &player_globals.parray[p];
889   struct pending* pend;
890   int move_num;
891   int flag1 = 1, flag2 = 1;
892   char *pos1 = GetFENpos (g, game_globals.garray[g].numHalfMoves - 1);
893   char *pos2 = GetFENpos (g, game_globals.garray[g].numHalfMoves);
894   char *pos;
895
896   if (game_globals.garray[g].numHalfMoves < 8)  /* can't have three repeats any quicker. */
897     return 0;
898
899   for (move_num = game_globals.garray[g].game_state.lastIrreversable - 1; // [HGM] FEN stored in moveList[numHalfMoves-1] !
900        move_num < game_globals.garray[g].numHalfMoves - 1; move_num++) {
901     pos = GetFENpos (g, move_num);
902     if (strlen(pos1) == strlen(pos) && !strcmp(pos1, pos))
903       flag1++;
904     if (strlen(pos2) == strlen(pos) && !strcmp(pos2, pos))
905       flag2++;
906 printf("%2d. %d-%d %s %s %s\n", move_num, flag1, flag2, pos1,pos2,pos);
907   }
908   if (flag1 >= 3 || flag2 >= 3) {
909     if ((pend = find_pend(pp->opponent, p, PEND_DRAW)) != NULL) {
910       delete_pending(pend);
911       decline_withdraw_offers(p, -1, -1,DO_DECLINE);
912     }
913     game_ended(g, (game_globals.garray[g].white == p) ? BLACK : WHITE, END_REPETITION);
914     return 1;
915   }
916   else return 0;
917 }
918
919 int com_draw(int p, param_list param)
920 {
921   struct player *pp = &player_globals.parray[p];
922   struct pending* pend;
923   int p1, g = pp->game;
924
925   if (!pIsPlaying(p)) {
926     return COM_OK;
927   }
928   if (Check50MoveRule (p, g) || CheckRepetition(p, g)) {
929     return COM_OK;
930   }
931   p1 = pp->opponent;
932
933   if ((player_globals.parray[p1].simul_info != NULL) && (player_globals.parray[p1].simul_info->numBoards &&
934         player_globals.parray[p1].simul_info->boards[player_globals.parray[p1].simul_info->onBoard] != g)) {
935     pprintf(p, "You can only make requests when the simul player is at your board.\n");
936     return COM_OK;
937   }
938
939   if ((pend = (find_pend(pp->opponent, p, PEND_DRAW))) != NULL) {
940     delete_pending(pend);
941     decline_withdraw_offers(p, -1, -1,DO_DECLINE);
942     game_ended(g, (game_globals.garray[g].white == p) ? BLACK : WHITE, END_AGREEDDRAW);
943   } else {
944     pprintf(pp->opponent, "\n");
945     pprintf_highlight(pp->opponent, "%s", pp->name);
946     pprintf_prompt(pp->opponent, " offers you a draw.\n");
947     pprintf(p, "Draw request sent.\n");
948     add_request(p, pp->opponent, PEND_DRAW);
949   }
950   return COM_OK;
951 }
952
953 int com_pause(int p, param_list param)
954 {
955   struct player *pp = &player_globals.parray[p];
956   int g, now;
957   struct pending* pend;
958
959   if (!pIsPlaying(p)) {
960     return COM_OK;
961   }
962   g = pp->game;
963   if (game_globals.garray[g].wTime == 0) {
964     pprintf(p, "You can't pause untimed games.\n");
965     return COM_OK;
966   }
967   if (game_globals.garray[g].clockStopped) {
968     pprintf(p, "Game is already paused, use \"unpause\" to resume.\n");
969     return COM_OK;
970   }
971   if ((pend = find_pend(pp->opponent, p, PEND_PAUSE)) != NULL) {
972     delete_pending(pend);
973     game_globals.garray[g].clockStopped = 1;
974     /* Roll back the time */
975     if (game_globals.garray[g].game_state.onMove == WHITE) {
976       game_globals.garray[g].wTime += (game_globals.garray[g].lastDecTime - game_globals.garray[g].lastMoveTime);
977     } else {
978       game_globals.garray[g].bTime += (game_globals.garray[g].lastDecTime - game_globals.garray[g].lastMoveTime);
979     }
980     now = tenth_secs();
981     if (game_globals.garray[g].numHalfMoves == 0)
982       game_globals.garray[g].timeOfStart = now;
983     game_globals.garray[g].lastMoveTime = now;
984     game_globals.garray[g].lastDecTime = now;
985     send_boards(g);
986     pprintf_prompt(pp->opponent, "\n%s accepted pause. Game clock paused.\n",
987                    pp->name);
988     pprintf(p, "Game clock paused.\n");
989   } else {
990     pprintf(pp->opponent, "\n");
991     pprintf_highlight(pp->opponent, "%s", pp->name);
992     pprintf_prompt(pp->opponent, " requests to pause the game.\n");
993     pprintf(p, "Pause request sent.\n");
994     add_request(p, pp->opponent, PEND_PAUSE);
995   }
996   return COM_OK;
997 }
998
999 int com_unpause(int p, param_list param)
1000 {
1001   struct player *pp = &player_globals.parray[p];
1002   int g;
1003   int now;
1004   struct pending* pend;
1005
1006   if (!pIsPlaying(p)) {
1007     return COM_OK;
1008   }
1009
1010   g = pp->game;
1011
1012   if (!game_globals.garray[g].clockStopped) {
1013     pprintf(p, "Game is not paused.\n");
1014     return COM_OK;
1015   }
1016   if ((pend = find_pend(pp->opponent, p, PEND_UNPAUSE)) != NULL) {
1017     delete_pending(pend);
1018     game_globals.garray[g].clockStopped = 0;
1019     now = tenth_secs();
1020     if (game_globals.garray[g].numHalfMoves == 0)
1021       game_globals.garray[g].timeOfStart = now;
1022     game_globals.garray[g].lastMoveTime = now;
1023     game_globals.garray[g].lastDecTime = now;
1024     send_boards(g);
1025     pprintf(p, "Game clock resumed.\n");
1026     pprintf_prompt(pp->opponent, "\nGame clock resumed.\n");
1027   } else {
1028     pprintf(pp->opponent, "\n");
1029     pprintf_highlight(pp->opponent, "%s", pp->name);
1030     pprintf_prompt(pp->opponent, " requests to unpause the game.\n");
1031     pprintf(p, "Unpause request sent.\n");
1032     add_request(p, pp->opponent, PEND_UNPAUSE);
1033   }
1034   return COM_OK;
1035 }
1036
1037 int com_abort(int p, param_list param)
1038 {
1039   struct player *pp = &player_globals.parray[p];
1040   struct pending* pend;
1041   int p1, g, myColor, yourColor, myGTime, yourGTime;
1042   int courtesyOK = 1;
1043
1044   g = pp->game;
1045   if (!pIsPlaying(p))
1046     return COM_OK;
1047
1048   p1 = pp->opponent;
1049   if (p == game_globals.garray[g].white) {
1050     myColor = WHITE;
1051     yourColor = BLACK;
1052     myGTime = game_globals.garray[g].wTime;
1053     yourGTime = game_globals.garray[g].bTime;
1054   } else {
1055     myColor = BLACK;
1056     yourColor = WHITE;
1057     myGTime = game_globals.garray[g].bTime;
1058     yourGTime = game_globals.garray[g].wTime;
1059   }
1060   if ((player_globals.parray[p1].simul_info != NULL) && 
1061      (player_globals.parray[p1].simul_info->numBoards &&
1062         player_globals.parray[p1].simul_info->boards[player_globals.parray[p1].simul_info->onBoard] != g)) {
1063     pprintf(p, "You can only make requests when the simul player is at your board.\n");
1064     return COM_OK;
1065   }
1066   if ((pend = find_pend(p1, p, PEND_ABORT)) != NULL) {
1067     delete_pending(pend);
1068     decline_withdraw_offers(p, -1, -1,DO_DECLINE);
1069     game_ended(g, yourColor, END_ABORT);
1070   } else {
1071     game_update_time(g);
1072
1073     if (net_globals.con[pp->socket]->timeseal
1074         && game_globals.garray[g].game_state.onMove == myColor
1075         && game_globals.garray[g].flag_pending == FLAG_ABORT) {
1076       /* It's my move, opponent has asked for abort; I lagged out,
1077          my timeseal prevented courtesyabort, and I am sending an abort
1078          request before acknowledging (and processing) my opponent's
1079          courtesyabort.  OK, let's abort already :-). */
1080       decline_withdraw_offers(p, -1, -1,DO_DECLINE);
1081       game_ended(g, yourColor, END_ABORT);
1082     }
1083
1084     if (net_globals.con[player_globals.parray[p1].socket]->timeseal) {  /* opp uses timeseal? */
1085
1086       int yourRealTime = (myColor == WHITE  ?  game_globals.garray[g].bRealTime
1087                                             :  game_globals.garray[g].wRealTime);
1088       if (myGTime > 0 && yourGTime <= 0 && yourRealTime > 0) {
1089         /* Override courtesyabort; opponent still has time.  Check for lag. */
1090         courtesyOK = 0;
1091
1092         if (game_globals.garray[g].game_state.onMove != myColor
1093             && game_globals.garray[g].flag_pending != FLAG_CHECKING) {
1094           /* Opponent may be lagging; let's ask. */
1095           game_globals.garray[g].flag_pending = FLAG_ABORT;
1096           game_globals.garray[g].flag_check_time = time(0);
1097           pprintf(p, "Opponent has timeseal; trying to courtesyabort.\n");
1098           pprintf(p1, "\n[G]\n");
1099           return COM_OK;
1100         }
1101       }
1102     }
1103
1104     if (myGTime > 0 && yourGTime <= 0 && courtesyOK) {
1105       /* player wants to abort + opponent is out of time = courtesyabort */
1106       pprintf(p, "Since you have time, and your opponent has none, the game has been aborted.");
1107       pprintf(p1, "Your opponent has aborted the game rather than calling your flag.");
1108       decline_withdraw_offers(p, -1, -1, DO_DECLINE);
1109       game_ended(g, myColor, END_COURTESY);
1110     } else {
1111       pprintf(p1, "\n");
1112       pprintf_highlight(p1, "%s", pp->name);
1113       pprintf(p1, " would like to abort the game; ");
1114       pprintf_prompt(p1, "type \"abort\" to accept.\n");
1115       pprintf(p, "Abort request sent.\n");
1116       add_request(p, p1, PEND_ABORT);
1117     }
1118   }
1119   return COM_OK;
1120 }
1121
1122 static int player_has_mating_material(struct game_state_t *gs, int color)
1123 {
1124   int i, j;
1125   int piece;
1126   int minor_pieces = 0;
1127
1128   for (i = 0; i < gs->files; i++)
1129     for (j = 0; j < gs->ranks; j++) {
1130       piece = gs->board[i][j];
1131       switch (piecetype(piece)) {
1132       case BISHOP:
1133       case KNIGHT:
1134         if (iscolor(piece, color))
1135           minor_pieces++;
1136         break;
1137       case KING:
1138       case NOPIECE:
1139         break;
1140       default:
1141         if (iscolor(piece, color))
1142           return 1;
1143       }
1144     }
1145   return ((minor_pieces > 1) ? 1 : 0);
1146 }
1147
1148 int com_flag(int p, param_list param)
1149 {
1150         struct player *pp = &player_globals.parray[p];
1151         struct game *gg;
1152         int g;
1153         int myColor;
1154
1155         if (!pIsPlaying(p)) {
1156                 return COM_OK;
1157         }
1158         g = pp->game;
1159
1160         gg = &game_globals.garray[g];
1161
1162         myColor = (p == gg->white ? WHITE : BLACK);
1163         if (gg->type == TYPE_UNTIMED) {
1164                 pprintf(p, "You can't flag an untimed game.\n");
1165                 return COM_OK;
1166         }
1167         if (gg->numHalfMoves < 2) {
1168                 pprintf(p, "You cannot flag before both players have moved.\nUse abort instead.\n");
1169                 return COM_OK;
1170         }
1171         game_update_time(g);
1172         
1173         {
1174                 int myTime, yourTime, opp = pp->opponent, serverTime;
1175                 
1176                 if (net_globals.con[pp->socket]->timeseal) {    /* does caller use timeseal? */
1177                         myTime = (myColor==WHITE?gg->wRealTime:gg->bRealTime);
1178                 } else {
1179                         myTime = (myColor == WHITE?gg->wTime:gg->bTime);
1180                 }
1181                 serverTime = (myColor == WHITE?gg->bTime:gg->wTime);
1182                 
1183                 if (net_globals.con[player_globals.parray[opp].socket]->timeseal) {     /* opp uses timeseal? */
1184                         yourTime = (myColor == WHITE?gg->bRealTime:gg->wRealTime);
1185                 } else {
1186                         yourTime = serverTime;
1187                 }
1188
1189                 /* the clocks to compare are now in myTime and yourTime */
1190                 if ((myTime <= 0) && (yourTime <= 0)) {
1191                         decline_withdraw_offers(p, -1, -1,DO_DECLINE);
1192                         game_ended(g, myColor, END_BOTHFLAG);
1193                         return COM_OK;
1194                 }
1195
1196                 if (yourTime > 0) {
1197                         /* Opponent still has time, but if that's only because s/he
1198                          * may be lagging, we should ask for an acknowledgement and then
1199                          * try to call the flag. */
1200                         
1201                         if (serverTime <= 0 && gg->game_state.onMove != myColor
1202                             && gg->flag_pending != FLAG_CHECKING) {                             
1203                                 /* server time thinks opponent is down, but RealTIme disagrees.
1204                                  * ask client to acknowledge it's alive. */                             
1205                                 gg->flag_pending = FLAG_CALLED;
1206                                 gg->flag_check_time = time(0);
1207                                 pprintf(p, "Opponent has timeseal; checking if (s)he's lagging.\n");
1208                                 pprintf (opp, "\n[G]\n");
1209                                 return COM_OK;
1210                         }
1211                         
1212                         /* if we're here, it means one of:
1213                          * 1. the server agrees opponent has time, whether lagging or not.
1214                          * 2. opp. has timeseal (if yourTime != serverTime), had time left
1215                          *    after the last move (yourTime > 0), and it's still your move.
1216                          * 3. we're currently checking a flag call after having receiving
1217                          *    acknowledgement from the other timeseal (and would have reset
1218                          *    yourTime if the flag were down). */
1219                         
1220                         pprintf(p, "Your opponent is not out of time!\n");
1221                         return COM_OK;
1222                 }
1223         }
1224         
1225         decline_withdraw_offers(p, -1, -1,DO_DECLINE);
1226         if (player_has_mating_material(&gg->game_state, myColor))
1227                 game_ended(g, myColor, END_FLAG);
1228         else
1229                 game_ended(g, myColor, END_FLAGNOMATERIAL);
1230         return COM_OK;
1231 }
1232
1233 int com_adjourn(int p, param_list param)
1234 {
1235   struct player *pp = &player_globals.parray[p];
1236   struct pending* pend;
1237   int p1, g, myColor, yourColor;
1238
1239   if (!pIsPlaying(p))
1240     return COM_OK;
1241
1242   p1 = pp->opponent;
1243   g = pp->game;
1244   if (!CheckPFlag(p, PFLAG_REG) || !CheckPFlag(p, PFLAG_REG)) {
1245     pprintf(p, "Both players must be registered to adjourn a game.  Use \"abort\".\n");
1246     return COM_OK;
1247   }
1248   if (game_globals.garray[g].link >= 0) {
1249     pprintf(p, "Bughouse games cannot be adjourned.\n");
1250     return COM_OK;
1251   }
1252   myColor = (p == game_globals.garray[g].white ? WHITE : BLACK);
1253   yourColor = (myColor == WHITE ? BLACK : WHITE);
1254
1255   if ((pend = find_pend(p1, p, PEND_ADJOURN)) != NULL) {
1256     delete_pending(pend);
1257     decline_withdraw_offers(p, -1, -1,DO_DECLINE);
1258     game_ended(pp->game, yourColor, END_ADJOURN);
1259   } else {
1260     game_update_time(g);
1261     if (((myColor == WHITE) && (game_globals.garray[g].wTime > 0) && (game_globals.garray[g].bTime <= 0))
1262         || ((myColor == BLACK) && (game_globals.garray[g].bTime > 0) && (game_globals.garray[g].wTime <= 0))) {
1263 /* player wants to adjourn + opponent is out of time = courtesyadjourn */
1264       pprintf(p, "Since you have time, and your opponent has none, the game has been adjourned.");
1265       pprintf(p1, "Your opponent has adjourned the game rather than calling your flag.");
1266       decline_withdraw_offers(p, -1, -1,DO_DECLINE);
1267       game_ended(g, myColor, END_COURTESYADJOURN);
1268     } else {
1269       pprintf(p1, "\n");
1270       pprintf_highlight(p1, "%s", pp->name);
1271       pprintf(p1, " would like to adjourn the game; ");
1272       pprintf_prompt(p1, "type \"adjourn\" to accept.\n");
1273       pprintf(p, "Adjourn request sent.\n");
1274       add_request(p, p1, PEND_ADJOURN);
1275     }
1276   }
1277   return COM_OK;
1278 }
1279
1280 int com_takeback(int p, param_list param)
1281 {
1282   struct player *pp = &player_globals.parray[p];
1283   int nHalfMoves = 1, g, i, p1, pend_half_moves;
1284   struct pending* from;
1285
1286   if (!pIsPlaying(p))
1287     return COM_OK;
1288
1289   p1 = pp->opponent;
1290   if ((player_globals.parray[p1].simul_info != NULL) && 
1291      (player_globals.parray[p1].simul_info->numBoards &&
1292         player_globals.parray[p1].simul_info->boards[player_globals.parray[p1].simul_info->onBoard] !=
1293         pp->game)) {
1294     pprintf(p, "You can only make requests when the simul player is at your board.\n");
1295     return COM_OK;
1296   }
1297
1298   g = pp->game;
1299   if (game_globals.garray[g].link >= 0) {
1300     pprintf(p, "Takeback not implemented for bughouse games yet.\n");
1301     return COM_OK;
1302   }
1303   if (param[0].type == TYPE_INT) {
1304     nHalfMoves = param[0].val.integer;
1305     if (nHalfMoves <= 0) {
1306       pprintf (p,"You can't takeback less than 1 move.\n");
1307       return COM_OK;
1308     }
1309   }
1310   if ((from = find_pend(pp->opponent, p, PEND_TAKEBACK)) != NULL) {
1311     pend_half_moves = from->wtime;
1312     delete_pending(from);
1313     if (pend_half_moves == nHalfMoves) {
1314       /* Doing the takeback */
1315       decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1316       for (i = 0; i < nHalfMoves; i++) {
1317         if (backup_move(g, REL_GAME) != MOVE_OK) {
1318           pprintf(game_globals.garray[g].white, "Can only backup %d moves\n", i);
1319           pprintf(game_globals.garray[g].black, "Can only backup %d moves\n", i);
1320           break;
1321         }
1322       }
1323
1324       game_globals.garray[g].wTimeWhenReceivedMove = 0;
1325       game_globals.garray[g].bTimeWhenReceivedMove = 0;
1326
1327       send_boards(g);
1328     } else {
1329
1330       if (!game_globals.garray[g].numHalfMoves) {
1331         pprintf(p, "There are no moves in your game.\n");
1332         pprintf_prompt(pp->opponent, "\n%s has declined the takeback request.\n", 
1333                        pp->name);
1334         return COM_OK;
1335       }
1336  
1337       if (game_globals.garray[g].numHalfMoves < nHalfMoves) {
1338         pprintf(p, "There are only %d half moves in your game.\n", game_globals.garray[g].numHalfMoves);
1339         pprintf_prompt(pp->opponent, "\n%s has declined the takeback request.\n", 
1340                        pp->name);
1341         return COM_OK;
1342       }
1343       pprintf(p, "You disagree on the number of half-moves to takeback.\n");
1344       pprintf(p, "Alternate takeback request sent.\n");
1345       pprintf_prompt(pp->opponent, "\n%s proposes a different number (%d) of half-move(s).\n", pp->name, nHalfMoves);
1346       from = add_request(p, pp->opponent, PEND_TAKEBACK);
1347       from->wtime = nHalfMoves;
1348     }
1349   } else {
1350
1351     if (!game_globals.garray[g].numHalfMoves) {
1352       pprintf(p, "There are no moves in your game.\n");
1353       return COM_OK;
1354     }
1355     if (game_globals.garray[g].numHalfMoves < nHalfMoves) {
1356       pprintf(p, "There are only %d half moves in your game.\n", game_globals.garray[g].numHalfMoves);
1357       return COM_OK;
1358     }
1359     pprintf(pp->opponent, "\n");
1360     pprintf_highlight(pp->opponent, "%s", pp->name);
1361     pprintf_prompt(pp->opponent, " would like to take back %d half move(s).\n",
1362            nHalfMoves);
1363     pprintf(p, "Takeback request sent.\n");
1364     from = add_request(p, pp->opponent, PEND_TAKEBACK);
1365     from->wtime = nHalfMoves;
1366   }
1367   return COM_OK;
1368 }
1369
1370
1371 int com_switch(int p, param_list param)
1372 {
1373   struct player *pp = &player_globals.parray[p];
1374   int g = pp->game, tmp, now, p1;
1375   char *strTmp;
1376   struct pending* pend;
1377
1378   if (!pIsPlaying(p))
1379     return COM_OK;
1380
1381   p1 = pp->opponent;
1382   if ((player_globals.parray[p1].simul_info != NULL) && (player_globals.parray[p1].simul_info->numBoards &&
1383         player_globals.parray[p1].simul_info->boards[player_globals.parray[p1].simul_info->onBoard] != g)) {
1384     pprintf(p, "You can only make requests when the simul player is at your board.\n");
1385     return COM_OK;
1386   }
1387
1388   if (game_globals.garray[g].link >= 0) {
1389     pprintf(p, "Switch not implemented for bughouse games.\n");
1390     return COM_OK;
1391   }
1392   if ((pend = find_pend(pp->opponent, p, PEND_SWITCH)) != NULL) {
1393     delete_pending(pend);
1394     /* Doing the switch */
1395     decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1396
1397     tmp = game_globals.garray[g].white;
1398     game_globals.garray[g].white = game_globals.garray[g].black;
1399     game_globals.garray[g].black = tmp;
1400     pp->side = (pp->side == WHITE) ? BLACK : WHITE;
1401     strTmp = strdup(game_globals.garray[g].white_name);
1402     strcpy(game_globals.garray[g].white_name, game_globals.garray[g].black_name);
1403     strcpy(game_globals.garray[g].black_name, strTmp);
1404     free(strTmp);
1405
1406     player_globals.parray[pp->opponent].side =
1407       (player_globals.parray[pp->opponent].side == WHITE) ? BLACK : WHITE;
1408     /* Roll back the time */
1409     if (game_globals.garray[g].game_state.onMove == WHITE) {
1410       game_globals.garray[g].wTime += (game_globals.garray[g].lastDecTime - game_globals.garray[g].lastMoveTime);
1411     } else {
1412       game_globals.garray[g].bTime += (game_globals.garray[g].lastDecTime - game_globals.garray[g].lastMoveTime);
1413     }
1414     now = tenth_secs();
1415     if (game_globals.garray[g].numHalfMoves == 0)
1416       game_globals.garray[g].timeOfStart = now;
1417     game_globals.garray[g].lastMoveTime = now;
1418     game_globals.garray[g].lastDecTime = now;
1419     send_boards(g);
1420     return COM_OK;
1421   }
1422   if (game_globals.garray[g].rated && game_globals.garray[g].numHalfMoves > 0) {
1423     pprintf(p, "You cannot switch sides once a rated game is underway.\n");
1424     return COM_OK;
1425   }
1426   pprintf(pp->opponent, "\n");
1427   pprintf_highlight(pp->opponent, "%s", pp->name);
1428   pprintf_prompt(pp->opponent, " would like to switch sides.\nType \"accept\" to switch sides, or \"decline\" to refuse.\n");
1429   pprintf(p, "Switch request sent.\n");
1430   add_request(p, pp->opponent, PEND_SWITCH);
1431   return COM_OK;
1432 }
1433
1434 int com_time(int p, param_list param)
1435 {
1436   struct player *pp = &player_globals.parray[p];
1437   int p1, g;
1438
1439   if (param[0].type == TYPE_NULL) {
1440     g = pp->game;
1441     if (!pIsPlaying(p))
1442       return COM_OK;
1443   } else {
1444     g = GameNumFromParam(p, &p1, &param[0]);
1445     if (g < 0)
1446       return COM_OK;
1447   }
1448   if ((g < 0) || (g >= game_globals.g_num) || (game_globals.garray[g].status != GAME_ACTIVE)) {
1449     pprintf(p, "There is no such game.\n");
1450     return COM_OK;
1451   }
1452   game_update_time(g);
1453   pprintf(p, "White (%s) : %d mins, %d secs\n",
1454           player_globals.parray[game_globals.garray[g].white].name,
1455           game_globals.garray[g].wTime / 600,
1456           (game_globals.garray[g].wTime - ((game_globals.garray[g].wTime / 600) * 600)) / 10);
1457   pprintf(p, "Black (%s) : %d mins, %d secs\n",
1458           player_globals.parray[game_globals.garray[g].black].name,
1459           game_globals.garray[g].bTime / 600,
1460           (game_globals.garray[g].bTime - ((game_globals.garray[g].bTime / 600) * 600)) / 10);
1461   return COM_OK;
1462 }
1463
1464 int com_ptime(int p, param_list param)
1465 {
1466   struct player *pp = &player_globals.parray[p];
1467   int retval, part = pp->partner;
1468
1469   if (part < 0) {
1470     pprintf(p, "You do not have a partner.\n");
1471     return COM_OK;
1472   }
1473   retval = pcommand (p, "time %s", player_globals.parray[part].name);
1474   if (retval == COM_OK)
1475     return COM_OK_NOPROMPT;
1476   else
1477     return retval;
1478 }
1479
1480 int com_boards(int p, param_list param)
1481 {
1482   char *category = NULL;
1483   char dname[MAX_FILENAME_SIZE];
1484   DIR *dirp;
1485   struct dirent *dp;
1486
1487   if (param[0].type == TYPE_WORD)
1488     category = param[0].val.word;
1489   if (category) {
1490     pprintf(p, "Boards Available For Category %s:\n", category);
1491     sprintf(dname, "%s/%s", BOARD_DIR, category);
1492   } else {
1493     pprintf(p, "Categories Available:\n");
1494     sprintf(dname, "%s", BOARD_DIR);
1495   }
1496   dirp = opendir(dname);
1497   if (!dirp) {
1498     pprintf(p, "No such category %s, try \"boards\".\n", category);
1499     return COM_OK;
1500   }
1501
1502 /* YUK! what a mess, how about printing an ordered directory? - DAV*/
1503
1504   for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
1505     if (!strcmp(dp->d_name, "."))
1506       continue;
1507     if (!strcmp(dp->d_name, ".."))
1508       continue;
1509     pprintf(p, "%s\n", dp->d_name);
1510   }
1511   closedir(dirp);
1512   return COM_OK;
1513 }
1514
1515 int com_simmatch(int p, param_list param)
1516 {
1517   struct player *pp = &player_globals.parray[p];
1518   int p1, g, adjourned;
1519   int num;
1520   char tmp[100];
1521   struct pending* pend;
1522   char* board = NULL;
1523   char* category = NULL;
1524   char fname[MAX_FILENAME_SIZE];
1525
1526   if (pp->game >=0) {
1527     if (game_globals.garray[pp->game].status == GAME_EXAMINE) {
1528       pprintf(p, "You are still examining a game.\n");
1529       return COM_OK;
1530     }
1531     if (game_globals.garray[pp->game].status == GAME_SETUP) {
1532       pprintf(p, "You are still setting up a position.\n");
1533       return COM_OK;
1534     }
1535   } 
1536   p1 = player_find_part_login(param[0].val.word);
1537   if (p1 < 0) {
1538     pprintf(p, "No user named \"%s\" is logged in.\n", param[0].val.word);
1539     return COM_OK;
1540   }
1541   if (p == p1) {
1542     pprintf(p, "You can't simmatch yourself!\n");
1543     return COM_OK;
1544   }
1545   if ((pend = find_pend(p1, p, PEND_SIMUL)) != NULL) {
1546
1547     /* Accepting Simul ! */
1548
1549     if ((pp->simul_info != NULL) && 
1550        (pp->simul_info->numBoards >= MAX_SIMUL)) {
1551       pprintf(p, "You are already playing the maximum of %d boards.\n", MAX_SIMUL);
1552       pprintf(p1, "Simul request removed, boards filled.\n");
1553       delete_pending(pend);
1554       return COM_OK;
1555     }
1556     unobserveAll(p);            /* stop observing when match starts */
1557     unobserveAll(p1);
1558
1559     g = game_new();
1560     adjourned = 0;
1561     if (game_read(g, p, p1) >= 0) {
1562       adjourned = 1;
1563       delete_pending(pend);
1564     }
1565
1566     if (!adjourned) {           /* no adjourned game, so begin a new game */
1567
1568       if ((pend->category != NULL) && (pend->board_type != NULL)) {
1569         board = strdup(pend->category);
1570         category = strdup(pend->board_type);
1571       } 
1572
1573       delete_pending(pend);
1574
1575       if (create_new_match(g,p, p1, 0, 0, 0, 0, 0, ((board == NULL) ? "\0" : board), ((category == NULL) ? "\0" : category), 1,1) == COM_FAILED) {
1576         pprintf(p, "There was a problem creating the new match.\n");
1577         pprintf_prompt(p1, "There was a problem creating the new match.\n");
1578         game_remove(g);
1579
1580         if (board != NULL) {
1581           free (board);
1582           free (category);
1583         }
1584         return COM_OK;
1585       }
1586
1587       if (board != NULL) {
1588         free (board);
1589         free (category);
1590       }
1591
1592     } else {                    /* resume adjourned game */
1593       game_delete(p, p1);
1594
1595       sprintf(tmp, "{Game %d (%s vs. %s) Continuing %s %s simul.}\n", g + 1, pp->name, player_globals.parray[p1].name, rstr[game_globals.garray[g].rated], bstr[game_globals.garray[g].type]);
1596       pprintf(p, tmp);
1597       pprintf(p1, tmp);
1598
1599       game_globals.garray[g].white = p;
1600       game_globals.garray[g].black = p1;
1601       game_globals.garray[g].status = GAME_ACTIVE;
1602       game_globals.garray[g].startTime = tenth_secs();
1603       game_globals.garray[g].lastMoveTime = game_globals.garray[g].startTime;
1604       game_globals.garray[g].lastDecTime = game_globals.garray[g].startTime;
1605       pp->game = g;
1606       pp->opponent = p1;
1607       pp->side = WHITE;
1608       player_globals.parray[p1].game = g;
1609       player_globals.parray[p1].opponent = p;
1610       player_globals.parray[p1].side = BLACK;
1611       send_boards(g);
1612     }
1613
1614     if (pp->simul_info == NULL) {
1615       pp->simul_info = (struct simul_info_t *) malloc(sizeof(struct simul_info_t));
1616       pp->simul_info->numBoards = 0;
1617       pp->simul_info->onBoard = 0;
1618       pp->simul_info->num_wins = pp->simul_info->num_draws
1619         = pp->simul_info->num_losses = 0;
1620     }
1621     num = pp->simul_info->numBoards;
1622     /*    pp->simul_info->results[num] = -1; */
1623     pp->simul_info->boards[num] = pp->game;
1624     pp->simul_info->numBoards++;
1625     if (pp->simul_info->numBoards > 1 &&
1626         pp->simul_info->onBoard >= 0)
1627       player_goto_board(p, pp->simul_info->onBoard);
1628     else
1629       pp->simul_info->onBoard = 0;
1630     return COM_OK;
1631   }
1632   if (find_pend(-1, p, PEND_SIMUL) != NULL) {
1633     pprintf(p, "You cannot be the simul giver and request to join another simul.\nThat would just be too confusing for me and you.\n");
1634     return COM_OK;
1635   }
1636   if (pp->simul_info != NULL) {
1637     if (pp->simul_info->numBoards) {
1638       pprintf(p, "You cannot be the simul giver and request to join another simul.\nThat would just be too confusing for me and you.\n");
1639       return COM_OK;
1640     }
1641   }
1642   if (pp->game >=0) {
1643     pprintf(p, "You are already playing a game.\n");
1644     return COM_OK;
1645   }
1646   if (!CheckPFlag(p1, PFLAG_SIMOPEN)) {
1647     pprintf_highlight(p, "%s", player_globals.parray[p1].name);
1648     pprintf(p, " is not open to receiving simul requests.\n");
1649     return COM_OK;
1650   }
1651   if ((player_globals.parray[p1].simul_info != NULL) && (player_globals.parray[p1].simul_info->numBoards >= MAX_SIMUL)) {
1652     pprintf_highlight(p, "%s", player_globals.parray[p1].name);
1653     pprintf(p, " is already playing the maximum of %d boards.\n", MAX_SIMUL);
1654     return COM_OK;
1655   }
1656
1657 /* loon: checking for some crazy situations we can't allow :) */
1658
1659   if ((player_globals.parray[p1].simul_info != NULL) && (player_globals.parray[p1].game >=0) && (player_globals.parray[p1].simul_info->numBoards == 0)) {
1660     pprintf_highlight(p, "%s", player_globals.parray[p1].name);
1661     if (player_globals.parray[game_globals.garray[player_globals.parray[p1].game].white].simul_info->numBoards) {
1662       pprintf(p, " is playing in ");
1663       pprintf_highlight(p, "%s", player_globals.parray[player_globals.parray[p1].opponent].name);
1664       pprintf(p, "'s simul, and can't accept.\n");
1665     } else {
1666       pprintf(p, " can't begin a simul while playing a non-simul game.\n");
1667     }
1668     return COM_OK;
1669   }
1670
1671   g = game_new();               /* Check if an adjourned untimed game */
1672   adjourned = ((game_read(g, p, p1) < 0) && (game_read(g, p1, p) < 0)) ? 0 : 1;
1673   if (adjourned) {
1674     if (!(game_globals.garray[g].type == TYPE_UNTIMED))
1675       adjourned = 0;
1676   }
1677   game_remove(g);
1678
1679   pend = add_request(p, p1, PEND_SIMUL);
1680
1681   if ((param[1].type == TYPE_WORD) && (param[2].type == TYPE_WORD)) {
1682
1683     sprintf(fname, "%s/%s/%s", BOARD_DIR, param[1].val.word , param[2].val.word);
1684     if (!file_exists(fname)) {
1685       pprintf(p, "No such category/board: %s/%s\n", param[1].val.word , param[2].val.word);
1686       return COM_OK;
1687     }
1688     pend->category = strdup(param[1].val.word);
1689     pend->board_type = strdup(param[2].val.word);
1690   } else {
1691     pend->category = NULL;
1692     pend->board_type = NULL;
1693   }
1694  
1695   pprintf(p1, "\n");
1696   pprintf_highlight(p1, "%s", pp->name);
1697   if (adjourned) {
1698     pprintf_prompt(p1, " requests to continue an adjourned simul game.\n");
1699     pprintf(p, "Request to resume simul sent. Adjourned game found.\n");
1700   } else {
1701     if (pend->category == NULL)
1702       pprintf_prompt(p1, " requests to join a simul match with you.\n");
1703     else
1704       pprintf_prompt(p1, " requests to join a %s %s simul match with you.\n",
1705                 pend->category,pend->board_type);
1706     pprintf(p, "Simul match request sent.\n");
1707   }
1708   return COM_OK;
1709 }
1710
1711 int com_goboard(int p, param_list param)
1712 {
1713   struct player *pp = &player_globals.parray[p];
1714   int on, g, p1, gamenum;
1715
1716   if (pp->simul_info == NULL) {
1717     pprintf(p, "You are not giving a simul.\n");
1718     return COM_OK;
1719   }
1720
1721   if (!pp->simul_info->numBoards) {
1722     pprintf(p, "You are not giving a simul.\n");
1723     return COM_OK;
1724   }
1725
1726   if (param[0].type == TYPE_WORD) {
1727
1728     p1 = player_find_part_login(param[0].val.word);
1729     if (p1 < 0) {
1730       pprintf(p, "No user named \"%s\" is logged in.\n", param[0].val.word);
1731       return COM_OK;
1732     }
1733     if (p == p1) {
1734       pprintf(p, "You can't goboard yourself!\n");
1735       return COM_OK;
1736     }
1737
1738     gamenum = player_globals.parray[p1].game;
1739     if (gamenum < 0) {
1740       pprintf (p,"%s is not playing a game.\n", player_globals.parray[p1].login);
1741       return COM_OK;
1742     }
1743
1744   } else { 
1745     gamenum = param[0].val.integer - 1;
1746     if (gamenum < 0)
1747       gamenum = 0;
1748   }
1749
1750   on = pp->simul_info->onBoard;
1751   g = pp->simul_info->boards[on];
1752   if (gamenum == g) {
1753     pprintf(p, "You are already at that board!\n");
1754     return COM_OK;
1755   }
1756   if (pp->simul_info->numBoards > 1) {
1757     decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1758     if (player_goto_simulgame_bynum(p, gamenum) !=-1) {
1759       if (g >= 0) {
1760         pprintf(game_globals.garray[g].black, "\n");
1761         pprintf_highlight(game_globals.garray[g].black, "%s", pp->name);
1762         pprintf_prompt(game_globals.garray[g].black, " has moved away from your board.\n");
1763       }
1764     } else
1765     pprintf(p, "You are not playing that game/person.\n");
1766   } else
1767     pprintf(p, "You are only playing one board!\n");
1768   return COM_OK;
1769 }
1770
1771 int com_simnext(int p, param_list param)
1772 {
1773   struct player *pp = &player_globals.parray[p];
1774   int on, g;
1775
1776   if (pp->simul_info == NULL) {
1777     pprintf(p, "You are not giving a simul.\n");
1778     return COM_OK;
1779   }
1780
1781   if (!pp->simul_info->numBoards) {
1782     pprintf(p, "You are not giving a simul.\n");
1783     return COM_OK;
1784   }
1785
1786   if (pp->simul_info->numBoards > 1) {
1787     decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1788     on = pp->simul_info->onBoard;
1789     g = pp->simul_info->boards[on];
1790     if (g >= 0) {
1791       pprintf(game_globals.garray[g].black, "\n");
1792       pprintf_highlight(game_globals.garray[g].black, "%s", pp->name);
1793       pprintf_prompt(game_globals.garray[g].black, " is moving away from your board.\n");
1794       player_goto_next_board(p);
1795     }
1796   } else
1797     pprintf(p, "You are only playing one board!\n");
1798   return COM_OK;
1799 }
1800
1801 int com_simprev(int p, param_list param)
1802 {
1803   struct player *pp = &player_globals.parray[p];
1804   int on, g;
1805
1806   if (pp->simul_info == NULL) {
1807     pprintf(p, "You are not giving a simul.\n");
1808     return COM_OK;
1809   }
1810
1811   if (!pp->simul_info->numBoards) {
1812     pprintf(p, "You are not giving a simul.\n");
1813     return COM_OK;
1814   }
1815   if (pp->simul_info->numBoards > 1) {
1816     decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1817     on = pp->simul_info->onBoard;
1818     g = pp->simul_info->boards[on];
1819     if (g >= 0) {
1820       pprintf(game_globals.garray[g].black, "\n");
1821       pprintf_highlight(game_globals.garray[g].black, "%s", pp->name);
1822       pprintf_prompt(game_globals.garray[g].black, " is moving back to the previous board.\n");
1823     }
1824     player_goto_prev_board(p);
1825   } else
1826     pprintf(p, "You are only playing one board!\n");
1827   return COM_OK;
1828 }
1829
1830 int com_simgames(int p, param_list param)
1831 {
1832   int p1 = p;
1833
1834   if (param[0].type == TYPE_WORD) {
1835     if ((p1 = player_find_part_login(param[0].val.word)) < 0) {
1836       pprintf(p, "No player named %s is logged in.\n", param[0].val.word);
1837       return COM_OK;
1838     }
1839   }
1840   if (p1 == p)
1841     pprintf(p, "You are playing %d simultaneous games.\n",
1842             player_num_active_boards(p1));
1843   else
1844     pprintf(p, "%s is playing %d simultaneous games.\n", player_globals.parray[p1].name,
1845             player_num_active_boards(p1));
1846   return COM_OK;
1847 }
1848
1849 int com_simpass(int p, param_list param)
1850 {
1851   struct player *pp = &player_globals.parray[p];
1852   int g, p1, on;
1853
1854   if (!pIsPlaying(p))
1855     return COM_OK;
1856
1857   g = pp->game;
1858   p1 = game_globals.garray[g].white;
1859
1860   if (player_globals.parray[p1].simul_info == NULL) {
1861     pprintf(p, "You are not participating in a simul.\n");
1862     return COM_OK;
1863   }
1864
1865   if (!player_globals.parray[p1].simul_info->numBoards) {
1866     pprintf(p, "You are not participating in a simul.\n");
1867     return COM_OK;
1868   }
1869   if (p == p1) {
1870     pprintf(p, "You are the simul holder and cannot pass!\n");
1871     return COM_OK;
1872   }
1873   if (player_num_active_boards(p1) == 1) {
1874     pprintf(p, "This is the only game, so passing is futile.\n");
1875     return COM_OK;
1876   }
1877   on = player_globals.parray[p1].simul_info->onBoard;
1878   if (player_globals.parray[p1].simul_info->boards[on] != g) {
1879     pprintf(p, "You cannot pass until the simul holder arrives!\n");
1880     return COM_OK;
1881   }
1882   if (game_globals.garray[g].passes >= MAX_SIMPASS) {
1883     Bell (p);
1884     pprintf(p, "You have reached your maximum of %d pass(es).\n", MAX_SIMPASS);
1885     pprintf(p, "Please move IMMEDIATELY!\n");
1886     pprintf_highlight(p1, "%s", pp->name);
1887     pprintf_prompt(p1, " tried to pass, but is out of passes.\n");
1888     return COM_OK;
1889   }
1890   decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1891
1892   game_globals.garray[g].passes++;
1893   pprintf(p, "You have passed and have %d pass(es) left.\n",
1894           (MAX_SIMPASS - game_globals.garray[g].passes));
1895   pprintf_highlight(p1, "%s", pp->name);
1896   pprintf_prompt(p1, " has decided to pass and has %d pass(es) left.\n",
1897                  (MAX_SIMPASS - game_globals.garray[g].passes));
1898   player_goto_next_board(p1);
1899   return COM_OK;
1900 }
1901
1902 int com_simabort(int p, param_list param)
1903 {
1904   struct player *pp = &player_globals.parray[p];
1905
1906   if (pp->simul_info == NULL) {
1907     pprintf(p, "You are not giving a simul.\n");
1908     return COM_OK;
1909   }
1910
1911   if (!pp->simul_info->numBoards) {
1912     pprintf(p, "You are not giving a simul.\n");
1913     return COM_OK;
1914   }
1915   decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1916   game_ended(pp->simul_info->boards[pp->simul_info->onBoard],
1917              WHITE, END_ABORT);
1918   return COM_OK;
1919 }
1920
1921 int com_simallabort(int p, param_list param)
1922 {
1923   struct player *pp = &player_globals.parray[p];
1924   int i;
1925
1926   if (pp->simul_info == NULL) {
1927     pprintf(p, "You are not giving a simul.\n");
1928     return COM_OK;
1929   }
1930
1931   if (!pp->simul_info->numBoards) {
1932     pprintf(p, "You are not giving a simul.\n");
1933     return COM_OK;
1934   }
1935
1936   decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1937   for (i = 0; i < pp->simul_info->numBoards; i++)
1938     if (pp->simul_info->boards[i] >= 0)
1939       game_ended(pp->simul_info->boards[i],
1940                  WHITE, END_ABORT);
1941
1942   return COM_OK;
1943 }
1944
1945 int com_simadjourn(int p, param_list param)
1946 {
1947   struct player *pp = &player_globals.parray[p];
1948
1949   if (pp->simul_info == NULL) {
1950     pprintf(p, "You are not giving a simul.\n");
1951     return COM_OK;
1952   }
1953
1954   if (!pp->simul_info->numBoards) {
1955     pprintf(p, "You are not giving a simul.\n");
1956     return COM_OK;
1957   }
1958   decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1959   game_ended(pp->simul_info->boards[pp->simul_info->onBoard],
1960              WHITE, END_ADJOURN);
1961   return COM_OK;
1962 }
1963
1964 int com_simalladjourn(int p, param_list param)
1965 {
1966   struct player *pp = &player_globals.parray[p];
1967   int i;
1968
1969   if (pp->simul_info == NULL) {
1970     pprintf(p, "You are not giving a simul.\n");
1971     return COM_OK;
1972   }
1973
1974   if (!pp->simul_info->numBoards) {
1975     pprintf(p, "You are not giving a simul.\n");
1976     return COM_OK;
1977   }
1978   decline_withdraw_offers(p, -1, -PEND_SIMUL,DO_DECLINE);
1979   for (i = 0; i < pp->simul_info->numBoards; i++)
1980     if (pp->simul_info->boards[i] >= 0)
1981       game_ended(pp->simul_info->boards[i],
1982                  WHITE, END_ADJOURN);
1983
1984   return COM_OK;
1985 }
1986
1987 int com_moretime(int p, param_list param)
1988 {
1989   struct player *pp = &player_globals.parray[p];
1990   int g, increment;
1991
1992   if ((pp->game >=0) &&((game_globals.garray[pp->game].status == GAME_EXAMINE) ||
1993         (game_globals.garray[pp->game].status == GAME_SETUP))) {
1994     pprintf(p, "You cannot use moretime in an examined game.\n");
1995     return COM_OK;
1996   }
1997   increment = param[0].val.integer;
1998   if (increment <= 0) {
1999     pprintf(p, "Moretime requires an integer value greater than zero.\n");
2000     return COM_OK;
2001   }
2002   if (!pIsPlaying(p))
2003     return COM_OK;
2004  
2005   if (increment > 600) {
2006     pprintf(p, "Moretime has a maximum limit of 600 seconds.\n");
2007     increment = 600;
2008   }
2009   g = pp->game;
2010   if (game_globals.garray[g].white == p) {
2011     game_globals.garray[g].bTime += increment * 10;
2012     game_globals.garray[g].bRealTime += increment * 10 * 100;
2013     pprintf(p, "%d seconds were added to your opponents clock\n",
2014             increment);
2015     pprintf_prompt(pp->opponent,
2016                    "\nYour opponent has added %d seconds to your clock.\n",
2017                    increment);
2018   }
2019   if (game_globals.garray[g].black == p) {
2020     game_globals.garray[g].wTime += increment * 10;;
2021     game_globals.garray[g].wRealTime += increment * 10 * 100;
2022     pprintf(p, "%d seconds were added to your opponents clock\n",
2023             increment);
2024     pprintf_prompt(pp->opponent,
2025                    "\nYour opponent has added %d seconds to your clock.\n",
2026                    increment);
2027   }
2028   return COM_OK;
2029 }
2030