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