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