aboutsummaryrefslogtreecommitdiffstats
path: root/FICS/gameproc.c
blob: f53518ce611c3e39d81f17d9ec5393f1be50be86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
/* gameproc.c
 *
 */

/*
    fics - An internet chess server.
    Copyright (C) 1993  Richard V. Nash

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
*/

/* Revision history:
   name		email		yy/mm/dd	Change
   Richard Nash			93/10/22	Created
   Dave Herscovici		95/11/26	Split into two files;
   						Second is 'obsproc.c'.
   Markus Uhlin			23/12/16	Fixed compiler warnings
   Markus Uhlin			24/01/04	Fixed pprintf_prompt() calls
   Markus Uhlin			24/03/30	Refactored and reformatted all
						functions.
   Markus Uhlin			24/03/30	Size-bounded string handling.
   Markus Uhlin			24/04/13	Added usage of msnprintf(),
						mstrlcpy() and mstrlcat().
*/

#include "stdinclude.h"
#include "common.h"

#include <err.h>

#include "command.h"
#include "comproc.h"
#include "config.h"
#include "eco.h"
#include "ficsmain.h"
#include "gamedb.h"
#include "gameproc.h"
#include "lists.h"
#include "matchproc.h"
#include "maxxes-utils.h"
#include "movecheck.h"
#include "network.h"
#include "obsproc.h"
#include "playerdb.h"
#include "ratings.h"
#include "rmalloc.h"
#include "utils.h"

#if __linux__
#include <bsd/string.h>
#endif

PUBLIC void
game_ended(int g, int winner, int why)
{
	char	*NameOfWinner, *NameOfLoser;
	char	 EndSymbol[10] = { '\0' };
	char	 outstr[200] = { '\0' };
	char	 tmp[200] = { '\0' };
	char	 winSymbol[10] = { '\0' };
	int	 beingplayed = 0; // i.e. it wasn't loaded for adjudication
	int	 gl = garray[g].link;
	int	 isDraw = 0;
	int	 p;
	int	 rate_change = 0;
	int	 whiteResult;

	beingplayed = (parray[garray[g].black].game == g);

	msnprintf(outstr, sizeof outstr, "\n{Game %d (%s vs. %s) ",
	    (g + 1),
	    parray[garray[g].white].name,
	    parray[garray[g].black].name);

	garray[g].result = why;
	garray[g].winner = winner;

	if (winner == WHITE) {
		whiteResult = RESULT_WIN;
		mstrlcpy(winSymbol, "1-0", sizeof winSymbol);
		NameOfWinner = parray[garray[g].white].name;
		NameOfLoser = parray[garray[g].black].name;
	} else {
		whiteResult = RESULT_LOSS;
		mstrlcpy(winSymbol, "0-1", sizeof winSymbol);
		NameOfWinner = parray[garray[g].black].name;
		NameOfLoser = parray[garray[g].white].name;
	}

	switch (why) {
	case END_CHECKMATE:
		msnprintf(tmp, sizeof tmp, "%s checkmated} %s\n",
		    NameOfLoser,
		    winSymbol);
		mstrlcpy(EndSymbol, "Mat", sizeof EndSymbol);
		rate_change = 1;
		break;
	case END_RESIGN:
		msnprintf(tmp, sizeof tmp, "%s resigns} %s\n",
		    NameOfLoser,
		    winSymbol);
		mstrlcpy(EndSymbol, "Res", sizeof EndSymbol);
		rate_change = 1;
		break;
	case END_FLAG:
		msnprintf(tmp, sizeof tmp, "%s forfeits on time} %s\n",
		    NameOfLoser,
		    winSymbol);
		mstrlcpy(EndSymbol, "Fla", sizeof EndSymbol);
		rate_change = 1;
		break;
	case END_STALEMATE:
		mstrlcpy(tmp, "Game drawn by stalemate} 1/2-1/2\n", sizeof tmp);
		isDraw = 1;
		mstrlcpy(EndSymbol, "Sta", sizeof EndSymbol);
		rate_change = 1;
		whiteResult = RESULT_DRAW;
		break;
	case END_AGREEDDRAW:
		mstrlcpy(tmp, "Game drawn by mutual agreement} 1/2-1/2\n",
		    sizeof tmp);
		isDraw = 1;
		mstrlcpy(EndSymbol, "Agr", sizeof EndSymbol);
		rate_change = 1;
		whiteResult = RESULT_DRAW;
		break;
	case END_BOTHFLAG:
		mstrlcpy(tmp, "Game drawn because both players ran out of "
		    "time} 1/2-1/2\n", sizeof tmp);
		isDraw = 1;
		mstrlcpy(EndSymbol, "Fla", sizeof EndSymbol);
		rate_change = 1;
		whiteResult = RESULT_DRAW;
		break;
	case END_REPETITION:
		mstrlcpy(tmp, "Game drawn by repetition} 1/2-1/2\n", sizeof tmp);
		isDraw = 1;
		mstrlcpy(EndSymbol, "Rep", sizeof EndSymbol);
		rate_change = 1;
		whiteResult = RESULT_DRAW;
		break;
	case END_50MOVERULE:
		mstrlcpy(tmp, "Game drawn by the 50 move rule} 1/2-1/2\n",
		    sizeof tmp);
		isDraw = 1;
		mstrlcpy(EndSymbol, "50", sizeof EndSymbol);
		rate_change = 1;
		whiteResult = RESULT_DRAW;
		break;
	case END_ADJOURN:
		if (gl >= 0) {
			mstrlcpy(tmp, "Bughouse game aborted.} *\n",
			    sizeof tmp);
			whiteResult = RESULT_ABORT;
		} else {
			mstrlcpy(tmp, "Game adjourned by mutual agreement} *\n",
			    sizeof tmp);
			game_save(g);
		}
		break;
	case END_LOSTCONNECTION:
		msnprintf(tmp, sizeof tmp, "%s lost connection; game ",
		    NameOfWinner);

		if (parray[garray[g].white].registered &&
		    parray[garray[g].black].registered &&
		    gl < 0) {
			mstrlcpy(tmp, "adjourned} *\n", sizeof tmp);
			game_save(g);
		} else
			mstrlcpy(tmp, "aborted} *\n", sizeof tmp);
		whiteResult = RESULT_ABORT;
		break;
	case END_ABORT:
		mstrlcpy(tmp, "Game aborted by mutual agreement} *\n",
		    sizeof tmp);
		whiteResult = RESULT_ABORT;
		break;
	case END_COURTESY:
		msnprintf(tmp, sizeof tmp, "Game courtesyaborted by %s} *\n",
		    NameOfWinner);
		whiteResult = RESULT_ABORT;
		break;
	case END_COURTESYADJOURN:
		if (gl >= 0) {
			msnprintf(tmp, sizeof tmp, "Bughouse game "
			    "courtesyaborted by %s.} *\n",
			    NameOfWinner);
			whiteResult = RESULT_ABORT;
		} else {
			msnprintf(tmp, sizeof tmp, "Game courtesyadjourned by "
			    "%s} *\n",
			    NameOfWinner);
			game_save(g);
		}
		break;
	case END_NOMATERIAL:
		// Draw by insufficient material (e.g., lone K vs. lone K)
		mstrlcpy(tmp, "Neither player has mating material} 1/2-1/2\n",
		    sizeof tmp);
		isDraw = 1;
		mstrlcpy(EndSymbol, "NM ", sizeof EndSymbol);
		rate_change = 1;
		whiteResult = RESULT_DRAW;
		break;
	case END_FLAGNOMATERIAL:
		msnprintf(tmp, sizeof tmp, "%s ran out of time and %s has no "
		    "material to mate} 1/2-1/2\n",
		    NameOfLoser,
		    NameOfWinner);
		isDraw = 1;
		mstrlcpy(EndSymbol, "TM ", sizeof EndSymbol);
		rate_change = 1;
		whiteResult = RESULT_DRAW;
		break;
	case END_ADJWIN:
		msnprintf(tmp, sizeof tmp, "%s wins by adjudication} %s\n",
		    NameOfWinner, winSymbol);
		mstrlcpy(EndSymbol, "Adj", sizeof EndSymbol);
		rate_change = 1;
		break;
	case END_ADJDRAW:
		mstrlcpy(tmp, "Game drawn by adjudication} 1/2-1/2\n",
		    sizeof tmp);
		isDraw = 1;
		mstrlcpy(EndSymbol, "Adj", sizeof EndSymbol);
		rate_change = 1;
		whiteResult = RESULT_DRAW;
		break;
	case END_ADJABORT:
		mstrlcpy(tmp, "Game aborted by adjudication} *\n", sizeof tmp);
		whiteResult = RESULT_ABORT;
		break;
	default:
		mstrlcpy(tmp, "Hmm, the game ended and I don't know why} *\n",
		    sizeof tmp);
		break;
	}

	mstrlcat(outstr, tmp, sizeof outstr);

	if (beingplayed) {
		pprintf_noformat(garray[g].white, "%s", outstr);
		pprintf_noformat(garray[g].black, "%s", outstr);

		if (parray[garray[g].white].bell)
			pprintf(garray[g].white, "\007");
		if (parray[garray[g].black].bell)
			pprintf(garray[g].black, "\007");

		garray[g].link = -1;	// IanO: avoids recursion

		if (gl >= 0 && garray[gl].link >= 0) {
			pprintf_noformat(garray[gl].white, "%s", outstr);
			pprintf_noformat(garray[gl].black, "%s", outstr);

			game_ended(gl, CToggle(winner), why);
		}

		for (p = 0; p < p_num; p++) {
			if (p == garray[g].white || p == garray[g].black)
				continue;
			if (parray[p].status != PLAYER_PROMPT)
				continue;
			if (!parray[p].i_game && !player_is_observe(p, g))
				continue;

			pprintf_noformat(p, "%s", outstr);
			pprintf_prompt(p, "%s", "");
		}
	}

	if (garray[g].rated && rate_change) {
		/* Adjust ratings */
		rating_update(g);
	} else {
		if (beingplayed) {
			pprintf(garray[g].white, "No ratings adjustment done."
			    "\n");
			pprintf(garray[g].black, "No ratings adjustment done."
			    "\n");
		}
	}

	if (rate_change && gl < 0)
		game_write_complete(g, isDraw, EndSymbol);

	/*
	 * Mail off the moves
	 */
	if (parray[garray[g].white].automail)
		pcommand(garray[g].white, "mailmoves");
	if (parray[garray[g].black].automail)
		pcommand(garray[g].black, "mailmoves");

	parray[garray[g].white].num_white++;
	parray[garray[g].white].lastColor = WHITE;
	parray[garray[g].black].num_black++;
	parray[garray[g].black].lastColor = BLACK;
	parray[garray[g].white].last_opponent = garray[g].black;
	parray[garray[g].black].last_opponent = garray[g].white;

	if (beingplayed) {
		parray[garray[g].white].game = -1;
		parray[garray[g].black].game = -1;
		parray[garray[g].white].opponent = -1;
		parray[garray[g].black].opponent = -1;

		if (garray[g].white != commanding_player)
			pprintf_prompt(garray[g].white, "%s", "");
		if (garray[g].black != commanding_player)
			pprintf_prompt(garray[g].black, "%s", "");

		if (parray[garray[g].white].simul_info.numBoards)
			player_simul_over(garray[g].white, g, whiteResult);
	}

	game_finish(g);
}

