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