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