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