]> git.pond.sub.org Git - empserver/blob - src/lib/subs/attsub.c
assault: Don't send spies into combat
[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         /*
1045          * We need to let spies assault even though they have no
1046          * offensive strength, because assault is how they sneak
1047          * ashore.  If this assault turns out to be a fight, they'll
1048          * be removed by get_ototal().
1049          */
1050         if (att_val < 1.0
1051             && !(combat_mode == A_ASSAULT && (lcp->l_flags & L_SPY))) {
1052             pr("%s has no offensive strength\n", prland(&land));
1053             continue;
1054         }
1055         if (!lnd_supply_all(&land)) {
1056             pr("%s is out of supply, and cannot %s\n",
1057                prland(&land), att_mode[combat_mode]);
1058             continue;
1059         }
1060         if (def->type == EF_SHIP && first_time) {
1061             first_time = 0;
1062             pr("You may board with a maximum of %d land units\n", maxland);
1063         }
1064         pr("%s has a base %s value of %.0f\n",
1065            prland(&land), att_mode[combat_mode], att_val);
1066         if (land_answer[(int)land.lnd_army] != 'Y') {
1067             sprintf(prompt,
1068                     "%s with %s %s (%c %d%%) [ynYNq?] ",
1069                     att_mode[combat_mode],
1070                     prland(&land),
1071                     prcom(1, off),
1072                     land.lnd_army ? land.lnd_army : '~',
1073                     land.lnd_effic);
1074             land_answer[(int)land.lnd_army] =
1075                 att_prompt(prompt, land.lnd_army);
1076             if (att_abort(combat_mode, off, def))
1077                 return;
1078             if (land_answer[(int)land.lnd_army] != 'y' &&
1079                 land_answer[(int)land.lnd_army] != 'Y')
1080                 continue;
1081         }
1082         llp = lnd_insque(&land, olist);
1083         llp->supplied = 1;
1084         llp->mobil = mobcost;
1085         llp->x = llp->unit.land.lnd_x;
1086         llp->y = llp->unit.land.lnd_y;
1087         llp->eff = llp->unit.land.lnd_effic;
1088         if (lnd_spyval(&land) > *a_spyp)
1089             *a_spyp = lnd_spyval(&land);
1090         if (lchr[land.lnd_type].l_flags & L_ENGINEER)
1091             ++*a_engineerp;
1092         if (def->type == EF_SHIP && ++count >= maxland)
1093             break;
1094     }
1095 }
1096
1097 /* What's the offense or defense multiplier? */
1098
1099 double
1100 att_combat_eff(struct combat *com)
1101 {
1102     double eff = 1.0;
1103     double str;
1104     struct shpstr ship;
1105
1106     if (com->type == EF_SECTOR) {
1107         eff = com->eff / 100.0;
1108         if (com->own == player->cnum) {
1109             str = com->sct_dcp->d_ostr;
1110             eff = 1.0 + (str - 1.0) * eff;
1111         } else
1112             eff = sector_strength(getsectp(com->x, com->y));
1113     } else if (com->type == EF_SHIP && com->own != player->cnum) {
1114         getship(com->shp_uid, &ship);
1115         eff = 1.0 + shp_armor(&ship) / 100.0;
1116     }
1117     return eff;
1118 }
1119
1120 int
1121 att_get_offense(int combat_mode, struct combat *off,
1122                 struct emp_qelem *olist, struct combat *def)
1123 {
1124     int ototal;
1125
1126     /*
1127      * Get the attacker units & mil again in case they changed while the
1128      * attacker was answering sub-prompts.
1129      */
1130
1131     ototal = get_ototal(combat_mode, off, olist, 1.0, 1);
1132     if (att_empty_attack(combat_mode, ototal, def))
1133         return abort_attack();
1134     if (combat_mode == A_PARA)
1135         return ototal;
1136     pr("\n             Initial attack strength: %8d\n", ototal);
1137     return ototal;
1138 }
1139
1140 /* Get the defensive units and reacting units */
1141 int
1142 att_get_defense(struct emp_qelem *olist, struct combat *def,
1143                 struct emp_qelem *dlist, int a_spy, int ototal)
1144 {
1145     int d_spy = 0;
1146     struct emp_qelem *qp;
1147     struct ulist *llp;
1148     int dtotal;
1149     int old_dtotal;
1150
1151     emp_initque(dlist);
1152     get_dlist(def, dlist, 0, &d_spy);
1153     dtotal = get_dtotal(def, dlist, 1.0, 0);
1154
1155     /*
1156      * Call in reacting units
1157      */
1158
1159     if (def->type == EF_SECTOR && def->sct_type != SCT_MOUNT)
1160         att_reacting_units(def, dlist, a_spy, &d_spy, ototal);
1161
1162     for (qp = olist->q_forw; qp != olist; qp = qp->q_forw) {
1163         llp = (struct ulist *)qp;
1164         intelligence_report(def->own, &llp->unit.land, d_spy,
1165                             "Scouts report attacking unit:");
1166     }
1167
1168     old_dtotal = dtotal;
1169     dtotal = get_dtotal(def, dlist, 1.0, 0);
1170     if (dtotal != old_dtotal)
1171         pr("Defense strength with reacting units: %8d\n", dtotal);
1172
1173     return dtotal;
1174 }
1175
1176 /* Get the defensive land units in the sector or on the ship */
1177
1178 static void
1179 get_dlist(struct combat *def, struct emp_qelem *list, int a_spy,
1180           int *d_spyp)
1181 {
1182     struct nstr_item ni;
1183     struct ulist *llp;
1184     struct lndstr land;
1185
1186 /* In here is where you need to take out spies and trains from the defending
1187    lists.  Spies try to hide, trains get trapped and can be boarded. */
1188
1189     snxtitem_xy(&ni, EF_LAND, def->x, def->y);
1190     while (nxtitem(&ni, &land)) {
1191         if (!land.lnd_own)
1192             continue;
1193         if (land.lnd_own != def->own)
1194             continue;
1195         if (def->type == EF_SECTOR && land.lnd_ship >= 0)
1196             continue;
1197         if (def->type == EF_SECTOR && land.lnd_land >= 0)
1198             continue;
1199         if (def->type == EF_SECTOR && (lchr[land.lnd_type].l_flags & L_SPY))
1200             continue;
1201         if (def->type == EF_SHIP && land.lnd_ship != def->shp_uid)
1202             continue;
1203         if (def->type == EF_LAND && land.lnd_land != def->lnd_uid)
1204             continue;
1205         intelligence_report(player->cnum, &land, a_spy,
1206                             "Scouts report defending unit:");
1207         llp = lnd_insque(&land, list);
1208         llp->supplied = lnd_supply_all(&land);
1209         llp->mobil = 0.0;
1210         llp->x = llp->unit.land.lnd_x;
1211         llp->y = llp->unit.land.lnd_y;
1212         llp->eff = llp->unit.land.lnd_effic;
1213         if (lnd_spyval(&land) > *d_spyp)
1214             *d_spyp = lnd_spyval(&land);
1215     }
1216 }
1217
1218 /* Calculate the total offensive strength */
1219
1220 static int
1221 get_ototal(int combat_mode, struct combat *off, struct emp_qelem *olist,
1222            double osupport, int check)
1223 {
1224     double ototal = 0.0;
1225     struct emp_qelem *qp, *next;
1226     struct ulist *llp;
1227     int n, w;
1228     double att_val;
1229
1230     /*
1231      * first, total the attacking mil
1232      */
1233
1234     for (n = 0; n <= off->last; ++n) {
1235         if (off[n].type == EF_BAD || (check &&
1236                                       att_get_combat(&off[n], 0) <= 0))
1237             continue;
1238         ototal += off[n].troops * att_combat_eff(off + n);
1239     }
1240
1241     /*
1242      * next, add in the attack_values of all
1243      * the attacking units
1244      */
1245
1246     for (qp = olist->q_forw; qp != olist; qp = next) {
1247         next = qp->q_forw;
1248         llp = (struct ulist *)qp;
1249         if (check && !get_oland(combat_mode, llp))
1250             continue;
1251         att_val = attack_val(combat_mode, &llp->unit.land);
1252         if (check && att_val < 1.0) {
1253             /*
1254              * No offensive strength, and fighting hasn't even begun.
1255              * Since ask_olist() doesn't offer such land units, except
1256              * for spies sometimes, it's either a spy, or the strength
1257              * must have been destroyed since then.  Leave it behind.
1258              */
1259             lnd_print(player->cnum, llp, "has no offensive strength");
1260             lnd_put_one(llp);
1261             continue;
1262         }
1263         if (combat_mode == A_ATTACK) {
1264             w = -1;
1265             for (n = 0; n <= off->last; ++n) {
1266                 if (off[n].type == EF_BAD)
1267                     continue;
1268                 if ((off[n].x == llp->unit.land.lnd_x) &&
1269                     (off[n].y == llp->unit.land.lnd_y))
1270                     w = n;
1271             }
1272             if (w < 0) {
1273                 lnd_print(player->cnum, llp,
1274                           "can't attack from this sector now");
1275                 lnd_put_one(llp);
1276                 continue;
1277             }
1278             att_val *= att_combat_eff(off + w);
1279         }
1280         ototal += att_val;
1281     }
1282     ototal *= osupport;
1283
1284     return ldround(ototal, 1);
1285 }
1286
1287 /* Calculate the total defensive strength */
1288
1289 static int
1290 get_dtotal(struct combat *def, struct emp_qelem *list, double dsupport,
1291            int check)
1292 {
1293     double dtotal = 0.0, eff = 1.0, d_unit;
1294     struct emp_qelem *qp, *next;
1295     struct ulist *llp;
1296
1297     if (check && att_get_combat(def, 1) < 0)
1298         return 0;
1299     eff = att_combat_eff(def);
1300     dtotal = def->troops * eff;
1301
1302     /*
1303      * next, add in the defense_values of all
1304      * the defending non-retreating units
1305      */
1306
1307     for (qp = list->q_forw; qp != list; qp = next) {
1308         next = qp->q_forw;
1309         llp = (struct ulist *)qp;
1310         if (check && !get_dland(def, llp))
1311             continue;
1312         d_unit = defense_val(&llp->unit.land);
1313         if (!llp->supplied)
1314             d_unit /= 2.0;
1315         dtotal += d_unit * eff;
1316     }
1317
1318     dtotal *= dsupport;
1319
1320     return ldround(dtotal, 1);
1321 }
1322
1323 /*
1324  * This is the land unit integrity check.
1325  */
1326
1327 static int
1328 get_oland(int combat_mode, struct ulist *llp)
1329 {
1330     struct lndstr *lp = &llp->unit.land;
1331     char buf[512];
1332
1333     getland(llp->unit.land.lnd_uid, lp);
1334
1335     if (lp->lnd_own != player->cnum) {
1336         sprintf(buf, "was destroyed and is no longer a part of the %s",
1337                 att_mode[combat_mode]);
1338         lnd_print(player->cnum, llp, buf);
1339         lnd_put_one(llp);
1340         return 0;
1341     }
1342     if (lp->lnd_x != llp->x || lp->lnd_y != llp->y) {
1343         sprintf(buf,
1344                 "left to fight another battle and is no longer a part of the %s",
1345                 att_mode[combat_mode]);
1346         lnd_print(player->cnum, llp, buf);
1347         lnd_put_one(llp);
1348         return 0;
1349     }
1350     if (lp->lnd_effic < llp->eff) {
1351         sprintf(buf, "damaged from %d%% to %d%%",
1352                 llp->eff, lp->lnd_effic);
1353         lnd_print(player->cnum, llp, buf);
1354     }
1355
1356     llp->eff = llp->unit.land.lnd_effic;
1357     return 1;
1358 }
1359
1360 static int
1361 get_dland(struct combat *def, struct ulist *llp)
1362 {
1363     struct lndstr *lp = &llp->unit.land;
1364
1365     getland(llp->unit.land.lnd_uid, lp);
1366
1367     if (lp->lnd_own != def->own) {
1368         lnd_print(llp->unit.land.lnd_own, llp,
1369                   "was destroyed and is no longer a part of the defense");
1370         lnd_put_one(llp);
1371         return 0;
1372     }
1373     if (lp->lnd_x != def->x || lp->lnd_y != def->y) {
1374         lnd_print(llp->unit.land.lnd_own, llp,
1375                   "left to go fight another battle and is no longer a part of the defense");
1376         lnd_put_one(llp);
1377         return 0;
1378     }
1379
1380     llp->eff = llp->unit.land.lnd_effic;
1381     return 1;
1382 }
1383
1384 static void
1385 kill_land(struct emp_qelem *list)
1386 {
1387     struct emp_qelem *qp, *next;
1388     struct ulist *llp;
1389
1390     for (qp = list->q_forw; qp != list; qp = next) {
1391         next = qp->q_forw;
1392         llp = (struct ulist *)qp;
1393         if (llp->unit.land.lnd_ship >= 0) {
1394             llp->unit.land.lnd_effic = 0;
1395             lnd_print(player->cnum, llp,
1396                       "cannot return to the ship, and dies!");
1397             lnd_put_one(llp);
1398         }
1399     }
1400 }
1401
1402 static void
1403 att_infect_units(struct emp_qelem *list, int plague)
1404 {
1405     struct emp_qelem *qp, *next;
1406     struct ulist *llp;
1407
1408     if (!plague)
1409         return;
1410     for (qp = list->q_forw; qp != list; qp = next) {
1411         next = qp->q_forw;
1412         llp = (struct ulist *)qp;
1413         if (llp->unit.land.lnd_pstage == PLG_HEALTHY)
1414             llp->unit.land.lnd_pstage = PLG_EXPOSED;
1415     }
1416 }
1417
1418 /*
1419  * Put the land unit on the disk.  If there was some mobility cost, then
1420  * subtract it from the units mobility.  Note that this works the same way
1421  * as sectors & ships in that no mobility is actually taken until the attacker
1422  * has committed to attacking.
1423  */
1424
1425 static void
1426 put_oland(struct emp_qelem *list)
1427 {
1428     struct emp_qelem *qp, *next;
1429     struct ulist *llp;
1430
1431     for (qp = list->q_forw; qp != list; qp = next) {
1432         next = qp->q_forw;
1433         llp = (struct ulist *)qp;
1434         llp->unit.land.lnd_mission = 0;
1435         llp->unit.land.lnd_harden = 0;
1436         llp->unit.land.lnd_mobil -= (int)llp->mobil;
1437         llp->mobil = 0.0;
1438         putland(llp->unit.land.lnd_uid, &llp->unit.land);
1439         if (llp->unit.land.lnd_own != player->cnum) {
1440             emp_remque(&llp->queue);
1441             free(llp);
1442         } else
1443             get_oland(A_ATTACK, llp);
1444     }
1445 }
1446
1447 /*
1448  * Keep sending in reinforcements until it looks like we're going to win.
1449  * Note that the "strength" command also calls this routine.
1450  */
1451
1452 double
1453 att_reacting_units(struct combat *def, struct emp_qelem *list, int a_spy,
1454                    int *d_spyp, int ototal)
1455 {
1456     struct nstr_item ni;
1457     struct lndstr land;
1458     struct ulist *llp;
1459     int dtotal;
1460     double new_land = 0;
1461     double mobcost;
1462     double pathcost;
1463     int origx, origy;
1464     double eff = att_combat_eff(def);
1465
1466     if (list)
1467         dtotal = get_dtotal(def, list, 1.0, 1);
1468     else
1469         dtotal = 0;
1470     snxtitem_all(&ni, EF_LAND);
1471     while (nxtitem(&ni, &land) && dtotal + new_land * eff < 1.2 * ototal) {
1472         if (!land.lnd_own)
1473             continue;
1474         if (land.lnd_mission != MI_RESERVE)
1475             continue;
1476         if ((land.lnd_x == def->x) && (land.lnd_y == def->y))
1477             continue;
1478         if (land.lnd_own != def->own)
1479             continue;
1480         if (land.lnd_ship >= 0)
1481             continue;
1482         if (land.lnd_land >= 0)
1483             continue;
1484         if (defense_val(&land) < 1.0)
1485             continue;
1486         if (!lnd_can_attack(&land))
1487             continue;
1488
1489         /* Only supplied units can react */
1490         if (list ? !lnd_supply_all(&land) : !lnd_could_be_supplied(&land))
1491             continue;
1492
1493         if (!in_oparea((struct empobj *)&land, def->x, def->y))
1494             continue;
1495
1496         pathcost = path_find(land.lnd_x, land.lnd_y, def->x, def->y,
1497                             def->own, lnd_mobtype(&land));
1498         if (pathcost < 0)
1499             continue;
1500         mobcost = lnd_pathcost(&land, pathcost);
1501         if (land.lnd_mobil < mobcost)
1502             continue;
1503
1504         new_land += defense_val(&land);
1505
1506         if (!list)              /* we are in the "strength" command */
1507             continue;
1508
1509         /* move to defending sector */
1510         land.lnd_mobil -= ldround(mobcost, 1);
1511         origx = land.lnd_x;
1512         origy = land.lnd_y;
1513         land.lnd_x = def->x;
1514         land.lnd_y = def->y;
1515         putland(land.lnd_uid, &land);
1516         wu(0, land.lnd_own, "%s reacts to %s.\n",
1517            prland(&land), xyas(land.lnd_x, land.lnd_y, land.lnd_own));
1518
1519         llp = lnd_insque(&land, list);
1520         llp->supplied = 1;
1521         llp->mobil = 0.0;
1522         llp->x = origx;
1523         llp->y = origy;
1524         llp->eff = land.lnd_effic;
1525         if (lnd_spyval(&land) > *d_spyp)
1526             *d_spyp = lnd_spyval(&land);
1527
1528         intelligence_report(player->cnum, &land, a_spy,
1529                             "Scouts sight reacting enemy unit:");
1530     }
1531     return new_land;
1532 }
1533
1534 /* Pop off shells and fly bombing missions to get your attack multiplier up */
1535
1536 static double
1537 get_osupport(char *outs, struct combat *def, int fort_sup, int ship_sup,
1538              int land_sup, int plane_sup)
1539 {
1540     double osupport = 1.0;
1541     int dam;
1542     double af, as, au, ap;
1543
1544     af = as = au = ap = 0.0;
1545     if (fort_sup) {
1546         dam = dd(def->own, player->cnum, def->x, def->y, 0, 0);
1547         af = dam / 100.0;
1548         osupport += af;
1549     }
1550     if (ship_sup) {
1551         dam = sd(def->own, player->cnum, def->x, def->y, 0, 0, 0);
1552
1553         as = dam / 100.0;
1554         osupport += as;
1555     }
1556
1557     if (land_sup) {
1558         dam = lnd_support(def->own, player->cnum, def->x, def->y, 0);
1559         au = dam / 100.0;
1560         osupport += au;
1561     }
1562
1563     if (plane_sup) {
1564         dam = off_support(def->x, def->y, def->own, player->cnum);
1565         ap = dam / 100.0;
1566         osupport += ap;
1567     }
1568     sprintf(outs, "attacker\t%1.2f\t%1.2f\t%1.2f\t%1.2f", af, as, au, ap);
1569     return osupport;
1570 }
1571
1572 /* Pop off shells and fly bombing missions to get your defense multiplier up */
1573
1574 static double
1575 get_dsupport(char *outs, struct emp_qelem *list, struct combat *def,
1576              int ototal, int dtotal)
1577 {
1578     double dsupport = 1.0;
1579     int dam;
1580     double df, ds, du, dp;
1581     int good = 0;
1582
1583     df = ds = du = dp = 0.0;
1584     if (dtotal < 0.1 * ototal) {
1585         good = -1;
1586     } else if (dtotal >= 1.2 * ototal) {
1587         good = 1;
1588     } else {
1589         dam = dd(player->cnum, def->own, def->x, def->y, 0, 1);
1590         df = dam / 100.0;
1591         dsupport += df;
1592
1593         dtotal = get_dtotal(def, list, dsupport, 0);
1594         if (dtotal < 1.2 * ototal) {
1595             dam = sd(player->cnum, def->own, def->x, def->y, 0, 1, 0);
1596             ds = dam / 100.0;
1597             dsupport += ds;
1598             dtotal = get_dtotal(def, list, dsupport, 0);
1599         }
1600         if (dtotal < 1.2 * ototal) {
1601             dam = lnd_support(player->cnum, def->own, def->x, def->y, 1);
1602             du = dam / 100.0;
1603             dsupport += du;
1604             dtotal = get_dtotal(def, list, dsupport, 1);
1605         }
1606         if (dtotal < 1.2 * ototal) {
1607             dam = def_support(def->x, def->y, player->cnum, def->own);
1608             dp = dam / 100.0;
1609             dsupport += dp;
1610         }
1611     }
1612     if (good)
1613         *outs = '\0';
1614     else
1615         sprintf(outs, "defender\t%1.2f\t%1.2f\t%1.2f\t%1.2f",
1616                 df, ds, du, dp);
1617     if (def->own) {
1618         if (good < 0)
1619             wu(0, def->own,
1620                "\nOdds are bad for us...support cancelled.\n\n");
1621         else if (good > 0)
1622             wu(0, def->own,
1623                "\nOdds are good for us...support cancelled.\n\n");
1624     }
1625     return dsupport;
1626 }
1627
1628 /*
1629  * Land mines add to the defense multiplier.  If the attacker has engineers
1630  * then this multiplier is cut in half.
1631  */
1632
1633 static double
1634 get_mine_dsupport(struct combat *def, int a_engineer)
1635 {
1636     int mines;
1637     struct sctstr sect;
1638
1639     getsect(def->x, def->y, &sect);
1640
1641     if (sect.sct_oldown != player->cnum) {
1642         mines = SCT_LANDMINES(&sect);
1643         mines = MIN(mines, 20);
1644         if (a_engineer)
1645             mines = ldround(mines / 2.0, 1);
1646         if (mines > 0) {
1647             if (def->own)
1648                 wu(0, def->own, "Defending mines add %1.2f\n",
1649                    mines * 0.02);
1650             pr("Defending mines add %1.2f\n", mines * 0.02);
1651             return mines * 0.02;
1652         }
1653     }
1654     return 0.0;
1655 }
1656
1657 /* Get the offensive and defensive support */
1658 int
1659 att_get_support(int combat_mode, int ofort, int oship, int oland,
1660                 int oplane, struct emp_qelem *olist, struct combat *off,
1661                 struct emp_qelem *dlist, struct combat *def,
1662                 double *osupportp, double *dsupportp, int a_engineer)
1663 {
1664     int ototal, dtotal;
1665     char osupports[512];
1666     char dsupports[512];
1667
1668     if (combat_mode == A_PARA)
1669         *osupports = '\0';
1670     else
1671         *osupportp = get_osupport(osupports, def,
1672                                   ofort, oship, oland, oplane);
1673
1674     /*
1675      * I need to put a 1 at the end of the next four total_stren calls
1676      * because units & mil may have been damaged by collateral damage or
1677      * nuclear warheads from the offensive & defensive support.
1678      */
1679
1680     ototal = get_ototal(combat_mode, off, olist, *osupportp, 1);
1681     if (att_empty_attack(combat_mode, ototal, def))
1682         return abort_attack();
1683     dtotal = get_dtotal(def, dlist, *dsupportp, 1);
1684
1685     /*
1686      * Calculate defensive support.  If odds are too good or too bad
1687      * then don't call in support.
1688      */
1689
1690     *dsupportp = get_dsupport(dsupports, dlist, def, ototal, dtotal);
1691     ototal = get_ototal(combat_mode, off, olist, *osupportp, 1);
1692     if (att_empty_attack(combat_mode, ototal, def))
1693         return abort_attack();
1694
1695     if ((*osupports || *dsupports) &&
1696         (*osupportp != 1.0 || *dsupportp != 1.0)) {
1697         pr("\n\t\tsupport values\n");
1698         pr("\t\tforts\tships\tunits\tplanes\n");
1699         if (*osupportp != 1.0)
1700             pr("%s\n", osupports);
1701         if (*dsupportp != 1.0)
1702             pr("%s\n", dsupports);
1703         pr("\n");
1704         if (def->own) {
1705             wu(0, def->own, "\n\t\tsupport values\n");
1706             wu(0, def->own, "\t\tforts\tships\tunits\tplanes\n");
1707             if (*osupportp != 1.0)
1708                 wu(0, def->own, "%s\n", osupports);
1709             if (*dsupportp != 1.0)
1710                 wu(0, def->own, "%s\n", dsupports);
1711             wu(0, def->own, "\n");
1712         }
1713     }
1714
1715     dtotal = get_dtotal(def, dlist, *dsupportp, 1);
1716     if (dtotal && def->type == EF_SECTOR)
1717         *dsupportp += get_mine_dsupport(def, a_engineer);
1718     return 0;
1719 }
1720
1721 /* How many two-legged bipeds are in this combat force? */
1722
1723 static int
1724 count_bodies(struct combat *off, struct emp_qelem *list)
1725 {
1726     int n;
1727     int bodies = 0;
1728     struct emp_qelem *qp;
1729     struct ulist *llp;
1730
1731     for (n = 0; n <= off->last; ++n)
1732         bodies += off[n].troops;
1733     for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
1734         llp = (struct ulist *)qp;
1735         bodies += llp->unit.land.lnd_item[I_MILIT];
1736     }
1737     return bodies;
1738 }
1739
1740 /* This is where the fighting actually occurs. */
1741
1742 int
1743 att_fight(int combat_mode, struct combat *off, struct emp_qelem *olist,
1744           double osupport, struct combat *def, struct emp_qelem *dlist,
1745           double dsupport)
1746 {
1747     int success = 0;
1748     int a_cas = 0;              /* Casualty counts */
1749     int d_cas = 0;
1750     int ototal;                 /* total attacking strength */
1751     int dtotal;                 /* total defending strength */
1752     int a_bodies;               /* total attacking mil (incl. mil in units) */
1753     int d_bodies;               /* total defending mil (incl. mil in units) */
1754     int d_mil;
1755     int a_troops[6];
1756     int n;
1757     int news_item;
1758     int recalctime;
1759     double odds;
1760     int newmob;
1761     char *action;
1762
1763     ototal = get_ototal(combat_mode, off, olist, osupport,
1764                         combat_mode != A_PARA);
1765     dtotal = get_dtotal(def, dlist, dsupport, 0);
1766     if (!dtotal)
1767         success = 1;
1768
1769     a_bodies = count_bodies(off, olist);
1770     d_bodies = count_bodies(def, dlist);
1771     d_mil = def->troops;
1772     for (n = 0; n <= off->last; ++n)
1773         if (off[n].type == EF_BAD)
1774             a_troops[n] = 0;
1775         else
1776             a_troops[n] = off[n].troops;
1777
1778     /* This switch is required to get the spacing right */
1779     switch (combat_mode) {
1780     case A_ATTACK:
1781         pr("               Final attack strength: %8d\n", ototal);
1782         break;
1783     case A_ASSAULT:
1784         pr("              Final assault strength: %8d\n", ototal);
1785         break;
1786     case A_PARA:
1787         if (def->sct_type == SCT_MOUNT ||
1788             def->sct_type == SCT_WATER ||
1789             def->sct_type == SCT_CAPIT ||
1790             def->sct_type == SCT_FORTR || def->sct_type == SCT_WASTE) {
1791             pr("You can't air-assault a %s sector!\n",
1792                def->sct_dcp->d_name);
1793             a_cas = a_bodies;
1794             off[0].troops = 0;
1795             ototal = get_ototal(A_PARA, off, olist, osupport, 0);
1796         }
1797         pr("          Final air-assault strength: %8d\n", ototal);
1798         break;
1799     case A_BOARD:
1800     case A_LBOARD:
1801         pr("                Final board strength: %8d\n", ototal);
1802     }
1803
1804
1805     pr("              Final defense strength: %8d\n", dtotal);
1806     odds = att_calcodds(ototal, dtotal);
1807     pr("                          Final odds: %8d%%\n", (int)(odds * 100));
1808
1809     /* spread the plague */
1810     if (combat_mode != A_PARA) {
1811         if (!def->plague)
1812             for (n = 0; n <= off->last; ++n)
1813                 if (off[n].type != EF_BAD)
1814                     def->plague |= off[n].plague;
1815         for (n = 0; n <= off->last; ++n)
1816             if (off[n].type != EF_BAD)
1817                 off[n].plague |= def->plague;
1818     }
1819     att_infect_units(olist, off->plague);
1820     att_infect_units(dlist, def->plague);
1821
1822     /*
1823      * Fighting is slightly random.  There is always that last little
1824      * effort you see people put in.  Or the stray bullet that takes out
1825      * an officer and the rest go into chaos.  Things like that.
1826      * Thus, we have added a very slight random factor that will sometimes
1827      * allow the little guy to win. We modify the odds a little
1828      * (either +- 5%) to account for this randomness.  We also only
1829      * recalculate the odds every 8-50 casualties, not every cacsualty,
1830      * since a single dead guy normally wouldn't cause a commander to
1831      * rethink his strategies, but 50 dead guys might.
1832      */
1833     odds += (roll(11) - 6) / 100.0;
1834     if (odds < 0.0)
1835         odds = 0.1;
1836     if (odds > 1.0)
1837         odds = 1.0;
1838     recalctime = 7 + roll(43);
1839     while (!success && ototal) {
1840         if (chance(odds)) {
1841             pr("!");
1842             d_cas += take_casualty(A_DEFEND, def, dlist);
1843             dtotal = get_dtotal(def, dlist, dsupport, 0);
1844             if (!dtotal)
1845                 ++success;
1846         } else {
1847             pr("@");
1848             a_cas += take_casualty(combat_mode, off, olist);
1849             ototal = get_ototal(combat_mode, off, olist, osupport, 0);
1850         }
1851         if (((a_cas + d_cas) % 70) == 69)
1852             pr("\n");
1853         if (recalctime-- <= 0) {
1854             recalctime = 7 + roll(43);
1855             odds = att_calcodds(ototal, dtotal);
1856             odds += (roll(11) - 6) / 100.0;
1857             if (odds < 0.0)
1858                 odds = 0.1;
1859             if (odds > 1.0)
1860                 odds = 1.0;
1861         }
1862     }
1863     pr("\n");
1864     /* update defense mobility & mil */
1865     if (success)
1866         def->mil = 0;
1867     else {
1868         if (def->type == EF_SECTOR && d_mil && d_cas) {
1869             if (def->mob < 0)
1870                 def->mobcost = 0;
1871             else {
1872                 newmob = damage(def->mob, 100 * d_cas / d_mil);
1873                 def->mobcost = MIN(20, def->mob - newmob);
1874             }
1875         }
1876         def->mil = def->troops;
1877     }
1878
1879     /* update attack mobility & mil */
1880     for (n = 0; n <= off->last; ++n)
1881         if (off[n].type != EF_BAD && off[n].troops < a_troops[n]) {
1882             if (off[n].type == EF_SECTOR && off[n].mil) {
1883                 if (!CANT_HAPPEN(off[n].mob < 0)) {
1884                     newmob = damage(off[n].mob,
1885                                     100 * (a_troops[n] - off[n].troops)
1886                                     / off[n].mil);
1887                     off[n].mobcost += MIN(20, off[n].mob - newmob);
1888                 }
1889             }
1890             off[n].mil -= a_troops[n] - off[n].troops;
1891         }
1892
1893     /* update land unit mobility */
1894     if (d_bodies && d_cas)
1895         lnd_takemob(dlist, (double)d_cas / d_bodies);
1896     if (a_bodies && a_cas)
1897         lnd_takemob(olist, (double)a_cas / a_bodies);
1898
1899     /* damage attacked sector */
1900     def->eff = effdamage(def->eff, (d_cas + a_cas) / 10);
1901
1902     pr("- Casualties -\n     Yours: %d\n", a_cas);
1903     pr("    Theirs: %d\n", d_cas);
1904     pr("Papershuffling ... %.1f B.T.U\n", (d_cas + a_cas) * 0.15);
1905     player->btused += (int)((d_cas + a_cas) * 0.015 + 0.5);
1906
1907     if (success) {
1908         switch (combat_mode) {
1909         case A_ATTACK:
1910             news_item = def->own ? N_WON_SECT : N_TOOK_UNOCC;
1911             pr("We have captured %s, sir!\n", prcom(0, def));
1912             action = "taking";
1913             break;
1914         case A_ASSAULT:
1915             news_item = def->own ? N_AWON_SECT : N_START_COL;
1916             pr("We have secured a beachhead at %s, sir!\n", prcom(0, def));
1917             action = "assaulting and taking";
1918             break;
1919         case A_PARA:
1920             news_item = def->own ? N_PWON_SECT : N_PARA_UNOCC;
1921             pr("We have captured %s, sir!\n", prcom(0, def));
1922             action = "air-assaulting and taking";
1923             break;
1924         case A_BOARD:
1925             news_item = N_BOARD_SHIP;
1926             pr("We have boarded %s, sir!\n", prcom(0, def));
1927             action = "boarding";
1928             break;
1929         case A_LBOARD:
1930             news_item = N_BOARD_LAND;
1931             pr("We have boarded %s, sir!\n", prcom(0, def));
1932             action = "boarding";
1933             break;
1934         default:
1935             CANT_REACH();
1936             news_item = 0;
1937             action = "defeating";
1938         }
1939     } else {
1940         switch (combat_mode) {
1941         case A_ATTACK:
1942             news_item = N_SCT_LOSE;
1943             pr("You have been defeated!\n");
1944             action = "attacking";
1945             break;
1946         case A_ASSAULT:
1947             news_item = N_ALOSE_SCT;
1948             pr("You have been defeated!\n");
1949             kill_land(olist);
1950             action = "trying to assault";
1951             break;
1952         case A_PARA:
1953             news_item = N_PLOSE_SCT;
1954             pr("All of your troops were destroyed\n");
1955             action = "trying to air-assault";
1956             break;
1957         case A_BOARD:
1958             news_item = N_SHP_LOSE;
1959             pr("You have been repelled\n");
1960             kill_land(olist);
1961             action = "trying to board";
1962             break;
1963         case A_LBOARD:
1964             news_item = N_LND_LOSE;
1965             pr("You have been repelled\n");
1966             kill_land(olist);
1967             action = "trying to board";
1968             break;
1969         default:
1970             CANT_REACH();
1971             news_item = 0;
1972             action = "fighting";
1973         }
1974     }
1975     nreport(player->cnum, news_item, def->own, 1);
1976     if (def->own) {
1977         wu(0, def->own,
1978            "%s lost %d troops %s %s\nWe lost %d troops defending\n",
1979            prnatid(player->cnum), a_cas,
1980            action, pr_com(0, def, def->own), d_cas);
1981     }
1982
1983     send_reacting_units_home(dlist);
1984
1985     /* putland the defending land */
1986     lnd_put(dlist);
1987
1988     /* putland the attacking land */
1989     put_oland(olist);
1990
1991     /* put the victim sector/ship/land */
1992     if (!success || !take_def(combat_mode, olist, off, def))
1993         put_combat(def);
1994
1995     /* put the attacking sectors/ship */
1996     for (n = 0; n <= off->last; ++n)
1997         if (off[n].type != EF_BAD)
1998             put_combat(&off[n]);
1999
2000     if (!success)
2001         return 0;
2002
2003     switch (combat_mode) {
2004     case A_ATTACK:
2005         ask_move_in(off, olist, def);
2006
2007         /* put sectors again to get abandon warnings */
2008         for (n = 0; n <= off->last; ++n)
2009             if (off[n].type != EF_BAD)
2010                 put_combat(&off[n]);
2011         break;
2012     default:
2013         att_move_in_off(combat_mode, off, olist, def);
2014     }
2015     if (def->mil > 0)
2016         pr("%d of your troops now occupy %s\n", def->mil, prcom(0, def));
2017     return 1;
2018 }
2019
2020 /* What percentage of the combat forces going head-to-head are we? */
2021
2022 static double
2023 att_calcodds(int ototal, int dtotal)
2024 {
2025     double odds;
2026
2027     /* calculate odds */
2028     if (ototal <= 0)
2029         odds = 0.0;
2030     else if (dtotal <= 0)
2031         odds = 1.0;
2032     else
2033         odds = (double)ototal / (dtotal + ototal);
2034
2035     return odds;
2036 }
2037
2038 /* Here's where the dead soldiers get dragged off the battlefield */
2039
2040 static int
2041 take_casualty(int combat_mode, struct combat *off, struct emp_qelem *olist)
2042 {
2043     int to_take = CASUALTY_LUMP;
2044     int biggest_troops = 0, index = -1;
2045     int n, tot_troops = 0, biggest_mil, cas;
2046     struct emp_qelem *qp;
2047     struct ulist *llp, *biggest;
2048
2049     for (n = 0; n <= off->last; ++n) {
2050         if (off[n].type != EF_BAD) {
2051             tot_troops += off[n].troops;
2052             if (off[n].troops > biggest_troops) {
2053                 biggest_troops = off[n].troops;
2054                 index = n;
2055             }
2056         }
2057     }
2058
2059     if (tot_troops)
2060         to_take -= tot_troops;
2061
2062     if (to_take >= 0) {
2063         for (n = 0; n <= off->last; ++n)
2064             if (off[n].type != EF_BAD)
2065                 off[n].troops = 0;
2066     } else {
2067         /*
2068          * They can all come off mil.  We rotate the casualties,
2069          * starting with the sector containing the most mil.
2070          */
2071         to_take = CASUALTY_LUMP;
2072         if (index < 0) {
2073             pr("ERROR: Tell the deity that you got the 'green librarian' error\n");
2074             index = 0;
2075         }
2076         while (to_take > 0) {
2077             for (n = index; n <= off->last && to_take; ++n) {
2078                 if (off[n].type != EF_BAD && off[n].troops > 0) {
2079                     --to_take;
2080                     --off[n].troops;
2081                 }
2082             }
2083             for (n = 0; n < index && to_take; ++n) {
2084                 if (off[n].type != EF_BAD && off[n].troops > 0) {
2085                     --to_take;
2086                     --off[n].troops;
2087                 }
2088             }
2089         }
2090         return CASUALTY_LUMP;
2091     }
2092
2093     if (QEMPTY(olist))
2094         return CASUALTY_LUMP - to_take;
2095
2096     /*
2097      *  Need to take some casualties from attacking units
2098      *  Procedure: find the biggest unit remaining (in
2099      *  terms of mil) and give it the casualties.
2100      */
2101     biggest = NULL;
2102     biggest_mil = -1;
2103     for (qp = olist->q_forw; qp != olist; qp = qp->q_forw) {
2104         llp = (struct ulist *)qp;
2105
2106         if (llp->unit.land.lnd_item[I_MILIT] > biggest_mil) {
2107             biggest_mil = llp->unit.land.lnd_item[I_MILIT];
2108             biggest = llp;
2109         }
2110     }
2111     if (biggest == NULL)
2112         return CASUALTY_LUMP - to_take;
2113
2114     cas = lnd_take_casualty(combat_mode, biggest, to_take);
2115     return CASUALTY_LUMP - (to_take - cas);
2116 }
2117
2118 /* Send reacting defense units back to where they came from (at no mob cost) */
2119
2120 static void
2121 send_reacting_units_home(struct emp_qelem *list)
2122 {
2123     struct emp_qelem *qp, *next;
2124     struct ulist *llp;
2125     char buf[1024];
2126
2127     for (qp = list->q_forw; qp != list; qp = next) {
2128         next = qp->q_forw;
2129         llp = (struct ulist *)qp;
2130         if ((llp->unit.land.lnd_x != llp->x) ||
2131             (llp->unit.land.lnd_y != llp->y)) {
2132             sprintf(buf, "returns to %s",
2133                     xyas(llp->x, llp->y, llp->unit.land.lnd_own));
2134             llp->unit.land.lnd_x = llp->x;
2135             llp->unit.land.lnd_y = llp->y;
2136             lnd_print(llp->unit.land.lnd_own, llp, buf);
2137             lnd_put_one(llp);
2138         }
2139     }
2140 }
2141
2142 /* Check for 0 offense strength.  This call will always preceed an abort */
2143
2144 int
2145 att_empty_attack(int combat_mode, int ototal, struct combat *def)
2146 {
2147     if (ototal <= 0) {
2148         if (def->own && player->cnum != def->own) {
2149             wu(0, def->own,
2150                "%s considered %sing you @%s\n",
2151                prnatid(player->cnum),
2152                att_mode[combat_mode], xyas(def->x, def->y, def->own));
2153         }
2154         pr("No troops for %s...\n", att_mode[combat_mode]);
2155         return 1;
2156     }
2157     return 0;
2158 }
2159
2160 /*
2161  * Take the defending sector or ship from the defender and give it to the
2162  * attacker.
2163  */
2164
2165 static int
2166 take_def(int combat_mode, struct emp_qelem *list, struct combat *off,
2167          struct combat *def)
2168 {
2169     int n;
2170     int occuppied = 0;
2171     struct ulist *llp, *delete_me = NULL;
2172     char buf[1024];
2173     struct sctstr sect;
2174     struct shpstr ship;
2175     struct lndstr land;
2176
2177     for (n = 0; n <= off->last && !occuppied; ++n) {
2178         if (off[n].type != EF_BAD &&
2179             off[n].troops > 0 &&
2180             (off[n].type != EF_SECTOR || off[n].mob)) {
2181             ++occuppied;
2182             if (def->type == EF_LAND) {
2183                 if (def->lnd_lcp->l_flags & L_SPY) {
2184                     continue;
2185                 }
2186             }
2187             --(off[n].troops);
2188             --(off[n].mil);
2189             ++def->mil;
2190             pr("1 mil from %s moves %s\n",
2191                prcom(0, off + n), prcom(2, def));
2192         }
2193     }
2194     if (!occuppied) {
2195         if (QEMPTY(list)) {
2196             pr("%s left unoccupied\n", prcom(0, def));
2197             if (def->own)
2198                 wu(0, def->own,
2199                    "No enemy troops moved %s so you still own it!\n",
2200                    pr_com(2, def, def->own));
2201             return 0;
2202         } else {
2203             llp = (struct ulist *)list->q_forw;
2204             llp->unit.land.lnd_x = def->x;
2205             llp->unit.land.lnd_y = def->y;
2206             take_move_in_mob(combat_mode, llp, off, def);
2207             if (def->type == EF_SHIP) {
2208                 llp->unit.land.lnd_ship = def->shp_uid;
2209                 sprintf(buf, "boards %s", prcom(0, def));
2210                 lnd_print(player->cnum, llp, buf);
2211                 delete_me = llp;
2212             } else {
2213                 llp->unit.land.lnd_ship = -1;
2214                 sprintf(buf, "moves in to occupy %s",
2215                         xyas(def->x, def->y, player->cnum));
2216                 lnd_print(player->cnum, llp, buf);
2217                 lnd_put_one(llp);
2218             }
2219         }
2220     }
2221     put_combat(def);
2222     if (def->type == EF_SECTOR) {
2223         getsect(def->x, def->y, &sect);
2224         takeover(&sect, player->cnum);
2225         if (sect.sct_type == SCT_CAPIT || sect.sct_type == SCT_MOUNT)
2226             caploss(&sect, def->own,
2227                     "* We have captured %s's capital, sir! *\n");
2228         putsect(&sect);
2229     } else if (def->type == EF_SHIP) {
2230         getship(def->shp_uid, &ship);
2231         takeover_ship(&ship, player->cnum);
2232         putship(ship.shp_uid, &ship);
2233     } else if (def->type == EF_LAND) {
2234         getland(def->lnd_uid, &land);
2235         takeover_land(&land, player->cnum);
2236         putland(land.lnd_uid, &land);
2237     }
2238     if (delete_me)
2239         lnd_put_one(delete_me);
2240     att_get_combat(def, 0);
2241     return 1;
2242 }
2243
2244 /*
2245  * Ask the attacker which mil & land units they'd like to move into the
2246  * conquered sector.
2247  */
2248
2249 static void
2250 ask_move_in(struct combat *off, struct emp_qelem *olist,
2251             struct combat *def)
2252 {
2253     int n;
2254     struct emp_qelem *qp, *next;
2255     struct ulist *llp;
2256     char buf[512];
2257     char prompt[512];
2258     char land_answer[256];
2259     char *answerp;
2260
2261     for (n = 0; n <= off->last; ++n)
2262         if (off[n].type != EF_BAD && off[n].troops > 0)
2263             if (off[n].mob) {
2264                 ask_move_in_off(&off[n], def);
2265                 if (player->aborted)
2266                     break;
2267             }
2268
2269     if (QEMPTY(olist))
2270         return;
2271     memset(land_answer, 0, sizeof(land_answer));
2272     for (qp = olist->q_forw; qp != olist; qp = next) {
2273         next = qp->q_forw;
2274         llp = (struct ulist *)qp;
2275         answerp = &land_answer[(int)llp->unit.land.lnd_army];
2276         if (player->aborted || att_get_combat(def, 0) < 0)
2277             *answerp = 'N';
2278         if (*answerp == 'Y')
2279             continue;
2280         if (!get_oland(A_ATTACK, llp))
2281             continue;
2282         if (*answerp != 'N') {
2283             sprintf(prompt, "Move in with %s (%c %d%%) [ynYNq?] ",
2284                     prland(&llp->unit.land),
2285                     llp->unit.land.lnd_army ? llp->unit.land.lnd_army : '~',
2286                     llp->unit.land.lnd_effic);
2287             *answerp = att_prompt(prompt, llp->unit.land.lnd_army);
2288             if (player->aborted || att_get_combat(def, 0) < 0)
2289                 *answerp = 'N';
2290             if (!get_oland(A_ATTACK, llp))
2291                 continue;
2292         }
2293         if (*answerp == 'y' || *answerp == 'Y')
2294             continue;
2295         sprintf(buf, "stays in %s",
2296                 xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y,
2297                      player->cnum));
2298         lnd_print(player->cnum, llp, buf);
2299         lnd_put_one(llp);
2300     }
2301     if (QEMPTY(olist))
2302         return;
2303     if (att_get_combat(def, 0) < 0) {
2304         for (qp = olist->q_forw; qp != olist; qp = next) {
2305             next = qp->q_forw;
2306             llp = (struct ulist *)qp;
2307             if (!get_oland(A_ATTACK, llp))
2308                 continue;
2309             sprintf(buf, "stays in %s",
2310                     xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y,
2311                          player->cnum));
2312             lnd_print(player->cnum, llp, buf);
2313             lnd_put_one(llp);
2314         }
2315         return;
2316     }
2317     if (opt_INTERDICT_ATT)
2318         lnd_interdict(olist, def->x, def->y, player->cnum);
2319     move_in_land(A_ATTACK, off, olist, def);
2320 }
2321
2322 void
2323 att_move_land(int combat_mode, struct combat *off, struct emp_qelem *olist,
2324               struct combat *def)
2325 {
2326     struct emp_qelem *qp, *next;
2327     struct ulist *llp;
2328
2329     for (qp = olist->q_forw; qp != olist; qp = next) {
2330         next = qp->q_forw;
2331         llp = (struct ulist *)qp;
2332         if (!get_oland(combat_mode, llp))
2333             continue;
2334         take_move_in_mob(combat_mode, llp, off, def);
2335         llp->unit.land.lnd_x = def->x;
2336         llp->unit.land.lnd_y = def->y;
2337         if (def->type == EF_SHIP)
2338             llp->unit.land.lnd_ship = def->shp_uid;
2339         else
2340             llp->unit.land.lnd_ship = -1;
2341     }
2342 }
2343
2344 /* Move offensive land units to the conquered sector or ship */
2345
2346 static void
2347 move_in_land(int combat_mode, struct combat *off, struct emp_qelem *olist,
2348              struct combat *def)
2349 {
2350     struct emp_qelem *qp, *next;
2351     struct ulist *llp;
2352     char buf[512];
2353
2354     att_move_land(combat_mode, off, olist, def);
2355
2356     if (def->type == EF_SECTOR) {
2357         if (opt_INTERDICT_ATT) {
2358             lnd_sweep(olist, 0, 0, player->cnum);
2359             lnd_check_mines(olist);
2360         }
2361         sprintf(buf, "now occupies %s", prcom(0, def));
2362     } else {
2363         sprintf(buf, "boards %s", prcom(0, def));
2364     }
2365
2366     for (qp = olist->q_forw; qp != olist; qp = next) {
2367         next = qp->q_forw;
2368         llp = (struct ulist *)qp;
2369         lnd_print(player->cnum, llp, buf);
2370     }
2371
2372     lnd_put(olist);
2373 }
2374
2375 /*
2376  * Move assaulting, paradropping, or boarding mil & units into def
2377  * If the mil are coming from a ship, then pack a lunch.
2378  */
2379
2380 void
2381 att_move_in_off(int combat_mode, struct combat *off,
2382                 struct emp_qelem *olist, struct combat *def)
2383 {
2384     struct sctstr sect;
2385     struct shpstr ship;
2386     int troops, n;
2387     int lunchbox = 0;
2388
2389     move_in_land(combat_mode, off, olist, def);
2390
2391     for (n = 0; n <= off->last; ++n) {
2392         if (off[n].type == EF_BAD || !off[n].troops)
2393             continue;
2394         troops = off[n].troops;
2395         off[n].troops = 0;
2396         off[n].mil -= troops;
2397         def->mil += troops;
2398         put_combat(off + n);
2399         if (combat_mode == A_ASSAULT) {
2400             if (CANT_HAPPEN(off[n].type != EF_SHIP))
2401                 continue;
2402             getship(off[n].shp_uid, &ship);
2403             lunchbox += (int)((troops + 1) * ship.shp_item[I_FOOD]
2404                               / (ship.shp_item[I_MILIT] + troops
2405                                  + ship.shp_item[I_CIVIL] + 0.5));
2406
2407             ship.shp_item[I_FOOD] -= lunchbox;
2408             putship(ship.shp_uid, &ship);
2409         }
2410     }
2411
2412     put_combat(def);
2413
2414     if (combat_mode == A_ASSAULT) {
2415         if (CANT_HAPPEN(def->type != EF_SECTOR))
2416             return;
2417         getsect(def->x, def->y, &sect);
2418         if (lunchbox > ITEM_MAX - sect.sct_item[I_FOOD])
2419             lunchbox = ITEM_MAX - sect.sct_item[I_FOOD];
2420         sect.sct_item[I_FOOD] += lunchbox;
2421         putsect(&sect);
2422     }
2423 }
2424
2425
2426 /* Ask how many mil to move in from each sector */
2427
2428 static void
2429 ask_move_in_off(struct combat *off, struct combat *def)
2430 {
2431     int mob_support;
2432     int num_mil, dam = 0, left;
2433     double d, weight;
2434     char prompt[512];
2435     char buf[1024];
2436     char *p;
2437
2438     if (att_get_combat(off, 0) <= 0)
2439         return;
2440     if (att_get_combat(def, 0) < 0)
2441         return;
2442     if (off->own != player->cnum)
2443         return;
2444     d = att_mobcost(off->own, def, MOB_MOVE);
2445     if ((mob_support = MIN(off->troops, (int)(off->mob / d))) <= 0)
2446         return;
2447     sprintf(prompt, "How many mil to move in from %s (%d max)? ",
2448             xyas(off->x, off->y, player->cnum), mob_support);
2449     p = getstring(prompt, buf);
2450     if (!p || !*p || (num_mil = atoi(p)) <= 0)
2451         return;
2452 /* Make sure we don't move in more than we can support mobility-wise */
2453     if (num_mil > mob_support)
2454         num_mil = mob_support;
2455     if (att_get_combat(off, 0) <= 0)
2456         return;
2457     if (att_get_combat(def, 0) < 0)
2458         return;
2459     if ((num_mil = MIN(off->troops, num_mil)) <= 0) {
2460         pr("No mil moved in from %s\n",
2461            xyas(off->x, off->y, player->cnum));
2462         return;
2463     }
2464     mob_support = MAX(1, (int)(num_mil * d));
2465     off->mob -= MIN(off->mob, mob_support);
2466     off->mil -= num_mil;
2467     off->troops -= num_mil;
2468     put_combat(off);
2469     left = num_mil;
2470     weight = (double)num_mil * ichr[I_MILIT].i_lbs;
2471     if (opt_INTERDICT_ATT && chance(weight / 200.0)) {
2472         if (chance(weight / 100.0))
2473             dam +=
2474                 ground_interdict(def->x, def->y, player->cnum, "military");
2475         dam += check_lmines(def->x, def->y, weight);
2476     }
2477
2478     if (dam) {
2479         left = commdamage(num_mil, dam, I_MILIT);
2480         if (left < num_mil) {
2481             if (left) {
2482                 pr("%d of the mil you were moving were destroyed!\n"
2483                    "Only %d mil made it to %s\n",
2484                    num_mil - left, left,
2485                    xyas(def->x, def->y, player->cnum));
2486             } else {
2487                 pr("All of the mil you were moving were destroyed!\n");
2488             }
2489         }
2490         /* maybe got nuked */
2491         if (att_get_combat(def, 0) < 0)
2492             return;
2493     }
2494     def->mil += left;
2495     put_combat(def);
2496 }
2497
2498
2499 /* Charge land units for moving into a sector or onto a ship */
2500
2501 static void
2502 take_move_in_mob(int combat_mode, struct ulist *llp, struct combat *off,
2503                  struct combat *def)
2504 {
2505     double mob = llp->unit.land.lnd_mobil;
2506     double gain = etu_per_update * land_mob_scale;
2507     double mobcost, moblim;
2508
2509     switch (combat_mode) {
2510     case A_ATTACK:
2511         mobcost = lnd_pathcost(&llp->unit.land,
2512                                att_mobcost(llp->unit.land.lnd_own, def,
2513                                            lnd_mobtype(&llp->unit.land)));
2514         break;
2515     case A_ASSAULT:
2516         /*
2517          * Set mobcost to basic assault cost, moblim to maximum
2518          * mobility to keep when assaulting from non-landing ship
2519          */
2520         if (lchr[llp->unit.land.lnd_type].l_flags & L_MARINE) {
2521             mobcost = gain / 2.0;
2522             moblim = 0;
2523         } else {
2524             mobcost = gain;
2525             moblim = -gain;
2526         }
2527         if (!(off->shp_mcp->m_flags & M_LAND))
2528             /* Not a landing ship, ensure we go to or below moblim */
2529             mobcost = MAX(mobcost, mob - moblim);
2530         break;
2531     case A_BOARD:
2532         if (lchr[llp->unit.land.lnd_type].l_flags & L_MARINE)
2533             mobcost = 10;
2534         else
2535             mobcost = 40;
2536         break;
2537     default:
2538         CANT_REACH();
2539         mobcost = 0;
2540     }
2541
2542     mob -= mobcost;
2543     if (mob < -127.0)
2544         mob = -127.0;
2545     llp->unit.land.lnd_mobil = (signed char)mob;
2546     llp->unit.land.lnd_harden = 0;
2547 }
2548
2549 static void
2550 free_list(struct emp_qelem *list)
2551 {
2552     struct emp_qelem *qp, *next;
2553
2554     if (!list || QEMPTY(list))
2555         return;
2556
2557     qp = list->q_forw;
2558     while (qp != list) {
2559         next = qp->q_forw;
2560         emp_remque(qp);
2561         free(qp);
2562         qp = next;
2563     }
2564 }
2565
2566 int
2567 att_free_lists(struct emp_qelem *olist, struct emp_qelem *dlist)
2568 {
2569     free_list(olist);
2570     free_list(dlist);
2571     return RET_OK;
2572 }
2573
2574 /*
2575  * sector_strength - Everyone starts at 1.  You can get up to a max
2576  *                   of d_dstr, depending on how much you build up the
2577  *                   defenses of the sector.
2578  */
2579
2580 double
2581 sector_strength(struct sctstr *sp)
2582 {
2583     double def = SCT_DEFENSE(sp) / 100.0;
2584     double base = sp->sct_type == SCT_MOUNT ? 2.0 : 1.0;
2585     double d = base + (dchr[sp->sct_type].d_dstr - base) * def;
2586
2587     return LIMIT_TO(d, base, dchr[sp->sct_type].d_dstr);
2588 }