PRIVATE int
was_promoted(game *g, int f, int r)
{
#define BUGHOUSE_PAWN_REVERT 1
#if BUGHOUSE_PAWN_REVERT
	for (int i = g->numHalfMoves-2; i > 0; i -= 2) {
		if (g->moveList[i].toFile == f &&
		    g->moveList[i].toRank == r) {
			if (g->moveList[i].piecePromotionTo)
				return 1;
			if (g->moveList[i].fromFile == ALG_DROP)
				return 0;
			f = g->moveList[i].fromFile;
			r = g->moveList[i].fromRank;
		}
	}
#endif
	return 0;
}

PUBLIC int
pIsPlaying(int p)
{
	int	g = parray[p].game;
	int	p1 = parray[p].opponent;

	if (g < 0 || garray[g].status == GAME_EXAMINE) {
		pprintf(p, "You are not playing a game.\n");
		return 0;
	} else if (garray[g].white != p && garray[g].black != p) {
		/*
		 * oh oh; big bad game bug.
		 */

		fprintf(stderr, "BUG: Player %s playing game %d according to "
		    "parray, but not according to garray.\n",
		    parray[p].name, (g + 1));
		pprintf(p, "Disconnecting you from game number %d.\n", (g + 1));
		parray[p].game = -1;

		if (p1 >= 0 &&
		    parray[p1].game == g &&
		    garray[g].white != p1 &&
		    garray[g].black != p1) {
			pprintf(p1, "Disconnecting you from game number %d.\n",
			    (g + 1));
			parray[p1].game = -1;
		}

		return 0;
	} else
		return 1;
}

