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
|
/* The combat-related actions of Xconq.
Copyright (C) 1987, 1988, 1989, 1991, 1992, 1993, 1994, 1995
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. */
/* Rules of combat: the attacker hits the defender ("other") unit and its
occupants, but the damage does not take effect right away. If counter
attacks are possible in this period, the defender always does so, with
the same odds. If the defender dies, then the attacker moves into the
cell. If the attacker dies, nothing happens. If both survive, then the
attacker may attempt to capture the defender. */
#include "conq.h"
extern int damaged_terrain_type PARAMS ((int t));
static int one_attack PARAMS ((Unit *atker, Unit *defender));
static void fire_on_unit PARAMS ((Unit *atker, Unit *other));
static void attack_unit PARAMS ((Unit *atker, Unit *other));
static void maybe_hit_unit PARAMS ((Unit *atker, Unit *other, int fire, int fallsoff));
static void hit_unit PARAMS ((Unit *unit, int hit, Unit *atker));
static void reckon_damage_here PARAMS ((int x, int y));
static void report_damage PARAMS ((Unit *unit, Unit *atker, Unit *mainunit));
static int retreat_unit PARAMS ((Unit *unit, Unit *atker));
static int retreat_in_dir PARAMS ((Unit *unit, int dir));
static void attempt_to_capture_unit PARAMS ((Unit *atker, Unit *other));
static void capture_unit_2 PARAMS ((Unit *unit, Unit *pris,
Side *prevside));
static void capture_occupant PARAMS ((Unit *unit, Unit *pris, Unit *occ, Side *newside));
static void detonate_on_cell PARAMS ((int x, int y));
static void notify_combat PARAMS ((Unit *unit, Unit *atker, char *str));
#undef DEF_ACTION
#define DEF_ACTION(name,code,args,prepfn,DOFN,checkfn,ARGDECL,doc) \
extern int DOFN PARAMS (ARGDECL);
#include "action.def"
int max_u_detonate_effect_range = -1;
int max_t_detonate_effect_range = -1;
int max_detonate_on_approach_range = -1;
/* Remember what the main units involved are, so display is handled relative
to them and not to any occupants. */
static Unit *amain, *omain;
int numsoundplays;
/* Attack action. */
/* This is an attack on a given unit at a given level of commitment. */
int
prep_attack_action(unit, unit2, defender, n)
Unit *unit, *unit2, *defender;
int n;
{
if (unit == NULL || unit->act == NULL)
return FALSE;
if (unit2 == NULL)
return FALSE;
unit->act->nextaction.type = ACTION_ATTACK;
unit->act->nextaction.args[0] = defender->id;
unit->act->nextaction.args[1] = n;
unit->act->nextaction.actee = unit2->id;
return TRUE;
}
int
do_attack_action(unit, unit2, defender, n)
Unit *unit, *unit2, *defender;
int n;
{
int u2 = unit2->type, u3 = defender->type;
int withdrawchance, surrenderchance;
action_point(unit2->side, defender->x, defender->y);
action_point(defender->side, defender->x, defender->y);
/* Defender might be a type that can sneak away to avoid attack. */
withdrawchance = uu_withdraw_per_attack(u2, u3);
if (withdrawchance > 0) {
if (probability(withdrawchance)) {
if (retreat_unit(defender, unit2)) {
if (alive(unit))
use_up_acp(unit, uu_acp_to_attack(u2, u3));
return A_ANY_DONE;
}
}
}
/* Defender might instead choose to surrender right off. */
surrenderchance = uu_surrender_per_attack(u2, u3);
if (surrenderchance > 0) {
if (probability(surrenderchance)) {
capture_unit(unit2, defender);
if (alive(unit))
use_up_acp(unit, uu_acp_to_attack(u2, u3));
return A_ANY_DONE;
}
}
/* Carry out a normal attack. */
one_attack(unit, defender);
if (alive(unit))
use_up_acp(unit, uu_acp_to_attack(u2, u3));
/* The defender in an attack has to take time to defend itself. */
if (alive(defender))
use_up_acp(defender, uu_acp_to_defend(u2, u3));
return A_ANY_DONE;
}
int
check_attack_action(unit, unit2, defender, n)
Unit *unit, *unit2, *defender;
int n;
{
int u, u2, u3, acp, u2x, u2y, dfx, dfy, dist, m;
if (!in_play(unit))
return A_ANY_ERROR;
if (!in_play(unit2))
return A_ANY_ERROR;
if (!in_play(defender))
return A_ANY_ERROR;
/* We can't attack ourselves. */
if (unit2 == defender)
return A_ANY_ERROR;
if (unit2->side != NULL && unit2->side == defender->side)
return A_ANY_ERROR;
u = unit->type;
u2 = unit2->type;
u3 = defender->type;
acp = uu_acp_to_attack(u2, u3);
if (acp < 1)
return A_ANY_CANNOT_DO;
if (!has_enough_acp(unit, acp))
return A_ANY_NO_ACP;
/* Check whether we can attack from inside a transport. */
if (unit2->transport && uu_occ_combat(u2, unit2->transport->type) == 0)
return A_ANY_ERROR;
u2x = unit2->x; u2y = unit2->y;
dfx = defender->x; dfy = defender->y;
dist = distance(u2x, u2y, dfx, dfy);
if (dist < uu_attack_range_min(u2, u3))
return A_ANY_TOO_NEAR;
if (dist > uu_attack_range(u2, u3))
return A_ANY_TOO_FAR;
if (uu_hit(u2, u3) <= 0)
return A_ANY_ERROR;
/* We have to have a minimum level of supply to be able to attack. */
for_all_material_types(m) {
if (unit2->supply[m] < um_to_fight(u2, m))
return A_ANY_NO_MATERIAL;
}
/* (should prorate ammo needs by intensity of attack) */
if (!enough_ammo(unit2, defender))
return A_ANY_ERROR;
/* Allow attacks even if zero damage, this amounts to "harassment". */
return A_ANY_OK;
}
/* Overrun action. */
/* Overrun is an attempt to occupy a given cell that may include attempts
to attack and/or capture any units in the way. */
int
prep_overrun_action(unit, unit2, x, y, z, n)
Unit *unit, *unit2;
int x, y, z, n;
{
if (unit == NULL || unit->act == NULL)
return FALSE;
if (unit2 == NULL)
return FALSE;
unit->act->nextaction.type = ACTION_OVERRUN;
unit->act->nextaction.args[0] = x;
unit->act->nextaction.args[1] = y;
unit->act->nextaction.args[2] = z;
unit->act->nextaction.args[3] = n;
unit->act->nextaction.actee = unit2->id;
return TRUE;
}
int
do_overrun_action(unit, unit2, x, y, z, n)
Unit *unit, *unit2;
int x, y, z, n;
{
int u, u2, u3, acpused, mpcost, acpcost, speed, ox, oy, oz;
Unit *defender;
u = unit->type; u2 = unit2->type;
ox = unit2->x; oy = unit2->y; oz = unit2->z;
action_point(unit2->side, x, y);
acpused = 0;
/* Attack every defender in turn. */
for_all_stack(x, y, defender) {
u3 = defender->type;
/* Don't attack any of our buddies. */
if (unit_trusts_unit(unit2, defender))
continue;
action_point(defender->side, x, y);
one_attack(unit2, defender);
if (alive(unit))
use_up_acp(unit, uu_acp_to_attack(u2, u3));
acpused += uu_acp_to_attack(u2, u3);
/* The target of an attack has to take time to defend itself. */
if (alive(defender)) {
use_up_acp(defender, uu_acp_to_defend(u, u3));
}
}
if (!alive(unit2))
return A_OVERRUN_FAILED;
if (in_blocking_zoc(unit2, x, y, z))
return A_OVERRUN_FAILED;
/* Try to enter the cleared cell now - might still have
friendlies filling it up already, so check first. */
if (can_occupy_cell(unit2, x, y)) {
mpcost = move_unit(unit2, x, y);
/* Note that we'll say the action succeeded even if
the cell did not have enough room for us to actually
be in it, which is a little weird. */
/* Now add up any extra movement costs of entering the new cell. */
mpcost += zoc_move_cost(unit2, ox, oy, oz);
acpcost = 0;
speed = unit_speed(unit2, x, y);
if (speed > 0)
acpcost = (mpcost * 100) / speed;
acpcost -= acpused;
if (acpcost > 0) {
/* Take the movement cost out of the moving unit if possible. */
use_up_acp(unit2, acpcost);
}
/* Count the unit as having actually moved. */
if (unit2->act)
++(unit2->act->actualmoves);
}
return A_OVERRUN_SUCCEEDED;
}
int
check_overrun_action(unit, unit2, x, y, z, n)
Unit *unit, *unit2;
int x, y, z, n;
{
int u, u2, u2x, u2y, u2z, u3, totcost, speed, mpavail, m;
Unit *defender;
if (!in_play(unit))
return A_ANY_ERROR;
if (!in_play(unit2))
return A_ANY_ERROR;
if (!inside_area(x, y))
return A_ANY_ERROR;
if (n == 0)
return A_ANY_ERROR;
u = unit->type;
u2 = unit2->type;
/* (should think about this some more - overrunning into a cell with no
resistance should work perhaps, even for noncombat units) */
if (!type_can_attack(u))
return A_ANY_CANNOT_DO;
if (!has_enough_acp(unit, 1))
return A_ANY_NO_ACP;
/* Check whether we can attack from inside a transport. */
/* (although this might be legit if unit is assumed to leave transport first) */
if (unit2->transport && uu_occ_combat(u2, unit2->transport->type) == 0)
return A_ANY_ERROR;
u2x = unit2->x; u2y = unit2->y; u2z = unit2->z;
/* We have to be in the same cell or an adjacent one. */
if (!between(0, distance(u2x, u2y, x, y), 1))
return A_ANY_TOO_FAR;
/* Now start looking at the move costs. */
u3 = (unit2->transport ? unit2->transport->type : NONUTYPE);
totcost = total_move_cost(u2, u3, u2x, u2y, u2z, x, y, u2z);
speed = unit_speed(unit2, x, y);
mpavail = (unit->act->acp * speed) / 100;
/* Zero mp always disallows movement, unless intra-cell. */
if (mpavail <= 0 && !(u2x == x && u2y == y && u2z == u2z))
return A_MOVE_NO_MP;
/* The free mp might get us enough moves, so add it before comparing. */
if (mpavail + u_free_mp(u2) < totcost)
return A_MOVE_NO_MP;
/* We have to have a minimum level of supply to be able to attack. */
for_all_material_types(m) {
if (unit2->supply[m] < um_to_fight(u2, m))
return A_ANY_NO_MATERIAL;
}
for_all_stack(x, y, defender) {
/* (should test if units here can be attacked en masse) */
/* (should prorate ammo needs by intensity of overrun) */
if (!enough_ammo(unit2, defender))
return A_ANY_NO_MATERIAL;
}
return A_ANY_OK;
}
/* Return true if the attacker defeated the defender, and can therefore
try to move into the defender's old position. */
static int
one_attack(atker, defender)
Unit *atker, *defender;
{
int ax = atker->x, ay = atker->y, ox = defender->x, oy = defender->y;
int counter;
Side *as = atker->side, *os = defender->side;
amain = atker; omain = defender;
attack_unit(atker, defender);
/* Do a counterattack if appropriate. */
counter = uu_counterattack(atker->type, defender->type);
if (counter > 0) {
/* (should use value to set strength/commitment of counterattack) */
attack_unit(defender, atker);
}
reckon_damage();
see_exact(as, ax, ay);
see_exact(as, ox, oy);
see_exact(os, ax, ay);
see_exact(os, ox, oy);
update_cell_display(as, ax, ay, TRUE);
update_cell_display(as, ox, oy, TRUE);
update_cell_display(os, ax, ay, TRUE);
update_cell_display(os, ox, oy, TRUE);
all_see_cell(ax, ay);
all_see_cell(ox, oy);
attempt_to_capture_unit(atker, defender);
/* If the defender was not captured, it might turn the tables! */
/* (Note that we cannot cache the attacker's and defender's types,
because the type might have changed due to damage, and we want
to know if the new type might countercapture.) */
if (alive(defender)
&& alive(atker)
&& defender->side == os
&& uu_countercapture(atker->type, defender->type) > 0) {
attempt_to_capture_unit(defender, atker);
}
return (alive(atker) && unit_at(ox, oy) == NULL);
}
/* Fire-at action. */
/* Shooting at a given unit. */
int
prep_fire_at_action(unit, unit2, defender, m)
Unit *unit, *unit2, *defender;
int m;
{
if (unit == NULL || unit->act == NULL)
return FALSE;
if (unit2 == NULL)
return FALSE;
unit->act->nextaction.type = ACTION_FIRE_AT;
unit->act->nextaction.args[0] = defender->id;
unit->act->nextaction.args[1] = m;
unit->act->nextaction.actee = unit2->id;
return TRUE;
}
int
do_fire_at_action(unit, unit2, defender, m)
Unit *unit, *unit2, *defender;
int m;
{
int ux = unit->x, uy = unit->y, ox, oy, oz;
action_point(unit2->side, defender->x, defender->y);
action_point(defender->side, defender->x, defender->y);
update_fire_at_display(unit2->side, unit, defender, m, TRUE);
update_fire_at_display(defender->side, unit, defender, m, TRUE);
ox = defender->x; oy = defender->y; oz = defender->z;
amain = unit; omain = defender;
fire_on_unit(unit, defender);
reckon_damage();
if (alive(unit))
use_up_acp(unit, u_acp_to_fire(unit2->type));
/* if (alive(defender)) use_up_acp(defender, 1); */
/* Each side sees what happened to its own unit. */
update_unit_display(unit2->side, unit2, TRUE);
if (unit != unit2)
update_unit_display(unit->side, unit, TRUE);
update_unit_display(defender->side, defender, TRUE);
/* The attacking side also sees the remote cell. */
update_cell_display(unit2->side, ox, oy, TRUE);
update_cell_display(defender->side, ox, oy, TRUE);
/* Victim might see something in attacker's cell. */
update_cell_display(defender->side, ux, uy, TRUE);
/* Actually, everybody might be seeing the combat. */
all_see_cell(ux, uy);
all_see_cell(ox, oy);
/* Always expend the ammo (but only if m is a valid mtype). */
return A_ANY_DONE;
}
/* Test a fire action for plausibility. */
int
check_fire_at_action(unit, unit2, unit3, m)
Unit *unit, *unit2, *unit3;
int m;
{
int u, u2, u3, ux, uy, uz, acp, dist, m2;
if (!in_play(unit))
return A_ANY_ERROR;
if (!in_play(unit2))
return A_ANY_ERROR;
if (!in_play(unit3))
return A_ANY_ERROR;
/* We can't attack ourselves. */
if (unit2 == unit3)
return A_ANY_ERROR;
u = unit->type; u2 = unit2->type; u3 = unit3->type;
ux = unit->x; uy = unit->y; uz = unit->z;
acp = u_acp_to_fire(u2);
if (acp < 1)
return A_ANY_CANNOT_DO;
if (!has_enough_acp(unit, acp))
return A_ANY_NO_ACP;
/* Check whether we can attack from inside a transport. */
if (unit2->transport && uu_occ_combat(u2, unit2->transport->type) == 0)
return A_ANY_ERROR;
/* Check that target is in range. */
dist = distance(ux, uy, unit3->x, unit3->y);
if (dist > u_range(u2))
return A_ANY_TOO_FAR;
if (dist < u_range_min(u2))
return A_ANY_TOO_NEAR;
if (unit2->side != NULL && unit2->side == unit3->side)
return A_ANY_ERROR;
/* Check intervening elevations. */
if (found_blocking_elevation(u2, ux, uy, uz, u3, unit3->x, unit3->y, unit3->z))
return A_ANY_ERROR;
/* We have to have a minimum level of supply to be able to attack. */
for_all_material_types(m2) {
if (unit2->supply[m2] < um_to_fight(u2, m2))
return A_ANY_NO_MATERIAL;
}
/* Check for enough of right kind of ammo. */
if (is_material_type(m)) {
if (unit->supply[m] == 0)
return A_ANY_NO_MATERIAL;
} else {
if (!enough_ammo(unit2, unit3))
return A_ANY_NO_MATERIAL;
}
return A_ANY_OK;
}
/* Fire-into action. */
/* Shooting at a given location. */
int
prep_fire_into_action(unit, unit2, x, y, z, m)
Unit *unit, *unit2;
int x, y, z, m;
{
if (unit == NULL || unit->act == NULL)
return FALSE;
if (unit2 == NULL)
return FALSE;
unit->act->nextaction.type = ACTION_FIRE_INTO;
unit->act->nextaction.args[0] = x;
unit->act->nextaction.args[1] = y;
unit->act->nextaction.args[2] = z;
unit->act->nextaction.args[3] = m;
unit->act->nextaction.actee = unit2->id;
return TRUE;
}
/* One can always shoot, if the cell is visible, but there might not
not be anything to hit! No counterattacks when shooting, and the
results might not be visible to the shooter. */
int
do_fire_into_action(unit, unit2, x, y, z, m)
Unit *unit, *unit2;
int x, y, z, m;
{
int ux = unit->x, uy = unit->y, ox, oy, oz;
SideMask sidemask;
Unit *other;
Side *side;
/* Show the firing unit doing its attack. */
update_fire_into_display(unit->side, unit2, x, y, z, m, TRUE);
/* Make up the list of sides that will see the incoming fire. */
sidemask = NOSIDES;
for_all_stack(x, y, other) {
if (other->side)
sidemask = add_side_to_set(other->side, sidemask);
}
for_all_sides(side) {
if (side_in_set(side, sidemask))
update_fire_into_display(side, unit2, x, y, z, m, TRUE);
}
/* If any units at target, hit them. */
for_all_stack(x, y, other) {
ox = other->x; oy = other->y; oz = other->z;
amain = unit; omain = other;
fire_on_unit(unit2, other);
reckon_damage();
/* Each side sees what happened to its unit that is being hit. */
update_unit_display(other->side, other, TRUE);
/* The attacking side also sees the remote cell. */
update_cell_display(unit->side, ox, oy, TRUE);
update_cell_display(other->side, ox, oy, TRUE);
/* Victim might see something in attacker's cell. */
update_cell_display(other->side, ux, uy, TRUE);
/* Actually, everybody might be seeing the combat. */
all_see_cell(ux, uy);
all_see_cell(ox, oy);
/* don't take moves though! */
}
/* Firing side gets just one update. */
update_unit_display(unit2->side, unit2, TRUE);
if (unit != unit2)
update_unit_display(unit->side, unit, TRUE);
if (alive(unit))
use_up_acp(unit, u_acp_to_fire(unit2->type));
/* Always expend the ammo (but only if m is a valid material). */
/* We're always "successful", even though the bombardment may have
had little or no actual effect. */
return A_ANY_DONE;
}
/* Test a shoot action for plausibility. */
int
check_fire_into_action(unit, unit2, x, y, z, m)
Unit *unit, *unit2;
int x, y, z, m;
{
int u, u2, u2x, u2y, u2z, acp, dist, m2;
if (!in_play(unit))
return A_ANY_ERROR;
if (!in_play(unit2))
return A_ANY_ERROR;
u2x = unit2->x; u2y = unit2->y; u2z = unit2->z;
/* Check that target location is meaningful. */
if (!inside_area(x, y))
return A_FIRE_INTO_OUTSIDE_WORLD;
u = unit->type;
u2 = unit2->type;
acp = u_acp_to_fire(u2);
if (acp < 1)
return A_ANY_CANNOT_DO;
if (!has_enough_acp(unit, acp))
return A_ANY_NO_ACP;
/* Check whether we can attack from inside a transport. */
if (unit2->transport && uu_occ_combat(u2, unit2->transport->type) == 0)
return A_ANY_ERROR;
/* Check that target is in range. */
dist = distance(u2x, u2y, x, y);
if (dist > u_range(u2))
return A_ANY_TOO_FAR;
if (dist < u_range_min(u2))
return A_ANY_TOO_NEAR;
/* Check intervening elevations and terrain. */
if (found_blocking_elevation(u2, u2x, u2y, u2z, NONUTYPE, x, y, z))
return A_ANY_ERROR;
/* We have to have a minimum level of supply to be able to attack. */
for_all_material_types(m2) {
if (unit2->supply[m2] < um_to_fight(u2, m2))
return A_ANY_NO_MATERIAL;
}
/* Check for enough of right kind of ammo. */
if (is_material_type(m)) {
if (unit->supply[m] == 0)
return A_ANY_NO_MATERIAL;
} else {
/* should just assume amount is appropriate? */
}
return A_ANY_OK;
}
int
found_blocking_elevation(u, ux, uy, uz, u2, u2x, u2y, u2z)
int u, ux, uy, uz, u2, u2x, u2y, u2z;
{
/* int maxrise = u_elev_at_max_range(u); */
if (world_is_flat())
return FALSE;
/* Adjacent cells can't be screened by elevation. */
/* (should accommodate possibility that target is at top of
cliff in adj and back away from its edge, thus screened) */
if (distance(ux, uy, u2x, u2y) <= 1)
return FALSE;
/* (should add scan of path) */
return FALSE;
}
static void
fire_on_unit(atker, other)
Unit *atker, *other;
{
int m, dist = distance(atker->x, atker->y, other->x, other->y);
if (alive(atker) && alive(other)) {
if (enough_ammo(atker, other)) {
maybe_hit_unit(atker, other, TRUE, (dist > u_hit_falloff_range(atker->type)));
for_all_material_types(m) {
if (um_hit_by(other->type, m) > 0) {
atker->supply[m] -= um_consumption_per_attack(atker->type, m);
}
}
/* The *victim* can lose acp. */
use_up_acp(other, uu_acp_to_be_fired_on(other->type, atker->type));
}
}
/* (should ping victim to see if it wants to respond to the attack?) */
}
/* Test to see if enough ammo is available to make the attack.
Need enough of *all* types - semi-bogus but too complicated otherwise? */
int
enough_ammo(unit, other)
Unit *unit, *other;
{
int m;
for_all_material_types(m) {
if (um_hit_by(other->type, m) > 0 &&
unit->supply[m] < um_consumption_per_attack(unit->type, m))
return FALSE;
}
return TRUE;
}
/* Single attack, no counterattack. Check and use ammo - usage independent
of outcome, but types used depend on unit types involved. */
static void
attack_unit(atker, other)
Unit *atker, *other;
{
int m;
if (alive(atker) && alive(other)) {
if (enough_ammo(atker, other)) {
maybe_hit_unit(atker, other, FALSE, FALSE);
for_all_material_types(m) {
if (um_hit_by(other->type, m) > 0) {
atker->supply[m] -= um_consumption_per_attack(atker->type, m);
}
}
}
}
/* (should ping victim to see if it wants to respond to the attack?) */
}
/* Make a single hit and maybe hit some passengers also. Power of hit
is constant, but chance is affected by terrain, quality,
and occupants' protective abilities. If a hit is successful, it may
have consequences on the defender's occupants, but limited by the
protection that the transport provides. */
static void
maybe_hit_unit(atker, other, fire, fallsoff)
Unit *atker, *other;
int fire, fallsoff;
{
int chance, t, hit = 0, a = atker->type, o = other->type;
int cxpeffect, cxpmax, effect, prot;
int dist, disthit, rangedelta, hitdelta, rangeamt;
int dmgspec;
int hitmovietype;
Unit *occ, *unit2;
Side *side3;
Dprintf("%s tries to hit %s", unit_desig(atker), unit_desig(other));
if (fire && uu_fire_hit(a, o) != -1)
chance = uu_fire_hit(a, o);
else
chance = uu_hit(a, o);
/* Combat experience tends to raise the hit chance, so do that first. */
cxpmax = u_cxp_max(a);
if (cxpmax > 0 && atker->cxp > 0) {
cxpeffect = uu_hit_cxp(a, o);
if (cxpeffect != 100) {
effect = 100 + (atker->cxp * (cxpeffect - 100)) / cxpmax;
chance = (chance * effect) / 100;
}
}
/* (should modify due to cxp of defender too) */
/* Account for terrain effects. */
t = terrain_at(atker->x, atker->y);
chance = (chance * ut_attack_terrain_effect(a, t)) / 100;
t = terrain_at(other->x, other->y);
chance = (chance * ut_defend_terrain_effect(o, t)) / 100;
/* Account for protective units nearby. */
for_all_occupants(other, occ) {
if (in_play(occ) && completed(occ)) {
prot = uu_protection(occ->type, o);
if (prot != 100)
chance = (chance * prot) / 100;
}
}
if (other->transport
&& in_play(other->transport)
&& completed(other->transport)) {
prot = uu_protection(other->transport->type, o);
if (prot != 100)
chance = (chance * prot) / 100;
}
for_all_stack(other->x, other->y, unit2) {
if (unit2 != other
&& in_play(unit2)
&& completed(unit2)
&& unit2->side == other->side) {
prot = uu_stack_protection(unit2->type, o);
if (prot != 100)
chance = (chance * prot) / 100;
}
}
if (fallsoff) {
dist = distance(atker->x, atker->y, other->x, other->y);
disthit = uu_hit_max_range_effect(a, o);
rangedelta = u_range(a) - u_hit_falloff_range(a);
rangeamt = dist - u_hit_falloff_range(a);
hitdelta = uu_hit(a, o) - disthit;
chance = uu_hit(a, o)
- ((uu_hit(a, o) - disthit) * rangeamt) / rangedelta;
}
Dprintf(", probability of hit is %d%%", chance);
/* Compute the hit itself. */
if (probability(chance)) {
if (fire && uu_fire_damage(a, o) != -1)
dmgspec = uu_fire_damage(a, o);
else
dmgspec = uu_damage(a, o);
/* Account for attacker's experience. */
if (cxpmax > 0 && atker->cxp > 0) {
cxpeffect = uu_damage_cxp(a, o);
if (cxpeffect != 100) {
effect = 100 + (atker->cxp * (cxpeffect - 100)) / cxpmax;
dmgspec = multiply_dice(dmgspec, effect);
}
}
hit = roll_dice(dmgspec);
}
if (hit > 0) {
Dprintf(", damage will be %d hp", hit);
} else {
Dprintf(", missed");
}
/* (should record a raw statistic?) */
/* Ablation is a chance for occupants or stack to take part of a hit themselves. */
if (hit > 0) {
/* (should decide how ablation computed) */
}
if (hit > 0) {
chance = uu_retreat_chance(a, o);
/* (should adjust chance by morale etc) */
if (probability(chance)) {
if (retreat_unit(other, atker)) {
notify_combat(other, atker, "%s retreats!");
hit = 0; /* should only be reduced hit, may still be > 0 */
}
}
}
hit_unit(other, hit, atker);
for_all_sides(side3) {
if (active_display(side3)
&& (all_see_all
|| (side3 == atker->side || side3 == other->side))) {
hitmovietype = ((hit >= other->hp) ? movie_death :
((hit > 0) ? movie_hit : movie_miss));
schedule_movie(side3, hitmovietype, other->id);
}
}
Dprintf("\n");
/* Recurse into occupants, maybe hit them too. */
for_all_occupants(other, occ) {
if (probability(100 - uu_protection(o, occ->type))) {
maybe_hit_unit(atker, occ, fire, fallsoff);
}
}
/* We get combat experience only if there could have been some damage. */
if (chance > 0) {
if (atker->cxp < u_cxp_max(a))
atker->cxp += uu_cxp_per_combat(a, o);
if (other->cxp < u_cxp_max(o))
other->cxp += uu_cxp_per_combat(o, a);
/* Occupants already gained their experience in the recursive call. */
}
}
/* Do the hit itself. */
static void
hit_unit(unit, hit, atker)
Unit *unit, *atker;
int hit;
{
int u = unit->type, hpmin;
/* Some units might detonate automatically upon being hit. */
if (hit > 0
&& atker != NULL
&& probability(uu_detonate_on_hit(u, atker->type))
&& !was_detonated(unit)) {
detonate_unit(unit, unit->x, unit->y, unit->z);
/* If the detonating unit still exists, then continue
on to normal damage computation. */
}
/* Record the loss of hp. */
unit->hp2 -= hit;
/* Attacker might not be able to do any more damage. Note that the
positioning of this code is such that all the usual side effects
of combat happen, but the victim doesn't get any more worse off
than it is already. */
if (atker != NULL) {
hpmin = uu_hp_min(atker->type, u);
if (hpmin > 0 && hpmin > unit->hp2) {
unit->hp2 = hpmin;
}
}
/* Maybe record for statistical analysis. */
/* (this is only useful if code always goes through here - is that true?) */
if (atker != NULL && atker->side != NULL) {
if (atker->side->atkstats[atker->type] == NULL)
atker->side->atkstats[atker->type] = (long *) xmalloc(numutypes * sizeof(long));
if (atker->side->hitstats[atker->type] == NULL)
atker->side->hitstats[atker->type] = (long *) xmalloc(numutypes * sizeof(long));
++((atker->side->atkstats[atker->type])[u]);
(atker->side->hitstats[atker->type])[u] += hit;
}
/* Some units may detonate automatically just before dying. */
if (hit > 0
&& unit->hp2 <= 0
&& probability(u_detonate_on_death(u))
&& !was_detonated(unit)) {
detonate_unit(unit, unit->x, unit->y, unit->z);
}
}
/* Hits on the main units have to be done later, so that mutual
destruction works properly. This function also does all the notifying. */
/* (What if occupants change type when killed, but transport vanishes?) */
void
reckon_damage()
{
/* Entertain everybody. */
play_movies(ALLSIDES);
/* Report the damage in more detail, now before the actual damage
is taken (which may cause many units to disappear). */
/* Normally we report the defender and then the attacker's damage,
but if the defender dies, we report its counterattack results
first and its death second. */
if (!(omain->hp2 <= 0 && amain->hp2 > 0)) {
report_damage(omain, amain, omain);
report_damage(amain, omain, amain);
} else {
report_damage(amain, omain, amain);
report_damage(omain, amain, omain);
}
damage_unit(omain, combat_dmg);
damage_unit(amain, combat_dmg);
}
static void
reckon_damage_here(x, y)
int x, y;
{
Unit *unit;
for_all_stack(x, y, unit) {
damage_unit(unit, combat_dmg);
}
}
void
reckon_damage_around(x, y, r)
int x, y, r;
{
if (r > 0) {
apply_to_area(x, y, r, reckon_damage_here);
} else {
reckon_damage_here(x, y);
}
}
static void
report_damage(unit, atker, mainunit)
Unit *unit, *atker, *mainunit;
{
Unit *occ;
if (unit->hp2 <= 0) {
if (unit == mainunit) {
notify_combat(atker, unit, "%s destroys %s!");
} else {
notify_combat(unit, atker, " (and destroys occupant %s!)");
}
} else if (unit->hp2 < unit->hp) {
if (unit == mainunit) {
notify_combat(atker, unit, "%s hits %s!");
} else {
notify_combat(unit, atker, " (and hits occupant %s!)");
}
} else {
/* messages about misses not too useful */
}
for_all_occupants(unit, occ) {
report_damage(occ, atker, mainunit);
}
}
/* Make the intended damage become real, and do any consequences. */
void
damage_unit(unit, dmgreason)
Unit *unit;
enum damage_reasons dmgreason;
{
int newacp;
HistEventType hevttype;
Obj *dameff;
Unit *occ;
/* Process all the occupants first. */
for_all_occupants(unit, occ) {
damage_unit(occ, dmgreason);
}
/* If no damage was recorded, just return. */
if (unit->hp2 == unit->hp)
return;
/* If unit is to die, do the consequences. */
if (unit->hp2 <= 0) {
if (u_wrecked_type(unit->type) == NONUTYPE) {
/* (should let occupants escape now?) */
hevttype = (dmgreason == combat_dmg ? H_UNIT_KILLED :
(dmgreason == accident_dmg ? H_UNIT_DIED_IN_ACCIDENT :
0));
kill_unit(unit, hevttype);
} else {
hevttype = (dmgreason == combat_dmg ? H_UNIT_WRECKED :
(dmgreason == accident_dmg ? H_UNIT_WRECKED_IN_ACCIDENT :
0));
change_unit_type(unit, u_wrecked_type(unit->type), hevttype);
/* Restore to default hp for the new type. */
unit->hp = unit->hp2 = u_hp(unit->type);
/* Get rid of occupants if overfull. */
eject_excess_occupants(unit);
/* change_unit_type already reported the wrecking as an event,
do we need any additional reportage? */
}
} else {
record_event(H_UNIT_DAMAGED, add_side_to_set(unit->side, NOSIDES),
unit->id, unit->hp, unit->hp2);
/* Change the unit's hp. */
unit->hp = unit->hp2;
/* Perhaps adjust the acp down. */
if (unit->act != NULL
&& unit->act->acp > 0
&& (dameff = u_acp_damage_effect(unit->type)) != lispnil) {
newacp = damaged_acp(unit, dameff);
/* The damaged acp limits the remaining acp, rather than trying
to do some sort of proportional adjustment, which would be
hard to get right. */
/* (should account for occupant effects on acp) */
unit->act->acp = min(unit->act->acp, newacp);
}
}
/* Clear any detonation flag that might have been set. */
if (alive(unit))
set_was_detonated(unit, FALSE);
/* Let the unit's owner know about all this. */
update_unit_display(unit->side, unit, TRUE);
}
/* Retreat is a special kind of movement that a unit uses to avoid
damage during combat. It bypasses some of the normal move rules. */
static int
retreat_unit(unit, atker)
Unit *unit, *atker;
{
int dir;
extern int retreating_from;
retreating_from = atker->type;
if (unit->x == atker->x && unit->y == atker->y) {
dir = random_dir();
} else {
dir = approx_dir(unit->x - atker->x, unit->y - atker->y);
}
if (retreat_in_dir(unit, dir))
return TRUE;
if (flip_coin()) {
if (retreat_in_dir(unit, left_dir(dir)))
return TRUE;
if (retreat_in_dir(unit, right_dir(dir)))
return TRUE;
} else {
if (retreat_in_dir(unit, right_dir(dir)))
return TRUE;
if (retreat_in_dir(unit, left_dir(dir)))
return TRUE;
}
retreating_from = NONUTYPE;
return FALSE;
}
static int
retreat_in_dir(unit, dir)
Unit *unit;
int dir;
{
int nx, ny, rslt;
extern int retreating;
extern int retreating_from;
/* (should it be possible for a unit to retreat out of the world?) */
if (!interior_point_in_dir(unit->x, unit->y, dir, &nx, &ny))
return FALSE;
retreating = TRUE;
rslt = check_move_action(unit, unit, nx, ny, unit->z);
if (!valid(rslt))
return FALSE;
do_move_action(unit, unit, nx, ny, unit->z);
retreating = FALSE;
retreating_from = NONUTYPE;
return TRUE;
}
/* Capture action. */
/* Prepare a capture action to be executed later. */
int
prep_capture_action(unit, unit2, unit3)
Unit *unit, *unit2, *unit3;
{
if (unit == NULL || unit->act == NULL)
return FALSE;
if (unit2 == NULL)
return FALSE;
unit->act->nextaction.type = ACTION_CAPTURE;
unit->act->nextaction.args[0] = unit3->id;
unit->act->nextaction.actee = unit2->id;
return TRUE;
}
/* Execute a capture action. */
int
do_capture_action(unit, unit2, unit3)
Unit *unit, *unit2, *unit3;
{
int rslt;
attempt_to_capture_unit(unit2, unit3);
use_up_acp(unit, uu_acp_to_capture(unit2->type, unit3->type));
if (unit3->side == unit2->side)
rslt = A_CAPTURE_SUCCEEDED;
else
rslt = A_CAPTURE_FAILED;
return rslt;
}
/* Check the validity of a capture action. */
int
check_capture_action(unit, unit2, unit3)
Unit *unit, *unit2, *unit3;
{
int u, u2, u3, acp, m;
if (!in_play(unit))
return A_ANY_ERROR;
if (!in_play(unit2))
return A_ANY_ERROR;
if (!in_play(unit3))
return A_ANY_ERROR;
/* We can't capture ourselves. */
if (unit2 == unit3)
return A_ANY_ERROR;
/* We can't capture units on our side. */
if (unit2->side == unit3->side)
return A_ANY_ERROR;
u = unit->type;
u2 = unit2->type;
u3 = unit3->type;
acp = uu_acp_to_capture(u2, u3);
if (acp < 1)
return A_ANY_CANNOT_DO;
if (capture_chance(u2, u3, unit3->side) == 0)
return A_ANY_CANNOT_DO;
if (distance(unit2->x, unit2->y, unit3->x, unit3->y) > 1)
return A_ANY_ERROR;
if (!has_enough_acp(unit, acp))
return A_ANY_NO_ACP;
/* We have to have a minimum level of supply to be able to capture. */
for_all_material_types(m) {
if (unit2->supply[m] < um_to_fight(u2, m))
return A_ANY_NO_MATERIAL;
}
return A_ANY_OK;
}
/* Handle capture possibility and repulse/slaughter. */
/* The chance to capture an enemy is modified by several factors.
Neutrals have a different chance to be captured, and presence of
occupants should also has an effect. Can't capture anything that is
on a kind of terrain that the capturer can't go on, unless victim has
"bridge effect". */
/* (Need a little better treatment of committed assaults, where lack of
success == death.) */
static void
attempt_to_capture_unit(atker, other)
Unit *atker, *other;
{
int a = atker->type, o = other->type, chance, prot;
int ox = other->x, oy = other->y;
Unit *occ;
Side *as = atker->side, *os = other->side;
chance = capture_chance(a, o, other->side);
if (alive(atker) && alive(other) && chance > 0) {
if (impassable(atker, ox, oy) && !uu_bridge(o, a))
return;
/* Can possibly detonate on *any* attempt to capture! */
if (probability(uu_detonate_on_capture(o, a))
&& !was_detonated(other)) {
detonate_unit(other, other->x, other->y, other->z);
/* Might not be possible to capture anything anymore. */
if (!alive(atker) || !alive(other))
return;
/* Types of units might have changed, recalc things. */
a = atker->type; o = other->type;
as = atker->side; os = other->side;
chance = capture_chance(a, o, other->side);
}
/* Occupants can protect the transport. */
for_all_occupants(other, occ) {
if (is_active(occ)) {
prot = uu_protection(occ->type, o);
chance = (chance * prot) / 100;
}
}
/* Test whether the capture actually happens. */
if (probability(chance)) {
capture_unit(atker, other);
} else if (atker->transport != NULL &&
(impassable(atker, ox, oy) ||
impassable(atker, atker->x, atker->y))) {
/* was the capture attempt a one-way trip? */
/* (should fix the test above - needs to be more accurate) */
notify_combat(other, atker, "%s resists capture; %s slaughtered!");
kill_unit(atker, H_UNIT_KILLED /* should be something appropriate */);
} else {
notify_combat(other, atker, "%s throws back %s!");
/* (should record failed attempt to capture?) */
}
if (chance > 0) {
if (atker->cxp < u_cxp_max(a))
atker->cxp += uu_cxp_per_capture(a, o);
/* (should not increment if side just changed?) */
if (other->cxp < u_cxp_max(o))
other->cxp += uu_cxp_per_capture(o, a);
}
}
}
int
capture_chance(u, u2, side2)
int u, u2;
Side *side2;
{
int chance, indepchance;
chance = uu_capture(u, u2);
if (side2 != NULL)
return chance;
indepchance = uu_indep_capture(u, u2);
return (indepchance < 0 ? chance : indepchance);
}
/* There are many consequences of a unit being captured. */
void
capture_unit(unit, pris)
Unit *unit, *pris;
{
int u = unit->type, px = pris->x, py = pris->y;
Unit *occ;
Side *ps = pris->side, *us = unit->side, *newside;
newside = unit->side;
/* Return a unit to its original side if we are buds with that side. */
if (pris->origside != newside && trusted_side(us, pris->origside))
newside = pris->origside;
if (probability(uu_scuttle(pris->type, unit->type))) {
/* (should add terrain effect on success too) */
/* (should characterize as a scuttle) */
kill_unit(pris, H_UNIT_DISBANDED);
}
if (!unit_allowed_on_side(pris, newside)) {
kill_unit(pris, H_UNIT_KILLED);
}
if (alive(pris)) {
if (newside == pris->origside)
notify_combat(unit, pris, "%s liberates %s!");
else
notify_combat(unit, pris, "%s captures %s!");
/* Decide the fate of each occupant of our prisoner. */
for_all_occupants(pris, occ) {
capture_occupant(unit, pris, occ, newside);
}
/* The change of side itself. This happens recursively to any
remaining occupants as well. */
change_unit_side(pris, newside, H_UNIT_CAPTURED, unit);
/* Garrison the newly-captured unit with hp from the capturing unit. */
garrison_unit(unit, pris);
capture_unit_2(unit, pris, ps);
/* The people at the new location may change sides immediately. */
if (people_sides_defined()
&& any_people_side_changes
&& probability(people_surrender_chance(pris->type, px, py))) {
change_people_side_around(px, py, pris->type, unit->side);
}
}
/* Update everybody's view of the situation. */
see_exact(ps, px, py);
update_cell_display(ps, px, py, TRUE);
all_see_cell(px, py);
}
/* Given that the main unit is going to be captured, decide what each occupant
will do. */
static void
capture_occupant(unit, pris, occ, newside)
Unit *unit, *pris, *occ;
Side *newside;
{
int u = unit->type;
Unit *subocc;
if (probability(uu_occ_escape(u, occ->type))) {
/* The occupant escapes, along with all its suboccupants. */
/* (should impl by moving to nearby cells?) */
} else if (0 /* (should allow for occ scuttling also) */) {
} else if (/* u_change_side(occ->type) || */ capture_chance(u, occ->type, occ->side) > 0) {
/* Side change will actually happen later. */
for_all_occupants(occ, subocc) {
capture_occupant(unit, occ, subocc, newside);
}
} else {
/* Occupant can't live as a prisoner, but suboccs might. */
for_all_occupants(occ, subocc) {
capture_occupant(unit, occ, subocc, newside);
}
/* Any suboccupants that didn't escape will die. */
/* (what if subocc captured tho? should move elsewhere) */
kill_unit(occ, H_UNIT_KILLED);
}
}
static void
capture_unit_2(unit, pris, prevside)
Unit *unit, *pris;
Side *prevside;
{
Unit *occ;
/* Our new unit's experience might be higher or lower, depending on what
capture really means (change of crew perhaps). */
pris->cxp = (pris->cxp * u_cxp_on_capture(pris->type)) / 100;
pris->cxp = min(unit->cxp, u_cxp_max(pris->type));
/* Clear any actions and plans. */
/* Don't use init_unit_actorstate! We just want to cancel any pending
action, but leave all the acp values untouched. */
if (pris->act)
pris->act->nextaction.type = ACTION_NONE;
/* (should probably adjust side's acp sums by new unit's amounts) */
init_unit_plan(pris);
/* Assign a number to this unit if it should have one. */
assign_unit_number(pris);
/* Likewise for occupants. */
for_all_occupants(pris, occ) {
capture_unit_2(unit, occ, prevside);
}
}
/* A detonate action just blasts the vicinity. */
int
prep_detonate_action(unit, unit2, x, y, z)
Unit *unit, *unit2;
int x, y, z;
{
if (unit == NULL || unit->act == NULL)
return FALSE;
if (unit2 == NULL)
return FALSE;
unit->act->nextaction.type = ACTION_DETONATE;
unit->act->nextaction.args[0] = x;
unit->act->nextaction.args[1] = y;
unit->act->nextaction.args[2] = z;
unit->act->nextaction.actee = unit2->id;
return TRUE;
}
int
do_detonate_action(unit, unit2, x, y, z)
Unit *unit, *unit2;
int x, y, z;
{
int u2 = unit2->type;
detonate_unit(unit2, x, y, z);
/* Note that if the maxrange is further than the actual range of this
detonation, only just-damaged units will be looked at. */
reckon_damage_around(x, y, max_u_detonate_effect_range);
/* Unit might have detonated outside its range of effect, so need this to make
its own damage is accounted for. */
if (alive(unit2))
reckon_damage_around(unit2->x, unit2->y, 0);
use_up_acp(unit, u_acp_to_detonate(u2));
return A_ANY_DONE;
}
int
check_detonate_action(unit, unit2, x, y, z)
Unit *unit, *unit2;
int x, y, z;
{
int u, u2, acp;
if (!in_play(unit))
return A_ANY_ERROR;
if (!in_play(unit2))
return A_ANY_ERROR;
if (!inside_area(x, y))
return A_ANY_ERROR;
u = unit->type;
u2 = unit2->type;
/* The unit must actually be able to detonate. */
acp = u_acp_to_detonate(u2);
if (acp < 1)
return A_ANY_CANNOT_DO;
/* Can only detonate in our own or an adjacent cell. */
/* (In other words, the detonating unit doesn't get to teleport
its detonation effects to any desired faraway location.) */
if (distance(unit2->x, unit2->y, x, y) > 1)
return A_ANY_ERROR;
if (!has_enough_acp(unit, acp))
return A_ANY_NO_ACP;
return A_ANY_OK;
}
static int tmpdetx, tmpdety;
/* Actual detonation may occur by explicit action or automatically; this
routine makes the detonation effects happen, pyrotechnics and all. */
int
detonate_unit(unit, x, y, z)
Unit *unit;
int x, y, z;
{
int u = unit->type, dir, x1, y1, dmg, t, maxrange;
Unit *unit2;
if (max_u_detonate_effect_range < 0) {
int u2, u3, range;
for_all_unit_types(u2) {
for_all_unit_types(u3) {
range = uu_detonation_range(u2, u3);
max_u_detonate_effect_range =
max(range, max_u_detonate_effect_range);
}
}
}
if (max_t_detonate_effect_range < 0) {
int u2, t, range;
for_all_unit_types(u2) {
for_all_terrain_types(t) {
range = ut_detonation_range(u2, t);
max_t_detonate_effect_range =
max(range, max_t_detonate_effect_range);
}
}
}
notify_combat(unit, unit, "%s detonates!");
set_was_detonated(unit, TRUE);
/* Hit the detonating unit first. */
hit_unit(unit, u_hp_per_detonation(u), NULL);
/* Hit units at ground zero. */
for_all_stack(x, y, unit2) {
if (unit2 != unit) {
hit_unit(unit2, uu_detonation_damage_at(u, unit2->type), unit);
}
}
damage_terrain(u, x, y);
/* Hit units and/or terrain in adjacent cells, if this is defined. */
if (max_u_detonate_effect_range >= 1) {
for_all_directions(dir) {
if (interior_point_in_dir(x, y, dir, &x1, &y1)) {
for_all_stack(x1, y1, unit2) {
dmg = uu_detonation_damage_adj(u, unit2->type);
hit_unit(unit2, dmg, unit);
}
}
}
}
if (max_t_detonate_effect_range >= 1) {
for_all_directions(dir) {
if (point_in_dir(x, y, dir, &x1, &y1)) {
damage_terrain(u, x1, y1);
}
}
}
/* Hit units that are further away. */
maxrange = max(max_u_detonate_effect_range, max_t_detonate_effect_range);
if (maxrange >= 2) {
tmpunit = unit;
tmpdetx = x; tmpdety = y;
apply_to_area(x, y, maxrange, detonate_on_cell);
}
/* (should test compatibility of any new terrain types with each other;
only after changes over with) */
return TRUE;
}
static void
detonate_on_cell(x, y)
int x, y;
{
int dist, dmg, sdmg, t;
Unit *unit2;
dist = distance(tmpdetx, tmpdety, x, y);
if (dist > 1 && dist <= max_u_detonate_effect_range) {
for_all_stack(x, y, unit2) {
if (dist <= uu_detonation_range(tmpunit->type, unit2->type)) {
dmg = uu_detonation_damage_adj(tmpunit->type, unit2->type);
/* Reduce by inverse square of the distance. */
sdmg = (dmg * 100) / (dist * dist);
dmg = prob_fraction(sdmg);
hit_unit(unit2, dmg, tmpunit);
}
}
}
if (dist > 1 && dist <= max_t_detonate_effect_range) {
damage_terrain(tmpunit->type, x, y);
}
}
void
damage_terrain(u, x, y)
int u, x, y;
{
int t, t2, dir, x1, y1;
/* Damage the cell's terrain. */
t = terrain_at(x, y);
if (probability(ut_detonation_damage(u, t))) {
t2 = damaged_terrain_type(t);
if (t2 == NONTTYPE) {
run_error("bad damaged type?");
return;
} else if (t2 != t) {
change_terrain_type(x, y, t2);
}
}
/* Apply to auxiliary terrain also. */
if (1 /* if any aux terrain */) {
for_all_terrain_types(t) {
switch (t_subtype(t)) {
case cellsubtype:
/* We already did this one. */
break;
case bordersubtype:
for_all_directions(dir) {
if (1) {
if (border_at(x, y, dir, t)
&& probability(ut_detonation_damage(u, t))) {
t2 = damaged_terrain_type(t);
if (t2 == NONTTYPE) {
set_border_at(x, y, dir, t, FALSE);
} else if (t2 != t) {
set_border_at(x, y, dir, t, FALSE);
set_border_at(x, y, dir, t2, TRUE);
/* There is potentially a problem with some
game designs here; if the new type t2 can
also be damaged by the detonation, then
the loop here will damage it in turn, at
least if t2 *follows* t in the list of terrain
types. Preventing this would require a lot of
buffering, so it's left as a limitation for now. */
}
}
}
}
break;
case connectionsubtype:
for_all_directions(dir) {
if (1) {
if (connection_at(x, y, dir, t)
&& probability(ut_detonation_damage(u, t))) {
t2 = damaged_terrain_type(t);
if (t2 == NONTTYPE) {
set_connection_at(x, y, dir, t, FALSE);
} else if (t2 != t) {
set_connection_at(x, y, dir, t, FALSE);
set_connection_at(x, y, dir, t2, TRUE);
}
}
}
}
break;
case coatingsubtype:
/* don't know how to damage coatings yet */
break;
}
}
}
}
int
damaged_terrain_type(t)
int t;
{
int t2, tot, othertot, test, sum, rslt;
tot = othertot = 0;
for_all_terrain_types(t2) {
if (t_subtype(t2) == t_subtype(t)) {
tot += tt_damaged_type(t, t2);
} else {
othertot += tt_damaged_type(t, t2);
}
}
rslt = NONTTYPE;
if ((tot + othertot) > 0) {
test = xrandom(tot + othertot);
sum = 0;
for_all_terrain_types(t2) {
if (t_subtype(t2) == t_subtype(t)) {
sum += tt_damaged_type(t, t2);
if (test < sum) {
rslt = t2;
break;
}
}
}
}
/* Random values between tot and othertot will have
fallen through the loop, and the rslt is NONTTYPE,
which indicates that the terrain must be removed
if possible. */
/* Paranoia check */
if (rslt != NONTTYPE && t_subtype(rslt) != t_subtype(t))
run_error("badness in damaged_terrain_type");
return rslt;
}
static void
notify_combat(unit1, unit2, str)
Unit *unit1, *unit2;
char *str;
{
char buf1[BUFSIZE], buf2[BUFSIZE];
Side *side3;
for_all_sides(side3) {
if (active_display(side3)
&& (all_see_all || side3 == unit1->side || side3 == unit2->side)) {
strcpy(buf1, unit_handle(side3, unit1));
strcpy(buf2, unit_handle(side3, unit2));
notify(side3, str, buf1, buf2);
}
}
}
/* Tests for whether given units or types can do particular types of actions. */
int
can_attack(unit)
Unit *unit;
{
return type_can_attack(unit->type);
}
int
type_can_attack(u)
int u;
{
int u2;
for_all_unit_types(u2) {
if (uu_acp_to_attack(u, u2) > 0 && uu_hit(u, u2) > 0 && uu_damage(u, u2) > 0)
return TRUE;
}
return FALSE;
}
int
can_fire(unit)
Unit *unit;
{
return type_can_fire(unit->type);
}
int
type_can_fire(u)
int u;
{
int u2, fhit, fdam;
if (u_acp_to_fire(u) == 0)
return FALSE;
for_all_unit_types(u2) {
fhit = uu_fire_hit(u, u2);
fdam = uu_fire_damage(u, u2);
if ((fhit == -1 ? (uu_hit(u, u2) > 0) : (fhit > 0))
&& (fdam == -1 ? (uu_damage(u, u2) > 0) : (fdam > 0)))
return TRUE;
}
return FALSE;
}
int
type_can_capture(u)
int u;
{
int u2;
for_all_unit_types(u2) {
if (uu_acp_to_capture(u, u2) > 0
&& (uu_capture(u, u2) > 0 || uu_indep_capture(u, u2) > 0))
return TRUE;
}
return FALSE;
}
int
can_detonate(unit)
Unit *unit;
{
return (u_acp_to_detonate(unit->type) > 0);
}
|