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