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
|
/* Unit task execution and general task functions.
Copyright (C) 1992-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 need_ai_task_reaction;
extern int attack_can_damage_or_capture PARAMS ((Unit *unit, Unit *unit2));
extern int target_visible PARAMS ((Unit *unit, Task *task));
#include "kernel.h"
extern Task *create_repair_task PARAMS ((void));
extern void set_repair_task PARAMS ((Unit *unit));
/* This is the number of tasks to allocate initially. More will always be
allocated as needed, so this should be a "reasonable" value. */
#ifndef INITMAXTASKS
#define INITMAXTASKS 100
#endif
#define CLEAR_AGENDA 99
static int compare_directions PARAMS ((const void *a0, const void *a1));
static int test_for_buildable PARAMS ((int x, int y));
static int repair_test PARAMS ((int x, int y));
static int resupply_test PARAMS ((int x, int y));
/* Declare all the task functions. */
#undef DEF_TASK
#define DEF_TASK(name,code,argtypes,FN) \
static TaskOutcome FN PARAMS ((Unit *unit, Task *task));
#include "task.def"
/* Array of descriptions of task types. */
TaskDefn taskdefns[] = {
#undef DEF_TASK
#define DEF_TASK(NAME,code,ARGTYPES,FN) { NAME, ARGTYPES, FN },
#include "task.def"
{ NULL, NULL, NULL }
};
/* The list of available task objects. */
Task *freetasks;
/* Pointer to a buffer that task debug info goes into. */
char *taskbuf;
static int tmpbuildutype;
static Unit *tmpbuilder, *tmpbuildunit;
/* Allocate an initial collection of task objects. */
void
init_tasks()
{
allocate_task_block();
}
/* Allocate a new block of tasks. */
void
allocate_task_block()
{
int i;
freetasks = (Task *) xmalloc(INITMAXTASKS * sizeof(Task));
/* Chain the tasks together. */
for (i = 0; i < INITMAXTASKS; ++i) {
freetasks[i].next = &freetasks[i+1];
}
freetasks[INITMAXTASKS-1].next = NULL;
}
/* Create and return a new task. */
Task *
create_task(type)
TaskType type;
{
int i;
Task *task;
/* Scarf up some more memory if we need it. */
if (freetasks == NULL) {
allocate_task_block();
}
/* Peel off a task from the free list. */
task = freetasks;
freetasks = task->next;
/* Reset its slots. */
task->type = type;
task->execnum = 0;
task->retrynum = 0;
for (i = 0; i < MAXTASKARGS; ++i)
task->args[i] = 0;
task->next = NULL;
return task;
}
Task *
clone_task(oldtask)
Task *oldtask;
{
int i;
Task *newtask;
newtask = create_task(oldtask->type);
/* Probably not a good idea to copy exec/retry counts, skip them. */
for (i = 0; i < MAXTASKARGS; ++i)
newtask->args[i] = oldtask->args[i];
return newtask;
}
/* The empty task always succeeds immediately. */
static TaskOutcome
do_none_task(unit, task)
Unit *unit;
Task *task;
{
return TASK_IS_COMPLETE;
}
static int
test_for_buildable(x, y)
int x, y;
{
Unit *unit;
for_all_stack(x, y, unit) {
if (in_play(unit)
&& !fullsized(unit)
&& unit->type == tmpbuildutype
&& unit->side == tmpbuilder->side) {
tmpbuildunit = unit;
return TRUE;
}
}
return FALSE;
}
/* The build task handles the research, tooling up, creation, and completion
for a given number of units of a given type. */
static TaskOutcome
do_build_task(unit, task)
Unit *unit;
Task *task;
{
int u = unit->type, dir, nx, ny, tp;
int x = unit->x, y = unit->y;
int u2 = task->args[0], run = task->args[3];
Unit *unit2 = NULL;
Side *us = unit->side;
/* First see if we've already built all the units requested. */
if (task->args[2] >= run) {
return TASK_IS_COMPLETE;
}
/* See if our technology needs improvement in order to build this type. */
if (is_unit_type(u2)
&& u_tech_to_build(u2) > 0
&& us->tech[u2] < u_tech_to_build(u2)) {
if (uu_acp_to_research(u, u2) > 0) {
if (valid(check_research_action(unit, unit, u2))) {
prep_research_action(unit, unit, u2);
return TASK_PREPPED_ACTION;
} else {
/* We get three trys to research before giving up. */
return (task->execnum < 3 ? TASK_IS_INCOMPLETE : TASK_FAILED);
}
} else {
/* Can't do the necessary research. */
return TASK_FAILED;
}
}
/* See if we need to toolup to work on this type. */
if (is_unit_type(u2)) {
tp = (unit->tooling ? unit->tooling[u2] : 0);
if (tp < uu_tp_to_build(u, u2)) {
if (uu_acp_to_toolup(u, u2) > 0) {
if (valid(check_toolup_action(unit, unit, u2))) {
prep_toolup_action(unit, unit, u2);
return TASK_PREPPED_ACTION;
} else {
/* We get three trys to toolup before giving up. */
return (task->execnum < 3 ? TASK_IS_INCOMPLETE : TASK_FAILED);
}
} else {
/* Can't do the necessary toolup. */
return TASK_FAILED;
}
}
}
/* Check out the unit supposedly in progress. */
if (task->args[1] != 0) {
unit2 = find_unit(task->args[1]);
if (in_play(unit2) && unit2->type == u2) {
if (fullsized(unit2)) {
/* Clear the reference to the unit. */
task->args[1] = 0;
++(task->args[2]);
if (task->args[2] >= run) {
return TASK_IS_COMPLETE;
}
}
}
}
/* Find something to work on */
unit2 = find_unit_to_complete(unit, task);
/* No incomplete unit found, so try to create one. */
if (unit2 == NULL) {
if (valid(check_create_in_action(unit, unit, u2, unit))) {
prep_create_in_action(unit, unit, u2, unit);
return TASK_PREPPED_ACTION;
} else if (valid(check_create_at_action(unit, unit, u2, x, y, 0))) {
prep_create_at_action(unit, unit, u2, x, y, 0);
return TASK_PREPPED_ACTION;
} else {
/* Try creating in an adjacent cell. */
for_all_directions(dir) {
if (interior_point_in_dir(x, y, dir, &nx, &ny)
&& valid(check_create_at_action(unit, unit, u2, nx, ny, 0))) {
prep_create_at_action(unit, unit, u2, nx, ny, 0);
return TASK_PREPPED_ACTION;
}
}
return TASK_FAILED;
}
} else {
/* Record the unit's id for use the next time around. */
task->args[1] = unit2->id;
}
/* We have an incomplete unit to work on, try to do a build action. */
if (valid(check_build_action(unit, unit, unit2))) {
prep_build_action(unit, unit, unit2);
return TASK_PREPPED_ACTION;
} else {
/* Try three times before giving up. */
return (task->execnum < 3 ? TASK_IS_INCOMPLETE : TASK_FAILED);
}
}
Unit *
find_unit_to_complete(unit, task)
Unit *unit;
Task *task;
{
Unit *occ;
int u = unit->type, nx, ny, range;
int u2 = task->args[0];
int x = unit->x, y = unit->y;
/* Check out the unit supposedly in progress. */
if (task->args[1] != 0) {
occ = find_unit(task->args[1]);
if (in_play(occ) && occ->type == u2 && !fullsized(occ))
return occ;
}
/* Maybe search for any appropriate incomplete occupants. */
for_all_occupants(unit, occ) {
if (in_play(occ)
&& !fullsized(occ)
&& occ->type == u2
&& occ->side == unit->side)
return occ;
}
/* Or else search for any appropriate incomplete units in this cell. */
for_all_stack(x, y, occ) {
if (in_play(occ)
&& !fullsized(occ)
&& occ->type == u2
&& occ->side == unit->side) {
return occ;
}
}
/* Or else search nearby area. */
if (is_unit_type(u2)) {
range = uu_build_range(u, u2);
if (range > 0) {
tmpbuilder = unit;
tmpbuildutype = u2;
if (search_around(x, y, range, test_for_buildable, &nx, &ny, 1)) {
return tmpbuildunit;
}
}
}
/* nothing found */
return NULL;
}
/* This is a "pure research" task, with the sole objective of increasing
technology. */
static TaskOutcome
do_research_task(unit, task)
Unit *unit;
Task *task;
{
int u = unit->type;
int u2 = task->args[0], lev = task->args[1];
Side *us = unit->side;
/* Independents can never ever do research. */
if (us == NULL)
return TASK_FAILED;
if (us->tech[u2] > u_tech_max(u2))
return TASK_FAILED; /* actually an error */
if (us->tech[u2] >= lev)
return TASK_IS_COMPLETE;
if (uu_acp_to_research(u, u2) <= 0)
return TASK_FAILED;
if (valid(check_research_action(unit, unit, u2))) {
prep_research_action(unit, unit, u2);
return TASK_PREPPED_ACTION;
} else {
/* We get three tries to research before giving up. */
return (task->execnum < 3 ? TASK_IS_INCOMPLETE : TASK_FAILED);
}
}
/* This is to capture a given type/side of unit at a given place. */
static TaskOutcome
do_capture_task(unit, task)
Unit *unit;
Task *task;
{
int u = unit->type, tx, ty, tu2, ts2, dist, movedist, rslt;
Unit *unit2;
Side *us = unit->side;
/* (should be able to say how hard to try) */
tx = task->args[0]; ty = task->args[1];
tu2 = task->args[2];
ts2 = task->args[3];
dist = distance(tx, ty, unit->x, unit->y);
switch (dist) {
case 0:
case 1:
for_all_stack(tx, ty, unit2) {
if ((ts2 >= 0 ?
(side_number(unit2->side) == ts2) :
(unit2->side != us /* should be "not a friendly side" */))
&& (tu2 == NONUTYPE || tu2 == unit->type)) {
if (valid(check_capture_action(unit, unit, unit2))) {
prep_capture_action(unit, unit, unit2);
return TASK_PREPPED_ACTION;
} else if (valid(check_attack_action(unit, unit, unit2, 100))) {
prep_attack_action(unit, unit, unit2, 100);
return TASK_PREPPED_ACTION;
} else {
/* We get several tries to capture before giving up. */
set_unit_reserve(unit->side, unit, TRUE, FALSE);
return (task->execnum < 5 ? TASK_IS_INCOMPLETE : TASK_FAILED);
}
}
}
/* Nothing was here to capture. */
return TASK_IS_COMPLETE;
default:
/* If on mobile transport, let it handle things. */
if (unit->transport != NULL
&& mobile(unit->transport->type)
/* and the transport is not blocked */
&& flip_coin()) {
return TASK_IS_INCOMPLETE;
}
/* If out of range and can move, push a task to get closer (usually). */
if (mobile(u) && probability(90)) {
push_move_near_task(unit, tx, ty, 1);
return TASK_IS_INCOMPLETE;
}
return TASK_FAILED;
}
}
/* The disband task's purpose is to make the unit disappear, so just keep doing
actions; when the unit goes away, so will the task. */
static TaskOutcome
do_disband_task(unit, task)
Unit *unit;
Task *task;
{
if (valid(check_disband_action(unit, unit))) {
prep_disband_action(unit, unit);
return TASK_PREPPED_ACTION;
} else {
/* (should try to find a nearby unit to do it?) */
return (task->execnum < 5 ? TASK_IS_INCOMPLETE : TASK_FAILED);
}
}
/* This task just attempts to do the explicitly specified action repeatedly. */
static TaskOutcome
do_action_task(unit, task)
Unit *unit;
Task *task;
{
int i;
if (task->args[0] > 0) {
--(task->args[0]);
if (unit->act == NULL)
return TASK_FAILED;
unit->act->nextaction.type = task->args[1];
for (i = 0; i < MAXACTIONARGS; ++i) {
unit->act->nextaction.args[i] = task->args[i + 2];
}
/* act on self always? */
unit->act->nextaction.actee = unit->id;
return TASK_PREPPED_ACTION;
} else {
return TASK_IS_COMPLETE;
}
}
static TaskOutcome
do_hit_position_task(unit, task)
Unit *unit;
Task *task;
{
int u = unit->type, tx, ty, dist;
/* This is to hit a given place. */
/* (ask for a number of hits?) */
tx = task->args[0]; ty = task->args[1];
dist = distance(tx, ty, unit->x, unit->y);
if (valid(check_fire_into_action(unit, unit, tx, ty, 0, -1))) {
prep_fire_into_action(unit, unit, tx, ty, 0, -1);
return TASK_PREPPED_ACTION;
} else if (mobile(u) && flip_coin()) {
/* We're too far away to shoot directly, add a move-to task. */
push_move_near_task(unit, tx, ty, max(1 /* attack range */, u_range(u)));
return TASK_IS_INCOMPLETE;
}
return TASK_FAILED;
}
static TaskOutcome
do_hit_unit_task(unit, task)
Unit *unit;
Task *task;
{
int u = unit->type, tx, ty, dist, movedist, enemythere, uview, tu, ts;
Unit *unit2;
Side *us = unit->side;
/* This is to hit a (given type/side of) unit at a given place. */
/* (should add spec for number of hits to attempt?) */
tx = task->args[0]; ty = task->args[1];
tu = task->args[2]; ts = task->args[3];
dist = distance(tx, ty, unit->x, unit->y);
if (dist <= 1 /* direct attack range */) {
if (can_attack(unit)) {
for_all_stack(tx, ty, unit2) {
if (!trusted_side(us, unit2->side)
&& (tu == NONUTYPE || unit2->type == tu)
/* (should check designated side to hit, if defined) */
) {
if (valid(check_attack_action(unit, unit, unit2, 100))
&& attack_can_damage_or_capture(unit, unit2)) {
prep_attack_action(unit, unit, unit2, 100);
return TASK_PREPPED_ACTION;
}
}
}
}
/* Might be able to fire at pointblank range. */
if (can_fire(unit) && dist >= u_range_min(u)) {
for_all_stack(tx, ty, unit2) {
if (!trusted_side(us, unit2->side)
&& (tu == NONUTYPE || unit2->type == tu)
/* (should check designated side to hit, if defined) */
) {
if (valid(check_fire_at_action(unit, unit, unit2, -1))
&& fire_can_damage(unit, unit2)) {
prep_fire_at_action(unit, unit, unit2, -1);
return TASK_PREPPED_ACTION;
}
}
}
}
return TASK_FAILED;
}
if (dist < u_range_min(u)) {
/* should move further away */
return TASK_FAILED;
}
if (dist < u_range(u)) {
if (units_visible(us, tx, ty)) {
for_all_stack(tx, ty, unit2) {
if (!trusted_side(us, unit2->side)
&& (tu == NONUTYPE || unit2->type == tu)
/* (should check designated side to hit, if defined) */
) {
if (valid(check_fire_at_action(unit, unit, unit2, -1))
&& fire_can_damage(unit, unit2)) {
prep_fire_at_action(unit, unit, unit2, -1);
return TASK_PREPPED_ACTION;
}
}
}
}
return TASK_FAILED;
}
if (!target_visible(unit, task))
return TASK_FAILED;
/* If on mobile transport, let it handle things. */
if (unit->transport != NULL
&& mobile(unit->transport->type)
/* and the transport is not blocked */
&& flip_coin()) {
return TASK_IS_INCOMPLETE;
}
/* If out of range and can move, push a task to get closer (maybe). */
if (mobile(u) && flip_coin()) {
movedist = max(1 /* attack range */, u_range(u));
if (dist > movedist + u_acp(u) /* or dist that could be covered in 1-2 turns */) {
movedist = max(movedist, (dist - movedist) / 4);
}
push_move_near_task(unit, tx, ty, movedist);
return TASK_IS_INCOMPLETE;
}
return TASK_FAILED;
}
/* Return true if the unit can actually damage the other unit. */
int
attack_can_damage_or_capture(unit, unit2)
Unit *unit, *unit2;
{
if (!alive(unit) || !alive(unit2))
return FALSE;
if (capture_chance(unit->type, unit2->type, unit2->side) > 0)
return TRUE;
/* (should check for right kind of ammo) */
if (uu_hit(unit->type, unit2->type) <= 0)
return FALSE;
/* (this is dubious - a no-damage attack could still consume acp) */
if (uu_damage(unit->type, unit2->type) <= 0)
return FALSE;
/* (should check if victim hp might be under damage low bounds) */
return TRUE;
}
/* Return true if the unit can actually damage the other unit. */
int
fire_can_damage(unit, unit2)
Unit *unit, *unit2;
{
if (!alive(unit) || !alive(unit2))
return FALSE;
/* (should check for right kind of ammo) */
if (uu_hit(unit->type, unit2->type) <= 0)
return FALSE;
/* (this is dubious - a no-damage attack could still consume acp) */
if (uu_damage(unit->type, unit2->type) <= 0)
return FALSE;
/* (should check if victim hp might be under damage low bounds) */
return TRUE;
}
int
target_visible(unit, task)
Unit *unit;
Task *task;
{
int u = unit->type, tx, ty, uview, tu, ts;
Unit *unit2;
Side *us = unit->side;
tx = task->args[0]; ty = task->args[1];
tu = task->args[2]; ts = task->args[3];
if (units_visible(us, tx, ty)) {
for_all_stack(tx, ty, unit2) {
if (!trusted_side(us, unit2->side)
&& (tu == NONUTYPE || unit2->type == tu)
/* (should check designated side to hit, if defined) */
) {
return TRUE;
}
}
} else {
/* Assess old image or non-image. */
uview = unit_view(us, tx, ty);
if (uview != EMPTY
&& (tu == NONUTYPE || tu == vtype(uview))) {
return TRUE;
}
}
return FALSE;
}
/* Move in a straight line, go through things or stop rather than going around. */
static TaskOutcome
do_move_dir_task(unit, task)
Unit *unit;
Task *task;
{
int dir, tx, ty;
Unit *unit2;
if ((task->args[1])-- > 0) {
dir = task->args[0];
if (!point_in_dir(unit->x, unit->y, dir, &tx, &ty)) {
return TASK_FAILED;
}
if (unit_at(tx, ty)) {
for_all_stack(tx, ty, unit2) {
if (unit_trusts_unit(unit, unit2) /* but a spy could enter untrusted unit... */
&& valid(check_enter_action(unit, unit, unit2))) {
prep_enter_action(unit, unit, unit2);
return TASK_PREPPED_ACTION;
}
}
return TASK_FAILED;
} else if (valid(check_move_action(unit, unit, tx, ty, 0))) {
prep_move_action(unit, unit, tx, ty, 0);
return TASK_PREPPED_ACTION;
} else {
return TASK_FAILED;
}
} else {
return TASK_IS_COMPLETE;
}
}
enum choicestate {
eitherway,
leftthenright,
rightthenleft,
leftonly,
rightonly
};
static TaskOutcome do_approach_subtask PARAMS ((Unit *unit, Task *task, int tx, int ty, short *statep));
/* The move-to task is the main way for units to get from point A to point B.
In addition to the destination, the task has a required distance, so it will
succeed if the unit is within that distance to the nominal destination. */
static TaskOutcome
do_move_to_task(unit, task)
Unit *unit;
Task *task;
{
int dist, tx, ty, check;
Unit *unit2, *occ;
/* This task is to get to a designated location somehow. */
tx = task->args[0]; ty = task->args[1];
dist = distance(tx, ty, unit->x, unit->y);
if (dist <= task->args[3]) {
return TASK_IS_COMPLETE;
}
switch (dist) {
case 0:
/* We're there already, nothing more to do. */
return TASK_IS_COMPLETE;
case 1:
/* Adjacent cell, do a single move. */
if (unit_at(tx, ty)) {
for_all_stack(tx, ty, unit2) {
if (can_occupy(unit, unit2)) {
if (valid(check_enter_action(unit, unit, unit2))) {
prep_enter_action(unit, unit, unit2);
return TASK_PREPPED_ACTION;
} else {
continue;
}
} else if (!trusted_side(unit->side, unit2->side)) {
/* This is probably not a good idea, combat odds not
taken into account. */
if (valid(check_attack_action(unit, unit, unit2, 100))) {
prep_attack_action(unit, unit, unit2, 100);
return TASK_PREPPED_ACTION;
} else {
continue;
}
} else {
/* We could find room in an occupant... */
for_all_occupants(unit2, occ) {
if (can_occupy(unit, occ)) {
if (valid(check_enter_action(unit, unit, occ))) {
prep_enter_action(unit, unit, occ);
return TASK_PREPPED_ACTION;
}
}
}
}
}
return TASK_FAILED;
}
if (valid(check = check_move_action(unit, unit, tx, ty, unit->z))) {
/* Moving into an empty cell. */
prep_move_action(unit, unit, tx, ty, unit->z);
return TASK_PREPPED_ACTION;
} else {
/* If we're just short on mp, wait until the next turn to try to move. */
if (check == A_MOVE_NO_MP) {
if (unit->side)
notify(unit->side, "%s has insufficient MP to move this turn; waiting until next",
unit_handle(unit->side, unit));
set_unit_reserve(unit->side, unit, TRUE, FALSE);
return TASK_IS_INCOMPLETE;
}
return TASK_FAILED;
}
break;
default:
if (dist <= u_move_range(unit->type)
&& valid(check_move_action(unit, unit, tx, ty, unit->z))) {
prep_move_action(unit, unit, tx, ty, unit->z);
return TASK_PREPPED_ACTION;
} else {
/* Still some distance away, pick a way to go. */
return do_approach_subtask(unit, task, tx, ty, &(task->args[4]));
}
}
return TASK_FAILED;
}
static TaskOutcome
do_approach_subtask(unit, task, tx, ty, statep)
Unit *unit;
Task *task;
int tx, ty;
short *statep;
{
int nx, ny, dirs[NUMDIRS], numdirs, i, numdirs2, check;
Unit *unit2;
/* If on mobile transport, let it handle things. */
if (unit->transport != NULL
&& mobile(unit->transport->type)
/* and the transport is not blocked */
&& flip_coin()) {
set_unit_reserve(unit->side, unit, TRUE, FALSE);
return TASK_IS_INCOMPLETE;
}
numdirs = choose_move_dirs(unit, tx, ty, TRUE,
plausible_move_dir, sort_directions, dirs);
for (i = 0; i < numdirs; ++i) {
point_in_dir(unit->x, unit->y, dirs[i], &nx, &ny);
for_all_stack(nx, ny, unit2) {
if (can_occupy(unit, unit2)) {
if (valid(check_enter_action(unit, unit, unit2))) {
prep_enter_action(unit, unit, unit2);
/* We (probably) made forward progress, so reopen choice of dirs. */
*statep = eitherway;
return TASK_PREPPED_ACTION;
} else {
continue;
}
} else if (!trusted_side(unit->side, unit2->side)) {
if (unit->occupant) {
/* More important to find a way through. */
continue;
} else {
/* This will encourage some re-evaluation. */
return TASK_FAILED;
}
#if 0 /* the following is rarely a good idea */
if (valid(check_attack_action(unit, unit, unit2, 100))) {
prep_attack_action(unit, unit, unit2, 100);
/* We (probably) made forward progress, so reopen choice of dirs. */
*statep = eitherway;
return TASK_PREPPED_ACTION;
} else {
continue;
}
#endif
}
}
if (valid(check_move_action(unit, unit, nx, ny, unit->z))) {
prep_move_action(unit, unit, nx, ny, unit->z);
/* We (probably) made forward progress, so reopen choice of dirs. */
*statep = eitherway;
return TASK_PREPPED_ACTION;
}
}
/* Get both right and left non-decreasing dirs. */
numdirs = choose_move_dirs(unit, tx, ty, TRUE, NULL, NULL, dirs);
numdirs2 = choose_move_dirs(unit, tx, ty, FALSE, NULL, NULL, dirs);
for (i = numdirs; i < numdirs2; ++i) {
if (plausible_move_dir(unit, dirs[i])) {
switch (*statep) {
case eitherway:
if (i == numdirs)
*statep = leftonly /* leftthenright */;
if (i == numdirs+1)
*statep = rightonly /* rightthenleft */;
break;
#if 0
case leftthenright:
if (i == numdirs)
*statep = rightonly;
if (i == numdirs+1)
*statep = rightonly;
continue;
break;
case rightthenleft:
if (i == numdirs+1)
*statep = leftonly;
continue;
break;
#endif
case leftonly:
if (i == numdirs+1)
continue;
break;
case rightonly:
if (i == numdirs)
continue;
break;
default:
run_warning("Weird right/left state %d", *statep);
*statep = leftonly;
break;
}
} else {
switch (*statep) {
case eitherway:
if (i == numdirs)
*statep = rightonly;
if (i == numdirs+1)
*statep = leftonly;
continue;
break;
#if 0
case leftthenright:
if (i == numdirs)
*statep = rightonly;
if (i == numdirs+1)
*statep = rightonly;
continue;
break;
case rightthenleft:
if (i == numdirs+1)
*statep = leftonly;
continue;
break;
#endif
case leftonly:
if (i == numdirs)
return TASK_FAILED;
if (i == numdirs+1)
continue;
break;
case rightonly:
if (i == numdirs)
continue;
if (i == numdirs+1)
return TASK_FAILED;
break;
default:
run_warning("Weird right/left state %d", *statep);
*statep = leftonly;
break;
}
}
point_in_dir(unit->x, unit->y, dirs[i], &nx, &ny);
for_all_stack(nx, ny, unit2) {
if (can_occupy(unit, unit2)) {
if (valid(check_enter_action(unit, unit, unit2))) {
prep_enter_action(unit, unit, unit2);
return TASK_PREPPED_ACTION;
} else {
continue;
}
} else if (!trusted_side(unit->side, unit2->side)) {
if (unit->occupant) {
/* More important to find a way through. */
continue;
} else {
/* This will encourage some re-evaluation. */
return TASK_FAILED;
}
#if 0 /* the following is rarely a good idea */
if (valid(check_attack_action(unit, unit, unit2, 100))) {
prep_attack_action(unit, unit, unit2, 100);
return TASK_PREPPED_ACTION;
} else {
continue;
}
#endif
}
}
if (valid(check = check_move_action(unit, unit, nx, ny, unit->z))) {
prep_move_action(unit, unit, nx, ny, unit->z);
return TASK_PREPPED_ACTION;
}
/* If we're just short on mp, wait until the next turn to try to move. */
if (check == A_MOVE_NO_MP) {
if (unit->side)
notify(unit->side, "%s has insufficient MP to move this turn; waiting until next",
unit_handle(unit->side, unit));
set_unit_reserve(unit->side, unit, TRUE, FALSE);
return TASK_IS_INCOMPLETE;
}
}
return TASK_FAILED;
}
static TaskOutcome
do_occupy_task(unit, task)
Unit *unit;
Task *task;
{
int dist;
Unit *transport = find_unit(task->args[0]);
if (!in_play(transport))
return TASK_FAILED;
/* (should also fail if we don't know where transport is anymore) */
if (unit->transport == transport) {
return TASK_IS_COMPLETE;
}
dist = distance(unit->x, unit->y, transport->x, transport->y);
if (dist <= 1) {
if (valid(check_enter_action(unit, unit, transport))) {
prep_enter_action(unit, unit, transport);
return TASK_PREPPED_ACTION;
} else {
/* Try a couple times, then fail if not working. */
return (task->execnum < 3 ? TASK_IS_INCOMPLETE : TASK_FAILED);
}
} else {
/* Still some distance away, pick a way to go. */
return do_approach_subtask(unit, task, transport->x, transport->y, &(task->args[1]));
}
}
/* Wait around for a particular unit. Give up if the unit is not
forthcoming. */
static TaskOutcome
do_pickup_task(unit, task)
Unit *unit;
Task *task;
{
Unit *occupant = find_unit(task->args[0]);
if (!in_play(occupant))
return TASK_FAILED;
wake_unit(occupant->side, occupant, FALSE);
if (occupant->transport == unit) {
return TASK_IS_COMPLETE;
} else if (task->execnum > 10) {
/* Waiting around isn't working for us, give up. If the
prospective occupant still needs us, we'll get another
call. */
return TASK_FAILED;
} else {
if (valid(check_enter_action(occupant, occupant, unit))) {
prep_enter_action(occupant, occupant, unit);
return TASK_PREPPED_ACTION;
} else if (valid(check_enter_action(unit, occupant, unit))) {
prep_enter_action(unit, occupant, unit);
return TASK_PREPPED_ACTION;
} else {
return (task->execnum < 5 ? TASK_IS_INCOMPLETE : TASK_FAILED);
}
}
}
static TaskOutcome
do_produce_task(unit, task)
Unit *unit;
Task *task;
{
int m, tot, sofar, amt;
m = task->args[0];
if (!is_material_type(m)) {
/* This may be an indication of code bogosity; warn? */
return TASK_FAILED;
}
tot = task->args[1];
sofar = task->args[2];
if (sofar >= tot)
return TASK_IS_COMPLETE;
amt = um_material_per_production(unit->type, m);
if (valid(check_produce_action(unit, unit, m, amt))) {
task->args[1] += amt;
prep_produce_action(unit, unit, m, amt);
return TASK_PREPPED_ACTION;
}
return TASK_FAILED;
}
static TaskOutcome
do_repair_task(unit, task)
Unit *unit;
Task *task;
{
int x, y, u = unit->type, m, range = area.maxdim;
int ux = unit->x, uy = unit->y;
Unit *unit2;
for_all_material_types(m) {
if (um_consumption_per_move(u, m) > 0) {
range = min(range, unit->supply[m] / um_consumption_per_move(u, m));
}
}
tmpunit = unit;
/* (should use doctrine to decide when repairs sufficient) */
if (unit->hp >= (u_hp(u) * 80) / 100) { /* what if unit is multi-part? */
return TASK_IS_COMPLETE;
} else if (u_hp_recovery(u) > 0) {
if (0 /* too close to enemies */) {
/* (should move away from danger) */
} else {
/* Just hang out and wait to get better. */
set_unit_reserve(unit->side, unit, TRUE, FALSE);
}
return TASK_IS_INCOMPLETE;
} else if (unit->transport != NULL /* and transport repairs */) {
set_unit_reserve(unit->side, unit, TRUE, FALSE);
return TASK_IS_INCOMPLETE;
} else if ((unit2 = repair_here(ux, uy)) != NULL
&& unit2 != unit->transport) {
prep_enter_action(unit, unit, unit2);
return TASK_PREPPED_ACTION;
} else if (search_around(ux, uy, range, repair_test, &x, &y, 1)) {
/* (should collect actual unit and chase it directly) */
push_move_to_task(unit, x, y);
return TASK_IS_INCOMPLETE;
} else {
/* (should be able to signal interface usefully somehow) */
return TASK_FAILED;
}
}
Unit *
repair_here(x, y)
int x, y;
{
Unit *unit;
for_all_stack(x, y, unit) {
if (unit != tmpunit
&& unit_trusts_unit(tmpunit, unit)
&& (uu_auto_repair(unit->type, tmpunit->type) > 0
|| 0 /* (should allow for explicit repair actions) */)) {
return unit;
}
/* should look at occupants in stack too */
}
return NULL;
}
static int
repair_test(x, y)
int x, y;
{
return (repair_here(x, y) != NULL);
}
static int *lowm = NULL, numlow;
Unit *
aux_resupply_here(unit)
Unit *unit;
{
int i, enough = TRUE;
Unit *occ;
/* what about allies? */
if (unit_trusts_unit(unit, tmpunit)
&& can_carry(unit, tmpunit)) {
for (i = 0; i < numlow; ++i) {
if (unit->supply[lowm[i]] == 0)
enough = FALSE;
}
/* this should be controlled by doctrine? */
/* shouldn't wake up, should get a new task to "wait up"
or even approach if possible */
/* wake_unit(unit->side, unit, FALSE); */
if (enough)
return unit;
}
for_all_occupants(unit, occ) {
if (aux_resupply_here(occ)) {
return occ;
}
}
return NULL;
}
Unit *
resupply_here(x, y)
int x, y;
{
Unit *unit, *resupplier;
for_all_stack(x, y, unit) {
resupplier = aux_resupply_here(unit);
if (resupplier)
return resupplier;
}
return NULL;
}
static int
resupply_test(x, y)
int x, y;
{
return (resupply_here(x, y) != NULL);
}
/* Replenish our supplies, using one of several strategies, which as usual
depends on the game, unit, terrain, etc. Strategies include 1) wait for
supply line or own production to replenish, 2) move to productive terrain
and then wait, 3) move within range of a supplier, and 4) request a supplier
to approach. */
/* (should see if production actions would resupply, prep those actions) */
static TaskOutcome
do_resupply_task(unit, task)
Unit *unit;
Task *task;
{
int x, y, u = unit->type, m, range;
int ux = unit->x, uy = unit->y;
Unit *unit2;
/* A unit acting randomly might have gotten this task, even if the game
has no materials or the unit no supply. Pretend that it was OK and
get out of here. */
if (nummtypes == 0 || unit->supply == NULL)
return TASK_IS_COMPLETE;
tmpside = unit->side;
tmpunit = unit;
if (lowm == NULL)
lowm = (int *) xmalloc(nummtypes * sizeof(int));
numlow = 0;
if (task->args[0] == NONMTYPE) {
for_all_material_types(m) {
if (unit->supply[m] < um_storage_x(u, m)) {
lowm[numlow++] = m;
}
}
} else {
m = task->args[0];
if (unit->supply[m] < um_storage_x(u, m)) {
lowm[numlow++] = m;
}
}
/* We're all full up, must be OK. */
if (numlow == 0) {
return TASK_IS_COMPLETE;
} else if (can_auto_resupply_self(unit, lowm, numlow)) {
set_unit_reserve(unit->side, unit, TRUE, FALSE);
return (probability(10) ? TASK_FAILED : TASK_IS_INCOMPLETE);
} else if (unit->transport != NULL) {
/* (should fix this test, transport is not necessarily of any help) */
/* (could attempt to resupply via direct action) */
set_unit_reserve(unit->side, unit, TRUE, FALSE);
return (probability(10) ? TASK_FAILED : TASK_IS_INCOMPLETE);
} else if ((unit2 = resupply_here(ux, uy)) != NULL
&& unit2 != unit->transport) {
prep_enter_action(unit, unit, unit2);
return TASK_PREPPED_ACTION;
} else {
/* Compute how far out to look for a resupply point; be a little
optimistic. */
range = operating_range_best(u);
/* (should reduce range if materials are really low) */
if (search_around(ux, uy, range, resupply_test, &x, &y, 1)) {
/* (should collect actual unit and chase it directly) */
/* (should only need to get within outlength of needed supplies) */
push_move_to_task(unit, x, y);
return TASK_IS_INCOMPLETE;
} else {
/* Failure - sometimes just sit, but usually try something else. */
if (probability(10))
set_unit_reserve(unit->side, unit, TRUE, FALSE);
/* (should be able to signal interface usefully somehow) */
return TASK_FAILED;
}
}
}
/* Return true if our own automatic material production is *greater*
than our consumption, for the given list of materials. */
int
can_auto_resupply_self(unit, materials, numtypes)
Unit *unit;
int *materials, numtypes;
{
int u = unit->type, i, m, rslt = TRUE, t = terrain_at(unit->x, unit->y);
for (i = 0; i < numtypes; ++i) {
m = materials[i];
if (um_base_production(u, m) * ut_productivity(u, t) <= um_base_consumption(u, m))
rslt = FALSE;
}
return rslt;
}
static TaskOutcome
do_sentry_task(unit, task)
Unit *unit;
Task *task;
{
if (task->args[0] > 0) {
set_unit_reserve(unit->side, unit, TRUE, FALSE);
--(task->args[0]);
return TASK_IS_INCOMPLETE;
} else {
/* Unit won't necessarily wake up, may just replan and
continue sleeping. */
return TASK_IS_COMPLETE;
}
}
/* This is the main routine for a unit to execute a task. It basically
consists of a dispatch to the execution code for each task type, and
handling for a task's several possible outcomes. Note that a task
does *not* directly invoke any actions; instead it will schedule
("prep") an action, which will be executed later by execute_action.
Therefore, it is possible for a task to succeed but the action to
fail, although each task type's code tries to reduce the chances
of this happening (not possible to prevent entirely - unit may
become damaged and unable to do perform an action after the task
had decided on that action). */
TaskOutcome
execute_task(unit)
Unit *unit;
{
Plan *plan = unit->plan;
TaskOutcome rslt;
Task *task;
extern int taskexecs;
/* This should never happen. */
if (unit->plan == NULL)
run_error("???");
if (need_ai_task_reaction && unit->plan->last_task_outcome != TASK_UNKNOWN)
return unit->plan->last_task_outcome;
task = plan->tasks;
++taskexecs;
rslt = execute_task_aux(unit, task);
DMprintf("%s did task %s, ", unit_desig(unit), task_desig(task));
/* Record it. */
memcpy(&(unit->plan->last_task), task, sizeof(Task));
unit->plan->last_task_outcome = rslt;
if (unit->side != NULL && side_has_ai(unit->side)) {
need_ai_task_reaction = TRUE;
}
/* Now look at what happened with task execution. */
switch (rslt) {
case TASK_UNKNOWN:
DMprintf("???unknown outcome???");
break;
case TASK_FAILED:
++task->retrynum;
DMprintf("failed try %d, ", task->retrynum);
if (probability(20) || task->retrynum > 5) {
pop_task(plan);
DMprintf("removed it");
/* We might be buzzing, so maybe go into reserve. */
if (probability(20)) {
plan->reserve = TRUE;
DMprintf(" and went into reserve");
}
} else {
DMprintf("will retry");
}
break;
case TASK_IS_INCOMPLETE:
/* Leave the task alone. */
DMprintf("incomplete");
break;
case TASK_PREPPED_ACTION:
/* Mention the action that was prepared to execute. */
DMprintf("prepped action %s", action_desig(&(unit->act->nextaction)));
break;
case TASK_IS_COMPLETE:
DMprintf("completed after %d executions", task->execnum);
pop_task(plan);
break;
default:
break;
}
DMprintf("\n");
return rslt;
}
/* Perform a single given task. */
/* (should make static) */
TaskOutcome
execute_task_aux(unit, task)
Unit *unit;
Task *task;
{
if (!alive(unit) || task == NULL)
return TASK_UNKNOWN;
DMprintf("%s doing task %s\n", unit_desig(unit), task_desig(task));
/* Count this execution. */
++task->execnum;
/* (should use function pointer table...) */
switch (task->type) {
case TASK_NONE:
/* This is a no-op, useful as a placeholder. Always "succeeds". */
return TASK_IS_COMPLETE;
case TASK_BUILD:
return do_build_task(unit, task);
case TASK_CAPTURE:
return do_capture_task(unit, task);
case TASK_DISBAND:
return do_disband_task(unit, task);
case TASK_DO_ACTION:
return do_action_task(unit, task);
case TASK_HIT_POSITION:
return do_hit_position_task(unit, task);
case TASK_HIT_UNIT:
return do_hit_unit_task(unit, task);
case TASK_MOVE_DIR:
return do_move_dir_task(unit, task);
case TASK_MOVE_TO:
return do_move_to_task(unit, task);
case TASK_OCCUPY:
return do_occupy_task(unit, task);
case TASK_PICKUP:
return do_pickup_task(unit, task);
case TASK_PRODUCE:
return do_produce_task(unit, task);
case TASK_REPAIR:
return do_repair_task(unit, task);
case TASK_RESEARCH:
return do_research_task(unit, task);
case TASK_RESUPPLY:
return do_resupply_task(unit, task);
case TASK_SENTRY:
return do_sentry_task(unit, task);
default:
/* This is bad, but not necessarily a reason to die instantly. */
run_warning("Unknown task type %d", task->type);
return TASK_FAILED;
}
}
/* This weird-looking routine computes next directions for moving to a
given spot. The number of directions ranges from 1 to 4, depending
on whether there is a straight-line path to the dest, and whether we are
required to take a direct path or are allowed to move in dirs that don't
the unit any closer (we never increase our distance though).
Some trickinesses: if area wraps, must resolve ambiguity about
getting to the same place going either direction (we pick shortest). */
int
choose_move_dirs(unit, tx, ty, shortest, dirtest, dirsort, dirs)
Unit *unit;
int tx, ty, shortest, *dirs;
int (*dirtest) PARAMS ((Unit *, int));
void (*dirsort) PARAMS ((Unit *, int *, int));
{
int dx, dxa, dy, dist, d1, d2, d3, d4, axis = -1, hextant = -1;
int numdirs = 0, shortestnumdirs;
dist = distance(unit->x, unit->y, tx, ty);
dx = tx - unit->x; dy = ty - unit->y;
if (area.xwrap) {
dxa = (tx + area.width) - unit->x;
if (ABS(dx) > ABS(dxa))
dx = dxa;
dxa = (tx - area.width) - unit->x;
if (ABS(dx) > ABS(dxa))
dx = dxa;
}
if (dx == 0 && dy == 0) {
return -1;
}
axis = hextant = -1;
if (dx == 0) {
axis = (dy > 0 ? NORTHEAST : SOUTHWEST);
} else if (dy == 0) {
axis = (dx > 0 ? EAST : WEST);
} else if (dx == (0 - dy)) {
axis = (dy > 0 ? NORTHWEST : SOUTHEAST);
} else if (dx > 0) {
hextant = (dy > 0 ? EAST :
(ABS(dx) > ABS(dy) ? SOUTHEAST : SOUTHWEST));
} else {
hextant = (dy < 0 ? WEST :
(ABS(dx) > ABS(dy) ? NORTHWEST : NORTHEAST));
}
if (axis >= 0) {
d1 = d2 = axis;
if (dirtest == NULL || (*dirtest)(unit, d1)) {
dirs[numdirs++] = d1;
}
}
if (hextant >= 0) {
d1 = left_dir(hextant);
d2 = hextant;
if (dirtest == NULL || (*dirtest)(unit, d1)) {
dirs[numdirs++] = d1;
}
if (dirtest == NULL || (*dirtest)(unit, d2)) {
dirs[numdirs++] = d2;
}
}
/* Check on other properties of the two choices. */
if (numdirs > 1 && dirsort != NULL) {
(*dirsort)(unit, dirs, numdirs);
}
if (dist > 1 && !shortest) {
shortestnumdirs = numdirs;
d3 = left_dir(d1);
d4 = right_dir(d2);
if (dirtest == NULL || (*dirtest)(unit, d3)) {
dirs[numdirs++] = d3;
}
if (dirtest == NULL || (*dirtest)(unit, d4)) {
dirs[numdirs++] = d4;
}
if (numdirs > shortestnumdirs + 1 && dirsort != NULL) {
(*dirsort)(unit, dirs + shortestnumdirs, numdirs - shortestnumdirs);
}
}
return numdirs;
}
/* A heuristic test for whether the given direction is a good one
to move in. */
int
plausible_move_dir(unit, dir)
Unit *unit;
int dir;
{
int u = unit->type, ux = unit->x, uy = unit->y, nx, ny, t, c;
point_in_dir(ux, uy, dir, &nx, &ny);
if (unit_at(nx, ny))
return TRUE;
t = terrain_at(nx, ny);
if ((ut_vanishes_on(u, t)
|| ut_wrecks_on(u, t))
&& !can_move_via_conn(unit, nx, ny))
return FALSE;
if (ut_mp_to_enter(u, t) <= u_acp(u))
return TRUE;
if (numconntypes > 0) {
/* Try each connection type to see if it works. */
for_all_terrain_types(c) {
if (t_is_connection(c)
&& aux_terrain_defined(c)
&& connection_at(ux, uy, dir, c)) {
if ((ut_mp_to_enter(u, c)
+ ut_mp_to_traverse(u, c)
+ ut_mp_to_leave(u, c)) <= u_acp(u))
return TRUE;
}
}
}
return FALSE;
}
/* This compares the desirability of two different directions. This is
somewhat tricky, because it should return < 0 if i0 designates a BETTER
direction than i1. */
int xs[NUMDIRS];
int ys[NUMDIRS];
int terrs[NUMDIRS];
static int
compare_directions(a0, a1)
CONST void *a0, *a1;
{
int i0, i1;
int u = tmputype, t0, t1;
int ux = tmpunit->x, uy = tmpunit->y, u2 = NONUTYPE;
int cost0 = 0, cost1 = 0, s, ps0, ps1, surr0, surr1, rslt;
extern int *any_people_surrenders;
i0 = *((int *) a0); i1 = *((int *) a1);
t0 = terrs[i0]; t1 = terrs[i1];
if (tmpunit->transport)
u2 = tmpunit->transport->type;
/* Check the overall movement cost of each direction. */
cost0 = total_move_cost(u, u2, ux, uy, 0, xs[i0], ys[i0], 0);
cost1 = total_move_cost(u, u2, ux, uy, 0, xs[i1], ys[i1], 0);
if (cost0 != cost1) {
return cost0 - cost1;
}
if (1 /* not in supply */) {
if ((rslt = ut_productivity(u, t1) - ut_productivity(u, t0)) != 0) {
return rslt;
}
}
if ((rslt = ut_mp_to_leave(u, t1) - ut_mp_to_leave(u, t0)) != 0) {
return rslt;
}
/* Chooser the safer terrain. */
if ((rslt = ut_accident_hit(u, t1) - ut_accident_hit(u, t0)) != 0) {
return rslt;
}
/* Choose the better-concealing terrain. */
/* (should only do if limited visibility) */
if ((rslt = ut_visibility(u, t1) - ut_visibility(u, t0)) != 0) {
return rslt;
}
/* Prefer to go over cells that we can change to our side. */
/* (should also do for control-side?) */
if (any_people_surrenders != NULL
&& any_people_surrenders[u]
&& people_sides_defined()) {
s = side_number(tmpunit->side);
ps0 = people_side_at(xs[i0], ys[i0]);
ps1 = people_side_at(xs[i1], ys[i1]);
surr0 = ut_people_surrender(u, t0)
* ((ps0 != NOBODY && s != ps0) ? 1 : 0);
surr1 = ut_people_surrender(u, t1)
* ((ps1 != NOBODY && s != ps1) ? 1 : 0);
if (surr0 != surr1) {
return surr1 - surr0;
}
}
return 0;
}
void
sort_directions(unit, dirs, numdirs)
Unit *unit;
int *dirs, numdirs;
{
int i, tmp, i0 = 0, i1 = 1, compar;
for (i = 0; i < numdirs; ++i) {
point_in_dir(unit->x, unit->y, dirs[i], &(xs[i]), &(ys[i]));
terrs[i] = terrain_at(xs[i], ys[i]);
}
tmpunit = unit;
tmputype = unit->type;
if (numdirs == 2) {
compar = compare_directions(&i0, &i1);
if (compar > 0 || (compar == 0 && flip_coin())) {
tmp = dirs[0]; dirs[0] = dirs[1]; dirs[1] = tmp;
}
} else if (numdirs > 2) {
qsort(dirs, numdirs, sizeof(int), compare_directions);
if (compare_directions(&i0, &i1) == 0 && flip_coin()) {
tmp = dirs[0]; dirs[0] = dirs[1]; dirs[1] = tmp;
}
}
}
/* Put the given task back onto the list of free tasks. */
void
free_task(task)
Task *task;
{
task->next = freetasks;
freetasks = task;
}
void
add_task(unit, pos, task)
Unit *unit;
int pos;
Task *task;
{
int numcleared;
if (unit->plan == NULL && !completed(unit)) {
init_unit_plan(unit);
}
if (!in_play(unit) || unit->plan == NULL) {
run_warning("Trying to do %s task with bad %s",
task_desig(task), unit_desig(unit));
return;
}
DMprintf("%s add task %s",
unit_desig(unit), task_desig(task));
if (pos == CLEAR_AGENDA) {
numcleared = clear_task_agenda(unit->plan);
DMprintf(" (cleared %d existing tasks)", numcleared);
}
DMprintf("\n");
switch (pos) {
case 0:
case CLEAR_AGENDA:
/* Put the task on the front of the agenda. */
task->next = unit->plan->tasks;
unit->plan->tasks = task;
break;
default:
run_error("can't do this yet");
break;
}
/* Shouldn't be asleep any longer. */
unit->plan->asleep = FALSE;
/* We're not in reserve. */
unit->plan->reserve = FALSE;
/* Presumably we're no longer waiting to be told what to do. */
set_waiting_for_tasks(unit, FALSE);
/* Reflect all this on displays. */
update_unit_display(unit->side, unit, FALSE);
}
Task *
create_move_to_task(x, y)
int x, y;
{
Task *task = create_task(TASK_MOVE_TO);
task->args[0] = x; task->args[1] = y;
task->args[2] = 0;
task->args[3] = 0;
task->args[4] = eitherway;
return task;
}
/* Give the unit a task to move to a given place, erasing every other task. */
void
set_move_to_task(unit, x, y)
Unit *unit;
int x, y;
{
add_task(unit, CLEAR_AGENDA, create_move_to_task(x, y));
}
void
push_move_to_task(unit, x, y)
Unit *unit;
int x, y;
{
if (!in_area(x, y)) {
run_warning("Trying to move %s to %d,%d", unit_desig(unit), x, y);
return;
}
add_task(unit, 0, create_move_to_task(x, y));
}
Task *
create_move_near_task(x, y, dist)
int x, y, dist;
{
Task *task = create_task(TASK_MOVE_TO);
task->args[0] = x; task->args[1] = y;
task->args[2] = 0;
task->args[3] = dist;
task->args[4] = eitherway;
return task;
}
void
set_move_near_task(unit, x, y, dist)
Unit *unit;
int x, y, dist;
{
add_task(unit, CLEAR_AGENDA, create_move_near_task(x, y, dist));
}
void
push_move_near_task(unit, x, y, dist)
Unit *unit;
int x, y, dist;
{
add_task(unit, 0, create_move_near_task(x, y, dist));
}
/* Create a task to move in a given direction for a given distance. */
Task *
create_move_dir_task(dir, n)
int dir, n;
{
Task *task = create_task(TASK_MOVE_DIR);
task->args[0] = dir;
task->args[1] = n;
return task;
}
/* Fill in the given unit with direction-moving orders. */
void
set_move_dir_task(unit, dir, n)
Unit *unit;
int dir, n;
{
add_task(unit, CLEAR_AGENDA, create_move_dir_task(dir, n));
}
/* This routine sets up a task to build a unit of the given type. */
Task *
create_build_task(u2, run)
int u2, run;
{
Task *task = create_task(TASK_BUILD);
task->args[0] = u2;
task->args[3] = run;
return task;
}
void
set_build_task(unit, u2, run)
Unit *unit;
int u2, run;
{
add_task(unit, CLEAR_AGENDA, create_build_task(u2, run));
}
void
push_build_task(unit, u2, run)
Unit *unit;
int u2, run;
{
add_task(unit, 0, create_build_task(u2, run));
}
/* This routine sets up a task to research a unit of the given type. */
Task *
create_research_task(u2, n)
int u2, n;
{
Task *task = create_task(TASK_RESEARCH);
task->args[0] = u2;
task->args[1] = n;
return task;
}
void
push_research_task(unit, u2, n)
Unit *unit;
int u2, n;
{
add_task(unit, 0, create_research_task(u2, n));
}
Task *
create_hit_task(x, y)
int x, y;
{
Task *task = create_task(TASK_HIT_UNIT);
task->args[0] = x; task->args[1] = y;
task->args[2] = NONUTYPE;
task->args[3] = -1;
return task;
}
void
set_hit_task(unit, x, y)
Unit *unit;
int x, y;
{
add_task(unit, CLEAR_AGENDA, create_hit_task(x, y));
}
void
push_hit_task(unit, x, y)
Unit *unit;
int x, y;
{
add_task(unit, 0, create_hit_task(x, y));
}
Task *
create_specific_hit_task(x, y, u, s)
int x, y, u, s;
{
Task *task = create_task(TASK_HIT_UNIT);
task->args[0] = x; task->args[1] = y;
task->args[2] = u;
task->args[3] = s;
return task;
}
void
set_specific_hit_task(unit, x, y, u, s)
Unit *unit;
int x, y, u, s;
{
add_task(unit, CLEAR_AGENDA, create_specific_hit_task(x, y, u, s));
}
void
push_specific_hit_task(unit, x, y, u, s)
Unit *unit;
int x, y, u, s;
{
add_task(unit, 0, create_specific_hit_task(x, y, u, s));
}
Task *
create_capture_task(x, y)
int x, y;
{
Task *task = create_task(TASK_CAPTURE);
task->args[0] = x; task->args[1] = y;
task->args[2] = NONUTYPE;
task->args[3] = -1;
return task;
}
void
set_capture_task(unit, x, y)
Unit *unit;
int x, y;
{
add_task(unit, CLEAR_AGENDA, create_capture_task(x, y));
}
void
push_capture_task(unit, x, y)
Unit *unit;
int x, y;
{
add_task(unit, 0, create_capture_task(x, y));
}
void
set_disband_task(unit)
Unit *unit;
{
add_task(unit, CLEAR_AGENDA, create_task(TASK_DISBAND));
}
Task *
create_repair_task()
{
return create_task(TASK_REPAIR);
}
void
set_repair_task(unit)
Unit *unit;
{
add_task(unit, CLEAR_AGENDA, create_repair_task());
}
Task *
create_resupply_task(m)
int m;
{
Task *task = create_task(TASK_RESUPPLY);
task->args[0] = m;
return task;
}
void
set_resupply_task(unit, m)
Unit *unit;
int m;
{
add_task(unit, CLEAR_AGENDA, create_resupply_task(m));
}
Task *
create_occupy_task(transport)
Unit *transport;
{
Task *task = create_task(TASK_OCCUPY);
task->args[0] = transport->id;
task->args[1] = eitherway;
/* add a waiting period also? */
return task;
}
void
push_occupy_task(unit, transport)
Unit *unit, *transport;
{
add_task(unit, 0, create_occupy_task(transport));
}
Task *
create_pickup_task(occ)
Unit *occ;
{
Task *task = create_task(TASK_PICKUP);
task->args[0] = occ->id;
/* add a waiting period also? */
return task;
}
void
push_pickup_task(unit, occ)
Unit *unit, *occ;
{
add_task(unit, 0, create_pickup_task(occ));
}
Task *
create_produce_task(m, n)
int m, n;
{
Task *task = create_task(TASK_PRODUCE);
task->args[0] = m;
task->args[1] = n;
/* Third arg is amount produced, which starts at 0. */
return task;
}
void
push_produce_task(unit, m, n)
Unit *unit;
int m, n;
{
add_task(unit, 0, create_produce_task(m, n));
}
Task *
create_sentry_task(n)
int n;
{
Task *task = create_task(TASK_SENTRY);
task->args[0] = n;
return task;
}
void
set_sentry_task(unit, n)
Unit *unit;
int n;
{
add_task(unit, CLEAR_AGENDA, create_sentry_task(n));
}
void
push_sentry_task(unit, n)
Unit *unit;
int n;
{
add_task(unit, 0, create_sentry_task(n));
}
extern int parse_location PARAMS ((Side *side, char *arg, int *xp, int *yp));
extern Unit *parse_unit PARAMS ((Side *side, char *arg));
/* Find a unit with the given name, either alive or dead. */
Unit *
parse_unit(side, nm)
Side *side;
char *nm;
{
Unit *unit;
if (empty_string(nm))
return NULL;
for_all_side_units(side, unit) {
if (alive(unit) && unit->name != NULL && strcmp(unit->name, nm) == 0)
return unit;
}
/* Under some circumstances, we can refer to other sides' units by name. */
for_all_units(unit) {
if (alive(unit)
&& unit->side != side
&& unit->name != NULL
&& strcmp(unit->name, nm) == 0
&& (side->see_all
|| side_sees_image(side, unit)))
return unit;
}
return NULL;
}
/* Given a textual description of a location, compute an x,y for it
if possible. */
int
parse_location(side, arg, xp, yp)
Side *side;
char *arg;
int *xp, *yp;
{
char *arg2;
Unit *unit;
*xp = strtol(arg, &arg2, 10);
if (arg != arg2 && *arg2 == ',') {
*yp = strtol(arg2 + 1, &arg, 10);
if (arg2 + 1 != arg)
return TRUE;
} else if ((unit = parse_unit(side, arg)) != NULL) {
*xp = unit->x; *yp = unit->y;
return TRUE;
}
notify(side, "location \"%s\" not recognized", arg);
return FALSE;
}
/* Given a string describing a task that has been entered in
by a player, generate a task object and return the rest
of the string, if NULL if failure. */
char *
parse_task(side, str, taskp)
Side *side;
char *str;
Task **taskp;
{
int tasktype, i, x, y, n, dir, u, taskargs[MAXTASKARGS], numargs;
char *arg, *arg2, substr[BUFSIZE], *rest, *argtypes;
Unit *unit;
*taskp = NULL;
rest = get_next_arg(str, substr, &arg);
/* Recognize special cases of task types first. */
if (strcmp(arg, "nil") == 0 || strcmp(arg, "nothing") == 0) {
/* NULL task with non-NULL return indicates order cancellation. */
*taskp = NULL;
return rest;
} else if (strcmp(arg, "move-near") == 0) {
rest = get_next_arg(rest, substr, &arg);
if (parse_location(side, arg, &x, &y)) {
rest = get_next_arg(rest, substr, &arg);
n = strtol(arg, &arg2, 10);
*taskp = create_move_near_task(x, y, n);
return rest;
}
}
tasktype = lookup_task_type(arg);
if (tasktype < 0) {
notify(side, "task type \"%s\" not recognized", arg);
return NULL;
}
switch (tasktype) {
case TASK_MOVE_TO:
rest = get_next_arg(rest, substr, &arg);
if (parse_location(side, arg, &x, &y)) {
*taskp = create_move_to_task(x, y);
return rest;
} else {
return NULL;
}
break;
default:
argtypes = taskdefns[tasktype].argtypes;
numargs = strlen(argtypes);
for (i = 0; i < numargs; ++i)
taskargs[i] = 0;
rest = get_next_arg(rest, substr, &arg);
for (i = 0; i < numargs; ++i) {
if (argtypes[i] == 'x' && argtypes[i+1] == 'y') {
/* If there are two arguments that are together a position,
interpret both together. */
if (parse_location(side, arg, &x, &y)) {
taskargs[i] = x; taskargs[i + 1] = y;
++i;
} else {
return NULL;
}
} else if (argtypes[i] == 'd') {
char *mydirchars = "ulnbhy"; /* (a local copy of ui.c thing) */
/* Match on names or chars for directions. */
for_all_directions(dir) {
if (strcmp(arg, dirnames[dir]) == 0) {
taskargs[i] = dir;
goto nextarg;
}
if (strlen(arg) == 1 && arg[0] == mydirchars[dir]) {
taskargs[i] = dir;
goto nextarg;
}
}
notify(side, "direction \"%s\" not recognized", arg);
} else if (argtypes[i] == 'u') {
u = utype_from_name(arg);
if (u != NONUTYPE) {
taskargs[i] = u;
} else {
notify(side, "unit type \"%s\" not recognized", arg);
}
} else if (argtypes[i] == 'U') {
unit = parse_unit(side, arg);
if (unit != NULL) {
taskargs[i] = unit->id;
} else {
notify(side, "unit called \"%s\" not recognized", arg);
}
} else {
/* Just collect an integer and stuff it. */
taskargs[i] = strtol(arg, &arg2, 10);
if (arg == arg2) {
notify(side, "argument \"%s\" not recognized", arg);
}
}
nextarg:
rest = get_next_arg(str, substr, &arg);
/* (should check for end of command or not?) */
}
*taskp = create_task(tasktype);
for (i = 0; i < numargs; ++i) {
(*taskp)->args[i] = taskargs[i];
}
return rest;
}
}
int
lookup_task_type(name)
char *name;
{
int i;
for (i = 0; taskdefns[i].name != NULL; ++i)
if (strcmp(name, taskdefns[i].name) == 0)
return i; /* should get real enum? */
return -1;
}
/* Describe a task succinctly - use for debugging only. */
char *
task_desig(task)
Task *task;
{
int i, slen;
char *argtypes;
if (taskbuf == NULL)
taskbuf = xmalloc(BUFSIZE);
if (task) {
sprintf(taskbuf, "{%s", taskdefns[task->type].name);
argtypes = taskdefns[task->type].argtypes;
slen = strlen(argtypes);
for (i = 0; i < slen; ++i) {
tprintf(taskbuf, "%c%d", (i == 0 ? ' ' : ','), task->args[i]);
}
tprintf(taskbuf, " x %d", task->execnum);
if (task->retrynum > 0) {
tprintf(taskbuf, " fail %d", task->retrynum);
}
strcat(taskbuf, "}");
} else {
sprintf(taskbuf, "no task");
}
return taskbuf;
}
|