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