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