PUBLIC void
process_move(int p, char *command)
{
	int		 g;
	int		 i;
	int		 len;
	int		 result;
	move_t		 move;
	unsigned int	 now;

	if (parray[p].game < 0) {
		pprintf(p, "You are not playing or examining a game.\n");
		return;
	}

	player_decline_offers(p, -1, -PEND_SIMUL);
	g = parray[p].game;

	if (garray[g].status != GAME_EXAMINE) {
		if (!pIsPlaying(p)) // XXX
			return;
		if (parray[p].side != garray[g].game_state.onMove) {
			pprintf(p, "It is not your move.\n");
			return;
		}
		if (garray[g].clockStopped) {
			pprintf(p, "Game clock is paused, use \"unpause\" "
			    "to resume.\n");
			return;
		}
	}

	if ((len = strlen(command)) > 1) {
		if (command[len - 2] == '=') {
			switch (tolower(command[strlen(command) - 1])) {
			case 'n':
				parray[p].promote = KNIGHT;
				break;
			case 'b':
				parray[p].promote = BISHOP;
				break;
			case 'r':
				parray[p].promote = ROOK;
				break;
			case 'q':
				parray[p].promote = QUEEN;
				break;
			default:
				pprintf(p, "Don't understand that move.\n");
				return;
				break;
			}
		}
	}

	switch (parse_move(command, &garray[g].game_state, &move,
			   parray[p].promote)) {
	case MOVE_ILLEGAL:
		pprintf(p, "Illegal move.\n");
		return;
		break;
	case MOVE_AMBIGUOUS:
		pprintf(p, "Ambiguous move.\n");
		return;
		break;
	default:
		break;
	}

	if (garray[g].status == GAME_EXAMINE) {
		garray[g].numHalfMoves++;

		if (garray[g].numHalfMoves > garray[g].examMoveListSize) {
			garray[g].examMoveListSize += 20;	// Allocate 20
								// moves at a
								// time
			if (!garray[g].examMoveList) {
				garray[g].examMoveList =
				    reallocarray(NULL,
				    sizeof(move_t),
				    garray[g].examMoveListSize);
				if (garray[g].examMoveList == NULL)
					err(1, "%s: reallocarray", __func__);
				else
					malloc_count++;
			} else {
				garray[g].examMoveList =
				    reallocarray(garray[g].examMoveList,
				    sizeof(move_t),
				    garray[g].examMoveListSize);
				if (garray[g].examMoveList == NULL)
					err(1, "%s: reallocarray", __func__);
			}
		}

		now = tenth_secs();

		result		= execute_move(&garray[g].game_state, &move, 1);
		move.atTime	= now; // XXX
		move.tookTime	= 0;
		MakeFENpos(g, (char *)move.FENpos);
		garray[g].examMoveList[garray[g].numHalfMoves - 1] = move;

		/*
		 * roll back time
		 */

		if (garray[g].game_state.onMove == WHITE) {
			garray[g].wTime +=
			    (garray[g].lastDecTime - garray[g].lastMoveTime);
		} else {
			garray[g].bTime +=
			    (garray[g].lastDecTime - garray[g].lastMoveTime);
		}

		// XXX: 'now' was assigned here
		// <--

		if (garray[g].numHalfMoves == 0)
			garray[g].timeOfStart = now;

		garray[g].lastMoveTime = now;
		garray[g].lastDecTime = now;
	} else { // real game
		i = parray[p].opponent;

		if (parray[i].simul_info.numBoards &&
		    parray[i].simul_info.boards[parray[i].simul_info.onBoard] !=
		    g) {
			pprintf(p, "It isn't your turn: wait until the simul "
			    "giver is at your board.\n");
			return;
		}

#ifdef TIMESEAL
		if (con[parray[p].socket].timeseal) { // Does he use timeseal?
			if (parray[p].side == WHITE) {
				int diff;

				garray[g].wLastRealTime = garray[g].wRealTime;
				garray[g].wTimeWhenMoved =
				    con[parray[p].socket].time;

				diff = (garray[g].wTimeWhenMoved -
					garray[g].wTimeWhenReceivedMove);

				if (diff < 0 ||
				    garray[g].wTimeWhenReceivedMove == 0) {
					/*
					 * Might seem weird - but
					 * could be caused by a person
					 * moving BEFORE he receives
					 * the board pos (this is
					 * possible due to lag) but
					 * it's safe to say he moved
					 * in 0 secs.
					 */
					garray[g].wTimeWhenReceivedMove =
					    garray[g].wTimeWhenMoved;
				} else {
					garray[g].wRealTime -=
					    (garray[g].wTimeWhenMoved -
					    garray[g].wTimeWhenReceivedMove);
				}
			} else if (parray[p].side == BLACK) {
				int diff;

				garray[g].bLastRealTime = garray[g].bRealTime;
				garray[g].bTimeWhenMoved =
				    con[parray[p].socket].time;

				diff = (garray[g].bTimeWhenMoved -
					garray[g].bTimeWhenReceivedMove);

				if (diff < 0 ||
				    garray[g].bTimeWhenReceivedMove == 0) {
					/*
					 * Might seem weird - but
					 * could be caused by a person
					 * moving BEFORE he receives
					 * the board pos (this is
					 * possible due to lag) but
					 * it's safe to say he moved
					 * in 0 secs.
					 */
					garray[g].bTimeWhenReceivedMove =
					    garray[g].bTimeWhenMoved;
				} else {
					garray[g].bRealTime -=
					    (garray[g].bTimeWhenMoved -
					    garray[g].bTimeWhenReceivedMove);
				}
			}
		}

		/*
		 * We need to reset the opp's time for receiving the
		 * board since the timeseal decoder only alters the
		 * time if it's 0. Otherwise the time would be changed
		 * if the player did a refresh which would screw up
		 * the timings.
		 */
		if (parray[p].side == WHITE)
			garray[g].bTimeWhenReceivedMove = 0;
		else
			garray[g].wTimeWhenReceivedMove = 0;
#endif

		game_update_time(g);

		/*
		 * XXX: Maybe add autoflag here in the future?
		 */

#ifdef TIMESEAL
		if (con[parray[p].socket].timeseal) {  // Does he use timeseal?
			if (parray[p].side == WHITE) {
				garray[g].wRealTime +=
				    (garray[g].wIncrement * 100);
				garray[g].wTime = (garray[g].wRealTime / 100);
			} else if (parray[p].side == BLACK) {
				garray[g].bRealTime +=
				    (garray[g].bIncrement * 100);
				garray[g].bTime = (garray[g].bRealTime / 100);
			}
		} else {
			if (garray[g].game_state.onMove == BLACK)
				garray[g].bTime += garray[g].bIncrement;
			if (garray[g].game_state.onMove == WHITE)
				garray[g].wTime += garray[g].wIncrement;
		}
#else
		if (garray[g].game_state.onMove == BLACK)
			garray[g].bTime += garray[g].bIncrement;
		if (garray[g].game_state.onMove == WHITE)
			garray[g].wTime += garray[g].wIncrement;
#endif

		/*
		 * Do the move.
		 */

		garray[g].numHalfMoves++;

		if (garray[g].numHalfMoves > garray[g].moveListSize) {
			garray[g].moveListSize += 20;	// Allocate 20 moves at
							// a time

			if (!garray[g].moveList) {
				garray[g].moveList =
				    rmalloc(sizeof(move_t) *
				    garray[g].moveListSize);
			} else {
				garray[g].moveList =
				    rrealloc(garray[g].moveList,
				    (sizeof(move_t) * garray[g].moveListSize));
			}
		}

		result = execute_move(&garray[g].game_state, &move, 1);

		if (result == MOVE_OK &&
		    garray[g].link >= 0 &&
		    move.pieceCaptured != NOPIECE) {
			/*
			 * Transfer captured piece to partner.
			 * Check if piece reverts to a pawn.
			 */
			if (was_promoted(&garray[g], move.toFile, move.toRank)) {
				update_holding(garray[g].link,
				    colorval(move.pieceCaptured) | PAWN);
			} else {
				update_holding(garray[g].link,
				    move.pieceCaptured);
			}
		}

		now = tenth_secs();
		move.atTime = now;

		if (garray[g].numHalfMoves > 1)
			move.tookTime = (move.atTime - garray[g].lastMoveTime);
		else
			move.tookTime = (move.atTime - garray[g].startTime);

		garray[g].lastMoveTime = now;
		garray[g].lastDecTime = now;

#ifdef TIMESEAL
		if (con[parray[p].socket].timeseal) { // Does he use timeseal?
			if (parray[p].side == WHITE) {
				move.tookTime =
				    ((garray[parray[p].game].wTimeWhenMoved -
				    garray[parray[p].game].wTimeWhenReceivedMove) /
				    100);
			} else {
				move.tookTime =
				    ((garray[parray[p].game].bTimeWhenMoved -
				    garray[parray[p].game].bTimeWhenReceivedMove) /
				    100);
			}
		}
#endif

		MakeFENpos(g, (char *)move.FENpos);
		garray[g].moveList[garray[g].numHalfMoves - 1] = move;
	}

	send_boards(g);

	if (result == MOVE_ILLEGAL)
		pprintf(p, "Internal error, illegal move accepted!\n");
	if (result == MOVE_OK && garray[g].status == GAME_EXAMINE) {
		for (int p1 = 0; p1 < p_num; p1++) {
			if (parray[p1].status != PLAYER_PROMPT)
				continue;
			if (player_is_observe(p1, g) || parray[p1].game == g) {
				pprintf_prompt(p1, "%s moves: %s\n",
				    parray[p].name, move.algString);
			}
		}
	}

	if (result == MOVE_CHECKMATE) {
		if (garray[g].status == GAME_EXAMINE) {
			for (int p1 = 0; p1 < p_num; p1++) {
				if (parray[p1].status != PLAYER_PROMPT)
					continue;
				if (player_is_observe(p1, g) ||
				    parray[p1].game == g) {
					pprintf(p1, "%s has been checkmated.\n",
					    (CToggle(garray[g].game_state.onMove)
					    == BLACK ? "White" : "Black"));
				}
			}
		} else {
			game_ended(g, CToggle(garray[g].game_state.onMove),
			    END_CHECKMATE);
		}
	}

	if (result == MOVE_STALEMATE) {
		if (garray[g].status == GAME_EXAMINE) {
			for (int p1 = 0; p1 < p_num; p1++) {
				if (parray[p1].status != PLAYER_PROMPT)
					continue;
				if (player_is_observe(p1, g) ||
				    parray[p1].game == g)
					pprintf(p1, "Stalemate.\n");
			}
		} else {
			game_ended(g, CToggle(garray[g].game_state.onMove),
			    END_STALEMATE);
		}
	}

	if (result == MOVE_NOMATERIAL) {
		if (garray[g].status == GAME_EXAMINE) {
			for (int p1 = 0; p1 < p_num; p1++) {
				if (parray[p1].status != PLAYER_PROMPT)
					continue;
				if (player_is_observe(p1, g) ||
				    parray[p1].game == g)
					pprintf(p1, "No mating material.\n");
			}
		} else {
			game_ended(g, CToggle(garray[g].game_state.onMove),
			    END_NOMATERIAL);
		}
	}
}

