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