2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2015, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * attsub.c: Attack subroutines
29 * Known contributors to this file:
31 * Steve McClure, 1996-2000
32 * Markus Armbruster, 2006-2015
52 #include "prototypes.h"
56 #define CASUALTY_LUMP 1 /* How big casualty chunks should be */
58 static void ask_olist(int combat_mode, struct combat *off,
59 struct combat *def, struct emp_qelem *olist,
60 char *land_answer, int *a_spyp, int *a_engineerp);
61 static void take_move_in_mob(int combat_mode, struct ulist *llp,
62 struct combat *off, struct combat *def);
63 static void move_in_land(int combat_mode, struct combat *off,
64 struct emp_qelem *olist, struct combat *def);
65 static void ask_move_in(struct combat *off, struct emp_qelem *olist,
67 static void ask_move_in_off(struct combat *off, struct combat *def);
69 static int board_abort(struct combat *off, struct combat *def);
70 static int land_board_abort(struct combat *off, struct combat *def);
71 static int ask_off(int combat_mode, struct combat *off,
73 static void get_dlist(struct combat *def, struct emp_qelem *, int a_spy,
75 static int get_ototal(int combat_mode, struct combat *off,
76 struct emp_qelem *olist, double osupport, int check);
77 static int get_dtotal(struct combat *def, struct emp_qelem *list,
78 double dsupport, int check);
79 static double att_calcodds(int, int);
80 static int take_casualty(int combat_mode, struct combat *off,
81 struct emp_qelem *olist);
83 static void send_reacting_units_home(struct emp_qelem *list);
84 static int take_def(int combat_mode, struct emp_qelem *list,
85 struct combat *off, struct combat *def);
87 static int get_oland(int, struct ulist *);
88 static int get_dland(struct combat *, struct ulist *);
91 /* must match combat types in combat.h */
92 "defend", "attack", "assault", "paradrop", "board", "lboard"
97 * The principal object in this code is the "combat" object. A combat object
98 * is either a sector or ship. There are
99 * usually two instances of this, the "def" or defense combat object, and
100 * the array of "off" or offense objects. The number of offense objects is
101 * determined by the value of off->last (e.g. more than one attacking sector).
102 * the type of the object is determined by combat->type which can take the
103 * values EF_SECTOR, EF_SHIP, EF_PLANE, or EF_BAD. Another important parameter
104 * which is often passed to these functions is combat_mode. This can take
105 * the value A_DEFEND, A_ATTACK, A_ASSAULT, A_PARA, A_BOARD and A_LBOARD.
106 * As these six modes of being in combat affect things like mobcost and combat
107 * value, there are often switches made on combat_mode. Note that in all cases
108 * no mobility is taken from sectors, ships, or land units until the player
109 * has committed to a fight. Instead, the cost is temporarily placed in
110 * combat->mobcost, or llp->mobil as the case may be, and then when the object
111 * is "put" back onto disk, then the amounts in these variables are subtracted
112 * from the object's mobility. It needs to be done this way as the objects
113 * are constantly being re-read from disk, and we don't want to take any mob
114 * unless a fight actually occurrs.
118 /* initialize combat object */
121 att_combat_init(struct combat *com, int type)
123 memset(com, 0, sizeof(*com));
128 /* print a combat object with optional preposition */
131 pr_com(int inon, struct combat *com, natid who)
133 if (com->type == EF_SECTOR) {
135 inon ? inon == 1 ? "in " : "into " : "",
136 xyas(com->x, com->y, who));
137 } else if (com->type == EF_SHIP) {
138 return prbuf("%s%s %s(#%d)",
139 inon ? inon == 1 ? "on " : "onto " : "",
140 com->shp_mcp->m_name, com->shp_name, com->shp_uid);
141 } else if (com->type == EF_LAND) {
142 return prbuf("%s%s #%d",
143 inon ? inon == 1 ? "on " : "onto " : "",
144 com->lnd_lcp->l_name, com->lnd_uid);
146 return "your forces";
151 prcom(int inon, struct combat *com)
153 return pr_com(inon, com, player->cnum);
157 * This is the combat object "type" based integrity check. It basically
158 * splits along three divisions: ship/sector, attacker/defender,
159 * first time/not first time.
163 att_get_combat(struct combat *com, int isdef)
177 if (!getsect(com->x, com->y, §)) {
178 pr("Bad sector: %s\n", xyas(com->x, com->y, player->cnum));
179 return att_combat_init(com, EF_BAD);
181 com->sct_type = sect.sct_type;
182 com->sct_dcp = &dchr[sect.sct_type];
183 mil = sect.sct_item[I_MILIT];
184 pstage = sect.sct_pstage;
185 owner = sect.sct_own;
186 eff = sect.sct_effic;
187 mob = sect.sct_mobil;
192 if (!getland(com->lnd_uid, &land) || !land.lnd_own) {
194 pr("Land unit #%d is not in the same sector!\n",
196 return att_combat_init(com, EF_BAD);
198 if (isdef && player->owner) {
199 pr("Boarding yourself? Try using the 'load' command.\n");
200 return att_combat_init(com, EF_BAD);
202 com->lnd_lcp = &lchr[(int)land.lnd_type];
203 mil = land.lnd_item[I_MILIT];
204 pstage = land.lnd_pstage;
205 owner = land.lnd_own;
206 eff = land.lnd_effic;
207 mob = land.lnd_mobil;
212 if (!getship(com->shp_uid, &ship) || !ship.shp_own) {
214 pr("Ship #%d is not in the same sector!\n", com->shp_uid);
216 pr("Ship #%d is not your ship!\n", com->shp_uid);
217 return att_combat_init(com, EF_BAD);
220 if (isdef && player->owner && ontradingblock(EF_SHIP, &ship)) {
221 pr("%s is on the trading block.\n", prcom(0, com));
222 return att_combat_init(com, EF_BAD);
225 if (isdef && player->owner) {
226 pr("Boarding yourself? Try using the 'tend' command.\n");
227 return att_combat_init(com, EF_BAD);
229 com->shp_mcp = &mchr[(int)ship.shp_type];
230 strncpy(com->shp_name, ship.shp_name, MAXSHPNAMLEN);
231 if (!isdef && !player->owner) {
233 pr("%s was just sunk!\n", prcom(0, com));
235 pr("Ship #%d is not your ship!\n", com->shp_uid);
236 return att_combat_init(com, EF_BAD);
238 mil = ship.shp_item[I_MILIT];
239 pstage = ship.shp_pstage;
240 owner = ship.shp_own;
241 eff = ship.shp_effic;
242 mob = ship.shp_mobil;
251 return att_combat_init(com, EF_BAD);
254 if (!com->set) { /* first time */
255 if (isdef) { /* defender */
257 } else { /* attacker */
259 pr("No mil %s\n", prcom(1, com));
261 pr("Only 1 mil %s\n", prcom(1, com));
262 /* don't abandon attacking sectors or ships */
263 com->troops = MAX(0, mil - 1);
265 com->plague = pstage == PLG_INFECT;
266 } else { /* not first time */
267 if (isdef) { /* defender */
268 if (com->x != x || com->y != y) {
269 pr("%s has moved!\n", prcom(0, com));
270 return att_combat_init(com, EF_BAD);
272 if (owner != com->own) {
274 pr("WARNING: The ownership of %s just changed from %s to %s!\n",
275 prcom(0, com), cname(com->own), cname(owner));
276 } else if (com->type == EF_SECTOR) {
277 pr("WARNING: %s just abandoned sector %s!\n",
279 xyas(com->x, com->y, player->cnum));
283 pr("WARNING: The enemy mil %s just %s from %d to %d!\n",
285 com->mil < mil ? "increased" : "decreased", com->mil,
288 } else { /* attacker */
289 if (owner != com->own && owner != player->cnum) {
290 /* must be EF_SECTOR */
292 pr("WARNING: Your %d mil in %s were destroyed because %s just took the sector!\n",
293 com->mil, xyas(com->x, com->y, player->cnum),
296 pr("%s just took %s!\n",
297 cname(owner), xyas(com->x, com->y, player->cnum));
298 return att_combat_init(com, EF_BAD);
300 if (com->troops && com->troops + 1 > mil) {
301 if (com->own == owner && player->cnum == owner)
303 pr("WARNING: Your mil %s has been reduced from %d to %d!\n",
304 prcom(1, com), com->troops, MAX(0, mil - 1));
305 com->troops = MAX(0, mil - 1);
320 * In the course of the fight, the combat object may have lost mil, eff, or
321 * mobility. This is the place where the data in the object gets flushed to
322 * disk to make it "real".
326 put_combat(struct combat *com)
335 getsect(com->x, com->y, §);
336 sect.sct_type = com->sct_type;
337 deff = sect.sct_effic - com->eff;
339 sect.sct_road -= sect.sct_road * deff / 100.0;
340 sect.sct_rail -= sect.sct_rail * deff / 100.0;
341 sect.sct_defense -= sect.sct_defense * deff / 100.0;
342 if (sect.sct_road <= 0)
344 if (sect.sct_rail <= 0)
346 if (sect.sct_defense <= 0)
347 sect.sct_defense = 0;
349 sect.sct_effic = com->eff;
351 if (opt_MOB_ACCESS) {
352 if ((com->mob - com->mobcost) < -127)
353 sect.sct_mobil = -127;
355 sect.sct_mobil = com->mob - com->mobcost;
357 if ((com->mob - com->mobcost) < 0)
360 sect.sct_mobil = com->mob - com->mobcost;
363 sect.sct_own = com->own;
365 if (sect.sct_pstage == PLG_HEALTHY)
366 sect.sct_pstage = PLG_EXPOSED;
368 sect.sct_item[I_MILIT] = com->mil;
370 com->own = sect.sct_own; /* avoid WARNING if sector reverts */
373 getland(com->lnd_uid, &land);
374 land.lnd_effic = com->eff;
376 if (com->mob - com->mobcost < -127)
377 land.lnd_mobil = -127;
379 land.lnd_mobil = (signed char)(com->mob - com->mobcost);
381 land.lnd_own = com->own;
383 if (land.lnd_pstage == PLG_HEALTHY)
384 land.lnd_pstage = PLG_EXPOSED;
386 if (!(com->lnd_lcp->l_flags & L_SPY))
387 land.lnd_item[I_MILIT] = com->mil;
388 if (com->own == player->cnum) {
389 land.lnd_mission = 0;
391 memset(land.lnd_rpath, 0, sizeof(land.lnd_rpath));
393 putland(com->lnd_uid, &land);
396 getship(com->shp_uid, &ship);
397 ship.shp_effic = com->eff;
399 if (com->mob - com->mobcost < -127)
400 ship.shp_mobil = -127;
402 ship.shp_mobil = (signed char)(com->mob - com->mobcost);
404 ship.shp_own = com->own;
406 if (ship.shp_pstage == PLG_HEALTHY)
407 ship.shp_pstage = PLG_EXPOSED;
409 ship.shp_item[I_MILIT] = com->mil;
410 if (com->own == player->cnum) {
411 ship.shp_mission = 0;
413 memset(ship.shp_rpath, 0, sizeof(ship.shp_rpath));
415 putship(com->shp_uid, &ship);
419 * FIXME if we just sank the ship, att_get_combat() will report
420 * "not in the same sector", and proceed to clobber *com. See
421 * also the workaround in boar().
423 att_get_combat(com, com->own != player->cnum);
426 /* If pre-attack, abort fight. If post-attack, don't move anything in */
431 return player->aborted = 1;
435 * This is the combat_mode based integrity check. It splits among two main
436 * divisions: first time/not first time, and attack/assault/para/board.
440 att_abort(int combat_mode, struct combat *off, struct combat *def)
446 if (att_get_combat(def, 1) < 0)
447 return abort_attack();
449 if (off && combat_mode != A_ATTACK) {
450 if (att_get_combat(off, 0) < 0)
451 return abort_attack();
452 if (off->type == EF_SHIP &&
453 !(off->x == def->x && off->y == def->y) &&
454 (!getsect(off->x, off->y, §) ||
455 sect.sct_type != SCT_WATER)) {
456 pr("%s can not %s from that far inland!\n",
457 prcom(0, off), att_mode[combat_mode]);
458 return abort_attack();
461 switch (combat_mode) {
463 if (!neigh(def->x, def->y, player->cnum) &&
464 !adj_units(def->x, def->y, player->cnum)) {
465 pr("You are not adjacent to %s\n",
466 xyas(def->x, def->y, player->cnum));
467 return abort_attack();
469 if (def->own == player->cnum) {
470 pr("You can't attack your own sector.\n");
471 return abort_attack();
475 if (off && mapdist(off->x, off->y, def->x, def->y) > 1) {
476 pr("You'll have to get there first...\n");
477 return abort_attack();
479 if (off && def->sct_type == SCT_MOUNT) {
480 pr("You can't assault a %s sector!\n", def->sct_dcp->d_name);
481 return abort_attack();
485 if (def->own == player->cnum) {
486 pr("You can't air-assault your own sector.\n");
487 return abort_attack();
489 if (off && (def->sct_type == SCT_MOUNT ||
490 def->sct_type == SCT_WATER ||
491 def->sct_type == SCT_CAPIT ||
492 def->sct_type == SCT_FORTR ||
493 def->sct_type == SCT_WASTE)) {
494 pr("You can't air-assault a %s sector!\n",
495 def->sct_dcp->d_name);
496 return abort_attack();
500 return board_abort(off, def);
502 return land_board_abort(off, def);
505 if (off && def->sct_dcp->d_mob0 < 0) {
506 pr("You can't %s a %s sector!\n",
507 att_mode[combat_mode], def->sct_dcp->d_name);
508 return abort_attack();
510 if (!off || off->relations_checked)
512 off->relations_checked = 1;
515 setcont(player->cnum, def->own, FOUND_SPY);
516 setcont(def->own, player->cnum, FOUND_SPY);
523 * Lots of special things need to be checked for boarding, so I put it in
528 board_abort(struct combat *off, struct combat *def)
530 struct shpstr aship, dship; /* for tech levels */
533 if (att_get_combat(def, 1) < 0)
534 return abort_attack();
539 if (att_get_combat(off, 0) < 0)
540 return abort_attack();
542 if (off->x != def->x || off->y != def->y) {
543 pr("Ship #%d is not in the same sector!\n", def->shp_uid);
544 return abort_attack();
546 if (off->type == EF_SHIP) {
548 pr("%s has no mobility!\n", prcom(0, off));
549 return abort_attack();
551 getship(off->shp_uid, &aship);
552 getship(def->shp_uid, &dship);
553 if (techfact(aship.shp_tech, shp_speed(&aship)) * off->eff
554 <= techfact(dship.shp_tech, shp_speed(&dship)) * def->eff) {
555 pr("Victim ship moves faster than you do!\n");
558 "%s %s failed to catch %s\n",
559 prnatid(aship.shp_own),
560 pr_com(0, off, def->own), pr_com(0, def, def->own));
561 return abort_attack();
563 } else if (off->type != EF_SECTOR) {
564 pr("Please tell the deity that you got the 'banana boat' error\n");
565 return abort_attack();
567 if (def->shp_mcp->m_flags & M_SUB) {
568 getsect(def->x, def->y, §);
569 if (sect.sct_type == SCT_WATER) {
570 pr("You can't board a submarine!\n");
571 return abort_attack();
578 * Lots of special things need to be checked for boarding, so I put it in
580 * STM - I copied it for land unit boarding. :)
584 land_board_abort(struct combat *off, struct combat *def)
586 if (att_get_combat(def, 1) < 0)
587 return abort_attack();
592 if (att_get_combat(off, 0) < 0)
593 return abort_attack();
595 if (off->x != def->x || off->y != def->y) {
596 pr("Land unit #%d is not in the same sector!\n", def->lnd_uid);
597 return abort_attack();
603 /* If we are boarding, then the defending ship gets a chance to fire back */
605 att_approach(struct combat *off, struct combat *def)
611 pr("Approaching %s...\n", prcom(0, def));
614 "%s is being approached by %s...\n",
615 pr_com(0, def, def->own), pr_com(0, off, def->own));
616 if (!(dam = shipdef(player->cnum, def->own, def->x, def->y)))
619 pr("They're firing at us sir!\n");
622 "Your fleet at %s does %d damage to %s\n",
623 xyas(def->x, def->y, def->own), dam, pr_com(0, off, def->own));
625 if (off->type == EF_SECTOR) {
626 getsect(off->x, off->y, §);
627 sectdamage(§, dam);
629 pr("Enemy fleet at %s does %d damage to %s\n",
630 xyas(def->x, def->y, player->cnum), dam, prcom(0, off));
631 } else if (off->type == EF_SHIP) {
632 getship(off->shp_uid, &ship);
633 shipdamage(&ship, dam);
634 putship(off->shp_uid, &ship);
635 if (def->own && ship.shp_effic < SHIP_MINEFF) {
636 wu(0, def->own, "%s sunk!\n", pr_com(0, off, def->own));
637 nreport(player->cnum, N_SHP_LOSE, def->own, 1);
640 if (att_get_combat(off, 0) < 0)
641 return abort_attack();
645 /* The attack is valid. Tell the attacker about what they're going to hit */
648 att_show(struct combat *def)
650 if (def->type == EF_SECTOR) {
651 pr("%s is a %d%% %s %s with approximately %d military.\n",
652 xyas(def->x, def->y, player->cnum),
653 roundintby((int)def->eff, 10),
654 cname(def->own), def->sct_dcp->d_name,
655 roundintby(def->troops, 10));
656 if (map_set(player->cnum, def->x, def->y, def->sct_dcp->d_mnem, 0))
657 writemap(player->cnum);
658 } else if (def->type == EF_SHIP || def->type == EF_LAND) {
659 pr("%s is about %d%% efficient and has approximately %d mil on board.\n",
660 prcom(0, def), roundintby((int)def->eff, 10),
661 roundintby(def->troops, 10));
665 /* Attack and assault ask the user which kind of support they want */
668 att_ask_support(int offset, int *fortp, int *shipp, int *landp,
673 *fortp = *shipp = *landp = *planep = 1;
675 if (player->argp[offset] != NULL) {
676 if ((player->argp[offset + 1] == NULL) ||
677 (player->argp[offset + 2] == NULL) ||
678 (player->argp[offset + 3] == NULL)) {
679 pr("If any support arguments are used, all must be!\n");
684 *landp = *planep = 0;
686 p = getstarg(player->argp[offset], "Use fort support? ", buf);
690 if ((*p == 'y') || (*p == 'Y'))
693 p = getstarg(player->argp[offset + 1], "Use ship support? ", buf);
697 if ((*p == 'y') || (*p == 'Y'))
700 p = getstarg(player->argp[offset + 2], "Use land support? ", buf);
704 if ((*p == 'y') || (*p == 'Y'))
707 p = getstarg(player->argp[offset + 3], "Use plane support? ", buf);
711 if ((*p == 'y') || (*p == 'Y'))
718 * Attack, assault, and board ask the attacker what they'd like to attack
719 * with. This includes mil and land units from each "off" object. Note that
720 * after each sub-prompt, we check to make sure that the attack is still
721 * valid, and if it's not, then we abort the attack.
725 att_ask_offense(int combat_mode, struct combat *off, struct combat *def,
726 struct emp_qelem *olist, int *a_spyp, int *a_engineerp)
729 char land_answer[256];
732 if (att_abort(combat_mode, off, def))
734 memset(land_answer, 0, sizeof(land_answer));
735 for (n = 0; n <= off->last; ++n) {
736 off[n].troops = ask_off(combat_mode, off + n, def);
737 if (att_abort(combat_mode, off, def))
739 ask_olist(combat_mode, off + n, def, olist, land_answer,
740 a_spyp, a_engineerp);
741 if (att_abort(combat_mode, off, def))
748 * Return path cost for @attacker to enter sector given by @def.
749 * @mobtype is a mobility type accepted by sector_mcost().
752 att_mobcost(natid attacker, struct combat *def, int mobtype)
757 if (CANT_HAPPEN(def->type != EF_SECTOR))
759 ok = getsect(def->x, def->y, §);
760 if (CANT_HAPPEN(!ok))
764 * We want the cost to move/march into the sector. If we just
765 * called sector_mcost(), we'd get the defender's cost. The
766 * attacker's cost is higher unless he's the old-owner. Note: if
767 * there are no civilians, a victorious attacker will become the
768 * old-owner. But he isn't now.
770 sect.sct_own = attacker;
772 return sector_mcost(§, mobtype);
775 /* How many mil is off allowed to attack with when it attacks def? */
778 get_mob_support(int combat_mode, struct combat *off, struct combat *def)
783 switch (combat_mode) {
785 mobcost = att_mobcost(off->own, def, MOB_MOVE);
786 if (mobcost < 0 || off->mob <= 0)
788 mob_support = off->mob / mobcost;
789 if (mob_support < off->troops)
790 pr("Sector %s has %d mobility which can only support %d mil,\n",
791 xyas(off->x, off->y, player->cnum), off->mob, mob_support);
793 mob_support = off->troops;
796 if (def->own != player->cnum && def->mil) {
797 if (off->shp_mcp->m_flags & M_SEMILAND)
798 return off->troops / 4;
799 else if (!(off->shp_mcp->m_flags & M_LAND))
800 return off->troops / 10;
804 if (off->type == EF_SECTOR && off->mob <= 0)
806 mob_support = def->shp_mcp->m_item[I_MILIT];
807 if (mob_support < off->troops)
808 pr("The size of the ship you are trying to board limits your party to %d mil,\n", mob_support);
810 mob_support = off->troops;
815 if (def->lnd_lcp->l_flags & L_SPY)
817 mob_support = def->lnd_lcp->l_item[I_MILIT];
818 if (mob_support < off->troops)
819 pr("The size of the unit you are trying to board limits your party to %d mil,\n", mob_support);
821 mob_support = off->troops;
828 * If the attacker decides to go through with the attack, then the
829 * sectors/ships they are attacking with may be charged some mobility.
830 * This is where that amount of mobility is calculated. It is actually
831 * subtracted "for real" from the object's mobility in put_combat().
835 calc_mobcost(int combat_mode, struct combat *off, struct combat *def,
842 switch (combat_mode) {
844 off->mobcost += MAX(1,
846 * att_mobcost(off->own, def, MOB_MOVE)));
849 off->mobcost += MAX(1, attacking_mil / 5);
854 off->mobcost += MAX(1, attacking_mil / 5);
857 /* the 2 in the formula below is a fudge factor */
858 getship(def->shp_uid, &ship);
859 off->mobcost += shp_speed(&ship) / 2 * def->eff / 100;
864 /* How many mil to we want to attack from off against def? */
867 ask_off(int combat_mode, struct combat *off, struct combat *def)
873 if (att_get_combat(off, 0) <= 0)
875 if ((off->type == EF_SECTOR) && (off->own != player->cnum))
877 if ((mob_support = get_mob_support(combat_mode, off, def)) <= 0)
879 if (off->type == EF_SECTOR) {
880 if (off->own != player->cnum)
882 sprintf(prompt, "Number of mil from %s at %s (max %d) : ",
883 off->sct_dcp->d_name,
884 xyas(off->x, off->y, player->cnum), mob_support);
886 sprintf(prompt, "Number of mil from %s (max %d) : ",
887 prcom(0, off), mob_support);
889 if ((attacking_mil = onearg(NULL, prompt)) < 0)
891 if (att_abort(combat_mode, off, def))
893 if (att_get_combat(off, 0) <= 0)
896 MIN(attacking_mil, MIN(mob_support, off->troops))) <= 0)
899 calc_mobcost(combat_mode, off, def, attacking_mil);
900 return attacking_mil;
904 * Which units would you like to attack with or move in with [ynYNq?]
908 att_prompt(char *prompt, char army)
916 p = getstring(prompt, buf);
917 if (!p || *p == 'q') {
923 if (tolower(*p) == 'y' || tolower(*p) == 'n')
925 pr("y - yes this unit\n"
927 "Y - yes to all units in army '%c'\n"
928 "N - no to all units in army '%c'\n"
929 "q - quit\n? - this help message\n\n",
934 /* Ask the attacker which units they want to attack/assault/board with */
937 ask_olist(int combat_mode, struct combat *off, struct combat *def,
938 struct emp_qelem *olist, char *land_answer, int *a_spyp,
943 double pathcost, mobcost;
953 if (def->type == EF_LAND)
955 if (def->type == EF_SHIP)
956 maxland = def->shp_mcp->m_nland;
958 snxtitem_xy(&ni, EF_LAND, off->x, off->y);
959 while (nxtitem(&ni, &land)) {
960 if (land.lnd_own != player->cnum)
962 if (land.lnd_effic < LAND_MINEFF)
964 if (land_answer[(int)land.lnd_army] == 'N')
966 if (!lnd_can_attack(&land))
968 lcp = &lchr[(int)land.lnd_type];
970 if (def->type == EF_SHIP && !maxland) {
971 pr("Land units are not able to board this kind of ship\n");
974 if (def->type == EF_SHIP
975 && (def->shp_mcp->m_flags & (M_SUPPLY | M_SUB)) != M_SUPPLY
976 && !(lcp->l_flags & L_LIGHT)) {
977 pr("Only light land units can board this kind of ship\n");
980 if (land.lnd_mobil <= 0) {
981 pr("%s is out of mobility, and cannot %s\n",
982 prland(&land), att_mode[combat_mode]);
987 if (ontradingblock(EF_LAND, &land)) {
988 pr("%s is on the trading block, and cannot %s\n",
989 prland(&land), att_mode[combat_mode]);
994 if (off->type == EF_SECTOR && land.lnd_ship >= 0) {
995 pr("%s is on ship #%d, and cannot %s\n",
996 prland(&land), land.lnd_ship, att_mode[combat_mode]);
998 } else if (off->type == EF_SHIP) {
999 if (land.lnd_ship != off->shp_uid)
1001 } else if (land.lnd_land >= 0) {
1002 pr("%s is on unit #%d, and cannot %s\n",
1003 prland(&land), land.lnd_land, att_mode[combat_mode]);
1006 switch (combat_mode) {
1009 * We used to let land units attack only if they have the
1010 * mobility consumed by the attack, not counting combat
1011 * and moving in to occupy. Making sure your land units
1012 * reach attack positions with enough mobility left is a
1013 * pain in the neck. We now require positive mobility,
1014 * just like for marching. Except we don't allow rushing
1015 * of high-mobility sectors (mountains): for those we
1016 * still require attack mobility.
1018 pathcost = att_mobcost(land.lnd_own, def, lnd_mobtype(&land));
1019 mobcost = lnd_pathcost(&land, pathcost);
1020 if (pathcost < 1.0) {
1021 if (land.lnd_mobil <= 0) {
1022 pr("%s is out of mobility\n", prland(&land));
1026 reqmob = MIN(land_mob_max, (int)ceil(mobcost));
1027 if (land.lnd_mobil < reqmob) {
1028 pr("%s does not have enough mobility (%d needed)\n",
1029 prland(&land), reqmob);
1037 if (!(lcp->l_flags & L_ASSAULT))
1044 att_val = attack_val(combat_mode, &land);
1045 if (att_val < 1.0) {
1046 pr("%s has no offensive strength\n", prland(&land));
1049 if (!lnd_supply_all(&land)) {
1050 pr("%s is out of supply, and cannot %s\n",
1051 prland(&land), att_mode[combat_mode]);
1054 if (def->type == EF_SHIP && first_time) {
1056 pr("You may board with a maximum of %d land units\n", maxland);
1058 pr("%s has a base %s value of %.0f\n",
1059 prland(&land), att_mode[combat_mode], att_val);
1060 if (land_answer[(int)land.lnd_army] != 'Y') {
1062 "%s with %s %s (%c %d%%) [ynYNq?] ",
1063 att_mode[combat_mode],
1066 land.lnd_army ? land.lnd_army : '~',
1068 land_answer[(int)land.lnd_army] =
1069 att_prompt(prompt, land.lnd_army);
1070 if (att_abort(combat_mode, off, def))
1072 if (land_answer[(int)land.lnd_army] != 'y' &&
1073 land_answer[(int)land.lnd_army] != 'Y')
1076 llp = lnd_insque(&land, olist);
1078 llp->mobil = mobcost;
1079 llp->x = llp->unit.land.lnd_x;
1080 llp->y = llp->unit.land.lnd_y;
1081 llp->eff = llp->unit.land.lnd_effic;
1082 if (lnd_spyval(&land) > *a_spyp)
1083 *a_spyp = lnd_spyval(&land);
1084 if (lchr[land.lnd_type].l_flags & L_ENGINEER)
1086 if (def->type == EF_SHIP && ++count >= maxland)
1091 /* What's the offense or defense multiplier? */
1094 att_combat_eff(struct combat *com)
1100 if (com->type == EF_SECTOR) {
1101 eff = com->eff / 100.0;
1102 if (com->own == player->cnum) {
1103 str = com->sct_dcp->d_ostr;
1104 eff = 1.0 + (str - 1.0) * eff;
1106 eff = sector_strength(getsectp(com->x, com->y));
1107 } else if (com->type == EF_SHIP && com->own != player->cnum) {
1108 getship(com->shp_uid, &ship);
1109 eff = 1.0 + shp_armor(&ship) / 100.0;
1115 att_get_offense(int combat_mode, struct combat *off,
1116 struct emp_qelem *olist, struct combat *def)
1121 * Get the attacker units & mil again in case they changed while the
1122 * attacker was answering sub-prompts.
1125 ototal = get_ototal(combat_mode, off, olist, 1.0, 1);
1126 if (att_empty_attack(combat_mode, ototal, def))
1127 return abort_attack();
1128 if (combat_mode == A_PARA)
1130 pr("\n Initial attack strength: %8d\n", ototal);
1134 /* Get the defensive units and reacting units */
1136 att_get_defense(struct emp_qelem *olist, struct combat *def,
1137 struct emp_qelem *dlist, int a_spy, int ototal)
1140 struct emp_qelem *qp;
1146 get_dlist(def, dlist, 0, &d_spy);
1147 dtotal = get_dtotal(def, dlist, 1.0, 0);
1150 * Call in reacting units
1153 if (def->type == EF_SECTOR && def->sct_type != SCT_MOUNT)
1154 att_reacting_units(def, dlist, a_spy, &d_spy, ototal);
1156 for (qp = olist->q_forw; qp != olist; qp = qp->q_forw) {
1157 llp = (struct ulist *)qp;
1158 intelligence_report(def->own, &llp->unit.land, d_spy,
1159 "Scouts report attacking unit:");
1162 old_dtotal = dtotal;
1163 dtotal = get_dtotal(def, dlist, 1.0, 0);
1164 if (dtotal != old_dtotal)
1165 pr("Defense strength with reacting units: %8d\n", dtotal);
1170 /* Get the defensive land units in the sector or on the ship */
1173 get_dlist(struct combat *def, struct emp_qelem *list, int a_spy,
1176 struct nstr_item ni;
1180 /* In here is where you need to take out spies and trains from the defending
1181 lists. Spies try to hide, trains get trapped and can be boarded. */
1183 snxtitem_xy(&ni, EF_LAND, def->x, def->y);
1184 while (nxtitem(&ni, &land)) {
1187 if (land.lnd_own != def->own)
1189 if (def->type == EF_SECTOR && land.lnd_ship >= 0)
1191 if (def->type == EF_SECTOR && land.lnd_land >= 0)
1193 if (def->type == EF_SHIP && land.lnd_ship != def->shp_uid)
1195 if (def->type == EF_LAND && land.lnd_land != def->lnd_uid)
1197 intelligence_report(player->cnum, &land, a_spy,
1198 "Scouts report defending unit:");
1199 llp = lnd_insque(&land, list);
1200 llp->supplied = lnd_supply_all(&land);
1202 llp->x = llp->unit.land.lnd_x;
1203 llp->y = llp->unit.land.lnd_y;
1204 llp->eff = llp->unit.land.lnd_effic;
1205 if (lnd_spyval(&land) > *d_spyp)
1206 *d_spyp = lnd_spyval(&land);
1210 /* Calculate the total offensive strength */
1213 get_ototal(int combat_mode, struct combat *off, struct emp_qelem *olist,
1214 double osupport, int check)
1216 double ototal = 0.0;
1217 struct emp_qelem *qp, *next;
1222 * first, total the attacking mil
1225 for (n = 0; n <= off->last; ++n) {
1226 if (off[n].type == EF_BAD || (check &&
1227 att_get_combat(&off[n], 0) <= 0))
1229 ototal += off[n].troops * att_combat_eff(off + n);
1233 * next, add in the attack_values of all
1234 * the attacking units
1237 for (qp = olist->q_forw; qp != olist; qp = next) {
1239 llp = (struct ulist *)qp;
1240 if (check && !get_oland(combat_mode, llp))
1242 if (combat_mode == A_ATTACK) {
1244 for (n = 0; n <= off->last; ++n) {
1245 if (off[n].type == EF_BAD)
1247 if ((off[n].x == llp->unit.land.lnd_x) &&
1248 (off[n].y == llp->unit.land.lnd_y))
1252 lnd_print(player->cnum, llp,
1253 "can't attack from this sector now");
1257 ototal += attack_val(combat_mode, &llp->unit.land) *
1258 att_combat_eff(off + w);
1260 ototal += attack_val(combat_mode, &llp->unit.land);
1265 return ldround(ototal, 1);
1268 /* Calculate the total defensive strength */
1271 get_dtotal(struct combat *def, struct emp_qelem *list, double dsupport,
1274 double dtotal = 0.0, eff = 1.0, d_unit;
1275 struct emp_qelem *qp, *next;
1278 if (check && att_get_combat(def, 1) < 0)
1280 eff = att_combat_eff(def);
1281 dtotal = def->troops * eff;
1284 * next, add in the defense_values of all
1285 * the defending non-retreating units
1288 for (qp = list->q_forw; qp != list; qp = next) {
1290 llp = (struct ulist *)qp;
1291 if (check && !get_dland(def, llp))
1293 d_unit = defense_val(&llp->unit.land);
1296 dtotal += d_unit * eff;
1301 return ldround(dtotal, 1);
1305 * This is the land unit integrity check.
1309 get_oland(int combat_mode, struct ulist *llp)
1311 struct lndstr *lp = &llp->unit.land;
1314 getland(llp->unit.land.lnd_uid, lp);
1316 if (lp->lnd_own != player->cnum) {
1317 sprintf(buf, "was destroyed and is no longer a part of the %s",
1318 att_mode[combat_mode]);
1319 lnd_print(player->cnum, llp, buf);
1323 if (lp->lnd_x != llp->x || lp->lnd_y != llp->y) {
1325 "left to fight another battle and is no longer a part of the %s",
1326 att_mode[combat_mode]);
1327 lnd_print(player->cnum, llp, buf);
1331 if (lp->lnd_effic < llp->eff) {
1332 sprintf(buf, "damaged from %d%% to %d%%",
1333 llp->eff, lp->lnd_effic);
1334 lnd_print(player->cnum, llp, buf);
1337 llp->eff = llp->unit.land.lnd_effic;
1342 get_dland(struct combat *def, struct ulist *llp)
1344 struct lndstr *lp = &llp->unit.land;
1347 getland(llp->unit.land.lnd_uid, lp);
1349 if (lp->lnd_effic < LAND_MINEFF) {
1350 sprintf(buf, "was destroyed and is no longer a part of the defense");
1351 lnd_print(llp->unit.land.lnd_own, llp, buf);
1355 if (lp->lnd_x != def->x || lp->lnd_y != def->y) {
1356 lnd_print(llp->unit.land.lnd_own, llp,
1357 "left to go fight another battle and is no longer a part of the defense");
1362 llp->eff = llp->unit.land.lnd_effic;
1367 kill_land(struct emp_qelem *list)
1369 struct emp_qelem *qp, *next;
1372 for (qp = list->q_forw; qp != list; qp = next) {
1374 llp = (struct ulist *)qp;
1375 if (llp->unit.land.lnd_ship >= 0) {
1376 llp->unit.land.lnd_effic = 0;
1377 lnd_print(player->cnum, llp,
1378 "cannot return to the ship, and dies!");
1385 att_infect_units(struct emp_qelem *list, int plague)
1387 struct emp_qelem *qp, *next;
1392 for (qp = list->q_forw; qp != list; qp = next) {
1394 llp = (struct ulist *)qp;
1395 if (llp->unit.land.lnd_pstage == PLG_HEALTHY)
1396 llp->unit.land.lnd_pstage = PLG_EXPOSED;
1401 * Put the land unit on the disk. If there was some mobility cost, then
1402 * subtract it from the units mobility. Note that this works the same way
1403 * as sectors & ships in that no mobility is actually taken until the attacker
1404 * has committed to attacking.
1408 put_oland(struct emp_qelem *list)
1410 struct emp_qelem *qp, *next;
1413 for (qp = list->q_forw; qp != list; qp = next) {
1415 llp = (struct ulist *)qp;
1416 llp->unit.land.lnd_mission = 0;
1417 llp->unit.land.lnd_harden = 0;
1418 llp->unit.land.lnd_mobil -= (int)llp->mobil;
1420 putland(llp->unit.land.lnd_uid, &llp->unit.land);
1421 if (llp->unit.land.lnd_own != player->cnum) {
1422 emp_remque(&llp->queue);
1425 get_oland(A_ATTACK, llp);
1430 * Keep sending in reinforcements until it looks like we're going to win.
1431 * Note that the "strength" command also calls this routine.
1435 att_reacting_units(struct combat *def, struct emp_qelem *list, int a_spy,
1436 int *d_spyp, int ototal)
1438 struct nstr_item ni;
1442 double new_land = 0;
1446 double eff = att_combat_eff(def);
1449 dtotal = get_dtotal(def, list, 1.0, 1);
1452 snxtitem_all(&ni, EF_LAND);
1453 while (nxtitem(&ni, &land) && dtotal + new_land * eff < 1.2 * ototal) {
1456 if (land.lnd_mission != MI_RESERVE)
1458 if ((land.lnd_x == def->x) && (land.lnd_y == def->y))
1460 if (land.lnd_own != def->own)
1462 if (land.lnd_ship >= 0)
1464 if (land.lnd_land >= 0)
1466 if (defense_val(&land) < 1.0)
1468 if (!lnd_can_attack(&land))
1471 /* Only supplied units can react */
1472 if (list ? !lnd_supply_all(&land) : !lnd_could_be_supplied(&land))
1475 if (!in_oparea((struct empobj *)&land, def->x, def->y))
1478 pathcost = path_find(land.lnd_x, land.lnd_y, def->x, def->y,
1479 def->own, lnd_mobtype(&land));
1482 mobcost = lnd_pathcost(&land, pathcost);
1483 if (land.lnd_mobil < mobcost)
1486 new_land += defense_val(&land);
1488 if (!list) /* we are in the "strength" command */
1491 /* move to defending sector */
1492 land.lnd_mobil -= ldround(mobcost, 1);
1495 land.lnd_x = def->x;
1496 land.lnd_y = def->y;
1497 putland(land.lnd_uid, &land);
1498 wu(0, land.lnd_own, "%s reacts to %s.\n",
1499 prland(&land), xyas(land.lnd_x, land.lnd_y, land.lnd_own));
1501 llp = lnd_insque(&land, list);
1506 llp->eff = land.lnd_effic;
1507 if (lnd_spyval(&land) > *d_spyp)
1508 *d_spyp = lnd_spyval(&land);
1510 intelligence_report(player->cnum, &land, a_spy,
1511 "Scouts sight reacting enemy unit:");
1516 /* Pop off shells and fly bombing missions to get your attack multiplier up */
1519 get_osupport(char *outs, struct combat *def, int fort_sup, int ship_sup,
1520 int land_sup, int plane_sup)
1522 double osupport = 1.0;
1524 double af, as, au, ap;
1526 af = as = au = ap = 0.0;
1528 dam = dd(def->own, player->cnum, def->x, def->y, 0, 0);
1533 dam = sd(def->own, player->cnum, def->x, def->y, 0, 0, 0);
1540 dam = lnd_support(def->own, player->cnum, def->x, def->y, 0);
1546 dam = off_support(def->x, def->y, def->own, player->cnum);
1550 sprintf(outs, "attacker\t%1.2f\t%1.2f\t%1.2f\t%1.2f", af, as, au, ap);
1554 /* Pop off shells and fly bombing missions to get your defense multiplier up */
1557 get_dsupport(char *outs, struct emp_qelem *list, struct combat *def,
1558 int ototal, int dtotal)
1560 double dsupport = 1.0;
1562 double df, ds, du, dp;
1565 df = ds = du = dp = 0.0;
1566 if (dtotal < 0.1 * ototal) {
1568 } else if (dtotal >= 1.2 * ototal) {
1571 dam = dd(player->cnum, def->own, def->x, def->y, 0, 1);
1575 dtotal = get_dtotal(def, list, dsupport, 0);
1576 if (dtotal < 1.2 * ototal) {
1577 dam = sd(player->cnum, def->own, def->x, def->y, 0, 1, 0);
1580 dtotal = get_dtotal(def, list, dsupport, 0);
1582 if (dtotal < 1.2 * ototal) {
1583 dam = lnd_support(player->cnum, def->own, def->x, def->y, 1);
1586 dtotal = get_dtotal(def, list, dsupport, 1);
1588 if (dtotal < 1.2 * ototal) {
1589 dam = def_support(def->x, def->y, player->cnum, def->own);
1597 sprintf(outs, "defender\t%1.2f\t%1.2f\t%1.2f\t%1.2f",
1602 "\nOdds are bad for us...support cancelled.\n\n");
1605 "\nOdds are good for us...support cancelled.\n\n");
1611 * Land mines add to the defense multiplier. If the attacker has engineers
1612 * then this multiplier is cut in half.
1616 get_mine_dsupport(struct combat *def, int a_engineer)
1621 getsect(def->x, def->y, §);
1623 if (sect.sct_oldown != player->cnum) {
1624 mines = SCT_LANDMINES(§);
1625 mines = MIN(mines, 20);
1627 mines = ldround(mines / 2.0, 1);
1630 wu(0, def->own, "Defending mines add %1.2f\n",
1632 pr("Defending mines add %1.2f\n", mines * 0.02);
1633 return mines * 0.02;
1639 /* Get the offensive and defensive support */
1641 att_get_support(int combat_mode, int ofort, int oship, int oland,
1642 int oplane, struct emp_qelem *olist, struct combat *off,
1643 struct emp_qelem *dlist, struct combat *def,
1644 double *osupportp, double *dsupportp, int a_engineer)
1647 char osupports[512];
1648 char dsupports[512];
1650 if (combat_mode == A_PARA)
1653 *osupportp = get_osupport(osupports, def,
1654 ofort, oship, oland, oplane);
1657 * I need to put a 1 at the end of the next four total_stren calls
1658 * because units & mil may have been damaged by collateral damage or
1659 * nuclear warheads from the offensive & defensive support.
1662 ototal = get_ototal(combat_mode, off, olist, *osupportp, 1);
1663 if (att_empty_attack(combat_mode, ototal, def))
1664 return abort_attack();
1665 dtotal = get_dtotal(def, dlist, *dsupportp, 1);
1668 * Calculate defensive support. If odds are too good or too bad
1669 * then don't call in support.
1672 *dsupportp = get_dsupport(dsupports, dlist, def, ototal, dtotal);
1673 ototal = get_ototal(combat_mode, off, olist, *osupportp, 1);
1674 if (att_empty_attack(combat_mode, ototal, def))
1675 return abort_attack();
1677 if ((*osupports || *dsupports) &&
1678 (*osupportp != 1.0 || *dsupportp != 1.0)) {
1679 pr("\n\t\tsupport values\n");
1680 pr("\t\tforts\tships\tunits\tplanes\n");
1681 if (*osupportp != 1.0)
1682 pr("%s\n", osupports);
1683 if (*dsupportp != 1.0)
1684 pr("%s\n", dsupports);
1687 wu(0, def->own, "\n\t\tsupport values\n");
1688 wu(0, def->own, "\t\tforts\tships\tunits\tplanes\n");
1689 if (*osupportp != 1.0)
1690 wu(0, def->own, "%s\n", osupports);
1691 if (*dsupportp != 1.0)
1692 wu(0, def->own, "%s\n", dsupports);
1693 wu(0, def->own, "\n");
1697 dtotal = get_dtotal(def, dlist, *dsupportp, 1);
1698 if (dtotal && def->type == EF_SECTOR)
1699 *dsupportp += get_mine_dsupport(def, a_engineer);
1703 /* How many two-legged bipeds are in this combat force? */
1706 count_bodies(struct combat *off, struct emp_qelem *list)
1710 struct emp_qelem *qp;
1713 for (n = 0; n <= off->last; ++n)
1714 bodies += off[n].troops;
1715 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
1716 llp = (struct ulist *)qp;
1717 bodies += llp->unit.land.lnd_item[I_MILIT];
1722 /* This is where the fighting actually occurs. */
1725 att_fight(int combat_mode, struct combat *off, struct emp_qelem *olist,
1726 double osupport, struct combat *def, struct emp_qelem *dlist,
1730 int a_cas = 0; /* Casualty counts */
1732 int ototal; /* total attacking strength */
1733 int dtotal; /* total defending strength */
1734 int a_bodies; /* total attacking mil (incl. mil in units) */
1735 int d_bodies; /* total defending mil (incl. mil in units) */
1745 ototal = get_ototal(combat_mode, off, olist, osupport,
1746 combat_mode != A_PARA);
1747 dtotal = get_dtotal(def, dlist, dsupport, 0);
1751 a_bodies = count_bodies(off, olist);
1752 d_bodies = count_bodies(def, dlist);
1753 d_mil = def->troops;
1754 for (n = 0; n <= off->last; ++n)
1755 if (off[n].type == EF_BAD)
1758 a_troops[n] = off[n].troops;
1760 /* This switch is required to get the spacing right */
1761 switch (combat_mode) {
1763 pr(" Final attack strength: %8d\n", ototal);
1766 pr(" Final assault strength: %8d\n", ototal);
1769 if (def->sct_type == SCT_MOUNT ||
1770 def->sct_type == SCT_WATER ||
1771 def->sct_type == SCT_CAPIT ||
1772 def->sct_type == SCT_FORTR || def->sct_type == SCT_WASTE) {
1773 pr("You can't air-assault a %s sector!\n",
1774 def->sct_dcp->d_name);
1777 ototal = get_ototal(A_PARA, off, olist, osupport, 0);
1779 pr(" Final air-assault strength: %8d\n", ototal);
1783 pr(" Final board strength: %8d\n", ototal);
1787 pr(" Final defense strength: %8d\n", dtotal);
1788 odds = att_calcodds(ototal, dtotal);
1789 pr(" Final odds: %8d%%\n", (int)(odds * 100));
1791 /* spread the plague */
1792 if (combat_mode != A_PARA) {
1794 for (n = 0; n <= off->last; ++n)
1795 if (off[n].type != EF_BAD)
1796 def->plague |= off[n].plague;
1797 for (n = 0; n <= off->last; ++n)
1798 if (off[n].type != EF_BAD)
1799 off[n].plague |= def->plague;
1801 att_infect_units(olist, off->plague);
1802 att_infect_units(dlist, def->plague);
1805 * Fighting is slightly random. There is always that last little
1806 * effort you see people put in. Or the stray bullet that takes out
1807 * an officer and the rest go into chaos. Things like that.
1808 * Thus, we have added a very slight random factor that will sometimes
1809 * allow the little guy to win. We modify the odds a little
1810 * (either +- 5%) to account for this randomness. We also only
1811 * recalculate the odds every 8-50 casualties, not every cacsualty,
1812 * since a single dead guy normally wouldn't cause a commander to
1813 * rethink his strategies, but 50 dead guys might.
1815 odds += (roll(11) - 6) / 100.0;
1820 recalctime = 7 + roll(43);
1821 while (!success && ototal) {
1824 d_cas += take_casualty(A_DEFEND, def, dlist);
1825 dtotal = get_dtotal(def, dlist, dsupport, 0);
1830 a_cas += take_casualty(combat_mode, off, olist);
1831 ototal = get_ototal(combat_mode, off, olist, osupport, 0);
1833 if (((a_cas + d_cas) % 70) == 69)
1835 if (recalctime-- <= 0) {
1836 recalctime = 7 + roll(43);
1837 odds = att_calcodds(ototal, dtotal);
1838 odds += (roll(11) - 6) / 100.0;
1846 /* update defense mobility & mil */
1850 if (def->type == EF_SECTOR && d_mil && d_cas) {
1854 newmob = damage(def->mob, 100 * d_cas / d_mil);
1855 def->mobcost = MIN(20, def->mob - newmob);
1858 def->mil = def->troops;
1861 /* update attack mobility & mil */
1862 for (n = 0; n <= off->last; ++n)
1863 if (off[n].type != EF_BAD && off[n].troops < a_troops[n]) {
1864 if (off[n].type == EF_SECTOR && off[n].mil) {
1865 if (!CANT_HAPPEN(off[n].mob < 0)) {
1866 newmob = damage(off[n].mob,
1867 100 * (a_troops[n] - off[n].troops)
1869 off[n].mobcost += MIN(20, off[n].mob - newmob);
1872 off[n].mil -= a_troops[n] - off[n].troops;
1875 /* update land unit mobility */
1876 if (d_bodies && d_cas)
1877 lnd_takemob(dlist, (double)d_cas / d_bodies);
1878 if (a_bodies && a_cas)
1879 lnd_takemob(olist, (double)a_cas / a_bodies);
1881 /* damage attacked sector */
1882 def->eff = effdamage(def->eff, (d_cas + a_cas) / 10);
1884 pr("- Casualties -\n Yours: %d\n", a_cas);
1885 pr(" Theirs: %d\n", d_cas);
1886 pr("Papershuffling ... %.1f B.T.U\n", (d_cas + a_cas) * 0.15);
1887 player->btused += (int)((d_cas + a_cas) * 0.015 + 0.5);
1890 switch (combat_mode) {
1892 news_item = def->own ? N_WON_SECT : N_TOOK_UNOCC;
1893 pr("We have captured %s, sir!\n", prcom(0, def));
1897 news_item = def->own ? N_AWON_SECT : N_START_COL;
1898 pr("We have secured a beachhead at %s, sir!\n", prcom(0, def));
1899 action = "assaulting and taking";
1902 news_item = def->own ? N_PWON_SECT : N_PARA_UNOCC;
1903 pr("We have captured %s, sir!\n", prcom(0, def));
1904 action = "air-assaulting and taking";
1907 news_item = N_BOARD_SHIP;
1908 pr("We have boarded %s, sir!\n", prcom(0, def));
1909 action = "boarding";
1912 news_item = N_BOARD_LAND;
1913 pr("We have boarded %s, sir!\n", prcom(0, def));
1914 action = "boarding";
1919 action = "defeating";
1922 switch (combat_mode) {
1924 news_item = N_SCT_LOSE;
1925 pr("You have been defeated!\n");
1926 action = "attacking";
1929 news_item = N_ALOSE_SCT;
1930 pr("You have been defeated!\n");
1932 action = "trying to assault";
1935 news_item = N_PLOSE_SCT;
1936 pr("All of your troops were destroyed\n");
1937 action = "trying to air-assault";
1940 news_item = N_SHP_LOSE;
1941 pr("You have been repelled\n");
1943 action = "trying to board";
1946 news_item = N_LND_LOSE;
1947 pr("You have been repelled\n");
1949 action = "trying to board";
1954 action = "fighting";
1957 nreport(player->cnum, news_item, def->own, 1);
1960 "%s lost %d troops %s %s\nWe lost %d troops defending\n",
1961 prnatid(player->cnum), a_cas,
1962 action, pr_com(0, def, def->own), d_cas);
1965 send_reacting_units_home(dlist);
1967 /* putland the defending land */
1970 /* putland the attacking land */
1973 /* put the victim sector/ship/land */
1974 if (!success || !take_def(combat_mode, olist, off, def))
1977 /* put the attacking sectors/ship */
1978 for (n = 0; n <= off->last; ++n)
1979 if (off[n].type != EF_BAD)
1980 put_combat(&off[n]);
1985 switch (combat_mode) {
1987 ask_move_in(off, olist, def);
1989 /* put sectors again to get abandon warnings */
1990 for (n = 0; n <= off->last; ++n)
1991 if (off[n].type != EF_BAD)
1992 put_combat(&off[n]);
1995 att_move_in_off(combat_mode, off, olist, def);
1998 pr("%d of your troops now occupy %s\n", def->mil, prcom(0, def));
2002 /* What percentage of the combat forces going head-to-head are we? */
2005 att_calcodds(int ototal, int dtotal)
2009 /* calculate odds */
2012 else if (dtotal <= 0)
2015 odds = (double)ototal / (dtotal + ototal);
2020 /* Here's where the dead soldiers get dragged off the battlefield */
2023 take_casualty(int combat_mode, struct combat *off, struct emp_qelem *olist)
2025 int to_take = CASUALTY_LUMP;
2026 int biggest_troops = 0, index = -1;
2027 int n, tot_troops = 0, biggest_mil, cas;
2028 struct emp_qelem *qp;
2029 struct ulist *llp, *biggest;
2031 for (n = 0; n <= off->last; ++n) {
2032 if (off[n].type != EF_BAD) {
2033 tot_troops += off[n].troops;
2034 if (off[n].troops > biggest_troops) {
2035 biggest_troops = off[n].troops;
2042 to_take -= tot_troops;
2045 for (n = 0; n <= off->last; ++n)
2046 if (off[n].type != EF_BAD)
2050 * They can all come off mil. We rotate the casualties,
2051 * starting with the sector containing the most mil.
2053 to_take = CASUALTY_LUMP;
2055 pr("ERROR: Tell the deity that you got the 'green librarian' error\n");
2058 while (to_take > 0) {
2059 for (n = index; n <= off->last && to_take; ++n) {
2060 if (off[n].type != EF_BAD && off[n].troops > 0) {
2065 for (n = 0; n < index && to_take; ++n) {
2066 if (off[n].type != EF_BAD && off[n].troops > 0) {
2072 return CASUALTY_LUMP;
2076 return CASUALTY_LUMP - to_take;
2079 * Need to take some casualties from attacking units
2080 * Procedure: find the biggest unit remaining (in
2081 * terms of mil) and give it the casualties.
2085 for (qp = olist->q_forw; qp != olist; qp = qp->q_forw) {
2086 llp = (struct ulist *)qp;
2088 if (llp->unit.land.lnd_item[I_MILIT] > biggest_mil) {
2089 biggest_mil = llp->unit.land.lnd_item[I_MILIT];
2093 if (biggest == NULL)
2094 return CASUALTY_LUMP - to_take;
2096 cas = lnd_take_casualty(combat_mode, biggest, to_take);
2097 return CASUALTY_LUMP - (to_take - cas);
2100 /* Send reacting defense units back to where they came from (at no mob cost) */
2103 send_reacting_units_home(struct emp_qelem *list)
2105 struct emp_qelem *qp, *next;
2109 for (qp = list->q_forw; qp != list; qp = next) {
2111 llp = (struct ulist *)qp;
2112 if ((llp->unit.land.lnd_x != llp->x) ||
2113 (llp->unit.land.lnd_y != llp->y)) {
2114 sprintf(buf, "returns to %s",
2115 xyas(llp->x, llp->y, llp->unit.land.lnd_own));
2116 llp->unit.land.lnd_x = llp->x;
2117 llp->unit.land.lnd_y = llp->y;
2118 lnd_print(llp->unit.land.lnd_own, llp, buf);
2124 /* Check for 0 offense strength. This call will always preceed an abort */
2127 att_empty_attack(int combat_mode, int ototal, struct combat *def)
2130 if (def->own && player->cnum != def->own) {
2132 "%s considered %sing you @%s\n",
2133 prnatid(player->cnum),
2134 att_mode[combat_mode], xyas(def->x, def->y, def->own));
2136 pr("No troops for %s...\n", att_mode[combat_mode]);
2143 * Take the defending sector or ship from the defender and give it to the
2148 take_def(int combat_mode, struct emp_qelem *list, struct combat *off,
2153 struct ulist *llp, *delete_me = NULL;
2159 for (n = 0; n <= off->last && !occuppied; ++n) {
2160 if (off[n].type != EF_BAD &&
2161 off[n].troops > 0 &&
2162 (off[n].type != EF_SECTOR || off[n].mob)) {
2164 if (def->type == EF_LAND) {
2165 if (def->lnd_lcp->l_flags & L_SPY) {
2172 pr("1 mil from %s moves %s\n",
2173 prcom(0, off + n), prcom(2, def));
2178 pr("%s left unoccupied\n", prcom(0, def));
2181 "No enemy troops moved %s so you still own it!\n",
2182 pr_com(2, def, def->own));
2185 llp = (struct ulist *)list->q_forw;
2186 llp->unit.land.lnd_x = def->x;
2187 llp->unit.land.lnd_y = def->y;
2188 take_move_in_mob(combat_mode, llp, off, def);
2189 if (def->type == EF_SHIP) {
2190 llp->unit.land.lnd_ship = def->shp_uid;
2191 sprintf(buf, "boards %s", prcom(0, def));
2192 lnd_print(player->cnum, llp, buf);
2195 llp->unit.land.lnd_ship = -1;
2196 sprintf(buf, "moves in to occupy %s",
2197 xyas(def->x, def->y, player->cnum));
2198 lnd_print(player->cnum, llp, buf);
2204 if (def->type == EF_SECTOR) {
2205 getsect(def->x, def->y, §);
2206 takeover(§, player->cnum);
2207 if (sect.sct_type == SCT_CAPIT || sect.sct_type == SCT_MOUNT)
2208 caploss(§, def->own,
2209 "* We have captured %s's capital, sir! *\n");
2211 } else if (def->type == EF_SHIP) {
2212 getship(def->shp_uid, &ship);
2213 takeover_ship(&ship, player->cnum);
2214 putship(ship.shp_uid, &ship);
2215 } else if (def->type == EF_LAND) {
2216 getland(def->lnd_uid, &land);
2217 takeover_land(&land, player->cnum);
2218 putland(land.lnd_uid, &land);
2221 lnd_put_one(delete_me);
2222 att_get_combat(def, 0);
2227 * Ask the attacker which mil & land units they'd like to move into the
2232 ask_move_in(struct combat *off, struct emp_qelem *olist,
2236 struct emp_qelem *qp, *next;
2240 char land_answer[256];
2243 for (n = 0; n <= off->last; ++n)
2244 if (off[n].type != EF_BAD && off[n].troops > 0)
2246 ask_move_in_off(&off[n], def);
2247 if (player->aborted)
2253 memset(land_answer, 0, sizeof(land_answer));
2254 for (qp = olist->q_forw; qp != olist; qp = next) {
2256 llp = (struct ulist *)qp;
2257 answerp = &land_answer[(int)llp->unit.land.lnd_army];
2258 if (player->aborted || att_get_combat(def, 0) < 0)
2260 if (*answerp == 'Y')
2262 if (!get_oland(A_ATTACK, llp))
2264 if (*answerp != 'N') {
2265 sprintf(prompt, "Move in with %s (%c %d%%) [ynYNq?] ",
2266 prland(&llp->unit.land),
2267 llp->unit.land.lnd_army ? llp->unit.land.lnd_army : '~',
2268 llp->unit.land.lnd_effic);
2269 *answerp = att_prompt(prompt, llp->unit.land.lnd_army);
2270 if (player->aborted || att_get_combat(def, 0) < 0)
2272 if (!get_oland(A_ATTACK, llp))
2275 if (*answerp == 'y' || *answerp == 'Y')
2277 sprintf(buf, "stays in %s",
2278 xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y,
2280 lnd_print(player->cnum, llp, buf);
2285 if (att_get_combat(def, 0) < 0) {
2286 for (qp = olist->q_forw; qp != olist; qp = next) {
2288 llp = (struct ulist *)qp;
2289 if (!get_oland(A_ATTACK, llp))
2291 sprintf(buf, "stays in %s",
2292 xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y,
2294 lnd_print(player->cnum, llp, buf);
2299 if (opt_INTERDICT_ATT)
2300 lnd_interdict(olist, def->x, def->y, player->cnum);
2301 move_in_land(A_ATTACK, off, olist, def);
2304 /* Move offensive land units to the conquered sector or ship */
2307 move_in_land(int combat_mode, struct combat *off, struct emp_qelem *olist,
2310 struct emp_qelem *qp, *next;
2316 for (qp = olist->q_forw; qp != olist; qp = next) {
2318 llp = (struct ulist *)qp;
2319 if (!get_oland(combat_mode, llp))
2321 take_move_in_mob(combat_mode, llp, off, def);
2322 llp->unit.land.lnd_x = def->x;
2323 llp->unit.land.lnd_y = def->y;
2324 if (def->type == EF_SHIP)
2325 llp->unit.land.lnd_ship = def->shp_uid;
2327 llp->unit.land.lnd_ship = -1;
2331 if (def->type == EF_SECTOR) {
2332 if (opt_INTERDICT_ATT) {
2333 lnd_sweep(olist, 0, 0, player->cnum);
2334 lnd_check_mines(olist);
2336 sprintf(buf, "now occupies %s", prcom(0, def));
2338 sprintf(buf, "boards %s", prcom(0, def));
2342 for (qp = olist->q_forw; qp != olist; qp = next) {
2344 llp = (struct ulist *)qp;
2345 lnd_print(player->cnum, llp, buf);
2353 * Move assaulting, paradropping, or boarding mil & units into def
2354 * If the mil are coming from a ship, then pack a lunch.
2358 att_move_in_off(int combat_mode, struct combat *off,
2359 struct emp_qelem *olist, struct combat *def)
2366 move_in_land(combat_mode, off, olist, def);
2368 for (n = 0; n <= off->last; ++n) {
2369 if (off[n].type == EF_BAD || !off[n].troops)
2371 troops = off[n].troops;
2373 off[n].mil -= troops;
2375 put_combat(off + n);
2376 if (combat_mode == A_ASSAULT) {
2377 if (CANT_HAPPEN(off[n].type != EF_SHIP))
2379 getship(off[n].shp_uid, &ship);
2380 lunchbox += (int)((troops + 1) * ship.shp_item[I_FOOD]
2381 / (ship.shp_item[I_MILIT] + troops
2382 + ship.shp_item[I_CIVIL] + 0.5));
2384 ship.shp_item[I_FOOD] -= lunchbox;
2385 putship(ship.shp_uid, &ship);
2391 if (combat_mode == A_ASSAULT) {
2392 if (CANT_HAPPEN(def->type != EF_SECTOR))
2394 getsect(def->x, def->y, §);
2395 if (lunchbox > ITEM_MAX - sect.sct_item[I_FOOD])
2396 lunchbox = ITEM_MAX - sect.sct_item[I_FOOD];
2397 sect.sct_item[I_FOOD] += lunchbox;
2403 /* Ask how many mil to move in from each sector */
2406 ask_move_in_off(struct combat *off, struct combat *def)
2409 int num_mil, dam = 0, left;
2415 if (att_get_combat(off, 0) <= 0)
2417 if (att_get_combat(def, 0) < 0)
2419 if (off->own != player->cnum)
2421 d = att_mobcost(off->own, def, MOB_MOVE);
2422 if ((mob_support = MIN(off->troops, (int)(off->mob / d))) <= 0)
2424 sprintf(prompt, "How many mil to move in from %s (%d max)? ",
2425 xyas(off->x, off->y, player->cnum), mob_support);
2426 p = getstring(prompt, buf);
2427 if (!p || !*p || (num_mil = atoi(p)) <= 0)
2429 /* Make sure we don't move in more than we can support mobility-wise */
2430 if (num_mil > mob_support)
2431 num_mil = mob_support;
2432 if (att_get_combat(off, 0) <= 0)
2434 if (att_get_combat(def, 0) < 0)
2436 if ((num_mil = MIN(off->troops, num_mil)) <= 0) {
2437 pr("No mil moved in from %s\n",
2438 xyas(off->x, off->y, player->cnum));
2441 mob_support = MAX(1, (int)(num_mil * d));
2442 off->mob -= MIN(off->mob, mob_support);
2443 off->mil -= num_mil;
2444 off->troops -= num_mil;
2447 weight = (double)num_mil * ichr[I_MILIT].i_lbs;
2448 if (opt_INTERDICT_ATT && chance(weight / 200.0)) {
2449 if (chance(weight / 100.0))
2451 ground_interdict(def->x, def->y, player->cnum, "military");
2452 dam += check_lmines(def->x, def->y, weight);
2456 left = commdamage(num_mil, dam, I_MILIT);
2457 if (left < num_mil) {
2459 pr("%d of the mil you were moving were destroyed!\n"
2460 "Only %d mil made it to %s\n",
2461 num_mil - left, left,
2462 xyas(def->x, def->y, player->cnum));
2464 pr("All of the mil you were moving were destroyed!\n");
2467 /* maybe got nuked */
2468 if (att_get_combat(def, 0) < 0)
2476 /* Charge land units for moving into a sector or onto a ship */
2479 take_move_in_mob(int combat_mode, struct ulist *llp, struct combat *off,
2482 double mob = llp->unit.land.lnd_mobil;
2483 double gain = etu_per_update * land_mob_scale;
2484 double mobcost, moblim;
2486 switch (combat_mode) {
2488 mobcost = lnd_pathcost(&llp->unit.land,
2489 att_mobcost(llp->unit.land.lnd_own, def,
2490 lnd_mobtype(&llp->unit.land)));
2494 * Set mobcost to basic assault cost, moblim to maximum
2495 * mobility to keep when assaulting from non-landing ship
2497 if (lchr[llp->unit.land.lnd_type].l_flags & L_MARINE) {
2498 mobcost = gain / 2.0;
2504 if (!(off->shp_mcp->m_flags & M_LAND))
2505 /* Not a landing ship, ensure we go to or below moblim */
2506 mobcost = MAX(mobcost, mob - moblim);
2509 if (lchr[llp->unit.land.lnd_type].l_flags & L_MARINE)
2522 llp->unit.land.lnd_mobil = (signed char)mob;
2523 llp->unit.land.lnd_harden = 0;
2527 free_list(struct emp_qelem *list)
2529 struct emp_qelem *qp, *next;
2531 if (!list || QEMPTY(list))
2535 while (qp != list) {
2544 att_free_lists(struct emp_qelem *olist, struct emp_qelem *dlist)
2552 * sector_strength - Everyone starts at 1. You can get up to a max
2553 * of d_dstr, depending on how much you build up the
2554 * defenses of the sector.
2558 sector_strength(struct sctstr *sp)
2560 double def = SCT_DEFENSE(sp) / 100.0;
2561 double base = sp->sct_type == SCT_MOUNT ? 2.0 : 1.0;
2562 double d = base + (dchr[sp->sct_type].d_dstr - base) * def;
2564 return LIMIT_TO(d, base, dchr[sp->sct_type].d_dstr);