PUBLIC int
com_resign(int p, param_list param)
{
	int	g, o, oconnected;

	if (param[0].type == TYPE_NULL) {
		g = parray[p].game;

		if (!pIsPlaying(p))
			return COM_OK;
		else {
			player_decline_offers(p, -1, -1);
			game_ended(g, (garray[g].white == p ? BLACK : WHITE),
			    END_RESIGN);
		}
	} else if (FindPlayer(p, param[0].val.word, &o, &oconnected)) {
		g = game_new();

		if (game_read(g, p, o) < 0) {
			if (game_read(g, o, p) < 0) {
				pprintf(p, "You have no stored game with %s\n",
				    parray[o].name);
				if (!oconnected)
					player_remove(o);
				return COM_OK;
			} else {
				garray[g].white = o;
				garray[g].black = p;
			}
		} else {
			garray[g].white = p;
			garray[g].black = o;
		}

		pprintf(p, "You resign your stored game with %s\n",
		    parray[o].name);

		game_delete(garray[g].white, garray[g].black);
		game_ended(g, (garray[g].white == p ? BLACK : WHITE),
		    END_RESIGN);

		pcommand(p, "message %s I have resigned our stored game "
		    "\"%s vs. %s.\"",
		    parray[o].name,
		    parray[garray[g].white].name,
		    parray[garray[g].black].name);

		if (!oconnected)
			player_remove(o);
	}

	return COM_OK;
}

PRIVATE int
Check50MoveRule(int p, int g)
{
	int	num_reversible = garray[g].numHalfMoves;

	if (garray[g].game_state.lastIrreversable >= 0)
		num_reversible -= garray[g].game_state.lastIrreversable;

	if (num_reversible > 99) {
		game_ended(g, (garray[g].white == p ? BLACK : WHITE),
		    END_50MOVERULE);
		return 1;
	}

	return 0;
}

PRIVATE char *
GetFENpos(int g, int half_move)
{
	if (half_move < 0)
		return ((char *)garray[g].FENstartPos);
	return ((char *)garray[g].moveList[half_move].FENpos);
}

PRIVATE int
CheckRepetition(int p, int g)
{
	char	*pos1 = GetFENpos(g, garray[g].numHalfMoves - 1);
	char	*pos2 = GetFENpos(g, garray[g].numHalfMoves);
	char	*pos;
	int	 flag1 = 1, flag2 = 1;
	int	 move_num;

	if (garray[g].numHalfMoves < 8) // Can't have three repeats any quicker.
		return 0;

	for (move_num = garray[g].game_state.lastIrreversable;
	    move_num < garray[g].numHalfMoves - 1;
	    move_num++) {
		pos = GetFENpos(g, move_num);

		if (strlen(pos1) == strlen(pos) && !strcmp(pos1, pos))
			flag1++;
		if (strlen(pos2) == strlen(pos) && !strcmp(pos2, pos))
			flag2++;
	}

	if (flag1 >= 3 || flag2 >= 3) {
		if (player_find_pendfrom(p, parray[p].opponent, PEND_DRAW)
		    >= 0) {
			player_remove_request(parray[p].opponent, p, PEND_DRAW);
			player_decline_offers(p, -1, -1);
		}

		game_ended(g, (garray[g].white == p ? BLACK : WHITE),
		    END_REPETITION);
		return 1;
	} else
		return 0;
}

PUBLIC int
com_draw(int p, param_list param)
{
	int p1, g = parray[p].game;

	ASSERT(param[0].type == TYPE_NULL);

	if (!pIsPlaying(p))
		return COM_OK;
	if (Check50MoveRule(p, g) || CheckRepetition(p, g))
		return COM_OK;

	p1 = parray[p].opponent;

	if (parray[p1].simul_info.numBoards &&
	    parray[p1].simul_info.boards[parray[p1].simul_info.onBoard] != g) {
		pprintf(p, "You can only make requests when the simul player "
		    "is at your board.\n");
		return COM_OK;
	}

	if (player_find_pendfrom(p, parray[p].opponent, PEND_DRAW) >= 0) {
		player_remove_request(parray[p].opponent, p, PEND_DRAW);
		player_decline_offers(p, -1, -1);
		game_ended(g, (garray[g].white == p ? BLACK : WHITE),
		    END_AGREEDDRAW);
	} else {
		pprintf(parray[p].opponent, "\n");
		pprintf_highlight(parray[p].opponent, "%s", parray[p].name);
		pprintf_prompt(parray[p].opponent, " offers you a draw.\n");
		pprintf(p, "Draw request sent.\n");
		player_add_request(p, parray[p].opponent, PEND_DRAW, 0);
	}

	return COM_OK;
}

PUBLIC int
com_pause(int p, param_list param)
{
	int	g;
	int	now;

	ASSERT(param[0].type == TYPE_NULL);

	if (!pIsPlaying(p))
		return COM_OK;

	g = parray[p].game;

	if (garray[g].wTime == 0) {
		pprintf(p, "You can't pause untimed games.\n");
		return COM_OK;
	}

	if (garray[g].clockStopped) {
		pprintf(p, "Game is already paused, "
		    "use \"unpause\" to resume.\n");
		return COM_OK;
	}

	if (player_find_pendfrom(p, parray[p].opponent, PEND_PAUSE) >= 0) {
		player_remove_request(parray[p].opponent, p, PEND_PAUSE);
		garray[g].clockStopped = 1;

		// Roll back the time
		if (garray[g].game_state.onMove == WHITE) {
			garray[g].wTime += (garray[g].lastDecTime -
					    garray[g].lastMoveTime);
		} else {
			garray[g].bTime += (garray[g].lastDecTime -
					    garray[g].lastMoveTime);
		}

		now = tenth_secs();

		if (garray[g].numHalfMoves == 0)
			garray[g].timeOfStart = now;

		garray[g].lastMoveTime = now;
		garray[g].lastDecTime = now;

		send_boards(g);

		pprintf_prompt(parray[p].opponent, "\n%s accepted pause. "
		    "Game clock paused.\n", parray[p].name);
		pprintf(p, "Game clock paused.\n");
	} else {
		pprintf(parray[p].opponent, "\n");
		pprintf_highlight(parray[p].opponent, "%s", parray[p].name);
		pprintf_prompt(parray[p].opponent, " requests to pause the "
		    "game.\n");
		pprintf(p, "Pause request sent.\n");
		player_add_request(p, parray[p].opponent, PEND_PAUSE, 0);
	}

	return COM_OK;
}

PUBLIC int
com_unpause(int p, param_list param)
{
	int	g;
	int	now;

	ASSERT(param[0].type == TYPE_NULL);

	if (!pIsPlaying(p))
		return COM_OK;

	g = parray[p].game;

	if (!garray[g].clockStopped) {
		pprintf(p, "Game is not paused.\n");
		return COM_OK;
	}

	garray[g].clockStopped = 0;
	now = tenth_secs();

	if (garray[g].numHalfMoves == 0)
		garray[g].timeOfStart = now;

	garray[g].lastMoveTime = now;
	garray[g].lastDecTime = now;

	send_boards(g);

	pprintf(p, "Game clock resumed.\n");
	pprintf_prompt(parray[p].opponent, "\nGame clock resumed.\n");

	return COM_OK;
}

