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