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