PUBLIC int
com_abort(int p, param_list param)
{
	int courtesyOK = 1;
	int p1, g, myColor, yourColor, myGTime, yourGTime;

	ASSERT(param[0].type == TYPE_NULL);

	g = parray[p].game;

	if (!pIsPlaying(p))
		return COM_OK;

	p1 = parray[p].opponent;

	if (p == garray[g].white) {
		myColor		= WHITE;
		yourColor	= BLACK;
		myGTime		= garray[g].wTime;
		yourGTime	= garray[g].bTime;
	} else {
		myColor		= BLACK;
		yourColor	= WHITE;
		myGTime		= garray[g].bTime;
		yourGTime	= garray[g].wTime;
	}

	if (parray[p1].simul_info.numBoards &&
	    parray[p1].simul_info.boards[parray[p1].simul_info.onBoard] != g) {
		pprintf(p, "You can only make requests when the simul player "
		    "is at your board.\n");
		return COM_OK;
	}

	if (player_find_pendfrom(p, p1, PEND_ABORT) >= 0) {
		player_remove_request(p1, p, PEND_ABORT);
		player_decline_offers(p, -1, -1);
		game_ended(g, yourColor, END_ABORT);
	} else {
		game_update_time(g);

#ifdef TIMESEAL
		if (con[parray[p].socket].timeseal &&
		    garray[g].game_state.onMove == myColor &&
		    garray[g].flag_pending == FLAG_ABORT) {
			/*
			 * It's my move, opponent has asked for abort;
			 * I lagged out, my timeseal prevented
			 * courtesyabort, and I sent an abort request
			 * before acknowledging (and processing) my
			 * opponent's courtesyabort. OK, let's abort
			 * already :-).
			 */

			player_decline_offers(p, -1, -1);
			game_ended(g, yourColor, END_ABORT);
		}

		if (con[parray[p1].socket].timeseal) { // Opp uses timeseal?
			int yourRealTime = (myColor == WHITE ?
			    garray[g].bRealTime : garray[g].wRealTime);

			if (myGTime > 0 && yourGTime <= 0 && yourRealTime > 0) {
				/*
				 * Override courtesyabort; opponent
				 * still has time. Check for lag.
				 */
				courtesyOK = 0;

				if (garray[g].game_state.onMove != myColor &&
				    garray[g].flag_pending != FLAG_CHECKING) {
					// Opponent may be lagging; let's ask.
					garray[g].flag_pending = FLAG_ABORT;
					garray[g].flag_check_time = time(0);

					pprintf(p, "Opponent has timeseal; "
					    "trying to courtesyabort.\n");
					pprintf(p1, "\n[G]\n");

					return COM_OK;
				}
			}
		}
#endif

		if (myGTime > 0 && yourGTime <= 0 && courtesyOK) {
			/*
			 * Player wants to abort + opponent is out of
			 * time = courtesyabort.
			 */

			pprintf(p, "Since you have time, and your opponent "
			    "has none, the game has been aborted.");
			pprintf(p1, "Your opponent has aborted the game "
			    "rather than calling your flag.");
			player_decline_offers(p, -1, -1);
			game_ended(g, myColor, END_COURTESY);
		} else {
			pprintf(p1, "\n");
			pprintf_highlight(p1, "%s", parray[p].name);
			pprintf(p1, " would like to abort the game; ");
			pprintf_prompt(p1, "type \"abort\" to accept.\n");
			pprintf(p, "Abort request sent.\n");
			player_add_request(p, p1, PEND_ABORT, 0);
		}
	}

	return COM_OK;
}

PUBLIC int
com_courtesyabort(int p, param_list param)
{
	pprintf(p, "Courtesyabort is obsolete; use \"abort\" instead.\n");
	return COM_OK;
}

PUBLIC int
com_courtesyadjourn(int p, param_list param)
{
	pprintf(p, "Use \"adjourn\" to courtesyadjourn a game.\n");
	return COM_OK;
}

PRIVATE int
player_has_mating_material(game_state_t *gs, int color)
{
	int	i, j;
	int	minor_pieces = 0;
	int	piece;

	for (i = 0; i < 8; i++) {
		for (j = 0; j < 8; j++) {
			piece = gs->board[i][j];

			switch (piecetype(piece)) {
			case BISHOP:
			case KNIGHT:
				if (iscolor(piece, color))
					minor_pieces++;
				break;
			case KING:
			case NOPIECE:
				break;
			default:
				if (iscolor(piece, color))
					return 1;
			}
		}
	}

	return (minor_pieces > 1 ? 1 : 0);
}

PUBLIC int
com_flag(int p, param_list param)
{
	int	g;
	int	myColor;

	ASSERT(param[0].type == TYPE_NULL);

	if (!pIsPlaying(p))
		return COM_OK;

	g = parray[p].game;
	myColor = (p == garray[g].white ? WHITE : BLACK);

	if (garray[g].type == TYPE_UNTIMED) {
		pprintf(p, "You can't flag an untimed game.\n");
		return COM_OK;
	}

	if (garray[g].numHalfMoves < 2) {
		pprintf(p, "You cannot flag before both players have moved.\n"
		    "Use abort instead.\n");
		return COM_OK;
	}

	game_update_time(g);

#ifdef TIMESEAL
	{
		int	myTime, yourTime, serverTime;
		int	opp = parray[p].opponent;

		if (con[parray[p].socket].timeseal) {	// Does the caller use
							// timeseal?
			myTime = (myColor == WHITE ? garray[g].wRealTime :
			    garray[g].bRealTime);
		} else {
			myTime = (myColor == WHITE ? garray[g].wTime :
			    garray[g].bTime);
		}

		serverTime = (myColor == WHITE ? garray[g].bTime :
		    garray[g].wTime);

		if (con[parray[opp].socket].timeseal) { // Opp uses timeseal?
			yourTime = (myColor == WHITE ? garray[g].bRealTime :
			    garray[g].wRealTime);
		} else {
			yourTime = serverTime;
		}

		// The clocks to compare are now in 'myTime' and 'yourTime'.
		if (myTime <= 0 && yourTime <= 0) {
			player_decline_offers(p, -1, -1);
			game_ended(g, myColor, END_BOTHFLAG);
			return COM_OK;
		}

		if (yourTime > 0) {
			/*
			 * Opponent still has time, but if that's only
			 * because s/he may be lagging, we should ask
			 * for an acknowledgement and then try to call
			 * the flag.
			 */

			if (serverTime <= 0 &&
			    garray[g].game_state.onMove != myColor &&
			    garray[g].flag_pending != FLAG_CHECKING) {
				garray[g].flag_pending = FLAG_CALLED;
				garray[g].flag_check_time = time(0);

				pprintf(p, "Opponent has timeseal; "
				    "checking if (s)he's lagging.\n");
				pprintf(opp, "\n[G]\n");

				return COM_OK;
			}

			/*
			 * If we're here, it means:
			 * 1) The server agrees opponent has time, whether
			 *    lagging or not.
			 * 2) Opponent has timeseal (if yourTime != serverTime),
			 *    had time left after the last move (yourTime > 0),
			 *    and it's still your move.
			 * 3) We're currently checking a flag call after having
			 *    receiving acknowledgement from the other timeseal
			 *    (and would have reset 'yourTime' if the flag were
			 *    down).
			 */
			pprintf(p, "Your opponent is not out of time!\n");
			return COM_OK;
		}
	}
#else	// !defined(TIMESEAL)
	if (garray[g].wTime <= 0 && garray[g].bTime <= 0) {
		player_decline_offers(p, -1, -1);
		game_ended(g, myColor, END_BOTHFLAG);
		return COM_OK;
	}

	if (myColor == WHITE) {
		if (garray[g].bTime > 0) {
			pprintf(p, "Your opponent is not out of time!\n");
			return COM_OK;
		}
	} else {
		if (garray[g].wTime > 0) {
			pprintf(p, "Your opponent is not out of time!\n");
			return COM_OK;
		}
	}
#endif

	player_decline_offers(p, -1, -1);

	if (player_has_mating_material(&garray[g].game_state, myColor))
		game_ended(g, myColor, END_FLAG);
	else
		game_ended(g, myColor, END_FLAGNOMATERIAL);

	return COM_OK;
}

PUBLIC int
com_adjourn(int p, param_list param)
{
	int	p1, g, myColor, yourColor;

	ASSERT(param[0].type == TYPE_NULL);

	if (!pIsPlaying(p))
		return COM_OK;

	p1 = parray[p].opponent;
	g = parray[p].game;

	if (!(parray[p].registered && parray[p1].registered)) {
		pprintf(p, "Both players must be registered to adjorn a game. "
		    "Use \"abort\".\n");
		return COM_OK;
	}

	if (garray[g].link >= 0) {
		pprintf(p, "Bughouse games cannot be adjourned.\n");
		return COM_OK;
	}

	myColor		= (p == garray[g].white ? WHITE : BLACK);
	yourColor	= (myColor == WHITE ? BLACK : WHITE);

	if (player_find_pendfrom(p, p1, PEND_ADJOURN) >= 0) {
		player_remove_request(p1, p, PEND_ADJOURN);
		player_decline_offers(p, -1, -1);
		game_ended(parray[p].game, yourColor, END_ADJOURN);
	} else {
		game_update_time(g);

		if ((myColor == WHITE &&
		    garray[g].wTime > 0 &&
		    garray[g].bTime <= 0) ||
		    (myColor == BLACK &&
		    garray[g].bTime > 0 &&
		    garray[g].wTime <= 0)) {
			/*
			 * Player wants to adjourn + opponent is out
			 * of time = courtesyadjourn.
			 */

			pprintf(p, "Since you have time, and your opponent "
			    "has none, the game has been adjourned.");
			pprintf(p1, "Your opponent has adjourned the game "
			    "rather than calling your flag.");
			player_decline_offers(p, -1, -1);
			game_ended(g, myColor, END_COURTESYADJOURN);
		} else {
			pprintf(p1, "\n");
			pprintf_highlight(p1, "%s", parray[p].name);
			pprintf(p1, " would like to adjourn the game; ");
			pprintf_prompt(p1, "type \"adjourn\" to accept.\n");
			pprintf(p, "Adjourn request sent.\n");
			player_add_request(p, p1, PEND_ADJOURN, 0);
		}
	}

	return COM_OK;
}

