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