[go: up one dir, main page]

Menu

[904618]: / src / sn_core.c  Maximize  Restore  History

Download this file

2084 lines (1709 with data), 45.6 kB

   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
// -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*-
// ----------------------------------------------------------------------------
// :oCCCCOCoc.
// .cCO8OOOOOOOOO8Oo:
// .oOO8OOOOOOOOOOOOOOOCc
// cO8888: .:oOOOOC. TM
// :888888: :CCCc .oOOOOC. ### ### #########
// C888888: .ooo: .C######## ##### ##### ###### ###### ##########
// O888888: .oO### ### ##### ##### ######## ######## #### ###
// C888888: :8O. .C########## ### #### ### ## ## ## ## #### ###
// :8@@@@8: :888c o### ### #### ### ######## ######## ##########
// :8@@@@C C@@@@ oo######## ### ## ### ###### ###### #########
// cO@@@@@@@@@@@@@@@@@Oc0
// :oO8@@@@@@@@@@Oo.
// .oCOOOOOCc. http://remood.org/
// ----------------------------------------------------------------------------
// Copyright (C) 2013-2013 GhostlyDeath <ghostlydeath@remood.org>
// <ghostlydeath@gmail.com>
// ----------------------------------------------------------------------------
// 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 3
// 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.
// ----------------------------------------------------------------------------
// DESCRIPTION:
/***************
*** INCLUDES ***
***************/
#include "doomtype.h"
#include "sn.h"
#include "g_state.h"
#include "d_player.h"
#include "d_netcmd.h"
#include "d_prof.h"
#include "console.h"
#include "dstrings.h"
#include "z_zone.h"
#include "p_demcmp.h"
#include "r_main.h"
#include "m_argv.h"
#include "g_game.h"
#include "p_setup.h"
#include "s_sound.h"
#include "d_clisrv.h"
/****************
*** CONSTANTS ***
****************/
#define MAXGLOBALBUFSIZE 8 // Size of global buffer
#define MAXRRSYNCSIZE (TICRATE * 2) // Round robin size
/*************
*** LOCALS ***
*************/
static struct
{
tic_t GameTic; // Gametic
uint32_t Code; // Sync Code
} l_SyncCodeRR[MAXRRSYNCSIZE]; // Round robin for sync code
static int32_t l_SyncRRAt; // Position of Round Robin
static bool_t l_DedSv; // Dedicated server
static bool_t l_Connected; // Connected
static bool_t l_Server; // We are server
static ticcmd_t* l_GlobalBuf; // Global buffer
static int32_t l_GlobalAt = -1; // Position Global buf is at
static int32_t l_GlobalMax = 0; // Max size of global buffer
static SN_Host_t* l_MyHost; // This games host
static SN_Host_t** l_Hosts; // Hosts
static int32_t l_NumHosts; // Number of hosts
SN_Host_t*** g_HostsP = &l_Hosts;
int32_t* g_NumHostsP = &l_NumHosts;
static SN_TicBuf_t l_NowTic[2]; // Tic that is now!
static uint8_t l_NowPress; // Which "now" is pressed
static SN_TicBuf_t l_LocalBuf[MAXNETXTICS]; // Local tic buffer
static int32_t l_LocalAt; // Local tics at
/****************
*** FUNCTIONS ***
****************/
/* D_NetSetPlayerName() -- Sets name of player */
bool_t D_NetSetPlayerName(const int32_t a_PlayerID, const char* const a_Name)
{
uint32_t OldNameHash;
uint32_t NewNameHash;
char OldName[MAXPLAYERNAME + 1];
/* Check */
if (a_PlayerID < 0 || a_PlayerID >= MAXPLAYERS || !a_Name)
return false;
/* Hash old name */
memset(OldName, 0, sizeof(OldName));
strncpy(OldName, player_names[a_PlayerID], MAXPLAYERNAME);
OldNameHash = Z_Hash(player_names[a_PlayerID]);
/* Copy name over */
strncpy(player_names[a_PlayerID], a_Name, MAXPLAYERNAME);
player_names[a_PlayerID][MAXPLAYERNAME - 1] = 0;
NewNameHash = Z_Hash(player_names[a_PlayerID]);
/* Inform? */
if (OldNameHash != NewNameHash)
CONL_OutputUT(CT_NETWORK, DSTR_NETPLAYERRENAMED, "%s%s\n", OldName, player_names[a_PlayerID]);
/* Success! */
return true;
}
bool_t D_NetPlayerChangedPause(const int32_t a_PlayerID)
{
return true;
}
/*** GLOBAL TICS ***/
/* SN_ExtCmdInGlobal() -- Grabs extended command in global command */
bool_t SN_ExtCmdInGlobal(const uint8_t a_ID, uint8_t** const a_Wp)
{
/* Check */
if (a_ID < 0 || a_ID >= NUMDTCT || !a_Wp)
return false;
/* Prevent global buffer overflow */
if (l_GlobalAt >= l_GlobalMax - 1)
{
Z_ResizeArray((void**)&l_GlobalBuf, sizeof(*l_GlobalBuf),
l_GlobalMax, l_GlobalMax + 1);
l_GlobalMax++;
}
/* Nothing grabbed? */
if (l_GlobalAt < 0)
{
l_GlobalAt = 0;
memset(&l_GlobalBuf[l_GlobalAt], 0, sizeof(l_GlobalBuf[l_GlobalAt]));
}
/* First attempt to grab, from first set */
l_GlobalBuf[l_GlobalAt].Ctrl.Type = 1; // Set extended
if (SN_ExtCmdInTicCmd(a_ID, a_Wp, &l_GlobalBuf[l_GlobalAt]))
return true;
/* Failed, increase global at and try next one */
l_GlobalAt++;
memset(&l_GlobalBuf[l_GlobalAt], 0, sizeof(l_GlobalBuf[l_GlobalAt]));
l_GlobalBuf[l_GlobalAt].Ctrl.Type = 1; // Set extended
if (SN_ExtCmdInTicCmd(a_ID, a_Wp, &l_GlobalBuf[l_GlobalAt]))
return true;
/* Completely failed */
return false;
}
/* SN_ExtCmdInTicCmd() -- Grabs extended command in tic command */
bool_t SN_ExtCmdInTicCmd(const uint8_t a_ID, uint8_t** const a_Wp, ticcmd_t* const a_TicCmd)
{
uint16_t* dsP;
uint8_t* dbP;
/* Check */
if (a_ID < 0 || a_ID >= NUMDTCT || !a_Wp || !a_TicCmd)
return false;
/* Extended Tic */
if (a_TicCmd->Ctrl.Type == 1)
{
dsP = &a_TicCmd->Ext.DataSize;
dbP = a_TicCmd->Ext.DataBuf;
}
/* Standard Tic */
else if (a_TicCmd->Ctrl.Type == 0)
{
dsP = &a_TicCmd->Std.DataSize;
dbP = a_TicCmd->Std.DataBuf;
}
/* Bad Type */
else
return false;
/* Not enough room to store extended command? */
if (c_TCDataSize[a_ID] + 2 >= MAXTCDATABUF - *dsP)
return false;
/* Write Command at point */
*a_Wp = &((dbP)[*dsP]);
WriteUInt8((uint8_t**)a_Wp, a_ID);
*dsP += c_TCDataSize[a_ID] + 1;
// Was written OK
return true;
}
/*** SERVER CONTROL ***/
/* SN_DropAllClients() -- Drops all clients from the game */
void SN_DropAllClients(const char* const a_Reason)
{
static bool_t Dropping;
int32_t i;
SN_Host_t* Host;
/* Do not double drop */
if (Dropping)
return;
Dropping = true;
/* Clear hosts */
if (l_Hosts)
for (i = 0; i < l_NumHosts; i++)
if ((Host = l_Hosts[i]))
SN_DisconnectHost(Host, (a_Reason ? a_Reason : "Server disconnect"));
/* Done dropping */
Dropping = false;
}
/* SN_CommonDiscStuff() -- Common disconnect stuff */
static void SN_CommonDiscStuff(void)
{
int32_t i;
/* Clear the global buffer */
if (l_GlobalBuf)
Z_Free(l_GlobalBuf);
l_GlobalBuf = NULL;
l_GlobalAt = -1;
l_GlobalMax = 0;
/* Clear local Buffer */
memset(l_LocalBuf, 0, sizeof(l_LocalBuf));
l_LocalAt = 0;
/* Clear now tics and jobs */
memset(l_NowTic, 0, sizeof(l_NowTic));
l_NowPress = 0;
SN_ClearJobs();
/* Draw stuff */
SN_SetServerLagWarn(0);
/* Sync Codes */
l_SyncRRAt = 0;
memset(l_SyncCodeRR, 0, sizeof(l_SyncCodeRR));
}
/* SN_Disconnect() -- Disconnects from server */
void SN_Disconnect(const bool_t a_FromDemo, const char* const a_Reason)
{
static bool_t InDis;
SN_Host_t* Host;
int i;
/* If disconnected already, stop */
if (InDis)
return;
// Do not double disconnect
InDis = true;
/* Stop demo playing */
if (!a_FromDemo)
if (demoplayback)
{
singledemo = false;
G_StopDemoPlay();
}
/* Remove splits */
if (l_Connected || SN_HasSocket())
D_NCResetSplits(a_FromDemo);
/* Clear hosts */
SN_DropAllClients(a_Reason);
/* Terminate network connection */
SN_NetTerm(a_Reason);
/* Destroy hosts */
if (l_Hosts)
{
// Individual host
for (i = 0; i < l_NumHosts; i++)
{
if ((Host = l_Hosts[i]))
SN_DestroyHost(Host);
l_Hosts[i] = NULL;
}
Z_Free(l_Hosts);
l_NumHosts = 0;
}
// Clear pointers
l_MyHost = NULL;
l_Hosts = NULL;
l_NumHosts = 0;
/* Initialize some players some */
for (i = 0; i < MAXPLAYERS; i++)
G_InitPlayer(&players[i]);
// In game
memset(playeringame, 0, sizeof(playeringame));
// Reset all variables
P_XGSSetAllDefaults();
/* Destroy the level */
P_ExClearLevel();
/* Common */
SN_CommonDiscStuff();
/* Clear flags */
l_Connected = l_Server = false;
/* Go back to the title screen */
if (!a_FromDemo)
{
gamestate = GS_DEMOSCREEN;
demosequence = -1;
pagetic = -1;
}
/* Done disconnecting */
InDis = false;
}
/* SN_PartialDisconnect() -- Partial Disconnect */
void SN_PartialDisconnect(const char* const a_Reason)
{
static bool_t InDis;
int32_t i;
SN_Host_t* Host;
/* If disconnected already, stop */
if (InDis)
return;
/* Show message */
CONL_OutputUT(CT_NETWORK, DSTR_DNETC_PARTIALDISC, "%s\n", (a_Reason ? a_Reason : "No Reason"));
/* Terminate network connection */
SN_NetTerm(a_Reason);
/* Common */
SN_CommonDiscStuff();
/* Magically become the server */
l_Server = l_Connected = true;
// Cleanup other players
for (i = 0; i < l_NumHosts; i++)
if ((Host = l_Hosts[i]))
if (!Host->Local) // Cleanup non-local players
{
Host->Cleanup = true;
strncpy(Host->QuitReason, "Partial disconnect", MAXQUITREASON);
}
/* Done disconnecting */
InDis = false;
}
/* SN_IsConnected() -- Connected to server */
bool_t SN_IsConnected(void)
{
return l_Connected;
}
/* SN_SetConnected() -- Set connection status */
void SN_SetConnected(const bool_t a_Set)
{
l_Connected = a_Set;
}
/* SN_IsServer() -- Is connected */
bool_t SN_IsServer(void)
{
return l_Server;
}
/* SN_StartWaiting() -- Start waiting */
void SN_StartWaiting(void)
{
/* Wipe from title screen */
if (gamestate == GS_DEMOSCREEN)
wipegamestate = GS_DEMOSCREEN;
/* Change gamestate */
gamestate = GS_WAITINGPLAYERS;
/* Wipe into level? */
// Only wipe from outside (title, etc.)
if (wipegamestate == GS_NULL)
wipegamestate = gamestate;
S_ChangeMusicName("D_WAITIN", 1); // A nice tune
}
/* SN_AddLocalProfiles() -- Adds local profile */
void SN_AddLocalProfiles(const int32_t a_NumLocal, const char** const a_Profs)
{
int32_t i;
const char* ProfN;
/* Check */
if (!a_Profs)
return;
/* Go through names */
for (i = 0; i < a_NumLocal; i++)
{
// Get profile to add from
ProfN = a_Profs[i]; // might be NULL
// Add profile to screens
SN_AddLocalPlayer(ProfN, 0, i, false);
// If first player, do not steal
//if (!i && !ProfN)
g_Splits[i].DoNotSteal = true;
}
}
/* SN_StartServer() -- Starts local server */
bool_t SN_StartServer(const int32_t a_NumLocal, const char** const a_Profs, const bool_t a_JoinPlayers)
{
SN_Host_t* Host;
SN_Port_t* Port;
int32_t i, j;
/* Disconnect first */
SN_Disconnect(false, "Starting server");
/* Set flags */
l_Server = true;
/* Go back to the first gametic */
gametic = 0;
/* Set the proper gamestate */
SN_StartWaiting();
/* Set game settings */
NG_ApplyVars();
/* Add local host player */
l_MyHost = SN_CreateHost();
l_MyHost->ID = D_CMakePureRandom();
l_MyHost->Local = true; // must be local
/* Add local profile players */
SN_AddLocalProfiles(a_NumLocal, a_Profs);
/* Calculate Split-screen */
R_ExecuteSetViewSize();
/* Force all available players to join */
if (a_JoinPlayers)
for (i = 0; i < l_NumHosts; i++)
if ((Host = l_Hosts[i]))
if (Host->Local)
for (j = 0; j < Host->NumPorts; j++)
if ((Port = Host->Ports[j]))
SN_PortTryJoin(Port);
/* Warp to map */
// This is here so auto-joined players are joined before the map
NG_WarpMap();
/* Force run a single tic */
D_RunSingleTic();
/* Created */
return true;
}
/* SN_StartLocalServer() -- Starts local server (just sets connected) */
bool_t SN_StartLocalServer(const int32_t a_NumLocal, const char** const a_Profs, const bool_t a_JoinPlayers, const bool_t a_MakePlayer)
{
int32_t Local;
const char* Profs[MAXSPLITS];
/* Local copy */
// If there are no local players, always make one
// But cannot be done for saves
if (a_MakePlayer)
{
if (a_NumLocal <= 0)
{
if (g_KeyDefaultProfile)
{
Profs[0] = g_KeyDefaultProfile->AccountName;
Local = 1;
}
}
else
for (Local = 0; Local < a_NumLocal; Local++)
if (Local < MAXSPLITS)
Profs[Local] = a_Profs[Local];
}
// Save game (or dedicated local server!?), re-attach ports
else
{
Local = 0;
memset(Profs, 0, sizeof(Profs));
}
/* Normal statr */
if (SN_StartServer(Local, Profs, a_JoinPlayers))
{
l_Connected = true;
return true;
}
/* Failed */
return false;
}
/* SN_ChangeMapCommand() -- Changes the current map */
static int SN_ChangeMapCommand(const uint32_t a_ArgC, const char** const a_ArgV)
{
/* Not enough args or not the server */
if (a_ArgC < 2 || !SN_IsServer() || !l_Connected)
return 1;
/* Set new map */
SN_ChangeMap(a_ArgV[1], !(a_ArgV[0][0] == 'w'));
/* Success */
return 0;
}
/* D_ServerCommand() -- Server Commands */
static int D_ServerCommand(const uint32_t a_ArgC, const char** const a_ArgV)
{
#define BUFSIZE 256
char Buf[BUFSIZE];
int i;
/* Clear */
memset(Buf, 0, sizeof(Buf));
/* (Partial) Disconnect */
if (!strcasecmp(a_ArgV[0], "disconnect") || !strcasecmp(a_ArgV[0], "part"))
{
// Read reason
for (i = 1; i < a_ArgC; i++)
{
strncat(Buf, a_ArgV[1], BUFSIZE - 1);
// Space splitter
if (i < a_ArgC - 1)
strncat(Buf, " ", BUFSIZE - 1);
}
// No reason?
if (!Buf[0])
strncat(Buf, "None given", BUFSIZE - 1);
// Full or partial?
if (a_ArgV[0][0] == 'p')
SN_PartialDisconnect(Buf);
else
SN_Disconnect(false, Buf);
// Worked
return 0;
}
/* Local Game/Server */
else if (!strcasecmp(a_ArgV[0], "localgame") || !strcasecmp(a_ArgV[0], "localserver"))
{
// Start local server, with this kind of stuff
SN_StartLocalServer(a_ArgC - 1, a_ArgV + 1, false, true);
// Worked
return 0;
}
/* Connect to server */
else if (!strcasecmp(a_ArgV[0], "connect"))
{
// Need 1 argument
if (a_ArgC < 2)
return 1;
// Look for --, start of profile data
for (i = 0; i < a_ArgC; i++)
if (!strcmp(a_ArgV[i], "--"))
break;
// There is no --
if (i >= a_ArgC)
i = 0;
// Disconnect from old server first
if (SN_IsConnected() || SN_HasSocket() || demoplayback)
SN_Disconnect(false, "Connecting to another server");
// Add local profiles if specified
if (i > 0)
SN_AddLocalProfiles(a_ArgC - i, a_ArgV + i);
// Create server
SN_NetCreate(false, a_ArgV[1], 0);
// Make waiting for player
SN_StartWaiting();
// Worked
return 0;
}
/* Failed */
return 1;
#undef BUFSIZE
}
/* SN_ServerInit() -- Initializes Server Mode */
bool_t SN_ServerInit(void)
{
#define BUFSIZE 256
char Buf[BUFSIZE];
const char* PProfs[MAXSPLITS];
int32_t np, i;
char* Addr;
uint16_t Port;
bool_t Anti;
/* Add commands */
CONL_AddCommand("disconnect", D_ServerCommand);
CONL_AddCommand("part", D_ServerCommand);
CONL_AddCommand("localgame", D_ServerCommand);
CONL_AddCommand("localserver", D_ServerCommand);
CONL_AddCommand("connect", D_ServerCommand);
// Other commands that might not belong here
CONL_AddCommand("map", SN_ChangeMapCommand);
CONL_AddCommand("warp", SN_ChangeMapCommand);
/* Initialize Multicast */
SN_OpenMCast();
/* Clear initial profiles */
memset(PProfs, 0, sizeof(PProfs));
/* Dedicated server? */
if (M_CheckParm("-dedicated"))
{
l_DedSv = true;
// No players inside
np = 0;
}
// Otherwise, set 1 player
else
{
// Not dedicated
l_DedSv = false;
// Add more players, if they are set
for (np = 0, i = 0; i < MAXSPLITS; i++)
{
// Command to check for (-pX)
snprintf(Buf, BUFSIZE - 1, "-p%i", i + 1);
// See if argument is set
if (M_CheckParm(Buf))
{
if (M_IsNextParm())
PProfs[np++] = M_GetNextParm();
else
PProfs[np++] = NULL; // No name selected
}
// If player 1 missing?
if (i == 0 && !np)
np = 1;
}
}
/* Anti-Connection */
Anti = false;
if (M_CheckParm("-anti"))
Anti = true;
/* Networked or local? */
// Command line local game
if (NG_IsAutoStart())
{
// Initial Command line server?
if (M_CheckParm("-host"))
{
// Start server (but do not join any players)
SN_StartServer(np, PProfs, false);
// Address to bind to, if any
if (M_IsNextParm())
Addr = M_GetNextParm();
else
Addr = NULL;
// Port
if (M_CheckParm("-port") && M_IsNextParm())
Port = C_strtoi32(M_GetNextParm(), NULL, 10);
else
Port = __REMOOD_BASEPORT;
// Create
SN_NetCreate(!Anti, Addr, Port);
// Normal server is always connected
if (!Anti)
l_Connected = true;
}
// Local Game (force joining of players and make local)
else
{
// Use wrapper func
SN_StartLocalServer(np, PProfs, true, true);
}
// Warp to map
NG_WarpMap();
// Successfully started
return true;
}
/* Wanting to connect to remote server */
else if (M_CheckParm("-connect"))
{
// Address to bind to, if any
if (M_IsNextParm())
Addr = M_GetNextParm();
else
Addr = NULL;
// Port
if (M_CheckParm("-port") && M_IsNextParm())
Port = C_strtoi32(M_GetNextParm(), NULL, 10);
else if (Anti)
Port = __REMOOD_BASEPORT;
else
do
{
Port = D_CMakePureRandom();
} while (Port <= 32767 || Port >= 65534);
// Create
SN_AddLocalProfiles(np, PProfs);
SN_NetCreate(Anti, Addr, Port);
// Normal client is never connected
if (Anti)
l_Connected = true;
// Make waiting for player
SN_StartWaiting();
// Started something
NG_SetAutoStart(true);
return true;
}
/* No server started */
return false;
#undef BUFSIZE
}
/*** LOOP ***/
/* SN_UpdateLocalPorts() -- Updates local ports */
void SN_UpdateLocalPorts(void)
{
int32_t i, j, pc;
D_SplitInfo_t* Split;
SN_Port_t* Port;
ticcmd_t* TicCmdP;
char* Profs[1];
/* Do not perform this when not connected */
if (!l_Connected)
return;
/* Go through local screens */
if (!l_DedSv) // Dedicated server only has bots
{
// No players in game
pc = 0;
// Go through all screens
for (i = 0; i < MAXSPLITS; i++)
{
// Does not have player
if (!D_ScrSplitHasPlayer(i))
continue;
// Increase local player count
pc++;
// Quick
Split = &g_Splits[i];
// Screen has no port
if (!Split->Port)
{
// If request not sent
if (!Split->RequestSent)
{
// Sent and give up in 5 sconds
Split->RequestSent = true;
Split->GiveUpAt = g_ProgramTic + (TICRATE * 5);
}
// Request sent
else
{
// Ran out of time?
if (g_ProgramTic >= Split->GiveUpAt)
{
D_NCRemoveSplit(i, false);
i--;
continue;
}
}
// Try to grab a port (xmit once per second)
Split->Port = SN_RequestPort(Split->ProcessID, (g_ProgramTic >= Split->PortTimeOut));
Split->PortTimeOut = g_ProgramTic + TICRATE;
// If grabbed, set local screen ID
if (Split->Port)
Split->Port->Screen = i;
}
// Screen has no profile
else if (!Split->Profile)
{
// Setup selection if not selecting
if (!Split->SelProfile)
{
// If default profile exists, use that (if nobody else is using it)
if (g_KeyDefaultProfile)
{
// Go through other screens
for (j = 0; j < MAXSPLITS; j++)
if (i != j && D_ScrSplitHasPlayer(j))
if (g_Splits[j].Profile == g_KeyDefaultProfile)
break;
// Nobody is using default
if (j >= MAXSPLITS)
Split->Profile = g_KeyDefaultProfile;
}
// Start selecting if no profile found
if (!Split->Profile)
{
Split->SelProfile = true;
Split->AtProf = D_ProfFirst();
}
}
}
// If port has no profile, set it
else if (!Split->Port->Profile)
SN_SetPortProfile(Split->Port, Split->Profile);
}
// No local players in game (always create one)
if (!pc)
{
Profs[0] = NULL;
SN_AddLocalProfiles(1, (const char ** const)Profs);
}
}
/* My host not set? */
if (!l_MyHost && !demoplayback)
for (i = 0; i < l_NumHosts; i++)
if (l_Hosts[i])
if (l_Hosts[i]->Local)
{
l_MyHost = l_Hosts[i];
break;
}
/* Go throgh local ports */
for (i = 0; l_MyHost && i < l_MyHost->NumPorts; i++)
{
// No port here?
if (!(Port = l_MyHost->Ports[i]))
continue;
// If bot, run bot commands
if (Port->Bot)
{
}
// Is a player
else
{
// Get split
Split = NULL;
if (Port->Screen >= 0 && Port->Screen < MAXSPLITS)
Split = &g_Splits[Port->Screen];
// If no profile set, ask for one
if (!Port->Profile)
{
// If split has profile, use that
if (Split->Profile)
SN_SetPortProfile(Port, Split->Profile);
// Otherwise ask
else
{
}
}
// Add tic commands for this port
else
{
// Place tic command at last spot, when possible
TicCmdP = NULL;
if (Port->LocalAt < MAXLBTSIZE - 1)
TicCmdP = &Port->LocalBuf[Port->LocalAt++];
else
TicCmdP = &Port->LocalBuf[0];
// Clear Tic command before rebuilding
memset(TicCmdP, 0, sizeof(*TicCmdP));
// Build tic commands
SN_PortTicCmd(Port, TicCmdP);
}
}
}
}
/* SN_CleanupHost() -- Cleans up host */
bool_t SN_CleanupHost(SN_Host_t* const a_Host)
{
char* s;
uint8_t* Wp;
int32_t At, Cat, i;
const char* Name;
SN_Port_t* Port;
/* Check */
if (!a_Host)
return false;
/* If server, create tic command */
if (l_Server)
{
// Remove players that the host controls
for (i = 0; i < a_Host->NumPorts; i++)
if ((Port = a_Host->Ports[i]))
if (Port->Player)
SN_RemovePlayer(Port->Player - players);
// Reason
for (Cat = 0, Wp = NULL, s = a_Host->QuitReason; *s; s++)
{
// Need more Wp
if (!Wp)
{
if (!SN_ExtCmdInGlobal(DTCT_SNQUITREASON, &Wp))
break; // Could not fit reason
At = 0; // Reset at
// Write ID and cat count
LittleWriteUInt32((uint32_t**)&Wp, a_Host->ID);
LittleWriteUInt32((uint32_t**)&Wp, 0);
WriteUInt8((uint8_t**)&Wp, 0);
WriteUInt8(&Wp, Cat++);
// Cap the cat
if (Cat > 1)
Cat = 1;
}
// Copy Character
Wp[At++] = *s;
// At is near the end
if (At >= MAXTCSTRINGCAT - 2)
Wp = NULL;
}
// Host cleanup
if (SN_ExtCmdInGlobal(DTCT_SNCLEANUPHOST, &Wp))
{
LittleWriteUInt32((uint32_t**)&Wp, a_Host->ID);
LittleWriteUInt32((uint32_t**)&Wp, 0);
WriteUInt8((uint8_t**)&Wp, 0);
}
}
/* Message */
// Figure out name
Name = "Client";
// Show message
CONL_OutputUT(CT_NETWORK, DSTR_NET_CLIENTGONE, "%s%s\n", Name, a_Host->QuitReason);
// Delete their host
SN_DestroyHost(a_Host);
return true;
}
/* SN_Update() -- Updates network state */
void SN_Update(void)
{
int32_t h, p;
SN_Host_t* Host;
SN_Port_t* Port;
/* Multicast */
SN_DoMultiCast();
/* Do not update ports or transport during demo */
if (!demoplayback)
{
// Update ports
SN_UpdateLocalPorts();
// HTTP Interface
I_UpdateHTTPSpy();
// Network Socket
SN_DoTrans();
}
/* Go through all hosts and ports */
for (h = 0; h < l_NumHosts; h++)
if ((Host = l_Hosts[h]))
{
// Cleaning up?
if (Host->Cleanup)
if (SN_CleanupHost(Host))
continue;
// Server only
if (l_Server)
{
// Handle all ports
for (p = 0; p < Host->NumPorts; p++)
if ((Port = Host->Ports[p]))
{
// If will join is set, try joining this port
if (Port->WillJoin)
SN_PortTryJoin(Port);
}
}
}
}
/*** HOST CONTROL ***/
/* SN_HostByAddr() -- Finds host by address */
SN_Host_t* SN_HostByAddr(const I_HostAddress_t* const a_Host)
{
int32_t i;
SN_Host_t* Host;
/* Check */
if (!a_Host)
return NULL;
/* Through host list */
for (i = 0; i < l_NumHosts; i++)
if ((Host = l_Hosts[i]))
if (I_NetCompareHost(&Host->Addr, a_Host))
return Host;
/* Not found */
return NULL;
}
/* SN_HostByID() -- Finds host by ID */
SN_Host_t* SN_HostByID(const uint32_t a_ID)
{
int32_t i;
SN_Host_t* Host;
/* Through host list */
for (i = 0; i < l_NumHosts; i++)
if ((Host = l_Hosts[i]))
if (Host->ID == a_ID)
return Host;
/* Not found */
return NULL;
}
/* SN_MyHost() -- Return your host */
SN_Host_t* SN_MyHost(void)
{
return l_MyHost;
}
/* SN_SetMyHost() -- Sets your current host */
void SN_SetMyHost(SN_Host_t* const a_Host)
{
l_MyHost = a_Host;
}
/* SN_CreateHost() -- Creates new host */
SN_Host_t* SN_CreateHost(void)
{
SN_Host_t* New;
int32_t i;
/* Allocate new */
New = Z_Malloc(sizeof(*New), PU_STATIC, NULL);
/* Initialize */
New->Save.Slot = -1;
/* Place into list */
for (i = 0; i < l_NumHosts; i++)
if (!l_Hosts[i])
return (l_Hosts[i] = New);
/* Need to resize */
Z_ResizeArray((void**)&l_Hosts, sizeof(*l_Hosts), l_NumHosts, l_NumHosts + 1);
return (l_Hosts[l_NumHosts++] = New);
}
/* SN_DestroyHost() -- Destroys host */
void SN_DestroyHost(SN_Host_t* const a_Host)
{
int32_t i;
/* Check */
if (!a_Host)
return;
/* Send disconnect */
SN_DisconnectHost(a_Host, NULL);
/* Remove from list */
for (i = 0; i < l_NumHosts; i++)
if (l_Hosts[i] == a_Host)
l_Hosts[i] = NULL;
// This is our host?
if (a_Host == l_MyHost)
l_MyHost = NULL;
/* Cleanup host */
// Clear ports
if (a_Host->Ports)
{
for (i = 0; i < a_Host->NumPorts; i++)
if (a_Host->Ports[i])
SN_RemovePort(a_Host->Ports[i]);
Z_Free(a_Host->Ports);
}
// Free
Z_Free(a_Host);
}
/*** PORT CONTROL ***/
/* SN_PortByID() -- Finds port by ID */
SN_Port_t* SN_PortByID(const uint32_t a_ID)
{
int32_t i, j;
SN_Host_t* Host;
SN_Port_t* Port;
/* Through host list */
for (i = 0; i < l_NumHosts; i++)
if ((Host = l_Hosts[i]))
for (j = 0; j < Host->NumPorts; j++)
if ((Port = Host->Ports[j]))
if (Port->ID == a_ID)
return Port;
/* Not found */
return NULL;
}
/* SN_AddPort() -- Add port to host */
SN_Port_t* SN_AddPort(SN_Host_t* const a_Host)
{
SN_Port_t* New;
int32_t i;
/* Check */
if (!a_Host)
return NULL;
/* Allocate */
New = Z_Malloc(sizeof(*New), PU_STATIC, NULL);
// Add to list
for (i = 0; i < a_Host->NumPorts; i++)
if (!a_Host->Ports[i])
{
a_Host->Ports[i] = New;
break;
}
// Need to resize
if (i >= a_Host->NumPorts)
{
Z_ResizeArray((void**)&a_Host->Ports, sizeof(*a_Host->Ports), a_Host->NumPorts, a_Host->NumPorts + 1);
a_Host->Ports[a_Host->NumPorts++] = New;
}
/* Set host */
New->Host = a_Host;
/* Return fresh port */
return New;
}
/* SN_RemovePort() -- Removes port */
void SN_RemovePort(SN_Port_t* const a_Port)
{
SN_Host_t* Host;
int32_t i;
uint8_t* Wp;
/* Check */
if (!a_Port)
return;
/* Get host */
Host = a_Port->Host;
/* Remove from host list */
for (i = 0; i < Host->NumPorts; i++)
if (Host->Ports[i] == a_Port)
{
Host->Ports[i] = NULL;
break;
}
// Port already removed (might be from a bot call)
if (i >= Host->NumPorts)
return;
/* Before removing port, place in command to let others know */
// But only as the server
if (l_Server)
if (SN_ExtCmdInGlobal(DTCT_SNUNPLUGPORT, &Wp))
{
LittleWriteUInt32((uint32_t**)&Wp, a_Port->Host->ID);
LittleWriteUInt32((uint32_t**)&Wp, a_Port->ID);
WriteUInt8((uint8_t**)&Wp, 0);
}
/* Remove bot (if any) */
// DBP is recursive on the server
BOT_DestroyByPort(a_Port);
/* Remove references by screen and player */
// Player
for (i = 0; i < MAXPLAYERS; i++)
if (players[i].Port == a_Port)
players[i].Port = NULL;
// Screen
for (i = 0; i < MAXSPLITS; i++)
if (D_ScrSplitVisible(i))
if (g_Splits[i].Port == a_Port)
{
D_NCRemoveSplit(i, false);
i = -1;
}
/* Free */
Z_Free(a_Port);
}
/* SN_UnplugPort() -- Unplugs port */
void SN_UnplugPort(SN_Port_t* const a_Port)
{
/* Check */
if (!a_Port)
return;
/* If Server, just remove */
if (l_Server)
{
// If port is playing, kill player that is controlled by it
if (a_Port->Player)
SN_RemovePlayer(a_Port->Player - players);
SN_RemovePort(a_Port);
}
/* Otherwise, tell server to remove */
else
SN_UnplugPortNet(a_Port);
}
/* SN_RequestPort() -- Requests port from server */
// a_XMit is used by the client when a transmission request should be made
// Otherwise, there will be an ugly 1 second delay ALL THE TIME when adding new
// local players. So if the server happens to send you a port in the middle of
// your timeout, then you still grab it.
SN_Port_t* SN_RequestPort(const uint32_t a_ProcessID, const bool_t a_XMit)
{
int32_t i;
SN_Port_t* Port, *Best, *PM;
uint32_t ID;
/* No local host */
if (!l_MyHost)
return NULL;
/* Go through and see if any ports are not used */
Best = PM = NULL;
for (i = 0; i < l_MyHost->NumPorts; i++)
{
// See if port is in this spot
if (!(Port = l_MyHost->Ports[i]))
continue;
// ProcessID match
if (a_ProcessID && Port->ProcessID == a_ProcessID)
PM = Port;
// If not a bot and is free, use this one
if (Port->Screen < 0 && !Port->Bot)
Best = Port;
}
// If no process ID is set and there is a best, use that
if (Best && !a_ProcessID)
return Best;
// Check for a process ID match
else if (PM && a_ProcessID)
return PM;
/* In network situation, ask for one */
// If server, can just create our own port
if (l_Server)
{
// Add port
Port = SN_AddPort(l_MyHost);
// Set port ID
do
{
ID = D_CMakePureRandom();
} while (!ID || SN_PortByID(ID) || SN_HostByID(ID));
Port->ID = ID;
// Set process ID (if any)
Port->ProcessID = a_ProcessID;
// Return it
return Port;
}
// Otherwise, need to send some packets
else if (l_Connected)
{
// Only go over the net if required
if (a_XMit)
SN_RequestPortNet(a_ProcessID);
}
/* No port found, yet */
return NULL;
}
/* SN_AddLocalPlayer() -- Adds local player to game */
bool_t SN_AddLocalPlayer(const char* const a_Name, const uint32_t a_JoyID, const int8_t a_ScreenID, const bool_t a_UseJoy)
{
uint32_t LastScreen;
int32_t PlaceAt, UngrabbedScreen;
D_Prof_t* Profile;
bool_t BumpSplits;
D_SplitInfo_t* Split;
/* Check */
if (a_ScreenID < 0 || a_ScreenID >= MAXSPLITS)
return false;
// Find Profile
Profile = NULL;
if (a_Name)
Profile = D_FindProfileEx(a_Name);
/* Find first free slot */
// Find the last screened player
UngrabbedScreen = -1;
for (LastScreen = 0; LastScreen < MAXSPLITS; LastScreen++)
{
if (!D_ScrSplitHasPlayer(LastScreen))
break;
// If the split has no profile and profile IS being added, take over
if (UngrabbedScreen == -1)
if (!g_Splits[LastScreen].Profile && !g_Splits[LastScreen].DoNotSteal)
UngrabbedScreen = LastScreen;
}
// Place at the first wanted spot, unless already bound
// Assign joystick to player, can be in game
if (a_UseJoy && D_ScrSplitHasPlayer(a_ScreenID) && !g_Splits[a_ScreenID].JoyBound)
PlaceAt = a_ScreenID;
// No joystick wanted
else
{
// Cannot fit any more players
if (LastScreen >= MAXSPLITS)
return false;
if (UngrabbedScreen != -1)
PlaceAt = UngrabbedScreen;
else
PlaceAt = LastScreen;
}
/* If not a bot, bind to a local screen */
// Bump splits? Not if a screen has a player (controlling keyboarder)
BumpSplits = true;
if (D_ScrSplitHasPlayer(PlaceAt))
BumpSplits = false;
// Pointer
Split = &g_Splits[PlaceAt];
// Never redisplay
// Also if a player is not active, then reset the display status
if (!demoplayback)
if (!Split->Active)
{
Split->Console = -1;
Split->Display = -1;
}
Split->RequestSent = false;
Split->GiveUpAt = 0;
// Bind stuff here
Split->Waiting = true;
Split->Profile = Profile;
Split->JoyBound = a_UseJoy;
Split->JoyID = a_JoyID;
// Generate unique process ID
do
{
Split->ProcessID = D_CMakePureRandom();
} while (!Split->ProcessID && D_NCSFindSplitByProcess(Split->ProcessID) != PlaceAt);
// Resize Splits
if (BumpSplits)
if (!demoplayback)
{
if (PlaceAt >= g_SplitScreen)
g_SplitScreen = ((int)PlaceAt);// - 1;
R_ExecuteSetViewSize();
}
/* If server, add local port */
if (l_Server && !Split->Port)
{
// Grab a port, only transmitting every second
Split->Port = SN_RequestPort(Split->ProcessID, (g_ProgramTic >= Split->PortTimeOut));
Split->PortTimeOut = g_ProgramTic + TICRATE;
// If grabbed, set local screen ID
if (Split->Port)
{
Split->Port->Screen = PlaceAt;
Split->Port->ProcessID = Split->ProcessID;
SN_SetPortProfile(Split->Port, Profile);
}
}
/* Added OK */
return true;
}
/* SN_BufForGameTic() -- Returns buffer for gametic */
SN_TicBuf_t* SN_BufForGameTic(const tic_t a_GameTic)
{
/* In the past */
if (a_GameTic < gametic)
return NULL;
/* Too far into the future */
if (a_GameTic >= gametic + MAXNETXTICS)
return NULL;
/* Return reference of local buffer */
return &l_LocalBuf[a_GameTic - gametic];
}
/* SN_StartTic() -- Start of a new tic */
void SN_StartTic(const tic_t a_GameTic)
{
SN_TicBuf_t* Now = &l_NowTic[l_NowPress];
/* Start of a new tic */
// Clear the current press
memset(Now, 0, sizeof(*Now));
/* Write gametic here */
Now->GameTic = a_GameTic;
}
/* SN_NumSeqTics() -- Returns number of sequential local tics */
int32_t SN_NumSeqTics(void)
{
int32_t RetVal;
/* Count */
for (RetVal = 0; RetVal < MAXNETXTICS; RetVal++)
if (!l_LocalBuf[RetVal].GotTic)
break; // No more tics received
/* Return count */
return RetVal;
}
/* SN_LocalTurn() -- Perform local turning */
void SN_LocalTurn(SN_Port_t* const a_Port, ticcmd_t* const a_TicCmd)
{
int32_t h;
// Handle local angles, if a local player
if (a_Port->Host && a_Port->Host->Local)
{
// Find screen number
for (h = 0; h < MAXSPLITS; h++)
if (D_ScrSplitHasPlayer(h))
if (g_Splits[h].Port == a_Port)
break;
if (h < MAXSPLITS)
{
// Absolute Angles
if (P_XGSVal(PGS_COABSOLUTEANGLE))
{
localangle[h] += a_TicCmd->Std.BaseAngleTurn << 16;
a_TicCmd->Std.angleturn = localangle[h] >> 16;
}
// Doom Angles
else
a_TicCmd->Std.angleturn = a_TicCmd->Std.BaseAngleTurn;
// Aiming Angle
if (a_TicCmd->Std.buttons & BT_RESETAIM)
localaiming[h] = 0;
else
{
// Panning Look
if (a_TicCmd->Std.buttons & BT_PANLOOK)
localaiming[h] = a_TicCmd->Std.BaseAiming << 16;
// Standard Look
else
localaiming[h] += a_TicCmd->Std.BaseAiming << 16;
}
// Clip aiming pitch to not exceed bounds
a_TicCmd->Std.aiming = G_ClipAimingPitch(&localaiming[h]);
}
}
}
/* SN_Tics() -- Handles tic commands */
void SN_Tics(ticcmd_t* const a_TicCmd, const bool_t a_Write, const int32_t a_Player)
{
SN_Host_t* Host;
SN_Port_t* Port;
int32_t h, p;
SN_TicBuf_t* Now = &l_NowTic[l_NowPress];
/* If not writing, clear tic command */
if (!a_Write)
memset(a_TicCmd, 0, sizeof(a_TicCmd));
/* If server, we dictate commands */
if (l_Server)
{
// Write Commands to clients
if (a_Write)
{
// Save tic command in the now press
p = (a_Player < 0 ? MAXPLAYERS : a_Player);
memmove(&Now->Tics[p], a_TicCmd, sizeof(ticcmd_t));
}
// Read command from tic queues
else
{
// Global
if (a_Player < 0)
{
// Something is in the buffer
if (l_GlobalAt >= 0)
{
// Use first global command
memmove(a_TicCmd, l_GlobalBuf, sizeof(*a_TicCmd));
// Move down other entries
for (p = 1; p <= l_GlobalAt; p++)
l_GlobalBuf[p - 1] = l_GlobalBuf[p];
memset(&l_GlobalBuf[l_GlobalAt], 0, sizeof(l_GlobalBuf[l_GlobalAt]));
// Reduce count
l_GlobalAt--;
}
// Nothing
else
memset(a_TicCmd, 0, sizeof(*a_TicCmd));
}
// Player
else
{
// Get port for this player
if (!(Port = players[a_Player].Port))
{
// Slower code?
for (h = 0; h < l_NumHosts; h++)
if ((Host = l_Hosts[h]))
for (p = 0; p < Host->NumPorts; p++)
if (Host->Ports[p] && Host->Ports[p]->Player == &players[a_Player])
{
Port = Host->Ports[p];
break;
}
if (!Port)
return; // oops!
}
// Merge all the local stuff
if (Port->LocalAt > 0)
{
D_XNetMergeTics(a_TicCmd, Port->LocalBuf, Port->LocalAt);
Port->LocalAt = 0;
memset(Port->LocalBuf, 0, sizeof(Port->LocalBuf));
// Store this tic as backup
memmove(&Port->BackupCmd, a_TicCmd, sizeof(Port->BackupCmd));
// Not lagging
Port->LocalStatFlags &= ~DTSF_LOCALSTICK;
}
// Missed tic generation (use backup tic)
else
{
memmove(a_TicCmd, &Port->BackupCmd, sizeof(Port->BackupCmd));
// Lagging
Port->LocalStatFlags |= DTSF_LOCALSTICK;
}
// Set ping from host
a_TicCmd->Ctrl.Ping = Port->Host->Ping & TICPINGAMOUNTMASK;
// Copy status flags from port
if (!a_TicCmd->Ctrl.Type)
a_TicCmd->Std.StatFlags = Port->StatFlags;
// Do local turning and aiming
SN_LocalTurn(Port, a_TicCmd);
}
}
}
/* Otherwise, server dictates to us */
else
{
// Client cannot write commands
if (a_Write)
return;
// Tic never received
if (!l_LocalBuf[0].GotTic)
return;
// Copy tic owner
memmove(a_TicCmd, &l_LocalBuf[0].Tics[(a_Player < 0 ? MAXPLAYERS : a_Player)], sizeof(*a_TicCmd));
}
}
/* SN_SyncCode() -- Inputs sync code */
void SN_SyncCode(const tic_t a_GameTic, const uint32_t a_Code)
{
SN_TicBuf_t* Now = &l_NowTic[l_NowPress];
/* If not connected, do not care */
if (!l_Connected)
return;
/* Time of last tic being ran */
SN_SetLastTic();
/* Server */
if (l_Server)
{
// Invert press
l_NowPress = !l_NowPress;
// Encode sync code in this buffer
Now->GameTic = a_GameTic;
Now->SyncCode = a_Code;
// Tic can now be sent to clients
SN_XMitTics(a_GameTic, Now);
// Round robin place
l_SyncCodeRR[l_SyncRRAt].GameTic = a_GameTic;
l_SyncCodeRR[l_SyncRRAt].Code = a_Code;
l_SyncRRAt = (l_SyncRRAt + 1) % MAXRRSYNCSIZE;
}
/* Client */
else
{
// Send sync code to server
SN_SendSyncCode(a_GameTic, a_Code);
// Move down all the local tics
memmove(&l_LocalBuf[0], &l_LocalBuf[1], sizeof(l_LocalBuf[0]) * (MAXNETXTICS - 1));
// Clear last local tic
memset(&l_LocalBuf[MAXNETXTICS - 1], 0, sizeof(l_LocalBuf[0]));
}
}
/* SN_CheckSyncCode() -- Checks sync code from client */
void SN_CheckSyncCode(SN_Host_t* const a_Host, const tic_t a_GameTic, const uint32_t a_Code)
{
register int i;
/* Check */
if (!a_GameTic)
return;
/* Look in loop */
for (i = 0; i < MAXRRSYNCSIZE; i++)
if (l_SyncCodeRR[i].GameTic == a_GameTic)
{
// Synced
if (l_SyncCodeRR[i].Code == a_Code)
return;
// Kick host
SN_DisconnectHost(a_Host, "Game desynchronized");
// Done anyway
return;
}
}
/* SN_SetPortProfile() -- Set port profile */
void SN_SetPortProfile(SN_Port_t* const a_Port, D_Prof_t* const a_Profile)
{
/* Check */
if (!a_Port)
return;
/* Set it locally */
a_Port->Profile = a_Profile;
// Clone to screen
if (a_Port->Screen >= 0 && a_Port->Screen < MAXSPLITS)
g_Splits[a_Port->Screen].Profile = a_Profile;
// No profile
if (!a_Profile)
return;
/* Broadcast information to everyone else */
// This also sets in game details, if playing, etc.
SN_PortSetting(a_Port, DSNPS_NAME, 0, a_Profile->DisplayName, 0);
SN_PortSetting(a_Port, DSNPS_VTEAM, a_Profile->VTeam, NULL, 0);
SN_PortSetting(a_Port, DSNPS_COLOR, a_Profile->Color, NULL, 0);
SN_PortSetting(a_Port, DSNPS_COUNTEROP, a_Profile->CounterOp, NULL, 0);
}
/* SN_PortRequestJoin() -- Request join on the server */
void SN_PortRequestJoin(SN_Port_t* const a_Port)
{
int32_t ActN, HypoN, LocN;
int32_t h, p;
SN_Host_t* Host;
SN_Port_t* Port;
/* Check */
if (!a_Port)
return;
/* Go through other ports to count players inside */
ActN = HypoN = 0;
for (h = 0; h < l_NumHosts; h++)
if ((Host = l_Hosts[h]))
for (p = 0; p < Host->NumPorts; p++)
if ((Port = Host->Ports[p]))
{
if (!Port->Player && Port->WillJoin)
HypoN++;
if (Port->Player)
ActN++;
}
// Add actual to hypo
HypoN += ActN;
/* Perform join */
// If server, does not exactly care
if (l_Server)
{
// No Room
if (ActN >= MAXPLAYERS)
return;
// If port is bot, always allow
if (a_Port->Bot)
a_Port->WillJoin = true;
// Otherwise, only allow if split screens are free
Host = l_MyHost;
LocN = 0;
for (p = 0; p < Host->NumPorts; p++)
if ((Port = Host->Ports[p]))
if (!Port->Bot && Port->Player)
LocN++;
// There is room
if (LocN < MAXSPLITS)
a_Port->WillJoin = true;
}
// Client must request it
else
{
// Do not spam server with join requests though
if (g_ProgramTic > a_Port->JoinWait)
{
SN_PortJoinGame(a_Port);
a_Port->JoinWait = g_ProgramTic + (TICRATE >> 1);
}
}
}
/* SN_PortTryJoin() -- Try to join port game */
void SN_PortTryJoin(SN_Port_t* const a_Port)
{
int32_t h, p;
SN_Host_t* Host;
SN_Port_t* Port;
SN_Port_t* PortMap[MAXPLAYERS];
uint8_t* Wp;
uint32_t Flags;
/* Only server can do this */
if (!l_Server)
return;
/* Check */
if (!a_Port)
return;
/* Do not join if already playing */
if (a_Port->Player)
return;
/* Unmark will join */
a_Port->WillJoin = false;
/* Go through other ports and map players */
// Clear
memset(PortMap, 0, sizeof(PortMap));
// Loop
for (h = 0; h < l_NumHosts; h++)
if ((Host = l_Hosts[h]))
for (p = 0; p < Host->NumPorts; p++)
if ((Port = Host->Ports[p]))
if (Port->Player)
PortMap[Port->Player - players] = Port;
/* Find free player spot */
for (p = 0; p < MAXPLAYERS; p++)
if (!PortMap[p] && !playeringame[p])
break;
// No room
if (p >= MAXPLAYERS)
return;
// Go back to port
Port = a_Port;
// If Host was never set, set
if (!Host)
Host = Port->Host;
/* Make this port own this player now */
Port->Player = &players[p];
/* Create join packet */
if (!SN_ExtCmdInGlobal(DTCT_SNJOINPLAYER, &Wp))
{
Port->Player = NULL; // whoops!!
return;
}
// Flags
Flags = 0;
if (Port->CounterOp)
Flags |= DTCJF_MONSTERTEAM;
// Fill
LittleWriteUInt32((uint32_t**)&Wp, Host->ID);
LittleWriteUInt32((uint32_t**)&Wp, Port->ID);
WriteUInt8((uint8_t**)&Wp, p);
WriteUInt8((uint8_t**)&Wp, Port->VTeam);
WriteUInt8((uint8_t**)&Wp, Port->Color);
LittleWriteUInt32((uint32_t**)&Wp, Flags);
}
/* SN_GetPortName() -- Get name of port */
const char* SN_GetPortName(SN_Port_t* const a_Port)
{
/* No port passed */
if (!a_Port)
return "Illegal Name";
/* Return correct name */
else if (a_Port->Player)
return player_names[a_Port->Player - players];
/* Do not know */
else
return "Player";
}
/* SN_PortSettingInt() -- Sets integer value of setting */
void SN_PortSetting(SN_Port_t* const a_Port, const SN_PortSetting_t a_Setting, const int32_t a_IntVal, const char* const a_StrVal, const uint32_t a_StrLen)
{
uint8_t* Wp;
int32_t i;
const char* p;
/* Check */
if (!a_Port || a_Setting < 0 || a_Setting >= NUMDSNPS)
return;
/* Server */
// Affect settings directly
if (SN_IsServer())
{
// Take gloal
if (!SN_ExtCmdInGlobal(DTCT_SNPORTSETTING, &Wp))
{
CONL_PrintF("missed global %i\n", l_GlobalAt);
return;
}
// Write Data
LittleWriteUInt32((uint32_t**)&Wp, a_Port->Host->ID);
LittleWriteUInt32((uint32_t**)&Wp, a_Port->ID);
WriteUInt8(&Wp, (a_Port->Player ? a_Port->Player - players : 255));
LittleWriteUInt16((uint16_t**)&Wp, a_Setting);
// String?
if (a_StrVal)
{
// No length specified
if (!a_StrLen)
for (p = a_StrVal, i = 0; i < MAXTCSTRINGCAT; i++)
{
// Write
WriteUInt8(&Wp, *p);
// Increase p as long as it is not NULL
if (*p)
p++;
}
// Length Specified
else
for (i = 0; i < MAXTCSTRINGCAT; i++)
if (i < a_StrLen)
WriteUInt8(&Wp, ((uint8_t*)a_StrVal)[i]);
else
WriteUInt8(&Wp, 0);
}
// Integer
else
{
LittleWriteInt32((int32_t**)&Wp, a_IntVal);
// Padding
for (i = 4; i < MAXTCSTRINGCAT; i++)
WriteUInt8(&Wp, 0);
}
}
/* Client */
// Send to server
else
{
SN_SendSettings(a_Port, a_Setting, a_IntVal, a_StrVal, a_StrLen);
}
/* If this is a local port, change settings internally for this port */
if (a_Port->Host->Local)
SN_PortSettingOnPort(a_Port, a_Setting, a_IntVal, a_StrVal, a_StrLen);
}
/* SN_PortSettingOnPort() -- Change setting on a port */
void SN_PortSettingOnPort(SN_Port_t* const a_Port, const SN_PortSetting_t a_Setting, const int32_t a_IntVal, const char* const a_StrVal, const uint32_t a_StrLen)
{
/* Check */
if (!a_Port || a_Setting < 0 || a_Setting >= NUMDSNPS)
return;
/* Based on setting passed */
switch (a_Setting)
{
case DSNPS_VTEAM: a_Port->VTeam = a_IntVal; break;
case DSNPS_COLOR: a_Port->Color = a_IntVal; break;
case DSNPS_COUNTEROP: a_Port->CounterOp = !!a_IntVal; break;
default: break;
}
/* String based settings (to prevent crashes) */
if (a_StrVal)
switch (a_Setting)
{
case DSNPS_NAME:
strncpy(a_Port->Name, a_StrVal, MAXPLAYERNAME);
a_Port->Name[MAXPLAYERNAME - 1] = 0;
break;
default: break;
}
}