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