PUBLIC int
com_takeback(int p, param_list param)
{
	int	from;
	int	g, i;
	int	nHalfMoves = 1;
	int	p1;

	if (!pIsPlaying(p))
		return COM_OK;

	p1 = parray[p].opponent;

	if (parray[p1].simul_info.numBoards &&
	    parray[p1].simul_info.boards[parray[p1].simul_info.onBoard] !=
	    parray[p].game) {
		pprintf(p, "You can only make requests when the simul player "
		    "is at your board.\n");
		return COM_OK;
	}

	g = parray[p].game;

	if (garray[g].link >= 0) {
		pprintf(p, "Takeback not implemented for bughouse games yet."
		    "\n");
		return COM_OK;
	}

	if (param[0].type == TYPE_INT)
		nHalfMoves = param[0].val.integer;

	if ((from = player_find_pendfrom(p, parray[p].opponent, PEND_TAKEBACK))
	    >= 0) {
		player_remove_request(parray[p].opponent, p, PEND_TAKEBACK);

		if (parray[p].p_from_list[from].param1 == nHalfMoves) {
			// Doing the takeback
			player_decline_offers(p, -1, -PEND_SIMUL);

			for (i = 0; i < nHalfMoves; i++) {
				if (backup_move(g, REL_GAME) != MOVE_OK) {
					pprintf(garray[g].white, "Can only "
					    "backup %d moves\n", i);
					pprintf(garray[g].black, "Can only "
					    "backup %d moves\n", i);
					break;
				}
			}

#ifdef TIMESEAL
			garray[g].wTimeWhenReceivedMove = 0;
			garray[g].bTimeWhenReceivedMove = 0;
#endif

			send_boards(g);
		} else {
			if (garray[g].numHalfMoves < nHalfMoves) {
				pprintf(p, "There are only %d half moves in "
				    "your game.\n", garray[g].numHalfMoves);
				pprintf_prompt(parray[p].opponent, "\n%s has "
				    "declined the takeback request.\n",
				    parray[p].name);
				return COM_OK;
			}

			pprintf(p, "You disagree on the number of half-moves "
			    "to takeback.\n");
			pprintf(p, "Alternate takeback request sent.\n");
			pprintf_prompt(parray[p].opponent, "\n%s proposes a "
			    "different number (%d) of half-move(s).\n",
			    parray[p].name,
			    nHalfMoves);
			player_add_request(p, parray[p].opponent, PEND_TAKEBACK,
			    nHalfMoves);
		}
	} else {
		if (garray[g].numHalfMoves < nHalfMoves) {
			pprintf(p, "There are only %d half moves in your game."
			    "\n", garray[g].numHalfMoves);
			return COM_OK;
		}

		pprintf(parray[p].opponent, "\n");
		pprintf_highlight(parray[p].opponent, "%s", parray[p].name);
		pprintf_prompt(parray[p].opponent, " would like to take back "
		    "%d half move(s).\n", nHalfMoves);
		pprintf(p, "Takeback request sent.\n");
		player_add_request(p, parray[p].opponent, PEND_TAKEBACK,
		    nHalfMoves);
	}

	return COM_OK;
}

PUBLIC int
com_switch(int p, param_list param)
{
	char	*strTmp;
	int	 g = parray[p].game;
	int	 p1;
	int	 tmp, now;

	if (!pIsPlaying(p))
		return COM_OK;

	p1 = parray[p].opponent;

	if (parray[p1].simul_info.numBoards &&
	    parray[p1].simul_info.boards[parray[p1].simul_info.onBoard] != g) {
		pprintf(p, "You can only make requests when the simul player "
		    "is at your board.\n");
		return COM_OK;
	}

	if (garray[g].link >= 0) {
		pprintf(p, "Switch not implemented for bughouse games.\n");
		return COM_OK;
	}

	if (player_find_pendfrom(p, parray[p].opponent, PEND_SWITCH) >= 0) {
		player_remove_request(parray[p].opponent, p, PEND_SWITCH);
		player_decline_offers(p, -1, -PEND_SIMUL);

		tmp = garray[g].white;
		garray[g].white = garray[g].black;
		garray[g].black = tmp;
		parray[p].side = (parray[p].side == WHITE ? BLACK : WHITE);

		strTmp = xstrdup(garray[g].white_name);
		mstrlcpy(garray[g].white_name, garray[g].black_name,
		    sizeof(garray[g].white_name));
		mstrlcpy(garray[g].black_name, strTmp,
		    sizeof(garray[g].black_name));
		strfree(strTmp);

		parray[parray[p].opponent].side =
		    (parray[parray[p].opponent].side == WHITE ? BLACK : WHITE);

		// Roll back the time
		if (garray[g].game_state.onMove == WHITE) {
			garray[g].wTime += (garray[g].lastDecTime -
					    garray[g].lastMoveTime);
		} else {
			garray[g].bTime += (garray[g].lastDecTime -
					    garray[g].lastMoveTime);
		}

		now = tenth_secs();

		if (garray[g].numHalfMoves == 0)
			garray[g].timeOfStart = now;

		garray[g].lastMoveTime = now;
		garray[g].lastDecTime = now;

		send_boards(g);
		return COM_OK;
	}

	if (garray[g].rated && garray[g].numHalfMoves > 0) {
		pprintf(p, "You cannot switch sides once a rated game is "
		    "underway.\n");
		return COM_OK;
	}

	pprintf(parray[p].opponent, "\n");
	pprintf_highlight(parray[p].opponent, "%s", parray[p].name);
	pprintf_prompt(parray[p].opponent, " would like to switch sides.\n"
	    "Type \"accept\" to switch sides, or \"decline\" to refuse.\n");
	pprintf(p, "Switch request sent.\n");
	player_add_request(p, parray[p].opponent, PEND_SWITCH, 0);

	return COM_OK;
}

PUBLIC int
com_time(int p, param_list param)
{
	int	p1, g;

	if (param[0].type == TYPE_NULL) {
		g = parray[p].game;

		if (!pIsPlaying(p))
			return COM_OK;
	} else {
		if ((g = GameNumFromParam(p, &p1, &param[0])) < 0)
			return COM_OK;
	}

	if (g < 0 || g >= g_num || garray[g].status != GAME_ACTIVE) {
		pprintf(p, "There is no such game.\n");
		return COM_OK;
	}

	game_update_time(g);

	pprintf(p, "White (%s) : %d mins, %d secs\n",
		parray[garray[g].white].name,
		garray[g].wTime / 600,
		(garray[g].wTime - ((garray[g].wTime / 600) * 600)) / 10);
	pprintf(p, "Black (%s) : %d mins, %d secs\n",
		parray[garray[g].black].name,
		garray[g].bTime / 600,
		(garray[g].bTime - ((garray[g].bTime / 600) * 600)) / 10);

	return COM_OK;
}

PUBLIC int
com_boards(int p, param_list param)
{
	DIR		*dirp;
	char		*category = NULL;
	char		 dname[MAX_FILENAME_SIZE] = { '\0' };
#ifdef USE_DIRENT
	struct dirent	*dp;
#else
	struct direct	*dp;
#endif

	if (param[0].type == TYPE_WORD)
		category = param[0].val.word;

	if (category) {
		pprintf(p, "Boards Available For Category %s:\n", category);
		msnprintf(dname, sizeof dname, "%s/%s", board_dir, category);
	} else {
		pprintf(p, "Categories Available:\n");
		msnprintf(dname, sizeof dname, "%s", board_dir);
	}

	if ((dirp = opendir(dname)) == NULL) {
		pprintf(p, "No such category %s, try \"boards\".\n", category);
		return COM_OK;
	}

	// YUK! What a mess, how about printing an ordered directory? - DAV
	for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
		if (!strcmp(dp->d_name, "."))
			continue;
		if (!strcmp(dp->d_name, ".."))
			continue;
		pprintf(p, "%s\n", dp->d_name);
	}

	closedir(dirp);
	return COM_OK;
}

