1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902
|
/*
===========================================================================
Return to Castle Wolfenstein single player GPL Source Code
Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company.
This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”).
RTCW SP Source Code 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.
RTCW SP Source Code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with RTCW SP Source Code. If not, see <http://www.gnu.org/licenses/>.
In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below.
If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
===========================================================================
*/
/*****************************************************************************
* name: be_ai_chat.c
*
* desc: bot chat AI
*
*
*****************************************************************************/
#include "../qcommon/q_shared.h"
#include "l_memory.h"
#include "l_libvar.h"
#include "l_script.h"
#include "l_precomp.h"
#include "l_struct.h"
#include "l_utils.h"
#include "l_log.h"
#include "aasfile.h"
#include "botlib.h"
#include "be_aas.h"
#include "be_aas_funcs.h"
#include "be_interface.h"
#include "be_ea.h"
#include "be_ai_chat.h"
//escape character
#define ESCAPE_CHAR 0x01 //'_'
//
// "hi ", people, " ", 0, " entered the game"
//becomes:
// "hi _rpeople_ _v0_ entered the game"
//
//match piece types
#define MT_VARIABLE 1 //variable match piece
#define MT_STRING 2 //string match piece
//reply chat key flags
#define RCKFL_AND 1 //key must be present
#define RCKFL_NOT 2 //key must be absent
#define RCKFL_NAME 4 //name of bot must be present
#define RCKFL_STRING 8 //key is a string
#define RCKFL_VARIABLES 16 //key is a match template
#define RCKFL_BOTNAMES 32 //key is a series of botnames
#define RCKFL_GENDERFEMALE 64 //bot must be female
#define RCKFL_GENDERMALE 128 //bot must be male
#define RCKFL_GENDERLESS 256 //bot must be genderless
//time to ignore a chat message after using it
#define CHATMESSAGE_RECENTTIME 20
//the actuall chat messages
typedef struct bot_chatmessage_s
{
char *chatmessage; //chat message string
float time; //last time used
struct bot_chatmessage_s *next; //next chat message in a list
} bot_chatmessage_t;
//bot chat type with chat lines
typedef struct bot_chattype_s
{
char name[MAX_CHATTYPE_NAME];
int numchatmessages;
bot_chatmessage_t *firstchatmessage;
struct bot_chattype_s *next;
} bot_chattype_t;
//bot chat lines
typedef struct bot_chat_s
{
bot_chattype_t *types;
} bot_chat_t;
//random string
typedef struct bot_randomstring_s
{
char *string;
struct bot_randomstring_s *next;
} bot_randomstring_t;
//list with random strings
typedef struct bot_randomlist_s
{
char *string;
int numstrings;
bot_randomstring_t *firstrandomstring;
struct bot_randomlist_s *next;
} bot_randomlist_t;
//synonym
typedef struct bot_synonym_s
{
char *string;
float weight;
struct bot_synonym_s *next;
} bot_synonym_t;
//list with synonyms
typedef struct bot_synonymlist_s
{
unsigned long int context;
float totalweight;
bot_synonym_t *firstsynonym;
struct bot_synonymlist_s *next;
} bot_synonymlist_t;
//fixed match string
typedef struct bot_matchstring_s
{
char *string;
struct bot_matchstring_s *next;
} bot_matchstring_t;
//piece of a match template
typedef struct bot_matchpiece_s
{
int type;
bot_matchstring_t *firststring;
int variable;
struct bot_matchpiece_s *next;
} bot_matchpiece_t;
//match template
typedef struct bot_matchtemplate_s
{
unsigned long int context;
int type;
int subtype;
bot_matchpiece_t *first;
struct bot_matchtemplate_s *next;
} bot_matchtemplate_t;
//reply chat key
typedef struct bot_replychatkey_s
{
int flags;
char *string;
bot_matchpiece_t *match;
struct bot_replychatkey_s *next;
} bot_replychatkey_t;
//reply chat
typedef struct bot_replychat_s
{
bot_replychatkey_t *keys;
float priority;
int numchatmessages;
bot_chatmessage_t *firstchatmessage;
struct bot_replychat_s *next;
} bot_replychat_t;
//string list
typedef struct bot_stringlist_s
{
char *string;
struct bot_stringlist_s *next;
} bot_stringlist_t;
//chat state of a bot
typedef struct bot_chatstate_s
{
int gender; //0=it, 1=female, 2=male
char name[32]; //name of the bot
char chatmessage[MAX_MESSAGE_SIZE];
int handle;
//the console messages visible to the bot
bot_consolemessage_t *firstmessage; //first message is the first typed message
bot_consolemessage_t *lastmessage; //last message is the last typed message, bottom of console
//number of console messages stored in the state
int numconsolemessages;
//the bot chat lines
bot_chat_t *chat;
} bot_chatstate_t;
typedef struct {
bot_chat_t *chat;
int inuse;
char filename[MAX_QPATH];
char chatname[MAX_QPATH];
} bot_ichatdata_t;
bot_ichatdata_t ichatdata[MAX_CLIENTS];
bot_chatstate_t *botchatstates[MAX_CLIENTS + 1];
//console message heap
bot_consolemessage_t *consolemessageheap = NULL;
bot_consolemessage_t *freeconsolemessages = NULL;
//list with match strings
bot_matchtemplate_t *matchtemplates = NULL;
//list with synonyms
bot_synonymlist_t *synonyms = NULL;
//list with random strings
bot_randomlist_t *randomstrings = NULL;
//reply chats
bot_replychat_t *replychats = NULL;
//========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//========================================================================
bot_chatstate_t *BotChatStateFromHandle( int handle ) {
if ( handle <= 0 || handle > MAX_CLIENTS ) {
botimport.Print( PRT_FATAL, "chat state handle %d out of range\n", handle );
return NULL;
} //end if
if ( !botchatstates[handle] ) {
botimport.Print( PRT_FATAL, "invalid chat state %d\n", handle );
return NULL;
} //end if
return botchatstates[handle];
} //end of the function BotChatStateFromHandle
//===========================================================================
// initialize the heap with unused console messages
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void InitConsoleMessageHeap( void ) {
int i, max_messages;
if ( consolemessageheap ) {
FreeMemory( consolemessageheap );
}
//
max_messages = (int) LibVarValue( "max_messages", "1024" );
consolemessageheap = (bot_consolemessage_t *) GetClearedHunkMemory( max_messages *
sizeof( bot_consolemessage_t ) );
consolemessageheap[0].prev = NULL;
consolemessageheap[0].next = &consolemessageheap[1];
for ( i = 1; i < max_messages - 1; i++ )
{
consolemessageheap[i].prev = &consolemessageheap[i - 1];
consolemessageheap[i].next = &consolemessageheap[i + 1];
} //end for
consolemessageheap[max_messages - 1].prev = &consolemessageheap[max_messages - 2];
consolemessageheap[max_messages - 1].next = NULL;
//pointer to the free console messages
freeconsolemessages = consolemessageheap;
} //end of the function InitConsoleMessageHeap
//===========================================================================
// allocate one console message from the heap
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
bot_consolemessage_t *AllocConsoleMessage( void ) {
bot_consolemessage_t *message;
message = freeconsolemessages;
if ( freeconsolemessages ) {
freeconsolemessages = freeconsolemessages->next;
}
if ( freeconsolemessages ) {
freeconsolemessages->prev = NULL;
}
return message;
} //end of the function AllocConsoleMessage
//===========================================================================
// deallocate one console message from the heap
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void FreeConsoleMessage( bot_consolemessage_t *message ) {
if ( freeconsolemessages ) {
freeconsolemessages->prev = message;
}
message->prev = NULL;
message->next = freeconsolemessages;
freeconsolemessages = message;
} //end of the function FreeConsoleMessage
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void BotRemoveConsoleMessage( int chatstate, int handle ) {
bot_consolemessage_t *m, *nextm;
bot_chatstate_t *cs;
cs = BotChatStateFromHandle( chatstate );
if ( !cs ) {
return;
}
for ( m = cs->firstmessage; m; m = nextm )
{
nextm = m->next;
if ( m->handle == handle ) {
if ( m->next ) {
m->next->prev = m->prev;
} else { cs->lastmessage = m->prev;}
if ( m->prev ) {
m->prev->next = m->next;
} else { cs->firstmessage = m->next;}
FreeConsoleMessage( m );
cs->numconsolemessages--;
break;
} //end if
} //end for
} //end of the function BotRemoveConsoleMessage
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void BotQueueConsoleMessage( int chatstate, int type, char *message ) {
bot_consolemessage_t *m;
bot_chatstate_t *cs;
cs = BotChatStateFromHandle( chatstate );
if ( !cs ) {
return;
}
m = AllocConsoleMessage();
if ( !m ) {
botimport.Print( PRT_ERROR, "empty console message heap\n" );
return;
} //end if
cs->handle++;
if ( cs->handle <= 0 || cs->handle > 8192 ) {
cs->handle = 1;
}
m->handle = cs->handle;
m->time = AAS_Time();
m->type = type;
Q_strncpyz( m->message, message, MAX_MESSAGE_SIZE );
m->next = NULL;
if ( cs->lastmessage ) {
cs->lastmessage->next = m;
m->prev = cs->lastmessage;
cs->lastmessage = m;
} //end if
else
{
cs->lastmessage = m;
cs->firstmessage = m;
m->prev = NULL;
} //end if
cs->numconsolemessages++;
} //end of the function BotQueueConsoleMessage
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
int BotNextConsoleMessage( int chatstate, bot_consolemessage_t *cm ) {
bot_chatstate_t *cs;
bot_consolemessage_t *firstmsg;
cs = BotChatStateFromHandle( chatstate );
if ( !cs ) {
return 0;
}
if ((firstmsg = cs->firstmessage)) {
cm->handle = firstmsg->handle;
cm->time = firstmsg->time;
cm->type = firstmsg->type;
Q_strncpyz(cm->message, firstmsg->message,
sizeof(cm->message));
/* We omit setting the two pointers in cm because pointer
* size in the VM differs between the size in the engine on
* 64 bit machines, which would lead to a buffer overflow if
* this functions is called from the VM. The pointers are
* of no interest to functions calling
* BotNextConsoleMessage anyways.
*/
return cm->handle;
} //end if
return 0;
} //end of the function BotConsoleMessage
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
int BotNumConsoleMessages( int chatstate ) {
bot_chatstate_t *cs;
cs = BotChatStateFromHandle( chatstate );
if ( !cs ) {
return 0;
}
return cs->numconsolemessages;
} //end of the function BotNumConsoleMessages
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
int IsWhiteSpace( char c ) {
if ( ( c >= 'a' && c <= 'z' )
|| ( c >= 'A' && c <= 'Z' )
|| ( c >= '0' && c <= '9' )
|| c == '(' || c == ')'
|| c == '?' || c == '\''
|| c == ':' || c == ','
|| c == '[' || c == ']'
|| c == '-' || c == '_'
|| c == '+' || c == '=' ) {
return qfalse;
}
return qtrue;
} //end of the function IsWhiteSpace
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void BotRemoveTildes( char *message ) {
int i;
//remove all tildes from the chat message
for ( i = 0; message[i]; i++ )
{
if ( message[i] == '~' ) {
memmove( &message[i], &message[i + 1], strlen( &message[i + 1] ) + 1 );
} //end if
} //end for
} //end of the function BotRemoveTildes
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void UnifyWhiteSpaces( char *string ) {
char *ptr, *oldptr;
for ( ptr = oldptr = string; *ptr; oldptr = ptr )
{
while ( *ptr && IsWhiteSpace( *ptr ) ) ptr++;
if ( ptr > oldptr ) {
//if not at the start and not at the end of the string
//write only one space
if ( oldptr > string && *ptr ) {
*oldptr++ = ' ';
}
//remove all other white spaces
if ( ptr > oldptr ) {
memmove( oldptr, ptr, strlen( ptr ) + 1 );
}
} //end if
while ( *ptr && !IsWhiteSpace( *ptr ) ) ptr++;
} //end while
} //end of the function UnifyWhiteSpaces
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
int StringContains( char *str1, char *str2, int casesensitive ) {
int len, i, j, index;
if ( str1 == NULL || str2 == NULL ) {
return -1;
}
len = strlen( str1 ) - strlen( str2 );
index = 0;
for ( i = 0; i <= len; i++, str1++, index++ )
{
for ( j = 0; str2[j]; j++ )
{
if ( casesensitive ) {
if ( str1[j] != str2[j] ) {
break;
}
} //end if
else
{
if ( toupper( str1[j] ) != toupper( str2[j] ) ) {
break;
}
} //end else
} //end for
if ( !str2[j] ) {
return index;
}
} //end for
return -1;
} //end of the function StringContains
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
char *StringContainsWord( char *str1, char *str2, int casesensitive ) {
int len, i, j;
len = strlen( str1 ) - strlen( str2 );
for ( i = 0; i <= len; i++, str1++ )
{
//if not at the start of the string
if ( i ) {
//skip to the start of the next word
while ( *str1 && *str1 != ' ' ) str1++;
if ( !*str1 ) {
break;
}
str1++;
} //end for
//compare the word
for ( j = 0; str2[j]; j++ )
{
if ( casesensitive ) {
if ( str1[j] != str2[j] ) {
break;
}
} //end if
else
{
if ( toupper( str1[j] ) != toupper( str2[j] ) ) {
break;
}
} //end else
} //end for
//if there was a word match
if ( !str2[j] ) {
//if the first string has an end of word
if ( !str1[j] || str1[j] == ' ' ) {
return str1;
}
} //end if
} //end for
return NULL;
} //end of the function StringContainsWord
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void StringReplaceWords( char *string, char *synonym, char *replacement ) {
char *str, *str2;
//find the synonym in the string
str = StringContainsWord( string, synonym, qfalse );
//if the synonym occured in the string
while ( str )
{
//if the synonym isn't part of the replacement which is already in the string
//usefull for abreviations
str2 = StringContainsWord( string, replacement, qfalse );
while ( str2 )
{
if ( str2 <= str && str < str2 + strlen( replacement ) ) {
break;
}
str2 = StringContainsWord( str2 + 1, replacement, qfalse );
} //end while
if ( !str2 ) {
memmove( str + strlen( replacement ), str + strlen( synonym ), strlen( str + strlen( synonym ) ) + 1 );
//append the synonum replacement
memcpy( str, replacement, strlen( replacement ) );
} //end if
//find the next synonym in the string
str = StringContainsWord( str + strlen( replacement ), synonym, qfalse );
} //end if
} //end of the function StringReplaceWords
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void BotDumpSynonymList( bot_synonymlist_t *synlist ) {
FILE *fp;
bot_synonymlist_t *syn;
bot_synonym_t *synonym;
fp = Log_FilePointer();
if ( !fp ) {
return;
}
for ( syn = synlist; syn; syn = syn->next )
{
fprintf( fp, "%ld : [", syn->context );
for ( synonym = syn->firstsynonym; synonym; synonym = synonym->next )
{
fprintf( fp, "(\"%s\", %1.2f)", synonym->string, synonym->weight );
if ( synonym->next ) {
fprintf( fp, ", " );
}
} //end for
fprintf( fp, "]\n" );
} //end for
} //end of the function BotDumpSynonymList
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
bot_synonymlist_t *BotLoadSynonyms( char *filename ) {
int pass, size, contextlevel, numsynonyms;
unsigned long int context, contextstack[32];
char *ptr = NULL;
source_t *source;
token_t token;
bot_synonymlist_t *synlist, *lastsyn, *syn;
bot_synonym_t *synonym, *lastsynonym;
size = 0;
synlist = NULL; //make compiler happy
syn = NULL; //make compiler happy
synonym = NULL; //make compiler happy
//the synonyms are parsed in two phases
for ( pass = 0; pass < 2; pass++ )
{
//
if ( pass && size ) {
ptr = (char *) GetClearedHunkMemory( size );
}
//
source = LoadSourceFile( filename );
if ( !source ) {
botimport.Print( PRT_ERROR, "counldn't load %s\n", filename );
return NULL;
} //end if
//
context = 0;
contextlevel = 0;
synlist = NULL; //list synonyms
lastsyn = NULL; //last synonym in the list
//
while ( PC_ReadToken( source, &token ) )
{
if ( token.type == TT_NUMBER ) {
context |= token.intvalue;
contextstack[contextlevel] = token.intvalue;
contextlevel++;
if ( contextlevel >= 32 ) {
SourceError( source, "more than 32 context levels" );
FreeSource( source );
return NULL;
} //end if
if ( !PC_ExpectTokenString( source, "{" ) ) {
FreeSource( source );
return NULL;
} //end if
} //end if
else if ( token.type == TT_PUNCTUATION ) {
if ( !strcmp( token.string, "}" ) ) {
contextlevel--;
if ( contextlevel < 0 ) {
SourceError( source, "too many }" );
FreeSource( source );
return NULL;
} //end if
context &= ~contextstack[contextlevel];
} //end if
else if ( !strcmp( token.string, "[" ) ) {
size += sizeof( bot_synonymlist_t );
if ( pass && ptr ) {
syn = (bot_synonymlist_t *) ptr;
ptr += sizeof( bot_synonymlist_t );
syn->context = context;
syn->firstsynonym = NULL;
syn->next = NULL;
if ( lastsyn ) {
lastsyn->next = syn;
} else { synlist = syn;}
lastsyn = syn;
} //end if
numsynonyms = 0;
lastsynonym = NULL;
while ( 1 )
{
size_t len;
if ( !PC_ExpectTokenString( source, "(" ) ||
!PC_ExpectTokenType( source, TT_STRING, 0, &token ) ) {
FreeSource( source );
return NULL;
} //end if
StripDoubleQuotes( token.string );
if ( strlen( token.string ) <= 0 ) {
SourceError( source, "empty string" );
FreeSource( source );
return NULL;
} //end if
len = strlen(token.string) + 1;
len = PAD(len, sizeof(long));
size += sizeof(bot_synonym_t) + len;
if ( pass && ptr ) {
synonym = (bot_synonym_t *) ptr;
ptr += sizeof( bot_synonym_t );
synonym->string = ptr;
ptr += len;
strcpy( synonym->string, token.string );
//
if ( lastsynonym ) {
lastsynonym->next = synonym;
} else { syn->firstsynonym = synonym;}
lastsynonym = synonym;
} //end if
numsynonyms++;
if ( !PC_ExpectTokenString( source, "," ) ||
!PC_ExpectTokenType( source, TT_NUMBER, 0, &token ) ||
!PC_ExpectTokenString( source, ")" ) ) {
FreeSource( source );
return NULL;
} //end if
if ( pass && ptr ) {
synonym->weight = token.floatvalue;
syn->totalweight += synonym->weight;
} //end if
if ( PC_CheckTokenString( source, "]" ) ) {
break;
}
if ( !PC_ExpectTokenString( source, "," ) ) {
FreeSource( source );
return NULL;
} //end if
} //end while
if ( numsynonyms < 2 ) {
SourceError( source, "synonym must have at least two entries" );
FreeSource( source );
return NULL;
} //end if
} //end else
else
{
SourceError( source, "unexpected %s", token.string );
FreeSource( source );
return NULL;
} //end if
} //end else if
} //end while
//
FreeSource( source );
//
if ( contextlevel > 0 ) {
SourceError( source, "missing }" );
return NULL;
} //end if
} //end for
botimport.Print( PRT_MESSAGE, "loaded %s\n", filename );
//
//BotDumpSynonymList(synlist);
//
return synlist;
} //end of the function BotLoadSynonyms
//===========================================================================
// replace all the synonyms in the string
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void BotReplaceSynonyms( char *string, unsigned long int context ) {
bot_synonymlist_t *syn;
bot_synonym_t *synonym;
for ( syn = synonyms; syn; syn = syn->next )
{
if ( !( syn->context & context ) ) {
continue;
}
for ( synonym = syn->firstsynonym->next; synonym; synonym = synonym->next )
{
StringReplaceWords( string, synonym->string, syn->firstsynonym->string );
} //end for
} //end for
} //end of the function BotReplaceSynonyms
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void BotReplaceWeightedSynonyms( char *string, unsigned long int context ) {
bot_synonymlist_t *syn;
bot_synonym_t *synonym, *replacement;
float weight, curweight;
for ( syn = synonyms; syn; syn = syn->next )
{
if ( !( syn->context & context ) ) {
continue;
}
//choose a weighted random replacement synonym
weight = random() * syn->totalweight;
if ( !weight ) {
continue;
}
curweight = 0;
for ( replacement = syn->firstsynonym; replacement; replacement = replacement->next )
{
curweight += replacement->weight;
if ( weight < curweight ) {
break;
}
} //end for
if ( !replacement ) {
continue;
}
//replace all synonyms with the replacement
for ( synonym = syn->firstsynonym; synonym; synonym = synonym->next )
{
if ( synonym == replacement ) {
continue;
}
StringReplaceWords( string, synonym->string, replacement->string );
} //end for
} //end for
} //end of the function BotReplaceWeightedSynonyms
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void BotReplaceReplySynonyms( char *string, unsigned long int context ) {
char *str1, *str2, *replacement;
bot_synonymlist_t *syn;
bot_synonym_t *synonym;
for ( str1 = string; *str1; )
{
//go to the start of the next word
while ( *str1 && *str1 <= ' ' ) str1++;
if ( !*str1 ) {
break;
}
//
for ( syn = synonyms; syn; syn = syn->next )
{
if ( !( syn->context & context ) ) {
continue;
}
for ( synonym = syn->firstsynonym->next; synonym; synonym = synonym->next )
{
//if the synonym is not at the front of the string continue
str2 = StringContainsWord( str1, synonym->string, qfalse );
if ( !str2 || str2 != str1 ) {
continue;
}
//
replacement = syn->firstsynonym->string;
//if the replacement IS in front of the string continue
str2 = StringContainsWord( str1, replacement, qfalse );
if ( str2 && str2 == str1 ) {
continue;
}
//
memmove( str1 + strlen( replacement ), str1 + strlen( synonym->string ),
strlen( str1 + strlen( synonym->string ) ) + 1 );
//append the synonum replacement
memcpy( str1, replacement, strlen( replacement ) );
//
break;
} //end for
//if a synonym has been replaced
if ( synonym ) {
break;
}
} //end for
//skip over this word
while ( *str1 && *str1 > ' ' ) str1++;
if ( !*str1 ) {
break;
}
} //end while
} //end of the function BotReplaceReplySynonyms
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
int BotLoadChatMessage( source_t *source, char *chatmessagestring ) {
char *ptr;
token_t token;
ptr = chatmessagestring;
*ptr = 0;
//
while ( 1 )
{
if ( !PC_ExpectAnyToken( source, &token ) ) {
return qfalse;
}
//fixed string
if ( token.type == TT_STRING ) {
StripDoubleQuotes( token.string );
if ( strlen( ptr ) + strlen( token.string ) + 1 > MAX_MESSAGE_SIZE ) {
SourceError( source, "chat message too long" );
return qfalse;
} //end if
strcat( ptr, token.string );
} //end else if
//variable string
else if ( token.type == TT_NUMBER && ( token.subtype & TT_INTEGER ) ) {
if ( strlen( ptr ) + 7 > MAX_MESSAGE_SIZE ) {
SourceError( source, "chat message too long" );
return qfalse;
} //end if
sprintf( &ptr[strlen( ptr )], "%cv%ld%c", ESCAPE_CHAR, token.intvalue, ESCAPE_CHAR );
} //end if
//random string
else if ( token.type == TT_NAME ) {
if ( strlen( ptr ) + 7 > MAX_MESSAGE_SIZE ) {
SourceError( source, "chat message too long" );
return qfalse;
} //end if
sprintf( &ptr[strlen( ptr )], "%cr%s%c", ESCAPE_CHAR, token.string, ESCAPE_CHAR );
} //end else if
else
{
SourceError( source, "unknown message component %s", token.string );
return qfalse;
} //end else
if ( PC_CheckTokenString( source, ";" ) ) {
break;
}
if ( !PC_ExpectTokenString( source, "," ) ) {
return qfalse;
}
} //end while
//
return qtrue;
} //end of the function BotLoadChatMessage
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void BotDumpRandomStringList( bot_randomlist_t *randomlist ) {
FILE *fp;
bot_randomlist_t *random;
bot_randomstring_t *rs;
fp = Log_FilePointer();
if ( !fp ) {
return;
}
for ( random = randomlist; random; random = random->next )
{
fprintf( fp, "%s = {", random->string );
for ( rs = random->firstrandomstring; rs; rs = rs->next )
{
fprintf( fp, "\"%s\"", rs->string );
if ( rs->next ) {
fprintf( fp, ", " );
} else { fprintf( fp, "}\n" );}
} //end for
} //end for
} //end of the function BotDumpRandomStringList
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
bot_randomlist_t *BotLoadRandomStrings( char *filename ) {
int pass, size;
char *ptr = NULL, chatmessagestring[MAX_MESSAGE_SIZE];
source_t *source;
token_t token;
bot_randomlist_t *randomlist, *lastrandom, *random;
bot_randomstring_t *randomstring;
#ifdef DEBUG
int starttime = Sys_MilliSeconds();
#endif //DEBUG
size = 0;
randomlist = NULL;
random = NULL;
//the synonyms are parsed in two phases
for ( pass = 0; pass < 2; pass++ )
{
//
if ( pass && size ) {
ptr = (char *) GetClearedHunkMemory( size );
}
//
source = LoadSourceFile( filename );
if ( !source ) {
botimport.Print( PRT_ERROR, "counldn't load %s\n", filename );
return NULL;
} //end if
//
randomlist = NULL; //list
lastrandom = NULL; //last
//
while ( PC_ReadToken( source, &token ) )
{
size_t len;
if ( token.type != TT_NAME ) {
SourceError( source, "unknown random %s", token.string );
FreeSource( source );
return NULL;
} //end if
len = strlen(token.string) + 1;
len = PAD(len, sizeof(long));
size += sizeof(bot_randomlist_t) + len;
if ( pass && ptr ) {
random = (bot_randomlist_t *) ptr;
ptr += sizeof( bot_randomlist_t );
random->string = ptr;
ptr += len;
strcpy( random->string, token.string );
random->firstrandomstring = NULL;
random->numstrings = 0;
//
if ( lastrandom ) {
lastrandom->next = random;
} else { randomlist = random;}
lastrandom = random;
} //end if
if ( !PC_ExpectTokenString( source, "=" ) ||
!PC_ExpectTokenString( source, "{" ) ) {
FreeSource( source );
return NULL;
} //end if
while ( !PC_CheckTokenString( source, "}" ) )
{
if ( !BotLoadChatMessage( source, chatmessagestring ) ) {
FreeSource( source );
return NULL;
} //end if
len = strlen(chatmessagestring) + 1;
len = PAD(len, sizeof(long));
size += sizeof(bot_randomstring_t) + len;
if ( pass && ptr ) {
randomstring = (bot_randomstring_t *) ptr;
ptr += sizeof( bot_randomstring_t );
randomstring->string = ptr;
ptr += len;
strcpy( randomstring->string, chatmessagestring );
//
random->numstrings++;
randomstring->next = random->firstrandomstring;
random->firstrandomstring = randomstring;
} //end if
} //end while
} //end while
//free the source after one pass
FreeSource( source );
} //end for
botimport.Print( PRT_MESSAGE, "loaded %s\n", filename );
//
#ifdef DEBUG
botimport.Print( PRT_MESSAGE, "random strings %d msec\n", Sys_MilliSeconds() - starttime );
//BotDumpRandomStringList(randomlist);
#endif //DEBUG
//
return randomlist;
} //end of the function BotLoadRandomStrings
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
char *RandomString( char *name ) {
bot_randomlist_t *random;
bot_randomstring_t *rs;
int i;
for ( random = randomstrings; random; random = random->next )
{
if ( !strcmp( random->string, name ) ) {
i = random() * random->numstrings;
for ( rs = random->firstrandomstring; rs; rs = rs->next )
{
if ( --i < 0 ) {
break;
}
} //end for
if ( rs ) {
return rs->string;
} //end if
} //end for
} //end for
return NULL;
} //end of the function RandomString
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void BotDumpMatchTemplates( bot_matchtemplate_t *matches ) {
FILE *fp;
bot_matchtemplate_t *mt;
bot_matchpiece_t *mp;
bot_matchstring_t *ms;
fp = Log_FilePointer();
if ( !fp ) {
return;
}
for ( mt = matches; mt; mt = mt->next )
{
//fprintf(fp, "%8d { ");
for ( mp = mt->first; mp; mp = mp->next )
{
if ( mp->type == MT_STRING ) {
for ( ms = mp->firststring; ms; ms = ms->next )
{
fprintf( fp, "\"%s\"", ms->string );
if ( ms->next ) {
fprintf( fp, "|" );
}
} //end for
} //end if
else if ( mp->type == MT_VARIABLE ) {
fprintf( fp, "%d", mp->variable );
} //end else if
if ( mp->next ) {
fprintf( fp, ", " );
}
} //end for
fprintf( fp, " = (%d, %d);}\n", mt->type, mt->subtype );
} //end for
} //end of the function BotDumpMatchTemplates
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void BotFreeMatchPieces( bot_matchpiece_t *matchpieces ) {
bot_matchpiece_t *mp, *nextmp;
bot_matchstring_t *ms, *nextms;
for ( mp = matchpieces; mp; mp = nextmp )
{
nextmp = mp->next;
if ( mp->type == MT_STRING ) {
for ( ms = mp->firststring; ms; ms = nextms )
{
nextms = ms->next;
FreeMemory( ms );
} //end for
} //end if
FreeMemory( mp );
} //end for
} //end of the function BotFreeMatchPieces
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
bot_matchpiece_t *BotLoadMatchPieces( source_t *source, char *endtoken ) {
int lastwasvariable, emptystring;
token_t token;
bot_matchpiece_t *matchpiece, *firstpiece, *lastpiece;
bot_matchstring_t *matchstring, *lastmatchstring;
firstpiece = NULL;
lastpiece = NULL;
//
lastwasvariable = qfalse;
//
while ( PC_ReadToken( source, &token ) )
{
if ( token.type == TT_NUMBER && ( token.subtype & TT_INTEGER ) ) {
if (token.intvalue >= MAX_MATCHVARIABLES) {
SourceError( source, "can't have more than %d match variables", MAX_MATCHVARIABLES );
FreeSource( source );
BotFreeMatchPieces( firstpiece );
return NULL;
} //end if
if ( lastwasvariable ) {
SourceError( source, "not allowed to have adjacent variables" );
FreeSource( source );
BotFreeMatchPieces( firstpiece );
return NULL;
} //end if
lastwasvariable = qtrue;
//
matchpiece = (bot_matchpiece_t *) GetClearedHunkMemory( sizeof( bot_matchpiece_t ) );
matchpiece->type = MT_VARIABLE;
matchpiece->variable = token.intvalue;
matchpiece->next = NULL;
if ( lastpiece ) {
lastpiece->next = matchpiece;
} else { firstpiece = matchpiece;}
lastpiece = matchpiece;
} //end if
else if ( token.type == TT_STRING ) {
//
matchpiece = (bot_matchpiece_t *) GetClearedHunkMemory( sizeof( bot_matchpiece_t ) );
matchpiece->firststring = NULL;
matchpiece->type = MT_STRING;
matchpiece->variable = 0;
matchpiece->next = NULL;
if ( lastpiece ) {
lastpiece->next = matchpiece;
} else { firstpiece = matchpiece;}
lastpiece = matchpiece;
//
lastmatchstring = NULL;
emptystring = qfalse;
//
do
{
if ( matchpiece->firststring ) {
if ( !PC_ExpectTokenType( source, TT_STRING, 0, &token ) ) {
FreeSource( source );
BotFreeMatchPieces( firstpiece );
return NULL;
} //end if
} //end if
StripDoubleQuotes( token.string );
matchstring = (bot_matchstring_t *) GetClearedHunkMemory( sizeof( bot_matchstring_t ) + strlen( token.string ) + 1 );
matchstring->string = (char *) matchstring + sizeof( bot_matchstring_t );
strcpy( matchstring->string, token.string );
if ( !strlen( token.string ) ) {
emptystring = qtrue;
}
matchstring->next = NULL;
if ( lastmatchstring ) {
lastmatchstring->next = matchstring;
} else { matchpiece->firststring = matchstring;}
lastmatchstring = matchstring;
} while ( PC_CheckTokenString( source, "|" ) );
//if there was no empty string found
if ( !emptystring ) {
lastwasvariable = qfalse;
}
} //end if
else
{
SourceError( source, "invalid token %s", token.string );
FreeSource( source );
BotFreeMatchPieces( firstpiece );
return NULL;
} //end else
if ( PC_CheckTokenString( source, endtoken ) ) {
break;
}
if ( !PC_ExpectTokenString( source, "," ) ) {
FreeSource( source );
BotFreeMatchPieces( firstpiece );
return NULL;
} //end if
} //end while
return firstpiece;
} //end of the function BotLoadMatchPieces
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void BotFreeMatchTemplates( bot_matchtemplate_t *mt ) {
bot_matchtemplate_t *nextmt;
for (; mt; mt = nextmt )
{
nextmt = mt->next;
BotFreeMatchPieces( mt->first );
FreeMemory( mt );
} //end for
} //end of the function BotFreeMatchTemplates
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
bot_matchtemplate_t *BotLoadMatchTemplates( char *matchfile ) {
source_t *source;
token_t token;
bot_matchtemplate_t *matchtemplate, *matches, *lastmatch;
unsigned long int context;
source = LoadSourceFile( matchfile );
if ( !source ) {
botimport.Print( PRT_ERROR, "counldn't load %s\n", matchfile );
return NULL;
} //end if
//
matches = NULL; //list with matches
lastmatch = NULL; //last match in the list
while ( PC_ReadToken( source, &token ) )
{
if ( token.type != TT_NUMBER || !( token.subtype & TT_INTEGER ) ) {
SourceError( source, "expected integer, found %s", token.string );
BotFreeMatchTemplates( matches );
FreeSource( source );
return NULL;
} //end if
//the context
context = token.intvalue;
//
if ( !PC_ExpectTokenString( source, "{" ) ) {
BotFreeMatchTemplates( matches );
FreeSource( source );
return NULL;
} //end if
//
while ( PC_ReadToken( source, &token ) )
{
if ( !strcmp( token.string, "}" ) ) {
break;
}
//
PC_UnreadLastToken( source );
//
matchtemplate = (bot_matchtemplate_t *) GetClearedHunkMemory( sizeof( bot_matchtemplate_t ) );
matchtemplate->context = context;
matchtemplate->next = NULL;
//add the match template to the list
if ( lastmatch ) {
lastmatch->next = matchtemplate;
} else { matches = matchtemplate;}
lastmatch = matchtemplate;
//load the match template
matchtemplate->first = BotLoadMatchPieces( source, "=" );
if ( !matchtemplate->first ) {
BotFreeMatchTemplates( matches );
return NULL;
} //end if
//read the match type
if ( !PC_ExpectTokenString( source, "(" ) ||
!PC_ExpectTokenType( source, TT_NUMBER, TT_INTEGER, &token ) ) {
BotFreeMatchTemplates( matches );
FreeSource( source );
return NULL;
} //end if
matchtemplate->type = token.intvalue;
//read the match subtype
if ( !PC_ExpectTokenString( source, "," ) ||
!PC_ExpectTokenType( source, TT_NUMBER, TT_INTEGER, &token ) ) {
BotFreeMatchTemplates( matches );
FreeSource( source );
return NULL;
} //end if
matchtemplate->subtype = token.intvalue;
//read trailing punctuations
if ( !PC_ExpectTokenString( source, ")" ) ||
!PC_ExpectTokenString( source, ";" ) ) {
BotFreeMatchTemplates( matches );
FreeSource( source );
return NULL;
} //end if
} //end while
} //end while
//free the source
FreeSource( source );
botimport.Print( PRT_MESSAGE, "loaded %s\n", matchfile );
//
//BotDumpMatchTemplates(matches);
//
return matches;
} //end of the function BotLoadMatchTemplates
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
int StringsMatch( bot_matchpiece_t *pieces, bot_match_t *match ) {
int lastvariable, index;
char *strptr, *newstrptr;
bot_matchpiece_t *mp;
bot_matchstring_t *ms;
//no last variable
lastvariable = -1;
//pointer to the string to compare the match string with
strptr = match->string;
//Log_Write("match: %s", strptr);
//compare the string with the current match string
for ( mp = pieces; mp; mp = mp->next )
{
//if it is a piece of string
if ( mp->type == MT_STRING ) {
newstrptr = NULL;
for ( ms = mp->firststring; ms; ms = ms->next )
{
if ( !strlen( ms->string ) ) {
newstrptr = strptr;
break;
} //end if
//Log_Write("MT_STRING: %s", mp->string);
index = StringContains( strptr, ms->string, qfalse );
if ( index >= 0 ) {
newstrptr = strptr + index;
if ( lastvariable >= 0 ) {
match->variables[lastvariable].length =
newstrptr - match->variables[lastvariable].ptr;
lastvariable = -1;
break;
} //end if
else if ( index == 0 ) {
break;
} //end else
newstrptr = NULL;
} //end if
} //end for
if ( !newstrptr ) {
return qfalse;
}
strptr = newstrptr + strlen( ms->string );
} //end if
//if it is a variable piece of string
else if ( mp->type == MT_VARIABLE ) {
//Log_Write("MT_VARIABLE");
match->variables[mp->variable].ptr = strptr;
lastvariable = mp->variable;
} //end else if
} //end for
//if a match was found
if ( !mp && ( lastvariable >= 0 || !strlen( strptr ) ) ) {
//if the last piece was a variable string
if ( lastvariable >= 0 ) {
match->variables[lastvariable].length = strlen( match->variables[lastvariable].ptr );
} //end if
return qtrue;
} //end if
return qfalse;
} //end of the function StringsMatch
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
int BotFindMatch( char *str, bot_match_t *match, unsigned long int context ) {
int i;
bot_matchtemplate_t *ms;
Q_strncpyz( match->string, str, MAX_MESSAGE_SIZE );
//remove any trailing enters
while ( strlen( match->string ) &&
match->string[strlen( match->string ) - 1] == '\n' )
{
match->string[strlen( match->string ) - 1] = '\0';
} //end while
//compare the string with all the match strings
for ( ms = matchtemplates; ms; ms = ms->next )
{
if ( !( ms->context & context ) ) {
continue;
}
//reset the match variable pointers
for ( i = 0; i < MAX_MATCHVARIABLES; i++ ) match->variables[i].ptr = NULL;
//
if ( StringsMatch( ms->first, match ) ) {
match->type = ms->type;
match->subtype = ms->subtype;
return qtrue;
} //end if
} //end for
return qfalse;
} //end of the function BotFindMatch
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void BotMatchVariable( bot_match_t *match, int variable, char *buf, int size ) {
if ( variable < 0 || variable >= MAX_MATCHVARIABLES ) {
botimport.Print( PRT_FATAL, "BotMatchVariable: variable out of range\n" );
strcpy( buf, "" );
return;
} //end if
if ( match->variables[variable].ptr ) {
if ( match->variables[variable].length < size ) {
size = match->variables[variable].length + 1;
}
strncpy( buf, match->variables[variable].ptr, size - 1 );
buf[size - 1] = '\0';
} //end if
else
{
strcpy( buf, "" );
} //end else
} //end of the function BotMatchVariable
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
bot_stringlist_t *BotFindStringInList( bot_stringlist_t *list, char *string ) {
bot_stringlist_t *s;
for ( s = list; s; s = s->next )
{
if ( !strcmp( s->string, string ) ) {
return s;
}
} //end for
return NULL;
} //end of the function BotFindStringInList
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
bot_stringlist_t *BotCheckChatMessageIntegrety( char *message, bot_stringlist_t *stringlist ) {
int i;
char *msgptr;
char temp[MAX_MESSAGE_SIZE];
bot_stringlist_t *s;
msgptr = message;
//
while ( *msgptr )
{
if ( *msgptr == ESCAPE_CHAR ) {
msgptr++;
switch ( *msgptr )
{
case 'v': //variable
{
//step over the 'v'
msgptr++;
while ( *msgptr && *msgptr != ESCAPE_CHAR ) msgptr++;
//step over the trailing escape char
if ( *msgptr ) {
msgptr++;
}
break;
} //end case
case 'r': //random
{
//step over the 'r'
msgptr++;
for ( i = 0; ( *msgptr && *msgptr != ESCAPE_CHAR ); i++ )
{
temp[i] = *msgptr++;
} //end while
temp[i] = '\0';
//step over the trailing escape char
if ( *msgptr ) {
msgptr++;
}
//find the random keyword
if ( !RandomString( temp ) ) {
if ( !BotFindStringInList( stringlist, temp ) ) {
Log_Write( "%s = {\"%s\"} //MISSING RANDOM\r\n", temp, temp );
s = GetClearedMemory( sizeof( bot_stringlist_t ) + strlen( temp ) + 1 );
s->string = (char *) s + sizeof( bot_stringlist_t );
strcpy( s->string, temp );
s->next = stringlist;
stringlist = s;
} //end if
} //end if
break;
} //end case
default:
{
botimport.Print( PRT_FATAL, "BotCheckChatMessageIntegrety: message \"%s\" invalid escape char\n", message );
break;
} //end default
} //end switch
} //end if
else
{
msgptr++;
} //end else
} //end while
return stringlist;
} //end of the function BotCheckChatMessageIntegrety
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void BotCheckReplyChatIntegrety( bot_replychat_t *replychat ) {
bot_replychat_t *rp;
bot_chatmessage_t *cm;
bot_stringlist_t *stringlist, *s, *nexts;
stringlist = NULL;
for ( rp = replychat; rp; rp = rp->next )
{
for ( cm = rp->firstchatmessage; cm; cm = cm->next )
{
stringlist = BotCheckChatMessageIntegrety( cm->chatmessage, stringlist );
} //end for
} //end for
for ( s = stringlist; s; s = nexts )
{
nexts = s->next;
FreeMemory( s );
} //end for
} //end of the function BotCheckReplyChatIntegrety
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void BotCheckInitialChatIntegrety( bot_chat_t *chat ) {
bot_chattype_t *t;
bot_chatmessage_t *cm;
bot_stringlist_t *stringlist, *s, *nexts;
stringlist = NULL;
for ( t = chat->types; t; t = t->next )
{
for ( cm = t->firstchatmessage; cm; cm = cm->next )
{
stringlist = BotCheckChatMessageIntegrety( cm->chatmessage, stringlist );
} //end for
} //end for
for ( s = stringlist; s; s = nexts )
{
nexts = s->next;
FreeMemory( s );
} //end for
} //end of the function BotCheckInitialChatIntegrety
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void BotDumpReplyChat( bot_replychat_t *replychat ) {
FILE *fp;
bot_replychat_t *rp;
bot_replychatkey_t *key;
bot_chatmessage_t *cm;
bot_matchpiece_t *mp;
fp = Log_FilePointer();
if ( !fp ) {
return;
}
fprintf( fp, "BotDumpReplyChat:\n" );
for ( rp = replychat; rp; rp = rp->next )
{
fprintf( fp, "[" );
for ( key = rp->keys; key; key = key->next )
{
if ( key->flags & RCKFL_AND ) {
fprintf( fp, "&" );
} else if ( key->flags & RCKFL_NOT ) {
fprintf( fp, "!" );
}
//
if ( key->flags & RCKFL_NAME ) {
fprintf( fp, "name" );
} else if ( key->flags & RCKFL_GENDERFEMALE ) {
fprintf( fp, "female" );
} else if ( key->flags & RCKFL_GENDERMALE ) {
fprintf( fp, "male" );
} else if ( key->flags & RCKFL_GENDERLESS ) {
fprintf( fp, "it" );
} else if ( key->flags & RCKFL_VARIABLES ) {
fprintf( fp, "(" );
for ( mp = key->match; mp; mp = mp->next )
{
if ( mp->type == MT_STRING ) {
fprintf( fp, "\"%s\"", mp->firststring->string );
} else { fprintf( fp, "%d", mp->variable );}
if ( mp->next ) {
fprintf( fp, ", " );
}
} //end for
fprintf( fp, ")" );
} //end if
else if ( key->flags & RCKFL_STRING ) {
fprintf( fp, "\"%s\"", key->string );
} //end if
if ( key->next ) {
fprintf( fp, ", " );
} else { fprintf( fp, "] = %1.0f\n", rp->priority );}
} //end for
fprintf( fp, "{\n" );
for ( cm = rp->firstchatmessage; cm; cm = cm->next )
{
fprintf( fp, "\t\"%s\";\n", cm->chatmessage );
} //end for
fprintf( fp, "}\n" );
} //end for
} //end of the function BotDumpReplyChat
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void BotFreeReplyChat( bot_replychat_t *replychat ) {
bot_replychat_t *rp, *nextrp;
bot_replychatkey_t *key, *nextkey;
bot_chatmessage_t *cm, *nextcm;
for ( rp = replychat; rp; rp = nextrp )
{
nextrp = rp->next;
for ( key = rp->keys; key; key = nextkey )
{
nextkey = key->next;
if ( key->match ) {
BotFreeMatchPieces( key->match );
}
if ( key->string ) {
FreeMemory( key->string );
}
FreeMemory( key );
} //end for
for ( cm = rp->firstchatmessage; cm; cm = nextcm )
{
nextcm = cm->next;
FreeMemory( cm );
} //end for
FreeMemory( rp );
} //end for
} //end of the function BotFreeReplyChat
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
bot_replychat_t *BotLoadReplyChat( char *filename ) {
char chatmessagestring[MAX_MESSAGE_SIZE];
char namebuffer[MAX_MESSAGE_SIZE];
source_t *source;
token_t token;
bot_chatmessage_t *chatmessage = NULL;
bot_replychat_t *replychat, *replychatlist;
bot_replychatkey_t *key;
source = LoadSourceFile( filename );
if ( !source ) {
botimport.Print( PRT_ERROR, "counldn't load %s\n", filename );
return NULL;
} //end if
//
replychatlist = NULL;
//
while ( PC_ReadToken( source, &token ) )
{
if ( strcmp( token.string, "[" ) ) {
SourceError( source, "expected [, found %s", token.string );
BotFreeReplyChat( replychatlist );
FreeSource( source );
return NULL;
} //end if
//
replychat = GetClearedHunkMemory( sizeof( bot_replychat_t ) );
replychat->keys = NULL;
replychat->next = replychatlist;
replychatlist = replychat;
//read the keys, there must be at least one key
do
{
//allocate a key
key = (bot_replychatkey_t *) GetClearedHunkMemory( sizeof( bot_replychatkey_t ) );
key->flags = 0;
key->string = NULL;
key->match = NULL;
key->next = replychat->keys;
replychat->keys = key;
//check for MUST BE PRESENT and MUST BE ABSENT keys
if ( PC_CheckTokenString( source, "&" ) ) {
key->flags |= RCKFL_AND;
} else if ( PC_CheckTokenString( source, "!" ) ) {
key->flags |= RCKFL_NOT;
}
//special keys
if ( PC_CheckTokenString( source, "name" ) ) {
key->flags |= RCKFL_NAME;
} else if ( PC_CheckTokenString( source, "female" ) ) {
key->flags |= RCKFL_GENDERFEMALE;
} else if ( PC_CheckTokenString( source, "male" ) ) {
key->flags |= RCKFL_GENDERMALE;
} else if ( PC_CheckTokenString( source, "it" ) ) {
key->flags |= RCKFL_GENDERLESS;
} else if ( PC_CheckTokenString( source, "(" ) ) { //match key
key->flags |= RCKFL_VARIABLES;
key->match = BotLoadMatchPieces( source, ")" );
if ( !key->match ) {
BotFreeReplyChat( replychatlist );
return NULL;
} //end if
} //end else if
else if ( PC_CheckTokenString( source, "<" ) ) { //bot names
key->flags |= RCKFL_BOTNAMES;
strcpy( namebuffer, "" );
do
{
if ( !PC_ExpectTokenType( source, TT_STRING, 0, &token ) ) {
BotFreeReplyChat( replychatlist );
FreeSource( source );
return NULL;
} //end if
StripDoubleQuotes( token.string );
if ( strlen( namebuffer ) ) {
strcat( namebuffer, "\\" );
}
strcat( namebuffer, token.string );
} while ( PC_CheckTokenString( source, "," ) );
if ( !PC_ExpectTokenString( source, ">" ) ) {
BotFreeReplyChat( replychatlist );
FreeSource( source );
return NULL;
} //end if
key->string = (char *) GetClearedHunkMemory( strlen( namebuffer ) + 1 );
strcpy( key->string, namebuffer );
} //end else if
else //normal string key
{
key->flags |= RCKFL_STRING;
if ( !PC_ExpectTokenType( source, TT_STRING, 0, &token ) ) {
BotFreeReplyChat( replychatlist );
FreeSource( source );
return NULL;
} //end if
StripDoubleQuotes( token.string );
key->string = (char *) GetClearedHunkMemory( strlen( token.string ) + 1 );
strcpy( key->string, token.string );
} //end else
//
PC_CheckTokenString( source, "," );
} while ( !PC_CheckTokenString( source, "]" ) );
//read the = sign and the priority
if ( !PC_ExpectTokenString( source, "=" ) ||
!PC_ExpectTokenType( source, TT_NUMBER, 0, &token ) ) {
BotFreeReplyChat( replychatlist );
FreeSource( source );
return NULL;
} //end if
replychat->priority = token.floatvalue;
//read the leading {
if ( !PC_ExpectTokenString( source, "{" ) ) {
BotFreeReplyChat( replychatlist );
FreeSource( source );
return NULL;
} //end if
replychat->numchatmessages = 0;
//while the trailing } is not found
while ( !PC_CheckTokenString( source, "}" ) )
{
if ( !BotLoadChatMessage( source, chatmessagestring ) ) {
BotFreeReplyChat( replychatlist );
FreeSource( source );
return NULL;
} //end if
chatmessage = (bot_chatmessage_t *) GetClearedHunkMemory( sizeof( bot_chatmessage_t ) + strlen( chatmessagestring ) + 1 );
chatmessage->chatmessage = (char *) chatmessage + sizeof( bot_chatmessage_t );
strcpy( chatmessage->chatmessage, chatmessagestring );
chatmessage->time = -2 * CHATMESSAGE_RECENTTIME;
chatmessage->next = replychat->firstchatmessage;
//add the chat message to the reply chat
replychat->firstchatmessage = chatmessage;
replychat->numchatmessages++;
} //end while
} //end while
FreeSource( source );
botimport.Print( PRT_MESSAGE, "loaded %s\n", filename );
//
//BotDumpReplyChat(replychatlist);
if ( botDeveloper ) {
BotCheckReplyChatIntegrety( replychatlist );
} //end if
//
if ( !replychatlist ) {
botimport.Print( PRT_MESSAGE, "no rchats\n" );
}
//
return replychatlist;
} //end of the function BotLoadReplyChat
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void BotDumpInitialChat( bot_chat_t *chat ) {
bot_chattype_t *t;
bot_chatmessage_t *m;
Log_Write( "{" );
for ( t = chat->types; t; t = t->next )
{
Log_Write( " type \"%s\"", t->name );
Log_Write( " {" );
Log_Write( " numchatmessages = %d", t->numchatmessages );
for ( m = t->firstchatmessage; m; m = m->next )
{
Log_Write( " \"%s\"", m->chatmessage );
} //end for
Log_Write( " }" );
} //end for
Log_Write( "}" );
} //end of the function BotDumpInitialChat
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
bot_chat_t *BotLoadInitialChat( char *chatfile, char *chatname ) {
int pass, foundchat, indent, size;
char *ptr = NULL;
char chatmessagestring[MAX_MESSAGE_SIZE];
source_t *source;
token_t token;
bot_chat_t *chat = NULL;
bot_chattype_t *chattype = NULL;
bot_chatmessage_t *chatmessage = NULL;
#ifdef DEBUG
int starttime;
starttime = Sys_MilliSeconds();
#endif //DEBUG
//
size = 0;
foundchat = qfalse;
//a bot chat is parsed in two phases
for ( pass = 0; pass < 2; pass++ )
{
//allocate memory
if ( pass && size ) {
ptr = (char *) GetClearedMemory( size );
}
//load the source file
source = LoadSourceFile( chatfile );
if ( !source ) {
botimport.Print( PRT_ERROR, "counldn't load %s\n", chatfile );
return NULL;
} //end if
//chat structure
if ( pass ) {
chat = (bot_chat_t *) ptr;
ptr += sizeof( bot_chat_t );
} //end if
size = sizeof( bot_chat_t );
//
while ( PC_ReadToken( source, &token ) )
{
if ( !strcmp( token.string, "chat" ) ) {
if ( !PC_ExpectTokenType( source, TT_STRING, 0, &token ) ) {
FreeSource( source );
return NULL;
} //end if
StripDoubleQuotes( token.string );
//after the chat name we expect an opening brace
if ( !PC_ExpectTokenString( source, "{" ) ) {
FreeSource( source );
return NULL;
} //end if
//if the chat name is found
if ( !Q_stricmp( token.string, chatname ) ) {
foundchat = qtrue;
//read the chat types
while ( 1 )
{
if ( !PC_ExpectAnyToken( source, &token ) ) {
FreeSource( source );
return NULL;
} //end if
if ( !strcmp( token.string, "}" ) ) {
break;
}
if ( strcmp( token.string, "type" ) ) {
SourceError( source, "expected type found %s", token.string );
FreeSource( source );
return NULL;
} //end if
//expect the chat type name
if ( !PC_ExpectTokenType( source, TT_STRING, 0, &token ) ||
!PC_ExpectTokenString( source, "{" ) ) {
FreeSource( source );
return NULL;
} //end if
StripDoubleQuotes( token.string );
if ( pass && ptr ) {
chattype = (bot_chattype_t *) ptr;
Q_strncpyz( chattype->name, token.string, MAX_CHATTYPE_NAME );
chattype->firstchatmessage = NULL;
//add the chat type to the chat
chattype->next = chat->types;
chat->types = chattype;
//
ptr += sizeof( bot_chattype_t );
} //end if
size += sizeof( bot_chattype_t );
//read the chat messages
while ( !PC_CheckTokenString( source, "}" ) )
{
size_t len;
if ( !BotLoadChatMessage( source, chatmessagestring ) ) {
FreeSource( source );
return NULL;
} //end if
len = strlen(chatmessagestring) + 1;
len = PAD(len, sizeof(long));
if ( pass && ptr ) {
chatmessage = (bot_chatmessage_t *) ptr;
chatmessage->time = -2 * CHATMESSAGE_RECENTTIME;
//put the chat message in the list
chatmessage->next = chattype->firstchatmessage;
chattype->firstchatmessage = chatmessage;
//store the chat message
ptr += sizeof( bot_chatmessage_t );
chatmessage->chatmessage = ptr;
strcpy( chatmessage->chatmessage, chatmessagestring );
ptr += len;
//the number of chat messages increased
chattype->numchatmessages++;
} //end if
size += sizeof(bot_chatmessage_t) + len;
} //end if
} //end while
} //end if
else //skip the bot chat
{
indent = 1;
while ( indent )
{
if ( !PC_ExpectAnyToken( source, &token ) ) {
FreeSource( source );
return NULL;
} //end if
if ( !strcmp( token.string, "{" ) ) {
indent++;
} else if ( !strcmp( token.string, "}" ) ) {
indent--;
}
} //end while
} //end else
} //end if
else
{
SourceError( source, "unknown definition %s", token.string );
FreeSource( source );
return NULL;
} //end else
} //end while
//free the source
FreeSource( source );
//if the requested character is not found
if ( !foundchat ) {
botimport.Print( PRT_ERROR, "couldn't find chat %s in %s\n", chatname, chatfile );
return NULL;
} //end if
} //end for
//
botimport.Print( PRT_MESSAGE, "loaded %s from %s\n", chatname, chatfile );
//
//BotDumpInitialChat(chat);
if ( botDeveloper ) {
BotCheckInitialChatIntegrety( chat );
} //end if
#ifdef DEBUG
botimport.Print( PRT_MESSAGE, "initial chats loaded in %d msec\n", Sys_MilliSeconds() - starttime );
#endif //DEBUG
//character was read successfully
return chat;
} //end of the function BotLoadInitialChat
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void BotFreeChatFile( int chatstate ) {
bot_chatstate_t *cs;
cs = BotChatStateFromHandle( chatstate );
if ( !cs ) {
return;
}
if ( cs->chat ) {
FreeMemory( cs->chat );
}
cs->chat = NULL;
} //end of the function BotFreeChatFile
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
int BotLoadChatFile( int chatstate, char *chatfile, char *chatname ) {
bot_chatstate_t *cs;
int n, avail = 0;
cs = BotChatStateFromHandle( chatstate );
if ( !cs ) {
return BLERR_CANNOTLOADICHAT;
}
BotFreeChatFile( chatstate );
if ( !LibVarGetValue( "bot_reloadcharacters" ) ) {
avail = -1;
for ( n = 0; n < MAX_CLIENTS; n++ ) {
if ( !ichatdata[n].inuse ) {
if ( avail == -1 ) {
avail = n;
}
continue;
}
if ( strcmp( chatfile, ichatdata[n].filename ) != 0 ) {
continue;
}
if ( strcmp( chatname, ichatdata[n].chatname ) != 0 ) {
continue;
}
cs->chat = ichatdata[n].chat;
// botimport.Print( PRT_MESSAGE, "retained %s from %s\n", chatname, chatfile );
return BLERR_NOERROR;
}
if ( avail == -1 ) {
botimport.Print( PRT_FATAL, "ichatdata table full; couldn't load chat %s from %s\n", chatname, chatfile );
return BLERR_CANNOTLOADICHAT;
}
}
cs->chat = BotLoadInitialChat( chatfile, chatname );
if ( !cs->chat ) {
botimport.Print( PRT_FATAL, "couldn't load chat %s from %s\n", chatname, chatfile );
return BLERR_CANNOTLOADICHAT;
} //end if
if ( !LibVarGetValue( "bot_reloadcharacters" ) ) {
ichatdata[avail].chat = cs->chat;
Q_strncpyz( ichatdata[avail].chatname, chatname, sizeof( ichatdata[avail].chatname ) );
Q_strncpyz( ichatdata[avail].filename, chatfile, sizeof( ichatdata[avail].filename ) );
ichatdata[avail].inuse = qtrue;
} //end if
return BLERR_NOERROR;
} //end of the function BotLoadChatFile
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
int BotExpandChatMessage( char *outmessage, char *message, unsigned long mcontext,
bot_matchvariable_t *variables, unsigned long vcontext, int reply ) {
int num, len, i, expansion;
char *outputbuf, *ptr, *msgptr;
char temp[MAX_MESSAGE_SIZE];
expansion = qfalse;
msgptr = message;
outputbuf = outmessage;
len = 0;
//
while ( *msgptr )
{
if ( *msgptr == ESCAPE_CHAR ) {
msgptr++;
switch ( *msgptr )
{
case 'v': //variable
{
msgptr++;
num = 0;
while ( *msgptr && *msgptr != ESCAPE_CHAR )
{
num = num * 10 + ( *msgptr++ ) - '0';
} //end while
//step over the trailing escape char
if ( *msgptr ) {
msgptr++;
}
if ( num > MAX_MATCHVARIABLES ) {
botimport.Print( PRT_ERROR, "BotConstructChat: message %s variable %d out of range\n", message, num );
return qfalse;
} //end if
ptr = variables[num].ptr;
if ( ptr ) {
for ( i = 0; i < variables[num].length; i++ )
{
temp[i] = ptr[i];
} //end for
temp[i] = 0;
//if it's a reply message
if ( reply ) {
//replace the reply synonyms in the variables
BotReplaceReplySynonyms( temp, vcontext );
} //end if
else
{
//replace synonyms in the variable context
BotReplaceSynonyms( temp, vcontext );
} //end else
//
if ( len + strlen( temp ) >= MAX_MESSAGE_SIZE ) {
botimport.Print( PRT_ERROR, "BotConstructChat: message %s too long\n", message );
return qfalse;
} //end if
strcpy( &outputbuf[len], temp );
len += strlen( temp );
} //end if
break;
} //end case
case 'r': //random
{
msgptr++;
for ( i = 0; ( *msgptr && *msgptr != ESCAPE_CHAR ); i++ )
{
temp[i] = *msgptr++;
} //end while
temp[i] = '\0';
//step over the trailing escape char
if ( *msgptr ) {
msgptr++;
}
//find the random keyword
ptr = RandomString( temp );
if ( !ptr ) {
botimport.Print( PRT_ERROR, "BotConstructChat: unknown random string %s\n", temp );
return qfalse;
} //end if
if ( len + strlen( ptr ) >= MAX_MESSAGE_SIZE ) {
botimport.Print( PRT_ERROR, "BotConstructChat: message \"%s\" too long\n", message );
return qfalse;
} //end if
strcpy( &outputbuf[len], ptr );
len += strlen( ptr );
expansion = qtrue;
break;
} //end case
default:
{
botimport.Print( PRT_FATAL, "BotConstructChat: message \"%s\" invalid escape char\n", message );
break;
} //end default
} //end switch
} //end if
else
{
outputbuf[len++] = *msgptr++;
if ( len >= MAX_MESSAGE_SIZE ) {
botimport.Print( PRT_ERROR, "BotConstructChat: message \"%s\" too long\n", message );
break;
} //end if
} //end else
} //end while
outputbuf[len] = '\0';
//replace synonyms weighted in the message context
BotReplaceWeightedSynonyms( outputbuf, mcontext );
//return true if a random was expanded
return expansion;
} //end of the function BotExpandChatMessage
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void BotConstructChatMessage( bot_chatstate_t *chatstate, char *message, unsigned long mcontext,
bot_matchvariable_t *variables, unsigned long vcontext, int reply ) {
int i;
char srcmessage[MAX_MESSAGE_SIZE];
strcpy( srcmessage, message );
for ( i = 0; i < 10; i++ )
{
if ( !BotExpandChatMessage( chatstate->chatmessage, srcmessage, mcontext, variables, vcontext, reply ) ) {
break;
} //end if
strcpy( srcmessage, chatstate->chatmessage );
} //end for
if ( i >= 10 ) {
botimport.Print( PRT_WARNING, "too many expansions in chat message\n" );
botimport.Print( PRT_WARNING, "%s\n", chatstate->chatmessage );
} //end if
} //end of the function BotConstructChatMessage
//===========================================================================
// randomly chooses one of the chat message of the given type
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
char *BotChooseInitialChatMessage( bot_chatstate_t *cs, char *type ) {
int n, numchatmessages;
float besttime;
bot_chattype_t *t;
bot_chatmessage_t *m, *bestchatmessage;
bot_chat_t *chat;
chat = cs->chat;
for ( t = chat->types; t; t = t->next )
{
if ( !Q_stricmp( t->name, type ) ) {
numchatmessages = 0;
for ( m = t->firstchatmessage; m; m = m->next )
{
if ( m->time > AAS_Time() ) {
continue;
}
numchatmessages++;
} //end if
//if all chat messages have been used recently
if ( numchatmessages <= 0 ) {
besttime = 0;
bestchatmessage = NULL;
for ( m = t->firstchatmessage; m; m = m->next )
{
if ( !besttime || m->time < besttime ) {
bestchatmessage = m;
besttime = m->time;
} //end if
} //end for
if ( bestchatmessage ) {
return bestchatmessage->chatmessage;
}
} //end if
else //choose a chat message randomly
{
n = random() * numchatmessages;
for ( m = t->firstchatmessage; m; m = m->next )
{
if ( m->time > AAS_Time() ) {
continue;
}
if ( --n < 0 ) {
m->time = AAS_Time() + CHATMESSAGE_RECENTTIME;
return m->chatmessage;
} //end if
} //end for
} //end else
return NULL;
} //end if
} //end for
return NULL;
} //end of the function BotChooseInitialChatMessage
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
int BotNumInitialChats( int chatstate, char *type ) {
bot_chatstate_t *cs;
bot_chattype_t *t;
cs = BotChatStateFromHandle( chatstate );
if ( !cs ) {
return 0;
}
for ( t = cs->chat->types; t; t = t->next )
{
if ( !Q_stricmp( t->name, type ) ) {
if ( LibVarGetValue( "bot_testichat" ) ) {
botimport.Print( PRT_MESSAGE, "%s has %d chat lines\n", type, t->numchatmessages );
botimport.Print( PRT_MESSAGE, "-------------------\n" );
}
return t->numchatmessages;
} //end if
} //end for
return 0;
} //end of the function BotNumInitialChats
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void BotInitialChat( int chatstate, char *type, int mcontext, char *var0, char *var1, char *var2, char *var3, char *var4, char *var5, char *var6, char *var7 ) {
char *message;
bot_matchvariable_t variables[MAX_MATCHVARIABLES];
bot_chatstate_t *cs;
cs = BotChatStateFromHandle( chatstate );
if ( !cs ) {
return;
}
//if no chat file is loaded
if ( !cs->chat ) {
return;
}
//choose a chat message randomly of the given type
message = BotChooseInitialChatMessage( cs, type );
//if there's no message of the given type
if ( !message ) {
#ifdef DEBUG
botimport.Print( PRT_MESSAGE, "no chat messages of type %s\n", type );
#endif //DEBUG
return;
} //end if
//
memset( variables, 0, sizeof( variables ) );
if ( var0 ) {
variables[0].ptr = var0;
variables[0].length = strlen( var0 );
}
if ( var1 ) {
variables[1].ptr = var1;
variables[1].length = strlen( var1 );
}
if ( var2 ) {
variables[2].ptr = var2;
variables[2].length = strlen( var2 );
}
if ( var3 ) {
variables[3].ptr = var3;
variables[3].length = strlen( var3 );
}
if ( var4 ) {
variables[4].ptr = var4;
variables[4].length = strlen( var4 );
}
if ( var5 ) {
variables[5].ptr = var5;
variables[5].length = strlen( var5 );
}
if ( var6 ) {
variables[6].ptr = var6;
variables[6].length = strlen( var6 );
}
if ( var7 ) {
variables[7].ptr = var7;
variables[7].length = strlen( var7 );
}
//
BotConstructChatMessage( cs, message, mcontext, variables, 0, qfalse );
} //end of the function BotInitialChat
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void BotPrintReplyChatKeys( bot_replychat_t *replychat ) {
bot_replychatkey_t *key;
bot_matchpiece_t *mp;
botimport.Print( PRT_MESSAGE, "[" );
for ( key = replychat->keys; key; key = key->next )
{
if ( key->flags & RCKFL_AND ) {
botimport.Print( PRT_MESSAGE, "&" );
} else if ( key->flags & RCKFL_NOT ) {
botimport.Print( PRT_MESSAGE, "!" );
}
//
if ( key->flags & RCKFL_NAME ) {
botimport.Print( PRT_MESSAGE, "name" );
} else if ( key->flags & RCKFL_GENDERFEMALE ) {
botimport.Print( PRT_MESSAGE, "female" );
} else if ( key->flags & RCKFL_GENDERMALE ) {
botimport.Print( PRT_MESSAGE, "male" );
} else if ( key->flags & RCKFL_GENDERLESS ) {
botimport.Print( PRT_MESSAGE, "it" );
} else if ( key->flags & RCKFL_VARIABLES ) {
botimport.Print( PRT_MESSAGE, "(" );
for ( mp = key->match; mp; mp = mp->next )
{
if ( mp->type == MT_STRING ) {
botimport.Print( PRT_MESSAGE, "\"%s\"", mp->firststring->string );
} else { botimport.Print( PRT_MESSAGE, "%d", mp->variable );}
if ( mp->next ) {
botimport.Print( PRT_MESSAGE, ", " );
}
} //end for
botimport.Print( PRT_MESSAGE, ")" );
} //end if
else if ( key->flags & RCKFL_STRING ) {
botimport.Print( PRT_MESSAGE, "\"%s\"", key->string );
} //end if
if ( key->next ) {
botimport.Print( PRT_MESSAGE, ", " );
} else { botimport.Print( PRT_MESSAGE, "] = %1.0f\n", replychat->priority );}
} //end for
botimport.Print( PRT_MESSAGE, "{\n" );
} //end of the function BotPrintReplyChatKeys
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
int BotReplyChat( int chatstate, char *message, int mcontext, int vcontext, char *var0, char *var1, char *var2, char *var3, char *var4, char *var5, char *var6, char *var7 ) {
bot_replychat_t *rchat, *bestrchat;
bot_replychatkey_t *key;
bot_chatmessage_t *m, *bestchatmessage;
bot_match_t match, bestmatch;
int bestpriority, num, found, res, numchatmessages;
bot_chatstate_t *cs;
cs = BotChatStateFromHandle( chatstate );
if ( !cs ) {
return qfalse;
}
memset( &match, 0, sizeof( bot_match_t ) );
strcpy( match.string, message );
bestpriority = -1;
bestchatmessage = NULL;
bestrchat = NULL;
//go through all the reply chats
for ( rchat = replychats; rchat; rchat = rchat->next )
{
found = qfalse;
for ( key = rchat->keys; key; key = key->next )
{
res = qfalse;
//get the match result
if ( key->flags & RCKFL_NAME ) {
res = ( StringContains( message, cs->name, qfalse ) != -1 );
} else if ( key->flags & RCKFL_BOTNAMES ) {
res = ( StringContains( key->string, cs->name, qfalse ) != -1 );
} else if ( key->flags & RCKFL_GENDERFEMALE ) {
res = ( cs->gender == CHAT_GENDERFEMALE );
} else if ( key->flags & RCKFL_GENDERMALE ) {
res = ( cs->gender == CHAT_GENDERMALE );
} else if ( key->flags & RCKFL_GENDERLESS ) {
res = ( cs->gender == CHAT_GENDERLESS );
} else if ( key->flags & RCKFL_VARIABLES ) {
res = StringsMatch( key->match, &match );
} else if ( key->flags & RCKFL_STRING ) {
res = ( StringContainsWord( message, key->string, qfalse ) != NULL );
}
//if the key must be present
if ( key->flags & RCKFL_AND ) {
if ( !res ) {
found = qfalse;
break;
} //end if
//botnames is an exception
//if (!(key->flags & RCKFL_BOTNAMES)) found = qtrue;
} //end else if
//if the key must be absent
else if ( key->flags & RCKFL_NOT ) {
if ( res ) {
found = qfalse;
break;
} //end if
} //end if
else if ( res ) {
found = qtrue;
} //end else
} //end for
//
if ( found ) {
if ( rchat->priority > bestpriority ) {
numchatmessages = 0;
for ( m = rchat->firstchatmessage; m; m = m->next )
{
if ( m->time > AAS_Time() ) {
continue;
}
numchatmessages++;
} //end if
num = random() * numchatmessages;
for ( m = rchat->firstchatmessage; m; m = m->next )
{
if ( --num < 0 ) {
break;
}
if ( m->time > AAS_Time() ) {
continue;
}
} //end for
//if the reply chat has a message
if ( m ) {
memcpy( &bestmatch, &match, sizeof( bot_match_t ) );
bestchatmessage = m;
bestrchat = rchat;
bestpriority = rchat->priority;
} //end if
} //end if
} //end if
} //end for
if ( bestchatmessage ) {
if ( var0 ) {
bestmatch.variables[0].ptr = var0;
bestmatch.variables[0].length = strlen( var0 );
}
if ( var1 ) {
bestmatch.variables[1].ptr = var1;
bestmatch.variables[1].length = strlen( var1 );
}
if ( var2 ) {
bestmatch.variables[2].ptr = var2;
bestmatch.variables[2].length = strlen( var2 );
}
if ( var3 ) {
bestmatch.variables[3].ptr = var3;
bestmatch.variables[3].length = strlen( var3 );
}
if ( var4 ) {
bestmatch.variables[4].ptr = var4;
bestmatch.variables[4].length = strlen( var4 );
}
if ( var5 ) {
bestmatch.variables[5].ptr = var5;
bestmatch.variables[5].length = strlen( var5 );
}
if ( var6 ) {
bestmatch.variables[6].ptr = var6;
bestmatch.variables[6].length = strlen( var6 );
}
if ( var7 ) {
bestmatch.variables[7].ptr = var7;
bestmatch.variables[7].length = strlen( var7 );
}
if ( LibVarGetValue( "bot_testrchat" ) ) {
for ( m = bestrchat->firstchatmessage; m; m = m->next )
{
BotConstructChatMessage( cs, m->chatmessage, mcontext, bestmatch.variables, vcontext, qtrue );
BotRemoveTildes( cs->chatmessage );
botimport.Print( PRT_MESSAGE, "%s\n", cs->chatmessage );
} //end if
} //end if
else
{
bestchatmessage->time = AAS_Time() + CHATMESSAGE_RECENTTIME;
BotConstructChatMessage( cs, bestchatmessage->chatmessage, mcontext, bestmatch.variables, vcontext, qtrue );
} //end else
return qtrue;
} //end if
return qfalse;
} //end of the function BotReplyChat
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
int BotChatLength( int chatstate ) {
bot_chatstate_t *cs;
cs = BotChatStateFromHandle( chatstate );
if ( !cs ) {
return 0;
}
return strlen( cs->chatmessage );
} //end of the function BotChatLength
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void BotEnterChat( int chatstate, int client, int sendto ) {
bot_chatstate_t *cs;
cs = BotChatStateFromHandle( chatstate );
if ( !cs ) {
return;
}
if ( strlen( cs->chatmessage ) ) {
BotRemoveTildes( cs->chatmessage );
if ( LibVarGetValue( "bot_testichat" ) ) {
botimport.Print( PRT_MESSAGE, "%s\n", cs->chatmessage );
} else {
if ( sendto == CHAT_TEAM ) {
EA_SayTeam( client, cs->chatmessage );
} else { EA_Say( client, cs->chatmessage );}
}
//clear the chat message from the state
strcpy( cs->chatmessage, "" );
} //end if
} //end of the function BotEnterChat
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void BotGetChatMessage( int chatstate, char *buf, int size ) {
bot_chatstate_t *cs;
cs = BotChatStateFromHandle( chatstate );
if ( !cs ) {
return;
}
BotRemoveTildes( cs->chatmessage );
strncpy( buf, cs->chatmessage, size - 1 );
buf[size - 1] = '\0';
//clear the chat message from the state
strcpy( cs->chatmessage, "" );
} //end of the function BotGetChatMessage
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void BotSetChatGender( int chatstate, int gender ) {
bot_chatstate_t *cs;
cs = BotChatStateFromHandle( chatstate );
if ( !cs ) {
return;
}
switch ( gender )
{
case CHAT_GENDERFEMALE: cs->gender = CHAT_GENDERFEMALE; break;
case CHAT_GENDERMALE: cs->gender = CHAT_GENDERMALE; break;
default: cs->gender = CHAT_GENDERLESS; break;
} //end switch
} //end of the function BotSetChatGender
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void BotSetChatName( int chatstate, char *name ) {
bot_chatstate_t *cs;
cs = BotChatStateFromHandle( chatstate );
if ( !cs ) {
return;
}
memset( cs->name, 0, sizeof( cs->name ) );
strncpy( cs->name, name, sizeof( cs->name ) - 1 );
cs->name[sizeof( cs->name ) - 1] = '\0';
} //end of the function BotSetChatName
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void BotResetChatAI( void ) {
bot_replychat_t *rchat;
bot_chatmessage_t *m;
for ( rchat = replychats; rchat; rchat = rchat->next )
{
for ( m = rchat->firstchatmessage; m; m = m->next )
{
m->time = 0;
} //end for
} //end for
} //end of the function BotResetChatAI
//========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//========================================================================
int BotAllocChatState( void ) {
int i;
for ( i = 1; i <= MAX_CLIENTS; i++ )
{
if ( !botchatstates[i] ) {
botchatstates[i] = GetClearedMemory( sizeof( bot_chatstate_t ) );
return i;
} //end if
} //end for
return 0;
} //end of the function BotAllocChatState
//========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//========================================================================
void BotFreeChatState( int handle ) {
bot_consolemessage_t m;
int h;
if ( handle <= 0 || handle > MAX_CLIENTS ) {
botimport.Print( PRT_FATAL, "chat state handle %d out of range\n", handle );
return;
} //end if
if ( !botchatstates[handle] ) {
botimport.Print( PRT_FATAL, "invalid chat state %d\n", handle );
return;
} //end if
if ( LibVarGetValue( "bot_reloadcharacters" ) ) {
BotFreeChatFile( handle );
} //end if
//free all the console messages left in the chat state
for ( h = BotNextConsoleMessage( handle, &m ); h; h = BotNextConsoleMessage( handle, &m ) )
{
//remove the console message
BotRemoveConsoleMessage( handle, h );
} //end for
FreeMemory( botchatstates[handle] );
botchatstates[handle] = NULL;
} //end of the function BotFreeChatState
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
int BotSetupChatAI( void ) {
char *file;
#ifdef DEBUG
int starttime = Sys_MilliSeconds();
#endif //DEBUG
file = LibVarString( "synfile", "syn.c" );
synonyms = BotLoadSynonyms( file );
file = LibVarString( "rndfile", "rnd.c" );
randomstrings = BotLoadRandomStrings( file );
file = LibVarString( "matchfile", "match.c" );
matchtemplates = BotLoadMatchTemplates( file );
//
if ( !LibVarValue( "nochat", "0" ) ) {
file = LibVarString( "rchatfile", "rchat.c" );
replychats = BotLoadReplyChat( file );
} //end if
InitConsoleMessageHeap();
#ifdef DEBUG
botimport.Print( PRT_MESSAGE, "setup chat AI %d msec\n", Sys_MilliSeconds() - starttime );
#endif //DEBUG
return BLERR_NOERROR;
} //end of the function BotSetupChatAI
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void BotShutdownChatAI( void ) {
int i;
//free all remaining chat states
for ( i = 0; i < MAX_CLIENTS; i++ )
{
if ( botchatstates[i] ) {
BotFreeChatState( i );
} //end if
} //end for
//free all cached chats
for ( i = 0; i < MAX_CLIENTS; i++ )
{
if ( ichatdata[i].inuse ) {
FreeMemory( ichatdata[i].chat );
ichatdata[i].inuse = qfalse;
} //end if
} //end for
if ( consolemessageheap ) {
FreeMemory( consolemessageheap );
}
consolemessageheap = NULL;
if ( matchtemplates ) {
BotFreeMatchTemplates( matchtemplates );
}
matchtemplates = NULL;
if ( randomstrings ) {
FreeMemory( randomstrings );
}
randomstrings = NULL;
if ( synonyms ) {
FreeMemory( synonyms );
}
synonyms = NULL;
if ( replychats ) {
BotFreeReplyChat( replychats );
}
replychats = NULL;
} //end of the function BotShutdownChatAI
|