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
|
#include "c_defs.h"
/************************************************************************
*
* server specific stuff
*
* $Id: server.c,v 1.21 2004/12/26 08:19:31 jon Exp $
*
* Copyright 2003 Jon Trulson under the ARTISTIC LICENSE. (See LICENSE).
***********************************************************************/
#include "conqdef.h"
#include "conqcom.h"
#include "conqlb.h"
#include "conf.h"
#include "server.h"
#include "serverpkt.h"
#include "context.h"
#include "record.h"
#include "conqlb.h"
#define MAXUDPERRS (15)
static int sudperrs = 0; /* keep track of udp write errors */
#if 0
static string guilt="Team genocided. Any armies you were carrying died of guilt.";
#endif
static string guilt="Your team was genocided.";
/* called when a write to a UDP socket fails. If the error threshold
is exceeded, disable UDP to client and let them know */
static void handleUDPErr(void)
{
sudperrs++;
if (sudperrs > MAXUDPERRS)
{
sInfo.tryUDP = FALSE;
sInfo.doUDP = FALSE;
close(sInfo.usock);
sInfo.usock = -1;
clog("NET: too many UDP send errors to client, switching to TCP");
clbStoreMsg(MSG_COMP, Context.snum,
"SERVER: too many UDP send errors. Switching to TCP");
}
return;
}
int sendClientStat(int sock, Unsgn8 flags, Unsgn8 snum, Unsgn8 team,
Unsgn16 unum, Unsgn8 esystem)
{
spClientStat_t scstat;
memset((void *)&scstat, 0, sizeof(spClientStat_t));
#if defined(DEBUG_SERVERCLNTSTAT)
clog("sendClientStat: snum = %d, unum = %d, flags = 0x%x, team = %d",
snum, unum, flags, team);
#endif
scstat.type = SP_CLIENTSTAT;
scstat.flags = flags;
scstat.snum = snum;
scstat.team = team;
scstat.unum = htons(unum);
scstat.esystem = esystem;
if (writePacket(PKT_TOCLIENT, sock, (Unsgn8 *)&scstat) <= 0)
return FALSE;
else
return TRUE;
}
int sendUser(int sock, Unsgn16 unum)
{
spUser_t *suser;
#if defined(DEBUG_SERVERSEND)
clog("sendUser: unum = %d",
unum);
#endif
/* not really any priv bits, but in case we add some later */
if ((suser = spktUser(unum)))
{
if (writePacket(PKT_TOCLIENT, sock, (Unsgn8 *)suser) <= 0)
return FALSE;
if (Context.recmode == RECMODE_ON)
recordWriteEvent((Unsgn8 *)suser);
}
return TRUE;
}
/* we have the potential to send a few packets here. */
int sendShip(int sock, Unsgn8 snum)
{
spShip_t *sship;
spShipSml_t *sshipsml;
spShipLoc_t *sshiploc;
#if defined(DEBUG_SERVERSEND)
clog("sendShip: snum = %d",
snum);
#endif
/* SP_SHIP */
if (Context.recmode == RECMODE_ON)
{
if ((sship = spktShip(snum, TRUE)))
recordWriteEvent((Unsgn8 *)sship);
}
if ((sship = spktShip(snum, FALSE)))
if (writePacket(PKT_TOCLIENT, sock, (Unsgn8 *)sship) <= 0)
return FALSE;
/* SP_SHIPSML */
if (Context.recmode == RECMODE_ON)
{
if ((sshipsml = spktShipSml(snum, TRUE)))
recordWriteEvent((Unsgn8 *)sshipsml);
}
if ((sshipsml = spktShipSml(snum, FALSE)))
if (writePacket(PKT_TOCLIENT, sock, (Unsgn8 *)sshipsml) <= 0)
return FALSE;
/* SP_SHIPLOC */
if (Context.recmode == RECMODE_ON)
{
if ((sshiploc = spktShipLoc(snum, TRUE)))
recordWriteEvent((Unsgn8 *)sshiploc);
}
if ((sshiploc = spktShipLoc(snum, FALSE)))
{
if (sInfo.doUDP)
{
if (writePacket(PKT_TOCLIENT, sInfo.usock, (Unsgn8 *)sshiploc) <= 0)
{
handleUDPErr();
return FALSE;
}
}
else
{
if (writePacket(PKT_TOCLIENT, sock, (Unsgn8 *)sshiploc) <= 0)
return FALSE;
}
}
return TRUE;
}
/* we have the potential to send 3 packets here. */
int sendPlanet(int sock, Unsgn8 pnum, int force)
{
spPlanet_t *splan;
spPlanetSml_t *splansml;
spPlanetLoc_t *splanloc;
spPlanetLoc2_t *splanloc2;
spPlanetInfo_t *splaninfo;
#if defined(DEBUG_SERVERSEND)
clog("sendPlanet: pnum = %d",
pnum);
#endif
/* SP_PLANET */
if (Context.recmode == RECMODE_ON)
{
if ((splan = spktPlanet(pnum, TRUE)))
recordWriteEvent((Unsgn8 *)splan);
}
if ((splan = spktPlanet(pnum, FALSE)))
if (writePacket(PKT_TOCLIENT, sock, (Unsgn8 *)splan) <= 0)
return FALSE;
/* SP_PLANETSML */
if (Context.recmode == RECMODE_ON)
{
if ((splansml = spktPlanetSml(pnum, TRUE)))
recordWriteEvent((Unsgn8 *)splansml);
}
if ((splansml = spktPlanetSml(pnum, FALSE)))
if (writePacket(PKT_TOCLIENT, sock, (Unsgn8 *)splansml) <= 0)
return FALSE;
/* SP_PLANETINFO */
if (Context.recmode == RECMODE_ON)
{
if ((splaninfo = spktPlanetInfo(pnum, TRUE)))
recordWriteEvent((Unsgn8 *)splaninfo);
}
if ((splaninfo = spktPlanetInfo(pnum, FALSE)))
if (writePacket(PKT_TOCLIENT, sock, (Unsgn8 *)splaninfo) <= 0)
return FALSE;
/* we will do loc packets for recording purposes only. loc2 is sent
to clients, but not recorded (server-side), since the clients
compute their own planetary movement based on them. */
/* SP_PLANETLOC */
if (Context.recmode == RECMODE_ON)
{
if ((splanloc = spktPlanetLoc(pnum, TRUE, force)))
recordWriteEvent((Unsgn8 *)splanloc);
}
/* SP_PLANETLOC2 */
if ((splanloc2 = spktPlanetLoc2(pnum, FALSE, force)))
if (writePacket(PKT_TOCLIENT, sock, (Unsgn8 *)splanloc2) <= 0)
return FALSE;
return TRUE;
}
/* send a status packet */
int sendServerStat(int sock)
{
int i;
int numusers = 0;
int numships = 0;
int numshipsactive = 0;
int numshipsvacant = 0;
int numshipsrobot = 0;
spServerStat_t sStat;
memset((void *)&sStat, 0, sizeof(spServerStat_t));
#if defined(DEBUG_SERVERSEND)
clog("sendServerStats: ENTER");
#endif
/* get total users */
for ( i = 0; i < MAXUSERS; i++)
if ( Users[i].live)
numusers++;
/* count ships */
for ( i = 1; i <= MAXSHIPS; i++ )
{
if ( Ships[i].status == SS_LIVE )
{
numships++;
if (SROBOT(i))
numshipsrobot++;
else
{
if (SVACANT(i))
numshipsvacant++;
else
numshipsactive++;
}
}
}
sStat.type = SP_SERVERSTAT;
sStat.numactive = numshipsactive;
sStat.numvacant = numshipsvacant;
sStat.numrobot = numshipsrobot;
sStat.numtotal = numships;
sStat.numusers = htons(numusers);
/* send out the current flags */
sStat.flags = getServerFlags();
sStat.flags = (Unsgn32)htonl(sStat.flags);
sStat.servertime = (Unsgn32)htonl(getnow(NULL, 0));
if (!writePacket(PKT_TOCLIENT, sock, (Unsgn8 *)&sStat))
{
clog("sendServerStats: writePacket failed\n");
return FALSE;
}
return TRUE;
}
int sendTorp(int sock, Unsgn8 tsnum, Unsgn8 tnum)
{
spTorp_t *storp;
spTorpLoc_t *storploc;
spTorpEvent_t *storpev;
/* no point in sending torp data if we're not playing */
if (sInfo.state != SVR_STATE_PLAY)
return TRUE;
if (tsnum <= 0 || tsnum > MAXSHIPS)
return FALSE;
if (tnum >= MAXTORPS)
return FALSE;
#if defined(DEBUG_SERVERSEND)
clog("sendTorp: %d %d", tsnum, tnum);
#endif
/* SP_TORPEVENT */
/* we only send these */
if ((storpev = spktTorpEvent(tsnum, tnum, FALSE)))
{
if (writePacket(PKT_TOCLIENT, sock, (Unsgn8 *)storpev) <= 0)
return FALSE;
}
/* SP_TORP */
/* we only record these */
if (Context.recmode == RECMODE_ON)
{
if ((storp = spktTorp(tsnum, tnum, TRUE)))
recordWriteEvent((Unsgn8 *)storp);
}
/* SP_TORPLOC */
/* we only record these */
if (Context.recmode == RECMODE_ON)
{
if ((storploc = spktTorpLoc(tsnum, tnum, TRUE)))
recordWriteEvent((Unsgn8 *)storploc);
}
return TRUE;
}
/* send a Feedback (MSG_FLAGS_FEEDBACK) message to the client.
* For MSG_FLAGS_FEEDBACK msgs, we do not need to deposit into
* the CB ringbuffer and deal with the associated 'lock-hogging' for a
* busy server. We will send these immediately and return.
*/
void sendFeedback(char *msg)
{
char buf[MESSAGE_SIZE + 64];
Msg_t themsg;
memset((void *)&themsg, 0, sizeof(Msg_t));
themsg.msgto = Context.snum;
themsg.msgfrom = MSG_COMP;
themsg.flags = MSG_FLAGS_FEEDBACK;
strncpy(themsg.msgbuf, msg, MESSAGE_SIZE - 1);
sendMessage(&themsg);
if (SysConf.LogMessages == TRUE)
{
clbFmtMsg(themsg.msgto, themsg.msgfrom, buf);
clog("MSG:FEEDBACK: %s: %s",
buf, themsg.msgbuf);
}
return;
}
/* send a msg to the client */
int sendMessage(Msg_t *msg)
{
spMessage_t smsg;
if (sInfo.state != SVR_STATE_PLAY)
return TRUE;
if (!msg)
return TRUE;
memset((void *)&smsg, 0, sizeof(spMessage_t));
#if defined(DEBUG_SERVERSEND)
clog("sendMessage: to = %d, from = %d, flags = 0x%02x, msg = '%s'",
msg->msgto, msg->msgfrom, msg->flags, msg->msgbuf);
#endif
smsg.type = SP_MESSAGE;
smsg.from = (Sgn16)htons(msg->msgfrom);
smsg.to = (Sgn16)htons(msg->msgto);
smsg.flags = msg->flags;
strncpy(smsg.msg, msg->msgbuf, MESSAGE_SIZE - 1);
/* don't record feeback or tersable msgs */
if (Context.recmode == RECMODE_ON)
if (!(smsg.flags & (MSG_FLAGS_FEEDBACK | MSG_FLAGS_TERSABLE)))
recordWriteEvent((Unsgn8 *)&smsg);
if (!writePacket(PKT_TOCLIENT, sInfo.sock, (Unsgn8 *)&smsg))
{
clog("sendMessage: writePacket failed\n");
return FALSE;
}
return TRUE;
}
int sendTeam(int sock, Unsgn8 team, int force)
{
spTeam_t *steam;
#if defined(DEBUG_SERVERSEND)
clog("sendTeam: team = %d, f = %d", team, force);
#endif
if (Context.recmode == RECMODE_ON)
{
if ((steam = spktTeam(team, force, TRUE)))
recordWriteEvent((Unsgn8 *)steam);
}
if ((steam = spktTeam(team, force, FALSE)))
if (writePacket(PKT_TOCLIENT, sock, (Unsgn8 *)steam) <= 0)
return FALSE;
return TRUE;
}
int sendConqInfo(int sock, int force)
{
spConqInfo_t *spci;
#if defined(DEBUG_SERVERSEND)
clog("sendConqInfo: f = %d", force);
#endif
if ((spci = spktConqInfo(force)))
if (writePacket(PKT_TOCLIENT, sock, (Unsgn8 *)spci) <= 0)
return FALSE;
return TRUE;
}
int sendHistory(int sock, int hnum)
{
spHistory_t *shist;
if (hnum < 0 || hnum > MAXHISTLOG)
return FALSE;
#if defined(DEBUG_SERVERSEND)
clog("sendHistory: hnum = %d", hnum);
#endif
if ((shist = spktHistory(hnum)))
if (writePacket(PKT_TOCLIENT, sock, (Unsgn8 *)shist) <= 0)
return FALSE;
return TRUE;
}
int sendDoomsday(int sock)
{
spDoomsday_t *dd;
#if defined(DEBUG_SERVERSEND)
clog("sendDoomsday");
#endif
if (Context.recmode == RECMODE_ON)
{
if ((dd = spktDoomsday(TRUE)))
recordWriteEvent((Unsgn8 *)dd);
}
if ((dd = spktDoomsday(FALSE)))
if (writePacket(PKT_TOCLIENT, sock, (Unsgn8 *)dd) <= 0)
return FALSE;
return TRUE;
}
void procSetName(Unsgn8 *buf)
{
cpSetName_t *cpsetn = (cpSetName_t *)buf;
if (!validPkt(CP_SETNAME, cpsetn))
return;
cpsetn->alias[MAXUSERPNAME - 1] = 0;
strncpy(Users[Context.unum].alias, cpsetn->alias, MAXUSERPNAME);
if (Context.snum > 0 && Context.snum <= MAXSHIPS)
strncpy(Ships[Context.snum].alias, cpsetn->alias, MAXUSERPNAME);
return;
}
void procSetCourse(Unsgn8 *buf)
{
cpSetCourse_t *csc = (cpSetCourse_t *)buf;
int lock;
real dir;
if (!validPkt(CP_SETCOURSE, csc))
return;
lock = (int)csc->lock;
dir = (real)((real)ntohs(csc->head) / 100.0);
#if defined(DEBUG_SERVERPROC)
clog("PROC SETCOURSE: lock = %d, head = %f",
lock, dir);
#endif
/* now we need to do a few checks to make sure it's legal ;-) */
if (dir < 0.0)
dir = 0.0;
if (dir > 359.9)
dir = 359.9;
if (lock > 0) /* could lock onto ships someday... */
lock = 0; /* but not today. */
if (lock < 0 && (-lock > NUMPLANETS))
lock = 0;
/* always applies to our own ship */
if ( Ships[Context.snum].warp < 0.0 ) /* if orbitting */
Ships[Context.snum].warp = 0.0; /* break orbit */
Ships[Context.snum].dhead = dir; /* set direction first to avoid */
Ships[Context.snum].lock = lock; /* a race in display() */
return;
}
void procSetWarp(cpCommand_t *swarp)
{
int snum = Context.snum; /* we always use our own ship */
char cbuf[BUFFER_SIZE];
real warp;
real mw;
if (!validPkt(CP_COMMAND, swarp))
return;
if (swarp->cmd != CPCMD_SETWARP)
return;
warp = (real)ntohs(swarp->detail);
#if defined(DEBUG_SERVERPROC)
clog("PROC SETWARP: warp = %f", warp);
#endif
if ( Ships[snum].dwarp == 0.0 && warp != 0.0 )
{
/* See if engines are working. */
if ( Ships[snum].efuse > 0 )
{
sendFeedback("Engines are currently overloaded.");
return;
}
/* No charge if already warp 0. */
if ( clbUseFuel( snum, ENGINES_ON_FUEL, FALSE, TRUE ) == FALSE)
{
sendFeedback("We don't have enough fuel.");
return;
}
/* Don't stop repairing if changing to warp 0. */
SFCLR(snum, SHIP_F_REPAIR);
}
/* If orbitting, break orbit. */
if ( Ships[snum].warp < 0.0 )
{
Ships[snum].warp = 0.0;
Ships[snum].lock = 0;
Ships[snum].dhead = Ships[snum].head;
}
/* Handle ship limitations. */
Ships[snum].dwarp = min( warp, ShipTypes[Ships[snum].shiptype].warplim );
/* Warn about damage limitations. */
mw = maxwarp( snum );
if ( around( Ships[snum].dwarp ) > mw )
{
sprintf(cbuf,
"(Due to damage, warp is currently limited to %.1f.)", mw);
sendFeedback(cbuf);
}
return;
}
void procSetShields(cpCommand_t *cmd)
{
int snum = Context.snum; /* we always use our own ship */
int shup;
if (!validPkt(CP_COMMAND, cmd))
return;
if (cmd->cmd != CPCMD_SETSHIELDS)
return;
shup = (((int)ntohs(cmd->detail)) ? TRUE : FALSE);
#if defined(DEBUG_SERVERPROC)
clog("PROC SETSHIELDS: sh = %d", shup);
#endif
if (shup)
{
SFSET(snum, SHIP_F_SHUP);
SFCLR(snum, SHIP_F_REPAIR);
}
else
SFCLR(snum, SHIP_F_SHUP);
return;
}
void procAlloc(cpCommand_t *cmd)
{
int snum = Context.snum; /* we always use our own ship */
int alloc;
if (!validPkt(CP_COMMAND, cmd))
return;
if (cmd->cmd != CPCMD_ALLOC)
return;
/* we only deal with weapons alloc. we compute engalloc based on this */
alloc = (int)ntohs(cmd->detail);
if (alloc < 30)
alloc = 30;
if (alloc > 70)
alloc = 70;
#if defined(DEBUG_SERVERPROC)
clog("PROC ALLOC: (w)alloc = %d", alloc);
#endif
Ships[snum].weapalloc = alloc;
Ships[snum].engalloc = (100 - alloc);
return;
}
void procCloak(cpCommand_t *cmd)
{
int snum = Context.snum; /* we always use our own ship */
string nofuel="Not enough fuel to engage cloaking device.";
if (!validPkt(CP_COMMAND, cmd))
return;
if (cmd->cmd != CPCMD_CLOAK)
return;
#if defined(DEBUG_SERVERPROC)
clog("PROC CLOAK");
#endif
if ( SCLOAKED(snum) )
{
SFCLR(snum, SHIP_F_CLOAKED);
return;
}
if ( Ships[snum].efuse > 0 )
{
sendFeedback("Engines are currently overloaded.");
return;
}
if ( Ships[snum].fuel < CLOAK_ON_FUEL )
{
sendFeedback(nofuel);
return;
}
SFCLR(snum, SHIP_F_REPAIR);
if ( ! clbUseFuel( snum, CLOAK_ON_FUEL, FALSE, TRUE ) )
{
sendFeedback(nofuel);
return;
}
SFSET(snum, SHIP_F_CLOAKED);
return;
}
void procDetSelf(cpCommand_t *cmd)
{
int snum = Context.snum; /* we always use our own ship */
int j;
if (!validPkt(CP_COMMAND, cmd))
return;
if (cmd->cmd != CPCMD_DETSELF)
return;
#if defined(DEBUG_SERVERPROC)
clog("PROC DETSELF");
#endif
for ( j = 0; j < MAXTORPS; j++ )
clbDetonate( snum, j );
return;
}
void procDetEnemy(cpCommand_t *cmd)
{
int snum = Context.snum; /* we always use our own ship */
if (!validPkt(CP_COMMAND, cmd))
return;
if (cmd->cmd != CPCMD_DETENEMY)
return;
#if defined(DEBUG_SERVERPROC)
clog("PROC DETENEMY");
#endif
clbEnemyDet( snum );
return;
}
void procDistress(cpCommand_t *cmd)
{
int snum = Context.snum; /* we always use our own ship */
char buf[128], cbuf[128];
real x;
int i, isorb = FALSE;
int tofriendly;
if (!validPkt(CP_COMMAND, cmd))
return;
if (cmd->cmd != CPCMD_DISTRESS)
return;
tofriendly = (int)ntohs(cmd->detail);
#if defined(DEBUG_SERVERPROC)
clog("PROC DISTRESS: tofriendly = %d", tofriendly);
#endif
sprintf( cbuf,
"sh=%d %c, dam=%d, fuel=%d, temp=",
round(Ships[snum].shields),
(SSHUP(snum)) ? 'U' : 'D',
round(Ships[snum].damage),
round(Ships[snum].fuel) );
i = round(Ships[snum].wtemp);
if ( i < 100 )
appint( i, cbuf );
else
appstr( "**", cbuf );
appchr( '/', cbuf );
i = round(Ships[snum].etemp);
if ( i < 100 )
appint( i, cbuf );
else
appstr( "**", cbuf );
i = Ships[snum].armies;
if ( i > 0 )
{
appstr( ", arm=", cbuf );
appint( i, cbuf );
}
if ( Ships[snum].wfuse > 0 )
appstr( ", -weap", cbuf );
if ( Ships[snum].efuse > 0 )
appstr( ", -eng", cbuf );
/* warp */
x = Ships[snum].warp;
if ( x >= 0.0 )
{
sprintf( buf, ", warp=%.1f", x );
appstr(buf, cbuf);
isorb = FALSE;
}
else
{
sprintf( buf, ", orbiting %.3s",
Planets[-Ships[snum].lock].name );
appstr(buf, cbuf);
isorb = TRUE;
}
/* heading */
if (isorb == FALSE)
{
i = Ships[snum].lock;
if ( i >= 0 || Ships[snum].warp < 0.0)
i = round( Ships[snum].head );
if ( -i > 0 && -i <= NUMPLANETS)
sprintf( buf, ", head=%.3s", Planets[-i].name );
else
sprintf( buf, ", head=%d", i );
appstr(buf, cbuf);
}
if (tofriendly)
clbStoreMsg( snum, MSG_FRIENDLY, cbuf );
else
clbStoreMsg( snum, -Ships[snum].team, cbuf );
return;
}
void procFirePhaser(cpCommand_t *cmd)
{
int snum = Context.snum; /* we always use our own ship */
real dir;
if (!validPkt(CP_COMMAND, cmd))
return;
if (cmd->cmd != CPCMD_FIREPHASER)
return;
dir = (real)ntohs(cmd->detail) / 100.0;
if (dir < 0)
dir = 0.0;
if (dir > 359.9)
dir = 359.9;
#if defined(DEBUG_SERVERPROC)
clog("PROC FIREPHASER: dir = %f", dir);
#endif
if ( SCLOAKED(snum) )
{
sendFeedback("The cloaking device is using all available power.");
return;
}
if ( Ships[snum].wfuse > 0 )
{
sendFeedback("Weapons are currently overloaded.");
return;
}
if ( Ships[snum].fuel < PHASER_FUEL )
{
sendFeedback("Not enough fuel to fire phasers.");
return;
}
if ( !clbPhaser( snum, dir ) )
sendFeedback(">PHASERS DRAINED<");
return;
}
void procOrbit(cpCommand_t *cmd)
{
int snum = Context.snum; /* we always use our own ship */
int pnum;
if (!validPkt(CP_COMMAND, cmd))
return;
if (cmd->cmd != CPCMD_ORBIT)
return;
#if defined(DEBUG_SERVERPROC)
clog("PROC ORBIT");
#endif
if ( ( Ships[snum].warp == ORBIT_CW ) || ( Ships[snum].warp == ORBIT_CCW ) )
return;
if ( ! clbFindOrbit( snum, &pnum ) )
return;
if ( Ships[snum].warp > MAX_ORBIT_WARP )
return;
clbOrbit( snum, pnum );
return;
}
void procRepair(cpCommand_t *cmd)
{
int snum = Context.snum; /* we always use our own ship */
if (!validPkt(CP_COMMAND, cmd))
return;
if (cmd->cmd != CPCMD_REPAIR)
return;
#if defined(DEBUG_SERVERPROC)
clog("PROC REPAIR");
#endif
if ( ! SCLOAKED(snum) )
{
SFSET(snum, SHIP_F_REPAIR);
Ships[snum].dwarp = 0.0;
}
return;
}
void procCoup(cpCommand_t *cmd)
{
int snum = Context.snum; /* we always use our own ship */
int i, pnum, now, entertime;
real failprob;
string nhp="We must be orbiting our home planet to attempt a coup.";
char cbuf[128];
int rv = -1;
if (!validPkt(CP_COMMAND, cmd))
return;
if (cmd->cmd != CPCMD_COUP)
return;
#if defined(DEBUG_SERVERPROC)
clog("PROC COUP");
#endif
/* Check for allowability. */
if ( oneplace( Ships[snum].kills ) < MIN_COUP_KILLS )
{
sendFeedback("Fleet orders require three kills before a coup can be attempted.");
return;
}
for ( i = 1; i <= NUMPLANETS; i = i + 1 )
if ( Planets[i].real && (Planets[i].team == Ships[snum].team) &&
(Planets[i].armies > 0) )
{
sendFeedback("We don't need to coup, we still have armies left!");
return;
}
if ( Ships[snum].warp >= 0.0 )
{
sendFeedback(nhp);
return;
}
pnum = -Ships[snum].lock;
if ( pnum != Teams[Ships[snum].team].homeplanet )
{
sendFeedback(nhp);
return;
}
if ( Planets[pnum].armies > MAX_COUP_ENEMY_ARMIES )
{
sendFeedback("The enemy is still too strong to attempt a coup.");
return;
}
i = Planets[pnum].uninhabtime;
if ( i > 0 )
{
sprintf( cbuf, "This planet is uninhabitable for %d more minutes.",
i );
sendFeedback(cbuf);
return;
}
/* Now our team can tell coup time for free. */
Teams[Ships[snum].team].coupinfo = TRUE;
i = Teams[Ships[snum].team].couptime;
if ( i > 0 )
{
sprintf( cbuf, "Our forces need %d more minutes to organize.", i );
sendFeedback(cbuf);
return;
}
/* Now wait it out... */
sendFeedback("Attempting coup...");
grand( &entertime );
while ( dgrand( entertime, &now ) < COUP_GRAND )
{
/* See if we're still alive. */
if ( ! clbStillAlive( Context.snum ) )
return;
/* Sleep */
c_sleep( ITER_SECONDS );
}
PVLOCK(&ConqInfo->lockword);
if ( Planets[pnum].team == Ships[snum].team )
{
PVUNLOCK(&ConqInfo->lockword);
sendFeedback("Sensors show hostile forces eliminated from the planet.");
return;
}
failprob = Planets[pnum].armies / MAX_COUP_ENEMY_ARMIES * 0.5 + 0.5;
if ( rnd() < failprob )
{
/* Failed; setup new reorganization time. */
Teams[Ships[snum].team].couptime = rndint( 5, 10 );
PVUNLOCK(&ConqInfo->lockword);
sendFeedback("Coup unsuccessful.");
return;
}
rv = clbTakePlanet( pnum, snum );
/* Make the planet not scanned. */
for ( i = 0; i < NUMPLAYERTEAMS; i = i + 1 )
Planets[pnum].scanned[i] = FALSE;
/* ...except by us */
Planets[pnum].scanned[Ships[snum].team] = TRUE;
Planets[pnum].armies = rndint( 10, 20 ); /* create token coup force */
Users[Ships[snum].unum].stats[USTAT_COUPS] += 1;
Teams[Ships[snum].team].stats[TSTAT_COUPS] += 1;
PVUNLOCK(&ConqInfo->lockword);
sendFeedback("Coup successful!");
if (rv >= 0 && rv < NUMPLAYERTEAMS) /* someone was geno'd, send team msg */
clbStoreMsg( MSG_COMP, -rv, guilt );
/* force a team update for this ship */
sendTeam(sInfo.sock, Ships[snum].team, TRUE);
return;
}
void procFireTorps(Unsgn8 *buf)
{
int snum = Context.snum; /* we always use our own ship */
cpFireTorps_t *cftorp = (cpFireTorps_t *)buf;
real dir;
int num;
if (!validPkt(CP_FIRETORPS, cftorp))
return;
dir = (real)((real)ntohs(cftorp->dir) / 100.0);
num = (int)cftorp->num;
if (dir < 0)
dir = 0.0;
if (dir > 359.9)
dir = 359.9;
#if defined(DEBUG_SERVERPROC)
clog("PROC FIRETORPS: dir = %f, num = %d", dir, num);
#endif
if ( SCLOAKED(snum) )
{
sendFeedback("The cloaking device is using all available power.");
return;
}
if ( Ships[snum].wfuse > 0 )
{
sendFeedback("Weapons are currently overloaded.");
return;
}
if ( Ships[snum].fuel < TORPEDO_FUEL )
{
sendFeedback("Not enough fuel to launch a torpedo.");
return;
}
if ( ! clbLaunch( snum, dir, num, LAUNCH_NORMAL ) )
{
sendFeedback(">TUBES EMPTY<");
}
return;
}
void procMessage(Unsgn8 *buf)
{
int snum = Context.snum; /* we always use our own ship */
cpMessage_t *cmsg = (cpMessage_t *)buf;
int to;
if (sInfo.state != SVR_STATE_PLAY)
return;
if (!validPkt(CP_MESSAGE, cmsg))
return;
to = (Sgn16)ntohs(cmsg->to);
cmsg->msg[MESSAGE_SIZE - 1] = 0;
#if defined(DEBUG_SERVERPROC)
clog("PROC MESSAGE: to %d", to);
#endif
clbStoreMsg(snum, to, cmsg->msg);
checkOperExec(snum, to, cmsg->msg);
return;
}
void procChangePassword(Unsgn8 *buf)
{
char salt[3];
int unum = Context.unum;
cpAuthenticate_t *cauth = (cpAuthenticate_t *)buf;
if (!validPkt(CP_AUTHENTICATE, buf))
return;
cauth->pw[MAXUSERNAME - 1] = 0;
/* we are just interested in the pw */
#if defined(DEBUG_SERVERPROC)
clog("PROC ChangePassword");
#endif
salt[0] = (Users[unum].username[0] != EOS) ? Users[unum].username[0] :
'J';
salt[1] = (Users[unum].username[1] != EOS) ? Users[unum].username[1] :
'T';
salt[2] = EOS;
strncpy(Users[unum].pw, (char *)crypt(cauth->pw, salt), MAXUSERNAME - 2);
Users[unum].pw[MAXUSERNAME - 1] = EOS;
return;
}
void procSetWar(cpCommand_t *cmd)
{
int snum = Context.snum; /* we always use our own ship */
int unum = Context.unum;
int dowait = FALSE, entertime, now, i;
Unsgn8 war;
if (!validPkt(CP_COMMAND, cmd))
return;
if (cmd->cmd != CPCMD_SETWAR)
return;
war = (Unsgn8)ntohs(cmd->detail);
#if defined(DEBUG_SERVERPROC)
clog("PROC SETWAR war = 0x%02x", war);
#endif
for (i=0; i<NUMPLAYERTEAMS; i++)
{
if (war & (1 << i))
{
if (!Ships[Context.snum].war[i]) /* if not at war, we will delay */
dowait = TRUE;
Ships[snum].war[i] = TRUE;
}
else
{
Ships[snum].war[i] = FALSE;
}
Users[unum].war[i] = Ships[snum].war[i];
}
/* now we 'sleep' for awhile, if dowait is set */
/* any packets the client tries to send will have to wait ;-) */
if (dowait && Ships[Context.snum].status != SS_RESERVED)
{
grand( &entertime );
while ( dgrand( entertime, &now ) < REARM_GRAND )
{
/* See if we're still alive. */
if ( ! clbStillAlive( Context.snum ) )
return;
/* Sleep */
c_sleep( ITER_SECONDS );
}
}
return;
}
void procRefit(cpCommand_t *cmd)
{
int snum = Context.snum; /* we always use our own ship */
int entertime, now;
int stype;
int pnum;
if (!validPkt(CP_COMMAND, cmd))
return;
if (cmd->cmd != CPCMD_REFIT)
return;
stype = (int)ntohs(cmd->detail);
if (stype < 0 || stype >= MAXNUMSHIPTYPES)
return;
#if defined(DEBUG_SERVERPROC)
clog("PROC REFIT: snum - %d, stype = %d", snum, stype);
#endif
/* Check for allowability. */
if ( oneplace( Ships[snum].kills ) < MIN_REFIT_KILLS )
{
sendFeedback("You must have at least one kill to refit.");
return;
}
pnum = -Ships[snum].lock;
if (Planets[pnum].team != Ships[snum].team || Ships[snum].warp >= 0.0)
{
sendFeedback("We must be orbiting a team owned planet to refit.");
return;
}
if (Ships[snum].armies != 0)
{
sendFeedback("You cannot refit while carrying armies");
return;
}
/* now we wait for a bit. */
grand( &entertime );
while ( dgrand( entertime, &now ) < REFIT_GRAND )
{
/* See if we're still alive. */
if ( ! clbStillAlive( snum ) )
return;
/* Sleep */
c_sleep( ITER_SECONDS );
}
/* set it */
Ships[snum].shiptype = stype;
return;
}
void procSetRate(cpCommand_t *cmd)
{
#if defined(DEBUG_SERVERPROC)
int snum = Context.snum;
#endif
int rate;
if (!validPkt(CP_COMMAND, cmd))
return;
if (cmd->cmd != CPCMD_SETRATE)
return;
rate = (int)ntohs(cmd->detail);
if (rate < 1 || rate > 10)
return;
#if defined(DEBUG_SERVERPROC)
clog("PROC SETRATE: snum = %d, rate = %d", snum, rate);
#endif
Context.updsec = rate;
return;
}
void procTow(cpCommand_t *cmd)
{
int snum = Context.snum;
int other;
char cbuf[BUFFER_SIZE];
if (!validPkt(CP_COMMAND, cmd))
return;
if (cmd->cmd != CPCMD_TOW)
return;
#if defined(DEBUG_SERVERPROC)
clog("PROC TOW: snum = %d", snum);
#endif
other = (int)ntohs(cmd->detail);
if ( Ships[snum].towedby != 0 )
{
c_strcpy( "But we are being towed by ", cbuf );
appship( Ships[snum].towedby, cbuf );
appchr( '!', cbuf );
sendFeedback(cbuf);
return;
}
if ( Ships[snum].towing != 0 )
{
c_strcpy( "But we're already towing ", cbuf );
appship( Ships[snum].towing, cbuf );
appchr( '.', cbuf );
sendFeedback(cbuf);
return;
}
cbuf[0] = 0;
PVLOCK(&ConqInfo->lockword);
if ( other < 1 || other > MAXSHIPS )
c_strcpy( "No such ship.", cbuf );
else if ( Ships[other].status != SS_LIVE )
c_strcpy( "Not found.", cbuf );
else if ( other == snum )
c_strcpy( "We can't tow ourselves!", cbuf );
else if ( dist( Ships[snum].x, Ships[snum].y, Ships[other].x, Ships[other].y ) > TRACTOR_DIST )
c_strcpy( "That ship is out of tractor range.", cbuf );
else if ( Ships[other].warp < 0.0 )
c_strcpy( "You can't tow a ship out of orbit.", cbuf );
else if ( sqrt( pow(( (real) (Ships[snum].dx - Ships[other].dx) ), (real) 2.0) +
pow( (real) ( Ships[snum].dy - Ships[other].dy ), (real) 2.0 ) ) /
( MM_PER_SEC_PER_WARP * ITER_SECONDS ) > MAX_TRACTOR_WARP )
sprintf( cbuf, "That ships relative velocity is higher than %2.1f.",
MAX_TRACTOR_WARP );
else if ( Ships[other].towing != 0 || Ships[other].towedby != 0 )
c_strcpy(
"There seems to be some interference with the tractor beams...",
cbuf );
else
{
Ships[other].towedby = snum;
Ships[snum].towing = other;
c_strcpy("Tractor beams engaged.", cbuf);
}
PVUNLOCK(&ConqInfo->lockword);
sendFeedback(cbuf);
return;
}
void procUnTow(cpCommand_t *cmd)
{
int snum = Context.snum;
char cbuf[BUFFER_SIZE];
int entertime, now;
int warsome;
if (!validPkt(CP_COMMAND, cmd))
return;
if (cmd->cmd != CPCMD_UNTOW)
return;
#if defined(DEBUG_SERVERPROC)
clog("PROC UNTOW: snum = %d", snum);
#endif
if ( Ships[snum].towedby != 0 )
{
/* If we're at war with him or he's at war with us, make it */
/* hard to break free. */
warsome = ( satwar( snum, Ships[snum].towedby) );
if ( warsome )
{
grand( &entertime );
while ( dgrand( entertime, &now ) < BREAKAWAY_GRAND )
{
if ( ! clbStillAlive( Context.snum ) )
return;
c_sleep( ITER_SECONDS );
}
}
if ( warsome && ( rnd() > BREAKAWAY_PROB ) )
sendFeedback("Attempt to break free failed.");
else
{
c_strcpy( "Breaking free from ship ", cbuf );
appship( Ships[snum].towedby, cbuf );
PVLOCK(&ConqInfo->lockword);
if ( Ships[snum].towedby != 0 )
{
/* Coast to a stop. */
Ships[snum].head = Ships[Ships[snum].towedby].head;
if (!SysConf.AllowSlingShot)
{ /* only set warp if valid JET - 9/15/97 */
if (Ships[Ships[snum].towedby].warp >= 0.0)
Ships[snum].warp = Ships[Ships[snum].towedby].warp;
else
Ships[snum].warp = 2.0;
}
else
Ships[snum].warp = Ships[Ships[snum].towedby].warp;
/* Release the tow. */
if ( Ships[Ships[snum].towedby].towing != 0 )
Ships[Ships[snum].towedby].towing = 0;
Ships[snum].towedby = 0;
}
PVUNLOCK(&ConqInfo->lockword);
appchr( '.', cbuf );
sendFeedback(cbuf);
}
}
else if ( Ships[snum].towing != 0 )
{
c_strcpy( "Tow released from ship ", cbuf );
appship( Ships[snum].towing, cbuf );
PVLOCK(&ConqInfo->lockword);
if ( Ships[snum].towing != 0 )
{
/* Set other ship coasting. */
Ships[Ships[snum].towing].head = Ships[snum].head;
if (!SysConf.AllowSlingShot)
{ /* only set warp if valid JET - 9/15/97 */
if (Ships[snum].warp >= 0.0)
Ships[Ships[snum].towing].warp = Ships[snum].warp;
else
Ships[Ships[snum].towing].warp = 2.0;
}
else
Ships[Ships[snum].towing].warp = Ships[snum].warp;
/* Release the tow. */
if ( Ships[Ships[snum].towing].towedby != 0 )
Ships[Ships[snum].towing].towedby = 0;
Ships[snum].towing = 0;
}
PVUNLOCK(&ConqInfo->lockword);
appchr( '.', cbuf );
sendFeedback(cbuf);
}
else
sendFeedback("No tractor beam activity detected.");
return;
}
void procBomb(cpCommand_t *cmd)
{
int snum = Context.snum;
int bomb;
int pnum, now, entertime, total, ototal, oparmies;
real x, killprob;
int oldsshup;
char buf[MSGMAXLINE];
char cbuf[BUFFER_SIZE];
string lastfew="The last few armies are eluding us.";
if (!validPkt(CP_COMMAND, cmd))
return;
if (cmd->cmd != CPCMD_BOMB)
return;
bomb = (int)ntohs(cmd->detail);
#if defined(DEBUG_SERVERPROC)
clog("PROC BOMB: snum = %d, start bombing = %d", snum, bomb);
#endif
if (!bomb) /* the bombing has stopped. yaay. */
return;
SFCLR(snum, SHIP_F_REPAIR);
/* Check for allowability. */
if ( Ships[snum].warp >= 0.0 )
{
sendAck(sInfo.sock, PKT_TOCLIENT, PSEV_INFO, PERR_CANCELED, NULL);
sendFeedback("We must be orbiting a planet to bombard it.");
return;
}
pnum = -Ships[snum].lock;
if ( Planets[pnum].type == PLANET_SUN || Planets[pnum].type == PLANET_MOON ||
Planets[pnum].team == TEAM_NOTEAM || Planets[pnum].armies == 0 )
{
sendAck(sInfo.sock, PKT_TOCLIENT, PSEV_INFO, PERR_CANCELED, NULL);
sendFeedback("There is no one there to bombard.");
return;
}
if ( Planets[pnum].team == Ships[snum].team )
{
sendAck(sInfo.sock, PKT_TOCLIENT, PSEV_INFO, PERR_CANCELED, NULL);
sendFeedback("We can't bomb our own armies!");
return;
}
if ( Planets[pnum].team != TEAM_SELFRULED && Planets[pnum].team != TEAM_GOD )
if ( ! Ships[snum].war[Planets[pnum].team] )
{
sendAck(sInfo.sock, PKT_TOCLIENT, PSEV_INFO, PERR_CANCELED, NULL);
sendFeedback("But we are not at war with this planet!");
return;
}
/* Handle war logic. */
Ships[snum].srpwar[pnum] = TRUE;
if ( Planets[pnum].team >= 0 && Planets[pnum].team < NUMPLAYERTEAMS )
{
/* For a team planet make the war sticky and send an intruder alert. */
Ships[snum].rwar[Planets[pnum].team] = TRUE;
clbIntrude( snum, pnum );
}
/* Planets owned by GOD have a special defense system. */
if ( Planets[pnum].team == TEAM_GOD )
{
sprintf( cbuf, "That was a bad idea, %s...", Ships[snum].alias );
clbDamage( snum, rnduni( 50.0, 100.0 ), KB_LIGHTNING );
sendAck(sInfo.sock, PKT_TOCLIENT, PSEV_INFO, PERR_CANCELED, NULL);
sendFeedback(cbuf);
return;
}
/* Lower shields. */
oldsshup = SSHUP(snum);
SFCLR(snum, SHIP_F_SHUP);
/* Bombard. */
total = 0;
ototal = -1; /* force an update the first time */
oparmies = -1;
grand( &entertime ); /* get start time */
while(TRUE)
{
if ( ! clbStillAlive( Context.snum ) )
return;
if ( isPacketWaiting(sInfo.sock) )
break;
/* See if it's time to bomb yet. */
while ((int) fabs ((real)dgrand( (int)entertime, (int *)&now )) >= BOMBARD_GRAND )
{
if ( Ships[snum].wfuse > 0 )
{
sendAck(sInfo.sock, PKT_TOCLIENT, PSEV_INFO, PERR_CANCELED,
NULL);
sendFeedback("Weapons are currently overloaded.");
goto cbrk22; /* break 2;*/
}
x = BOMBARD_FUEL * (real)(BOMBARD_GRAND / 1000.0);
if ( ! clbUseFuel( snum, x, TRUE, TRUE ) )
{
sendAck(sInfo.sock, PKT_TOCLIENT, PSEV_INFO, PERR_CANCELED,
NULL);
sendFeedback("Not enough fuel to bombard.");
goto cbrk22; /* break 2;*/
}
/* entertime = mod( entertime + BOMBARD_GRAND, 24*60*60*1000 );*/
grand(&entertime);
killprob = (real)((BOMBARD_PROB *
((real) weaeff( snum ) *
(real)((real)Planets[pnum].armies/100.0))) + 0.5);
if ( rnd() < killprob )
{
PVLOCK(&ConqInfo->lockword);
if ( Planets[pnum].armies <= MIN_BOMB_ARMIES )
{
/* No more armies left to bomb. */
PVUNLOCK(&ConqInfo->lockword);
sendAck(sInfo.sock, PKT_TOCLIENT, PSEV_INFO, PERR_CANCELED,
NULL);
sendFeedback(lastfew);
goto cbrk22; /* break 2;*/
}
Planets[pnum].armies = Planets[pnum].armies - 1;
Ships[snum].kills = Ships[snum].kills + BOMBARD_KILLS;
Users[Ships[snum].unum].stats[USTAT_ARMBOMB] += 1;
Teams[Ships[snum].team].stats[TSTAT_ARMBOMB] += 1;
PVUNLOCK(&ConqInfo->lockword);
total = total + 1;
}
}
if ( Planets[pnum].armies <= MIN_BOMB_ARMIES )
{
/* No more armies left to bomb. */
sendAck(sInfo.sock, PKT_TOCLIENT, PSEV_INFO, PERR_CANCELED, NULL);
sendFeedback(lastfew);
break;
}
if ( Planets[pnum].armies != oparmies || ototal != total )
{
/* Either our bomb run total or the population changed. */
oparmies = Planets[pnum].armies;
if ( total == 1 )
c_strcpy( "y", buf );
else
c_strcpy( "ies", buf );
sprintf( cbuf, "Bombing %s, %d arm%s killed, %d left.",
Planets[pnum].name, total, buf, oparmies );
sendFeedback(cbuf);
ototal = total;
}
c_sleep( ITER_SECONDS );
}
cbrk22:
;
/* Restore shields. */
if (oldsshup)
SFSET(snum, SHIP_F_SHUP);
return;
}
void procBeam(cpCommand_t *cmd)
{
int snum = Context.snum;
int beam, beamup;
int pnum, total, num, upmax, downmax, capacity, beamax, i;
int ototal, entertime, now;
int oldsshup, dirup, zeroed, conqed;
char cbuf[BUFFER_SIZE];
real rkills;
string lastfew="Fleet orders prohibit removing the last three armies.";
int rv = -1;
if (!validPkt(CP_COMMAND, cmd))
return;
if (cmd->cmd != CPCMD_BEAM)
return;
/* detail is (armies & 0x00ff), 0x8000 set if beaming down */
beam = (int)(ntohs(cmd->detail) & 0x00ff);
if (beam == 0)
return; /* stop beaming */
if (ntohs(cmd->detail) & 0x8000)
beamup = FALSE; /* beaming down */
else
beamup = TRUE; /* beaming up */
#if defined(DEBUG_SERVERPROC)
clog("PROC BEAM: snum = %d, detail = 0x%04x, beamup = %d, beam = %d",
snum, cmd->detail, beamup, beam);
#endif
SFCLR(snum, SHIP_F_REPAIR);
/* Check for allowability. */
if ( Ships[snum].warp >= 0.0 )
{
sendAck(sInfo.sock, PKT_TOCLIENT, PSEV_INFO, PERR_CANCELED, NULL);
sendFeedback("We must be orbiting a planet to use the transporter.");
return;
}
pnum = -Ships[snum].lock;
if ( Ships[snum].armies > 0 )
{
if ( Planets[pnum].type == PLANET_SUN )
{
sendAck(sInfo.sock, PKT_TOCLIENT, PSEV_INFO, PERR_CANCELED, NULL);
sendFeedback("Idiot! Our armies will fry down there!");
return;
}
else if ( Planets[pnum].type == PLANET_MOON )
{
sendAck(sInfo.sock, PKT_TOCLIENT, PSEV_INFO, PERR_CANCELED, NULL);
sendFeedback("Fool! Our armies will suffocate down there!");
return;
}
else if ( Planets[pnum].team == TEAM_GOD )
{
sendAck(sInfo.sock, PKT_TOCLIENT, PSEV_INFO, PERR_CANCELED, NULL);
sendFeedback("GOD->you: YOUR ARMIES AREN'T GOOD ENOUGH FOR THIS PLANET.");
return;
}
}
i = Planets[pnum].uninhabtime;
if ( i > 0 )
{
sprintf( cbuf, "This planet is uninhabitable for %d more minute",
i );
if ( i != 1 )
appchr( 's', cbuf );
appchr( '.', cbuf );
sendAck(sInfo.sock, PKT_TOCLIENT, PSEV_INFO, PERR_CANCELED, NULL);
sendFeedback(cbuf);
return;
}
if ( Planets[pnum].team != Ships[snum].team &&
Planets[pnum].team != TEAM_SELFRULED &&
Planets[pnum].team != TEAM_NOTEAM )
if ( ! Ships[snum].war[Planets[pnum].team] && Planets[pnum].armies != 0) /* can take empty planets */
{
sendAck(sInfo.sock, PKT_TOCLIENT, PSEV_INFO, PERR_CANCELED, NULL);
sendFeedback("But we are not at war with this planet!");
return;
}
if ( Ships[snum].armies == 0 &&
Planets[pnum].team == Ships[snum].team && Planets[pnum].armies <= MIN_BEAM_ARMIES )
{
sendAck(sInfo.sock, PKT_TOCLIENT, PSEV_INFO, PERR_CANCELED, NULL);
sendFeedback(lastfew);
return;
}
rkills = Ships[snum].kills;
if ( rkills < (real)1.0 )
{
sendAck(sInfo.sock, PKT_TOCLIENT, PSEV_INFO, PERR_CANCELED, NULL);
sendFeedback("Fleet orders prohibit beaming armies until you have a kill.");
return;
}
/* Figure out what can be beamed. */
downmax = Ships[snum].armies;
if ( clbSPWar(snum,pnum) ||
Planets[pnum].team == TEAM_SELFRULED ||
Planets[pnum].team == TEAM_NOTEAM ||
Planets[pnum].team == TEAM_GOD ||
Planets[pnum].armies == 0 )
{
upmax = 0;
}
else
{
capacity = min( ifix( rkills ) * 2, ShipTypes[Ships[snum].shiptype].armylim );
upmax = min( Planets[pnum].armies - MIN_BEAM_ARMIES,
capacity - Ships[snum].armies );
}
/* If there are armies to beam but we're selfwar... */
if ( upmax > 0 && selfwar(snum) && Ships[snum].team == Planets[pnum].team )
{
if ( downmax <= 0 )
{
c_strcpy( "The arm", cbuf );
if ( upmax == 1 )
appstr( "y is", cbuf );
else
appstr( "ies are", cbuf );
appstr( " reluctant to beam aboard a pirate vessel.", cbuf );
sendAck(sInfo.sock, PKT_TOCLIENT, PSEV_INFO, PERR_CANCELED, NULL);
sendFeedback(cbuf);
return;
}
upmax = 0;
}
/* Figure out which direction to beam. */
if ( upmax <= 0 && downmax <= 0 )
{
sendAck(sInfo.sock, PKT_TOCLIENT, PSEV_INFO, PERR_CANCELED, NULL);
sendFeedback("There is no one to beam.");
return;
}
if ( upmax <= 0 )
dirup = FALSE;
else if ( downmax <= 0 )
dirup = TRUE;
else
dirup = beamup;
if ( dirup )
beamax = upmax;
else
beamax = downmax;
if (beam > beamax)
beam = beamax;
num = beam;
/* Now we are ready! */
if ( Planets[pnum].team >= NUMPLAYERTEAMS )
{
/* If the planet is not race owned, make it war with us. */
Ships[snum].srpwar[pnum] = TRUE;
}
else if ( Planets[pnum].team != Ships[snum].team )
{
/* For a team planet make the war sticky and send an intruder alert. */
Ships[snum].rwar[Planets[pnum].team] = TRUE;
/* Chance to create a robot here. */
clbIntrude( snum, pnum );
}
/* Lower shields. */
oldsshup = SSHUP(snum);
SFCLR(snum, SHIP_F_SHUP);
/* Beam. */
total = 0;
ototal = -1; /* force an update the first time */
zeroed = FALSE;
conqed = FALSE;
grand( &entertime );
while(TRUE)
{
if ( ! clbStillAlive( Context.snum ) )
return;
if ( isPacketWaiting(sInfo.sock) )
break;
/* See if it's time to beam again. */
while ( dgrand( entertime, &now ) >= BEAM_GRAND )
{
grand(&entertime);
PVLOCK(&ConqInfo->lockword);
if ( dirup )
{
/* Beam up. */
if ( Planets[pnum].armies <= MIN_BEAM_ARMIES )
{
PVUNLOCK(&ConqInfo->lockword);
sendAck(sInfo.sock, PKT_TOCLIENT, PSEV_INFO, PERR_CANCELED,
NULL);
sendFeedback(lastfew);
break;
}
Ships[snum].armies = Ships[snum].armies + 1;
Planets[pnum].armies = Planets[pnum].armies - 1;
}
else
{
/* Beam down. */
Ships[snum].armies = Ships[snum].armies - 1;
if ( Planets[pnum].team == TEAM_NOTEAM || Planets[pnum].armies == 0 )
{
rv = clbTakePlanet( pnum, snum );
conqed = TRUE;
}
else if ( Planets[pnum].team != Ships[snum].team )
{
Planets[pnum].armies = Planets[pnum].armies - 1;
if ( Planets[pnum].armies == 0 )
{
rv = clbZeroPlanet( pnum, snum );
zeroed = TRUE;
}
}
else
Planets[pnum].armies = Planets[pnum].armies + 1;
}
PVUNLOCK(&ConqInfo->lockword);
total = total + 1;
/* someone was geno'd, send team msg */
if (rv >= 0 && rv < NUMPLAYERTEAMS)
clbStoreMsg( MSG_COMP, -rv, guilt );
if ( total >= num )
{
/* Done. */
sendAck(sInfo.sock, PKT_TOCLIENT, PSEV_INFO, PERR_DONE,
NULL);
sendFeedback("");
goto cbrk21;
}
}
if ( ototal != total )
{
c_strcpy( "Beaming ", cbuf );
if ( dirup )
appstr( "up from ", cbuf );
else
appstr( "down to ", cbuf );
appstr( Planets[pnum].name, cbuf );
appstr( ", ", cbuf );
if ( total == 0 )
appstr( "no", cbuf );
else
appint( total, cbuf );
appstr( " arm", cbuf );
if ( total == 1 )
{
appchr( 'y', cbuf );
}
else
{
appstr( "ies", cbuf );
}
appstr( " transported, ", cbuf );
appint( num - total, cbuf );
appstr( " to go.", cbuf );
sendFeedback(cbuf);
ototal = total;
}
if ( dirup && Planets[pnum].armies <= MIN_BEAM_ARMIES )
{
sendFeedback(lastfew);
break;
}
c_sleep( ITER_SECONDS );
}
cbrk21:
/* Restore shields. */
if (oldsshup)
SFSET(snum, SHIP_F_SHUP);
if ( conqed )
{
sprintf( cbuf, "You have conquered %s.", Planets[pnum].name );
sendFeedback(cbuf);
}
else if ( zeroed )
sendFeedback("Sensors show hostile forces eliminated from the planet.");
return;
}
void procDestruct(cpCommand_t *cmd)
{
int snum = Context.snum;
int entertime, now;
if (!validPkt(CP_COMMAND, cmd))
return;
if (cmd->cmd != CPCMD_DESTRUCT)
return;
#if defined(DEBUG_SERVERPROC)
clog("PROC DESTRUCT: snum = %d, detail = 0x%04x",
snum, cmd->detail);
#endif
if (!ntohs(cmd->detail))
return; /* canceled a self destruct */
/* time to breach the core */
if ( SCLOAKED(snum) )
{
sendAck(sInfo.sock, PKT_TOCLIENT, PSEV_INFO, PERR_CANCELED,
NULL);
sendFeedback("The cloaking device is using all available power.");
return;
}
/* Set up the destruct fuse. */
Ships[Context.snum].sdfuse = SELFDESTRUCT_FUSE;
gsecs( &entertime );
Context.msgok = TRUE; /* messages are ok in the beginning */
while ( Ships[Context.snum].sdfuse > 0 )
{
Ships[Context.snum].sdfuse = SELFDESTRUCT_FUSE - dsecs ( entertime, &now );
/* Display new messages until T-minus 3 seconds. */
if ( Ships[Context.snum].sdfuse < 3 )
Context.msgok = FALSE;
if ( ! clbStillAlive( Context.snum ) )
{
/* Died in the process. */
Ships[Context.snum].sdfuse = 0;
return;
}
if ( isPacketWaiting(sInfo.sock) )
{
Ships[Context.snum].sdfuse = 0;
sendAck(sInfo.sock, PKT_TOCLIENT, PSEV_INFO, PERR_CANCELED,
NULL);
return;
}
c_sleep( ITER_SECONDS );
} /* end while */
Context.msgok = FALSE; /* turn off messages */
if ( Doomsday->status == DS_LIVE )
{
if ( dist(Ships[Context.snum].x, Ships[Context.snum].y,
Doomsday->x, Doomsday->y) <= DOOMSDAY_KILL_DIST )
{
Doomsday->status = DS_OFF;
clbStoreMsg( MSG_DOOM, MSG_ALL, "AIEEEEEEEE!" );
clbKillShip( Context.snum, KB_GOTDOOMSDAY );
}
else
if (clbStillAlive(Context.snum)) /* if we're not dead yet... */
clbKillShip( Context.snum, KB_SELF );
}
else
{
if (clbStillAlive(Context.snum)) /* if we're not dead yet... */
clbKillShip( Context.snum, KB_SELF );
}
sendAck(sInfo.sock, PKT_TOCLIENT, PSEV_INFO, PERR_DONE,
NULL);
return;
}
void procAutoPilot(cpCommand_t *cmd)
{
int snum = Context.snum;
int laststat, now;
if (!validPkt(CP_COMMAND, cmd))
return;
if (cmd->cmd != CPCMD_AUTOPILOT)
return;
#if defined(DEBUG_SERVERPROC)
clog("PROC AUTOPILOT: snum = %d, detail = 0x%04x",
snum, ntohs(cmd->detail));
#endif
if (!ntohs(cmd->detail))
return; /* cancelled autopilot */
/* allowed? */
if (!Users[Ships[snum].unum].ooptions[OOPT_AUTOPILOT])
{
clog("PROC AUTOPILOT: unum = %d, snum = %d: NOT ALLOWED",
Ships[snum].unum, snum);
return;
}
sendFeedback("Autopilot activated.");
SFSET(snum, SHIP_F_ROBOT);
gsecs( &laststat ); /* initialize stat timer */
while ( clbStillAlive( Context.snum ) )
{
/* Make sure we still control our ship. */
if ( Ships[snum].pid != Context.pid )
break;
/* See if it's time to update the statistics. */
if ( dsecs( laststat, &now ) >= 15 )
{
conqstats( Context.snum );
laststat = now;
}
if ( isPacketWaiting(sInfo.sock) )
{
sendAck(sInfo.sock, PKT_TOCLIENT, PSEV_INFO, PERR_CANCELED,
NULL);
break;
}
c_sleep(ITER_SECONDS);
}
SFCLR(snum, SHIP_F_ROBOT);
Ships[snum].action = 0;
sendAck(sInfo.sock, PKT_TOCLIENT, PSEV_INFO, PERR_DONE,
NULL);
return;
}
void procReload(cpCommand_t *cmd)
{
if (!validPkt(CP_COMMAND, cmd))
return;
if (cmd->cmd != CPCMD_RELOAD)
return;
/* reset the packet history */
spktInitPkt();
return;
}
|