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