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