summaryrefslogtreecommitdiffstats
path: root/apps/plugins/doom/g_game.c
blob: 99f8e63187557f70e5d52ed54731cc18f579349e (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
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
/* Emacs style mode select   -*- C++ -*-
 *-----------------------------------------------------------------------------
 *
 *
 *  PrBoom a Doom port merged with LxDoom and LSDLDoom
 *  based on BOOM, a modified and improved DOOM engine
 *  Copyright (C) 1999 by
 *  id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman
 *  Copyright (C) 1999-2000 by
 *  Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze
 *
 *  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.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 *  02111-1307, USA.
 *
 * DESCRIPTION:  none
 *  The original Doom description was none, basically because this file
 *  has everything. This ties up the game logic, linking the menu and
 *  input code to the underlying game by creating & respawning players,
 *  building game tics, calling the underlying thing logic.
 *
 *-----------------------------------------------------------------------------
 */

#include "doomdef.h"
#include "doomstat.h"
#include "z_zone.h"
#include "f_finale.h"
#include "m_argv.h"
#include "m_misc.h"
#include "m_menu.h"
#include "m_random.h"
#include "i_system.h"
#include "p_map.h"
#include "p_setup.h"
#include "p_saveg.h"
#include "p_tick.h"

#include "d_main.h"

#include "wi_stuff.h"
#include "hu_stuff.h"
#include "st_stuff.h"
#include "am_map.h"

// Needs access to LFB.
#include "v_video.h"

#include "w_wad.h"
#include "r_main.h"
#include "s_sound.h"

// Data.
#include "dstrings.h"
#include "sounds.h"

// SKY handling - still the wrong place.
#include "r_data.h"
#include "r_sky.h"
#include "p_inter.h"
#include "g_game.h"

#include "rockmacros.h"

#define SAVEGAMESIZE  0x20000
#define SAVESTRINGSIZE  24

static size_t   savegamesize = SAVEGAMESIZE; // killough
static boolean  netdemo;
static const byte *demobuffer;   /* cph - only used for playback */
static int    demofd; /* cph - record straight to file */
static const byte *demo_p;
static short    consistancy[MAXPLAYERS][BACKUPTICS];

gameaction_t   gameaction;
gamestate_t    gamestate;
skill_t        gameskill;
boolean        respawnmonsters;
int            gameepisode;
int            gamemap;
boolean         paused;
// CPhipps - moved *_loadgame vars here
static boolean forced_loadgame = false;
static boolean command_loadgame = false;

boolean         usergame;      // ok to save / end game
boolean         timingdemo;    // if true, exit with report on completion
boolean         fastdemo;      // if true, run at full speed -- killough
boolean         nodrawers;     // for comparative timing purposes
boolean         noblit;        // for comparative timing purposes
int             starttime;     // for comparative timing purposes
boolean         deathmatch;    // only if started as net death
boolean         netgame;       // only true if packets are broadcast
boolean         playeringame[MAXPLAYERS];
player_t        players[MAXPLAYERS];
int             consoleplayer; // player taking events and displaying
int             displayplayer; // view being displayed
int             gametic;
int             levelstarttic; // gametic at level start
extern int      basetic;       /* killough 9/29/98: for demo sync */
int             totalkills, totallive, totalitems, totalsecret;    // for intermission
boolean         demorecording;
boolean         demoplayback;
boolean         singledemo;           // quit after playing a demo from cmdline
wbstartstruct_t wminfo;               // parms for world map / intermission
boolean         haswolflevels = false;// jff 4/18/98 wolf levels present
static byte     *savebuffer;          // CPhipps - static
int             autorun = false;      // always running?          // phares
int             totalleveltimes;      // CPhipps - total time for all completed levels
int		longtics;

//
// controls (have defaults)
//
int     key_right;
int     key_left;
int     key_up;
int     key_down;
int     key_menu_right;                                      // phares 3/7/98
int     key_menu_left;                                       //     |
int     key_menu_up;                                         //     V
int     key_menu_down;
int     key_menu_backspace;                                  //     ^
int     key_menu_escape;                                     //     |
int     key_menu_enter;                                      // phares 3/7/98
int     key_strafeleft;
int     key_straferight;
int     key_fire;
int     key_use;
int     key_strafe;
int     key_speed;
int     key_escape = KEY_ESCAPE;                           // phares 4/13/98
int     key_weapon;

int     key_savegame;                                               // phares
int     key_loadgame;                                               //    |
int     key_autorun;                                                //    V
int     key_reverse;
int     key_zoomin;
int     key_zoomout;

int     key_reverse;

int     key_chat;
int     key_backspace;
int     key_enter;
int     key_map_right;
int     key_map_left;
int     key_map_up;
int     key_map_down;
int     key_map_zoomin;
int     key_map_zoomout;
int     key_map;
int     key_map_gobig;
int     key_map_follow;
int     key_map_mark;
int     key_map_clear;
int     key_map_grid;
int     key_map_overlay; // cph - map overlay
int     key_map_rotate;  // cph - map rotation
int     key_help = KEY_F1;                                 // phares 4/13/98
int     key_soundvolume;
int     key_hud;
int     key_quicksave;
int     key_endgame;
int     key_messages;
int     key_quickload;
int     key_quit;
int     key_gamma;
int     key_spy;
int     key_pause;
int     key_setup;
int     destination_keys[MAXPLAYERS];
int     key_weapontoggle;
int     key_weapon1;
int     key_weapon2;
int     key_weapon3;
int     key_weapon4;
int     key_weapon5;
int     key_weapon6;
int     key_weapon7;                                                //    ^
int     key_weapon8;                                                //    |
int     key_weapon9;                                                // phares

int     key_screenshot;             // killough 2/22/98: screenshot key
int     mousebfire;
int     mousebstrafe;
int     mousebforward;
int     joybfire;
int     joybstrafe;
int     joybuse;
int     joybspeed;

#define MAXPLMOVE  (forwardmove[1])
#define TURBOTHRESHOLD 0x32
#define SLOWTURNTICS 6
#define QUICKREVERSE (short)32768 // 180 degree reverse                    // phares
#define NUMKEYS  512

fixed_t  forwardmove[2] = {0x19, 0x32};
fixed_t  sidemove[2] = {0x18, 0x28};
fixed_t  angleturn[3] = {640, 1280, 320}; // + slow turn

// CPhipps - made lots of key/button state vars static
static boolean gamekeydown[NUMKEYS];
static int     turnheld;       // for accelerative turning

static boolean mousearray[4];
static boolean *mousebuttons = &mousearray[1];    // allow [-1]

// mouse values are used once
static int   mousex;
static int   mousey;
static unsigned int   dclicktime;
static unsigned int   dclickstate;
static unsigned int   dclicks;
static unsigned int   dclicktime2;
static unsigned int   dclickstate2;
static unsigned int   dclicks2;

// scrollwheel values
static int scrollmag;

// joystick values are repeated
static int   joyxmove;
static int   joyymove;
static boolean joyarray[5];
static boolean *joybuttons = &joyarray[1];    // allow [-1]

// Game events info
static buttoncode_t special_event; // Event triggered by local player, to send
static byte  savegameslot;         // Slot to load if gameaction == ga_loadgame
char         savedescription[SAVEDESCLEN];  // Description to save in savegame if gameaction == ga_savegame

//jff 3/24/98 declare startskill external, define defaultskill here
extern skill_t startskill;      //note 0-based
int defaultskill;               //note 1-based

// killough 2/8/98: make corpse queue variable in size
int    bodyqueslot, bodyquesize;        // killough 2/8/98
mobj_t **bodyque = 0;                   // phares 8/10/98

void*  statcopy;    // for statistics driver

static void G_DoSaveGame (boolean menu);
static const byte* G_ReadDemoHeader(const byte* demo_p);

//
// G_BuildTiccmd
// Builds a ticcmd from all of the available inputs
// or reads it from the demo buffer.
// If recording a demo, write it out
//
static inline signed char fudgef(signed char b)
{
   static int c;
   if (!b || !demo_compatibility || longtics) return b;
   if (++c & 0x1f) return b;
   b |= 1; if (b>2) b-=2;
   return b;
}

static inline signed short fudgea(signed short b)
{
   if (!b || !demo_compatibility || !longtics) return b;
   b |= 1; if (b>2) b-=2;
   return b;
}

void G_BuildTiccmd(ticcmd_t* cmd)
{
   boolean strafe;
   boolean bstrafe;
   int speed;
   int tspeed;
   int forward;
   int side;
   int newweapon=0;                                          // phares
   /* cphipps - remove needless I_BaseTiccmd call, just set the ticcmd to zero */
   memset(cmd,0,sizeof*cmd);
   cmd->consistancy = consistancy[consoleplayer][maketic%BACKUPTICS];

   strafe = gamekeydown[key_strafe] || mousebuttons[mousebstrafe]
            || joybuttons[joybstrafe];
   speed = autorun || gamekeydown[key_speed] || joybuttons[joybspeed]; // phares

   forward = side = 0;

   // use two stage accelerative turning
   // on the keyboard and joystick
   if (joyxmove < 0 || joyxmove > 0 ||
         gamekeydown[key_right] || gamekeydown[key_left])
      turnheld += ticdup;
   else
      turnheld = 0;

   if (turnheld < SLOWTURNTICS)
      tspeed = 2;             // slow turn
   else
      tspeed = speed;

   // turn 180 degrees in one keystroke?                           // phares
   //    |
   if (gamekeydown[key_reverse])                                   //    V
   {
      cmd->angleturn += QUICKREVERSE;                             //    ^
      gamekeydown[key_reverse] = false;                           //    |
   }                                                             // phares

   // let movement keys cancel each other out

   /* strafe with scrollwheel */
   if (scrollmag > 0)
         side += 5*sidemove[speed];
   if (scrollmag < 0)
         side -= 5*sidemove[speed];
   scrollmag = 0;         

   if (strafe)
   {
      if (gamekeydown[key_right])
         side += sidemove[speed];
      if (gamekeydown[key_left])
         side -= sidemove[speed];
      if (joyxmove > 0)
         side += sidemove[speed];
      if (joyxmove < 0)
         side -= sidemove[speed];
   }
   else
   {
      if (gamekeydown[key_right])
         cmd->angleturn -= angleturn[tspeed];
      if (gamekeydown[key_left])
         cmd->angleturn += angleturn[tspeed];
      if (joyxmove > 0)
         cmd->angleturn -= angleturn[tspeed];
      if (joyxmove < 0)
         cmd->angleturn += angleturn[tspeed];
   }

   if (gamekeydown[key_up])
      forward += forwardmove[speed];
   if (gamekeydown[key_down])
      forward -= forwardmove[speed];
   if (joyymove < 0)
      forward += forwardmove[speed];
   if (joyymove > 0)
      forward -= forwardmove[speed];
   if (gamekeydown[key_straferight])
      side += sidemove[speed];
   if (gamekeydown[key_strafeleft])
      side -= sidemove[speed];

   // buttons
   cmd->chatchar = HU_dequeueChatChar();

   if (gamekeydown[key_fire] || mousebuttons[mousebfire] ||
         joybuttons[joybfire])
      cmd->buttons |= BT_ATTACK;

   if (gamekeydown[key_use] || joybuttons[joybuse])
   {
      cmd->buttons |= BT_USE;
      // clear double clicks if hit use button
      dclicks = 0;
   }

   // Toggle between the top 2 favorite weapons.                   // phares
   // If not currently aiming one of these, switch to              // phares
   // the favorite. Only switch if you possess the weapon.         // phares

   // killough 3/22/98:
   //
   // Perform automatic weapons switch here rather than in p_pspr.c,
   // except in demo_compatibility mode.
   //
   // killough 3/26/98, 4/2/98: fix autoswitch when no weapons are left

   if ((!demo_compatibility && players[consoleplayer].attackdown && // killough
         !P_CheckAmmo(&players[consoleplayer])) || gamekeydown[key_weapontoggle])
      newweapon = P_SwitchWeapon(&players[consoleplayer]);           // phares
   else
   {                                 // phares 02/26/98: Added gamemode checks
      if(gamekeydown[key_weapon])
      {
         volatile unsigned int wpcheck;   // I don't know why this is needed, but it is
         for(wpcheck=0; wpcheck<9; wpcheck++)
            if(players[consoleplayer].weaponowned[wpcheck] && wpcheck>players[consoleplayer].readyweapon )
            {
               newweapon=wpcheck;
               break;
            }
         if(players[consoleplayer].weaponowned[wp_chainsaw]&&newweapon==0)
            newweapon=1;
      }
      else
      {
         newweapon =
            gamekeydown[key_weapon1] ? wp_fist :    // killough 5/2/98: reformatted
            gamekeydown[key_weapon2] ? wp_pistol :
            gamekeydown[key_weapon3] ? wp_shotgun :
            gamekeydown[key_weapon4] ? wp_chaingun :
            gamekeydown[key_weapon5] ? wp_missile :
            gamekeydown[key_weapon6] && gamemode != shareware ? wp_plasma :
            gamekeydown[key_weapon7] && gamemode != shareware ? wp_bfg :
            gamekeydown[key_weapon8] ? wp_chainsaw :
            gamekeydown[key_weapon9] && gamemode == commercial ? wp_supershotgun :
            wp_nochange;
      }

      // killough 3/22/98: For network and demo consistency with the
      // new weapons preferences, we must do the weapons switches here
      // instead of in p_user.c. But for old demos we must do it in
      // p_user.c according to the old rules. Therefore demo_compatibility
      // determines where the weapons switch is made.

      // killough 2/8/98:
      // Allow user to switch to fist even if they have chainsaw.
      // Switch to fist or chainsaw based on preferences.
      // Switch to shotgun or SSG based on preferences.

      if (!demo_compatibility)
      {
         const player_t *player = &players[consoleplayer];

         // only select chainsaw from '1' if it's owned, it's
         // not already in use, and the player prefers it or
         // the fist is already in use, or the player does not
         // have the berserker strength.

         if (newweapon==wp_fist && player->weaponowned[wp_chainsaw] &&
                player->readyweapon!=wp_chainsaw &&
                (player->readyweapon==wp_fist ||
                !player->powers[pw_strength] ||
                P_WeaponPreferred(wp_chainsaw, wp_fist)))
            newweapon = wp_chainsaw;

         // Select SSG from '3' only if it's owned and the player
         // does not have a shotgun, or if the shotgun is already
         // in use, or if the SSG is not already in use and the
         // player prefers it.
         if(!gamekeydown[key_weapon])
            if (newweapon == wp_shotgun && gamemode == commercial &&
                  player->weaponowned[wp_supershotgun] &&
                  (!player->weaponowned[wp_shotgun] ||
                   player->readyweapon == wp_shotgun ||
                   (player->readyweapon != wp_supershotgun &&
                    P_WeaponPreferred(wp_supershotgun, wp_shotgun))))
               newweapon = wp_supershotgun;

      }
      // killough 2/8/98, 3/22/98 -- end of weapon selection changes
   }

   if(newweapon >wp_nochange) // something is messed up with the weapon switching code above allowing it to give values greater
   {                           // then wp_nochange which really screws the game up
      newweapon=0;
   }
   if (newweapon != wp_nochange)
   {
      cmd->buttons |= BT_CHANGE;
      cmd->buttons |= newweapon<<BT_WEAPONSHIFT;
   }

   // mouse
   if (mousebuttons[mousebforward])
      forward += forwardmove[speed];

   // forward double click
   if (mousebuttons[mousebforward] != dclickstate && dclicktime > 1 )
   {
      dclickstate = mousebuttons[mousebforward];
      if (dclickstate)
         dclicks++;
      if (dclicks == 2)
      {
         cmd->buttons |= BT_USE;
         dclicks = 0;
      }
      else
         dclicktime = 0;
   }
   else
      if ((dclicktime += ticdup) > 20)
      {
         dclicks = 0;
         dclickstate = 0;
      }

   // strafe double click

   bstrafe = mousebuttons[mousebstrafe] || joybuttons[joybstrafe];
   if (bstrafe != dclickstate2 && dclicktime2 > 1 )
   {
      dclickstate2 = bstrafe;
      if (dclickstate2)
         dclicks2++;
      if (dclicks2 == 2)
      {
         cmd->buttons |= BT_USE;
         dclicks2 = 0;
      }
      else
         dclicktime2 = 0;
   }
   else
      if ((dclicktime2 += ticdup) > 20)
      {
         dclicks2 = 0;
         dclickstate2 = 0;
      }
   forward += mousey;
   if (strafe)
      side += mousex / 4;       /* mead  Don't want to strafe as fast as turns.*/
   else
      cmd->angleturn -= mousex; /* mead now have enough dynamic range 2-10-00 */

   mousex = mousey = 0;

   if (forward > MAXPLMOVE)
      forward = MAXPLMOVE;
   else if (forward < -MAXPLMOVE)
      forward = -MAXPLMOVE;
   if (side > MAXPLMOVE)
      side = MAXPLMOVE;
   else if (side < -MAXPLMOVE)
      side = -MAXPLMOVE;

   cmd->forwardmove += fudgef(forward);
   cmd->sidemove += side;
   cmd->angleturn = fudgea(cmd->angleturn);

   // CPhipps - special events (game new/load/save/pause)
   if (special_event & BT_SPECIAL) {
      cmd->buttons = special_event;
      special_event = 0;
   }
}

//
// G_RestartLevel
//

void G_RestartLevel(void)
{
   special_event = BT_SPECIAL | (BTS_RESTARTLEVEL & BT_SPECIALMASK);
}

#include "z_bmalloc.h"
//
// G_DoLoadLevel
//
extern  gamestate_t     wipegamestate;

static void G_DoLoadLevel (void)
{
   int i;

   // Set the sky map.
   // First thing, we have a dummy sky texture name,
   //  a flat. The data is in the WAD only because
   //  we look for an actual index, instead of simply
   //  setting one.

   skyflatnum = R_FlatNumForName ( SKYFLATNAME );

   // DOOM determines the sky texture to be used
   // depending on the current episode, and the game version.
   if (gamemode == commercial)
      // || gamemode == pack_tnt   //jff 3/27/98 sorry guys pack_tnt,pack_plut
      // || gamemode == pack_plut) //aren't gamemodes, this was matching retail
   {
      skytexture = R_TextureNumForName ("SKY3");
      if (gamemap < 12)
         skytexture = R_TextureNumForName ("SKY1");
      else
         if (gamemap < 21)
            skytexture = R_TextureNumForName ("SKY2");
   }
   else //jff 3/27/98 and lets not forget about DOOM and Ultimate DOOM huh?
      switch (gameepisode)
      {
      case 1:
         skytexture = R_TextureNumForName ("SKY1");
         break;
      case 2:
         skytexture = R_TextureNumForName ("SKY2");
         break;
      case 3:
         skytexture = R_TextureNumForName ("SKY3");
         break;
      case 4: // Special Edition sky
         skytexture = R_TextureNumForName ("SKY4");
         break;
      }//jff 3/27/98 end sky setting fix

   levelstarttic = gametic;        // for time calculation

   if (!demo_compatibility && !mbf_features)   // killough 9/29/98
      basetic = gametic;

   if (wipegamestate == GS_LEVEL)
      wipegamestate = -1;             // force a wipe

   gamestate = GS_LEVEL;

   for (i=0 ; i<MAXPLAYERS ; i++)
   {
      if (playeringame[i] && players[i].playerstate == PST_DEAD)
         players[i].playerstate = PST_REBORN;
      memset (players[i].frags,0,sizeof(players[i].frags));
   }

   // initialize the msecnode_t freelist.                     phares 3/25/98
   // any nodes in the freelist are gone by now, cleared
   // by Z_FreeTags() when the previous level ended or player
   // died.

   {
      DECLARE_BLOCK_MEMORY_ALLOC_ZONE(secnodezone);
      NULL_BLOCK_MEMORY_ALLOC_ZONE(secnodezone);
      //extern msecnode_t *headsecnode; // phares 3/25/98
      //headsecnode = NULL;
   }

   P_SetupLevel (gameepisode, gamemap, 0, gameskill);
   displayplayer = consoleplayer;    // view the guy you are playing
   gameaction = ga_nothing;
   Z_CheckHeap ();

   // clear cmd building stuff
   memset (gamekeydown, 0, sizeof(gamekeydown));
   joyxmove = joyymove = 0;
   mousex = mousey = 0;
   special_event = 0; paused = false;
   memset (mousebuttons, 0, sizeof(mousebuttons));
   memset (joybuttons, 0, sizeof(joybuttons));

   // killough 5/13/98: in case netdemo has consoleplayer other than green
   ST_Start();
   HU_Start();

   // killough: make -timedemo work on multilevel demos
   // Move to end of function to minimize noise -- killough 2/22/98:

   if (timingdemo)
   {
      static int first=1;
      if (first)
      {
         starttime = I_GetTime ();
         first=0;
      }
   }
}

//
// G_Responder
// Get info needed to make ticcmd_ts for the players.
//
boolean G_Responder (event_t* ev)
{
   // allow spy mode changes even during the demo
   // killough 2/22/98: even during DM demo
   //
   // killough 11/98: don't autorepeat spy mode switch

#if 0
   if (ev->data1 == key_spy && netgame && (demoplayback || !deathmatch) &&
         gamestate == GS_LEVEL)
   {
      if (ev->type == ev_keyup)
         gamekeydown[key_spy] = false;
      if (ev->type == ev_keydown && !gamekeydown[key_spy])
      {
         gamekeydown[key_spy] = true;
         do                                          // spy mode
            if (++displayplayer >= MAXPLAYERS)
               displayplayer = 0;
         while (!playeringame[displayplayer] && displayplayer!=consoleplayer);

         ST_Start();    // killough 3/7/98: switch status bar views too
         HU_Start();
         S_UpdateSounds(players[displayplayer].mo);
      }
      return true;
   }
#endif

   // any other key pops up menu if in demos
   //
   // killough 8/2/98: enable automap in -timedemo demos
   //
   // killough 9/29/98: make any key pop up menu regardless of
   // which kind of demo, and allow other events during playback

   if (gameaction == ga_nothing && (demoplayback || gamestate == GS_DEMOSCREEN))
   {
      // killough 9/29/98: allow user to pause demos during playback
      if (ev->type == ev_keydown && ev->data1 == key_pause)
      {
         if (paused ^= 2)
            S_PauseSound();
         else
            S_ResumeSound();
         return true;
      }
      // killough 10/98:
      // Don't pop up menu, if paused in middle
      // of demo playback, or if automap active.
      // Don't suck up keys, which may be cheats

      return gamestate == GS_DEMOSCREEN &&
             !(paused & 2) && !(automapmode & am_active) &&
             ((ev->type == ev_keydown) ||
              (ev->type == ev_mouse && ev->data1) ||
              (ev->type == ev_joystick && ev->data1)) ?
             M_StartControlPanel(), true : false;
   }

   if (gamestate == GS_FINALE && F_Responder(ev))
      return true;  // finale ate the event

   switch (ev->type)
   {
   case ev_keydown:
      if (ev->data1 == key_pause)           // phares
      {
         special_event = BT_SPECIAL | (BTS_PAUSE & BT_SPECIALMASK);
         return true;
      }
      if (ev->data1 <NUMKEYS)
         gamekeydown[ev->data1] = true;
      return true;    // eat key down events

   case ev_keyup:
      if (ev->data1 <NUMKEYS)
         gamekeydown[ev->data1] = false;
      return false;   // always let key up events filter down

   case ev_mouse:
      mousebuttons[0] = ev->data1 & 1;
      mousebuttons[1] = ev->data1 & 2;
      mousebuttons[2] = ev->data1 & 4;
      mousex = ev->data2*(mouseSensitivity+5)/10;
      mousey = ev->data3*(mouseSensitivity+5)/10;
      return true;    // eat events

   case ev_joystick:
      joybuttons[0] = ev->data1 & 1;
      joybuttons[1] = ev->data1 & 2;
      joybuttons[2] = ev->data1 & 4;
      joybuttons[3] = ev->data1 & 8;
      joyxmove = ev->data2;
      joyymove = ev->data3;
      return true;    // eat events

   case ev_scroll:
      scrollmag = ev->data1;

   default:
      break;
   }
   return false;
}

//
// G_Ticker
// Make ticcmd_ts for the players.
//
extern int mapcolor_me;

void G_Ticker (void)
{
   int  i;
   static gamestate_t prevgamestate;

   P_MapStart();
   // do player reborns if needed
   for (i=0 ; i<MAXPLAYERS ; i++)
      if (playeringame[i] && players[i].playerstate == PST_REBORN)
         G_DoReborn (i);
   P_MapEnd();

   // do things to change the game state
   while (gameaction != ga_nothing)
   {
      switch (gameaction)
      {
      case ga_loadlevel:
         // force players to be initialized on level reload
         for (i=0 ; i<MAXPLAYERS ; i++)
            players[i].playerstate = PST_REBORN;
         G_DoLoadLevel ();
         break;
      case ga_newgame:
         G_DoNewGame ();
         break;
      case ga_loadgame:
         G_DoLoadGame ();
         break;
      case ga_savegame:
         G_DoSaveGame (false);
         break;
      case ga_playdemo:
         G_DoPlayDemo ();
         break;
      case ga_completed:
         G_DoCompleted ();
         break;
      case ga_victory:
         F_StartFinale ();
         break;
      case ga_worlddone:
         G_DoWorldDone ();
         break;
      case ga_nothing:
         break;
      }
   }

   if (paused & 2 || (!demoplayback && menuactive && !netgame))
      basetic++;  // For revenant tracers and RNG -- we must maintain sync
   else
   {
      // get commands, check consistancy, and build new consistancy check
      int buf = (gametic/ticdup)%BACKUPTICS;

      for (i=0 ; i<MAXPLAYERS ; i++)
      {
         if (playeringame[i])
         {
            ticcmd_t *cmd = &players[i].cmd;

            memcpy(cmd, &netcmds[i][buf], sizeof *cmd);

            if (demoplayback)
               G_ReadDemoTiccmd (cmd);
            if (demorecording)
               G_WriteDemoTiccmd (cmd);

            // check for turbo cheats
            // killough 2/14/98, 2/20/98 -- only warn in netgames and demos

            if ((netgame || demoplayback) && cmd->forwardmove > TURBOTHRESHOLD &&
                  !(gametic&31) && ((gametic>>5)&3) == i )
            {
               extern const char *player_names[];
               /* cph - don't use sprintf, use doom_printf */
               doom_printf ("%s is turbo!", player_names[i]);
            }

            if (netgame && !netdemo && !(gametic%ticdup) )
            {
               if (gametic > BACKUPTICS
                     && consistancy[i][buf] != cmd->consistancy)
                  I_Error("G_Ticker: Consistency failure (%i should be %i)",
                          cmd->consistancy, consistancy[i][buf]);
               if (players[i].mo)
                  consistancy[i][buf] = players[i].mo->x;
               else
                  consistancy[i][buf] = 0; // killough 2/14/98
            }
         }
      }

      // check for special buttons
      for (i=0 ; i<MAXPLAYERS ; i++)
      {
         if (playeringame[i])
         {
            if (players[i].cmd.buttons & BT_SPECIAL)
            {
               switch (players[i].cmd.buttons & BT_SPECIALMASK)
               {
               case BTS_PAUSE:
                  paused ^= 1;
                  if (paused)
                     S_PauseSound ();
                  else
                     S_ResumeSound ();
                  break;
   
               case BTS_SAVEGAME:
                  if (!savedescription[0])
                     strcpy(savedescription, "NET GAME");
                  savegameslot =
                     (players[i].cmd.buttons & BTS_SAVEMASK)>>BTS_SAVESHIFT;
                  gameaction = ga_savegame;
                  break;

                  // CPhipps - remote loadgame request
               case BTS_LOADGAME:
                  savegameslot =
                     (players[i].cmd.buttons & BTS_SAVEMASK)>>BTS_SAVESHIFT;
                  gameaction = ga_loadgame;
                  forced_loadgame = netgame; // Force if a netgame
                  command_loadgame = false;
                  break;

                  // CPhipps - Restart the level
               case BTS_RESTARTLEVEL:
                  if (demoplayback || (compatibility_level < lxdoom_1_compatibility))
                     break;     // CPhipps - Ignore in demos or old games
                  gameaction = ga_loadlevel;
                  break;
               }
               players[i].cmd.buttons = 0;
            }
         }  
      }
   }

   // cph - if the gamestate changed, we may need to clean up the old gamestate
   if (gamestate != prevgamestate) {
      switch (prevgamestate) {
      case GS_INTERMISSION:
         WI_End();
      default:
         break;
      }
      prevgamestate = gamestate;
   }

   // do main actions
   switch (gamestate)
   {
   case GS_LEVEL:
      P_Ticker ();
      ST_Ticker ();
      AM_Ticker ();
      HU_Ticker ();
      break;

   case GS_INTERMISSION:
      WI_Ticker ();
      break;

   case GS_FINALE:
      F_Ticker ();
      break;

   case GS_DEMOSCREEN:
      D_PageTicker ();
      break;
   }
}


//
// PLAYER STRUCTURE FUNCTIONS
// also see P_SpawnPlayer in P_Things
//

//
// G_PlayerFinishLevel
// Can when a player completes a level.
//
void G_PlayerFinishLevel(int player)
{
   player_t *p = &players[player];
   memset(p->powers, 0, sizeof (p->powers));
   memset(p->cards, 0, sizeof (p->cards));
   p->mo->flags &= ~MF_SHADOW;  // cancel invisibility
   p->extralight = 0;   // cancel gun flashes
   p->fixedcolormap = 0;  // cancel ir gogles
   p->damagecount = 0;   // no palette changes
   p->bonuscount = 0;
}

// CPhipps - G_SetPlayerColour
// Player colours stuff
//
// G_SetPlayerColour

#include "r_draw.h"
extern byte playernumtotrans[MAXPLAYERS];

void G_ChangedPlayerColour(int pn, int cl)
{
   int i;

   if (!netgame) return;

   mapcolor_plyr[pn] = cl;

   // Rebuild colour translation tables accordingly
   R_InitTranslationTables();
   // Change translations on existing player mobj's
   for (i=0; i<MAXPLAYERS; i++) {
      if ((gamestate == GS_LEVEL) && playeringame[i] && (players[i].mo != NULL)) {
         players[i].mo->flags &= ~MF_TRANSLATION;
         players[i].mo->flags |= playernumtotrans[i] << MF_TRANSSHIFT;
      }
   }
}

//
// G_PlayerReborn
// Called after a player dies
// almost everything is cleared and initialized
//
void G_PlayerReborn (int player)
{
   player_t *p;
   int i;
   int frags[MAXPLAYERS];
   int killcount;
   int itemcount;
   int secretcount;

   memcpy (frags, players[player].frags, sizeof frags);
   killcount = players[player].killcount;
   itemcount = players[player].itemcount;
   secretcount = players[player].secretcount;

   p = &players[player];

   // killough 3/10/98,3/21/98: preserve cheats across idclev
   {
      int cheats = p->cheats;
      memset (p, 0, sizeof(*p));
      p->cheats = cheats;
   }

   memcpy(players[player].frags, frags, sizeof(players[player].frags));
   players[player].killcount = killcount;
   players[player].itemcount = itemcount;
   players[player].secretcount = secretcount;

   p->usedown = p->attackdown = true; // don't do anything immediately
   p->playerstate = PST_LIVE;
   p->health = MAXHEALTH;
   p->readyweapon = p->pendingweapon = wp_pistol;
   p->weaponowned[wp_fist] = true;
   p->weaponowned[wp_pistol] = true;
   p->ammo[am_clip] = 50;

   for (i=0 ; i<NUMAMMO ; i++)
      p->maxammo[i] = maxammo[i];

}

//
// G_CheckSpot
// Returns false if the player cannot be respawned
// at the given mapthing_t spot
// because something is occupying it
//

void P_SpawnPlayer(mapthing_t *mthing);

boolean G_CheckSpot(int playernum, mapthing_t *mthing)
{
   fixed_t     x,y;
   subsector_t *ss;
   int         i;

   if (!players[playernum].mo)
   {
      // first spawn of level, before corpses
      for (i=0 ; i<playernum ; i++)
         if (players[i].mo->x == mthing->x << FRACBITS
               && players[i].mo->y == mthing->y << FRACBITS)
            return false;
      return true;
   }

   x = mthing->x << FRACBITS;
   y = mthing->y << FRACBITS;

   // killough 4/2/98: fix bug where P_CheckPosition() uses a non-solid
   // corpse to detect collisions with other players in DM starts
   //
   // Old code:
   // if (!P_CheckPosition (players[playernum].mo, x, y))
   //    return false;

   players[playernum].mo->flags |=  MF_SOLID;
   i = P_CheckPosition(players[playernum].mo, x, y);
   players[playernum].mo->flags &= ~MF_SOLID;
   if (!i)
      return false;

   // flush an old corpse if needed
   // killough 2/8/98: make corpse queue have an adjustable limit
   // killough 8/1/98: Fix bugs causing strange crashes

   if (bodyquesize > 0)
   {
      static mobj_t **bodyque;
      static int queuesize;
      if (queuesize < bodyquesize)
      {
         bodyque = realloc(bodyque, bodyquesize*sizeof*bodyque);
         memset(bodyque+queuesize, 0,
                (bodyquesize-queuesize)*sizeof*bodyque);
         queuesize = bodyquesize;
      }
      if (bodyqueslot >= bodyquesize)
         P_RemoveMobj(bodyque[bodyqueslot % bodyquesize]);
      bodyque[bodyqueslot++ % bodyquesize] = players[playernum].mo;
   }
   else
      if (!bodyquesize)
         P_RemoveMobj(players[playernum].mo);

   // spawn a teleport fog
   ss = R_PointInSubsector (x,y);
   { // Teleport fog at respawn point
      fixed_t xa=0,ya=0;
      int an;
      mobj_t      *mo;

      /* BUG: an can end up negative, because mthing->angle is (signed) short.
       * We have to emulate original Doom's behaviour, deferencing past the start
       * of the array, into the previous array (finetangent) */
      an = ( ANG45 * ((signed)mthing->angle/45) ) >> ANGLETOFINESHIFT;
      switch (an) {
      case -4096: xa = finetangent[2048];   // finecosine[-4096]
         ya = finetangent[0];      // finesine[-4096]
         break;
      case -3072: xa = finetangent[3072];   // finecosine[-3072]
         ya = finetangent[1024];   // finesine[-3072]
         break;
      case -2048: xa = finesine[0];   // finecosine[-2048]
         ya = finetangent[2048];   // finesine[-2048]
         break;
      case -1024:	xa = finesine[1024];     // finecosine[-1024]
         ya = finetangent[3072];  // finesine[-1024]
         break;
      case 1024:
      case 2048:
      case 3072:
      case 4096:
      case 0:	xa = finecosine[an];
         ya = finesine[an];
         break;
      default: I_Error("G_CheckSpot: unexpected angle %d\n",an);
      }


      mo = P_SpawnMobj(x+20*xa, y+20*ya, ss->sector->floorheight, MT_TFOG);

      if (players[consoleplayer].viewz != 1)
         S_StartSound(mo, sfx_telept);  // don't start sound on first frame
   }

   return true;
}


//
// G_DeathMatchSpawnPlayer
// Spawns a player at one of the random death match spots
// called at level load and each death
//
void G_DeathMatchSpawnPlayer (int playernum)
{
   int j, selections = deathmatch_p - deathmatchstarts;

   if (selections < MAXPLAYERS)
      I_Error("G_DeathMatchSpawnPlayer: Only %i deathmatch spots, %d required",
              selections, MAXPLAYERS);

   for (j=0 ; j<20 ; j++)
   {
      int i = P_Random(pr_dmspawn) % selections;
      if (G_CheckSpot (playernum, &deathmatchstarts[i]) )
      {
         deathmatchstarts[i].type = playernum+1;
         P_SpawnPlayer (&deathmatchstarts[i]);
         return;
      }
   }

   // no good spot, so the player will probably get stuck
   P_SpawnPlayer (&playerstarts[playernum]);
}

//
// G_DoReborn
//

void G_DoReborn (int playernum)
{
   if (!netgame)
      gameaction = ga_loadlevel; // reload the level from scratch
   (void)playernum;
#if 0
   else
   {      // respawn at the start
      int i;

      // first dissasociate the corpse
      players[playernum].mo->player = NULL;

      // spawn at random spot if in death match
      if (deathmatch)
      {
         G_DeathMatchSpawnPlayer (playernum);
         return;
      }

      if (G_CheckSpot (playernum, &playerstarts[playernum]) )
      {
         P_SpawnPlayer (&playerstarts[playernum]);
         return;
      }

      // try to spawn at one of the other players spots
      for (i=0 ; i<MAXPLAYERS ; i++)
      {
         if (G_CheckSpot (playernum, &playerstarts[i]) )
         {
            playerstarts[i].type = playernum+1; // fake as other player
            P_SpawnPlayer (&playerstarts[i]);
            playerstarts[i].type = i+1;  // restore
            return;
         }
         // he's going to be inside something.  Too bad.
      }
      P_SpawnPlayer (&playerstarts[playernum]);
   }
#endif
}

// DOOM Par Times
int pars[4][10] =
   {
      {0},
      {0,30,75,120,90,165,180,180,30,165},
      {0,90,90,90,120,90,360,240,30,170},
      {0,90,45,90,150,90,90,165,30,135}
   };

// DOOM II Par Times
int cpars[32] =
   {
      30,90,120,120,90,150,120,120,270,90, //  1-10
      210,150,150,150,210,150,420,150,210,150, // 11-20
      240,150,180,150,150,300,330,420,300,180, // 21-30
      120,30     // 31-32
   };

static boolean  secretexit;

void G_ExitLevel (void)
{
   secretexit = false;
   gameaction = ga_completed;
}

// Here's for the german edition.
// IF NO WOLF3D LEVELS, NO SECRET EXIT!

void G_SecretExitLevel (void)
{
   if (gamemode!=commercial || haswolflevels)
      secretexit = true;
   else
      secretexit = false;
   gameaction = ga_completed;
}

//
// G_DoCompleted
//

void G_DoCompleted (void)
{
   int i;

   gameaction = ga_nothing;

   for (i=0; i<MAXPLAYERS; i++)
      if (playeringame[i])
         G_PlayerFinishLevel(i);        // take away cards and stuff

   if (automapmode & am_active)
      AM_Stop();

   if (gamemode != commercial) // kilough 2/7/98
      switch(gamemap)
      {
         // cph - Remove ExM8 special case, so it gets summary screen displayed
      case 9:
         for (i=0 ; i<MAXPLAYERS ; i++)
            players[i].didsecret = true;
         break;
      }

   wminfo.didsecret = players[consoleplayer].didsecret;
   wminfo.epsd = gameepisode -1;
   wminfo.last = gamemap -1;

   // wminfo.next is 0 biased, unlike gamemap
   if (gamemode == commercial)
   {
      if (secretexit)
         switch(gamemap)
         {
         case 15:
            wminfo.next = 30; break;
         case 31:
            wminfo.next = 31; break;
         }
      else
         switch(gamemap)
         {
         case 31:
         case 32:
            wminfo.next = 15; break;
         default:
            wminfo.next = gamemap;
         }
   }
   else
   {
      if (secretexit)
         wminfo.next = 8;  // go to secret level
      else
         if (gamemap == 9)
         {
            // returning from secret level
            switch (gameepisode)
            {
            case 1:
               wminfo.next = 3;
               break;
            case 2:
               wminfo.next = 5;
               break;
            case 3:
               wminfo.next = 6;
               break;
            case 4:
               wminfo.next = 2;
               break;
            }
         }
         else
            wminfo.next = gamemap;          // go to next level
   }

   wminfo.maxkills = totalkills;
   wminfo.maxitems = totalitems;
   wminfo.maxsecret = totalsecret;
   wminfo.maxfrags = 0;

   if ( gamemode == commercial )
      wminfo.partime = TICRATE*cpars[gamemap-1];
   else
      wminfo.partime = TICRATE*pars[gameepisode][gamemap];

   wminfo.pnum = consoleplayer;

   for (i=0 ; i<MAXPLAYERS ; i++)
   {
      wminfo.plyr[i].in = playeringame[i];
      wminfo.plyr[i].skills = players[i].killcount;
      wminfo.plyr[i].sitems = players[i].itemcount;
      wminfo.plyr[i].ssecret = players[i].secretcount;
      wminfo.plyr[i].stime = leveltime;
      memcpy (wminfo.plyr[i].frags, players[i].frags,
              sizeof(wminfo.plyr[i].frags));
   }

   /* cph - modified so that only whole seconds are added to the totalleveltimes
    *  value; so our total is compatible with the "naive" total of just adding
    *  the times in seconds shown for each level. Also means our total time
    *  will agree with Compet-n.
    */
   wminfo.totaltimes = (totalleveltimes += (leveltime - leveltime%35));

   gamestate = GS_INTERMISSION;
   automapmode &= ~am_active;

   if (statcopy)
      memcpy (statcopy, &wminfo, sizeof(wminfo));

   WI_Start (&wminfo);
}


//
// G_WorldDone
//

void G_WorldDone (void)
{
   gameaction = ga_worlddone;

   if (secretexit)
      players[consoleplayer].didsecret = true;

   if (gamemode == commercial)
   {
      switch (gamemap)
      {
      case 15:
      case 31:
         if (!secretexit)
            break;
      case 6:
      case 11:
      case 20:
      case 30:
         F_StartFinale ();
         break;
      }
   }
   else if (gamemap == 8)
      gameaction = ga_victory; // cph - after ExM8 summary screen, show victory stuff
}

void G_DoWorldDone (void)
{
   idmusnum = -1;             //jff 3/17/98 allow new level's music to be loaded
   gamestate = GS_LEVEL;
   gamemap = wminfo.next+1;
   G_DoLoadLevel();
   gameaction = ga_nothing;
   AM_clearMarks();           //jff 4/12/98 clear any marks on the automap
}

// killough 2/28/98: A ridiculously large number
// of players, the most you'll ever need in a demo
// or savegame. This is used to prevent problems, in
// case more players in a game are supported later.

#define MIN_MAXPLAYERS 32

extern boolean setsizeneeded;
void R_ExecuteSetViewSize(void);

//CPhipps - savename variable redundant

/* killough 12/98:
 * This function returns a signature for the current wad.
 * It is used to distinguish between wads, for the purposes
 * of savegame compatibility warnings, and options lookups.
 */

static uint_64_t G_UpdateSignature(uint_64_t s, const char *name)
{
   int i, lump = W_CheckNumForName(name);
   if (lump != -1 && (i = lump+10) < numlumps)
      do
      {
         int size = W_LumpLength(i);
         const byte *p = W_CacheLumpNum(i);
         while (size--)
            s <<= 1, s += *p++;
         W_UnlockLumpNum(i);
      }
      while (--i > lump);
   return s;
}

static uint_64_t G_Signature(void)
{
   static uint_64_t s = 0;
   static boolean computed = false;
   char name[9];
   int episode, map;

   if (!computed) {
      computed = true;
      if (gamemode == commercial)
         for (map = haswolflevels ? 32 : 30; map; map--)
            snprintf(name, sizeof(name), "map%02d", map), s = G_UpdateSignature(s, name);
      else
         for (episode = gamemode==retail ? 4 :
                        gamemode==shareware ? 1 : 3; episode; episode--)
            for (map = 9; map; map--)
               snprintf(name, sizeof(name), "E%dM%d", episode, map), s = G_UpdateSignature(s, name);
   }
   return s;
}

//
// killough 5/15/98: add forced loadgames, which allow user to override checks
//

void G_ForcedLoadGame(void)
{
   // CPhipps - net loadgames are always forced, so we only reach here
   //  in single player
   gameaction = ga_loadgame;
   forced_loadgame = true;
}

// killough 3/16/98: add slot info
// killough 5/15/98: add command-line
void G_LoadGame(int slot, boolean command)
{
   if (!demoplayback && !command) {
      // CPhipps - handle savegame filename in G_DoLoadGame
      //         - Delay load so it can be communicated in net game
      //         - store info in special_event
      special_event = BT_SPECIAL | (BTS_LOADGAME & BT_SPECIALMASK) |
                      ((slot << BTS_SAVESHIFT) & BTS_SAVEMASK);
      forced_loadgame = netgame; // CPhipps - always force load netgames
   } else {
      // Do the old thing, immediate load
      gameaction = ga_loadgame;
      forced_loadgame = false;
      savegameslot = slot;
      demoplayback = false;
   }
   command_loadgame = command;
}

// killough 5/15/98:
// Consistency Error when attempting to load savegame.

static void G_LoadGameErr(const char *msg)
{
   (void) msg;  // Need to fix, but right now we're ignoring a forced load
   Z_Free(savebuffer);                // Free the savegame buffer
//   M_ForcedLoadGame(msg);             // Print message asking for 'Y' to force
   if (command_loadgame)              // If this was a command-line -loadgame
   {
      D_StartTitle();                // Start the title screen
      gamestate = GS_DEMOSCREEN;     // And set the game state accordingly
   }
}

// CPhipps - size of version header
#define VERSIONSIZE   16

const char * comp_lev_str[MAX_COMPATIBILITY_LEVEL] =
   { "doom v1.2", "demo", "doom", "\"boom compatibility\"", "boom v2.01", "boom v2.02", "lxdoom v1.3.2+",
     "MBF", "PrBoom 2.03beta", "PrBoom v2.1.0-2.1.1",
     "Current PrBoom"  };

static const struct {
   unsigned int comp_level;
   const char* ver_printf;
   int version;
} version_headers[] = {
                         { prboom_1_compatibility, "PrBoom %d", 260},
                         /* cph - we don't need a new version_header for prboom_3_comp/v2.1.1, since
                          *  the file format is unchanged. */
                         { prboom_3_compatibility, "PrBoom %d", 210}
                      };

static const size_t num_version_headers = sizeof(version_headers) / sizeof(version_headers[0]);

void G_DoLoadGame(void)
{
   int  i;
   // CPhipps - do savegame filename stuff here
   char name[100+1];     // killough 3/22/98
   int savegame_compatibility = -1;

   G_SaveGameName(name,sizeof(name),savegameslot, demoplayback);

   gameaction = ga_nothing;

   M_ReadFile(name, &savebuffer);
   save_p = savebuffer + SAVESTRINGSIZE;

   // CPhipps - read the description field, compare with supported ones
   for (i=0; (size_t)i<num_version_headers; i++) {
      char vcheck[VERSIONSIZE];
      // killough 2/22/98: "proprietary" version string :-)
      snprintf (vcheck,sizeof(vcheck), version_headers[i].ver_printf, version_headers[i].version);

      if (!strncmp(save_p, vcheck, VERSIONSIZE)) {
         savegame_compatibility = version_headers[i].comp_level;
         i = num_version_headers;
      }
   }
   if (savegame_compatibility == -1) {
      if (forced_loadgame) {
         savegame_compatibility = MAX_COMPATIBILITY_LEVEL-1;
      } else {
         G_LoadGameErr("Unrecognised savegame version!\nAre you sure? (y/n) ");
         return;
      }
   }

   save_p += VERSIONSIZE;

   // CPhipps - always check savegames even when forced,
   //  only print a warning if forced
   {  // killough 3/16/98: check lump name checksum (independent of order)
      uint_64_t checksum = 0;

      checksum = G_Signature();

      if (memcmp(&checksum, save_p, sizeof checksum)) {
         if (!forced_loadgame) {
            char *msg = malloc(strlen(save_p + sizeof checksum) + 128);
            strcpy(msg,"Incompatible Savegame!!!\n");
            if (save_p[sizeof checksum])
               strcat(strcat(msg,"Wads expected:\n\n"), save_p + sizeof checksum);
            strcat(msg, "\nAre you sure?");
            G_LoadGameErr(msg);
            free(msg);
            return;
         } else
            printf("G_DoLoadGame: Incompatible savegame\n");
      }
      save_p += sizeof checksum;
   }

   save_p += strlen(save_p)+1;

   /* cph - FIXME - compatibility flag? */
   compatibility_level = savegame_compatibility;
   save_p++;

   gameskill = *save_p++;
   gameepisode = *save_p++;
   gamemap = *save_p++;

   for (i=0 ; i<MAXPLAYERS ; i++)
      playeringame[i] = *save_p++;
   save_p += MIN_MAXPLAYERS-MAXPLAYERS;         // killough 2/28/98

   idmusnum = *save_p++;           // jff 3/17/98 restore idmus music
   if (idmusnum==255) idmusnum=-1; // jff 3/18/98 account for unsigned byte

   /* killough 3/1/98: Read game options
    * killough 11/98: move down to here
    */
   save_p = (char*)G_ReadOptions(save_p);

   // load a base level
   G_InitNew (gameskill, gameepisode, gamemap);

   /* get the times - killough 11/98: save entire word */
   memcpy(&leveltime, save_p, sizeof leveltime);
   save_p += sizeof leveltime;

   /* cph - total episode time */
   if (compatibility_level >= prboom_2_compatibility) {
      memcpy(&totalleveltimes, save_p, sizeof totalleveltimes);
      save_p += sizeof totalleveltimes;
   }
   else totalleveltimes = 0;

   // killough 11/98: load revenant tracer state
   basetic = gametic - *save_p++;

   // dearchive all the modifications
   P_UnArchivePlayers ();
   P_UnArchiveWorld ();
   P_UnArchiveThinkers ();
   P_UnArchiveSpecials ();
   P_UnArchiveRNG ();    // killough 1/18/98: load RNG information
   P_UnArchiveMap ();    // killough 1/22/98: load automap information

   if (*save_p != 0xe6)
      I_Error ("G_DoLoadGame: Bad savegame");

   // done
   Z_Free (savebuffer);

   if (setsizeneeded)
      R_ExecuteSetViewSize ();

   // draw the pattern into the back screen
   R_FillBackScreen ();

   /* killough 12/98: support -recordfrom and -loadgame -playdemo */
   if (!command_loadgame)
      singledemo = false;  /* Clear singledemo flag if loading from menu */
   else
      if (singledemo) {
         gameaction = ga_loadgame; /* Mark that we're loading a game before demo */
         G_DoPlayDemo();           /* This will detect it and won't reinit level */
      } else /* Command line + record means it's a recordfrom */
         if (demorecording)
            G_BeginRecording();
}

//
// G_SaveGame
// Called by the menu task.
// Description is a 24 byte text string
//

void G_SaveGame(int slot, char *description)
{
   strcpy(savedescription, description);
   if (demoplayback) {
      /* cph - We're doing a user-initiated save game while a demo is
       * running so, go outside normal mechanisms
       */
      savegameslot = slot;
      G_DoSaveGame(true);
   }
   // CPhipps - store info in special_event
   special_event = BT_SPECIAL | (BTS_SAVEGAME & BT_SPECIALMASK) |
                   ((slot << BTS_SAVESHIFT) & BTS_SAVEMASK);
#ifdef HAVE_NET
   D_NetSendMisc(nm_savegamename, strlen(savedescription)+1, savedescription);
#endif
}

// Check for overrun and realloc if necessary -- Lee Killough 1/22/98
void CheckSaveGame(size_t size)
{
   size_t pos = save_p - savebuffer;
   size += 1024;  // breathing room
   if (pos+size > savegamesize)
      save_p = (savebuffer = realloc(savebuffer,
                                     savegamesize += (size+1023) & ~1023)) + pos;
}

/* killough 3/22/98: form savegame name in one location
 * (previously code was scattered around in multiple places)
 * cph - Avoid possible buffer overflow problems by passing
 * size to this function and using snprintf */

void G_SaveGameName(char *name, size_t size, int slot, boolean demoplayback)
{
   const char* sgn = demoplayback ? "demosav" : SAVEGAMENAME;
   snprintf (name, size, "%s%d.dsg", sgn, slot);
}

static void G_DoSaveGame (boolean menu)
{
   char name[100+1];
   char name2[VERSIONSIZE];
   char *description;
   int  length, i;

   gameaction = ga_nothing; // cph - cancel savegame at top of this function,
   // in case later problems cause a premature exit

   G_SaveGameName(name,sizeof(name),savegameslot, demoplayback && !menu);

   description = savedescription;

   save_p = savebuffer = malloc(savegamesize);

   CheckSaveGame(SAVESTRINGSIZE+VERSIONSIZE+sizeof(unsigned long));
   memcpy (save_p, description, SAVESTRINGSIZE);
   save_p += SAVESTRINGSIZE;
   memset (name2,0,sizeof(name2));

   // CPhipps - scan for the version header
   for (i=0; (size_t)i<num_version_headers; i++)
      if (version_headers[i].comp_level == compatibility_level) {
         // killough 2/22/98: "proprietary" version string :-)
         snprintf (name2,sizeof(name2),version_headers[i].ver_printf,version_headers[i].version);
         memcpy (save_p, name2, VERSIONSIZE);
         i = num_version_headers+1;
      }

   if ((size_t)i == num_version_headers) {
      doom_printf("No savegame signature known for\nthis compatibility level\n"
                  "%d/%d, %d registered", compatibility_level,
                  MAX_COMPATIBILITY_LEVEL, (signed)num_version_headers);
      free(savebuffer); // cph - free data
      return;
   }

   save_p += VERSIONSIZE;

   { /* killough 3/16/98, 12/98: store lump name checksum */
      uint_64_t checksum = G_Signature();
      memcpy(save_p, &checksum, sizeof checksum);
      save_p += sizeof checksum;
   }

   // killough 3/16/98: store pwad filenames in savegame
   {
      // CPhipps - changed for new wadfiles handling
      int i = 0;
      for (*save_p = 0; (size_t)i<numwadfiles; i++)
      {
         const char *const w = wadfiles[i].name;
         CheckSaveGame(strlen(w)+2);
         strcat(strcat(save_p, w), "\n");
      }
      save_p += strlen(save_p)+1;
   }

   CheckSaveGame(GAME_OPTION_SIZE+MIN_MAXPLAYERS+10);

   /* cph - FIXME? - Save compatibility level */
   *save_p++ = 0;

   *save_p++ = gameskill;
   *save_p++ = gameepisode;
   *save_p++ = gamemap;

   for (i=0 ; i<MAXPLAYERS ; i++)
      *save_p++ = playeringame[i];

   for (;i<MIN_MAXPLAYERS;i++)         // killough 2/28/98
      *save_p++ = 0;

   *save_p++ = idmusnum;               // jff 3/17/98 save idmus state

   save_p = G_WriteOptions(save_p);    // killough 3/1/98: save game options

   /* cph - FIXME - endianness? */
   /* killough 11/98: save entire word */
   memcpy(save_p, &leveltime, sizeof leveltime);
   save_p += sizeof leveltime;

   /* cph - total episode time */
   if (compatibility_level >= prboom_2_compatibility) {
      memcpy(save_p, &totalleveltimes, sizeof totalleveltimes);
      save_p += sizeof totalleveltimes;
   }
   else totalleveltimes = 0;

   // killough 11/98: save revenant tracer state
   *save_p++ = (gametic-basetic) & 255;

   // killough 3/22/98: add Z_CheckHeap after each call to ensure consistency
   Z_CheckHeap();
   P_ArchivePlayers();
   Z_CheckHeap();

   // phares 9/13/98: Move mobj_t->index out of P_ArchiveThinkers so the
   // indices can be used by P_ArchiveWorld when the sectors are saved.
   // This is so we can save the index of the mobj_t of the thinker that
   // caused a sound, referenced by sector_t->soundtarget.
   P_ThinkerToIndex();

   P_ArchiveWorld();
   Z_CheckHeap();
   P_ArchiveThinkers();

   // phares 9/13/98: Move index->mobj_t out of P_ArchiveThinkers, simply
   // for symmetry with the P_ThinkerToIndex call above.

   P_IndexToThinker();

   Z_CheckHeap();
   P_ArchiveSpecials();
   P_ArchiveRNG();    // killough 1/18/98: save RNG information
   Z_CheckHeap();
   P_ArchiveMap();    // killough 1/22/98: save automap information

   *save_p++ = 0xe6;   // consistancy marker

   length = save_p - savebuffer;

   Z_CheckHeap();
   doom_printf( "%s", M_WriteFile(name, savebuffer, length)
                ? GGSAVED /* Ty - externalised */
                : "Game save failed!"); // CPhipps - not externalised

   free(savebuffer);  // killough
   savebuffer = save_p = NULL;

   savedescription[0] = 0;
}

static skill_t d_skill;
static int     d_episode;
static int     d_map;

void G_DeferedInitNew(skill_t skill, int episode, int map)
{
   d_skill = skill;
   d_episode = episode;
   d_map = map;
   gameaction = ga_newgame;
}

extern int variable_friction;
extern int default_variable_friction;  // ice & mud

extern int weapon_recoil, default_weapon_recoil;    // weapon recoil

extern int allow_pushers;
extern int default_allow_pushers;     // MT_PUSH Things

extern int player_bobbing;
extern int default_player_bobbing;    // whether player bobs or not

extern int monsters_remember, default_monsters_remember;

/* cph -
 * G_Compatibility
 *
 * Initialises the comp[] array based on the compatibility_level
 * For reference, MBF did:
 * for (i=0; i < COMP_TOTAL; i++)
 *   comp[i] = compatibility;
 *
 * Instead, we have a lookup table showing at what version a fix was
 *  introduced.
 */

void G_Compatibility(void)
{
   static const complevel_t fix_levels[COMP_NUM] = {
            mbf_compatibility, /* comp_telefrag - monsters used to telefrag only
              * on MAP30, now they do it for spawners only */
            mbf_compatibility, /* comp_dropoff - MBF encourages things to drop
              * off of overhangs */
            boom_compatibility,/* comp_vile - original Doom archville bugs like
              * ghosts */
            boom_compatibility,/* comp_pain - original Doom limits Pain Elements
              * from spawning too many skulls */
            boom_compatibility,/* comp_skull - original Doom let skulls be spit
              * through walls by Pain Elementals */
            boom_compatibility,/* comp_blazing - original Doom duplicated
              * blazing door sound */
            mbf_compatibility, /* comp_doorlight - MBF made door lighting changes
              * more gradual */
            boom_compatibility,/* comp_model - improvements to the game physics */
            boom_compatibility,/* comp_god - fixes to God mode */
            mbf_compatibility, /* comp_falloff - MBF encourages things to drop
              * off of overhangs */
            boom_compatibility_compatibility,
            /* comp_floors - fixes for moving floors bugs */
            boom_compatibility,/* comp_skymap */
            mbf_compatibility, /* comp_pursuit - MBF AI change, limited pursuit? */
            boom_compatibility,/* comp_doorstuck - monsters stuck in doors fix */
            mbf_compatibility, /* comp_staylift - MBF AI change, monsters try
              * to stay on lifts */
            lxdoom_1_compatibility, /* comp_zombie - prevent dead players
                   * triggering stuff */
            boom_compatibility_compatibility,  /* comp_stairs - see p_floor.c */
            mbf_compatibility, /* comp_infcheat - FIXME */
            boom_compatibility,/* comp_zerotags - allow zero tags in wads */
            lxdoom_1_compatibility, /* comp_moveblock - enables keygrab and
                   * mancubi shots going thru walls */
            prboom_2_compatibility, /* comp_respawn - objects which aren't on the map
                                     * at game start respawn at (0,0) */
            boom_compatibility_compatibility,  /* comp_sound - see s_sound.c */
         };
   int i;
   for (i=0; i<COMP_NUM; i++)
      comp[i] = compatibility_level < fix_levels[i];
   for (; i<COMP_TOTAL; i++) comp[i] = 1;
}

#ifdef DOGS
/* killough 7/19/98: Marine's best friend :) */
static int G_GetHelpers(void)
{
   int j = M_CheckParm ("-dog");

   if (!j)
      j = M_CheckParm ("-dogs");
return j ? j+1 < myargc ? atoi(myargv[j+1]) : 1 : default_dogs;
}
#endif


// killough 3/1/98: function to reload all the default parameter
// settings before a new game begins

void G_ReloadDefaults(void)
{
   // killough 3/1/98: Initialize options based on config file
   // (allows functions above to load different values for demos
   // and savegames without messing up defaults).

   weapon_recoil = default_weapon_recoil;    // weapon recoil

   player_bobbing = default_player_bobbing;  // whether player bobs or not

   variable_friction = allow_pushers = true;

   monsters_remember = default_monsters_remember;   // remember former enemies

   monster_infighting = default_monster_infighting; // killough 7/19/98

#ifdef DOGS
   dogs = netgame ? 0 : G_GetHelpers();             // killough 7/19/98
   dog_jumping = default_dog_jumping;
#endif

   distfriend = default_distfriend;                 // killough 8/8/98

   monster_backing = default_monster_backing;     // killough 9/8/98

   monster_avoid_hazards = default_monster_avoid_hazards; // killough 9/9/98

   monster_friction = default_monster_friction;     // killough 10/98

   help_friends = default_help_friends;             // killough 9/9/98

   monkeys = default_monkeys;

   // jff 1/24/98 reset play mode to command line spec'd version
   // killough 3/1/98: moved to here
//   respawnparm = clrespawnparm;
//   fastparm = clfastparm;
//   nomonsters = clnomonsters;

   //jff 3/24/98 set startskill from defaultskill in config file, unless
   // it has already been set by a -skill parameter
   if (startskill==sk_none)
      startskill = (skill_t)(defaultskill-1);

   demoplayback = false;
   singledemo = false;            // killough 9/29/98: don't stop after 1 demo
   netdemo = false;

   // killough 2/21/98:
   memset(playeringame+1, 0, sizeof(*playeringame)*(MAXPLAYERS-1));

   consoleplayer = 0;

   compatibility_level = default_compatibility_level;
   {
      int i = M_CheckParm("-complevel");
      if (i && (1+i) < myargc) compatibility_level = atoi(myargv[i+1]);
   }
   if (compatibility_level == (complevel_t)-1)
      compatibility_level = MAX_COMPATIBILITY_LEVEL-1;

   if (mbf_features)
      memcpy(comp, default_comp, sizeof comp);
   else
      G_Compatibility();

   // killough 3/31/98, 4/5/98: demo sync insurance
   demo_insurance = default_demo_insurance == 1;

   rngseed += 1 + gametic; // CPhipps
//   rngseed += I_GetRandomTimeSeed() + gametic; // CPhipps
}

void G_DoNewGame (void)
{
   G_ReloadDefaults();            // killough 3/1/98
   netgame = false;               // killough 3/1/98
   deathmatch = false;
   G_InitNew (d_skill, d_episode, d_map);
   gameaction = ga_nothing;

   //jff 4/26/98 wake up the status bar in case were coming out of a DM demo
   ST_Start();
}

// killough 4/10/98: New function to fix bug which caused Doom
// lockups when idclev was used in conjunction with -fast.

void G_SetFastParms(int fast_pending)
{
   static int fast = 0;            // remembers fast state
   int i;
   if (fast != fast_pending) {     /* only change if necessary */
      if ((fast = fast_pending))
      {
         for (i=S_SARG_RUN1; i<=S_SARG_PAIN2; i++)
            if (states[i].tics != 1 || demo_compatibility) // killough 4/10/98
               states[i].tics >>= 1;  // don't change 1->0 since it causes cycles
         mobjinfo[MT_BRUISERSHOT].speed = 20*FRACUNIT;
         mobjinfo[MT_HEADSHOT].speed = 20*FRACUNIT;
         mobjinfo[MT_TROOPSHOT].speed = 20*FRACUNIT;
      }
      else
      {
         for (i=S_SARG_RUN1; i<=S_SARG_PAIN2; i++)
            states[i].tics <<= 1;
         mobjinfo[MT_BRUISERSHOT].speed = 15*FRACUNIT;
         mobjinfo[MT_HEADSHOT].speed = 10*FRACUNIT;
         mobjinfo[MT_TROOPSHOT].speed = 10*FRACUNIT;
      }
   }
}

// The sky texture to be used instead of the F_SKY1 dummy.
extern  int skytexture;

//
// G_InitNew
// Can be called by the startup code or the menu task,
// consoleplayer, displayplayer, playeringame[] should be set.
//

void G_InitNew(skill_t skill, int episode, int map)
{
   int i;

   if (paused)
   {
      paused = false;
      S_ResumeSound();
   }

   if (skill > sk_nightmare)
      skill = sk_nightmare;

   if (episode < 1)
      episode = 1;

   if (gamemode == retail)
   {
      if (episode > 4)
         episode = 4;
   }
   else
      if (gamemode == shareware)
      {
         if (episode > 1)
            episode = 1; // only start episode 1 on shareware
      }
      else
         if (episode > 3)
            episode = 3;

   if (map < 1)
      map = 1;
   if (map > 9 && gamemode != commercial)
      map = 9;

   G_SetFastParms(fastparm || skill == sk_nightmare);  // killough 4/10/98

   M_ClearRandom();

   respawnmonsters = skill == sk_nightmare || respawnparm;

   // force players to be initialized upon first level load
   for (i=0 ; i<MAXPLAYERS ; i++)
      players[i].playerstate = PST_REBORN;

   usergame = true;                // will be set false if a demo
   paused = false;
   automapmode &= ~am_active;
   gameepisode = episode;
   gamemap = map;
   gameskill = skill;

   totalleveltimes = 0; // cph

   //jff 4/16/98 force marks on automap cleared every new level start
   AM_clearMarks();

   G_DoLoadLevel ();
}


//
// DEMO RECORDING
//

unsigned char DEMOMARKER=0x80;

void G_ReadDemoTiccmd (ticcmd_t* cmd)
{
   if (*demo_p == DEMOMARKER)
      G_CheckDemoStatus();      // end of demo data stream
   else
   {
      cmd->forwardmove = ((signed char)*demo_p++);
      cmd->sidemove = ((signed char)*demo_p++);
      if (!longtics) {
         cmd->angleturn = ((unsigned char)*demo_p++)<<8;
      } else {
         unsigned int lowbyte = (unsigned char)*demo_p++;
         cmd->angleturn = (((signed int)(*demo_p++))<<8) + lowbyte;
      }
      cmd->buttons = (unsigned char)*demo_p++;
   }
}

/* Demo limits removed -- killough
 * cph - record straight to file
 */
void G_WriteDemoTiccmd (ticcmd_t* cmd)
{
   char buf[5];
   char *p = buf;

   *p++ = cmd->forwardmove;
   *p++ = cmd->sidemove;
   if (!longtics) {
      *p++ = (cmd->angleturn+128)>>8;
   } else {
      signed short a = cmd->angleturn;
      *p++ = a & 0xff;
      *p++ = (a >> 8) & 0xff;
   }
   *p++ = cmd->buttons;
   if (write(demofd, buf, p-buf) != p-buf)
      I_Error("G_WriteDemoTiccmd: error writing demo");

   /* cph - alias demo_p to it so we can read it back */
   demo_p = buf;
   G_ReadDemoTiccmd (cmd);         // make SURE it is exactly the same
}

//
// G_RecordDemo
//

void G_RecordDemo (const char* name)
{
   char     demoname[100];
   usergame = false;
   AddDefaultExtension(strcpy(demoname, name), ".lmp");  // 1/18/98 killough
   demorecording = true;
   /* cph - Record demos straight to file
    * If file already exists, try to continue existing demo
    */
   if (fileexists(demoname)) {
      demofd = open(demoname, O_WRONLY | O_APPEND);
   } else {
      demofd = open(demoname, O_WRONLY | O_RDONLY);
      if (demofd) {
         int slot = -1;
         int rc;
         {
            byte buf[200];
            size_t len;
            read(demofd, buf, sizeof(buf));

            len = G_ReadDemoHeader(buf) - buf;
            lseek(demofd, len, SEEK_SET);
         }
         do {
            byte buf[4];
            rc = read(demofd, buf, sizeof(buf));
            if (buf[0] == DEMOMARKER) break;
            if (buf[3] & BT_SPECIAL)
               if ((buf[3] & BT_SPECIALMASK) == BTS_SAVEGAME)
                  slot = (buf[3] & BTS_SAVEMASK)>>BTS_SAVESHIFT;
         } while (rc == /* sizeof(buf) is out of scope here */ 4 );
         if (slot == -1) I_Error("G_RecordDemo: No save in demo, can't continue");
         lseek(demofd, -rc, SEEK_CUR);
         G_LoadGame(slot, false);
         autostart = false;
      }
   }
   if (!demofd) I_Error("G_RecordDemo: failed to open %s", name);
}

// These functions are used to read and write game-specific options in demos
// and savegames so that demo sync is preserved and savegame restoration is
// complete. Not all options (for example "compatibility"), however, should
// be loaded and saved here. It is extremely important to use the same
// positions as before for the variables, so if one becomes obsolete, the
// byte(s) should still be skipped over or padded with 0's.
// Lee Killough 3/1/98

extern boolean forceOldBsp;

byte *G_WriteOptions(byte *demo_p)
{
   byte *target = demo_p + GAME_OPTION_SIZE;

   *demo_p++ = monsters_remember;  // part of monster AI

   *demo_p++ = variable_friction;  // ice & mud

   *demo_p++ = weapon_recoil;      // weapon recoil

   *demo_p++ = allow_pushers;      // MT_PUSH Things

   *demo_p++ = 0;

   *demo_p++ = player_bobbing;  // whether player bobs or not

   // killough 3/6/98: add parameters to savegame, move around some in demos
   *demo_p++ = respawnparm;
   *demo_p++ = fastparm;
   *demo_p++ = nomonsters;

   *demo_p++ = demo_insurance;        // killough 3/31/98

   // killough 3/26/98: Added rngseed. 3/31/98: moved here
   *demo_p++ = (byte)((rngseed >> 24) & 0xff);
   *demo_p++ = (byte)((rngseed >> 16) & 0xff);
   *demo_p++ = (byte)((rngseed >>  8) & 0xff);
   *demo_p++ = (byte)( rngseed        & 0xff);

   // Options new to v2.03 begin here

   *demo_p++ = monster_infighting;   // killough 7/19/98

#ifdef DOGS
   *demo_p++ = dogs;                 // killough 7/19/98
#else
   *demo_p++ = 0;
#endif

   *demo_p++ = 0;
   *demo_p++ = 0;

   *demo_p++ = (distfriend >> 8) & 0xff;  // killough 8/8/98
   *demo_p++ =  distfriend       & 0xff;  // killough 8/8/98

   *demo_p++ = monster_backing;         // killough 9/8/98

   *demo_p++ = monster_avoid_hazards;    // killough 9/9/98

   *demo_p++ = monster_friction;         // killough 10/98

   *demo_p++ = help_friends;             // killough 9/9/98

#ifdef DOGS
   *demo_p++ = dog_jumping;
#else
   *demo_p++ = 0;
#endif

   *demo_p++ = monkeys;

   {   // killough 10/98: a compatibility vector now
      int i;
      for (i=0; i < COMP_TOTAL; i++)
         *demo_p++ = comp[i] != 0;
   }

   *demo_p++ = (compatibility_level >= prboom_2_compatibility) && forceOldBsp; // cph 2002/07/20

   //----------------
   // Padding at end
   //----------------
   while (demo_p < target)
      *demo_p++ = 0;

   if (demo_p != target)
      I_Error("G_WriteOptions: GAME_OPTION_SIZE is too small");

   return target;
}

/* Same, but read instead of write
 * cph - const byte*'s
 */

const byte *G_ReadOptions(const byte *demo_p)
{
   const byte *target = demo_p + GAME_OPTION_SIZE;

   monsters_remember = *demo_p++;

   variable_friction = *demo_p;  // ice & mud
   demo_p++;

   weapon_recoil = *demo_p;       // weapon recoil
   demo_p++;

   allow_pushers = *demo_p;      // MT_PUSH Things
   demo_p++;

   demo_p++;

   player_bobbing = *demo_p;     // whether player bobs or not
   demo_p++;

   // killough 3/6/98: add parameters to savegame, move from demo
   respawnparm = *demo_p++;
   fastparm = *demo_p++;
   nomonsters = *demo_p++;

   demo_insurance = *demo_p++;              // killough 3/31/98

   // killough 3/26/98: Added rngseed to demos; 3/31/98: moved here

   rngseed  = *demo_p++ & 0xff;
   rngseed <<= 8;
   rngseed += *demo_p++ & 0xff;
   rngseed <<= 8;
   rngseed += *demo_p++ & 0xff;
   rngseed <<= 8;
   rngseed += *demo_p++ & 0xff;

   // Options new to v2.03
   if (mbf_features)
   {
      monster_infighting = *demo_p++;   // killough 7/19/98

#ifdef DOGS
      dogs = *demo_p++;                 // killough 7/19/98
#else
      demo_p++;
#endif

      demo_p += 2;

      distfriend = *demo_p++ << 8;      // killough 8/8/98
      distfriend+= *demo_p++;

      monster_backing = *demo_p++;     // killough 9/8/98

      monster_avoid_hazards = *demo_p++; // killough 9/9/98

      monster_friction = *demo_p++;      // killough 10/98

      help_friends = *demo_p++;          // killough 9/9/98

#ifdef DOGS
      dog_jumping = *demo_p++;           // killough 10/98
#else
      demo_p++;
#endif

      monkeys = *demo_p++;

      {   // killough 10/98: a compatibility vector now
         int i;
         for (i=0; i < COMP_TOTAL; i++)
            comp[i] = *demo_p++;
      }

      forceOldBsp = *demo_p++; // cph 2002/07/20
   }
   else  /* defaults for versions <= 2.02 */
   {
      /* cph - comp[] has already been set up right by G_Compatibility */

      monster_infighting = 1;           // killough 7/19/98

      monster_backing = 0;              // killough 9/8/98

      monster_avoid_hazards = 0;        // killough 9/9/98

      monster_friction = 0;             // killough 10/98

      help_friends = 0;                 // killough 9/9/98

#ifdef DOGS
      dogs = 0;                         // killough 7/19/98
      dog_jumping = 0;                  // killough 10/98
#endif

      monkeys = 0;
   }

   return target;
}

void G_BeginRecording (void)
{
   int i;
   byte *demostart, *demo_p;
   demostart = demo_p = malloc(1000);

   /* cph - 3 demo record formats supported: MBF+, BOOM, and Doom v1.9 */
   if (mbf_features) {
      { /* Write version code into demo */
         unsigned char v=0;
         switch(compatibility_level) {
         case doom_12_compatibility:
         case doom_demo_compatibility:
         case doom_compatibility:
         case boom_compatibility_compatibility:
         case boom_201_compatibility:
         case boom_202_compatibility:
         case lxdoom_1_compatibility:
         case prboom_1_compatibility:
         case MAX_COMPATIBILITY_LEVEL:
         case mbf_compatibility: v = 204; break;
         case prboom_2_compatibility: v = 210; break;
         case prboom_3_compatibility: v = 211; break;
         }
         *demo_p++ = v;
      }

      // signature
      *demo_p++ = 0x1d;
      *demo_p++ = 'M';
      *demo_p++ = 'B';
      *demo_p++ = 'F';
      *demo_p++ = 0xe6;
      *demo_p++ = '\0';

      /* killough 2/22/98: save compatibility flag in new demos
       * cph - FIXME? MBF demos will always be not in compat. mode */
      *demo_p++ = 0;

      *demo_p++ = gameskill;
      *demo_p++ = gameepisode;
      *demo_p++ = gamemap;
      *demo_p++ = deathmatch;
      *demo_p++ = consoleplayer;

      demo_p = G_WriteOptions(demo_p); // killough 3/1/98: Save game options

      for (i=0 ; i<MAXPLAYERS ; i++)
         *demo_p++ = playeringame[i];

      // killough 2/28/98:
      // We always store at least MIN_MAXPLAYERS bytes in demo, to
      // support enhancements later w/o losing demo compatibility

      for (; i<MIN_MAXPLAYERS; i++)
         *demo_p++ = 0;

   } else if (compatibility_level > doom_compatibility) {
      byte v=0, c=0; /* Nominally, version and compatibility bits */
      switch (compatibility_level) {
      case boom_compatibility_compatibility: v = 202, c = 1; break;
      case boom_201_compatibility: v = 201; c = 0; break;
      case boom_202_compatibility: v = 202, c = 0; break;
      default: I_Error("G_BeginRecording: Boom compatibility level unrecognised?");
      }
      *demo_p++ = v;

      // signature
      *demo_p++ = 0x1d;
      *demo_p++ = 'B';
      *demo_p++ = 'o';
      *demo_p++ = 'o';
      *demo_p++ = 'm';
      *demo_p++ = 0xe6;

      /* CPhipps - save compatibility level in demos */
      *demo_p++ = c;

      *demo_p++ = gameskill;
      *demo_p++ = gameepisode;
      *demo_p++ = gamemap;
      *demo_p++ = deathmatch;
      *demo_p++ = consoleplayer;

      demo_p = G_WriteOptions(demo_p); // killough 3/1/98: Save game options

      for (i=0 ; i<MAXPLAYERS ; i++)
         *demo_p++ = playeringame[i];

      // killough 2/28/98:
      // We always store at least MIN_MAXPLAYERS bytes in demo, to
      // support enhancements later w/o losing demo compatibility

      for (; i<MIN_MAXPLAYERS; i++)
         *demo_p++ = 0;
   } else { // cph - write old v1.9 demos (might even sync)
      longtics = M_CheckParm("-longtics");
      *demo_p++ = longtics ? 111 : 109; // v1.9 has best chance of syncing these
      *demo_p++ = gameskill;
      *demo_p++ = gameepisode;
      *demo_p++ = gamemap;
      *demo_p++ = deathmatch;
      *demo_p++ = respawnparm;
      *demo_p++ = fastparm;
      *demo_p++ = nomonsters;
      *demo_p++ = consoleplayer;
      for (i=0; i<4; i++)  // intentionally hard-coded 4 -- killough
         *demo_p++ = playeringame[i];
   }

   if (write(demofd, demostart, demo_p-demostart) != (signed)(size_t)(demo_p-demostart))
      I_Error("G_BeginRecording: Error writing demo header");
   free(demostart);
}


//
// G_PlayDemo
//

static const char *defdemoname;

void G_DeferedPlayDemo (const char* name)
{
   defdemoname = name;
   gameaction = ga_playdemo;
}

static int demolumpnum = -1;

static const byte* G_ReadDemoHeader(const byte *demo_p)
{
   skill_t skill;
   int i, episode, map;
   int demover;

   basetic = gametic;  // killough 9/29/98

   // killough 2/22/98, 2/28/98: autodetect old demos and act accordingly.
   // Old demos turn on demo_compatibility => compatibility; new demos load
   // compatibility flag, and other flags as well, as a part of the demo.

   demover = *demo_p++;

   if (demover < 200)     // Autodetect old demos
   {
      compatibility_level = doom_demo_compatibility;
      if (demover >= 111) longtics = 1;

      G_Compatibility();

      // killough 3/2/98: force these variables to be 0 in demo_compatibility

      variable_friction = 0;

      weapon_recoil = 0;

      allow_pushers = 0;

      monster_infighting = 1;           // killough 7/19/98

#ifdef DOGS
      dogs = 0;                         // killough 7/19/98
      dog_jumping = 0;                  // killough 10/98
#endif

      monster_backing = 0;              // killough 9/8/98

      monster_avoid_hazards = 0;        // killough 9/9/98

      monster_friction = 0;             // killough 10/98
      help_friends = 0;                 // killough 9/9/98
      monkeys = 0;

      // killough 3/6/98: rearrange to fix savegame bugs (moved fastparm,
      // respawnparm, nomonsters flags to G_LoadOptions()/G_SaveOptions())

      if ((skill=demover) >= 100)         // For demos from versions >= 1.4
      {
         skill = *demo_p++;
         episode = *demo_p++;
         map = *demo_p++;
         deathmatch = *demo_p++;
         respawnparm = *demo_p++;
         fastparm = *demo_p++;
         nomonsters = *demo_p++;
         consoleplayer = *demo_p++;
      }
      else
      {
         episode = *demo_p++;
         map = *demo_p++;
         deathmatch = respawnparm = fastparm =
                                       nomonsters = consoleplayer = 0;
      }
   }
   else    // new versions of demos
   {
      demo_p += 6;               // skip signature;
      switch (demover) {
      case 200: /* BOOM */
      case 201:
         if (!*demo_p++)
            compatibility_level = boom_201_compatibility;
         else
            compatibility_level = boom_compatibility_compatibility;
         break;
      case 202:
         if (!*demo_p++)
            compatibility_level = boom_202_compatibility;
         else
            compatibility_level = boom_compatibility_compatibility;
         break;
      case 203:
         /* LxDoom or MBF - determine from signature
          * cph - load compatibility level */
         switch (demobuffer[2]) {
         case 'B': /* LxDoom */
            /* cph - DEMOSYNC - LxDoom demos recorded in compatibility modes support dropped */
            compatibility_level = lxdoom_1_compatibility;
            break;
         case 'M':
            compatibility_level = mbf_compatibility;
            demo_p++;
            break;
         }
         break;
      case 210:
         compatibility_level = prboom_2_compatibility;
         demo_p++;
         break;
      case 211:
         compatibility_level = prboom_3_compatibility;
         demo_p++;
         break;
      }
      G_Compatibility();
      skill = *demo_p++;
      episode = *demo_p++;
      map = *demo_p++;
      deathmatch = *demo_p++;
      consoleplayer = *demo_p++;

      demo_p = G_ReadOptions(demo_p);  // killough 3/1/98: Read game options

      if (demover == 200)              // killough 6/3/98: partially fix v2.00 demos
         demo_p += 128-GAME_OPTION_SIZE;
   }

//   printf( "G_DoPlayDemo: playing demo with %s compatibility\n",
//           comp_lev_str[compatibility_level]);

   if (demo_compatibility)  // only 4 players can exist in old demos
   {
      for (i=0; i<4; i++)  // intentionally hard-coded 4 -- killough
         playeringame[i] = *demo_p++;
      for (;i < MAXPLAYERS; i++)
         playeringame[i] = 0;
   }
   else
   {
      for (i=0 ; i < MAXPLAYERS; i++)
         playeringame[i] = *demo_p++;
      demo_p += MIN_MAXPLAYERS - MAXPLAYERS;
   }

   if (playeringame[1])
   {
      netgame = true;
      netdemo = true;
   }

   if (gameaction != ga_loadgame) { /* killough 12/98: support -loadgame */
      G_InitNew(skill, episode, map);
   }

   for (i=0; i<MAXPLAYERS;i++)         // killough 4/24/98
      players[i].cheats = 0;

   return demo_p;
}

void G_DoPlayDemo(void)
{
   char basename[9];

   ExtractFileBase(defdemoname,basename);           // killough
   basename[8] = 0;
   demobuffer = demo_p = W_CacheLumpNum(demolumpnum = W_GetNumForName(basename));
   /* cph - store lump number for unlocking later */

   demo_p = G_ReadDemoHeader(demo_p);

   gameaction = ga_nothing;
   usergame = false;

   demoplayback = true;
}

//
// G_TimeDemo
//

void G_TimeDemo(const char *name) // CPhipps - const char*
{
   timingdemo = true;
   singletics = true;
   defdemoname = name;
   gameaction = ga_playdemo;
}


/* G_CheckDemoStatus
 *
 * Called after a death or level completion to allow demos to be cleaned up
 * Returns true if a new demo loop action will take place
 */

boolean G_CheckDemoStatus (void)
{
   if (demorecording)
   {
      demorecording = false;
      write(demofd, &DEMOMARKER, 1);
      close(demofd);
      I_Error("G_CheckDemoStatus: Demo recorded");
      return false;  // killough
   }

   if (timingdemo)
   {
//      int endtime = I_GetTime_RealTime ();
      int endtime = I_GetTime ();
      // killough -- added fps information and made it work for longer demos:
      unsigned realtics = endtime-starttime;
      int fd=open(GAMEBASE "timedemo.txt",O_WRONLY | O_CREAT,0666);
      fdprintf (fd,"Timed %d gametics in %d realtics = %d frames per second",
               (unsigned) gametic, realtics,
               (unsigned) gametic * TICRATE/ realtics);
      close(fd);
      I_Error ("%d gametics in %d realtics",
               (unsigned) gametic,realtics,
               (unsigned) gametic * TICRATE / realtics);
      return false;
   }

   if (demoplayback)
   {
      if (singledemo)
         I_Error("Done Playing Demo");
         return false;
//         exit(0);  // killough

      if (demolumpnum != -1) {
         // cph - unlock the demo lump
         W_UnlockLumpNum(demolumpnum);
         demolumpnum = -1;
      }
      G_ReloadDefaults();    // killough 3/1/98
      netgame = false;       // killough 3/29/98
      deathmatch = false;
      D_AdvanceDemo ();
      return true;
   }
   return false;
}

// killough 1/22/98: this is a "Doom printf" for messages. I've gotten
// tired of using players->message=... and so I've added this dprintf.
//
// killough 3/6/98: Made limit static to allow z_zone functions to call
// this function, without calling realloc(), which seems to cause problems.

#define MAX_MESSAGE_SIZE 1024
static char msg[MAX_MESSAGE_SIZE];

// CPhipps - renamed to doom_printf to avoid name collision with glibc
void doom_printf(const char *s, ...)
{
   va_list v;
   va_start(v,s);
   vsnprintf(msg,sizeof(msg),s,v);        /* print message in buffer */
   va_end(v);
   players[consoleplayer].message = msg;  // set new message
}