35f19371504558866616fc06f944ac3fc53c3dd2
[capablanca.git] / lasker-2.2.3 / src / matchproc.c
1 /*
2    This program is free software; you can redistribute it and/or modify
3    it under the terms of the GNU General Public License as published by
4    the Free Software Foundation; either version 2 of the License, or
5    (at your option) any later version.
6    
7    This program is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10    GNU General Public License for more details.
11    
12    You should have received a copy of the GNU General Public License
13    along with this program; if not, write to the Free Software
14    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 */
16
17 /**
18   matchproc.c
19   Feb 9 1996 - rewritten - DAV
20
21 */
22
23 #include "includes.h"
24
25 const char *colorstr[] = {"", "[black] ", "[white] "};
26 static const char *adjustr[] = {"", " (adjourned)"};
27
28 static void prepare_match(int g,int wt,int bt,int winc,int binc,int wp, int bp,int rated)
29 {
30
31   wt = wt * 60;                 /* To Seconds */
32   bt = bt * 60;
33   game_globals.garray[g].white = wp;
34   game_globals.garray[g].black = bp;
35   strcpy(game_globals.garray[g].white_name, player_globals.parray[wp].name);
36   strcpy(game_globals.garray[g].black_name, player_globals.parray[bp].name);
37   game_globals.garray[g].status = GAME_ACTIVE;
38   if ((game_globals.garray[g].type == TYPE_UNTIMED) || (game_globals.garray[g].type == TYPE_NONSTANDARD))
39     game_globals.garray[g].rated = 0;
40   else
41     game_globals.garray[g].rated = rated;
42   game_globals.garray[g].private = BoolCheckPFlag(wp, PFLAG_PRIVATE)
43                       || BoolCheckPFlag(bp, PFLAG_PRIVATE);
44   game_globals.garray[g].white = wp;
45   if (game_globals.garray[g].type == TYPE_BLITZ) {
46     game_globals.garray[g].white_rating = player_globals.parray[wp].b_stats.rating;
47     game_globals.garray[g].black_rating = player_globals.parray[bp].b_stats.rating;
48   } else if (game_globals.garray[g].type == TYPE_WILD) {
49     game_globals.garray[g].white_rating = player_globals.parray[wp].w_stats.rating;
50     game_globals.garray[g].black_rating = player_globals.parray[bp].w_stats.rating;
51   } else if (game_globals.garray[g].type == TYPE_LIGHT) {
52     game_globals.garray[g].white_rating = player_globals.parray[wp].l_stats.rating;
53     game_globals.garray[g].black_rating = player_globals.parray[bp].l_stats.rating;
54   } else if (game_globals.garray[g].type == TYPE_BUGHOUSE) {
55     game_globals.garray[g].white_rating = player_globals.parray[wp].bug_stats.rating;
56     game_globals.garray[g].black_rating = player_globals.parray[bp].bug_stats.rating;
57   } else {
58     game_globals.garray[g].white_rating = player_globals.parray[wp].s_stats.rating;
59     game_globals.garray[g].black_rating = player_globals.parray[bp].s_stats.rating;
60   }
61
62   game_globals.garray[g].game_state.gameNum = g;
63
64   game_globals.garray[g].wTime = wt * 10;
65   game_globals.garray[g].wInitTime = wt * 10;
66   game_globals.garray[g].wIncrement = winc * 10;
67   game_globals.garray[g].bTime = bt * 10;
68
69   if (game_globals.garray[g].type != TYPE_UNTIMED) {
70     if (wt == 0)
71         game_globals.garray[g].wTime = 100;
72     if (bt == 0)
73         game_globals.garray[g].bTime = 100;
74   } /* 0 x games start with 10 seconds */
75
76   game_globals.garray[g].wRealTime = game_globals.garray[g].wTime * 100;
77   game_globals.garray[g].bRealTime = game_globals.garray[g].bTime * 100;
78   game_globals.garray[g].wTimeWhenReceivedMove = 0;
79   game_globals.garray[g].bTimeWhenReceivedMove = 0;
80
81   game_globals.garray[g].bInitTime = bt * 10;
82   game_globals.garray[g].bIncrement = binc * 10;
83
84   if (game_globals.garray[g].game_state.onMove == BLACK) {   /* Start with black */
85     game_globals.garray[g].numHalfMoves = 1;
86     game_globals.garray[g].moveListSize = 1;
87     game_globals.garray[g].moveList = (struct move_t *) malloc(sizeof(struct move_t));
88     game_globals.garray[g].moveList[0].fromFile = -1;
89     game_globals.garray[g].moveList[0].fromRank = -1;
90     game_globals.garray[g].moveList[0].toFile = -1;
91     game_globals.garray[g].moveList[0].toRank = -1;
92     game_globals.garray[g].moveList[0].color = WHITE;
93     strcpy(game_globals.garray[g].moveList[0].moveString, "NONE");
94     strcpy(game_globals.garray[g].moveList[0].algString, "NONE");
95   } else {
96     game_globals.garray[g].numHalfMoves = 0;
97     game_globals.garray[g].moveListSize = 0;
98     game_globals.garray[g].moveList = NULL;
99   }
100
101   game_globals.garray[g].timeOfStart = tenth_secs();
102   game_globals.garray[g].startTime = tenth_secs();
103   game_globals.garray[g].lastMoveTime = game_globals.garray[g].startTime;
104   game_globals.garray[g].lastDecTime = game_globals.garray[g].startTime;
105   game_globals.garray[g].clockStopped = 0;
106
107 }
108
109 static void pick_colors(int* wp,int* bp,int white,int wt,int bt,int winc,
110                                                                 int binc)
111
112 {
113  int reverse = 0;
114
115   if (white == 0) {
116     reverse = 1; /* did challenger ask for black? */
117
118   } else if (white == -1) { /* unknown */
119
120     if ((wt == bt) && (winc == binc)) { /* if diff times challenger is white */
121
122       if (CheckPFlag(*wp, PFLAG_LASTBLACK)==CheckPFlag(*bp, PFLAG_LASTBLACK)) {
123         if ((player_globals.parray[*wp].num_white - player_globals.parray[*wp].num_black) >
124           (player_globals.parray[*bp].num_white - player_globals.parray[*bp].num_black))
125           reverse = 1; /* whose played the most extra whites gets black */
126
127       } else if (!CheckPFlag(*wp, PFLAG_LASTBLACK))
128         reverse = 1;
129
130     } else
131       reverse = 1;              
132   }
133   if (reverse) {
134     int tmp = *wp;
135     *wp = *bp;
136     *bp = tmp;
137   }
138 }
139
140 static void output_match_messages(int wp,int bp,int g, char* mess)
141 {
142
143   int printed;
144   int avail_printed = 0;
145   char notavail_white[200];
146   char notavail_black[200];
147   int p;
148   char *outStr;
149
150   notavail_white[0] = '\0';
151   notavail_black[0] = '\0';
152
153   asprintf(&outStr,"\nCreating: %s (%d) %s (%d) %s %s %d %d\n",
154            player_globals.parray[wp].name,
155            game_globals.garray[g].white_rating,
156            player_globals.parray[bp].name,
157            game_globals.garray[g].black_rating,
158            rstr[game_globals.garray[g].rated],
159            bstr[game_globals.garray[g].type],
160            game_globals.garray[g].wInitTime, 
161            game_globals.garray[g].wIncrement);
162   pprintf(wp, "%s", outStr);
163   pprintf(bp, "%s", outStr);
164   free(outStr);
165
166   asprintf(&outStr, "\n{Game %d (%s vs. %s) %s %s %s match.}\n",
167           g + 1, player_globals.parray[wp].name,
168           player_globals.parray[bp].name,
169           mess,
170           rstr[game_globals.garray[g].rated],
171           bstr[game_globals.garray[g].type]);
172   pprintf(wp, "%s", outStr);
173   pprintf(bp, "%s", outStr);
174
175   if (!(player_num_active_boards(wp)) && CheckPFlag(wp, PFLAG_OPEN))
176 /* open may be 0 if a simul */
177     getnotavailmess(wp,notavail_white);
178   if (CheckPFlag(bp, PFLAG_OPEN))
179     getnotavailmess(bp,notavail_black);
180   for (p = 0; p < player_globals.p_num; p++) {
181     struct player *pp = &player_globals.parray[p];
182     int gnw, gnb;
183     printed = 0;
184
185     if ((p == wp) || (p == bp))
186       continue;
187     if (pp->status != PLAYER_PROMPT)
188       continue;
189     if (CheckPFlag(p, PFLAG_GIN)) {
190       pprintf(p, "%s", outStr);
191       printed = 1;
192     }
193     gnw = in_list(p, L_GNOTIFY, player_globals.parray[wp].login);
194     gnb = in_list(p, L_GNOTIFY, player_globals.parray[bp].login);
195     if (gnw || gnb) {
196       pprintf(p, "\nGame notification: ");
197
198       if (gnw)
199         pprintf_highlight(p, player_globals.parray[wp].name);
200       else
201         pprintf(p, player_globals.parray[wp].name);
202
203       pprintf(p, " (%s) vs. ",
204               ratstr(GetRating(&player_globals.parray[wp], game_globals.garray[g].type)));
205
206       if (gnb)
207         pprintf_highlight(p, player_globals.parray[bp].name);
208       else
209         pprintf(p, player_globals.parray[bp].name);
210       pprintf(p, " (%s) %s %s %d %d: Game %d\n",
211                      ratstr(GetRating(&player_globals.parray[bp], game_globals.garray[g].type)),
212                      rstr[game_globals.garray[g].rated], bstr[game_globals.garray[g].type],
213                      game_globals.garray[g].wInitTime/600, game_globals.garray[g].wIncrement/10, g+1);
214
215       printed = 1;
216     }
217
218     if (CheckPFlag(p, PFLAG_AVAIL) && CheckPFlag(p, PFLAG_OPEN) && (pp->game < 0) &&
219        (CheckPFlag(wp, PFLAG_OPEN) || CheckPFlag(bp, PFLAG_OPEN))) {
220
221       if (((player_globals.parray[wp].b_stats.rating <= pp->availmax) &&
222          (player_globals.parray[wp].b_stats.rating >= pp->availmin)) ||
223          (!pp->availmax)) {
224          pprintf (p,"\n%s",notavail_white);
225          avail_printed = 1;
226       }
227
228       if (((player_globals.parray[bp].b_stats.rating <= pp->availmax) &&
229          (player_globals.parray[bp].b_stats.rating >= pp->availmin)) ||
230          (!pp->availmax)) {
231       pprintf (p,"\n%s",notavail_black);
232       avail_printed = 1;
233       }
234
235       if (avail_printed) {
236         printed = 1;
237         avail_printed = 0;
238         pprintf (p,"\n");
239       } /* was black or white open originally (for simuls) */
240     }
241
242     if (printed)
243       send_prompt (p);
244   }
245
246   free(outStr);
247 }
248
249 int create_new_match(int g, int white_player, int black_player,
250                              int wt, int winc, int bt, int binc,
251                              int rated, char *category, char *board,
252                              int white, int simul)
253 {
254
255   remove_request(white_player, black_player, -PEND_PARTNER);
256
257   pick_colors(&white_player,&black_player,white,wt,bt,winc,binc);
258
259   decline_withdraw_offers(white_player,-1, PEND_MATCH,DO_WITHDRAW | DO_DECLINE);
260   decline_withdraw_offers(black_player,-1, PEND_MATCH,DO_WITHDRAW | DO_DECLINE);
261   decline_withdraw_offers(black_player,-1, PEND_SIMUL,DO_WITHDRAW | DO_DECLINE);
262   if (simul) {
263     decline_withdraw_offers(white_player, -1, PEND_SIMUL, DO_WITHDRAW);
264   } else {
265     decline_withdraw_offers(white_player, -1, PEND_SIMUL,
266                             DO_WITHDRAW | DO_DECLINE);
267   }
268   if (board_init(g,&game_globals.garray[g].game_state, category, board)) {
269     pprintf(white_player, "PROBLEM LOADING BOARD. Game Aborted.\n");
270     pprintf(black_player, "PROBLEM LOADING BOARD. Game Aborted.\n");
271     d_printf( "CHESSD: PROBLEM LOADING BOARD %s %s. Game Aborted.\n",
272             category, board);
273     game_remove(g);
274     return COM_FAILED;
275   }
276
277   if (simul)
278     game_globals.garray[g].type = TYPE_UNTIMED;
279   else
280     game_globals.garray[g].type = game_isblitz(wt, winc, bt, binc, category, board);
281
282   prepare_match(g,wt,bt,winc,binc,white_player,black_player,rated);
283
284   output_match_messages(white_player,black_player,g, "Creating");
285
286   player_globals.parray[white_player].game = g;
287   player_globals.parray[white_player].opponent = black_player;
288   player_globals.parray[white_player].side = WHITE;
289   player_globals.parray[white_player].promote = QUEEN;
290   player_globals.parray[black_player].game = g;
291   player_globals.parray[black_player].opponent = white_player;
292   player_globals.parray[black_player].side = BLACK;
293   player_globals.parray[black_player].promote = QUEEN;
294   send_boards(g);
295   gics_globals.userstat.games++;
296
297   /* obey any 'follow' lists */
298   follow_start(white_player,black_player);
299
300   return COM_OK;
301 }
302  
303 int accept_match(struct pending *pend, int p, int p1)
304 {
305   struct player *pp = &player_globals.parray[p];
306   int wt, winc, bt, binc, rated, white;
307   char category[50], board[50];
308   char tmp[100];
309   int bh = 0, partner = 0, pp1 = 0, g1, g2;
310  
311   unobserveAll(p);              /* stop observing when match starts */
312   unobserveAll(p1);
313
314   wt = pend->wtime;
315   winc = pend->winc;
316   bt = pend->btime;
317   binc = pend->binc;
318   rated = pend->rated;
319   strcpy (category, pend->category);
320   strcpy (board, pend->board_type);
321   white = (pend->seek_color == -1) ? -1 : 1 - pend->seek_color;
322
323   pprintf(p, "You accept the challenge of %s.\n", player_globals.parray[p1].name);
324   pprintf_prompt(p1, "\n%s accepts your challenge.\n", pp->name);
325
326   if(!pend->status)
327     delete_pending(pend);
328
329   withdraw_seeks(p);
330   withdraw_seeks(p1);
331
332   pend_join_match (p,p1);
333
334   if (game_isblitz(wt, winc, bt, binc, category, board) == TYPE_BUGHOUSE) {
335     bh = 1;
336
337     if ((partner = pp->partner) >= 0 &&
338         (pp1 = player_globals.parray[p1].partner) >= 0) {
339       unobserveAll(partner);         /* stop observing when match starts */
340       unobserveAll(pp1);
341
342       pprintf(partner, "\nYour partner accepts the challenge of %s.\n", player_globals.parray[p1].name);
343       pprintf(pp1, "\n%s accepts your partner's challenge.\n", pp->name);
344
345       pend_join_match (partner,pp1);
346     } else {
347       return COM_OK;
348     }
349   }
350
351   g1 = game_new(); /* create a game structure */
352   if (g1 < 0) {
353     sprintf(tmp, "There was a problem creating the new match.\n");
354     pprintf(p, tmp);
355     pprintf_prompt(p1, tmp);
356   }
357
358   if (game_read(g1, p1, p) >= 0) {
359           int swap;
360           swap = p;
361           p = p1;
362           p1 = swap;
363           pp = &player_globals.parray[p];
364   } else if (game_read(g1, p, p1) < 0) { /* so no adjourned game */ 
365     if (create_new_match(g1,p, p1, wt, winc, bt, binc, rated, category, board, white,0) == COM_FAILED)
366       return COM_OK;
367
368  /* create first game */
369
370     if (bh) { /* do bughouse creation */
371
372       white = (pp->side == WHITE ? 0 : 1);
373       g2 = game_new();
374       if ((g2 < 0) || (create_new_match(g2,partner, pp1, wt, winc, bt, binc, rated, category, board,white,0) == COM_FAILED)) {
375          sprintf(tmp, "There was a problem creating the new match.\n");
376          pprintf_prompt(partner, tmp);
377          pprintf_prompt(pp1, tmp);
378          sprintf(tmp, "There was a problem creating your partner's match.\n");
379          game_remove(g1); /* abort first game */
380          return COM_OK;
381        }
382
383       game_globals.garray[g1].link = g2; /* link the games */
384       game_globals.garray[g2].link = g1;
385
386       sprintf(tmp, "\nYour partner is playing game %d (%s vs. %s).\n",
387               g2 + 1, game_globals.garray[g2].white_name, game_globals.garray[g2].black_name);
388       pprintf(p, tmp);
389       pprintf_prompt(p1, tmp);
390
391       sprintf(tmp, "\nYour partner is playing game %d (%s vs. %s).\n",
392               g1 + 1, game_globals.garray[g1].white_name, game_globals.garray[g1].black_name);
393       pprintf_prompt(partner, tmp);
394       pprintf_prompt(pp1, tmp);
395     } 
396     return COM_OK;
397   }
398
399        /* resume adjourned game */
400
401   game_delete(p, p1); /* remove the game from disk */
402
403   sprintf(tmp, "{Game %d (%s vs. %s) Continuing %s %s match.}\n",
404         g1+1, pp->name, player_globals.parray[p1].name,
405         rstr[game_globals.garray[g1].rated], bstr[game_globals.garray[g1].type]);
406   pprintf(p, tmp);
407   pprintf(p1, tmp);
408   output_match_messages(p, p1, g1, "Continuing");
409
410   game_globals.garray[g1].white = p;
411   game_globals.garray[g1].black = p1;
412   game_globals.garray[g1].status = GAME_ACTIVE;
413   game_globals.garray[g1].result = END_NOTENDED;
414   game_globals.garray[g1].startTime = tenth_secs();
415   game_globals.garray[g1].lastMoveTime = game_globals.garray[g1].startTime;
416   game_globals.garray[g1].lastDecTime = game_globals.garray[g1].startTime;
417   pp->game = g1;
418   pp->opponent = p1;
419   pp->side = WHITE;
420   player_globals.parray[p1].game = g1;
421   player_globals.parray[p1].opponent = p;
422   player_globals.parray[p1].side = BLACK;
423
424   send_boards(g1);
425
426   /* obey any 'follow' lists */
427   follow_start(p,p1);
428
429   return COM_OK;
430 }
431
432 /* board and category are initially empty strings */
433 static int parse_match_string(int p, int* wt,int* bt,int* winc,int* binc,
434                                 int* white,int* rated,char* category,
435                                 char* board, char* mstring)
436 {
437   int numba;
438   int confused = 0;
439   char parsebuf[100];
440   int bughouse = 0;
441
442   while (!confused && (sscanf(mstring, " %99s", parsebuf) == 1)) {
443     mstring = eatword(eatwhite(mstring));
444
445     if ((category[0] != '\0') && (board[0] == '\0') && !bughouse)
446       strcpy(board, parsebuf);
447     else if (isdigit(*parsebuf)) {
448       if ((numba = atoi(parsebuf)) < 0) {
449         pprintf(p, "You can't specify negative time controls.\n");
450         return 0;
451       } else if (numba > 999) {
452         pprintf(p, "You can't specify time or inc above 999.\n");
453         return 0;
454       } else if (*wt == -1) {
455         *wt = numba;
456       } else if (*winc == -1) {
457         *winc = numba;
458       } else if (*bt == -1) {
459         *bt = numba;
460       } else if (*binc == -1) {
461         *binc = numba;
462       } else
463         confused = 1;
464
465     } else if ((!strcmp("rated", parsebuf)) || (!strcmp("r",parsebuf))) {
466       if (*rated == -1)
467         *rated = 1;
468       else
469         confused = 1;
470     } else if ((!strcmp("unrated", parsebuf)) || (!strcmp("u",parsebuf))) {
471       if (*rated == -1)
472         *rated = 0;
473       else
474         confused = 1;
475     } else if (!strcmp("white", parsebuf) || !strcmp ("w", parsebuf)) {
476       if (*white == -1)
477         *white = 1;
478       else
479         confused = 1;
480     } else if (!strcmp("black", parsebuf) || !strcmp ("b", parsebuf)) {
481       if (*white == -1)
482         *white = 0;
483       else
484         confused = 1;
485
486     } else if (category[0] == '\0') {
487       if ((parsebuf[0] == 'w') && (isdigit(*(parsebuf + 1)))) {
488         strcpy (category, "wild");
489         strcpy (board, parsebuf+1);
490       } else {
491         strcpy(category,parsebuf);
492         if (!strncmp("bughouse",category, strlen(category))
493             && strlen(category) >= 3) {
494           strcpy(category, "bughouse");
495           bughouse = 1;
496         }
497       }
498     } else
499       confused = 1;
500   }
501
502   if (confused) {
503     pprintf(p, "Can't interpret %s in match command.\n", parsebuf);
504     return 0;
505   }
506   return 1;
507 }
508
509 static void print_match_rating_info(int p,int p1,int type,int rated)
510 {
511   if (rated) {
512     int win, draw, loss;
513     double newsterr;
514
515     rating_sterr_delta(p1, p, type, time(0), RESULT_WIN, &win, &newsterr);
516     rating_sterr_delta(p1, p, type, time(0), RESULT_DRAW, &draw, &newsterr);
517     rating_sterr_delta(p1, p, type, time(0), RESULT_LOSS, &loss, &newsterr);
518     pprintf(p1, "Your %s rating will change:  Win: %s%d,  Draw: %s%d,  Loss: %s%d\n",
519             bstr[type],
520             (win >= 0) ? "+" : "", win,
521             (draw >= 0) ? "+" : "", draw,
522             (loss >= 0) ? "+" : "", loss);
523     pprintf(p1, "Your new RD will be %5.1f\n", newsterr);
524
525     rating_sterr_delta(p, p1, type, time(0), RESULT_WIN, &win, &newsterr);
526     rating_sterr_delta(p, p1, type, time(0), RESULT_DRAW, &draw, &newsterr);
527     rating_sterr_delta(p, p1, type, time(0), RESULT_LOSS, &loss, &newsterr);
528     pprintf(p, "Your %s rating will change:  Win: %s%d,  Draw: %s%d,  Loss: %s%d\n",
529             bstr[type],
530             (win >= 0) ? "+" : "", win,
531             (draw >= 0) ? "+" : "", draw,
532             (loss >= 0) ? "+" : "", loss);
533     pprintf(p, "Your new RD will be %5.1f\n", newsterr);
534   }
535 }
536
537 static void check_lists_match(int p,int p1)
538 {
539   struct player *pp = &player_globals.parray[p];
540   if (in_list(p, L_COMPUTER, pp->name)) {
541     pprintf(p1, "--** %s is a ", pp->name);
542     pprintf_highlight(p1, "computer");
543     pprintf(p1, " **--\n");
544   }
545   if (in_list(p, L_COMPUTER, player_globals.parray[p1].name)) {
546     pprintf(p, "--** %s is a ", player_globals.parray[p1].name);
547     pprintf_highlight(p, "computer");
548     pprintf(p, " **--\n");
549   }
550   if (in_list(p, L_ABUSER, pp->name)) {
551     pprintf(p1, "--** %s is in the ", pp->name);
552     pprintf_highlight(p1, "abuser");
553     pprintf(p1, " list **--\n");
554   }
555   if (in_list(p, L_ABUSER, player_globals.parray[p1].name)) {
556     pprintf(p, "--** %s is in the ", player_globals.parray[p1].name);
557     pprintf_highlight(p, "abuser");
558     pprintf(p, " list **--\n");
559   }
560 }
561
562 int com_match(int p, param_list param)
563 {
564   struct player *pp = &player_globals.parray[p];
565   int adjourned;                /* adjourned game? */
566   int g;                        /* more adjourned game junk */
567   int p1;
568   int bh = 0, partner = 0, pp1 = 0;
569   struct pending* pendfrom;
570   struct pending* pendto;
571   struct pending* pend;
572   int wt = -1;                  /* white start time */
573   int winc = -1;                /* white increment */
574   int bt = -1;                  /* black start time */
575   int binc = -1;                /* black increment */
576   int rated = -1;               /* 1 = rated, 0 = unrated */
577   int white = -1;               /* 1 = want white, 0 = want black */
578   char category[100], board[100];
579   textlist *clauses = NULL;
580   int type = 0;
581
582   category[0] ='\0';
583   board[0] ='\0';
584
585   if ((pp->game >= 0) && ((game_globals.garray[pp->game].status == GAME_EXAMINE)
586 || (game_globals.garray[pp->game].status == GAME_SETUP))) {
587
588     pprintf(p, "You can't challenge while you are examining a game.\n");
589     return COM_OK;
590   }
591
592   if (pp->game >= 0) {
593     pprintf(p, "You can't challenge while you are playing a game.\n");
594     return COM_OK;
595   }
596
597   stolower(param[0].val.word);
598   p1 = player_find_part_login(param[0].val.word);
599   if (p1 < 0) {
600     pprintf(p, "No user named \"%s\" is logged in.\n", param[0].val.word);
601     return COM_OK;
602   }
603
604   if (p1 == p) {                /* Allowing to match yourself to enter
605                                    analysis mode */
606     ExamineScratch (p, param, 0);
607     return COM_OK;
608   }
609
610   if (!CheckPFlag(p1, PFLAG_OPEN)) {
611     pprintf(p, "Player \"%s\" is not open to match requests.\n", player_globals.parray[p1].name);
612     return COM_OK;
613   }
614
615   if (player_globals.parray[p1].game >= 0) {
616     pprintf(p, "Player \"%s\" is involved in another game.\n", player_globals.parray[p1].name);    return COM_OK;
617   }
618
619   if (CheckPFlag(p, PFLAG_TOURNEY) && !CheckPFlag(p1, PFLAG_TOURNEY)) {
620     pprintf(p, "You may only match players with their tournament variable set.\n");
621     return COM_OK;
622   }
623
624   if (!CheckPFlag(p, PFLAG_TOURNEY) && CheckPFlag(p1, PFLAG_TOURNEY)) {
625     pprintf(p, "%s is in a tournament, and cannot accept other challenges.\n", player_globals.parray[p1].name);
626     return COM_OK;
627   }
628
629   if (!CheckPFlag(p, PFLAG_OPEN)) {
630     PFlagON(p, PFLAG_OPEN);
631     pprintf(p, "Setting you open for matches.\n");
632   }
633
634
635 /* look for an adjourned game between p and p1 */
636   g = game_new();
637   adjourned = (game_read(g, p, p1) >= 0) || (game_read(g, p1, p) >= 0);
638   if (adjourned) {
639     type = game_globals.garray[g].type;
640     wt = game_globals.garray[g].wInitTime / 600;
641     bt = game_globals.garray[g].bInitTime / 600;
642     winc = game_globals.garray[g].wIncrement / 10;
643     binc = game_globals.garray[g].bIncrement / 10;
644     rated = game_globals.garray[g].rated;
645   }
646   game_remove(g);
647
648   pendto = find_pend(p, p1, PEND_MATCH);
649
650   pendfrom = find_pend(p1, p, PEND_MATCH);
651  
652   if (!adjourned) {
653     if (in_list(p1, L_NOPLAY, pp->login)) {
654       pprintf(p, "You are on %s's noplay list.\n", player_globals.parray[p1].name);
655       return COM_OK;
656     }
657     if (player_censored(p1, p)) {
658       pprintf(p, "Player \"%s\" is censoring you.\n", player_globals.parray[p1].name);
659       return COM_OK;
660     }
661     if (player_censored(p, p1)) {
662       pprintf(p, "You are censoring \"%s\".\n", player_globals.parray[p1].name);
663       return COM_OK;
664     }
665
666     if (param[1].type != TYPE_NULL) {
667       if (!parse_match_string(p, &wt,&bt,&winc,&binc,&white,&rated,category,
668                                                   board,param[1].val.string))
669       return COM_OK; /* couldn't parse */
670     }
671
672     if (rated == -1)
673       rated = BoolCheckPFlag(p, PFLAG_RATED);
674     if (!CheckPFlag(p, PFLAG_REG) || !CheckPFlag(p1, PFLAG_REG))
675       rated = 0;
676
677     if (winc == -1)
678       winc = (wt == -1) ? pp->d_inc : 0;  /* match 5 == match 5 0 */
679
680     if (wt == -1)
681       wt = pp->d_time;
682
683     if (bt == -1)
684       bt = wt;
685
686     if (binc == -1)
687       binc = winc;
688
689     if (category[0]) {
690       if (!board[0] && strcmp(category,"bughouse")) {
691         pprintf(p, "You must specify a board and a category.\n");
692         return COM_OK;
693
694       } else if (board[0]) { /* not bughouse */
695         char fname[MAX_FILENAME_SIZE];
696
697         sprintf(fname, "%s/%s/%s", BOARD_DIR, category, board);
698         if (!file_exists(fname)) {
699           pprintf(p, "No such category/board: %s/%s\n", category, board);
700           return COM_OK;
701         }
702       }
703     }
704     type = game_isblitz(wt, winc, bt, binc, category, board);
705
706     if (!strcmp(category, "bughouse")) {
707       type = TYPE_BUGHOUSE;
708       if (rated && pp->partner >= 0 && player_globals.parray[p1].partner >= 0) {
709         if (!CheckPFlag(pp->partner, PFLAG_REG)
710               || !CheckPFlag(player_globals.parray[p1].partner, PFLAG_REG))
711           rated = 0;
712       }
713     }
714     if (rated && (type == TYPE_NONSTANDARD)) {
715       pprintf(p, "Game is non-standard - reverting to unrated\n");
716       rated = 0;
717     }
718     if (rated && (type == TYPE_UNTIMED)) {
719       pprintf(p, "Game is untimed - reverting to unrated\n");
720       rated = 0;
721     }
722     if ((pendfrom == NULL) && !CheckPFlag(p1, PFLAG_ROPEN)
723         && (rated != BoolCheckPFlag(p1, PFLAG_RATED))) {
724       pprintf(p, "%s only wants to play %s games.\n", player_globals.parray[p1].name,
725               rstr[!rated]);
726       pprintf_highlight(p1, "Ignoring");
727       pprintf(p1, " %srated match request from %s.\n",
728               (rated ? "" : "un"), pp->name);
729       return COM_OK;
730     }
731
732     /* Now check formula. */
733     if (!adjourned
734         && !GameMatchesFormula(p,p1, wt,winc,bt,binc, rated, type, &clauses)) {
735       pprintf(p, "Match request does not fit formula for %s:\n",
736               player_globals.parray[p1].name);
737       pprintf(p, "%s's formula: %s\n", player_globals.parray[p1].name, player_globals.parray[p1].formula);
738       ShowClauses (p, p1, clauses);
739       ClearTextList(clauses);
740       pprintf_highlight(p1, "Ignoring");
741       pprintf_prompt(p1, " (formula): %s (%d) %s (%d) %s.\n",
742                      pp->name,
743                      GetRating(&player_globals.parray[p], type),
744                      player_globals.parray[p1].name,
745                      GetRating(&player_globals.parray[p1], type),
746             game_str(rated, wt * 60, winc, bt * 60, binc, category, board));
747       return COM_OK;
748     }
749
750     if (type == TYPE_BUGHOUSE) {
751       bh = 1;
752       partner = pp->partner;
753       pp1 = player_globals.parray[p1].partner;
754
755       if (pp < 0) {
756         pprintf(p, "You have no partner for bughouse.\n");
757         return COM_OK;
758       }
759       if (pp1 < 0) {
760         pprintf(p, "Your opponent has no partner for bughouse.\n");
761         return COM_OK;
762       }
763       if (partner == pp1) { /* should be an impossible case - DAV */
764         pprintf(p, "You and your opponent both chose the same partner!\n");
765         return COM_OK;
766       }
767       if (partner == p1 || pp1 == p) {
768         pprintf(p, "You and your opponent can't choose each other as partners!\n");
769         return COM_OK;
770       }
771       if (player_globals.parray[partner].partner != p) { /* another impossible case - DAV */
772         pprintf(p, "Your partner hasn't chosen you as his partner!\n");
773         return COM_OK;
774       }
775       if (player_globals.parray[pp1].partner != p1) { /* another impossible case - DAV */
776         pprintf(p, "Your opponent's partner hasn't chosen your opponent as his partner!\n");
777         return COM_OK;
778       }
779       if (!CheckPFlag(partner, PFLAG_OPEN) || player_globals.parray[partner].game >= 0) {
780         pprintf(p, "Your partner isn't open to play right now.\n");
781         return COM_OK;
782       }
783       if (!CheckPFlag(pp1, PFLAG_OPEN) || player_globals.parray[pp1].game >= 0) {
784         pprintf(p, "Your opponent's partner isn't open to play right now.\n");
785         return COM_OK;
786       }
787
788       /* Bypass NOPLAY lists, censored lists, ratedness, privacy, and formula for now */
789       /*  Active challenger/ee will determine these. */
790     }
791     /* Ok match offer will be made */
792
793   }                             /* adjourned games shouldn't have to worry
794                                    about that junk? */
795                                 /* keep incase of adjourned bughouse in future*/
796
797   if (pendto != NULL) {
798     pprintf(p, "Updating offer already made to \"%s\".\n", player_globals.parray[p1].name);
799   }
800
801   if (pendfrom != NULL) {
802     if (pendto != NULL) {
803       pprintf(p, "Pending list error!.\n");
804       d_printf( "CHESSD: This shouldn't happen. You can't have a match pending from and to the same person.\n");
805       return COM_OK;
806     }
807
808     if (adjourned || ((wt == pendfrom->btime) &&
809                       (winc == pendfrom->binc) &&
810                       (bt == pendfrom->wtime) &&
811                       (binc == pendfrom->winc) &&
812                       (rated == pendfrom->rated) &&
813                       ((white == -1) || (white + pendfrom->seek_color == 1)) &&
814                (!strcmp(category, pendfrom->category)) &&
815                  (!strcmp(board, pendfrom->board_type)))) {
816       /* Identical match, should accept! */
817       accept_match(pendfrom,p, p1);
818       return COM_OK;
819
820     } else {
821       delete_pending(pendfrom);
822     }
823   }
824
825   if (pendto == NULL)
826     pend = add_pending(p,p1,PEND_MATCH);
827   else
828     pend = pendto; 
829
830   pend->wtime = wt;
831   pend->winc = winc;
832   pend->btime = bt;
833   pend->binc = binc;
834   pend->rated = rated;
835   pend->seek_color = white;
836   pend->game_type = type;
837   pend->category = strdup(category);
838   pend->board_type = strdup (board);
839
840   if (pendfrom != NULL) {
841     pprintf(p, "Declining offer from %s and offering new match parameters.\n", player_globals.parray[p1].name);
842     pprintf(p1, "\n%s declines your match offer a match with these parameters:", pp->name);
843   }
844
845   if (pendto != NULL) {
846     pprintf(p, "Updating match request to: ");
847     pprintf(p1, "\n%s updates the match request.\n", pp->name);
848   } else {
849     pprintf(p, "Issuing: ");
850     pprintf(p1, "\n");
851   }
852
853   pprintf(p, "%s (%s) %s", pp->name,
854           ratstrii(GetRating(&player_globals.parray[p], type), p),
855           colorstr[white + 1]);
856   pprintf_highlight(p, "%s", player_globals.parray[p1].name);
857   pprintf(p, " (%s) %s%s.\n",
858           ratstrii(GetRating(&player_globals.parray[p1], type), p1),
859           game_str(rated, wt * 60, winc, bt * 60, binc, category, board),
860           adjustr[adjourned]);
861   pprintf(p1, "Challenge: ");
862   pprintf_highlight(p1, "%s", pp->name);
863   pprintf(p1, " (%s) %s",
864           ratstrii(GetRating(&player_globals.parray[p], type), p),
865           colorstr[white + 1]);
866   pprintf(p1, "%s (%s) %s%s.\n", player_globals.parray[p1].name,
867           ratstrii(GetRating(&player_globals.parray[p1], type), p1),
868           game_str(rated, wt * 60, winc, bt * 60, binc, category, board),
869           adjustr[adjourned]);
870   Bell (p1);
871
872   if (bh) {
873
874     pprintf(partner, "\nYour bughouse partner issuing %s (%s) %s",
875             pp->name, ratstrii(GetRating(&player_globals.parray[p], type), p),
876             colorstr[white + 1]);
877     pprintf_highlight(partner, "%s", player_globals.parray[p1].name);
878     pprintf(partner, " (%s) %s.\n",
879             ratstrii(GetRating(&player_globals.parray[p1], type), p1),
880             game_str(rated, wt * 60, winc, bt * 60, binc, category, board));
881     pprintf(partner, "Your game would be ");
882     pprintf_highlight(partner, "%s", player_globals.parray[pp1].name);
883     pprintf_prompt(partner, " (%s) %s%s (%s) %s.\n",
884           ratstrii(GetRating(&player_globals.parray[pp1], type), pp1),
885           colorstr[white + 1], player_globals.parray[partner].name,
886           ratstrii(GetRating(&player_globals.parray[partner], type), partner),
887           game_str(rated, wt * 60, winc, bt * 60, binc, category, board));
888     Bell (partner);
889
890     pprintf(pp1, "\nYour bughouse partner was challenged ");
891     pprintf_highlight(pp1, "%s", pp->name);
892     pprintf(pp1, " (%s) %s", ratstrii(GetRating(&player_globals.parray[p], type), p),
893                              colorstr[white + 1]);
894     pprintf(pp1, "%s (%s) %s.\n", player_globals.parray[p1].name,
895             ratstrii(GetRating(&player_globals.parray[p1], type), p1),
896             game_str(rated, wt * 60, winc, bt * 60, binc, category, board));
897     pprintf(pp1, "Your game would be %s (%s) %s", player_globals.parray[pp1].name,
898           ratstrii(GetRating(&player_globals.parray[pp1], type), pp1),
899           colorstr[white + 1]);
900     pprintf_highlight(pp1, "%s", player_globals.parray[partner].name);
901     pprintf_prompt(pp1, " (%s) %s.\n",
902           ratstrii(GetRating(&player_globals.parray[partner], type), partner),
903           game_str(rated, wt * 60, winc, bt * 60, binc, category, board));
904     Bell(pp1);
905   }
906
907   check_lists_match (p,p1);
908
909   print_match_rating_info (p,p1,type,rated);
910
911   pprintf_prompt(p1, "You can \"accept\" or \"decline\", or propose different parameters.\n");
912
913   return COM_OK;
914 }
915
916
917 /*
918   rmatch is used by mamer to start matches in tournaments
919 */
920 int com_rmatch(int p, param_list param)
921 {
922         struct player *pp = &player_globals.parray[p];
923         int p1, p2;
924
925         if (!in_list(p, L_TD, pp->name)) {
926                 pprintf(p, "Only TD programs are allowed to use this command.\n");
927                 return COM_OK;
928         }
929
930         if ((p1 = player_find_bylogin(param[0].val.word)) < 0 || 
931             !CheckPFlag(p1, PFLAG_REG)) {
932                 /* can't rmatch this user ... */
933                 return COM_OK;
934         }
935
936         if ((p2 = player_find_bylogin(param[1].val.word)) < 0 || 
937             !CheckPFlag(p2, PFLAG_REG)) {
938                 /* can't rmatch this user ... */
939                 return COM_OK;
940         }
941
942         return pcommand(p1, "match %s %s", param[1].val.word, param[2].val.string);
943 }