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