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