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