]> git.pond.sub.org Git - empserver/blob - src/lib/subs/attsub.c
63aeff6f2ababff266bbce8f927ef704dd3ac0e7
[empserver] / src / lib / subs / attsub.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2012, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
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.
10  *
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.
15  *
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/>.
18  *
19  *  ---
20  *
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.
24  *
25  *  ---
26  *
27  *  attsub.c: Attack subroutines
28  *
29  *  Known contributors to this file:
30  *     Ken Stevens, 1995
31  *     Steve McClure, 1996-2000
32  *     Markus Armbruster, 2006-2011
33  */
34
35 #include <config.h>
36
37 #include <ctype.h>
38 #include <math.h>
39 #include "combat.h"
40 #include "empobj.h"
41 #include "file.h"
42 #include "map.h"
43 #include "misc.h"
44 #include "mission.h"
45 #include "news.h"
46 #include "nsc.h"
47 #include "optlist.h"
48 #include "path.h"
49 #include "plague.h"
50 #include "player.h"
51 #include "prototypes.h"
52 #include "unit.h"
53 #include "xy.h"
54
55 #define CASUALTY_LUMP   1       /* How big casualty chunks should be */
56
57 static void ask_olist(int combat_mode, struct combat *off,
58                       struct combat *def, struct emp_qelem *olist,
59                       char *land_answer, int *a_spyp, int *a_engineerp);
60 static void take_move_in_mob(int combat_mode, struct ulist *llp,
61                              struct combat *off, struct combat *def);
62 static void move_in_land(int combat_mode, struct combat *off,
63                          struct emp_qelem *olist, struct combat *def);
64 static void ask_move_in(struct combat *off, struct emp_qelem *olist,
65                         struct combat *def);
66 static void ask_move_in_off(struct combat *off, struct combat *def);
67
68 static int board_abort(struct combat *off, struct combat *def);
69 static int land_board_abort(struct combat *off, struct combat *def);
70 static int ask_off(int combat_mode, struct combat *off,
71                    struct combat *def);
72 static void get_dlist(struct combat *def, struct emp_qelem *, int a_spy,
73                       int *d_spyp);
74 static int get_ototal(int combat_mode, struct combat *off,
75                       struct emp_qelem *olist, double osupport, int check);
76 static int get_dtotal(struct combat *def, struct emp_qelem *list,
77                       double dsupport, int check);
78 static double att_calcodds(int, int);
79 static int take_casualty(int combat_mode, struct combat *off,
80                          struct emp_qelem *olist);
81
82 static void send_reacting_units_home(struct emp_qelem *list);
83 static int take_def(int combat_mode, struct emp_qelem *list,
84                     struct combat *off, struct combat *def);
85
86 static int get_oland(int, struct ulist *);
87 static int get_dland(struct combat *, struct ulist *);
88
89 char *att_mode[] = {
90     /* must match combat types in combat.h */
91     "defend", "attack", "assault", "paradrop", "board", "lboard"
92 };
93
94
95 /*
96  * The principal object in this code is the "combat" object.  A combat object
97  * is either a sector or ship.  There are
98  * usually two instances of this, the "def" or defense combat object, and
99  * the array of "off" or offense objects.  The number of offense objects is
100  * determined by the value of off->last (e.g. more than one attacking sector).
101  * the type of the object is determined by combat->type which can take the
102  * values EF_SECTOR, EF_SHIP, EF_PLANE, or EF_BAD.  Another important parameter
103  * which is often passed to these functions is combat_mode.  This can take
104  * the value A_DEFEND, A_ATTACK, A_ASSAULT, A_PARA, A_BOARD and A_LBOARD.
105  * As these six modes of being in combat affect things like mobcost and combat
106  * value, there are often switches made on combat_mode.  Note that in all cases
107  * no mobility is taken from sectors, ships, or land units until the player
108  * has committed to a fight.  Instead, the cost is temporarily placed in
109  * combat->mobcost, or llp->mobil as the case may be, and then when the object
110  * is "put" back onto disk, then the amounts in these variables are subtracted
111  * from the object's mobility.  It needs to be done this way as the objects
112  * are constantly being re-read from disk, and we don't want to take any mob
113  * unless a fight actually occurrs.
114  * -Ken Stevens
115  */
116
117 /* initialize combat object */
118
119 int
120 att_combat_init(struct combat *com, int type)
121 {
122     memset(com, 0, sizeof(*com));
123     com->type = type;
124     return type;
125 }
126
127 /* print a combat object with optional preposition */
128
129 static char *
130 pr_com(int inon, struct combat *com, natid who)
131 {
132     if (com->type == EF_SECTOR) {
133         return prbuf("%s%s",
134                      inon ? inon == 1 ? "in " : "into " : "",
135                      xyas(com->x, com->y, who));
136     } else if (com->type == EF_SHIP) {
137         return prbuf("%s%s %s(#%d)",
138                      inon ? inon == 1 ? "on " : "onto " : "",
139                      com->shp_mcp->m_name, com->shp_name, com->shp_uid);
140     } else if (com->type == EF_LAND) {
141         return prbuf("%s%s #%d",
142                      inon ? inon == 1 ? "on " : "onto " : "",
143                      com->lnd_lcp->l_name, com->lnd_uid);
144     } else {
145         return "your forces";
146     }
147 }
148
149 static char *
150 prcom(int inon, struct combat *com)
151 {
152     return pr_com(inon, com, player->cnum);
153 }
154
155 /*
156  * This is the combat object "type" based integrity check.  It basically
157  * splits along three divisions: ship/sector, attacker/defender,
158  * first time/not first time.
159  */
160
161 int
162 att_get_combat(struct combat *com, int isdef)
163 {
164     struct sctstr sect;
165     struct shpstr ship;
166     struct lndstr land;
167     int pstage;
168     natid owner;
169     int mil;
170     int eff;
171     int mob;
172     coord x, y;
173
174     switch (com->type) {
175     case EF_SECTOR:
176         if (!getsect(com->x, com->y, &sect)) {
177             pr("Bad sector: %s\n", xyas(com->x, com->y, player->cnum));
178             return att_combat_init(com, EF_BAD);
179         }
180         com->sct_type = sect.sct_type;
181         com->sct_dcp = &dchr[sect.sct_type];
182         mil = sect.sct_item[I_MILIT];
183         pstage = sect.sct_pstage;
184         owner = sect.sct_own;
185         eff = sect.sct_effic;
186         mob = sect.sct_mobil;
187         x = com->x;
188         y = com->y;
189         break;
190     case EF_LAND:
191         if (!getland(com->lnd_uid, &land) || !land.lnd_own) {
192             if (isdef)
193                 pr("Land unit #%d is not in the same sector!\n",
194                    com->lnd_uid);
195             return att_combat_init(com, EF_BAD);
196         }
197         if (isdef && player->owner) {
198             pr("Boarding yourself?  Try using the 'load' command.\n");
199             return att_combat_init(com, EF_BAD);
200         }
201         com->lnd_lcp = &lchr[(int)land.lnd_type];
202         mil = land.lnd_item[I_MILIT];
203         pstage = land.lnd_pstage;
204         owner = land.lnd_own;
205         eff = land.lnd_effic;
206         mob = land.lnd_mobil;
207         x = land.lnd_x;
208         y = land.lnd_y;
209         break;
210     case EF_SHIP:
211         if (!getship(com->shp_uid, &ship) || !ship.shp_own) {
212             if (isdef)
213                 pr("Ship #%d is not in the same sector!\n", com->shp_uid);
214             else
215                 pr("Ship #%d is not your ship!\n", com->shp_uid);
216             return att_combat_init(com, EF_BAD);
217         }
218         if (opt_MARKET) {
219             if (isdef && player->owner && ontradingblock(EF_SHIP, &ship)) {
220                 pr("%s is on the trading block.\n", prcom(0, com));
221                 return att_combat_init(com, EF_BAD);
222             }
223         }
224         if (isdef && player->owner) {
225             pr("Boarding yourself?  Try using the 'tend' command.\n");
226             return att_combat_init(com, EF_BAD);
227         }
228         com->shp_mcp = &mchr[(int)ship.shp_type];
229         strncpy(com->shp_name, ship.shp_name, MAXSHPNAMLEN);
230         if (!isdef && !player->owner) {
231             if (com->set)
232                 pr("%s was just sunk!\n", prcom(0, com));
233             else
234                 pr("Ship #%d is not your ship!\n", com->shp_uid);
235             return att_combat_init(com, EF_BAD);
236         }
237         mil = ship.shp_item[I_MILIT];
238         pstage = ship.shp_pstage;
239         owner = ship.shp_own;
240         eff = ship.shp_effic;
241         mob = ship.shp_mobil;
242         x = ship.shp_x;
243         y = ship.shp_y;
244         break;
245     case EF_PLANE:
246         return com->mil;
247     case EF_BAD:
248         return EF_BAD;
249     default:
250         return att_combat_init(com, EF_BAD);
251     }
252
253     if (!com->set) {            /* first time */
254         if (isdef) {            /* defender */
255             com->troops = mil;
256         } else {                /* attacker */
257             if (!mil)
258                 pr("No mil %s\n", prcom(1, com));
259             else if (mil == 1)
260                 pr("Only 1 mil %s\n", prcom(1, com));
261             /* don't abandon attacking sectors or ships */
262             com->troops = MAX(0, mil - 1);
263         }
264         com->plague = pstage == PLG_INFECT;
265     } else {                    /* not first time */
266         if (isdef) {            /* defender */
267             if (com->x != x || com->y != y) {
268                 pr("%s has moved!\n", prcom(0, com));
269                 return att_combat_init(com, EF_BAD);
270             }
271             if (owner != com->own) {
272                 if (owner) {
273                     pr("WARNING: The ownership of %s just changed from %s to %s!\n",
274                        prcom(0, com), cname(com->own), cname(owner));
275                 } else if (com->type == EF_SECTOR) {
276                     pr("WARNING: %s just abandoned sector %s!\n",
277                        cname(com->own),
278                        xyas(com->x, com->y, player->cnum));
279                 }
280             }
281             if (com->mil != mil)
282                 pr("WARNING: The enemy mil %s just %s from %d to %d!\n",
283                    prcom(1, com),
284                    com->mil < mil ? "increased" : "decreased", com->mil,
285                    mil);
286             com->troops = mil;
287         } else {                /* attacker */
288             if (owner != com->own && owner != player->cnum) {
289                 /* must be EF_SECTOR */
290                 if (com->troops)
291                     pr("WARNING: Your %d mil in %s were destroyed because %s just took the sector!\n",
292                        com->mil, xyas(com->x, com->y, player->cnum),
293                        cname(owner));
294                 else
295                     pr("%s just took %s!\n",
296                        cname(owner), xyas(com->x, com->y, player->cnum));
297                 return att_combat_init(com, EF_BAD);
298             }
299             if (com->troops && com->troops + 1 > mil) {
300                 if (com->own == owner && player->cnum == owner)
301                     /* not a takeover */
302                     pr("WARNING: Your mil %s has been reduced from %d to %d!\n",
303                        prcom(1, com), com->troops, MAX(0, mil - 1));
304                 com->troops = MAX(0, mil - 1);
305             }
306         }
307     }
308     com->set = 1;
309     com->mil = mil;
310     com->own = owner;
311     com->x = x;
312     com->y = y;
313     com->eff = eff;
314     com->mob = mob;
315     return com->troops;
316 }
317
318 /*
319  * In the course of the fight, the combat object may have lost mil, eff, or
320  * mobility.  This is the place where the data in the object gets flushed to
321  * disk to make it "real".
322  */
323
324 static void
325 put_combat(struct combat *com)
326 {
327     struct sctstr sect;
328     struct shpstr ship;
329     struct lndstr land;
330     int deff;
331
332     switch (com->type) {
333     case EF_SECTOR:
334         getsect(com->x, com->y, &sect);
335         sect.sct_type = com->sct_type;
336         deff = sect.sct_effic - com->eff;
337         if (deff > 0) {
338             sect.sct_road -= sect.sct_road * deff / 100.0;
339             sect.sct_rail -= sect.sct_rail * deff / 100.0;
340             sect.sct_defense -= sect.sct_defense * deff / 100.0;
341             if (sect.sct_road <= 0)
342                 sect.sct_road = 0;
343             if (sect.sct_rail <= 0)
344                 sect.sct_rail = 0;
345             if (sect.sct_defense <= 0)
346                 sect.sct_defense = 0;
347         }
348         sect.sct_effic = com->eff;
349         if (com->mobcost) {
350             if (opt_MOB_ACCESS) {
351                 if ((com->mob - com->mobcost) < -127)
352                     sect.sct_mobil = -127;
353                 else
354                     sect.sct_mobil = com->mob - com->mobcost;
355             } else {
356                 if ((com->mob - com->mobcost) < 0)
357                     sect.sct_mobil = 0;
358                 else
359                     sect.sct_mobil = com->mob - com->mobcost;
360             }
361         }
362         sect.sct_own = com->own;
363         if (com->plague) {
364             if (sect.sct_pstage == PLG_HEALTHY)
365                 sect.sct_pstage = PLG_EXPOSED;
366         }
367         sect.sct_item[I_MILIT] = com->mil;
368         putsect(&sect);
369         com->own = sect.sct_own;        /* avoid WARNING if sector reverts */
370         break;
371     case EF_LAND:
372         getland(com->lnd_uid, &land);
373         land.lnd_effic = com->eff;
374         if (com->mobcost) {
375             if (com->mob - com->mobcost < -127)
376                 land.lnd_mobil = -127;
377             else
378                 land.lnd_mobil = (signed char)(com->mob - com->mobcost);
379         }
380         land.lnd_own = com->own;
381         if (com->plague) {
382             if (land.lnd_pstage == PLG_HEALTHY)
383                 land.lnd_pstage = PLG_EXPOSED;
384         }
385         if (!(com->lnd_lcp->l_flags & L_SPY))
386             land.lnd_item[I_MILIT] = com->mil;
387         if (com->own == player->cnum) {
388             land.lnd_mission = 0;
389             land.lnd_rflags = 0;
390             memset(land.lnd_rpath, 0, sizeof(land.lnd_rpath));
391         }
392         putland(com->lnd_uid, &land);
393         break;
394     case EF_SHIP:
395         getship(com->shp_uid, &ship);
396         ship.shp_effic = com->eff;
397         if (com->mobcost) {
398             if (com->mob - com->mobcost < -127)
399                 ship.shp_mobil = -127;
400             else
401                 ship.shp_mobil = (signed char)(com->mob - com->mobcost);
402         }
403         ship.shp_own = com->own;
404         if (com->plague) {
405             if (ship.shp_pstage == PLG_HEALTHY)
406                 ship.shp_pstage = PLG_EXPOSED;
407         }
408         ship.shp_item[I_MILIT] = com->mil;
409         if (com->own == player->cnum) {
410             ship.shp_mission = 0;
411             ship.shp_rflags = 0;
412             memset(ship.shp_rpath, 0, sizeof(ship.shp_rpath));
413         }
414         putship(com->shp_uid, &ship);
415     }
416     com->mobcost = 0;
417     att_get_combat(com, com->own != player->cnum);
418 }
419
420 /* If pre-attack, abort fight.  If post-attack, don't move anything in */
421
422 static int
423 abort_attack(void)
424 {
425     return player->aborted = 1;
426 }
427
428 /*
429  * This is the combat_mode based integrity check.  It splits among two main
430  * divisions: first time/not first time, and attack/assault/para/board.
431  */
432
433 int
434 att_abort(int combat_mode, struct combat *off, struct combat *def)
435 {
436     struct sctstr sect;
437
438     if (player->aborted)
439         return 1;
440     if (att_get_combat(def, 1) < 0)
441         return abort_attack();
442
443     if (off && combat_mode != A_ATTACK) {
444         if (att_get_combat(off, 0) < 0)
445             return abort_attack();
446         if (off->type == EF_SHIP &&
447             !(off->x == def->x && off->y == def->y) &&
448             (!getsect(off->x, off->y, &sect) ||
449              sect.sct_type != SCT_WATER)) {
450             pr("%s can not %s from that far inland!\n",
451                prcom(0, off), att_mode[combat_mode]);
452             return abort_attack();
453         }
454     }
455     switch (combat_mode) {
456     case A_ATTACK:
457         if (!neigh(def->x, def->y, player->cnum) &&
458             !adj_units(def->x, def->y, player->cnum)) {
459             pr("You are not adjacent to %s\n",
460                xyas(def->x, def->y, player->cnum));
461             return abort_attack();
462         }
463         if (def->own == player->cnum) {
464             pr("You can't attack your own sector.\n");
465             return abort_attack();
466         }
467         break;
468     case A_ASSAULT:
469         if (off && mapdist(off->x, off->y, def->x, def->y) > 1) {
470             pr("You'll have to get there first...\n");
471             return abort_attack();
472         }
473         if (off && def->sct_type == SCT_MOUNT) {
474             pr("You can't assault a %s sector!\n", def->sct_dcp->d_name);
475             return abort_attack();
476         }
477         break;
478     case A_PARA:
479         if (def->own == player->cnum) {
480             pr("You can't air-assault your own sector.\n");
481             return abort_attack();
482         }
483         if (off && (def->sct_type == SCT_MOUNT ||
484                     def->sct_type == SCT_WATER ||
485                     def->sct_type == SCT_CAPIT ||
486                     def->sct_type == SCT_FORTR ||
487                     def->sct_type == SCT_WASTE)) {
488             pr("You can't air-assault a %s sector!\n",
489                def->sct_dcp->d_name);
490             return abort_attack();
491         }
492         break;
493     case A_BOARD:
494         return board_abort(off, def);
495     case A_LBOARD:
496         return land_board_abort(off, def);
497     }
498
499     if (off && def->sct_dcp->d_mob0 < 0) {
500         pr("You can't %s a %s sector!\n",
501            att_mode[combat_mode], def->sct_dcp->d_name);
502         return abort_attack();
503     }
504     if (!off || off->relations_checked)
505         return 0;
506     off->relations_checked = 1;
507
508     if (opt_HIDDEN) {
509         setcont(player->cnum, def->own, FOUND_SPY);
510         setcont(def->own, player->cnum, FOUND_SPY);
511     }
512
513     return 0;
514 }
515
516 /*
517  * Lots of special things need to be checked for boarding, so I put it in
518  * it's own function.
519  */
520
521 static int
522 board_abort(struct combat *off, struct combat *def)
523 {
524     struct shpstr aship, dship; /* for tech levels */
525     struct sctstr sect;
526
527     if (att_get_combat(def, 1) < 0)
528         return abort_attack();
529
530     if (!off)
531         return 0;
532
533     if (att_get_combat(off, 0) < 0)
534         return abort_attack();
535
536     if (off->x != def->x || off->y != def->y) {
537         pr("Ship #%d is not in the same sector!\n", def->shp_uid);
538         return abort_attack();
539     }
540     if (off->type == EF_SHIP) {
541         if (off->mob <= 0) {
542             pr("%s has no mobility!\n", prcom(0, off));
543             return abort_attack();
544         }
545         getship(off->shp_uid, &aship);
546         getship(def->shp_uid, &dship);
547         if (techfact(aship.shp_tech, shp_speed(&aship)) * off->eff
548             <= techfact(dship.shp_tech, shp_speed(&dship)) * def->eff) {
549             pr("Victim ship moves faster than you do!\n");
550             if (def->own)
551                 wu(0, def->own,
552                    "%s (#%d) %s failed to catch %s\n",
553                    cname(aship.shp_own), aship.shp_own,
554                    pr_com(0, off, def->own), pr_com(0, def, def->own));
555             return abort_attack();
556         }
557     } else if (off->type != EF_SECTOR) {
558         pr("Please tell the deity that you got the 'banana boat' error\n");
559         return abort_attack();
560     }
561     if (def->shp_mcp->m_flags & M_SUB) {
562         getsect(def->x, def->y, &sect);
563         if (sect.sct_type == SCT_WATER) {
564             pr("You can't board a submarine!\n");
565             return abort_attack();
566         }
567     }
568     return 0;
569 }
570
571 /*
572  * Lots of special things need to be checked for boarding, so I put it in
573  * it's own function.
574  * STM - I copied it for land unit boarding. :)
575  */
576
577 static int
578 land_board_abort(struct combat *off, struct combat *def)
579 {
580     if (att_get_combat(def, 1) < 0)
581         return abort_attack();
582
583     if (!off)
584         return 0;
585
586     if (att_get_combat(off, 0) < 0)
587         return abort_attack();
588
589     if (off->x != def->x || off->y != def->y) {
590         pr("Land unit #%d is not in the same sector!\n", def->lnd_uid);
591         return abort_attack();
592     }
593
594     return 0;
595 }
596
597 /* If we are boarding, then the defending ship gets a chance to fire back */
598 int
599 att_approach(struct combat *off, struct combat *def)
600 {
601     int dam;
602     struct sctstr sect;
603     struct shpstr ship;
604
605     pr("Approaching %s...\n", prcom(0, def));
606     if (def->own)
607         wu(0, def->own,
608            "%s is being approached by %s...\n",
609            pr_com(0, def, def->own), pr_com(0, off, def->own));
610     if (!(dam = shipdef(player->cnum, def->own, def->x, def->y)))
611         return 0;
612
613     pr("They're firing at us sir!\n");
614     if (def->own) {
615         wu(0, def->own,
616            "Your fleet at %s does %d damage to %s\n",
617            xyas(def->x, def->y, def->own), dam, pr_com(0, off, def->own));
618     }
619     if (off->type == EF_SECTOR) {
620         getsect(off->x, off->y, &sect);
621         sectdamage(&sect, dam);
622         putsect(&sect);
623         pr("Enemy fleet at %s does %d damage to %s\n",
624            xyas(def->x, def->y, player->cnum), dam, prcom(0, off));
625     } else if (off->type == EF_SHIP) {
626         getship(off->shp_uid, &ship);
627         shipdamage(&ship, dam);
628         putship(off->shp_uid, &ship);
629         if (def->own && ship.shp_effic < SHIP_MINEFF) {
630             wu(0, def->own, "%s sunk!\n", pr_com(0, off, def->own));
631             nreport(player->cnum, N_SHP_LOSE, def->own, 1);
632         }
633     }
634     if (att_get_combat(off, 0) < 0)
635         return abort_attack();
636     return 0;
637 }
638
639 /* The attack is valid.  Tell the attacker about what they're going to hit */
640
641 int
642 att_show(struct combat *def)
643 {
644     /* Note that we tell the player about the treaty BEFORE we tell them
645        about the item.  If we didn't, then they gain free information */
646     if (def->type == EF_SECTOR) {
647         if (!trechk(player->cnum, def->own, LANATT))
648             return abort_attack();
649         pr("%s is a %d%% %s %s with approximately %d military.\n",
650            xyas(def->x, def->y, player->cnum),
651            roundintby((int)def->eff, 10),
652            cname(def->own), def->sct_dcp->d_name,
653            roundintby(def->troops, 10));
654         if (map_set(player->cnum, def->x, def->y, def->sct_dcp->d_mnem, 0))
655             writemap(player->cnum);
656     } else if (def->type == EF_SHIP || def->type == EF_LAND) {
657         if (def->type == EF_SHIP) {
658             if (!trechk(player->cnum, def->own, SEAATT))
659                 return abort_attack();
660         } else {
661             if (!trechk(player->cnum, def->own, LNDATT))
662                 return abort_attack();
663         }
664         pr("%s is about %d%% efficient and has approximately %d mil on board.\n",
665            prcom(0, def), roundintby((int)def->eff, 10),
666            roundintby(def->troops, 10));
667     }
668     /* Ok, everything is fine */
669     return 0;
670 }
671
672 /* Attack and assault ask the user which kind of support they want */
673
674 int
675 att_ask_support(int offset, int *fortp, int *shipp, int *landp,
676                 int *planep)
677 {
678     char buf[1024];
679     char *p;
680     *fortp = *shipp = *landp = *planep = 1;
681
682     if (player->argp[offset] != NULL) {
683         if ((player->argp[offset + 1] == NULL) ||
684             (player->argp[offset + 2] == NULL) ||
685             (player->argp[offset + 3] == NULL)) {
686             pr("If any support arguments are used, all must be!\n");
687             return RET_SYN;
688         }
689
690         *fortp = *shipp = 0;
691         *landp = *planep = 0;
692
693         p = getstarg(player->argp[offset], "Use fort support? ", buf);
694         if (!p)
695             return RET_SYN;
696
697         if ((*p == 'y') || (*p == 'Y'))
698             *fortp = 1;
699
700         p = getstarg(player->argp[offset + 1], "Use ship support? ", buf);
701         if (!p)
702             return RET_SYN;
703
704         if ((*p == 'y') || (*p == 'Y'))
705             *shipp = 1;
706
707         p = getstarg(player->argp[offset + 2], "Use land support? ", buf);
708         if (!p)
709             return RET_SYN;
710
711         if ((*p == 'y') || (*p == 'Y'))
712             *landp = 1;
713
714         p = getstarg(player->argp[offset + 3], "Use plane support? ", buf);
715         if (!p)
716             return RET_SYN;
717
718         if ((*p == 'y') || (*p == 'Y'))
719             *planep = 1;
720     }
721     return RET_OK;
722 }
723
724 /*
725  * Attack, assault, and board ask the attacker what they'd like to attack
726  * with.  This includes mil and land units from each "off" object.  Note that
727  * after each sub-prompt, we check to make sure that the attack is still
728  * valid, and if it's not, then we abort the attack.
729  */
730
731 int
732 att_ask_offense(int combat_mode, struct combat *off, struct combat *def,
733                 struct emp_qelem *olist, int *a_spyp, int *a_engineerp)
734 {
735     int n;
736     char land_answer[256];
737
738     emp_initque(olist);
739     if (att_abort(combat_mode, off, def))
740         return 0;
741     memset(land_answer, 0, sizeof(land_answer));
742     for (n = 0; n <= off->last; ++n) {
743         off[n].troops = ask_off(combat_mode, off + n, def);
744         if (att_abort(combat_mode, off, def))
745             return 0;
746         ask_olist(combat_mode, off + n, def, olist, land_answer,
747                   a_spyp, a_engineerp);
748         if (att_abort(combat_mode, off, def))
749             return 0;
750     }
751     return 0;
752 }
753
754 /*
755  * Return path cost for ATTACKER to enter sector given by DEF.
756  * MOBTYPE is a mobility type accepted by sector_mcost().
757  */
758 static double
759 att_mobcost(natid attacker, struct combat *def, int mobtype)
760 {
761     struct sctstr sect;
762     int ok;
763
764     if (CANT_HAPPEN(def->type != EF_SECTOR))
765         return -1.0;
766     ok = getsect(def->x, def->y, &sect);
767     if (CANT_HAPPEN(!ok))
768         return -1.0;
769
770     /*
771      * We want the cost to move/march into the sector.  If we just
772      * called sector_mcost(), we'd get the defender's cost.  The
773      * attacker's cost is higher unless he's the old-owner.  Note: if
774      * there are no civilians, a victorious attacker will become the
775      * old-owner.  But he isn't now.
776      */
777     sect.sct_own = attacker;
778     sect.sct_mobil = 0;
779     return sector_mcost(&sect, mobtype);
780 }
781
782 /* How many mil is off allowed to attack with when it attacks def? */
783
784 static int
785 get_mob_support(int combat_mode, struct combat *off, struct combat *def)
786 {
787     int mob_support;
788     double mobcost;
789
790     switch (combat_mode) {
791     case A_ATTACK:
792         mobcost = att_mobcost(off->own, def, MOB_MOVE);
793         if (mobcost < 0 || off->mob <= 0)
794             return 0;
795         mob_support = off->mob / mobcost;
796         if (mob_support < off->troops)
797             pr("Sector %s has %d mobility which can only support %d mil,\n",
798                xyas(off->x, off->y, player->cnum), off->mob, mob_support);
799         else
800             mob_support = off->troops;
801         return mob_support;
802     case A_ASSAULT:
803         if (def->own != player->cnum && def->mil) {
804             if (off->shp_mcp->m_flags & M_SEMILAND)
805                 return off->troops / 4;
806             else if (!(off->shp_mcp->m_flags & M_LAND))
807                 return off->troops / 10;
808         }
809         break;
810     case A_BOARD:
811         if (off->type == EF_SECTOR && off->mob <= 0)
812             return 0;
813         mob_support = def->shp_mcp->m_item[I_MILIT];
814         if (mob_support < off->troops)
815             pr("The size of the ship you are trying to board limits your party to %d mil,\n", mob_support);
816         else
817             mob_support = off->troops;
818         return mob_support;
819     case A_LBOARD:
820         if (off->mob <= 0)
821             return 0;
822         if (def->lnd_lcp->l_flags & L_SPY)
823             return 1;
824         mob_support = def->lnd_lcp->l_item[I_MILIT];
825         if (mob_support < off->troops)
826             pr("The size of the unit you are trying to board limits your party to %d mil,\n", mob_support);
827         else
828             mob_support = off->troops;
829         return mob_support;
830     }
831     return off->troops;
832 }
833
834 /*
835  * If the attacker decides to go through with the attack, then the
836  * sectors/ships they are attacking with may be charged some mobility.
837  * This is where that amount of mobility is calculated.  It is actually
838  * subtracted "for real" from the object's mobility in put_combat().
839  */
840
841 static void
842 calc_mobcost(int combat_mode, struct combat *off, struct combat *def,
843              int attacking_mil)
844 {
845     struct shpstr ship;
846
847     if (!attacking_mil)
848         return;
849     switch (combat_mode) {
850     case A_ATTACK:
851         off->mobcost += MAX(1,
852                             (int)(attacking_mil
853                                   * att_mobcost(off->own, def, MOB_MOVE)));
854         break;
855     case A_LBOARD:
856         off->mobcost += MAX(1, attacking_mil / 5);
857         break;
858     case A_BOARD:
859         switch (off->type) {
860         case EF_SECTOR:
861             off->mobcost += MAX(1, attacking_mil / 5);
862             break;
863         case EF_SHIP:
864             /* the 2 in the formula below is a fudge factor */
865             getship(def->shp_uid, &ship);
866             off->mobcost += (def->eff / 100) * (shp_speed(&ship) / 2);
867         }
868     }
869 }
870
871 /* How many mil to we want to attack from off against def? */
872
873 static int
874 ask_off(int combat_mode, struct combat *off, struct combat *def)
875 {
876     int attacking_mil;
877     int mob_support;
878     char prompt[512];
879
880     if (att_get_combat(off, 0) <= 0)
881         return 0;
882     if ((off->type == EF_SECTOR) && (off->own != player->cnum))
883         return 0;
884     if ((mob_support = get_mob_support(combat_mode, off, def)) <= 0)
885         return 0;
886     if (off->type == EF_SECTOR) {
887         if (off->own != player->cnum)
888             return 0;
889         sprintf(prompt, "Number of mil from %s at %s (max %d) : ",
890                 off->sct_dcp->d_name,
891                 xyas(off->x, off->y, player->cnum), mob_support);
892     } else {
893         sprintf(prompt, "Number of mil from %s (max %d) : ",
894                 prcom(0, off), mob_support);
895     }
896     if ((attacking_mil = onearg(NULL, prompt)) < 0)
897         abort_attack();
898     if (att_abort(combat_mode, off, def))
899         return 0;
900     if (att_get_combat(off, 0) <= 0)
901         return 0;
902     if ((attacking_mil =
903          MIN(attacking_mil, MIN(mob_support, off->troops))) <= 0)
904         return 0;
905
906     calc_mobcost(combat_mode, off, def, attacking_mil);
907     return attacking_mil;
908 }
909
910 /*
911  * Which units would you like to attack with or move in with [ynYNq?]
912  */
913
914 static char
915 att_prompt(char *prompt, char army)
916 {
917     char buf[1024];
918     char *p;
919
920     if (!army)
921         army = '~';
922     for (;;) {
923         p = getstring(prompt, buf);
924         if (!p || *p == 'q') {
925             abort_attack();
926             return 'N';
927         }
928         if (!*p)
929             return 'n';
930         if (tolower(*p) == 'y' || tolower(*p) == 'n')
931             return *p;
932         pr("y - yes this unit\n"
933            "n - no this unit\n"
934            "Y - yes to all units in army '%c'\n"
935            "N - no to all units in army '%c'\n"
936            "q - quit\n? - this help message\n\n",
937            army, army);
938     }
939 }
940
941 /* Ask the attacker which units they want to attack/assault/board with */
942
943 static void
944 ask_olist(int combat_mode, struct combat *off, struct combat *def,
945           struct emp_qelem *olist, char *land_answer, int *a_spyp,
946           int *a_engineerp)
947 {
948     struct nstr_item ni;
949     struct lndstr land;
950     double pathcost, mobcost;
951     int reqmob;
952     struct ulist *llp;
953     struct lchrstr *lcp;
954     double att_val;
955     int count = 0;
956     int maxland = 0;
957     int first_time = 1;
958     char prompt[512];
959
960     if (def->type == EF_LAND)
961         return;
962     if (def->type == EF_SHIP)
963         maxland = def->shp_mcp->m_nland;
964
965     snxtitem_xy(&ni, EF_LAND, off->x, off->y);
966     while (nxtitem(&ni, &land)) {
967         if (land.lnd_own != player->cnum)
968             continue;
969         if (land.lnd_effic < LAND_MINEFF)
970             continue;
971         if (land_answer[(int)land.lnd_army] == 'N')
972             continue;
973         if (!lnd_can_attack(&land))
974             continue;
975         lcp = &lchr[(int)land.lnd_type];
976
977         if (def->type == EF_SHIP && !maxland) {
978             pr("Land units are not able to board this kind of ship\n");
979             return;
980         }
981         if (def->type == EF_SHIP
982             && (def->shp_mcp->m_flags & (M_SUPPLY | M_SUB)) != M_SUPPLY
983             && !(lcp->l_flags & L_LIGHT)) {
984             pr("Only light land units can board this kind of ship\n");
985             continue;
986         }
987         if (land.lnd_mobil <= 0) {
988             pr("%s is out of mobility, and cannot %s\n",
989                prland(&land), att_mode[combat_mode]);
990             continue;
991         }
992
993         if (opt_MARKET) {
994             if (ontradingblock(EF_LAND, &land)) {
995                 pr("%s is on the trading block, and cannot %s\n",
996                    prland(&land), att_mode[combat_mode]);
997                 continue;
998             }
999         }
1000
1001         if (off->type == EF_SECTOR && land.lnd_ship >= 0) {
1002             pr("%s is on ship #%d, and cannot %s\n",
1003                prland(&land), land.lnd_ship, att_mode[combat_mode]);
1004             continue;
1005         } else if (off->type == EF_SHIP) {
1006             if (land.lnd_ship != off->shp_uid)
1007                 continue;
1008         } else if (land.lnd_land >= 0) {
1009             pr("%s is on unit #%d, and cannot %s\n",
1010                prland(&land), land.lnd_land, att_mode[combat_mode]);
1011             continue;
1012         }
1013         switch (combat_mode) {
1014         case A_ATTACK:
1015             /*
1016              * We used to let land units attack only if they have the
1017              * mobility consumed by the attack, not counting combat
1018              * and moving in to occupy.  Making sure your land units
1019              * reach attack positions with enough mobility left is a
1020              * pain in the neck.  We now require positive mobility,
1021              * just like for marching.  Except we don't allow rushing
1022              * of high-mobility sectors (mountains): for those we
1023              * still require attack mobility.
1024              */
1025             pathcost = att_mobcost(land.lnd_own, def, lnd_mobtype(&land));
1026             mobcost = lnd_pathcost(&land, pathcost);
1027             if (pathcost < 1.0) {
1028                 if (land.lnd_mobil <= 0) {
1029                     pr("%s is out of mobility\n", prland(&land));
1030                     continue;
1031                 }
1032             } else {
1033                 reqmob = MIN(land_mob_max, (int)ceil(mobcost));
1034                 if (land.lnd_mobil < reqmob) {
1035                     pr("%s does not have enough mobility (%d needed)\n",
1036                        prland(&land), reqmob);
1037                     continue;
1038                 }
1039             }
1040             break;
1041         case A_ASSAULT:
1042         case A_BOARD:
1043             mobcost = 0;
1044             if (!(lcp->l_flags & L_ASSAULT))
1045                 continue;
1046             break;
1047         default:
1048             CANT_REACH();
1049             return;
1050         }
1051         att_val = attack_val(combat_mode, &land);
1052         if (att_val < 1.0) {
1053             pr("%s has no offensive strength\n", prland(&land));
1054             continue;
1055         }
1056         if (!lnd_supply_all(&land)) {
1057             pr("%s is out of supply, and cannot %s\n",
1058                prland(&land), att_mode[combat_mode]);
1059             continue;
1060         }
1061         if (def->type == EF_SHIP && first_time) {
1062             first_time = 0;
1063             pr("You may board with a maximum of %d land units\n", maxland);
1064         }
1065         pr("%s has a base %s value of %.0f\n",
1066            prland(&land), att_mode[combat_mode], att_val);
1067         if (land_answer[(int)land.lnd_army] != 'Y') {
1068             sprintf(prompt,
1069                     "%s with %s %s (%c %d%%) [ynYNq?] ",
1070                     att_mode[combat_mode],
1071                     prland(&land),
1072                     prcom(1, off),
1073                     land.lnd_army ? land.lnd_army : '~',
1074                     land.lnd_effic);
1075             land_answer[(int)land.lnd_army] =
1076                 att_prompt(prompt, land.lnd_army);
1077             if (att_abort(combat_mode, off, def))
1078                 return;
1079             if (land_answer[(int)land.lnd_army] != 'y' &&
1080                 land_answer[(int)land.lnd_army] != 'Y')
1081                 continue;
1082         }
1083         if (!(llp = malloc(sizeof(struct ulist)))) {
1084             logerror("Malloc failed in attack!\n");
1085             abort_attack();
1086             return;
1087         }
1088         memset(llp, 0, sizeof(struct ulist));
1089         emp_insque(&llp->queue, olist);
1090         llp->mobil = mobcost;
1091         llp->unit.land = land;
1092         llp->x = llp->unit.land.lnd_x;
1093         llp->y = llp->unit.land.lnd_y;
1094         llp->chrp = (struct empobj_chr *)&lchr[(int)llp->unit.land.lnd_type];
1095         llp->eff = llp->unit.land.lnd_effic;
1096         if (lnd_spyval(&land) > *a_spyp)
1097             *a_spyp = lnd_spyval(&land);
1098         if (((struct lchrstr *)llp->chrp)->l_flags & L_ENGINEER)
1099             ++*a_engineerp;
1100         if (def->type == EF_SHIP && ++count >= maxland)
1101             break;
1102     }
1103 }
1104
1105 /* What's the offense or defense multiplier? */
1106
1107 double
1108 att_combat_eff(struct combat *com)
1109 {
1110     double eff = 1.0;
1111     double str;
1112     struct shpstr ship;
1113
1114     if (com->type == EF_SECTOR) {
1115         eff = com->eff / 100.0;
1116         if (com->own == player->cnum) {
1117             str = com->sct_dcp->d_ostr;
1118             eff = 1.0 + (str - 1.0) * eff;
1119         } else
1120             eff = sector_strength(getsectp(com->x, com->y));
1121     } else if (com->type == EF_SHIP && com->own != player->cnum) {
1122         getship(com->shp_uid, &ship);
1123         eff = 1.0 + shp_armor(&ship) / 100.0;
1124     }
1125     return eff;
1126 }
1127
1128 int
1129 att_get_offense(int combat_mode, struct combat *off,
1130                 struct emp_qelem *olist, struct combat *def)
1131 {
1132     int ototal;
1133
1134     /*
1135      * Get the attacker units & mil again in case they changed while the
1136      * attacker was answering sub-prompts.
1137      */
1138
1139     ototal = get_ototal(combat_mode, off, olist, 1.0, 1);
1140     if (att_empty_attack(combat_mode, ototal, def))
1141         return abort_attack();
1142     if (combat_mode == A_PARA)
1143         return ototal;
1144     pr("\n             Initial attack strength: %8d\n", ototal);
1145     return ototal;
1146 }
1147
1148 /* Get the defensive units and reacting units */
1149 int
1150 att_get_defense(struct emp_qelem *olist, struct combat *def,
1151                 struct emp_qelem *dlist, int a_spy, int ototal)
1152 {
1153     int d_spy = 0;
1154     struct emp_qelem *qp;
1155     struct ulist *llp;
1156     int dtotal;
1157     int old_dtotal;
1158
1159     emp_initque(dlist);
1160     get_dlist(def, dlist, 0, &d_spy);
1161     dtotal = get_dtotal(def, dlist, 1.0, 0);
1162
1163     /*
1164      * Call in reacting units
1165      */
1166
1167     if (def->type == EF_SECTOR && def->sct_type != SCT_MOUNT)
1168         att_reacting_units(def, dlist, a_spy, &d_spy, ototal);
1169
1170     for (qp = olist->q_forw; qp != olist; qp = qp->q_forw) {
1171         llp = (struct ulist *)qp;
1172         intelligence_report(def->own, &llp->unit.land, d_spy,
1173                             "Scouts report attacking unit:");
1174     }
1175
1176     old_dtotal = dtotal;
1177     dtotal = get_dtotal(def, dlist, 1.0, 0);
1178     if (dtotal != old_dtotal)
1179         pr("Defense strength with reacting units: %8d\n", dtotal);
1180
1181     return dtotal;
1182 }
1183
1184 /* Get the defensive land units in the sector or on the ship */
1185
1186 static void
1187 get_dlist(struct combat *def, struct emp_qelem *list, int a_spy,
1188           int *d_spyp)
1189 {
1190     struct nstr_item ni;
1191     struct ulist *llp;
1192     struct lndstr land;
1193
1194 /* In here is where you need to take out spies and trains from the defending
1195    lists.  Spies try to hide, trains get trapped and can be boarded. */
1196
1197     snxtitem_xy(&ni, EF_LAND, def->x, def->y);
1198     while (nxtitem(&ni, &land)) {
1199         if (!land.lnd_own)
1200             continue;
1201         if (land.lnd_own != def->own)
1202             continue;
1203         if (def->type == EF_SECTOR && land.lnd_ship >= 0)
1204             continue;
1205         if (def->type == EF_SECTOR && land.lnd_land >= 0)
1206             continue;
1207         if (def->type == EF_SHIP && land.lnd_ship != def->shp_uid)
1208             continue;
1209         if (def->type == EF_LAND && land.lnd_land != def->lnd_uid)
1210             continue;
1211         intelligence_report(player->cnum, &land, a_spy,
1212                             "Scouts report defending unit:");
1213         if (!(llp = malloc(sizeof(struct ulist)))) {
1214             logerror("Malloc failed in attack!\n");
1215             abort_attack();
1216             return;
1217         }
1218         memset(llp, 0, sizeof(struct ulist));
1219         emp_insque(&llp->queue, list);
1220         llp->supplied = lnd_supply_all(&land);
1221         llp->unit.land = land;
1222         llp->x = llp->unit.land.lnd_x;
1223         llp->y = llp->unit.land.lnd_y;
1224         llp->chrp = (struct empobj_chr *)&lchr[(int)llp->unit.land.lnd_type];
1225         llp->eff = llp->unit.land.lnd_effic;
1226         if (lnd_spyval(&land) > *d_spyp)
1227             *d_spyp = lnd_spyval(&land);
1228     }
1229 }
1230
1231 /* Calculate the total offensive strength */
1232
1233 static int
1234 get_ototal(int combat_mode, struct combat *off, struct emp_qelem *olist,
1235            double osupport, int check)
1236 {
1237     double ototal = 0.0;
1238     struct emp_qelem *qp, *next;
1239     struct ulist *llp;
1240     int n, w;
1241
1242     /*
1243      * first, total the attacking mil
1244      */
1245
1246     for (n = 0; n <= off->last; ++n) {
1247         if (off[n].type == EF_BAD || (check &&
1248                                       att_get_combat(&off[n], 0) <= 0))
1249             continue;
1250         ototal += off[n].troops * att_combat_eff(off + n);
1251     }
1252
1253     /*
1254      * next, add in the attack_values of all
1255      * the attacking units
1256      */
1257
1258     for (qp = olist->q_forw; qp != olist; qp = next) {
1259         next = qp->q_forw;
1260         llp = (struct ulist *)qp;
1261         if (check && !get_oland(combat_mode, llp))
1262             continue;
1263         if (combat_mode == A_ATTACK) {
1264             w = -1;
1265             for (n = 0; n <= off->last; ++n) {
1266                 if (off[n].type == EF_BAD)
1267                     continue;
1268                 if ((off[n].x == llp->unit.land.lnd_x) &&
1269                     (off[n].y == llp->unit.land.lnd_y))
1270                     w = n;
1271             }
1272             if (w < 0) {
1273                 lnd_print(player->cnum, llp,
1274                           "can't attack from this sector now");
1275                 lnd_delete(llp);
1276                 continue;
1277             }
1278             ototal += attack_val(combat_mode, &llp->unit.land) *
1279                 att_combat_eff(off + w);
1280         } else {
1281             ototal += attack_val(combat_mode, &llp->unit.land);
1282         }
1283     }
1284     ototal *= osupport;
1285
1286     return ldround(ototal, 1);
1287 }
1288
1289 /* Calculate the total defensive strength */
1290
1291 static int
1292 get_dtotal(struct combat *def, struct emp_qelem *list, double dsupport,
1293            int check)
1294 {
1295     double dtotal = 0.0, eff = 1.0, d_unit;
1296     struct emp_qelem *qp, *next;
1297     struct ulist *llp;
1298
1299     if (check && att_get_combat(def, 1) < 0)
1300         return 0;
1301     eff = att_combat_eff(def);
1302     dtotal = def->troops * eff;
1303
1304     /*
1305      * next, add in the defense_values of all
1306      * the defending non-retreating units
1307      */
1308
1309     for (qp = list->q_forw; qp != list; qp = next) {
1310         next = qp->q_forw;
1311         llp = (struct ulist *)qp;
1312         if (check && !get_dland(def, llp))
1313             continue;
1314         d_unit = defense_val(&llp->unit.land);
1315         if (!llp->supplied)
1316             d_unit /= 2.0;
1317         dtotal += d_unit * eff;
1318     }
1319
1320     dtotal *= dsupport;
1321
1322     return ldround(dtotal, 1);
1323 }
1324
1325 /*
1326  * This is the land unit integrity check.
1327  */
1328
1329 static int
1330 get_oland(int combat_mode, struct ulist *llp)
1331 {
1332     struct lndstr *lp = &llp->unit.land;
1333     char buf[512];
1334
1335     getland(llp->unit.land.lnd_uid, lp);
1336
1337     if (lp->lnd_own != player->cnum) {
1338         sprintf(buf, "was destroyed and is no longer a part of the %s",
1339                 att_mode[combat_mode]);
1340         lnd_print(player->cnum, llp, buf);
1341         lnd_delete(llp);
1342         return 0;
1343     }
1344     if (lp->lnd_x != llp->x || lp->lnd_y != llp->y) {
1345         sprintf(buf,
1346                 "left to fight another battle and is no longer a part of the %s",
1347                 att_mode[combat_mode]);
1348         lnd_print(player->cnum, llp, buf);
1349         lnd_delete(llp);
1350         return 0;
1351     }
1352     if (lp->lnd_effic < llp->eff) {
1353         sprintf(buf, "damaged from %d%% to %d%%",
1354                 llp->eff, lp->lnd_effic);
1355         lnd_print(player->cnum, llp, buf);
1356     }
1357
1358     llp->eff = llp->unit.land.lnd_effic;
1359     return 1;
1360 }
1361
1362 static int
1363 get_dland(struct combat *def, struct ulist *llp)
1364 {
1365     struct lndstr *lp = &llp->unit.land;
1366     char buf[512];
1367
1368     getland(llp->unit.land.lnd_uid, lp);
1369
1370     if (lp->lnd_effic < LAND_MINEFF) {
1371         sprintf(buf, "was destroyed and is no longer a part of the defense");
1372         lnd_print(llp->unit.land.lnd_own, llp, buf);
1373         lnd_delete(llp);
1374         return 0;
1375     }
1376     if (lp->lnd_x != def->x || lp->lnd_y != def->y) {
1377         lnd_print(llp->unit.land.lnd_own, llp,
1378                   "left to go fight another battle and is no longer a part of the defense");
1379         lnd_delete(llp);
1380         return 0;
1381     }
1382
1383     llp->eff = llp->unit.land.lnd_effic;
1384     return 1;
1385 }
1386
1387 static void
1388 kill_land(struct emp_qelem *list)
1389 {
1390     struct emp_qelem *qp, *next;
1391     struct ulist *llp;
1392
1393     for (qp = list->q_forw; qp != list; qp = next) {
1394         next = qp->q_forw;
1395         llp = (struct ulist *)qp;
1396         if (llp->unit.land.lnd_ship >= 0) {
1397             llp->unit.land.lnd_effic = 0;
1398             lnd_print(player->cnum, llp,
1399                       "cannot return to the ship, and dies!");
1400             lnd_delete(llp);
1401         }
1402     }
1403 }
1404
1405 static void
1406 att_infect_units(struct emp_qelem *list, int plague)
1407 {
1408     struct emp_qelem *qp, *next;
1409     struct ulist *llp;
1410
1411     if (!plague)
1412         return;
1413     for (qp = list->q_forw; qp != list; qp = next) {
1414         next = qp->q_forw;
1415         llp = (struct ulist *)qp;
1416         if (llp->unit.land.lnd_pstage == PLG_HEALTHY)
1417             llp->unit.land.lnd_pstage = PLG_EXPOSED;
1418     }
1419 }
1420
1421 /*
1422  * Put the land unit on the disk.  If there was some mobility cost, then
1423  * subtract it from the units mobility.  Note that this works the same way
1424  * as sectors & ships in that no mobility is actually taken until the attacker
1425  * has committed to attacking.
1426  */
1427
1428 static void
1429 put_oland(struct emp_qelem *list)
1430 {
1431     struct emp_qelem *qp, *next;
1432     struct ulist *llp;
1433
1434     for (qp = list->q_forw; qp != list; qp = next) {
1435         next = qp->q_forw;
1436         llp = (struct ulist *)qp;
1437         llp->unit.land.lnd_mission = 0;
1438         llp->unit.land.lnd_harden = 0;
1439         llp->unit.land.lnd_mobil -= (int)llp->mobil;
1440         llp->mobil = 0.0;
1441         putland(llp->unit.land.lnd_uid, &llp->unit.land);
1442         if (llp->unit.land.lnd_own != player->cnum) {
1443             emp_remque((struct emp_qelem *)llp);
1444             free(llp);
1445         } else
1446             get_oland(A_ATTACK, llp);
1447     }
1448 }
1449
1450 /*
1451  * Keep sending in reinforcements until it looks like we're going to win.
1452  * Note that the "strength" command also calls this routine.
1453  */
1454
1455 double
1456 att_reacting_units(struct combat *def, struct emp_qelem *list, int a_spy,
1457                    int *d_spyp, int ototal)
1458 {
1459     struct nstr_item ni;
1460     struct lndstr land;
1461     struct ulist *llp;
1462     int dtotal;
1463     double new_land = 0;
1464     double mobcost;
1465     double pathcost;
1466     int origx, origy;
1467     double eff = att_combat_eff(def);
1468
1469     if (list)
1470         dtotal = get_dtotal(def, list, 1.0, 1);
1471     else
1472         dtotal = 0;
1473     snxtitem_all(&ni, EF_LAND);
1474     while (nxtitem(&ni, &land) && dtotal + new_land * eff < 1.2 * ototal) {
1475         if (!land.lnd_own)
1476             continue;
1477         if (land.lnd_mission != MI_RESERVE)
1478             continue;
1479         if ((land.lnd_x == def->x) && (land.lnd_y == def->y))
1480             continue;
1481         if (land.lnd_own != def->own)
1482             continue;
1483         if (land.lnd_ship >= 0)
1484             continue;
1485         if (land.lnd_land >= 0)
1486             continue;
1487         if (defense_val(&land) < 1.0)
1488             continue;
1489         if (!lnd_can_attack(&land))
1490             continue;
1491
1492         /* Only supplied units can react */
1493         if (list ? !lnd_supply_all(&land) : !lnd_could_be_supplied(&land))
1494             continue;
1495
1496         if (!in_oparea((struct empobj *)&land, def->x, def->y))
1497             continue;
1498
1499         pathcost = path_find(land.lnd_x, land.lnd_y, def->x, def->y,
1500                             def->own, lnd_mobtype(&land));
1501         if (pathcost < 0)
1502             continue;
1503         mobcost = lnd_pathcost(&land, pathcost);
1504         if (land.lnd_mobil < mobcost)
1505             continue;
1506
1507         new_land += defense_val(&land);
1508
1509         if (!list)              /* we are in the "strength" command */
1510             continue;
1511
1512         /* move to defending sector */
1513         land.lnd_mobil -= ldround(mobcost, 1);
1514         origx = land.lnd_x;
1515         origy = land.lnd_y;
1516         land.lnd_x = def->x;
1517         land.lnd_y = def->y;
1518         putland(land.lnd_uid, &land);
1519         wu(0, land.lnd_own, "%s reacts to %s.\n",
1520            prland(&land), xyas(land.lnd_x, land.lnd_y, land.lnd_own));
1521
1522         llp = malloc(sizeof(struct ulist));
1523
1524         memset(llp, 0, sizeof(struct ulist));
1525         llp->supplied = 1;
1526         llp->x = origx;
1527         llp->y = origy;
1528         llp->chrp = (struct empobj_chr *)&lchr[(int)land.lnd_type];
1529         llp->unit.land = land;
1530         llp->eff = land.lnd_effic;
1531         emp_insque(&llp->queue, list);
1532         if (lnd_spyval(&land) > *d_spyp)
1533             *d_spyp = lnd_spyval(&land);
1534
1535         intelligence_report(player->cnum, &land, a_spy,
1536                             "Scouts sight reacting enemy unit:");
1537     }
1538     return new_land;
1539 }
1540
1541 /* Pop off shells and fly bombing missions to get your attack multiplier up */
1542
1543 static double
1544 get_osupport(char *outs, struct combat *def, int fort_sup, int ship_sup,
1545              int land_sup, int plane_sup)
1546 {
1547     double osupport = 1.0;
1548     int dam;
1549     double af, as, au, ap;
1550
1551     af = as = au = ap = 0.0;
1552     if (fort_sup) {
1553         dam = dd(def->own, player->cnum, def->x, def->y, 0, 0);
1554         af = dam / 100.0;
1555         osupport += af;
1556     }
1557     if (ship_sup) {
1558         dam = sd(def->own, player->cnum, def->x, def->y, 0, 0, 0);
1559
1560         as = dam / 100.0;
1561         osupport += as;
1562     }
1563
1564     if (land_sup) {
1565         dam = lnd_support(def->own, player->cnum, def->x, def->y, 0);
1566         au = dam / 100.0;
1567         osupport += au;
1568     }
1569
1570     if (plane_sup) {
1571         dam = off_support(def->x, def->y, def->own, player->cnum);
1572         ap = dam / 100.0;
1573         osupport += ap;
1574     }
1575     sprintf(outs, "attacker\t%1.2f\t%1.2f\t%1.2f\t%1.2f\n", af, as, au,
1576             ap);
1577     return osupport;
1578 }
1579
1580 /* Pop off shells and fly bombing missions to get your defense multiplier up */
1581
1582 static double
1583 get_dsupport(char *outs, struct emp_qelem *list, struct combat *def,
1584              int ototal, int dtotal)
1585 {
1586     double dsupport = 1.0;
1587     int dam;
1588     double df, ds, du, dp;
1589     int good = 0;
1590
1591     df = ds = du = dp = 0.0;
1592     if (dtotal < 0.1 * ototal) {
1593         good = -1;
1594     } else if (dtotal >= 1.2 * ototal) {
1595         good = 1;
1596     } else {
1597         dam = dd(player->cnum, def->own, def->x, def->y, 0, 1);
1598         df = dam / 100.0;
1599         dsupport += df;
1600
1601         dtotal = get_dtotal(def, list, dsupport, 0);
1602         if (dtotal < 1.2 * ototal) {
1603             dam = sd(player->cnum, def->own, def->x, def->y, 0, 1, 0);
1604             ds = dam / 100.0;
1605             dsupport += ds;
1606             dtotal = get_dtotal(def, list, dsupport, 0);
1607         }
1608         if (dtotal < 1.2 * ototal) {
1609             dam = lnd_support(player->cnum, def->own, def->x, def->y, 1);
1610             du = dam / 100.0;
1611             dsupport += du;
1612             dtotal = get_dtotal(def, list, dsupport, 1);
1613         }
1614         if (dtotal < 1.2 * ototal) {
1615             dam = def_support(def->x, def->y, player->cnum, def->own);
1616             dp = dam / 100.0;
1617             dsupport += dp;
1618         }
1619     }
1620     if (good)
1621         *outs = '\0';
1622     else
1623         sprintf(outs, "defender\t%1.2f\t%1.2f\t%1.2f\t%1.2f\n\n", df, ds,
1624                 du, dp);
1625     if (def->own) {
1626         if (good < 0)
1627             wu(0, def->own,
1628                "\nOdds are bad for us...support cancelled.\n\n");
1629         else if (good > 0)
1630             wu(0, def->own,
1631                "\nOdds are good for us...support cancelled.\n\n");
1632     }
1633     return dsupport;
1634 }
1635
1636 /*
1637  * Land mines add to the defense multiplier.  If the attacker has engineers
1638  * then this multiplier is cut in half.
1639  */
1640
1641 static double
1642 get_mine_dsupport(struct combat *def, int a_engineer)
1643 {
1644     int mines;
1645     struct sctstr sect;
1646
1647     getsect(def->x, def->y, &sect);
1648
1649     if (sect.sct_oldown != player->cnum) {
1650         mines = SCT_LANDMINES(&sect);
1651         mines = MIN(mines, 20);
1652         if (a_engineer)
1653             mines = ldround(mines / 2.0, 1);
1654         if (mines > 0) {
1655             if (def->own)
1656                 wu(0, def->own, "Defending mines add %1.2f\n",
1657                    mines * 0.02);
1658             pr("Defending mines add %1.2f\n", mines * 0.02);
1659             return mines * 0.02;
1660         }
1661     }
1662     return 0.0;
1663 }
1664
1665 /* Get the offensive and defensive support */
1666 int
1667 att_get_support(int combat_mode, int ofort, int oship, int oland,
1668                 int oplane, struct emp_qelem *olist, struct combat *off,
1669                 struct emp_qelem *dlist, struct combat *def,
1670                 double *osupportp, double *dsupportp, int a_engineer)
1671 {
1672     int ototal, dtotal;
1673     char osupports[512];
1674     char dsupports[512];
1675
1676     if (combat_mode == A_PARA)
1677         *osupports = '\0';
1678     else
1679         *osupportp = get_osupport(osupports, def,
1680                                   ofort, oship, oland, oplane);
1681
1682     /*
1683      * I need to put a 1 at the end of the next four total_stren calls
1684      * because units & mil may have been damaged by collateral damage or
1685      * nuclear warheads from the offensive & defensive support.
1686      */
1687
1688     ototal = get_ototal(combat_mode, off, olist, *osupportp, 1);
1689     if (att_empty_attack(combat_mode, ototal, def))
1690         return abort_attack();
1691     dtotal = get_dtotal(def, dlist, *dsupportp, 1);
1692
1693     /*
1694      * Calculate defensive support.  If odds are too good or too bad
1695      * then don't call in support.
1696      */
1697
1698     *dsupportp = get_dsupport(dsupports, dlist, def, ototal, dtotal);
1699     ototal = get_ototal(combat_mode, off, olist, *osupportp, 1);
1700     if (att_empty_attack(combat_mode, ototal, def))
1701         return abort_attack();
1702
1703     if ((*osupports || *dsupports) &&
1704         (*osupportp != 1.0 || *dsupportp != 1.0)) {
1705         pr("\n\t\tsupport values\n");
1706         pr("\t\tforts\tships\tunits\tplanes\n");
1707         if (*osupportp != 1.0)
1708             pr("%s", osupports);
1709         if (*dsupportp != 1.0)
1710             pr("%s", dsupports);
1711         if (def->own) {
1712             wu(0, def->own, "\n\t\tsupport values\n");
1713             wu(0, def->own, "\t\tforts\tships\tunits\tplanes\n");
1714             if (*osupportp != 1.0)
1715                 wu(0, def->own, "%s", osupports);
1716             if (*dsupportp != 1.0)
1717                 wu(0, def->own, "%s", dsupports);
1718         }
1719     }
1720
1721     dtotal = get_dtotal(def, dlist, *dsupportp, 1);
1722     if (dtotal && def->type == EF_SECTOR)
1723         *dsupportp += get_mine_dsupport(def, a_engineer);
1724     return 0;
1725 }
1726
1727 /* How many two-legged bipeds are in this combat force? */
1728
1729 static int
1730 count_bodies(struct combat *off, struct emp_qelem *list)
1731 {
1732     int n;
1733     int bodies = 0;
1734     struct emp_qelem *qp;
1735     struct ulist *llp;
1736
1737     for (n = 0; n <= off->last; ++n)
1738         bodies += off[n].troops;
1739     for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
1740         llp = (struct ulist *)qp;
1741         bodies += llp->unit.land.lnd_item[I_MILIT];
1742     }
1743     return bodies;
1744 }
1745
1746 /* This is where the fighting actually occurs. */
1747
1748 int
1749 att_fight(int combat_mode, struct combat *off, struct emp_qelem *olist,
1750           double osupport, struct combat *def, struct emp_qelem *dlist,
1751           double dsupport)
1752 {
1753     int success = 0;
1754     int a_cas = 0;              /* Casualty counts */
1755     int d_cas = 0;
1756     int ototal;                 /* total attacking strength */
1757     int dtotal;                 /* total defending strength */
1758     int a_bodies;               /* total attacking mil (incl. mil in units) */
1759     int d_bodies;               /* total defending mil (incl. mil in units) */
1760     int d_mil;
1761     int a_troops[6];
1762     int n;
1763     int news_item;
1764     int recalctime;
1765     double odds;
1766     int newmob;
1767     char *action;
1768
1769     ototal = get_ototal(combat_mode, off, olist, osupport,
1770                         combat_mode != A_PARA);
1771     dtotal = get_dtotal(def, dlist, dsupport, 0);
1772     if (!dtotal)
1773         success = 1;
1774
1775     a_bodies = count_bodies(off, olist);
1776     d_bodies = count_bodies(def, dlist);
1777     d_mil = def->troops;
1778     for (n = 0; n <= off->last; ++n)
1779         if (off[n].type == EF_BAD)
1780             a_troops[n] = 0;
1781         else
1782             a_troops[n] = off[n].troops;
1783
1784     /* This switch is required to get the spacing right */
1785     switch (combat_mode) {
1786     case A_ATTACK:
1787         pr("               Final attack strength: %8d\n", ototal);
1788         break;
1789     case A_ASSAULT:
1790         pr("              Final assault strength: %8d\n", ototal);
1791         break;
1792     case A_PARA:
1793         if (def->sct_type == SCT_MOUNT ||
1794             def->sct_type == SCT_WATER ||
1795             def->sct_type == SCT_CAPIT ||
1796             def->sct_type == SCT_FORTR || def->sct_type == SCT_WASTE) {
1797             pr("You can't air-assault a %s sector!\n",
1798                def->sct_dcp->d_name);
1799             a_cas = a_bodies;
1800             off[0].troops = 0;
1801             ototal = get_ototal(A_PARA, off, olist, osupport, 0);
1802         }
1803         pr("          Final air-assault strength: %8d\n", ototal);
1804         break;
1805     case A_BOARD:
1806     case A_LBOARD:
1807         pr("                Final board strength: %8d\n", ototal);
1808     }
1809
1810
1811     pr("              Final defense strength: %8d\n", dtotal);
1812     odds = att_calcodds(ototal, dtotal);
1813     pr("                          Final odds: %8d%%\n", (int)(odds * 100));
1814
1815     /* spread the plague */
1816     if (combat_mode != A_PARA) {
1817         if (!def->plague)
1818             for (n = 0; n <= off->last; ++n)
1819                 if (off[n].type != EF_BAD)
1820                     def->plague |= off[n].plague;
1821         for (n = 0; n <= off->last; ++n)
1822             if (off[n].type != EF_BAD)
1823                 off[n].plague |= def->plague;
1824     }
1825     att_infect_units(olist, off->plague);
1826     att_infect_units(dlist, def->plague);
1827
1828     /*
1829      * Fighting is slightly random.  There is always that last little
1830      * effort you see people put in.  Or the stray bullet that takes out
1831      * an officer and the rest go into chaos.  Things like that.
1832      * Thus, we have added a very slight random factor that will sometimes
1833      * allow the little guy to win. We modify the odds a little
1834      * (either +- 5%) to account for this randomness.  We also only
1835      * recalculate the odds every 8-50 casualties, not every cacsualty,
1836      * since a single dead guy normally wouldn't cause a commander to
1837      * rethink his strategies, but 50 dead guys might.
1838      */
1839     odds += (random() % 11 - 5) / 100.0;
1840     if (odds < 0.0)
1841         odds = 0.1;
1842     if (odds > 1.0)
1843         odds = 1.0;
1844     recalctime = 8 + (random() % 43);
1845     while (!success && ototal) {
1846         if (chance(odds)) {
1847             pr("!");
1848             d_cas += take_casualty(A_DEFEND, def, dlist);
1849             dtotal = get_dtotal(def, dlist, dsupport, 0);
1850             if (!dtotal)
1851                 ++success;
1852         } else {
1853             pr("@");
1854             a_cas += take_casualty(combat_mode, off, olist);
1855             ototal = get_ototal(combat_mode, off, olist, osupport, 0);
1856         }
1857         if (((a_cas + d_cas) % 70) == 69)
1858             pr("\n");
1859         if (recalctime-- <= 0) {
1860             recalctime = 8 + (random() % 43);
1861             odds = att_calcodds(ototal, dtotal);
1862             odds += (random() % 11 - 5) / 100.0;
1863             if (odds < 0.0)
1864                 odds = 0.1;
1865             if (odds > 1.0)
1866                 odds = 1.0;
1867         }
1868     }
1869     pr("\n");
1870     /* update defense mobility & mil */
1871     if (success)
1872         def->mil = 0;
1873     else {
1874         if (def->type == EF_SECTOR && d_mil && d_cas) {
1875             if (def->mob < 0)
1876                 def->mobcost = 0;
1877             else {
1878                 newmob = damage(def->mob, 100 * d_cas / d_mil);
1879                 def->mobcost = MIN(20, def->mob - newmob);
1880             }
1881         }
1882         def->mil = def->troops;
1883     }
1884
1885     /* update attack mobility & mil */
1886     for (n = 0; n <= off->last; ++n)
1887         if (off[n].type != EF_BAD && off[n].troops < a_troops[n]) {
1888             if (off[n].type == EF_SECTOR && off[n].mil) {
1889                 if (!CANT_HAPPEN(off[n].mob < 0)) {
1890                     newmob = damage(off[n].mob,
1891                                     100 * (a_troops[n] - off[n].troops)
1892                                     / off[n].mil);
1893                     off[n].mobcost += MIN(20, off[n].mob - newmob);
1894                 }
1895             }
1896             off[n].mil -= a_troops[n] - off[n].troops;
1897         }
1898
1899     /* update land unit mobility */
1900     if (d_bodies && d_cas)
1901         lnd_takemob(dlist, (double)d_cas / d_bodies);
1902     if (a_bodies && a_cas)
1903         lnd_takemob(olist, (double)a_cas / a_bodies);
1904
1905     /* damage attacked sector */
1906     def->eff = effdamage(def->eff, (d_cas + a_cas) / 10);
1907
1908     pr("- Casualties -\n     Yours: %d\n", a_cas);
1909     pr("    Theirs: %d\n", d_cas);
1910     pr("Papershuffling ... %.1f B.T.U\n", (d_cas + a_cas) * 0.15);
1911     player->btused += (int)((d_cas + a_cas) * 0.015 + 0.5);
1912
1913     if (success) {
1914         switch (combat_mode) {
1915         case A_ATTACK:
1916             news_item = def->own ? N_WON_SECT : N_TOOK_UNOCC;
1917             pr("We have captured %s, sir!\n", prcom(0, def));
1918             action = "taking";
1919             break;
1920         case A_ASSAULT:
1921             news_item = def->own ? N_AWON_SECT : N_START_COL;
1922             pr("We have secured a beachhead at %s, sir!\n", prcom(0, def));
1923             action = "assaulting and taking";
1924             break;
1925         case A_PARA:
1926             news_item = def->own ? N_PWON_SECT : N_PARA_UNOCC;
1927             pr("We have captured %s, sir!\n", prcom(0, def));
1928             action = "air-assaulting and taking";
1929             break;
1930         case A_BOARD:
1931             news_item = N_BOARD_SHIP;
1932             pr("We have boarded %s, sir!\n", prcom(0, def));
1933             action = "boarding";
1934             break;
1935         case A_LBOARD:
1936             news_item = N_BOARD_LAND;
1937             pr("We have boarded %s, sir!\n", prcom(0, def));
1938             action = "boarding";
1939             break;
1940         default:
1941             CANT_REACH();
1942             news_item = 0;
1943             action = "defeating";
1944         }
1945     } else {
1946         switch (combat_mode) {
1947         case A_ATTACK:
1948             news_item = N_SCT_LOSE;
1949             pr("You have been defeated!\n");
1950             action = "attacking";
1951             break;
1952         case A_ASSAULT:
1953             news_item = N_ALOSE_SCT;
1954             pr("You have been defeated!\n");
1955             kill_land(olist);
1956             action = "trying to assault";
1957             break;
1958         case A_PARA:
1959             news_item = N_PLOSE_SCT;
1960             pr("All of your troops were destroyed\n");
1961             action = "trying to air-assault";
1962             break;
1963         case A_BOARD:
1964             news_item = N_SHP_LOSE;
1965             pr("You have been repelled\n");
1966             kill_land(olist);
1967             action = "trying to board";
1968             break;
1969         case A_LBOARD:
1970             news_item = N_LND_LOSE;
1971             pr("You have been repelled\n");
1972             kill_land(olist);
1973             action = "trying to board";
1974             break;
1975         default:
1976             CANT_REACH();
1977             news_item = 0;
1978             action = "fighting";
1979         }
1980     }
1981     nreport(player->cnum, news_item, def->own, 1);
1982     if (def->own) {
1983         wu(0, def->own,
1984            "%s (#%d) lost %d troops %s %s\nWe lost %d troops defending\n",
1985            cname(player->cnum), player->cnum, a_cas,
1986            action, pr_com(0, def, def->own), d_cas);
1987     }
1988
1989     send_reacting_units_home(dlist);
1990
1991     /* putland the defending land */
1992     unit_put(dlist, 0);
1993
1994     /* putland the attacking land */
1995     put_oland(olist);
1996
1997     /* put the victim sector/ship/land */
1998     if (!success || !take_def(combat_mode, olist, off, def))
1999         put_combat(def);
2000
2001     /* put the attacking sectors/ship */
2002     for (n = 0; n <= off->last; ++n)
2003         if (off[n].type != EF_BAD)
2004             put_combat(&off[n]);
2005
2006     if (!success)
2007         return 0;
2008
2009     switch (combat_mode) {
2010     case A_ATTACK:
2011         ask_move_in(off, olist, def);
2012
2013         /* put sectors again to get abandon warnings */
2014         for (n = 0; n <= off->last; ++n)
2015             if (off[n].type != EF_BAD)
2016                 put_combat(&off[n]);
2017         break;
2018     default:
2019         att_move_in_off(combat_mode, off, olist, def);
2020     }
2021     if (def->mil > 0)
2022         pr("%d of your troops now occupy %s\n", def->mil, prcom(0, def));
2023     return 1;
2024 }
2025
2026 /* What percentage of the combat forces going head-to-head are we? */
2027
2028 static double
2029 att_calcodds(int ototal, int dtotal)
2030 {
2031     double odds;
2032
2033     /* calculate odds */
2034     if (ototal <= 0)
2035         odds = 0.0;
2036     else if (dtotal <= 0)
2037         odds = 1.0;
2038     else
2039         odds = (double)ototal / (dtotal + ototal);
2040
2041     return odds;
2042 }
2043
2044 /* Here's where the dead soldiers get dragged off the battlefield */
2045
2046 static int
2047 take_casualty(int combat_mode, struct combat *off, struct emp_qelem *olist)
2048 {
2049     int to_take = CASUALTY_LUMP;
2050     int biggest_troops = 0, index = -1;
2051     int n, tot_troops = 0, biggest_mil, cas;
2052     struct emp_qelem *qp, *biggest;
2053     struct ulist *llp;
2054
2055     for (n = 0; n <= off->last; ++n) {
2056         if (off[n].type != EF_BAD) {
2057             tot_troops += off[n].troops;
2058             if (off[n].troops > biggest_troops) {
2059                 biggest_troops = off[n].troops;
2060                 index = n;
2061             }
2062         }
2063     }
2064
2065     if (tot_troops)
2066         to_take -= tot_troops;
2067
2068     if (to_take >= 0) {
2069         for (n = 0; n <= off->last; ++n)
2070             if (off[n].type != EF_BAD)
2071                 off[n].troops = 0;
2072     } else {
2073         /*
2074          * They can all come off mil.  We rotate the casualties,
2075          * starting with the sector containing the most mil.
2076          */
2077         to_take = CASUALTY_LUMP;
2078         if (index < 0) {
2079             pr("ERROR: Tell the deity that you got the 'green librarian' error\n");
2080             index = 0;
2081         }
2082         while (to_take > 0) {
2083             for (n = index; n <= off->last && to_take; ++n) {
2084                 if (off[n].type != EF_BAD && off[n].troops > 0) {
2085                     --to_take;
2086                     --off[n].troops;
2087                 }
2088             }
2089             for (n = 0; n < index && to_take; ++n) {
2090                 if (off[n].type != EF_BAD && off[n].troops > 0) {
2091                     --to_take;
2092                     --off[n].troops;
2093                 }
2094             }
2095         }
2096         return CASUALTY_LUMP;
2097     }
2098
2099     if (QEMPTY(olist))
2100         return CASUALTY_LUMP - to_take;
2101
2102     /*
2103      *  Need to take some casualties from attacking units
2104      *  Procedure: find the biggest unit remaining (in
2105      *  terms of mil) and give it the casualties.
2106      */
2107     biggest = NULL;
2108     biggest_mil = -1;
2109     for (qp = olist->q_forw; qp != olist; qp = qp->q_forw) {
2110         llp = (struct ulist *)qp;
2111
2112         if (llp->unit.land.lnd_item[I_MILIT] > biggest_mil) {
2113             biggest_mil = llp->unit.land.lnd_item[I_MILIT];
2114             biggest = qp;
2115         }
2116     }
2117     if (biggest == NULL)
2118         return CASUALTY_LUMP - to_take;
2119
2120     llp = (struct ulist *)biggest;
2121     cas = lnd_take_casualty(combat_mode, llp, to_take);
2122     return CASUALTY_LUMP - (to_take - cas);
2123 }
2124
2125 /* Send reacting defense units back to where they came from (at no mob cost) */
2126
2127 static void
2128 send_reacting_units_home(struct emp_qelem *list)
2129 {
2130     struct emp_qelem *qp, *next;
2131     struct ulist *llp;
2132     char buf[1024];
2133
2134     for (qp = list->q_forw; qp != list; qp = next) {
2135         next = qp->q_forw;
2136         llp = (struct ulist *)qp;
2137         if ((llp->unit.land.lnd_x != llp->x) ||
2138             (llp->unit.land.lnd_y != llp->y)) {
2139             sprintf(buf, "returns to %s",
2140                     xyas(llp->x, llp->y, llp->unit.land.lnd_own));
2141             llp->unit.land.lnd_x = llp->x;
2142             llp->unit.land.lnd_y = llp->y;
2143             lnd_print(llp->unit.land.lnd_own, llp, buf);
2144             lnd_delete(llp);
2145         }
2146     }
2147 }
2148
2149 /* Check for 0 offense strength.  This call will always preceed an abort */
2150
2151 int
2152 att_empty_attack(int combat_mode, int ototal, struct combat *def)
2153 {
2154     if (ototal <= 0) {
2155         if (def->own && player->cnum != def->own) {
2156             wu(0, def->own,
2157                "%s (#%d) considered %sing you @%s\n",
2158                cname(player->cnum), player->cnum,
2159                att_mode[combat_mode], xyas(def->x, def->y, def->own));
2160         }
2161         pr("No troops for %s...\n", att_mode[combat_mode]);
2162         return 1;
2163     }
2164     return 0;
2165 }
2166
2167 /*
2168  * Take the defending sector or ship from the defender and give it to the
2169  * attacker.
2170  */
2171
2172 static int
2173 take_def(int combat_mode, struct emp_qelem *list, struct combat *off,
2174          struct combat *def)
2175 {
2176     int n;
2177     int occuppied = 0;
2178     struct ulist *llp, *delete_me = NULL;
2179     char buf[1024];
2180     struct sctstr sect;
2181     struct shpstr ship;
2182     struct lndstr land;
2183
2184     for (n = 0; n <= off->last && !occuppied; ++n) {
2185         if (off[n].type != EF_BAD &&
2186             off[n].troops > 0 &&
2187             (off[n].type != EF_SECTOR || off[n].mob)) {
2188             ++occuppied;
2189             if (def->type == EF_LAND) {
2190                 if (def->lnd_lcp->l_flags & L_SPY) {
2191                     continue;
2192                 }
2193             }
2194             --(off[n].troops);
2195             --(off[n].mil);
2196             ++def->mil;
2197             pr("1 mil from %s moves %s\n",
2198                prcom(0, off + n), prcom(2, def));
2199         }
2200     }
2201     if (!occuppied) {
2202         if (QEMPTY(list)) {
2203             pr("%s left unoccupied\n", prcom(0, def));
2204             if (def->own)
2205                 wu(0, def->own,
2206                    "No enemy troops moved %s so you still own it!\n",
2207                    pr_com(2, def, def->own));
2208             return 0;
2209         } else {
2210             llp = (struct ulist *)list->q_forw;
2211             llp->unit.land.lnd_x = def->x;
2212             llp->unit.land.lnd_y = def->y;
2213             take_move_in_mob(combat_mode, llp, off, def);
2214             if (def->type == EF_SHIP) {
2215                 llp->unit.land.lnd_ship = def->shp_uid;
2216                 sprintf(buf, "boards %s", prcom(0, def));
2217                 lnd_print(player->cnum, llp, buf);
2218                 delete_me = llp;
2219             } else {
2220                 llp->unit.land.lnd_ship = -1;
2221                 sprintf(buf, "moves in to occupy %s",
2222                         xyas(def->x, def->y, player->cnum));
2223                 lnd_print(player->cnum, llp, buf);
2224                 lnd_delete(llp);
2225             }
2226         }
2227     }
2228     put_combat(def);
2229     if (def->type == EF_SECTOR) {
2230         getsect(def->x, def->y, &sect);
2231         takeover(&sect, player->cnum);
2232         if (sect.sct_type == SCT_CAPIT || sect.sct_type == SCT_MOUNT)
2233             caploss(&sect, def->own,
2234                     "* We have captured %s's capital, sir! *\n");
2235         putsect(&sect);
2236     } else if (def->type == EF_SHIP) {
2237         getship(def->shp_uid, &ship);
2238         takeover_ship(&ship, player->cnum);
2239         putship(ship.shp_uid, &ship);
2240     } else if (def->type == EF_LAND) {
2241         getland(def->lnd_uid, &land);
2242         takeover_land(&land, player->cnum);
2243         putland(land.lnd_uid, &land);
2244     }
2245     if (delete_me)
2246         lnd_delete(delete_me);
2247     att_get_combat(def, 0);
2248     return 1;
2249 }
2250
2251 /*
2252  * Ask the attacker which mil & land units they'd like to move into the
2253  * conquered sector.
2254  */
2255
2256 static void
2257 ask_move_in(struct combat *off, struct emp_qelem *olist,
2258             struct combat *def)
2259 {
2260     int n;
2261     struct emp_qelem *qp, *next;
2262     struct ulist *llp;
2263     char buf[512];
2264     char prompt[512];
2265     char land_answer[256];
2266     char *answerp;
2267
2268     for (n = 0; n <= off->last; ++n)
2269         if (off[n].type != EF_BAD && off[n].troops > 0)
2270             if (off[n].mob) {
2271                 ask_move_in_off(&off[n], def);
2272                 if (player->aborted)
2273                     break;
2274             }
2275
2276     if (QEMPTY(olist))
2277         return;
2278     memset(land_answer, 0, sizeof(land_answer));
2279     for (qp = olist->q_forw; qp != olist; qp = next) {
2280         next = qp->q_forw;
2281         llp = (struct ulist *)qp;
2282         answerp = &land_answer[(int)llp->unit.land.lnd_army];
2283         if (player->aborted || att_get_combat(def, 0) < 0)
2284             *answerp = 'N';
2285         if (*answerp == 'Y')
2286             continue;
2287         if (!get_oland(A_ATTACK, llp))
2288             continue;
2289         if (*answerp != 'N') {
2290             sprintf(prompt, "Move in with %s (%c %d%%) [ynYNq?] ",
2291                     prland(&llp->unit.land),
2292                     llp->unit.land.lnd_army ? llp->unit.land.lnd_army : '~',
2293                     llp->unit.land.lnd_effic);
2294             *answerp = att_prompt(prompt, llp->unit.land.lnd_army);
2295             if (player->aborted || att_get_combat(def, 0) < 0)
2296                 *answerp = 'N';
2297             if (!get_oland(A_ATTACK, llp))
2298                 continue;
2299         }
2300         if (*answerp == 'y' || *answerp == 'Y')
2301             continue;
2302         sprintf(buf, "stays in %s",
2303                 xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y,
2304                      player->cnum));
2305         lnd_print(player->cnum, llp, buf);
2306         lnd_delete(llp);
2307     }
2308     if (QEMPTY(olist))
2309         return;
2310     if (att_get_combat(def, 0) < 0) {
2311         for (qp = olist->q_forw; qp != olist; qp = next) {
2312             next = qp->q_forw;
2313             llp = (struct ulist *)qp;
2314             if (!get_oland(A_ATTACK, llp))
2315                 continue;
2316             sprintf(buf, "stays in %s",
2317                     xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y,
2318                          player->cnum));
2319             lnd_print(player->cnum, llp, buf);
2320             lnd_delete(llp);
2321         }
2322         return;
2323     }
2324     if (opt_INTERDICT_ATT)
2325         lnd_interdict(olist, def->x, def->y, player->cnum);
2326     move_in_land(A_ATTACK, off, olist, def);
2327 }
2328
2329 /* Move offensive land units to the conquered sector or ship */
2330
2331 static void
2332 move_in_land(int combat_mode, struct combat *off, struct emp_qelem *olist,
2333              struct combat *def)
2334 {
2335     struct emp_qelem *qp, *next;
2336     struct ulist *llp;
2337     char buf[512];
2338
2339     if (QEMPTY(olist))
2340         return;
2341     for (qp = olist->q_forw; qp != olist; qp = next) {
2342         next = qp->q_forw;
2343         llp = (struct ulist *)qp;
2344         if (!get_oland(combat_mode, llp))
2345             continue;
2346         take_move_in_mob(combat_mode, llp, off, def);
2347         llp->unit.land.lnd_x = def->x;
2348         llp->unit.land.lnd_y = def->y;
2349         if (def->type == EF_SHIP)
2350             llp->unit.land.lnd_ship = def->shp_uid;
2351         else
2352             llp->unit.land.lnd_ship = -1;
2353     }
2354     if (QEMPTY(olist))
2355         return;
2356     if (def->type == EF_SECTOR) {
2357         if (opt_INTERDICT_ATT) {
2358             lnd_sweep(olist, 0, 0, player->cnum);
2359             lnd_check_mines(olist);
2360         }
2361         sprintf(buf, "now occupies %s", prcom(0, def));
2362     } else {
2363         sprintf(buf, "boards %s", prcom(0, def));
2364     }
2365     if (QEMPTY(olist))
2366         return;
2367     for (qp = olist->q_forw; qp != olist; qp = next) {
2368         next = qp->q_forw;
2369         llp = (struct ulist *)qp;
2370         lnd_print(player->cnum, llp, buf);
2371     }
2372     if (QEMPTY(olist))
2373         return;
2374     unit_put(olist, 0);
2375 }
2376
2377 /*
2378  * Move assaulting, paradropping, or boarding mil & units into def
2379  * If the mil are coming from a ship, then pack a lunch.
2380  */
2381
2382 void
2383 att_move_in_off(int combat_mode, struct combat *off,
2384                 struct emp_qelem *olist, struct combat *def)
2385 {
2386     struct sctstr sect;
2387     struct shpstr ship;
2388     int troops, n;
2389     int lunchbox = 0;
2390
2391     move_in_land(combat_mode, off, olist, def);
2392
2393     for (n = 0; n <= off->last; ++n) {
2394         if (off[n].type == EF_BAD || !off[n].troops)
2395             continue;
2396         troops = off[n].troops;
2397         off[n].troops = 0;
2398         off[n].mil -= troops;
2399         def->mil += troops;
2400         put_combat(off + n);
2401         if (combat_mode == A_ASSAULT) {
2402             if (CANT_HAPPEN(off[n].type != EF_SHIP))
2403                 continue;
2404             getship(off[n].shp_uid, &ship);
2405             lunchbox += (int)((troops + 1) * ship.shp_item[I_FOOD]
2406                               / (ship.shp_item[I_MILIT] + troops
2407                                  + ship.shp_item[I_CIVIL] + 0.5));
2408
2409             ship.shp_item[I_FOOD] -= lunchbox;
2410             putship(ship.shp_uid, &ship);
2411         }
2412     }
2413
2414     put_combat(def);
2415
2416     if (combat_mode == A_ASSAULT) {
2417         if (CANT_HAPPEN(def->type != EF_SECTOR))
2418             return;
2419         getsect(def->x, def->y, &sect);
2420         if (lunchbox > ITEM_MAX - sect.sct_item[I_FOOD])
2421             lunchbox = ITEM_MAX - sect.sct_item[I_FOOD];
2422         sect.sct_item[I_FOOD] += lunchbox;
2423         putsect(&sect);
2424     }
2425 }
2426
2427
2428 /* Ask how many mil to move in from each sector */
2429
2430 static void
2431 ask_move_in_off(struct combat *off, struct combat *def)
2432 {
2433     int mob_support;
2434     int num_mil, dam = 0, left;
2435     double d, weight;
2436     char prompt[512];
2437     char buf[1024];
2438     char *p;
2439
2440     if (att_get_combat(off, 0) <= 0)
2441         return;
2442     if (att_get_combat(def, 0) < 0)
2443         return;
2444     if (off->own != player->cnum)
2445         return;
2446     d = att_mobcost(off->own, def, MOB_MOVE);
2447     if ((mob_support = MIN(off->troops, (int)(off->mob / d))) <= 0)
2448         return;
2449     sprintf(prompt, "How many mil to move in from %s (%d max)? ",
2450             xyas(off->x, off->y, player->cnum), mob_support);
2451     p = getstring(prompt, buf);
2452     if (!p || !*p || (num_mil = atoi(p)) <= 0)
2453         return;
2454 /* Make sure we don't move in more than we can support mobility-wise */
2455     if (num_mil > mob_support)
2456         num_mil = mob_support;
2457     if (att_get_combat(off, 0) <= 0)
2458         return;
2459     if (att_get_combat(def, 0) < 0)
2460         return;
2461     if ((num_mil = MIN(off->troops, num_mil)) <= 0) {
2462         pr("No mil moved in from %s\n",
2463            xyas(off->x, off->y, player->cnum));
2464         return;
2465     }
2466     mob_support = MAX(1, (int)(num_mil * d));
2467     off->mob -= MIN(off->mob, mob_support);
2468     off->mil -= num_mil;
2469     off->troops -= num_mil;
2470     put_combat(off);
2471     left = num_mil;
2472     weight = (double)num_mil * ichr[I_MILIT].i_lbs;
2473     if (opt_INTERDICT_ATT && chance(weight / 200.0)) {
2474         if (chance(weight / 100.0))
2475             dam +=
2476                 ground_interdict(def->x, def->y, player->cnum, "military");
2477         dam += check_lmines(def->x, def->y, weight);
2478     }
2479
2480     if (dam) {
2481         left = commdamage(num_mil, dam, I_MILIT);
2482         if (left < num_mil) {
2483             if (left) {
2484                 pr("%d of the mil you were moving were destroyed!\n"
2485                    "Only %d mil made it to %s\n",
2486                    num_mil - left, left,
2487                    xyas(def->x, def->y, player->cnum));
2488             } else {
2489                 pr("All of the mil you were moving were destroyed!\n");
2490             }
2491         }
2492         /* maybe got nuked */
2493         if (att_get_combat(def, 0) < 0)
2494             return;
2495     }
2496     def->mil += left;
2497     put_combat(def);
2498 }
2499
2500
2501 /* Charge land units for moving into a sector or onto a ship */
2502
2503 static void
2504 take_move_in_mob(int combat_mode, struct ulist *llp, struct combat *off,
2505                  struct combat *def)
2506 {
2507     double mob = llp->unit.land.lnd_mobil;
2508     double gain = etu_per_update * land_mob_scale;
2509     double mobcost, moblim;
2510
2511     switch (combat_mode) {
2512     case A_ATTACK:
2513         mobcost = lnd_pathcost(&llp->unit.land,
2514                                att_mobcost(llp->unit.land.lnd_own, def,
2515                                            lnd_mobtype(&llp->unit.land)));
2516         break;
2517     case A_ASSAULT:
2518         /*
2519          * Set mobcost to basic assault cost, moblim to maximum
2520          * mobility to keep when assaulting from non-landing ship
2521          */
2522         if (((struct lchrstr *)llp->chrp)->l_flags & L_MARINE) {
2523             mobcost = gain / 2.0;
2524             moblim = 0;
2525         } else {
2526             mobcost = gain;
2527             moblim = -gain;
2528         }
2529         if (!(off->shp_mcp->m_flags & M_LAND))
2530             /* Not a landing ship, ensure we go to or below moblim */
2531             mobcost = MAX(mobcost, mob - moblim);
2532         break;
2533     case A_BOARD:
2534         if (((struct lchrstr *)llp->chrp)->l_flags & L_MARINE)
2535             mobcost = 10;
2536         else
2537             mobcost = 40;
2538         break;
2539     default:
2540         CANT_REACH();
2541         mobcost = 0;
2542     }
2543
2544     mob -= mobcost;
2545     if (mob < -127.0)
2546         mob = -127.0;
2547     llp->unit.land.lnd_mobil = (signed char)mob;
2548     llp->unit.land.lnd_harden = 0;
2549 }
2550
2551 static void
2552 free_list(struct emp_qelem *list)
2553 {
2554     struct emp_qelem *qp, *next;
2555
2556     if (!list || QEMPTY(list))
2557         return;
2558
2559     qp = list->q_forw;
2560     while (qp != list) {
2561         next = qp->q_forw;
2562         emp_remque(qp);
2563         free(qp);
2564         qp = next;
2565     }
2566 }
2567
2568 int
2569 att_free_lists(struct emp_qelem *olist, struct emp_qelem *dlist)
2570 {
2571     free_list(olist);
2572     free_list(dlist);
2573     return RET_OK;
2574 }
2575
2576 /*
2577  * sector_strength - Everyone starts at 1.  You can get up to a max
2578  *                   of d_dstr, depending on how much you build up the
2579  *                   defenses of the sector.
2580  */
2581
2582 double
2583 sector_strength(struct sctstr *sp)
2584 {
2585     double def = SCT_DEFENSE(sp) / 100.0;
2586     double base = sp->sct_type == SCT_MOUNT ? 2.0 : 1.0;
2587     double d = base + (dchr[sp->sct_type].d_dstr - base) * def;
2588
2589     if (d > dchr[sp->sct_type].d_dstr)
2590         d = dchr[sp->sct_type].d_dstr;
2591     if (d < base)
2592         d = base;
2593     return d;
2594 }