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