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
|
/* Unit plan handling for Xconq.
Copyright (C) 1991-1998 Stanley T. Shebs.
Xconq is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version. See the file COPYING. */
#include "conq.h"
extern int target_visible PARAMS ((Unit *unit, Task *task));
#include "kernel.h"
extern int repair_if_damaged PARAMS ((Unit *unit));
extern Task *create_repair_task PARAMS ((void));
extern void set_repair_task PARAMS ((Unit *unit));
extern short any_auto_repair;
static void plan_passive PARAMS ((Unit *unit, int try));
static void plan_offense PARAMS ((Unit *unit));
static void plan_offense_support PARAMS ((Unit *unit));
static void plan_defense PARAMS ((Unit *unit));
static void plan_exploration PARAMS ((Unit *unit));
static void plan_explorer_support PARAMS ((Unit *unit));
static void plan_random PARAMS ((Unit *unit));
static void wake_at PARAMS ((int x, int y));
static int alternate_target_here PARAMS ((int x, int y));
static int side_planning_to_capture PARAMS ((Side *side, int u, int x, int y));
#define CLEAR_AGENDA 99
/* (should have a generic struct for all plan type attrs) */
char *plantypenames[] = {
#undef DEF_PLAN
#define DEF_PLAN(NAME,code) NAME,
#include "plan.def"
NULL
};
/* Every unit that can act needs a plan object, types that can't act
should have it cleared out. Note that incomplete units are expected
to be able to act in the future, so it's acceptable to run this for
incomplete units to give them a plan. */
void
init_unit_plan(unit)
Unit *unit;
{
if (u_acp(unit->type) > 0) {
/* Might already have a plan, so don't always realloc. */
if (unit->plan == NULL) {
unit->plan = create_plan();
}
/* Put the plan into a default state, side will work it up later. */
/* (should release goals also) */
clear_task_agenda(unit->plan);
/* Zero the plan just as xmalloc would. */
memset(unit->plan, 0, sizeof(Plan));
unit->plan->type = PLAN_PASSIVE;
unit->plan->creation_turn = g_turn();
/* Allow AIs to make this unit do things. */
unit->plan->aicontrol = TRUE;
/* Enable supply alarms by default. */
unit->plan->supply_alarm = TRUE;
/* Clear the task outcome. */
unit->plan->last_task_outcome = TASK_UNKNOWN;
} else {
/* Brainless units don't need anything, can free up plan. */
if (unit->plan != NULL) {
free_plan(unit->plan);
}
unit->plan = NULL;
}
}
void
set_unit_plan_type(side, unit, type)
Side *side;
Unit *unit;
int type;
{
int oldtype;
if (unit->plan) {
oldtype = unit->plan->type;
if (type != oldtype) {
if (type == PLAN_NONE) {
type = PLAN_PASSIVE;
force_replan(side, unit, FALSE);
}
unit->plan->type = type;
if (side != NULL)
update_unit_display(side, unit, TRUE);
}
}
}
void
set_unit_asleep(side, unit, flag, recurse)
Side *side;
Unit *unit;
int flag, recurse;
{
int oldflag;
Unit *occ;
if (unit->plan) {
oldflag = unit->plan->asleep;
if (flag != oldflag) {
unit->plan->asleep = flag;
if (side != NULL)
update_unit_display(side, unit, TRUE);
}
}
if (recurse) {
for_all_occupants(unit, occ) {
set_unit_asleep(side, occ, flag, recurse);
}
}
}
void
set_unit_reserve(side, unit, flag, recurse)
Side *side;
Unit *unit;
int flag, recurse;
{
int oldflag;
Unit *occ;
if (unit->plan) {
oldflag = unit->plan->reserve;
if (flag != oldflag) {
unit->plan->reserve = flag;
if (side != NULL)
update_unit_display(side, unit, TRUE);
}
}
if (recurse) {
for_all_occupants(unit, occ) {
set_unit_reserve(side, occ, flag, recurse);
}
}
}
void
set_unit_ai_control(side, unit, flag, recurse)
Side *side;
Unit *unit;
int flag, recurse;
{
int oldflag;
Unit *occ;
if (unit->plan) {
oldflag = unit->plan->aicontrol;
if (flag != oldflag) {
unit->plan->aicontrol = flag;
if (side != NULL)
update_unit_display(side, unit, TRUE);
}
}
if (recurse) {
for_all_occupants(unit, occ) {
set_unit_ai_control(side, occ, flag, recurse);
}
}
}
void
set_unit_main_goal(side, unit, goal)
Side *side;
Unit *unit;
Goal *goal;
{
if (unit->plan) {
unit->plan->maingoal = goal;
}
}
void
set_unit_waiting_for_transport(side, unit, flag)
Side *side;
Unit *unit;
int flag;
{
if (unit->plan) {
unit->plan->waitingfortransport = flag;
}
}
/* Execute the plan. */
void
execute_plan(unit, try)
Unit *unit;
int try;
{
Plan *plan = unit->plan;
extern int planexecs;
if (!in_play(unit) || !completed(unit)) {
DMprintf("%s shouldn't be planning yet\n", unit_desig(unit));
return;
}
DMprintf("%s using plan %s", unit_desig(unit), plan_desig(plan));
if (try > 1)
DMprintf(" (try #%d)", try);
DMprintf("\n");
/* Units that are asleep or in reserve do nothing. */
if (plan->asleep || plan->reserve)
return;
++planexecs;
if (try > 5) {
DMprintf("%s redoing plan too often, going into reserve\n",
unit_desig(unit));
plan->reserve = TRUE;
return;
}
if (plan->execs_this_turn > 1000) {
DMprintf("%s executed plan 1000 times this turn, going into reserve\n",
unit_desig(unit));
plan->reserve = TRUE;
return;
}
/* Unit actually has a plan, dispatch on its type. */
switch (plan->type) {
case PLAN_NONE:
/* Unit has not gotten a plan yet, leave it alone. */
break;
case PLAN_PASSIVE:
plan_passive(unit, try);
break;
case PLAN_OFFENSIVE:
plan_offense(unit);
break;
case PLAN_DEFENSIVE:
plan_defense(unit);
break;
case PLAN_EXPLORATORY:
plan_exploration(unit);
break;
case PLAN_RANDOM:
plan_random(unit);
break;
default:
case_panic("plan type", plan->type);
break;
}
++(plan->execs_this_turn);
}
/* See if we're too far away from an assigned position, set a task
to move back if so. */
int
move_into_formation(unit)
Unit *unit;
{
int nx, ny, dist;
Plan *plan = unit->plan;
Goal *goal;
Unit *leader;
leader = plan->funit;
if (leader != NULL) {
goal = plan->formation;
/* Ensure that the leader is still someone we want to follow. */
if (!in_play(leader)
|| !unit_trusts_unit(unit, leader)
|| goal->args[0] != leader->id) {
notify(unit->side, "%s leader is gone, cancelling formation",
unit_handle(unit->side, unit));
/* (should free goal object?) */
plan->formation = NULL;
plan->funit = NULL;
/* Unit is available to do something else. */
return FALSE;
}
nx = leader->x + goal->args[1]; ny = leader->y + goal->args[2];
dist = goal->args[3];
if (distance(unit->x, unit->y, nx, ny) > dist) {
/* (should perhaps insert after current task?) */
set_move_near_task(unit, nx, ny, dist);
return TRUE;
}
}
return FALSE;
}
int task_is_in_agenda PARAMS ((Plan *plan, Task *task));
/* See if there are any standing orders that currently apply to the given unit,
and schedule a task if so. Return TRUE if a task was added. */
int
execute_standing_order(unit, addtask)
Unit *unit;
int addtask;
{
Unit *transport;
Side *side;
StandingOrder *sorder;
side = unit->side;
if (side == NULL)
return FALSE;
for (sorder = side->orders; sorder != NULL; sorder = sorder->next) {
if (sorder->types[unit->type] && unit->plan) {
switch (sorder->condtype) {
case sorder_at:
if (unit->x == sorder->a1 && unit->y == sorder->a2) {
/* If the task is already in the plan, don't do
anything. */
if (task_is_in_agenda(unit->plan, sorder->task))
return FALSE;
if (addtask)
add_task(unit, 0, clone_task(sorder->task));
return TRUE;
}
break;
case sorder_in:
transport = unit->transport;
if (transport != NULL && transport->id == sorder->a1) {
/* If the task is already in the plan, don't do
anything. */
if (task_is_in_agenda(unit->plan, sorder->task))
return FALSE;
if (addtask)
add_task(unit, 0, clone_task(sorder->task));
return TRUE;
}
break;
case sorder_near:
if (distance(unit->x, unit->y, sorder->a1, sorder->a2) <= sorder->a3) {
/* If the task is already in the plan, don't do
anything. */
if (task_is_in_agenda(unit->plan, sorder->task))
return FALSE;
if (addtask)
add_task(unit, 0, clone_task(sorder->task));
return TRUE;
}
break;
default:
run_warning("Unknown order condition type");
break;
}
}
}
return FALSE;
}
int tasks_match PARAMS ((Task *task1, Task *task2));
int
task_is_in_agenda(plan, task)
Plan *plan;
Task *task;
{
Task *task2;
for (task2 = plan->tasks; task2 != NULL; task2 = task2->next) {
if (tasks_match(task, task2))
return TRUE;
}
return FALSE;
}
int
tasks_match(task1, task2)
Task *task1, *task2;
{
int i;
if (task1->type != task2->type)
return FALSE;
for (i = 0; i < MAXTASKARGS; ++i)
if (task1->args[i] != task2->args[i])
return FALSE;
return TRUE;
}
/* Passive units just work from the task queue or else wait to be told
what to do. */
static void
plan_passive(unit, try)
Unit *unit;
int try;
{
Plan *plan = unit->plan;
/* Special-case human cities in the intro game to automatically
start producing infantry initially. */
/* (would be more efficient to put in a once-per-turn location,
should look for one) */
if (g_turn() <= 1
&& mainmodule != NULL
&& ((mainmodule->name != NULL
&& strcmp(mainmodule->name, INTRO_GAME) == 0)
|| (mainmodule->origmodulename != NULL
&& strcmp(mainmodule->origmodulename, INTRO_GAME) == 0))
&& strcmp(u_type_name(unit->type), "city") == 0) {
push_build_task(unit, 0, 99);
}
if (plan->supply_is_low && plan->supply_alarm) {
plan->supply_alarm = FALSE;
if (0 /* auto resupply */) {
set_resupply_task(unit, NONMTYPE);
} else if (plan->tasks
&& (plan->tasks->type == TASK_RESUPPLY
|| (plan->tasks->type == TASK_MOVE_TO
&& plan->tasks->next
&& plan->tasks->next->type == TASK_RESUPPLY))) {
/* do nothing */
} else {
clear_task_agenda(plan);
set_waiting_for_tasks(unit, TRUE);
}
}
if (plan->tasks) {
/* (should check that doctrine being followed correctly) */
execute_task(unit);
} else if (unit->side
&& unit->side->orders
&& execute_standing_order(unit, TRUE)) {
execute_task(unit);
} else if (plan->formation && move_into_formation(unit)) {
execute_task(unit);
} else {
/* Our goal is now to get guidance from the side. */
set_waiting_for_tasks(unit, TRUE);
}
}
/* A unit operating offensively advances and attacks when possible. */
int find_alternate_hit_target PARAMS ((Unit *unit, Task *task, int *xp, int *yp));
static void
plan_offense(unit)
Unit *unit;
{
int u = unit->type;
int x, y, w, h, range, x1, y1, nx, ny;
Plan *plan = unit->plan;
Task *lasttask;
if (resupply_if_low(unit)) {
if (plan->tasks)
execute_task(unit);
return;
}
if (rearm_if_low(unit)) {
if (plan->tasks)
execute_task(unit);
return;
}
if (repair_if_damaged(unit)) {
if (plan->tasks)
execute_task(unit);
return;
}
if (plan->tasks) {
execute_task(unit);
if (plan->last_task_outcome == TASK_FAILED) {
lasttask = &(plan->last_task);
if (lasttask->type == TASK_HIT_UNIT
&& lasttask->args[2] != NONUTYPE
&& !target_visible(unit, lasttask)) {
/* Target seems to have disappeared, look around for it. */
DMprintf("%s hit target has disappeared, looking for it; ",
unit_desig(unit));
if (find_alternate_hit_target(unit, lasttask, &nx, &ny)) {
if (plan->tasks
&& plan->tasks->type == lasttask->type
&& plan->tasks->args[0] == lasttask->args[0]
&& plan->tasks->args[1] == lasttask->args[1]
&& plan->tasks->args[2] == lasttask->args[2]
&& plan->tasks->args[3] == lasttask->args[3]
) {
pop_task(plan);
}
push_specific_hit_task(unit, nx, ny, lasttask->args[2], lasttask->args[3]);
DMprintf(" found at %d,%d\n", nx, ny);
} else {
DMprintf(" not found\n");
}
}
}
/* Irrespective of what happened, we don't want to step on the task
yet. */
return;
}
if (plan->maingoal && mobile(u)) {
switch (plan->maingoal->type) {
case GOAL_VICINITY_HELD:
x = plan->maingoal->args[0]; y = plan->maingoal->args[1];
w = plan->maingoal->args[2]; h = plan->maingoal->args[3];
if (distance(x, y, unit->x, unit->y) > max(w, h)) {
/* Outside the goal area - move in towards it. */
if (random_point_near(x, y, w / 2, &x1, &y1)) {
x = x1; y = y1;
}
DMprintf("%s to go on offensive to %d,%d\n",
unit_desig(unit), x, y);
set_move_near_task(unit, x, y, max(w, h) / 2);
if (unit->transport
&& mobile(unit->transport->type)
&& unit->transport->plan) {
set_move_near_task(unit->transport, x, y, max(w, h) / 2);
}
} else {
range = max(w, h);
/* No special goal, look for something to fight with. */
/* Sometimes be willing to look a little farther out. */
if (probability(50))
range *= 2;
if (do_for_occupants(unit)) {
/* Occupants have decided for us, fall through. */
} else if (go_after_victim(unit, range)) {
/* Found a victim to go after, fall through. */
} else if (probability(20) && self_build_base_for_self(unit)) {
} else if (!all_see_all) {
DMprintf("%s will explore instead\n", unit_desig(unit));
plan_exploration(unit); /* or patrol */
/* Running under exploration rules now. */
return;
}
}
break;
default:
DMprintf("offensive unit has some goal\n");
break;
}
} else if (mobile(u)) {
range = operating_range_best(u);
if (probability(50))
range = min(range, 2 * u_acp(u));
if (do_for_occupants(unit)) {
} else if (go_after_victim(unit, range)) {
/* No special goal, but found something to fight with. */
} else if (!all_see_all) {
DMprintf("%s will explore instead\n", unit_desig(unit));
plan_exploration(unit); /* or patrol */
/* Running under exploration rules now. */
return;
} else {
/* should go to a "best location" if possible. */
/* (should do a sentry task) */
}
} else if (can_fire(unit) && fire_at_opportunity(unit)) {
} else {
plan_offense_support(unit);
}
if (plan->tasks) {
execute_task(unit);
} else {
DMprintf("%s found nothing to do offensively", unit_desig(unit));
if (flip_coin()) {
DMprintf("- going into reserve");
plan->reserve = TRUE;
} else if (probability(5)) {
DMprintf("- going to sleep");
plan->asleep = TRUE;
}
DMprintf("\n");
}
}
/* Look through list of occupants to see if an occupant needs the transport to do
something. */
int
do_for_occupants(unit)
Unit *unit;
{
Unit *occ;
Goal *goal;
Task *task;
for_all_occupants(unit, occ) {
if (occ->plan) {
/* Get the unit towards its goal, if it has one. */
goal = occ->plan->maingoal;
if (goal != NULL
&& goal->type == GOAL_VICINITY_HELD
&& distance(goal->args[0], goal->args[1], unit->x, unit->y)
> goal->args[2]) {
set_move_near_task(unit, goal->args[0], goal->args[1],
max(goal->args[2] / 2, 1));
DMprintf("%s will go where occupant %s wants to go (goal %s)\n",
unit_desig(unit), unit_desig(occ), goal_desig(goal));
return TRUE;
}
/* If the unit does not have a goal, see if it has a task. */
for_all_tasks(occ->plan, task) {
if ((task->type == TASK_MOVE_TO
|| task->type == TASK_HIT_UNIT)
&& (task->args[0] != unit->x
|| task->args[1] != unit->y)
&& distance(task->args[0], task->args[1], unit->x, unit->y) > 1
) {
/* Note that we assume the transport is mobile, which is OK currently
because of where this routine is called from. */
set_move_near_task(unit, task->args[0], task->args[1], 1);
DMprintf("%s will go where occupant %s wants to go (task %s)\n",
unit_desig(unit), unit_desig(occ), task_desig(task));
return TRUE;
}
}
}
}
return FALSE;
}
int
self_build_base_for_self(unit)
Unit *unit;
{
int u = unit->type, u2, cando = FALSE;
for_all_unit_types(u2) {
if (uu_acp_to_create(u, u2) > 0
&& (uu_creation_cp(u, u2) >= u_cp(u2)
|| uu_acp_to_build(u, u2) > 0)
/* (should check if any advantage to building) */
) {
cando = TRUE;
break;
}
}
if (cando) {
DMprintf("%s building %s as a base for itself\n",
unit_desig(unit), u_type_name(u2));
set_build_task(unit, u2, 1);
return TRUE;
}
return FALSE;
}
static void
plan_offense_support(unit)
Unit *unit;
{
int u = unit->type, u2, u3 = NONUTYPE, backup = NONUTYPE;
Task *task;
for_all_unit_types(u2) {
if (mobile(u2)
&& (type_can_attack(u2) || type_can_fire(u2))
&& uu_acp_to_create(u, u2) > 0) {
backup = u2;
if (flip_coin()) {
u3 = u2;
break;
}
}
}
if (u3 == NONUTYPE)
u3 = backup;
if (is_unit_type(u3)) {
task = unit->plan->tasks;
if (task == NULL || task->type != TASK_BUILD) {
DMprintf("%s supporting offense by building %s\n",
unit_desig(unit), u_type_name(u3));
set_build_task(unit, u3, 2);
} else {
DMprintf("%s already building, leaving alone\n",
unit_desig(unit));
}
} else {
DMprintf("%s has no way to support an offensive\n", unit_desig(unit));
}
}
int
find_alternate_hit_target(unit, task, xp, yp)
Unit *unit;
Task *task;
int *xp, *yp;
{
int range;
tmpunit = unit;
tmputype = task->args[2];
tmpside = side_n(task->args[3]);
/* (should adjust search radius for speed?) */
range = u_acp(tmputype) + 1;
return search_around(task->args[0], task->args[1], range,
alternate_target_here, xp, yp, 1);
}
static int
alternate_target_here(x, y)
int x, y;
{
int uview;
Unit *unit2;
Side *side = tmpunit->side;
if (units_visible(side, x, y)) {
if ((unit2 = unit_at(x, y)) != NULL
&& unit2->type == tmputype
&& unit2->side == tmpside)
return TRUE;
} else if (terrain_view(side, x, y) != UNSEEN
&& (uview = unit_view(side, x, y)) != EMPTY
&& vtype(uview) == tmputype
&& side_n(vside(uview)) == tmpside)
return TRUE;
return FALSE;
}
/* Defensive units don't go out looking for trouble, but they should
react strongly to threats. */
static void
plan_defense(unit)
Unit *unit;
{
int u = unit->type, range, x, y, w, h, x1, y1;
Plan *plan = unit->plan;
if (resupply_if_low(unit)) {
if (plan->tasks)
execute_task(unit);
return;
}
if (rearm_if_low(unit)) {
if (plan->tasks)
execute_task(unit);
return;
}
if (repair_if_damaged(unit)) {
if (plan->tasks)
execute_task(unit);
return;
}
if (plan->tasks) {
/* (should analyze and maybe decide to change task) */
execute_task(unit);
return;
}
if (plan->maingoal) {
switch (plan->maingoal->type) {
case GOAL_VICINITY_HELD:
x = plan->maingoal->args[0]; y = plan->maingoal->args[1];
w = plan->maingoal->args[2]; h = plan->maingoal->args[3];
if (distance(x, y, unit->x, unit->y) > max(w, h)) {
/* Outside the goal area - move in towards it. */
if (random_point_near(x, y, w / 2, &x1, &y1)) {
x = x1; y = y1;
}
DMprintf("%s to go on defensive to %d,%d\n",
unit_desig(unit), x, y);
set_move_near_task(unit, x, y, max(w, h) / 2);
if (unit->transport
&& mobile(unit->transport->type)
&& unit->transport->plan) {
set_move_near_task(unit->transport, x, y, max(w, h) / 2);
}
} else {
range = max(w, h);
/* No special goal, look for something to fight with. */
/* Sometimes be willing to look a little farther out. */
if (probability(50))
range *= 2;
if (do_for_occupants(unit)) {
/* Occupants have decided for us, fall through. */
} else if (go_after_victim(unit, range)) {
/* Found a victim to go after, fall through. */
} else if (probability(20) && self_build_base_for_self(unit)) {
} else if (!all_see_all) {
DMprintf("%s will explore instead\n", unit_desig(unit));
plan_exploration(unit); /* or patrol */
/* Running under exploration rules now. */
return;
}
}
break;
default:
DMprintf("defensive unit has some goal\n");
break;
}
/* (might be able to defend by interposing self?) */
return;
}
/* Generally useful to capture things, so even units on defense
should take opportunities if they can. */
if (capture_indep_if_nearby(unit))
return;
if (capture_useful_if_nearby(unit))
return;
if (can_fire(unit)) {
/* No special goal, look for something to shoot at. */
if (fire_at_opportunity(unit)) {
execute_task(unit);
return;
}
/* Nothing to shoot at, so just hang out. */
/* (should consider moving to better shooting position if possible) */
} else if (can_attack(unit)) {
/* Consider moving a short ways to attack an interloper. */
range = 3 * u_acp(u);
if (go_after_victim(unit, range)) {
execute_task(unit);
return;
}
/* Nobody close by, just hang out, shifting around a bit occasionally. */
if (mobile(unit->type) && probability(10)) {
if (random_point_near(unit->x, unit->y, u_acp(u), &x1, &y1)) {
DMprintf("%s to shift defensive position to %d,%d\n",
unit_desig(unit), x1, y1);
set_move_near_task(unit, x1, y1, 0);
execute_task(unit);
return;
}
}
} else {
/* No specific goal, no combat ability - so nothing to do! */
/* (but should test for ability to detonate or capture first) */
}
if (plan->tasks) {
execute_task(unit);
} else if (!plan->reserve) {
/* Just stay in reserve for now. */
DMprintf("%s going into defensive reserve\n", unit_desig(unit));
plan->reserve = TRUE;
} else {
/* can never get here? */
}
}
static void
plan_exploration(unit)
Unit *unit;
{
Plan *plan = unit->plan;
int u = unit->type;
int x, y, w, h, range, x1, y1;
/* If the world has no secrets, exploration is sort of pointless. */
if (all_see_all) {
plan->reserve = TRUE;
return;
}
if (resupply_if_low(unit)) {
if (plan->tasks)
execute_task(unit);
return;
}
if (repair_if_damaged(unit)) {
if (plan->tasks)
execute_task(unit);
return;
}
if (capture_indep_if_nearby(unit))
return;
if (capture_useful_if_nearby(unit))
return;
if (plan->tasks) {
/* (should see if a change of task is worthwhile) */
execute_task(unit);
return;
}
if (plan->maingoal) {
switch (plan->maingoal->type) {
case GOAL_VICINITY_KNOWN:
case GOAL_VICINITY_HELD:
if (mobile(u)) {
x = plan->maingoal->args[0]; y = plan->maingoal->args[1];
w = plan->maingoal->args[2]; h = plan->maingoal->args[3];
if (distance(x, y, unit->x, unit->y) > max(w, h)) {
/* Out of the area, move into it. */
if (random_point_near(x, y, max(w, h) / 2, &x1, &y1)) {
x = x1; y = y1;
}
DMprintf("%s to explore towards %d,%d\n",
unit_desig(unit), x, y);
set_move_near_task(unit, x, y, max(w, h) / 2);
} else {
if (explore_reachable_cell(unit, max(w, h) + 2)) {
/* Found a cell to explore. */
} else {
if (flip_coin()) {
DMprintf("%s clearing goal\n", unit_desig(unit));
plan->maingoal = NULL;
}
DMprintf("%s to walk randomly\n", unit_desig(unit));
random_walk(unit);
}
}
} else {
plan_explorer_support(unit);
}
break;
case GOAL_WORLD_KNOWN:
if (mobile(u)) {
if (explore_reachable_cell(unit, area.maxdim)) {
/* Found a cell to explore. */
} else {
DMprintf("%s to walk randomly\n", unit_desig(unit));
random_walk(unit);
}
} else {
plan_explorer_support(unit);
}
break;
default:
DMprintf("%s goal %s?\n",
unit_desig(unit), goal_desig(unit->plan->maingoal));
break;
}
} else {
/* No specific goal, just poke around. */
if (mobile(u)) {
range = area.maxdim / 2;
if (explore_reachable_cell(unit, range)) {
} else if (flip_coin()) {
/* (should call a plan eraser) */
unit->plan->type = PLAN_PASSIVE;
} else {
DMprintf("%s to walk randomly\n", unit_desig(unit));
random_walk(unit);
}
} else {
plan_explorer_support(unit);
}
}
if (plan->tasks) {
execute_task(unit);
} else {
if (probability(10)) {
DMprintf("no tasks, going to sleep (dunno why)\n");
plan->asleep = TRUE;
}
}
}
static void
plan_explorer_support(unit)
Unit *unit;
{
int u = unit->type, u2, u3, backup;
Task *task;
for_all_unit_types(u2) {
if (mobile(u2)
&& 1 /* better on more kinds of terrain? */
&& uu_acp_to_create(u, u2) > 0) {
backup = u2;
if (flip_coin()) {
u3 = u2;
break;
}
}
}
if (u3 == NONUTYPE)
u3 = backup;
if (u3 != NONUTYPE) {
task = unit->plan->tasks;
if (task == NULL || task->type != TASK_BUILD) {
DMprintf("%s supporting exploration by building %s\n",
unit_desig(unit), u_type_name(u3));
push_build_task(unit, u3, 2);
} else {
DMprintf("%s already building, leaving alone\n",
unit_desig(unit));
}
} else {
DMprintf("%s has no way to support exploration\n", unit_desig(unit));
}
}
static int victimx, victimy, victimrating, victimutype, victimsidenum;
int
victim_here(x, y)
int x, y;
{
int u2 = NONUTYPE, uview, rating, dist;
Unit *unit2;
Side *side = tmpunit->side, *oside = NULL;
if (units_visible(side, x, y)) {
if ((unit2 = unit_at(x, y)) != NULL) {
u2 = unit2->type;
oside = unit2->side;
} else {
return FALSE;
}
} else if (terrain_view(side, x, y) != UNSEEN
&& (uview = unit_view(side, x, y)) != EMPTY) {
u2 = vtype(uview);
oside = side_n(vside(uview));
} else {
return FALSE;
}
if (is_unit_type(u2)
&& enemy_side(side, oside)
&& ((could_hit(tmpunit->type, u2)
&& uu_damage(tmpunit->type, u2) > 0
&& (!worth_capturing(side, u2, oside, x, y)
|| capture_chance(tmpunit->type, u2, oside) > 0))
|| capture_chance(tmpunit->type, u2, oside) > 0)
) {
/* If our unit can't capture the prospective victim, but somebody
else is planning to do so, leave it alone and keep looking for
a victim. */
if ((uu_acp_to_attack(tmpunit->type, u2) < 1
|| capture_chance(tmpunit->type, u2, oside) <= 0)
&& side_planning_to_capture(side, u2, x, y)) {
return FALSE;
}
if (capture_chance(tmpunit->type, u2, oside) > 0) {
victimutype = u2;
victimsidenum = side_number(oside);
return TRUE;
}
if (tmpunit->occupant != NULL
&& could_hit(u2, tmpunit->type)
&& uu_damage(u2, tmpunit->type) > 0
/* and valuable occupants not protected... */
) {
return FALSE;
}
rating = uu_zz_bhw(tmpunit->type, u2);
/* Further-away units are less interesting than closer ones. */
dist = distance(tmpunit->x, tmpunit->y, x, y);
if (dist > u_acp(tmpunit->type)) {
rating /= max(1, isqrt(dist - u_acp(tmpunit->type)));
}
if (rating > victimrating || (rating == victimrating && flip_coin())) {
victimx = x; victimy = y;
victimrating = rating;
victimutype = u2;
victimsidenum = side_number(oside);
}
}
return FALSE;
}
/* This decides whether a given unit type seen at a given location is worth
trying to capture. */
int
worth_capturing(side, u2, side2, x, y)
Side *side, *side2;
int u2, x, y;
{
int u, bestchance = 0;
/* See how likely we are to be able to capture the type. */
for_all_unit_types(u) {
bestchance = max(capture_chance(u, u2, side2), bestchance);
}
if (bestchance == 0)
return FALSE;
/* (should account for other considerations too, like which types of units we have) */
return TRUE;
}
/* This routine looks for somebody, anybody to attack. */
int
go_after_victim(unit, range)
Unit *unit;
int range;
{
int x, y, rslt;
tmpunit = unit;
DMprintf("%s seeking victim within %d; found ",
unit_desig(unit), range);
victimrating = -9999;
rslt = search_around(unit->x, unit->y, range, victim_here, &x, &y, 1);
if (rslt) {
DMprintf("s%d %s at %d,%d\n", victimsidenum, u_type_name(victimutype), x, y);
/* Set up a task to go after the unit found. */
/* (should be able to set capture task if better) */
set_specific_hit_task(unit, x, y, victimutype, victimsidenum);
if (unit->transport != NULL
&& mobile(unit->transport->type)
&& unit->transport->plan) {
set_move_near_task(unit->transport, x, y, 1);
}
} else if (victimrating > -9999) {
DMprintf("s%d %s (rated %d) at %d,%d\n",
victimsidenum, u_type_name(victimutype), victimrating, victimx, victimy);
/* Set up a task to go after the unit found. */
/* (should be able to set capture task if better) */
set_specific_hit_task(unit, victimx, victimy, victimutype, victimsidenum);
if (unit->transport != NULL
&& mobile(unit->transport->type)
&& unit->transport->plan) {
set_move_near_task(unit->transport, victimx, victimy, 1);
}
/* We succeeded after all. */
rslt = TRUE;
} else {
DMprintf("nothing\n");
}
return rslt;
}
static int targetx, targety, targetrating, targetutype, targetsidenum;
int
target_here(x, y)
int x, y;
{
int u2 = NONUTYPE, uview, rating, dist;
Unit *unit2;
Side *side = tmpunit->side, *oside = NULL;
if (units_visible(side, x, y)) {
if ((unit2 = unit_at(x, y)) != NULL) {
u2 = unit2->type;
oside = unit2->side;
} else {
/* Nothing to shoot at here. */
return FALSE;
}
} else if (terrain_view(side, x, y) != UNSEEN
&& (uview = unit_view(side, x, y)) != EMPTY) {
u2 = vtype(uview);
oside = side_n(vside(uview));
} else {
/* Nothing (or at least nothing visible) to shoot at here. */
return FALSE;
}
if (is_unit_type(u2)
&& enemy_side(side, oside)
&& could_hit(tmpunit->type, u2)
&& uu_damage(tmpunit->type, u2) > 0
/* and have correct ammo */
&& !side_planning_to_capture(side, u2, x, y)
) {
rating = uu_zz_bfw(tmpunit->type, u2);
/* Further-away units are less interesting than closer ones. */
dist = distance(tmpunit->x, tmpunit->y, x, y);
if (dist > 0)
rating /= dist;
if (rating > targetrating || (rating == targetrating && flip_coin())) {
targetx = x; targety = y;
targetrating = rating;
targetutype = u2;
targetsidenum = side_number(oside);
}
}
return FALSE;
}
int
fire_at_opportunity(unit)
Unit *unit;
{
int x, y, range, rslt;
tmpunit = unit;
range = u_range(unit->type);
targetrating = -9999;
DMprintf("%s seeking target within %d; found ",
unit_desig(unit), range);
rslt = search_around(unit->x, unit->y, range, target_here, &x, &y, 1);
if (rslt) {
DMprintf("s%d %s at %d,%d\n", targetsidenum, u_type_name(targetutype), x, y);
/* Set up a task to shoot at the unit found. */
set_specific_hit_task(unit, x, y, targetutype, targetsidenum);
} else if (targetrating > -9999) {
DMprintf("s%d %s (rated %d) at %d,%d\n",
targetsidenum, u_type_name(targetutype), targetrating, x, y);
/* Set up a task to shoot at the unit found. */
set_specific_hit_task(unit, targetx, targety, targetutype, targetsidenum);
} else {
DMprintf("nothing\n");
}
return rslt;
}
/* This is called by individual plans to see if a particular unit type at a
particular location is scheduled to be captured. */
static int
side_planning_to_capture(side, u, x, y)
Side *side;
int u, x, y;
{
Task *task;
Unit *unit;
for_all_side_units(side, unit) {
if (in_play(unit)
&& unit->plan) {
for_all_tasks(unit->plan, task) {
if (task->type == TASK_CAPTURE
&& task->args[0] == x && task->args[1] == y
&& (task->args[2] == NONUTYPE || task->args[2] == u))
return TRUE;
if (task->type == TASK_HIT_UNIT
&& task->args[0] == x && task->args[1] == y
&& (task->args[2] == NONUTYPE || task->args[2] == u)
&& uu_capture(unit->type, u) > 0)
return TRUE;
}
}
}
return FALSE;
}
/* Check to see if our grand plans are at risk of being sideswiped by lack of
supply, and set up a resupply task if so. */
int
resupply_if_low(unit)
Unit *unit;
{
int u = unit->type, lowm;
Task *curtask = unit->plan->tasks;
if (!mobile(u))
return FALSE;
lowm = low_on_supplies_one(unit);
if (lowm != NONMTYPE) {
/* See if we're already moving to a supply source. */
if (curtask != NULL
&& curtask->type == TASK_MOVE_TO /* or other types? */
&& supplies_here(unit, curtask->args[0], curtask->args[1], lowm))
/* Let the movement task execute. */
return FALSE;
/* See if we already have a resupply task for the same material. */
if (curtask != NULL
&& curtask->type == TASK_RESUPPLY
&& curtask->args[0] == lowm)
return (execute_task(unit) != TASK_FAILED);
/* Otherwise set up a task. */
DMprintf("%s low on %s, looking for source\n",
unit_desig(unit), m_type_name(lowm));
set_resupply_task(unit, lowm);
return (execute_task(unit) != TASK_FAILED);
}
return FALSE;
}
/* Return a type of essential material that the unit is running out of. */
int
low_on_supplies_one(unit)
Unit *unit;
{
int u = unit->type, m;
for_all_material_types(m) {
if ((um_base_consumption(u, m) > 0 || um_consumption_per_move(u, m) > 0)
&& um_storage_x(u, m) > 0
&& unit->supply[m] <= (unit_doctrine(unit)->resupply_percent * um_storage_x(u, m)) / 100
) {
return m;
}
}
return NONMTYPE;
}
int
rearm_if_low(unit)
Unit *unit;
{
int u = unit->type, lowm;
Task *curtask = unit->plan->tasks;
if (!mobile(u))
return FALSE;
lowm = low_on_ammo_one(unit);
if (lowm != NONMTYPE) {
if (curtask != NULL
&& curtask->type == TASK_MOVE_TO /* or other types? */
&& supplies_here(unit, curtask->args[0], curtask->args[1], lowm))
/* Let the movement task execute. */
return FALSE;
/* See if we already have a resupply task for the same material. */
if (curtask != NULL
&& curtask->type == TASK_RESUPPLY
&& curtask->args[0] == lowm)
return (execute_task(unit) != TASK_FAILED);
/* Otherwise set up a task. */
DMprintf("%s low on %s, looking for source\n",
unit_desig(unit), m_type_name(lowm));
set_resupply_task(unit, lowm);
return (execute_task(unit) != TASK_FAILED);
}
return FALSE;
}
/* Return a type of material that we want to use in combat. */
int
low_on_ammo_one(unit)
Unit *unit;
{
int u = unit->type, m;
for_all_material_types(m) {
if (um_consumption_per_attack(u, m) > 0
&& um_storage_x(u, m) > 0
&& unit->supply[m] <= (unit_doctrine(unit)->rearm_percent * um_storage_x(u, m)) / 100
) {
return m;
}
}
return NONMTYPE;
}
int
supplies_here(unit, x, y, m)
Unit *unit;
int x, y, m;
{
Unit *unit2;
for_all_stack(x, y, unit2) {
if (unit2 != unit
&& unit_trusts_unit(unit2, unit)
&& unit2->supply[m] > 0
&& um_outlength(unit2->type, m) >= 0) {
return TRUE;
}
}
return FALSE;
}
extern int repair_if_damaged PARAMS ((Unit *unit));
extern int repairs_here PARAMS ((Unit *unit, int x, int y));
int
repair_if_damaged(unit)
Unit *unit;
{
int u = unit->type;
Task *curtask = unit->plan->tasks;
if (unit->hp > (u_hp_max(u) * unit_doctrine(unit)->repair_percent) / 100)
return FALSE;
if (u_hp_recovery(u) <= 0 && !any_auto_repair)
return FALSE;
/* See if we already have a repair task. */
if (curtask != NULL
&& curtask->type == TASK_REPAIR)
return (execute_task(unit) != TASK_FAILED);
if (curtask != NULL
&& curtask->type == TASK_MOVE_TO /* or other types? */
&& repairs_here(unit, curtask->args[0], curtask->args[1]))
/* Just let the movement task execute. */
return FALSE;
/* (should test whether any repairing units in existence) */
/* Otherwise set up a task. */
DMprintf("%s badly damaged, looking for repairs\n", unit_desig(unit));
set_repair_task(unit);
return (execute_task(unit) != TASK_FAILED);
}
int
repairs_here(unit, x, y)
Unit *unit;
int x, y;
{
Unit *unit2;
for_all_stack(x, y, unit2) {
if (unit2 != unit
&& unit_trusts_unit(unit2, unit)
&& (uu_auto_repair(unit2->type, unit->type) > 0
|| 0 /* (should allow for explicit repair actions) */)) {
return TRUE;
}
}
return FALSE;
}
/* Look within a limited distance for any independent unit that could be
captured, and set up tasks to go get it. */
int
capture_indep_if_nearby(unit)
Unit *unit;
{
int u = unit->type, range = 5;
int x, y, rslt;
Task *curtask = unit->plan->tasks;
if (!mobile(u))
return FALSE;
if (!could_capture_any(unit->type))
return FALSE;
tmpunit = unit;
/* See if we're already doing such a task. */
if (curtask != NULL
&& ((curtask->type == TASK_MOVE_TO
&& indep_captureable_here(curtask->args[0], curtask->args[1]))
|| (curtask->type == TASK_CAPTURE
&& indep_captureable_here(curtask->args[0], curtask->args[1]))))
return FALSE;
DMprintf("%s searching for easy capture within %d; found ",
unit_desig(unit), range);
rslt = search_around(unit->x, unit->y, range, indep_captureable_here,
&x, &y, 1);
if (rslt) {
DMprintf("one at %d,%d\n", x, y);
/* Set up a task to go after the unit found. */
set_capture_task(unit, x, y);
if (unit->transport
&& mobile(unit->transport->type)
&& unit->transport->plan) {
set_move_near_task(unit->transport, x, y, 1);
}
return (execute_task(unit) != TASK_FAILED);
} else {
DMprintf("nothing\n");
}
return FALSE;
}
int
indep_captureable_here(x, y)
int x, y;
{
int u2 = NONUTYPE, uview;
Unit *unit2;
Side *side = tmpunit->side, *side2 = NULL;
/* Collect a type and side of unit at the given location. */
if (units_visible(side, x, y)) {
if ((unit2 = unit_at(x, y)) != NULL) {
u2 = unit2->type;
side2 = unit2->side;
} else {
return FALSE;
}
} else if (terrain_view(side, x, y) != UNSEEN
&& (uview = unit_view(side, x, y)) != EMPTY) {
u2 = vtype(uview);
side2 = side_n(vside(uview));
} else {
return FALSE;
}
return (is_unit_type(u2)
&& side2 == NULL
&& capture_chance(tmpunit->type, u2, side2) > 10);
}
/* Look within a limited distance for any type of unit that would be good to
own, and set up tasks to go get it. */
int
capture_useful_if_nearby(unit)
Unit *unit;
{
int u = unit->type, range = 2;
int x, y, rslt;
Task *curtask = unit->plan->tasks;
if (!mobile(u))
return FALSE;
if (!could_capture_any(unit->type))
return FALSE;
tmpunit = unit;
/* See if we're already doing such a task. */
if (curtask != NULL
&& ((curtask->type == TASK_MOVE_TO
&& useful_captureable_here(curtask->args[0], curtask->args[1]))
|| (curtask->type == TASK_CAPTURE
&& useful_captureable_here(curtask->args[0], curtask->args[1]))))
return FALSE;
DMprintf("%s searching for useful capture within %d; found ",
unit_desig(unit), range);
rslt = search_around(unit->x, unit->y, range, useful_captureable_here,
&x, &y, 1);
if (rslt) {
DMprintf("one at %d,%d\n", x, y);
/* Set up a task to go after the unit found. */
set_capture_task(unit, x, y);
if (unit->transport
&& mobile(unit->transport->type)
&& unit->transport->plan) {
set_move_near_task(unit->transport, x, y, 1);
}
return (execute_task(unit) != TASK_FAILED);
} else {
DMprintf("nothing\n");
}
return FALSE;
}
int
useful_captureable_here(x, y)
int x, y;
{
int u2 = NONUTYPE, uview;
Unit *unit2;
Side *side = tmpunit->side, *oside = NULL;
if (units_visible(side, x, y)) {
for_all_stack(x, y, unit2) {
u2 = unit2->type;
oside = unit2->side;
if (is_unit_type(u2)
&& !trusted_side(side, oside)
&& capture_chance(tmpunit->type, u2, oside) > 0
&& useful_type(side, u2)
) {
tmputype = u2;
return TRUE;
}
}
} else if (terrain_view(side, x, y) != UNSEEN
&& (uview = unit_view(side, x, y)) != EMPTY) {
u2 = vtype(uview);
oside = side_n(vside(uview));
if (is_unit_type(u2)
&& !trusted_side(side, oside)
&& capture_chance(tmpunit->type, u2, oside) > 0
&& useful_type(side, u2)
) {
tmputype = u2;
return TRUE;
}
}
return FALSE;
}
/* Return true if the given type of unit is useful in some way to the
given side. This is almost always true. */
int
useful_type(side, u)
Side *side;
int u;
{
if (!type_allowed_on_side(u, side))
return FALSE;
return TRUE;
}
int
could_capture_any(u)
int u;
{
int u2;
for_all_unit_types(u2) {
if (uu_capture(u, u2) > 0 || uu_indep_capture(u, u2) > 0)
return TRUE;
/* also check if u2 in game, on other side, etc? */
}
return FALSE;
}
/* This is a semi-testing routine that basically picks something for the
unit to do without worrying about its validity. The rest of the
system should function correctly no matter what this thing comes up with.
Piles of warnings are likely, but that's OK. */
/* (should actually have two modes for this - testing, which allows bad data,
and normal usage with no garbage generated, which is used with unintelligent
AIs) */
static void
plan_random(unit)
Unit *unit;
{
int dir, x1, y1, n;
TaskType tasktype;
Task *task;
Action action;
char *argtypestr;
if (flip_coin()) {
if (unit->plan->tasks) {
execute_task(unit);
return;
}
/* Pick a random task. */
tasktype = xrandom((int) NUMTASKTYPES);
switch (tasktype) {
case TASK_NONE:
task = create_task(tasktype);
add_task(unit, CLEAR_AGENDA, task);
break;
case TASK_BUILD:
task = create_build_task(xrandom(numutypes), xrandom(99));
add_task(unit, CLEAR_AGENDA, task);
break;
case TASK_RESEARCH:
task = create_task(tasktype);
add_task(unit, CLEAR_AGENDA, task);
break;
case TASK_CAPTURE:
dir = random_dir();
point_in_dir(unit->x, unit->y, dir, &x1, &y1);
set_capture_task(unit, x1, y1);
break;
case TASK_DO_ACTION:
task = create_task(tasktype);
add_task(unit, CLEAR_AGENDA, task);
break;
case TASK_HIT_POSITION:
task = create_task(tasktype);
add_task(unit, CLEAR_AGENDA, task);
break;
case TASK_HIT_UNIT:
task = create_task(tasktype);
add_task(unit, CLEAR_AGENDA, task);
break;
case TASK_MOVE_DIR:
dir = random_dir();
n = xrandom(10);
set_move_dir_task(unit, dir, n);
break;
case TASK_MOVE_TO:
dir = random_dir();
point_in_dir(unit->x, unit->y, dir, &x1, &y1);
set_move_to_task(unit, x1, y1);
break;
case TASK_OCCUPY:
task = create_task(tasktype);
add_task(unit, CLEAR_AGENDA, task);
break;
case TASK_PICKUP:
task = create_task(tasktype);
add_task(unit, CLEAR_AGENDA, task);
break;
case TASK_PRODUCE:
if (nummtypes > 0) {
task = create_produce_task((flip_coin() ? xrandom(nummtypes) : NONMTYPE), xrandom(100));
add_task(unit, CLEAR_AGENDA, task);
}
break;
case TASK_REPAIR:
set_repair_task(unit);
break;
case TASK_DISBAND:
set_disband_task(unit);
break;
case TASK_RESUPPLY:
if (nummtypes > 0) {
set_resupply_task(unit, (flip_coin() ? xrandom(nummtypes) : NONMTYPE));
}
break;
case TASK_SENTRY:
set_sentry_task(unit, xrandom(5));
break;
default:
case_panic("task type", tasktype);
break;
}
}
if (unit->plan && unit->plan->tasks)
return;
/* Otherwise go for a random action. */
memset(&action, 0, sizeof(Action));
action.type = (ActionType) xrandom((int) NUMACTIONTYPES);
argtypestr = actiondefns[(int) action.type].argtypes;
make_plausible_random_args(argtypestr, 0, &(action.args[0]), unit);
if (flip_coin()) {
action.actee = unit->id;
} else {
while (find_unit(action.actee = xrandom(numunits)+1) == NULL
&& probability(98));
}
unit->act->nextaction = action;
DMprintf("%s will randomly try %s\n",
unit_desig(unit), action_desig(&action));
}
/* This attempts to make some vaguely plausible arguments for an action,
using the types of each arg as a guide. It also generates *invalid*
arguments occasionally, which tests error checking in the actions' code. */
void
make_plausible_random_args(argtypestr, i, args, unit)
char *argtypestr;
int i;
short *args;
Unit *unit;
{
char argch;
int slen, arg;
slen = strlen(argtypestr);
while (i < slen && i < 10) {
argch = argtypestr[i];
switch (argch) {
case 'n':
arg = (flip_coin() ? xrandom(10) :
(flip_coin() ? xrandom(100) :
(xrandom(20000) - 10000)));
break;
case 'u':
/* Go a little outside range, so as to get some invalid types. */
arg = xrandom(numutypes + 2) - 1;
break;
case 'm':
arg = xrandom(nummtypes + 2) - 1;
break;
case 't':
arg = xrandom(numttypes + 2) - 1;
break;
case 'x':
arg = (unit != NULL && flip_coin() ? (unit->x + xrandom(5) - 2) :
(xrandom(area.width + 4) - 2));
break;
case 'y':
arg = (unit != NULL && flip_coin() ? (unit->y + xrandom(5) - 2) :
(xrandom(area.height + 4) - 2));
break;
case 'z':
arg = (flip_coin() ? 0 : xrandom(10));
break;
case 'd':
arg = random_dir();
break;
case 'U':
/* Cast around for a valid unit. */
while (find_unit(arg = xrandom(numunits)+1) == NULL
&& probability(98));
break;
case 'S':
arg = xrandom(numsides + 3) - 1;
break;
default:
run_warning("Garbled action arg type '%c'\n", argch);
arg = 0;
break;
}
args[i++] = arg;
}
}
/* Random walking just attempts to move around. */
void
random_walk(unit)
Unit *unit;
{
int dir = random_dir(), x1, y1, tries = 0;
while (!interior_point_in_dir(unit->x, unit->y, dir, &x1, &y1)) {
if (++tries > 500) {
run_warning("something is wrong");
break;
}
dir = random_dir();
}
set_move_to_task(unit, x1, y1);
}
/* Record the unit as waiting for orders about what to do. */
void
set_waiting_for_tasks(unit, flag)
Unit *unit;
int flag;
{
unit->plan->waitingfortasks = flag;
if (unit->side != NULL)
unit->side->numwaiting += (unit->plan->waitingfortasks ? 1 : -1);
}
/* Put a unit in reserve. */
void
reserve_unit(side, unit)
Side *side;
Unit *unit;
{
if (unit->plan) {
unit->plan->reserve = TRUE;
set_waiting_for_tasks(unit, FALSE);
update_unit_display(side, unit, TRUE);
}
}
/* General routine to wake a unit up (and maybe all its cargo). */
void
wake_unit(side, unit, wakeocc)
Side *side;
Unit *unit;
int wakeocc;
{
Unit *occ;
/* (should test that side is permitted to wake) */
if (unit->plan) {
unit->plan->asleep = FALSE;
unit->plan->reserve = FALSE;
update_unit_display(side, unit, TRUE);
}
if (wakeocc) {
for_all_occupants(unit, occ)
wake_unit(side, occ, wakeocc);
}
}
/* The area wakeup. */
static int tmpflag;
static void
wake_at(x, y)
int x, y;
{
Unit *unit;
for_all_stack(x, y, unit) {
if (side_controls_unit(tmpside, unit)) {
wake_unit(tmpside, unit, tmpflag);
}
}
}
void
wake_area(side, x, y, n, occs)
Side *side;
int x, y, n, occs;
{
tmpside = side;
tmpflag = occs;
apply_to_area(x, y, n, wake_at);
}
void
set_formation(unit, leader, x, y, dist, flex)
Unit *unit, *leader;
int x, y, dist, flex;
{
Plan *plan = unit->plan;
Goal *goal;
if (plan == NULL)
return;
if (!in_play(unit))
return;
if (leader != NULL) {
if (!in_play(leader))
return;
goal = create_goal(GOAL_KEEP_FORMATION, unit->side, TRUE);
goal->args[0] = leader->id;
} else {
if (!inside_area(x, y))
return;
goal = create_goal(GOAL_KEEP_FORMATION, unit->side, TRUE);
goal->args[0] = 0;
}
goal->args[1] = x; goal->args[2] = y;
goal->args[3] = dist;
goal->args[4] = flex;
plan->formation = goal;
plan->funit = leader;
}
void
delay_unit(unit, flag)
Unit *unit;
int flag;
{
if (in_play(unit) && unit->plan) {
unit->plan->delayed = TRUE;
}
}
/* Return the distance that we can go by shortest path before running out
of important supplies. Will return at least 1, since we can *always*
move one cell to safety. This is a worst-case routine, too complicated
to worry about units getting refreshed by terrain or whatever. */
int
range_left(unit)
Unit *unit;
{
int u = unit->type, m, least = 12345; /* bigger than any real value */
for_all_material_types(m) {
if (um_consumption_per_move(u, m) > 0) {
least = min(least, unit->supply[m] / um_consumption_per_move(u, m));
}
#if 0
/* This code is too pessimistic if no account taken of supply line or
production, so leave out for now. */
if (um_base_consumption(u, m) > 0) {
tmp = (u_speed(u) * unit->supply[m]) / um_base_consumption(u, m);
least = min(least, tmp);
}
#endif
}
return (least == 12345 ? 1 : least);
}
/* Estimate the goodness and badness of cells in the immediate vicinity. */
int
find_worths(range)
int range;
{
return 0;
}
/* This is a heuristic estimation of the value of one unit type
hitting on another. Should take cost of production into account as
well as the chance and significance of any effect. */
int
attack_worth(unit, e)
Unit *unit;
int e;
{
int u = unit->type, worth;
worth = uu_zz_bhw(u, e);
/* Risk of death? */
/* if (uu_damage(e, u) >= unit->hp)
worth /= (could_capture(u, e) ? 1 : 4);
if (could_capture(u, e)) worth *= 4; */
return worth;
}
/* Support functions. */
/* Return true if the given position is threatened by the given unit type. */
int
threat(side, u, x0, y0)
Side *side;
int u, x0, y0;
{
int d, x, y, thr = 0;
Side *side2;
int view;
for_all_directions(d) {
point_in_dir(x0, y0, d, &x, &y);
view = 0 /* side_view(side, x, y) */;
if (view != UNSEEN && view != EMPTY) {
side2 = side_n(vside(view));
if (allied_side(side, side2)) {
if (uu_capture(u, vtype(view)) > 0) thr += 1000;
if (uu_zz_bhw(u, vtype(view)) > 0) thr += 100;
}
}
}
return thr;
}
void
pop_task(plan)
Plan *plan;
{
Task *oldtask;
if (plan->tasks) {
oldtask = plan->tasks;
plan->tasks = plan->tasks->next;
free_task(oldtask);
}
}
int
react_to_enemies(unit)
Unit *unit;
{
return (unit->plan ? TRUE : FALSE);
}
/* Patrol just does move_to, but cycling waypoints around when the first */
/* one has been reached. */
int
move_patrol(unit)
Unit *unit;
{
#if 0
int tx, ty;
if (unit->plan->orders.rept-- > 0) {
if (unit->x == unit->plan->orders.p.pt[0].x &&
unit->y == unit->plan->orders.p.pt[0].y) {
tx = unit->plan->orders.p.pt[0].x;
ty = unit->plan->orders.p.pt[0].y;
unit->plan->orders.p.pt[0].x = unit->plan->orders.p.pt[1].x;
unit->plan->orders.p.pt[0].y = unit->plan->orders.p.pt[1].y;
unit->plan->orders.p.pt[1].x = tx;
unit->plan->orders.p.pt[1].y = ty;
}
return move_to(unit, unit->plan->orders.p.pt[0].x, unit->plan->orders.p.pt[0].y,
(unit->plan->orders.flags & SHORTESTPATH));
}
#endif
return TRUE;
}
/* Basic routine to compute how long a unit will take to build something. */
int
build_time(unit, prod)
Unit *unit;
int prod;
{
int schedule = 1 /* uu_make(unit->type, prod) */;
int u, research_delay = 0;
/* Add penalty (or unpenalty!) for first unit of a type. */
/* is "counts" a reliable way to test? */
if (unit->side->counts[prod] <= 1) {
/* research_delay = ((schedule * u_research(prod)) / 100); */
for_all_unit_types(u) {
if (unit->side->counts[u] > 1) {
research_delay -=
(1 /*uu_make(unit->type, u)*/ *
uu_tech_crossover(prod, u)) / 100;
}
if (research_delay > 0) {
schedule += research_delay;
}
}
}
return schedule;
}
int
clear_task_agenda(plan)
Plan *plan;
{
int numcleared;
Task *oldtask;
if (plan == NULL || plan->tasks == NULL)
return 0;
numcleared = 0;
while (plan->tasks != NULL) {
oldtask = plan->tasks;
plan->tasks = plan->tasks->next;
free_task(oldtask);
++numcleared;
}
return numcleared;
}
Plan *
create_plan()
{
Plan *plan = (Plan *) xmalloc(sizeof(Plan));
return plan;
}
void
free_plan(plan)
Plan *plan;
{
if (plan == NULL)
run_error("no plan here?");
/* Make tasks available for reallocation. */
clear_task_agenda(plan);
free(plan);
}
/* Describe a plan succinctly. This is primarily for debugging, not
for normal user display. */
char *planbuf = NULL;
char *
plan_desig(plan)
Plan *plan;
{
Task *task;
int extra = 0;
if (planbuf == NULL)
planbuf = xmalloc(1000);
if (plan == NULL) {
sprintf(planbuf, "no plan");
} else if (plan->type == PLAN_NONE) {
sprintf(planbuf, "unformed plan");
} else {
if (plan->tasks) {
tmpbuf[0] = '\0';
for_all_tasks(plan, task) {
if (strlen(tmpbuf) < 100) {
strcat(tmpbuf, " ");
strcat(tmpbuf, task_desig(task));
} else {
++extra;
}
}
if (extra > 0) {
tprintf(tmpbuf, " ... %d more ...", extra);
}
} else {
sprintf(tmpbuf, " no tasks");
}
sprintf(planbuf, "type %s %s",
plantypenames[plan->type], goal_desig(plan->maingoal));
if (plan->formation) {
strcat(planbuf, " ");
strcat(planbuf, goal_desig(plan->formation));
}
if (plan->asleep)
strcat(planbuf, " asleep");
if (plan->reserve)
strcat(planbuf, " reserve");
if (plan->delayed)
strcat(planbuf, " delayed");
if (plan->waitingfortasks)
strcat(planbuf, " waiting");
if (plan->supply_alarm)
strcat(planbuf, " supply_alarm");
if (plan->supply_is_low)
strcat(planbuf, " supply_is_low");
strcat(planbuf, tmpbuf);
}
return planbuf;
}
/* True if unit is in immediate danger of being captured. */
/* Needs check on capturer transport being seen. */
int
might_be_captured(unit)
Unit *unit;
{
int d, x, y;
Unit *unit2;
for_all_directions(d) {
if (interior_point_in_dir(unit->x, unit->y, d, &x, &y)) {
if (((unit2 = unit_at(x, y)) != NULL) &&
(enemy_side(unit->side, unit2->side)) &&
(uu_capture(unit2->type, unit->type) > 0))
return TRUE;
}
}
return FALSE;
}
/* Clear a unit's plan out. */
void
force_replan(side, unit, passive_only)
Side *side;
Unit *unit;
int passive_only;
{
extern int need_ai_planning;
if (unit->plan == NULL)
return;
if ((passive_only ? unit->plan->type == PLAN_PASSIVE : TRUE)) {
unit->plan->type = PLAN_PASSIVE;
clear_task_agenda(unit->plan);
}
unit->plan->maingoal = NULL;
unit->plan->formation = NULL;
unit->plan->funit = NULL;
unit->plan->asleep = FALSE;
unit->plan->reserve = FALSE;
set_waiting_for_tasks(unit, FALSE);
unit->plan->delayed = FALSE;
need_ai_planning = TRUE;
}
/* Auxiliary functions for unit planning in Xconq. */
/* router flags */
#define SAMEPATH 1
#define EXPLORE_PATH 2
/* These macros are a cache used for planning purposes by machines. */
#define markloc(x, y) (set_tmp1_at(x, y, mark))
#define markedloc(x, y) (tmp1_at(x, y) == mark)
#define get_fromdir(x, y) (tmp2_at(x, y))
#define set_fromdir(x, y, dir) (set_tmp2_at(x, y, dir))
#define get_dist(x, y) (tmp3_at(x, y))
#define set_dist(x, y, d) (set_tmp3_at(x, y, d))
int
occupant_could_capture(unit, u2)
Unit *unit;
int u2;
{
Unit *occ;
for_all_occupants(unit, occ)
if (uu_capture(occ->type, u2) > 0)
return TRUE;
return FALSE;
}
/* Check to see if there is anyone around to capture. */
int
can_capture_neighbor(unit)
Unit *unit;
{
int d, x, y;
int view;
Side *side2;
for_all_directions(d) {
if (interior_point_in_dir(unit->x, unit->y, d, &x, &y)) {
view = unit_view(unit->side, x, y);
if (view != UNSEEN && view != EMPTY) {
side2 = side_n(vside(view));
if (!allied_side(unit->side, side2)) {
if (uu_capture(unit->type, vtype(view)) > 0) {
/* need some other way to change move order quickly */
return TRUE;
}
}
}
}
}
return FALSE;
}
/* check if our first occupant can capture something. Doesn't look at
other occupants. */
int
occupant_can_capture_neighbor(unit)
Unit *unit;
{
Unit *occ = unit->occupant;
if (occ != NULL && occ->act && occ->act->acp > 0 && occ->side == unit->side) {
if (can_capture_neighbor(occ)) {
return TRUE;
}
}
return FALSE;
}
/* Find the closes unit, first prefering bases, and then transports. */
int
find_closest_unit(side, x0, y0, maxdist, pred, rxp, ryp)
Side *side;
int x0, y0, maxdist, (*pred)(), *rxp, *ryp;
{
#if 0
Unit *unit;
int u, dist;
int found = FALSE;
for_all_unit_types(u) {
if (u_is_base(u)) {
for (unit = NULL /* side_strategy(side)->unitlist[u]*/; unit != NULL; unit = unit->mlist) {
if (alive(unit) &&
(dist = distance(x0, y0, unit->x, unit->y)) <= maxdist) {
if ((*pred)(unit->x, unit->y)) {
maxdist = dist - 1;
*rxp = unit->x; *ryp = unit->y;
found = TRUE;
}
}
}
}
}
if (found) {
return TRUE;
}
for_all_unit_types(u) {
if (!u_is_base(u) && u_is_transport(u)) {
for (unit = NULL /*side_strategy(side)->unitlist[u]*/; unit != NULL; unit = unit->mlist) {
if (alive(unit)
&& distance(x0, y0, unit->x, unit->y) <= maxdist) {
if ((*pred)(unit->x, unit->y)) {
maxdist = dist - 1;
*rxp = unit->x; *ryp = unit->y;
found = TRUE;
}
}
}
}
}
if (found) {
return TRUE;
}
/* (What's the point of finding a non-base/non-transport?) */
for_all_unit_types(u) {
if (!u_is_base(u) && !u_is_transport(u)) {
for (unit = NULL/*side_strategy(side)->unitlist[u]*/; unit != NULL; unit = unit->mlist) {
if (alive(unit)
&& distance(x0, y0, unit->x, unit->y) <= maxdist) {
if ((*pred)(unit->x, unit->y)) {
maxdist = dist - 1;
*rxp = unit->x; *ryp = unit->y;
found = TRUE;
}
}
}
}
}
if (found) {
return TRUE;
}
#endif
return FALSE;
}
/* True if the given unit is a sort that can build other units. */
int
can_build(unit)
Unit *unit;
{
int u2;
for_all_unit_types(u2) {
if (could_create(unit->type, u2))
return TRUE;
}
return FALSE;
}
int
can_build_or_help(unit)
Unit *unit;
{
int u2;
for_all_unit_types(u2) {
if (could_create(unit->type, u2)
|| uu_acp_to_build(unit->type, u2) > 0
|| uu_acp_to_research(unit->type, u2) > 0)
return TRUE;
}
return FALSE;
}
int
can_produce(unit)
Unit *unit;
{
int m;
for_all_material_types(m) {
if (um_acp_to_produce(unit->type, m))
return TRUE;
}
return FALSE;
}
/* Test if unit can move out into adjacent cells. */
int
can_move(unit)
Unit *unit;
{
int d, x, y;
for_all_directions(d) {
if (interior_point_in_dir(unit->x, unit->y, d, &x, &y)) {
/* (should account for world-leaving options?) */
if (could_live_on(unit->type, terrain_at(x, y)))
return TRUE;
}
}
return FALSE;
}
/* Returns the type of missing supplies. Not great routine if first */
/* material is a type of ammo. */
int
out_of_ammo(unit)
Unit *unit;
{
int u = unit->type, r;
for_all_material_types(r) {
if (um_consumption_per_attack(u, r) > 0 && unit->supply[r] <= 0)
return r;
}
return (-1);
}
/* Check how long a unit can sit where it is */
int
survival_time(unit)
Unit *unit;
{
int u = unit->type, m, least = 99999, rate, tmp;
int t = terrain_at(unit->x, unit->y);
for_all_material_types(m) {
rate = (um_base_consumption(u, m)
- (um_base_production(u, m) * ut_productivity(u, t)) / 100);
if (rate > 0) {
tmp = unit->supply[m];
if (unit->act) {
tmp += unit->act->actualmoves * um_consumption_per_move(u, m);
}
least = min(least, tmp / rate);
}
}
return least;
}
int
usable_cell(unit, x, y)
Unit *unit;
int x, y;
{
int u = unit->type;
int view = unit_view(unit->side, x, y);
return (((view == EMPTY) || view == UNSEEN ||
(allied_side(side_n(vside(view)),unit->side) &&
could_carry(vtype(view), u))) &&
could_live_on(u, terrain_at(x, y)));
}
Task *explorechain;
int
explorable_cell(x, y)
int x, y;
{
return (terrain_view(tmpside, x, y) == UNSEEN);
}
int
reachable_unknown(x, y)
int x, y;
{
if (!inside_area(x, y))
return FALSE;
if (terrain_view(tmpside, x, y) == UNSEEN) {
if (adj_known_ok_terrain(x, y, tmpside, tmpunit->type)) {
return TRUE;
} else {
return FALSE;
}
} else {
return FALSE;
}
}
/* Test whether the given location has an adjacent cell that is ok for
the given type to be out in the open. */
int
adj_known_ok_terrain(x, y, side, u)
int x, y, u;
Side *side;
{
int dir, x1, y1, t;
if (!inside_area(x, y) || side == NULL)
return FALSE;
for_all_directions(dir) {
if (interior_point_in_dir(x, y, dir, &x1, &y1)) {
if (terrain_view(side, x1, y1) == UNSEEN)
continue;
t = terrain_at(x1, y1);
if (!terrain_always_impassable(u, t))
return TRUE;
}
}
return FALSE;
}
/* Go to the nearest cell that we can see how to get to. */
int
explore_reachable_cell(unit, range)
Unit *unit;
int range;
{
int x, y;
if (all_see_all || g_terrain_seen())
return FALSE;
tmpunit = unit;
tmpside = unit->side;
DMprintf("%s searching within %d for cell to explore -", unit_desig(unit), range);
if (search_around(unit->x, unit->y, range, reachable_unknown, &x, &y, 1)) {
set_move_near_task(unit, x, y, 1);
DMprintf("found one at %d,%d\n", x, y);
return TRUE;
}
DMprintf("found nothing\n");
return FALSE;
}
/* Check for any makers this unit should be capturing. */
int
should_capture_maker(unit)
Unit *unit;
{
return 0;
}
#if 0
/* Returns true if the given unit can't leave its cell for some reason. */
int
no_possible_moves(unit)
Unit *unit;
{
int fx = unit->x, fy = unit->y, ut = unit->type;
int d, x, y;
int view;
Side *side = unit->side;
for_all_directions(d) {
x = wrapx(fx + dirx[d]); y = limity(fy + diry[d]);
view = unit_view(side, x, y);
if (view == EMPTY) {
if (could_move(ut, terrain_at(x, y)))
return FALSE;
} else if (enemy_side(side_n(vside(view)) , side)
&& could_hit(ut, vtype(view))) {
return FALSE;
} else if (could_carry(vtype(view), ut) &&
allied_side(side_n(vside(view)), side))
return FALSE;
}
return TRUE;
}
#endif
/* Estimate the usual number of turns to finish construction. */
int
normal_completion_time(u, u2)
int u, u2;
{
if (u_acp(u) == 0 || uu_cp_per_build(u, u2) == 0)
return (-1);
return (u_cp(u2) - uu_creation_cp(u, u2)) /
(uu_cp_per_build(u, u2) * u_acp(u));
}
/* True if anybody at all is on any adjacent cell. */
int
adj_unit(x, y)
int x, y;
{
int d, x1, y1;
for_all_directions(d) {
if (interior_point_in_dir(x, y, d, &x1, &y1)) {
if (unit_at(x1, y1))
return TRUE;
}
}
return FALSE;
}
/* A unit runs low on supplies at the halfway point. Formula is the same
no matter how/if occupants eat transports' supplies. */
int
past_halfway_point(unit)
Unit *unit;
{
int u = unit->type, m;
for_all_material_types(m) {
if (((um_base_consumption(u, m) > 0) || (um_consumption_per_move(u, m) > 0)) &&
/* should check that the transport is adequate for supplying the fuel */
(unit->transport == NULL)) {
if (2 * unit->supply[m] <= um_storage_x(u, m))
return TRUE;
}
}
return FALSE;
}
/* This is the maximum distance from "home" that a unit can expect to get,
travelling on its most hostile terrain type. */
int
operating_range_worst(u)
int u;
{
int m, t, prod, range, worstrange = area.maxdim;
for_all_material_types(m) {
if (um_base_consumption(u, m) > 0) {
for_all_terrain_types(t) {
if (!terrain_always_impassable(u, t)) {
prod = (um_base_production(u, m) * ut_productivity(u, t)) / 100;
if (prod < um_base_consumption(u, m)) {
range = um_storage_x(u, m) / (um_base_consumption(u, m) - prod);
if (range < worstrange)
worstrange = range;
}
}
}
}
}
return worstrange;
}
/* Same, but for best terrain. */
int
operating_range_best(u)
int u;
{
int m, t, prod, range, tbestrange, tbest = 0, bestrange = 0;
int moves, consump;
for_all_terrain_types(t) {
if (!terrain_always_impassable(u, t)) {
tbestrange = area.maxdim;
for_all_material_types(m) {
consump = 0;
moves = (u_acp(u) * u_speed(u)) / 100;
if (um_consumption_per_move(u, m) > 0) {
consump = moves * um_consumption_per_move(u, m);
}
if (moves <= 0)
moves = 1;
if (um_base_consumption(u, m) > 0) {
consump = max(consump, um_base_consumption(u, m));
}
prod = (um_base_production(u, m) * ut_productivity(u, t)) / 100;
if (prod > 0) {
consump = max(0, consump - prod);
}
if (consump > 0) {
range = (um_storage_x(u, m) * moves) / consump;
if (range < tbestrange)
tbestrange = range;
}
}
if (tbestrange > bestrange) {
bestrange = tbestrange;
tbest = t;
}
}
}
return bestrange;
}
void
dispose_of_plan(unit)
Unit *unit;
{
/* Might not be anything to dispose of. */
if (unit->plan == NULL)
return;
free_plan(unit->plan);
unit->plan = NULL;
}
int
lookup_plan_type(name)
char *name;
{
int i;
extern char *plantypenames[];
for (i = 0; plantypenames[i] != NULL; ++i)
/* should get real enum */
if (strcmp(name, plantypenames[i]) == 0)
return i;
return PLAN_NONE;
}
|