PUBLIC int
com_simmatch(int p, param_list param)
{
	char	tmp[100] = { '\0' };
	int	num;
	int	p1, g, adjourned;

	if (parray[p].game >= 0 && garray[parray[p].game].status ==
	    GAME_EXAMINE) {
		pprintf(p, "You are still examining a game.\n");
		return COM_OK;
	}

	p1 = player_find_part_login(param[0].val.word);

	if (p1 < 0) {
		pprintf(p, "No user named \"%s\" is logged in.\n",
		    param[0].val.word);
		return COM_OK;
	}

	if (p == p1) {
		pprintf(p, "You can't simmatch yourself!\n");
		return COM_OK;
	}

	if (player_find_pendfrom(p, p1, PEND_SIMUL) >= 0) {
		player_remove_request(p, p1, PEND_MATCH);
		player_remove_request(p1, p, PEND_MATCH);
		player_remove_request(p, p1, PEND_SIMUL);
		player_remove_request(p1, p, PEND_SIMUL);
		player_withdraw_offers(p, -1, PEND_SIMUL);
		player_decline_offers(p1, -1, PEND_SIMUL);
		player_withdraw_offers(p1, -1, PEND_SIMUL);
		player_decline_offers(p, -1, PEND_MATCH);
		player_withdraw_offers(p, -1, PEND_MATCH);
		player_decline_offers(p1, -1, PEND_MATCH);
		player_withdraw_offers(p1, -1, PEND_MATCH);
		player_decline_offers(p, -1, PEND_PARTNER);
		player_withdraw_offers(p, -1, PEND_PARTNER);
		player_decline_offers(p1, -1, PEND_PARTNER);
		player_withdraw_offers(p1, -1, PEND_PARTNER);

		if (parray[p].simul_info.numBoards >= MAX_SIMUL) {
			pprintf(p, "You are already playing the maximum of %d "
			    "boards.\n", MAX_SIMUL);
			pprintf(p1, "Simul request removed, boards filled.\n");
			return COM_OK;
		}

		// stop observing when match starts
		unobserveAll(p);
		unobserveAll(p1);

		g = game_new();
		adjourned = 0;

		if (game_read(g, p, p1) >= 0)
			adjourned = 1;

		if (!adjourned) { // no adjourned game - so begin a new game.
			game_remove(g);

			if (create_new_match(p, p1, 0, 0, 0, 0, 0, "standard",
			    "standard", 1)) {
				pprintf(p, "There was a problem creating the "
				    "new match.\n");
				pprintf_prompt(p1, "There was a problem "
				    "creating the new match.\n");
				return COM_OK;
			}
		} else { // resume adjourned game
			game_delete(p, p1);

			msnprintf(tmp, sizeof tmp, "{Game %d (%s vs. %s) "
			    "Continuing %s %s simul.}\n",
			    (g + 1),
			    parray[p].name,
			    parray[p1].name,
			    rstr[garray[g].rated],
			    bstr[garray[g].type]);

			pprintf(p, "%s", tmp);
			pprintf(p1, "%s", tmp);

			garray[g].white = p;
			garray[g].black = p1;
			garray[g].status = GAME_ACTIVE;
			garray[g].startTime = tenth_secs();
			garray[g].lastMoveTime = garray[g].startTime;
			garray[g].lastDecTime = garray[g].startTime;
			parray[p].game = g;
			parray[p].opponent = p1;
			parray[p].side = WHITE;
			parray[p1].game = g;
			parray[p1].opponent = p;
			parray[p1].side = BLACK;

			send_boards(g);
		}

		num = parray[p].simul_info.numBoards;

		parray[p].simul_info.results[num] = -1;
		parray[p].simul_info.boards[num] = parray[p].game;
		parray[p].simul_info.numBoards++;

		if (parray[p].simul_info.numBoards > 1 &&
		    parray[p].simul_info.onBoard >= 0)
			player_goto_board(p, parray[p].simul_info.onBoard);
		else
			parray[p].simul_info.onBoard = 0;
		return COM_OK;
	}

	if (player_find_pendfrom(p, -1, PEND_SIMUL) >= 0) {
		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");
		return COM_OK;
	}

	if (parray[p].simul_info.numBoards) {
		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");
		return COM_OK;
	}

	if (parray[p].game >= 0) {
		pprintf(p, "You are already playing a game.\n");
		return COM_OK;
	}

	if (!parray[p1].sopen) {
		pprintf_highlight(p, "%s", parray[p1].name);
		pprintf(p, " is not open to receiving simul requests.\n");
		return COM_OK;
	}

	if (parray[p1].simul_info.numBoards >= MAX_SIMUL) {
		pprintf_highlight(p, "%s", parray[p1].name);
		pprintf(p, " is already playing the maximum of %d boards.\n",
		    MAX_SIMUL);
		return COM_OK;
	}

	// loon: checking for some crazy situations we can't allow :)
	if (parray[p1].game >= 0 && parray[p1].simul_info.numBoards == 0) {
		pprintf_highlight(p, "%s", parray[p1].name);

		if (parray[garray[parray[p1].game].white].simul_info.numBoards) {
			pprintf(p, " is playing in ");
			pprintf_highlight(p, "%s",
			    parray[parray[p1].opponent].name);
			pprintf(p, "'s simul, and can't accept.\n");
		} else {
			pprintf(p, " can't begin a simul while playing a "
			    "non-simul game.\n");
		}

		return COM_OK;
	}

	g = game_new();
	adjourned = ((game_read(g, p, p1) < 0 && game_read(g, p1, p) < 0)
	    ? 0 : 1);

	if (adjourned) {
		if (!(garray[g].type == TYPE_UNTIMED))
			adjourned = 0;
	}

	game_remove(g);

	if (player_add_request(p, p1, PEND_SIMUL, 0)) {
		pprintf(p, "Maximum number of pending actions reached. "
		    "Your request was not sent.\nTry again later.\n");
		return COM_OK;
	} else {

		pprintf(p1, "\n");
		pprintf_highlight(p1, "%s", parray[p].name);

		if (adjourned) {
			pprintf_prompt(p1, " requests to continue an "
			    "adjourned simul game.\n");
			pprintf(p, "Request to resume simul sent. "
			    "Adjourned game found.\n");
		} else {
			pprintf_prompt(p1, " requests to join a simul match "
			    "with you.\n");
			pprintf(p, "Simul match request sent.\n");
		}
	}

	return COM_OK;
}

PUBLIC int
com_goboard(int p, param_list param)
{
	int on, g, p1;

	if (!parray[p].simul_info.numBoards) {
		pprintf(p, "You are not giving a simul.\n");
		return COM_OK;
	}

	p1 = player_find_part_login(param[0].val.word);

	if (p1 < 0) {
		pprintf(p, "No user named \"%s\" is logged in.\n",
		    param[0].val.word);
		return COM_OK;
	}

	if (p == p1) {
		pprintf(p, "You can't goboard yourself!\n");
		return COM_OK;
	}

	on = parray[p].simul_info.onBoard;
	g = parray[p].simul_info.boards[on];

	if (p1 == garray[g].black) {
		pprintf(p, "You are already at that board!\n");
		return COM_OK;
	}

	if (parray[p].simul_info.numBoards > 1) {
		player_decline_offers(p, -1, -PEND_SIMUL);

		if (player_goto_simulgame_bynum(p, parray[p1].game) != -1) {
			if (g >= 0) {
				pprintf(garray[g].black, "\n");
				pprintf_highlight(garray[g].black, "%s",
				    parray[p].name);
				pprintf_prompt(garray[g].black, " has moved "
				    "away from your board.\n");
			}
		}
	} else
		pprintf(p, "You are only playing one board!\n");
	return COM_OK;
}

PUBLIC int
com_gonum(int p, param_list param)
{
	int on, g, gamenum;

	if (!parray[p].simul_info.numBoards) {
		pprintf(p, "You are not giving a simul.\n");
		return COM_OK;
	}

	on = parray[p].simul_info.onBoard;
	g = parray[p].simul_info.boards[on];
	gamenum = param[0].val.integer - 1;

	if (gamenum < 0)
		gamenum = 0;

	if (on == gamenum) {
		pprintf(p, "You are already at that board!\n");
		return COM_OK;
	}

	if (parray[p].simul_info.numBoards > 1) {
		player_decline_offers(p, -1, -PEND_SIMUL);

		if (player_goto_simulgame_bynum(p, gamenum) != -1) {
			if (g >= 0) {
				pprintf(garray[g].black, "\n");
				pprintf_highlight(garray[g].black, "%s",
				    parray[p].name);
				pprintf_prompt(garray[g].black, " has moved "
				    "away from your board.\n");
			}
		}
	} else
		pprintf(p, "You are only playing one board!\n");
	return COM_OK;
}

PUBLIC int
com_simnext(int p, param_list param)
{
	int on, g;

	if (!parray[p].simul_info.numBoards) {
		pprintf(p, "You are not giving a simul.\n");
		return COM_OK;
	}

	if (parray[p].simul_info.numBoards > 1) {
		player_decline_offers(p, -1, -PEND_SIMUL);
		on = parray[p].simul_info.onBoard;
		g = parray[p].simul_info.boards[on];

		if (g >= 0) {
			pprintf(garray[g].black, "\n");
			pprintf_highlight(garray[g].black, "%s",
			    parray[p].name);
			pprintf_prompt(garray[g].black, " is moving away from "
			    "your board.\n");
			player_goto_next_board(p);
		}
	} else
		pprintf(p, "You are only playing one board!\n");
	return COM_OK;
}

PUBLIC int
com_simprev(int p, param_list param)
{
	int on, g;

	if (!parray[p].simul_info.numBoards) {
		pprintf(p, "You are not giving a simul.\n");
		return COM_OK;
	}

	if (parray[p].simul_info.numBoards > 1) {
		player_decline_offers(p, -1, -PEND_SIMUL);
		on = parray[p].simul_info.onBoard;
		g = parray[p].simul_info.boards[on];

		if (g >= 0) {
			pprintf(garray[g].black, "\n");
			pprintf_highlight(garray[g].black, "%s",
			    parray[p].name);
			pprintf_prompt(garray[g].black, " is moving back to "
			    "the previous board.\n");
		}

		player_goto_prev_board(p);
	} else
		pprintf(p, "You are only playing one board!\n");
	return COM_OK;
}

PUBLIC int
com_simgames(int p, param_list param)
{
	int p1 = p;

	if (param[0].type == TYPE_WORD) {
		if ((p1 = player_find_part_login(param[0].val.word)) < 0) {
			pprintf(p, "No player named %s is logged in.\n",
			    param[0].val.word);
			return COM_OK;
		}
	}

	if (p1 == p) {
		pprintf(p, "You are playing %d simultaneous games.\n",
		    player_num_active_boards(p1));
	} else {
		pprintf(p, "%s is playing %d simultaneous games.\n",
		    parray[p1].name, player_num_active_boards(p1));
	}

	return COM_OK;
}

PUBLIC int
com_simpass(int p, param_list param)
{
	int	g, p1, on;

	if (!pIsPlaying(p))
		return COM_OK;

	g	= parray[p].game;
	p1	= garray[g].white;

	if (!parray[p1].simul_info.numBoards) {
		pprintf(p, "You are not participating in a simul.\n");
		return COM_OK;
	}

	if (p == p1) {
		pprintf(p, "You are the simul holder and cannot pass!\n");
		return COM_OK;
	}

	if (player_num_active_boards(p1) == 1) {
		pprintf(p, "This is the only game, so passing is futile.\n");
		return COM_OK;
	}

	on = parray[p1].simul_info.onBoard;

	if (parray[p1].simul_info.boards[on] != g) {
		pprintf(p, "You cannot pass until the simul holder arrives!\n");
		return COM_OK;
	}

	if (garray[g].passes >= MAX_SIMPASS) {
		if (parray[p].bell)
			pprintf(p, "\a");
		pprintf(p, "You have reached your maximum of %d pass(es).\n",
		    MAX_SIMPASS);
		pprintf(p, "Please move IMMEDIATELY!\n");
		pprintf_highlight(p1, "%s", parray[p].name);
		pprintf_prompt(p1, " tried to pass, but is out of passes.\n");
		return COM_OK;
	}

	player_decline_offers(p, -1, -PEND_SIMUL);
	garray[g].passes++;

	pprintf(p, "You have passed and have %d pass(es) left.\n",
	    (MAX_SIMPASS - garray[g].passes));
	pprintf_highlight(p1, "%s", parray[p].name);
	pprintf_prompt(p1, " has decided to pass and has %d pass(es) left.\n",
	    (MAX_SIMPASS - garray[g].passes));
	player_goto_next_board(p1);

	return COM_OK;
}

PUBLIC int
com_simabort(int p, param_list param)
{
	if (!parray[p].simul_info.numBoards) {
		pprintf(p, "You are not giving a simul.\n");
		return COM_OK;
	}

	player_decline_offers(p, -1, -PEND_SIMUL);
	game_ended(parray[p].simul_info.boards[parray[p].simul_info.onBoard],
	    WHITE, END_ABORT);

	return COM_OK;
}

PUBLIC int
com_simallabort(int p, param_list param)
{
	if (!parray[p].simul_info.numBoards) {
		pprintf(p, "You are not giving a simul.\n");
		return COM_OK;
	}

	player_decline_offers(p, -1, -PEND_SIMUL);

	for (int i = 0; i < parray[p].simul_info.numBoards; i++) {
		if (parray[p].simul_info.boards[i] >= 0) {
			game_ended(parray[p].simul_info.boards[i], WHITE,
			    END_ABORT);
		}
	}

	return COM_OK;
}

PUBLIC int
com_simadjourn(int p, param_list param)
{
	if (!parray[p].simul_info.numBoards) {
		pprintf(p, "You are not giving a simul.\n");
		return COM_OK;
	}

	player_decline_offers(p, -1, -PEND_SIMUL);
	game_ended(parray[p].simul_info.boards[parray[p].simul_info.onBoard],
	    WHITE, END_ADJOURN);

	return COM_OK;
}

PUBLIC int
com_simalladjourn(int p, param_list param)
{
	if (!parray[p].simul_info.numBoards) {
		pprintf(p, "You are not giving a simul.\n");
		return COM_OK;
	}

	player_decline_offers(p, -1, -PEND_SIMUL);

	for (int i = 0; i < parray[p].simul_info.numBoards; i++) {
		if (parray[p].simul_info.boards[i] >= 0) {
			game_ended(parray[p].simul_info.boards[i], WHITE,
			    END_ADJOURN);
		}
	}

	return COM_OK;
}

PUBLIC int
com_moretime(int p, param_list param)
{
	int	g, increment;

	ASSERT(param[0].type == TYPE_INT);

	if (parray[p].game >= 0 && garray[parray[p].game].status ==
	    GAME_EXAMINE) {
		pprintf(p, "You cannot use moretime in an examined game.\n");
		return COM_OK;
	}

	if ((increment = param[0].val.integer) <= 0) {
		pprintf(p, "Moretime requires an integer value greater than "
		    "zero.\n");
		return COM_OK;
	}

	if (!pIsPlaying(p))
		return COM_OK;

	if (increment > 600) {
		pprintf(p, "Moretime has a maximum limit of 600 seconds.\n");
		increment = 600;
	}

	g = parray[p].game;

	if (garray[g].white == p) {
		garray[g].bTime += increment * 10;
#ifdef TIMESEAL
		garray[g].bRealTime += increment * 10 * 100;
#endif
		pprintf(p, "%d seconds were added to your opponents clock\n",
		    increment);
		pprintf_prompt(parray[p].opponent, "\nYour opponent has "
		    "added %d seconds to your clock.\n", increment);
	}

	if (garray[g].black == p) {
		garray[g].wTime += increment * 10;;
#ifdef TIMESEAL
		garray[g].wRealTime += increment * 10 * 100;
#endif
		pprintf(p, "%d seconds were added to your opponents clock\n",
		    increment);
		pprintf_prompt(parray[p].opponent, "\nYour opponent has "
		    "added %d seconds to your clock.\n", increment);
	}

	return COM_OK;
}