]> git.pond.sub.org Git - empserver/blob - src/lib/subs/attsub.c
Remove a bunch of redundant casts.
[empserver] / src / lib / subs / attsub.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2005, 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 "plague.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) * aship.shp_speed * off->eff
636             <= techfact(dship.shp_tech, 1.0) * dship.shp_speed * def->eff) {
637             pr("Victim ship moves faster than you do!\n");
638             if (def->own)
639                 wu(0, def->own,
640                    "%s (#%d) %s failed to catch %s\n",
641                    cname(aship.shp_own), aship.shp_own,
642                    pr_com(0, off, def->own), pr_com(0, def, def->own));
643             return abort_attack();
644         }
645     } else if (off->type != EF_SECTOR) {
646         pr("Please tell the deity that you got the 'banana boat' error\n");
647         return abort_attack();
648     }
649     if (def->shp_mcp->m_flags & M_SUB) {
650         getsect(def->x, def->y, &sect);
651         if (sect.sct_type == SCT_WATER) {
652             pr("You can't board a submarine!\n");
653             return abort_attack();
654         }
655     }
656     return 0;
657 }
658
659 /*
660  * Lots of special things need to be checked for boarding, so I put it in
661  * it's own function.
662  * STM - I copied it for land unit boarding. :)
663  */
664
665 static int
666 land_board_abort(struct combat *off, struct combat *def)
667 {
668     if (att_get_combat(def, 1) < 0)
669         return abort_attack();
670
671     if (!off)
672         return 0;
673
674     if (att_get_combat(off, 0) < 0)
675         return abort_attack();
676
677     if (off->x != def->x || off->y != def->y) {
678         pr("Land unit #%d is not in the same sector!\n", def->lnd_uid);
679         return abort_attack();
680     }
681
682     return 0;
683 }
684
685 /* If we are boarding, then the defending ship gets a chance to fire back */
686 int
687 att_approach(struct combat *off, struct combat *def)
688 {
689     int dam;
690     struct sctstr sect;
691     struct shpstr ship;
692
693     pr("Approaching %s...\n", prcom(0, def));
694     if (def->own)
695         wu(0, def->own,
696            "%s is being approached by %s...\n",
697            pr_com(0, def, def->own), pr_com(0, off, def->own));
698     if (!(dam = shipdef(player->cnum, def->own, def->x, def->y)))
699         return 0;
700
701     pr("They're firing at us sir!\n");
702     if (def->own) {
703         wu(0, def->own,
704            "Your fleet at %s does %d damage to %s\n",
705            xyas(def->x, def->y, def->own), dam, pr_com(0, off, def->own));
706     }
707     if (off->type == EF_SECTOR) {
708         getsect(off->x, off->y, &sect);
709         sectdamage(&sect, dam, 0);
710         putsect(&sect);
711         pr("Enemy fleet at %s does %d damage to %s\n",
712            xyas(def->x, def->y, player->cnum), dam, prcom(0, off));
713     } else if (off->type == EF_SHIP) {
714         getship(off->shp_uid, &ship);
715         shipdamage(&ship, dam);
716         putship(off->shp_uid, &ship);
717         if (def->own && ship.shp_effic < SHIP_MINEFF) {
718             wu(0, def->own, "%s sunk!\n", pr_com(0, off, def->own));
719             nreport(player->cnum, N_SHP_LOSE, def->own, 1);
720         }
721     }
722     if (att_get_combat(off, 0) < 0)
723         return abort_attack();
724     return 0;
725 }
726
727 /* The attack is valid.  Tell the attacker about what they're going to hit */
728
729 int
730 att_show(struct combat *def)
731 {
732     /* Note that we tell the player about the treaty BEFORE we tell them
733        about the item.  If we didn't, then they gain free information */
734     if (def->type == EF_SECTOR) {
735         if (!trechk(player->cnum, def->own, LANATT))
736             return abort_attack();
737         pr("%s is a %d%% %s %s with approximately %d military.\n",
738            xyas(def->x, def->y, player->cnum),
739            roundintby((int)def->eff, 10),
740            cname(def->own), def->sct_dcp->d_name,
741            roundintby(def->troops, 10));
742         if (map_set(player->cnum, def->x, def->y, def->sct_dcp->d_mnem, 0))
743             writemap(player->cnum);
744     } else if (def->type == EF_SHIP || def->type == EF_LAND) {
745         if (def->type == EF_SHIP) {
746             if (!trechk(player->cnum, def->own, SEAATT))
747                 return abort_attack();
748         } else {
749             if (!trechk(player->cnum, def->own, LNDATT))
750                 return abort_attack();
751         }
752         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));
753     }
754     /* Ok, everything is fine */
755     return 0;
756 }
757
758 /* Attack and assault ask the user which kind of support they want */
759
760 int
761 att_ask_support(int offset, int *fortp, int *shipp, int *landp,
762                 int *planep)
763 {
764     s_char buf[1024];
765     s_char *p;
766     *fortp = *shipp = *landp = *planep = 1;
767
768     if (player->argp[offset] != (s_char *)0) {
769         if ((player->argp[offset + 1] == (s_char *)0) ||
770             (player->argp[offset + 2] == (s_char *)0) ||
771             (player->argp[offset + 3] == (s_char *)0)) {
772             pr("If any support arguments are used, all must be!\n");
773             return RET_SYN;
774         }
775
776         *fortp = *shipp = 0;
777         *landp = *planep = 0;
778
779         if (!(p = getstarg(player->argp[offset], "Use fort support? ",
780                            buf)))
781             return RET_SYN;
782
783         if ((*p == 'y') || (*p == 'Y'))
784             *fortp = 1;
785
786         if (!(p = getstarg(player->argp[offset + 1], "Use ship support? ",
787                            buf)))
788             return RET_SYN;
789
790         if ((*p == 'y') || (*p == 'Y'))
791             *shipp = 1;
792
793         if (!(p = getstarg(player->argp[offset + 2], "Use land support? ",
794                            buf)))
795             return RET_SYN;
796
797         if ((*p == 'y') || (*p == 'Y'))
798             *landp = 1;
799
800         if (!(p = getstarg(player->argp[offset + 3], "Use plane support? ",
801                            buf)))
802             return RET_SYN;
803
804         if ((*p == 'y') || (*p == 'Y'))
805             *planep = 1;
806     }
807     return RET_OK;
808 }
809
810 /*
811  * Attack, assault, and board ask the attacker what they'd like to attack
812  * with.  This includes mil and land units from each "off" object.  Note that
813  * after each sub-prompt, we check to make sure that the attack is still
814  * valid, and if it's not, then we abort the attack.
815  */
816
817 int
818 att_ask_offense(int combat_mode, struct combat *off, struct combat *def,
819                 struct emp_qelem *olist, int *a_spyp, int *a_engineerp)
820 {
821     int n;
822     s_char land_answer[1024];
823
824     emp_initque(olist);
825     if (att_abort(combat_mode, off, def))
826         return 0;
827     memset(land_answer, 0, sizeof(land_answer));
828     for (n = 0; n <= off->last; ++n) {
829         off[n].troops = ask_off(combat_mode, off + n, def);
830         if (att_abort(combat_mode, off, def))
831             return 0;
832         ask_olist(combat_mode, off + n, def, olist, land_answer,
833                   a_spyp, a_engineerp);
834         if (att_abort(combat_mode, off, def))
835             return 0;
836     }
837     return 0;
838 }
839
840 /* How many mil is off allowed to attack with when it attacks def? */
841
842 static int
843 get_mob_support(int combat_mode, struct combat *off, struct combat *def)
844 {
845     int mob_support;
846
847     switch (combat_mode) {
848     case A_ATTACK:
849         mob_support = off->mob / sector_mcost(getsectp(def->x, def->y),
850                                               MOB_ROAD);
851         if (mob_support < 0)
852             mob_support = 0;
853 /*              mob_support = off->mob / sector_mcost(def->sct_type, def->eff);*/
854         if (mob_support < off->troops)
855             pr("Sector %s has %d mobility which can only support %d mil,\n", xyas(off->x, off->y, player->cnum), off->mob, mob_support);
856         else
857             mob_support = off->troops;
858         return mob_support;
859     case A_ASSAULT:
860         if (def->own != player->cnum && def->mil) {
861             if (off->shp_mcp->m_flags & M_SEMILAND)
862                 return off->troops / 4;
863             else if (!(off->shp_mcp->m_flags & M_LAND))
864                 return off->troops / 10;
865         }
866         break;
867     case A_BOARD:
868         if (off->type == EF_SECTOR && off->mob <= 0)
869             return 0;
870         mob_support = def->shp_mcp->m_item[I_MILIT];
871         if (mob_support < off->troops)
872             pr("The size of the ship you are trying to board limits your party to %d mil,\n", mob_support);
873         else
874             mob_support = off->troops;
875         return mob_support;
876     case A_LBOARD:
877         if (off->mob <= 0)
878             return 0;
879         if (def->lnd_lcp->l_flags & L_SPY)
880             return 1;
881         mob_support = def->lnd_lcp->l_item[I_MILIT];
882         if (mob_support < off->troops)
883             pr("The size of the unit you are trying to board limits your party to %d mil,\n", mob_support);
884         else
885             mob_support = off->troops;
886         return mob_support;
887     }
888     return off->troops;
889 }
890
891 /*
892  * If the attacker decides to go through with the attack, then the
893  * sectors/ships they are attacking with may be charged some mobility.
894  * This is where that amount of mobility is calculated.  It is actually
895  * subtracted "for real" from the object's mobility in put_combat().
896  */
897
898 static void
899 calc_mobcost(int combat_mode, struct combat *off, struct combat *def,
900              int attacking_mil)
901 {
902     struct shpstr ship;
903
904     if (!attacking_mil)
905         return;
906     switch (combat_mode) {
907     case A_ATTACK:
908         off->mobcost +=
909             max(1,
910                 (int)(attacking_mil *
911                       sector_mcost(getsectp(def->x, def->y), MOB_ROAD)));
912         break;
913     case A_LBOARD:
914         off->mobcost += max(1, attacking_mil / 5);
915         break;
916     case A_BOARD:
917         switch (off->type) {
918         case EF_SECTOR:
919             off->mobcost += max(1, attacking_mil / 5);
920             break;
921         case EF_SHIP:
922             /* the 2 in the formula below is a fudge factor */
923             getship(def->shp_uid, &ship);
924             off->mobcost += (def->eff / 100) * (ship.shp_speed / 2);
925         }
926     }
927 }
928
929 /* How many mil to we want to attack from off against def? */
930
931 static int
932 ask_off(int combat_mode, struct combat *off, struct combat *def)
933 {
934     int attacking_mil;
935     int mob_support;
936     s_char prompt[512];
937
938     if (att_get_combat(off, 0) <= 0)
939         return 0;
940     if ((off->type == EF_SECTOR) && (off->own != player->cnum))
941         return 0;
942     if ((mob_support = get_mob_support(combat_mode, off, def)) <= 0)
943         return 0;
944     if (off->type == EF_SECTOR) {
945         if (off->own != player->cnum)
946             return 0;
947         sprintf(prompt, "Number of mil from %s at %s (max %d) : ",
948                 off->sct_dcp->d_name,
949                 xyas(off->x, off->y, player->cnum), mob_support);
950     } else {
951         sprintf(prompt, "Number of mil from %s (max %d) : ",
952                 prcom(0, off), mob_support);
953     }
954     if ((attacking_mil = onearg(0, prompt)) < 0)
955         abort_attack();
956     if (att_abort(combat_mode, off, def))
957         return 0;
958     if (att_get_combat(off, 0) <= 0)
959         return 0;
960     if ((attacking_mil =
961          min(attacking_mil, min(mob_support, off->troops))) <= 0)
962         return 0;
963
964     calc_mobcost(combat_mode, off, def, attacking_mil);
965     return attacking_mil;
966 }
967
968 /*
969  * Which units would you like to attack with or move in with [ynYNq?]
970  */
971
972 static s_char
973 att_prompt(s_char *prompt, s_char army)
974 {
975     s_char buf[1024];
976     s_char *p = buf;
977
978     if (army == ' ')
979         army = '~';
980     *buf = -2;
981     while (!p || (*p != 'y' && *p != 'n' && *p != 'Y' && *p != 'N')) {
982         if (p && *p == 'q') {
983             abort_attack();
984             return 'N';
985         }
986         if (!p || !*p)
987             return 'n';
988         if (p && *p != -2)
989             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);
990         p = getstring(prompt, buf);
991         if (player->aborted) {
992             buf[0] = 'N';
993             p = buf;
994         }
995     }
996     return *p;
997 }
998
999 /* Ask the attacker which units they want to attack/assault/board with */
1000
1001 static void
1002 ask_olist(int combat_mode, struct combat *off, struct combat *def,
1003           struct emp_qelem *olist, s_char *land_answer, int *a_spyp,
1004           int *a_engineerp)
1005 {
1006     struct nstr_item ni;
1007     struct lndstr land;
1008     double mobcost;
1009     struct llist *llp;
1010     struct lchrstr *lcp;
1011     int att_val;
1012     int count = 0;
1013     int maxland = 0;
1014     int first_time = 1;
1015     s_char prompt[512];
1016
1017     if (def->type == EF_LAND)
1018         return;
1019     if (def->type == EF_SHIP)
1020         maxland = def->shp_mcp->m_nland;
1021
1022     snxtitem_xy(&ni, EF_LAND, off->x, off->y);
1023     while (nxtitem(&ni, &land)) {
1024         if (land.lnd_own != player->cnum)
1025             continue;
1026         if (land.lnd_effic < LAND_MINEFF)
1027             continue;
1028         if (land_answer[(int)land.lnd_army] == 'N')
1029             continue;
1030         if (!lnd_can_attack(&land))
1031             continue;
1032         lcp = &lchr[(int)land.lnd_type];
1033
1034         if (def->type == EF_SHIP && !maxland) {
1035             pr("Land units are not able to board this kind of ship\n");
1036             return;
1037         }
1038         if (land.lnd_mobil <= 0) {
1039             pr("%s is out of mobility, and cannot %s\n",
1040                prland(&land), att_mode[combat_mode]);
1041             continue;
1042         }
1043
1044         if (opt_MARKET) {
1045             if (ontradingblock(EF_LAND, (int *)&land)) {
1046                 pr("%s is on the trading block, and cannot %s\n",
1047                    prland(&land), att_mode[combat_mode]);
1048                 continue;
1049             }
1050         }
1051
1052         if (off->type == EF_SECTOR && land.lnd_ship >= 0) {
1053             pr("%s is on ship #%d, and cannot %s\n",
1054                prland(&land), land.lnd_ship, att_mode[combat_mode]);
1055             continue;
1056         } else if (off->type == EF_SHIP) {
1057             if (land.lnd_ship != off->shp_uid)
1058                 continue;
1059         } else if (land.lnd_land >= 0) {
1060             pr("%s is on unit #%d, and cannot %s\n",
1061                prland(&land), land.lnd_land, att_mode[combat_mode]);
1062             continue;
1063         }
1064         switch (combat_mode) {
1065         case A_ATTACK:
1066             mobcost =
1067                 lnd_mobcost(&land, getsectp(def->x, def->y), MOB_NONE);
1068             if (land.lnd_mobil < mobcost) {
1069                 pr("%s does not have enough mobility (%d needed)\n",
1070                    prland(&land), (int)ceil(mobcost));
1071                 continue;
1072             }
1073             break;
1074         case A_ASSAULT:
1075         case A_BOARD:
1076             mobcost = 0;
1077             if (!(lcp->l_flags & L_ASSAULT))
1078                 continue;
1079         }
1080         att_val = attack_val(combat_mode, &land);
1081         if (!att_val) {
1082             pr("%s has no offensive strength\n", prland(&land));
1083             continue;
1084         }
1085         resupply_all(&land);
1086         putland(land.lnd_uid, &land);
1087         if (!has_supply(&land)) {
1088             pr("%s is out of supply, and cannot %s\n",
1089                prland(&land), att_mode[combat_mode]);
1090             continue;
1091         }
1092         if (def->type == EF_SHIP && first_time) {
1093             first_time = 0;
1094             pr("You may board with a maximum of %d land units\n", maxland);
1095         }
1096         pr("%s has a base %s value of %d\n",
1097            prland(&land), att_mode[combat_mode], att_val);
1098         if (land_answer[(int)land.lnd_army] != 'Y') {
1099             sprintf(prompt,
1100                     "%s with %s %s (%c %d%%) [ynYNq?] ",
1101                     att_mode[combat_mode],
1102                     prland(&land),
1103                     prcom(1, off),
1104                     land.lnd_army == ' ' ? '~' : land.lnd_army,
1105                     land.lnd_effic);
1106             land_answer[(int)land.lnd_army] =
1107                 att_prompt(prompt, land.lnd_army);
1108             if (att_abort(combat_mode, off, def))
1109                 return;
1110             if (land_answer[(int)land.lnd_army] != 'y' &&
1111                 land_answer[(int)land.lnd_army] != 'Y')
1112                 continue;
1113         }
1114         if (!(llp = malloc(sizeof(struct llist)))) {
1115             logerror("Malloc failed in attack!\n");
1116             abort_attack();
1117             return;
1118         }
1119         memset(llp, 0, sizeof(struct llist));
1120         emp_insque(&llp->queue, olist);
1121         llp->mobil = mobcost;
1122         if (!get_land(combat_mode, def, land.lnd_uid, llp, 0))
1123             continue;
1124         if (lnd_spyval(&land) > *a_spyp)
1125             *a_spyp = lnd_spyval(&land);
1126         if (llp->lcp->l_flags & L_ENGINEER)
1127             ++(*a_engineerp);
1128         if (def->type == EF_SHIP && ++count >= maxland)
1129             break;
1130     }
1131 }
1132
1133 /* What's the offense or defense multiplier? */
1134
1135 double
1136 att_combat_eff(struct combat *com)
1137 {
1138     double eff = 1.0;
1139     double str;
1140     struct shpstr ship;
1141
1142     if (com->type == EF_SECTOR) {
1143         eff = com->eff / 100.0;
1144         if (com->own == player->cnum) {
1145             str = com->sct_dcp->d_ostr;
1146             eff = 1.0 + ((str - 1.0) * eff);
1147         } else
1148             eff = sector_strength(getsectp(com->x, com->y));
1149 /*                      str = com->sct_dcp->d_dstr;*/
1150     } else if (com->type == EF_SHIP && com->own != player->cnum) {
1151         getship(com->shp_uid, &ship);
1152         eff = (1.0 + ship.shp_armor / 100.0);
1153     }
1154     return eff;
1155 }
1156
1157 /*
1158  * Estimate the defense strength and give the attacker a chance to abort
1159  * if the odds are less than 50%
1160  */
1161
1162 int
1163 att_estimate_defense(int combat_mode, struct combat *off,
1164                      struct emp_qelem *olist, struct combat *def,
1165                      int a_spy)
1166 {
1167     int ototal;
1168     int estimate;
1169     int odds;
1170
1171     /*
1172      * Get the attacker units & mil again in case they changed while the
1173      * attacker was answering sub-prompts.
1174      */
1175
1176     ototal = get_ototal(combat_mode, off, olist, 1.0, 1);
1177     if (att_empty_attack(combat_mode, ototal, def))
1178         return abort_attack();
1179     if (combat_mode == A_PARA)
1180         return ototal;
1181     pr("\n             Initial attack strength: %8d\n", ototal);
1182
1183     estimate = att_combat_eff(def) * roundintby(def->troops, 10);
1184     estimate += att_combat_eff(def) * get_dlist(def, 0, a_spy, 0);
1185
1186     /*
1187      * Calculate the initial (pre-support) attack odds.  If they're less
1188      * than 50%, ask for a confirmation.
1189      */
1190
1191     odds = (int)(att_calcodds(ototal, estimate) * 100);
1192
1193 /*
1194         if (odds < 50) {
1195                 pr("          Estimated defense strength: %8d\n", estimate);
1196                 pr("                      Estimated odds: %8d%%\n\n",odds);
1197                 sprintf(prompt, "Are you sure you want to %s [yn]? ",
1198                         att_mode[combat_mode]);
1199                 if (!confirm(prompt))
1200                         return abort_attack();
1201                 ototal = get_ototal(combat_mode, off, olist,1.0,1);
1202                 if (att_empty_attack(combat_mode, ototal, def))
1203                         return abort_attack();
1204         }
1205  */
1206     return ototal;
1207 }
1208
1209 /* Get the defensive units and reacting units */
1210 int
1211 att_get_defense(struct emp_qelem *olist, struct combat *def,
1212                 struct emp_qelem *dlist, int a_spy, int ototal)
1213 {
1214     int d_spy = 0;
1215     struct emp_qelem *qp;
1216     struct llist *llp;
1217     int dtotal;
1218     int old_dtotal;
1219
1220     emp_initque(dlist);
1221     get_dlist(def, dlist, 0, &d_spy);
1222     dtotal = get_dtotal(def, dlist, 1.0, 0);
1223
1224     /*
1225      * Call in reacting units
1226      */
1227
1228     if (def->type == EF_SECTOR && def->sct_type != SCT_MOUNT)
1229         att_reacting_units(def, dlist, a_spy, &d_spy, ototal);
1230
1231     for (qp = olist->q_forw; qp != olist; qp = qp->q_forw) {
1232         llp = (struct llist *)qp;
1233         intelligence_report(def->own, &llp->land, d_spy,
1234                             "Scouts report attacking unit:");
1235     }
1236
1237     old_dtotal = dtotal;
1238     dtotal = get_dtotal(def, dlist, 1.0, 0);
1239     if (dtotal != old_dtotal)
1240         pr("Defense strength with reacting units: %8d\n", dtotal);
1241
1242     return dtotal;
1243 }
1244
1245 /* Get the defensive land units in the sector or on the ship */
1246
1247 static int
1248 get_dlist(struct combat *def, struct emp_qelem *list, int a_spy,
1249           int *d_spyp)
1250 {
1251     struct nstr_item ni;
1252     struct llist *llp;
1253     struct lndstr land;
1254     int estimate = 0;
1255
1256 /* In here is where you need to take out spies and trains from the defending
1257    lists.  Spies try to hide, trains get trapped and can be boarded. */
1258
1259     snxtitem_xy(&ni, EF_LAND, def->x, def->y);
1260     while (nxtitem(&ni, &land)) {
1261         if (!land.lnd_own)
1262             continue;
1263         if (land.lnd_own != def->own)
1264             continue;
1265         if (def->type == EF_SECTOR && land.lnd_ship >= 0)
1266             continue;
1267         if (def->type == EF_SECTOR && land.lnd_land >= 0)
1268             continue;
1269         if (def->type == EF_SHIP && land.lnd_ship != def->shp_uid)
1270             continue;
1271         if (def->type == EF_LAND && land.lnd_land != def->lnd_uid)
1272             continue;
1273         if (!list) {            /* Just estimating the enemy strength */
1274             estimate += intelligence_report(player->cnum,
1275                                             &land, a_spy,
1276                                             "Scouts report defending unit:");
1277             continue;
1278         }
1279         if (!(llp = malloc(sizeof(struct llist)))) {
1280             logerror("Malloc failed in attack!\n");
1281             abort_attack();
1282             return 0;
1283         }
1284         memset(llp, 0, sizeof(struct llist));
1285         emp_insque(&llp->queue, list);
1286         llp->supplied = has_supply(&land);
1287         if (!get_land(A_DEFEND, def, land.lnd_uid, llp, 1))
1288             continue;
1289         if (lnd_spyval(&land) > *d_spyp)
1290             *d_spyp = lnd_spyval(&land);
1291     }
1292     return estimate;
1293 }
1294
1295 /* Calculate the total offensive strength */
1296
1297 static int
1298 get_ototal(int combat_mode, struct combat *off, struct emp_qelem *olist,
1299            double osupport, int check)
1300 {
1301     double ototal = 0.0;
1302     struct emp_qelem *qp, *next;
1303     struct llist *llp;
1304     int n, w;
1305
1306     /*
1307      * first, total the attacking mil
1308      */
1309
1310     for (n = 0; n <= off->last; ++n) {
1311         if (off[n].type == EF_BAD || (check &&
1312                                       att_get_combat(&off[n], 0) <= 0))
1313             continue;
1314         ototal += off[n].troops * att_combat_eff(off + n);
1315     }
1316
1317     /*
1318      * next, add in the attack_values of all
1319      * the attacking units
1320      */
1321
1322     for (qp = olist->q_forw; qp != olist; qp = next) {
1323         next = qp->q_forw;
1324         llp = (struct llist *)qp;
1325         if (check && !get_land(combat_mode, 0, llp->land.lnd_uid, llp, 0))
1326             continue;
1327         if (combat_mode == A_ATTACK) {
1328             w = -1;
1329             for (n = 0; n <= off->last; ++n) {
1330                 if (off[n].type == EF_BAD)
1331                     continue;
1332                 if ((off[n].x == llp->land.lnd_x) &&
1333                     (off[n].y == llp->land.lnd_y))
1334                     w = n;
1335             }
1336             if (w < 0) {
1337                 lnd_delete(llp, "is in a sector not owned by you");
1338                 continue;
1339             }
1340             ototal += attack_val(combat_mode, &llp->land) *
1341                 att_combat_eff(off + w);
1342         } else {
1343             ototal += attack_val(combat_mode, &llp->land);
1344         }
1345     }
1346     ototal *= osupport;
1347
1348     return ldround(ototal, 1);
1349 }
1350
1351 /* Calculate the total defensive strength */
1352
1353 static int
1354 get_dtotal(struct combat *def, struct emp_qelem *list, double dsupport,
1355            int check)
1356 {
1357     double dtotal = 0.0, eff = 1.0, d_unit;
1358     struct emp_qelem *qp, *next;
1359     struct llist *llp;
1360
1361     if (check && att_get_combat(def, 1) < 0)
1362         return 0;
1363     eff = att_combat_eff(def);
1364     dtotal = def->troops * eff;
1365
1366     /*
1367      * next, add in the defense_values of all
1368      * the defending non-retreating units
1369      */
1370
1371     for (qp = list->q_forw; qp != list; qp = next) {
1372         next = qp->q_forw;
1373         llp = (struct llist *)qp;
1374         d_unit = 0.0;
1375         if (check && !get_land(A_DEFEND, def, llp->land.lnd_uid, llp, 1))
1376             continue;
1377         d_unit = defense_val(&llp->land);
1378         if (!llp->supplied)
1379             d_unit = ((double)defense_val(&llp->land) / 2.0);
1380         dtotal += d_unit * eff;
1381     }
1382
1383     dtotal *= dsupport;
1384
1385     return ldround(dtotal, 1);
1386 }
1387
1388 /*
1389  * This is the land unit integrity check.  Note that we don't print
1390  * warnings about victim land units because the attacker may not have seen them
1391  */
1392
1393 static int
1394 get_land(int combat_mode, struct combat *def, int uid, struct llist *llp,
1395          int victim_land)
1396 {
1397     struct lndstr *lp = &llp->land;
1398     s_char buf[512];
1399
1400     getland(uid, lp);
1401
1402     if (!llp->lcp) {            /* first time */
1403         llp->x = llp->land.lnd_x;
1404         llp->y = llp->land.lnd_y;
1405         llp->lcp = &lchr[(int)llp->land.lnd_type];
1406     } else {                    /* not first time */
1407         if (lp->lnd_effic < LAND_MINEFF) {
1408             sprintf(buf, "was destroyed and is no longer a part of the %s",
1409                     att_mode[combat_mode]);
1410             lnd_delete(llp, buf);
1411             return 0;
1412         }
1413         if (victim_land) {
1414             if (lp->lnd_x != def->x || lp->lnd_y != def->y) {
1415                 lnd_delete(llp,
1416                            "left to go fight another battle and is no longer a part of the defense");
1417                 return 0;
1418             }
1419         } else {
1420             if (lp->lnd_own != player->cnum) {
1421                 sprintf(buf,
1422                         "was destroyed and is no longer a part of the %s",
1423                         att_mode[combat_mode]);
1424                 lnd_delete(llp, buf);
1425                 return 0;
1426             }
1427             if (lp->lnd_x != llp->x || lp->lnd_y != llp->y) {
1428                 sprintf(buf,
1429                         "left to fight another battle and is no longer a part of the %s",
1430                         att_mode[combat_mode]);
1431                 lnd_delete(llp, buf);
1432                 return 0;
1433             }
1434             if (lp->lnd_effic < llp->eff) {
1435                 sprintf(buf, "damaged from %d%% to %d%%",
1436                         llp->eff, lp->lnd_effic);
1437                 lnd_print(llp, buf);
1438             }
1439         }
1440     }
1441     llp->eff = llp->land.lnd_effic;
1442
1443     return 1;
1444 }
1445
1446 /*
1447  * Put the land unit on the disk.  If there was some mobility cost, then
1448  * subtract it from the units mobility.  Note that this works the same way
1449  * as sectors & ships in that no mobility is actually taken until the attacker
1450  * has committed to attacking.
1451  */
1452
1453 static void
1454 kill_land(struct emp_qelem *list)
1455 {
1456     struct emp_qelem *qp, *next;
1457     struct llist *llp;
1458
1459     for (qp = list->q_forw; qp != list; qp = next) {
1460         next = qp->q_forw;
1461         llp = (struct llist *)qp;
1462         if (llp->land.lnd_ship >= 0) {
1463             llp->land.lnd_effic = 0;
1464             lnd_delete(llp, "cannot return to the ship, and dies!");
1465         }
1466     }
1467 }
1468
1469 static void
1470 att_infect_units(struct emp_qelem *list, int plague)
1471 {
1472     struct emp_qelem *qp, *next;
1473     struct llist *llp;
1474
1475     if (!plague)
1476         return;
1477     for (qp = list->q_forw; qp != list; qp = next) {
1478         next = qp->q_forw;
1479         llp = (struct llist *)qp;
1480         if (llp->land.lnd_pstage == PLG_HEALTHY)
1481             llp->land.lnd_pstage = PLG_EXPOSED;
1482     }
1483 }
1484
1485 static void
1486 put_land(struct emp_qelem *list)
1487 {
1488     struct emp_qelem *qp, *next;
1489     struct llist *llp;
1490
1491     for (qp = list->q_forw; qp != list; qp = next) {
1492         next = qp->q_forw;
1493         llp = (struct llist *)qp;
1494         llp->land.lnd_mission = 0;
1495         llp->land.lnd_harden = 0;
1496         llp->land.lnd_mobil -= (int)llp->mobil;
1497         llp->mobil = 0.0;
1498         putland(llp->land.lnd_uid, &llp->land);
1499         if (llp->land.lnd_own != player->cnum) {
1500             emp_remque((struct emp_qelem *)llp);
1501             free(llp);
1502         } else
1503             get_land(A_ATTACK, 0, llp->land.lnd_uid, llp, 0);
1504     }
1505 }
1506
1507 /*
1508  * Keep sending in reinforcements until it looks like we're going to win.
1509  * Note that the "strength" command also calls this routine.
1510  */
1511
1512 int
1513 att_reacting_units(struct combat *def, struct emp_qelem *list, int a_spy,
1514                    int *d_spyp, int ototal)
1515 {
1516     struct nstr_item ni;
1517     struct lndstr land;
1518     struct sctstr sect, dsect;
1519     struct llist *llp;
1520     int dtotal;
1521     int new_land = 0;
1522     double mobcost;
1523     double move_cost;
1524     int supply_state;
1525     int dist;
1526     int radius;
1527     int origx, origy;
1528     double eff = att_combat_eff(def);
1529     s_char *p;
1530     s_char buf[1024];
1531
1532     /*
1533      *
1534      * All units that are within their reaction radius and not damaged
1535      * below their morale value now get to react to the threatened sect.
1536      * Once we've sent enough to counter the threat, stop sending them.
1537      *
1538      * Not anymore.  All units get to react. :)
1539      */
1540
1541     if (list)
1542         dtotal = get_dtotal(def, list, 1.0, 1);
1543     else
1544         dtotal = 0;
1545     snxtitem_all(&ni, EF_LAND);
1546     while (nxtitem(&ni, &land) &&
1547            (dtotal + new_land * eff < (int)(1.2 * (float)ototal))) {
1548         if (!land.lnd_own)
1549             continue;
1550         if (!land.lnd_rad_max)
1551             continue;
1552         if ((land.lnd_x == def->x) && (land.lnd_y == def->y))
1553             continue;
1554         if (land.lnd_own != def->own)
1555             continue;
1556         if (land.lnd_ship >= 0)
1557             continue;
1558         if (!defense_val(&land))
1559             continue;
1560 /*
1561                 if (land.lnd_effic <= land.lnd_retreat)
1562                         continue;
1563  */
1564         if (!lnd_can_attack(&land))
1565             continue;
1566
1567         /* Only supplied units can react */
1568         if (!(supply_state = has_supply(&land)))
1569             continue;
1570
1571         dist = mapdist(land.lnd_x, land.lnd_y, def->x, def->y);
1572
1573         getsect(land.lnd_x, land.lnd_y, &sect);
1574         /* Units on efficient headquarters can react 1 farther */
1575         if ((sect.sct_type == SCT_HEADQ) && (sect.sct_effic >= 60))
1576             radius = land.lnd_rad_max + 1;
1577         else
1578             radius = land.lnd_rad_max;
1579
1580         if (land.lnd_mission == MI_RESERVE)
1581             radius += 2;
1582
1583         if (dist > radius)
1584             continue;
1585
1586         getsect(def->x, def->y, &dsect);
1587         if (!(p = BestLandPath(buf, &sect, &dsect, &move_cost, MOB_ROAD)))
1588             continue;
1589
1590         mobcost = land.lnd_effic * 0.01 * lchr[(int)land.lnd_type].l_spd;
1591         if (mobcost < 0.01)
1592             mobcost = 0.01;
1593         mobcost = 480.0 / (mobcost + techfact(land.lnd_tech, mobcost));
1594         mobcost *= (move_cost * 5.0);
1595
1596         if (land.lnd_mobil < mobcost)
1597             continue;
1598
1599         new_land += defense_val(&land);
1600
1601         if (!list)              /* we are in the "strength" command */
1602             continue;
1603
1604         /* move to defending sector */
1605         land.lnd_mobil -= ldround(mobcost, 1);
1606         origx = land.lnd_x;
1607         origy = land.lnd_y;
1608         land.lnd_x = def->x;
1609         land.lnd_y = def->y;
1610         putland(land.lnd_uid, &land);
1611         wu(0, land.lnd_own, "%s reacts to %s.\n",
1612            prland(&land), xyas(land.lnd_x, land.lnd_y, land.lnd_own));
1613
1614         llp = (struct llist *)
1615             malloc(sizeof(struct llist));
1616
1617         memset(llp, 0, sizeof(struct llist));
1618         llp->supplied = supply_state;
1619         llp->x = origx;
1620         llp->y = origy;
1621         llp->lcp = &lchr[(int)land.lnd_type];
1622         llp->land = land;
1623         emp_insque(&llp->queue, list);
1624         if (lnd_spyval(&land) > *d_spyp)
1625             *d_spyp = lnd_spyval(&land);
1626
1627         intelligence_report(player->cnum, &land, a_spy,
1628                             "Scouts sight reacting enemy unit:");
1629     }
1630     return new_land;
1631 }
1632
1633 /* Pop off shells and fly bombing missions to get your attack multiplier up */
1634
1635 static double
1636 get_osupport(s_char *outs, struct combat *def, int fort_sup, int ship_sup,
1637              int land_sup, int plane_sup)
1638 {
1639     double osupport = 1.0;
1640     int dam;
1641     double af, as, au, ap;
1642
1643     af = as = au = ap = 0.0;
1644     if (fort_sup) {
1645         dam = dd(def->own, player->cnum, def->x, def->y, 0, 0);
1646         af = ((double)dam / 100.0);
1647         osupport += af;
1648     }
1649     if (ship_sup) {
1650         dam = sd(def->own, player->cnum, def->x, def->y, 0, 0, 0);
1651
1652         as = ((double)dam / 100.0);
1653         osupport += as;
1654     }
1655
1656     if (land_sup) {
1657         dam = lnd_support(def->own, player->cnum, def->x, def->y, 0);
1658         au = ((double)dam / 100.0);
1659         osupport += au;
1660     }
1661
1662     if (plane_sup) {
1663         dam = off_support(def->x, def->y, def->own, player->cnum);
1664         ap = (((double)dam) / 100.0);
1665         osupport += ap;
1666     }
1667     sprintf(outs, "attacker\t%1.2f\t%1.2f\t%1.2f\t%1.2f\n", af, as, au,
1668             ap);
1669     return osupport;
1670 }
1671
1672 /* Pop off shells and fly bombing missions to get your defense multiplier up */
1673
1674 static double
1675 get_dsupport(s_char *outs, struct emp_qelem *list, struct combat *def,
1676              int ototal, int dtotal)
1677 {
1678     double dsupport = 1.0;
1679     int dam;
1680     double df, ds, du, dp;
1681     int good = 0;
1682
1683     df = ds = du = dp = 0.0;
1684     if (dtotal < 0.1 * ototal) {
1685         good = -1;
1686     } else if (dtotal >= 1.2 * ototal) {
1687         good = 1;
1688     } else {
1689         dam = dd(player->cnum, def->own, def->x, def->y, 0, 1);
1690         df = ((double)dam / 100.0);
1691         dsupport += df;
1692
1693         dtotal = get_dtotal(def, list, dsupport, 0);
1694         if (dtotal < 1.2 * ototal) {
1695             dam = sd(player->cnum, def->own, def->x, def->y, 0, 1, 0);
1696             ds = ((double)dam / 100.0);
1697             dsupport += ds;
1698             dtotal = get_dtotal(def, list, dsupport, 0);
1699         }
1700         if (dtotal < 1.2 * ototal) {
1701             dam = lnd_support(player->cnum, def->own, def->x, def->y, 1);
1702             du = ((double)dam / 100.0);
1703             dsupport += du;
1704             dtotal = get_dtotal(def, list, dsupport, 1);
1705         }
1706         if (dtotal < 1.2 * ototal) {
1707             dam = def_support(def->x, def->y, player->cnum, def->own);
1708             dp = (((double)dam) / 100.0);
1709             dsupport += dp;
1710         }
1711     }
1712     if (good)
1713         *outs = '\0';
1714     else
1715         sprintf(outs, "defender\t%1.2f\t%1.2f\t%1.2f\t%1.2f\n\n", df, ds,
1716                 du, dp);
1717     if (def->own) {
1718         if (good < 0)
1719             wu(0, def->own,
1720                "\nOdds are bad for us...support cancelled.\n\n");
1721         else if (good > 0)
1722             wu(0, def->own,
1723                "\nOdds are good for us...support cancelled.\n\n");
1724     }
1725     return dsupport;
1726 }
1727
1728 /*
1729  * Land mines add to the defense multiplier.  If the attacker has engineers
1730  * then this multiplier is cut in half.
1731  */
1732
1733 static double
1734 get_mine_dsupport(struct combat *def, int a_engineer)
1735 {
1736     int mines;
1737     struct sctstr sect;
1738
1739     getsect(def->x, def->y, &sect);
1740
1741     if (sect.sct_oldown != player->cnum) {
1742         mines = min(sect.sct_mines, 20);
1743         if (a_engineer)
1744             mines = ldround(((double)mines / 2.0), 1);
1745         if (mines > 0) {
1746             if (def->own)
1747                 wu(0, def->own, "Defending mines add %1.2f\n",
1748                    mines * 0.02);
1749             pr("Defending mines add %1.2f\n", mines * 0.02);
1750             return mines * 0.02;
1751         }
1752     }
1753     return 0.0;
1754 }
1755
1756 /* Get the offensive and defensive support */
1757 int
1758 att_get_support(int combat_mode, int ofort, int oship, int oland,
1759                 int oplane, struct emp_qelem *olist, struct combat *off,
1760                 struct emp_qelem *dlist, struct combat *def,
1761                 double *osupportp, double *dsupportp, int a_engineer)
1762 {
1763     int ototal, dtotal;
1764     s_char osupports[512];
1765     s_char dsupports[512];
1766
1767     if (combat_mode == A_PARA)
1768         *osupports = '\0';
1769     else
1770         *osupportp = get_osupport(osupports, def,
1771                                   ofort, oship, oland, oplane);
1772
1773     /*
1774      * I need to put a 1 at the end of the next four total_stren calls
1775      * becauase units & mil may have been damaged by collateral damage or
1776      * neclear warheads from the offensive & defensive support.
1777      */
1778
1779     ototal = get_ototal(combat_mode, off, olist, *osupportp, 1);
1780     if (att_empty_attack(combat_mode, ototal, def))
1781         return abort_attack();
1782     dtotal = get_dtotal(def, dlist, *dsupportp, 1);
1783
1784     /*
1785      * Calculate defensive support.  If odds are too good or too bad
1786      * then don't call in support.
1787      */
1788
1789     *dsupportp = get_dsupport(dsupports, dlist, def, ototal, dtotal);
1790     ototal = get_ototal(combat_mode, off, olist, *osupportp, 1);
1791     if (att_empty_attack(combat_mode, ototal, def))
1792         return abort_attack();
1793
1794     if ((*osupports || *dsupports) &&
1795         (*osupportp != 1.0 || *dsupportp != 1.0)) {
1796         pr("\n\t\tsupport values\n");
1797         pr("\t\tforts\tships\tunits\tplanes\n");
1798         if (*osupportp != 1.0)
1799             pr("%s", osupports);
1800         if (*dsupportp != 1.0)
1801             pr("%s", dsupports);
1802         if (def->own) {
1803             wu(0, def->own, "\n\t\tsupport values\n");
1804             wu(0, def->own, "\t\tforts\tships\tunits\tplanes\n");
1805             if (*osupportp != 1.0)
1806                 wu(0, def->own, "%s", osupports);
1807             if (*dsupportp != 1.0)
1808                 wu(0, def->own, "%s", dsupports);
1809         }
1810     }
1811
1812     dtotal = get_dtotal(def, dlist, *dsupportp, 1);
1813     if (dtotal && def->type == EF_SECTOR)
1814         *dsupportp += get_mine_dsupport(def, a_engineer);
1815     return 0;
1816 }
1817
1818 /* How many two-legged bipeds are in this combat force? */
1819
1820 static int
1821 count_bodies(struct combat *off, struct emp_qelem *list)
1822 {
1823     int n;
1824     int bodies = 0;
1825     struct emp_qelem *qp;
1826     struct llist *llp;
1827
1828     for (n = 0; n <= off->last; ++n)
1829         bodies += off[n].troops;
1830     for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
1831         llp = (struct llist *)qp;
1832         bodies += total_mil(&llp->land);
1833     }
1834     return bodies;
1835 }
1836
1837 /* This is where the fighting actually occurs. */
1838
1839 int
1840 att_fight(int combat_mode, struct combat *off, struct emp_qelem *olist,
1841           double osupport, struct combat *def, struct emp_qelem *dlist,
1842           double dsupport)
1843 {
1844     int success = 0;
1845     int a_cas = 0;              /* Casualty counts */
1846     int d_cas = 0;
1847     int ototal;                 /* total attacking strength */
1848     int dtotal;                 /* total defending strength */
1849     int a_bodies;               /* total attacking mil (incl. mil in units) */
1850     int d_bodies;               /* total defending mil (incl. mil in units) */
1851     int d_mil;
1852     int a_troops[6];
1853     int n;
1854     int news_item;
1855     int recalctime;
1856     double odds;
1857     s_char *action;
1858
1859     ototal = get_ototal(combat_mode, off, olist, osupport,
1860                         combat_mode != A_PARA);
1861     dtotal = get_dtotal(def, dlist, dsupport, 0);
1862     if (!dtotal)
1863         success = 1;
1864
1865     a_bodies = count_bodies(off, olist);
1866     d_bodies = count_bodies(def, dlist);
1867     d_mil = def->troops;
1868     for (n = 0; n <= off->last; ++n)
1869         if (off[n].type == EF_BAD)
1870             a_troops[n] = 0;
1871         else
1872             a_troops[n] = off[n].troops;
1873
1874     /* This switch is required to get the spacing right */
1875     switch (combat_mode) {
1876     case A_ATTACK:
1877         pr("               Final attack strength: %8d\n", ototal);
1878         break;
1879     case A_ASSAULT:
1880         pr("              Final assault strength: %8d\n", ototal);
1881         break;
1882     case A_PARA:
1883         if (def->sct_type == SCT_MOUNT ||
1884             def->sct_type == SCT_WATER ||
1885             def->sct_type == SCT_CAPIT ||
1886             def->sct_type == SCT_FORTR || def->sct_type == SCT_WASTE) {
1887             pr("You can't air-assault a %s sector!\n",
1888                def->sct_dcp->d_name);
1889             a_cas = a_bodies;
1890             off[0].troops = 0;
1891             ototal = get_ototal(A_PARA, off, olist, osupport, 0);
1892         }
1893         pr("          Final air-assault strength: %8d\n", ototal);
1894         break;
1895     case A_BOARD:
1896     case A_LBOARD:
1897         pr("                Final board strength: %8d\n", ototal);
1898     }
1899
1900
1901     pr("              Final defense strength: %8d\n", dtotal);
1902     odds = att_calcodds(ototal, dtotal);
1903     pr("                          Final odds: %8d%%\n", (int)(odds * 100));
1904
1905     /* spread the plague */
1906     if (combat_mode != A_PARA) {
1907         if (!def->plague)
1908             for (n = 0; n <= off->last; ++n)
1909                 if (off[n].type != EF_BAD)
1910                     def->plague |= off[n].plague;
1911         for (n = 0; n <= off->last; ++n)
1912             if (off[n].type != EF_BAD)
1913                 off[n].plague |= def->plague;
1914     }
1915     att_infect_units(olist, off->plague);
1916     att_infect_units(dlist, def->plague);
1917
1918     /* Fighting is slightly random.  There is always that last little 
1919      * effort you see people put in.  Or the stray bullet that takes out
1920      * an officer and the rest go into chaos.  Things like that.
1921      * Thus, we have added a very slight random factor that will sometimes
1922      * allow the little guy to win. We modify the odds a little
1923      * (either +- 5%) to account for this randomness.  We also only
1924      * recalculate the odds every 8-50 casualties, not every cacsualty,
1925      * since a single dead guy normally wouldn't cause a commander to
1926      * rethink his strategies, but 50 dead guys might. */
1927     odds = odds + (double)((double)((random() % 11) - 5) / 100.0);
1928     if (odds < 0.0)
1929         odds = 0.1;
1930     if (odds > 1.0)
1931         odds = 1.0;
1932     recalctime = 8 + (random() % 43);
1933     while (!success && ototal) {
1934         if (chance(odds)) {
1935             pr("!");
1936             d_cas += take_casualty(A_DEFEND, def, dlist);
1937             dtotal = get_dtotal(def, dlist, dsupport, 0);
1938             if (!dtotal)
1939                 ++success;
1940         } else {
1941             pr("@");
1942             a_cas += take_casualty(combat_mode, off, olist);
1943             ototal = get_ototal(combat_mode, off, olist, osupport, 0);
1944         }
1945         if (((a_cas + d_cas) % 70) == 69)
1946             pr("\n");
1947         if (recalctime-- <= 0) {
1948             recalctime = 8 + (random() % 43);
1949             odds = att_calcodds(ototal, dtotal);
1950             odds = odds + (double)((double)((random() % 11) - 5) / 100.0);
1951             if (odds < 0.0)
1952                 odds = 0.1;
1953             if (odds > 1.0)
1954                 odds = 1.0;
1955         }
1956     }
1957     pr("\n");
1958     /* update defense mobility & mil */
1959     if (success)
1960         def->mil = 0;
1961     else {
1962         if (def->type == EF_SECTOR && d_mil && d_cas) {
1963             int tmob;
1964
1965             /* Make sure we use a positive mobility here */
1966             tmob = ((def->mob < 0) ? -(def->mob) : (def->mob));
1967             def->mobcost =
1968                 min(20, min(1, tmob - damage(tmob, 100 * d_cas / d_mil)));
1969         }
1970         def->mil = def->troops;
1971     }
1972
1973     /* update attack mobility & mil */
1974     for (n = 0; n <= off->last; ++n)
1975         if (off[n].type != EF_BAD && off[n].troops < a_troops[n]) {
1976             if (off[n].type == EF_SECTOR && off[n].mil)
1977                 off[n].mobcost +=
1978                     min(20,
1979                         min(1,
1980                             off[n].mob - damage(off[n].mob,
1981                                                 100 * (a_troops[n] -
1982                                                        off[n].troops) /
1983                                                 off[n].mil)));
1984             off[n].mil -= a_troops[n] - off[n].troops;
1985         }
1986
1987     /* update land unit mobility */
1988     if (d_bodies && d_cas)
1989         lnd_takemob(dlist, (double)d_cas / d_bodies);
1990     if (a_bodies && a_cas)
1991         lnd_takemob(olist, (double)a_cas / a_bodies);
1992
1993     /* damage attacked sector */
1994     def->eff = effdamage(def->eff, (d_cas + a_cas) / 10);
1995
1996     pr("- Casualties -\n     Yours: %d\n", a_cas);
1997     pr("    Theirs: %d\n", d_cas);
1998     pr("Papershuffling ... %.1f B.T.U\n", (d_cas + a_cas) * 0.15);
1999     player->btused += (int)((d_cas + a_cas) * 0.015 + 0.5);
2000
2001     if (success) {
2002         switch (combat_mode) {
2003         case A_ATTACK:
2004             news_item = def->own ? N_WON_SECT : N_TOOK_UNOCC;
2005             pr("We have captured %s, sir!\n", prcom(0, def));
2006             action = "taking";
2007             break;
2008         case A_ASSAULT:
2009             news_item = def->own ? N_AWON_SECT : N_START_COL;
2010             pr("We have secured a beachhead at %s, sir!\n", prcom(0, def));
2011             action = "assaulting and taking";
2012             break;
2013         case A_PARA:
2014             news_item = def->own ? N_PWON_SECT : N_PARA_UNOCC;
2015             pr("We have captured %s, sir!\n", prcom(0, def));
2016             action = "air-assaulting and taking";
2017             break;
2018         case A_BOARD:
2019             news_item = N_BOARD_SHIP;
2020             pr("We have boarded %s, sir!\n", prcom(0, def));
2021             action = "boarding";
2022             break;
2023         case A_LBOARD:
2024             news_item = N_BOARD_LAND;
2025             pr("We have boarded %s, sir!\n", prcom(0, def));
2026             action = "boarding";
2027             break;
2028         }
2029     } else {
2030         switch (combat_mode) {
2031         case A_ATTACK:
2032             news_item = N_SCT_LOSE;
2033             pr("You have been defeated!\n");
2034             action = "attacking";
2035             break;
2036         case A_ASSAULT:
2037             news_item = N_ALOSE_SCT;
2038             pr("You have been defeated!\n");
2039             kill_land(olist);
2040             action = "trying to assault";
2041             break;
2042         case A_PARA:
2043             news_item = N_PLOSE_SCT;
2044             pr("All of your troops were destroyed\n");
2045             action = "trying to air-assault";
2046             break;
2047         case A_BOARD:
2048             news_item = N_SHP_LOSE;
2049             pr("You have been repelled\n");
2050             kill_land(olist);
2051             action = "trying to board";
2052             break;
2053         case A_LBOARD:
2054             news_item = N_LND_LOSE;
2055             pr("You have been repelled\n");
2056             kill_land(olist);
2057             action = "trying to board";
2058             break;
2059         }
2060     }
2061     nreport(player->cnum, news_item, def->own, 1);
2062     if (def->own) {
2063         wu(0, def->own,
2064            "%s (#%d) lost %d troops %s %s\nWe lost %d troops defending\n",
2065            cname(player->cnum), player->cnum, a_cas,
2066            action, pr_com(0, def, def->own), d_cas);
2067     }
2068
2069     if (opt_SNEAK_ATTACK) {
2070         do_sneak(def, success);
2071     }
2072
2073     send_reacting_units_home(dlist);
2074
2075     /* putland the defending land */
2076     lnd_put(dlist, 0);
2077
2078     /* putland the attacking land */
2079     put_land(olist);
2080
2081     /* put the victim sector/ship/land */
2082     if (!success || !take_def(combat_mode, olist, off, def))
2083         put_combat(def);
2084
2085     /* put the attacking sectors/ship */
2086     for (n = 0; n <= off->last; ++n)
2087         if (off[n].type != EF_BAD)
2088             put_combat(&off[n]);
2089
2090     if (!success)
2091         return 0;
2092
2093     switch (combat_mode) {
2094     case A_ATTACK:
2095         ask_move_in(off, olist, def);
2096
2097         /* put sectors again to get abandon warnings */
2098         for (n = 0; n <= off->last; ++n)
2099             if (off[n].type != EF_BAD)
2100                 put_combat(&off[n]);
2101         break;
2102     default:
2103         att_move_in_off(combat_mode, off, olist, def);
2104     }
2105     if (def->mil > 0)
2106         pr("%d of your troops now occupy %s\n", def->mil, prcom(0, def));
2107     return 1;
2108 }
2109
2110 /* What percentage of the combat forces going head-to-head are we? */
2111
2112 double
2113 att_calcodds(int ototal, int dtotal)
2114 {
2115     double odds;
2116
2117     /* calculate odds */
2118     if (ototal <= 0)
2119         odds = 0.0;
2120     else if (dtotal <= 0)
2121         odds = 1.0;
2122     else
2123         odds = ((double)ototal) / (dtotal + ototal);
2124
2125     return odds;
2126 }
2127
2128 /* Here's where the dead soldiers get dragged off the battlefield */
2129
2130 static int
2131 take_casualty(int combat_mode, struct combat *off, struct emp_qelem *olist)
2132 {
2133     int to_take = CASUALTY_LUMP;
2134     int biggest_troops = 0, index = -1;
2135     int n, tot_troops = 0, biggest_mil, cas;
2136     struct emp_qelem *qp, *biggest;
2137     struct llist *llp;
2138
2139     for (n = 0; n <= off->last; ++n) {
2140         if (off[n].type != EF_BAD) {
2141             tot_troops += off[n].troops;
2142             if (off[n].troops > biggest_troops) {
2143                 biggest_troops = off[n].troops;
2144                 index = n;
2145             }
2146         }
2147     }
2148
2149     if (tot_troops)
2150         to_take -= tot_troops;
2151
2152     if (to_take >= 0) {
2153         for (n = 0; n <= off->last; ++n)
2154             if (off[n].type != EF_BAD)
2155                 off[n].troops = 0;
2156     } else {
2157         /*
2158          * They can all come off mil.  We rotate the casualties,
2159          * starting with the sector containing the most mil.
2160          */
2161         to_take = CASUALTY_LUMP;
2162         if (index < 0) {
2163             pr("ERROR: Tell the deity that you got the 'green librarian' error\n");
2164             index = 0;
2165         }
2166         while (to_take > 0) {
2167             for (n = index; n <= off->last && to_take; ++n) {
2168                 if (off[n].type != EF_BAD && off[n].troops > 0) {
2169                     --to_take;
2170                     --off[n].troops;
2171                 }
2172             }
2173             for (n = 0; n < index && to_take; ++n) {
2174                 if (off[n].type != EF_BAD && off[n].troops > 0) {
2175                     --to_take;
2176                     --off[n].troops;
2177                 }
2178             }
2179         }
2180         return CASUALTY_LUMP;
2181     }
2182
2183     if (QEMPTY(olist))
2184         return (CASUALTY_LUMP - to_take);
2185
2186     /*
2187      *  Need to take some casualties from attacking units
2188      *  Procedure: find the biggest unit remaining (in
2189      *  terms of mil) and give it the casualties.
2190      */
2191     biggest = (struct emp_qelem *)0;
2192     biggest_mil = -1;
2193     for (qp = olist->q_forw; qp != olist; qp = qp->q_forw) {
2194         llp = (struct llist *)qp;
2195
2196         if (total_mil(&llp->land) > biggest_mil) {
2197             biggest_mil = total_mil(&llp->land);
2198             biggest = qp;
2199         }
2200     }
2201     if (biggest == (struct emp_qelem *)0)
2202         return (CASUALTY_LUMP - to_take);
2203
2204     llp = (struct llist *)biggest;
2205     cas = lnd_take_casualty(combat_mode, llp, to_take);
2206     return (CASUALTY_LUMP - (to_take - cas));
2207 }
2208
2209 /* Send reacting defense units back to where they came from (at no mob cost) */
2210
2211 static void
2212 send_reacting_units_home(struct emp_qelem *list)
2213 {
2214     struct emp_qelem *qp, *next;
2215     struct llist *llp;
2216     s_char buf[1024];
2217
2218     for (qp = list->q_forw; qp != list; qp = next) {
2219         next = qp->q_forw;
2220         llp = (struct llist *)qp;
2221         if ((llp->land.lnd_x != llp->x) || (llp->land.lnd_y != llp->y)) {
2222             sprintf(buf, "returns to %s",
2223                     xyas(llp->x, llp->y, llp->land.lnd_own));
2224             llp->land.lnd_x = llp->x;
2225             llp->land.lnd_y = llp->y;
2226             lnd_delete(llp, buf);
2227         }
2228     }
2229 }
2230
2231 /* Check for 0 offense strength.  This call will always preceed an abort */
2232
2233 int
2234 att_empty_attack(int combat_mode, int ototal, struct combat *def)
2235 {
2236     if (ototal <= 0) {
2237         if (def->own && player->cnum != def->own) {
2238             wu(0, def->own,
2239                "%s (#%d) considered %sing you @%s\n",
2240                cname(player->cnum), player->cnum,
2241                att_mode[combat_mode], xyas(def->x, def->y, def->own));
2242         }
2243         pr("No troops for %s...\n", att_mode[combat_mode]);
2244         return 1;
2245     }
2246     return 0;
2247 }
2248
2249 /*
2250  * Take the defending sector or ship from the defender and give it to the
2251  * attacker.
2252  */
2253
2254 static int
2255 take_def(int combat_mode, struct emp_qelem *list, struct combat *off,
2256          struct combat *def)
2257 {
2258     int n;
2259     int occuppied = 0;
2260     struct llist *llp, *delete_me = 0;
2261     s_char buf[1024];
2262     struct sctstr sect;
2263     struct shpstr ship;
2264     struct lndstr land;
2265
2266     for (n = 0; n <= off->last && !occuppied; ++n) {
2267         if (off[n].type != EF_BAD &&
2268             off[n].troops > 0 &&
2269             (off[n].type != EF_SECTOR || off[n].mob)) {
2270             ++occuppied;
2271             if (def->type == EF_LAND) {
2272                 if (def->lnd_lcp->l_flags & L_SPY) {
2273                     continue;
2274                 }
2275             }
2276             --(off[n].troops);
2277             --(off[n].mil);
2278             ++def->mil;
2279             pr("1 mil from %s moves %s\n",
2280                prcom(0, off + n), prcom(2, def));
2281         }
2282     }
2283     if (!occuppied) {
2284         if (QEMPTY(list)) {
2285             pr("%s left unoccupied\n", prcom(0, def));
2286             if (def->own)
2287                 wu(0, def->own,
2288                    "No enemy troops moved %s so you still own it!\n",
2289                    pr_com(2, def, def->own));
2290             return 0;
2291         } else {
2292             llp = (struct llist *)list->q_forw;
2293             llp->land.lnd_x = def->x;
2294             llp->land.lnd_y = def->y;
2295             take_move_in_mob(combat_mode, llp, off, def);
2296             if (def->type == EF_SHIP) {
2297                 llp->land.lnd_ship = def->shp_uid;
2298                 sprintf(buf, "boards %s", prcom(0, def));
2299                 delete_me = llp;
2300             } else {
2301                 llp->land.lnd_ship = -1;
2302                 sprintf(buf, "moves in to occupy %s",
2303                         xyas(def->x, def->y, player->cnum));
2304                 lnd_delete(llp, buf);
2305             }
2306         }
2307     }
2308     put_combat(def);
2309     if (def->type == EF_SECTOR) {
2310         getsect(def->x, def->y, &sect);
2311         takeover(&sect, player->cnum);
2312         if (sect.sct_type == SCT_CAPIT || sect.sct_type == SCT_MOUNT)
2313             caploss(&sect, def->own,
2314                     "* We have captured %s's capital, sir! *\n");
2315         putsect(&sect);
2316     } else if (def->type == EF_SHIP) {
2317         getship(def->shp_uid, &ship);
2318         takeover_ship(&ship, player->cnum, 1);
2319         putship(ship.shp_uid, &ship);
2320     } else if (def->type == EF_LAND) {
2321         getland(def->lnd_uid, &land);
2322         takeover_land(&land, player->cnum, 1);
2323         putland(land.lnd_uid, &land);
2324     }
2325     if (delete_me)
2326         lnd_delete(delete_me, buf);
2327     att_get_combat(def, 0);
2328     return 1;
2329 }
2330
2331 /*
2332  * Ask the attacker which mil & land units they'd like to move into the
2333  * conquered sector.
2334  */
2335
2336 static void
2337 ask_move_in(struct combat *off, struct emp_qelem *olist,
2338             struct combat *def)
2339 {
2340     int n;
2341     struct emp_qelem *qp, *next;
2342     struct llist *llp;
2343     s_char buf[512];
2344     s_char prompt[512];
2345     s_char land_answer[1024];
2346     s_char *answerp;
2347
2348     for (n = 0; n <= off->last; ++n)
2349         if (off[n].type != EF_BAD && off[n].troops > 0)
2350             if (off[n].mob) {
2351                 ask_move_in_off(&off[n], def);
2352                 if (player->aborted)
2353                     break;
2354             }
2355
2356     if (QEMPTY(olist))
2357         return;
2358     memset(land_answer, 0, sizeof(land_answer));
2359     for (qp = olist->q_forw; qp != olist; qp = next) {
2360         next = qp->q_forw;
2361         llp = (struct llist *)qp;
2362         answerp = &land_answer[(int)llp->land.lnd_army];
2363         if (player->aborted || att_get_combat(def, 0) < 0)
2364             *answerp = 'N';
2365         if (*answerp == 'Y')
2366             continue;
2367         if (*answerp != 'N') {
2368             if (!get_land(A_ATTACK, def, llp->land.lnd_uid, llp, 0))
2369                 continue;
2370             sprintf(prompt, "Move in with %s (%c %d%%) [ynYNq?] ",
2371                     prland(&llp->land),
2372                     llp->land.lnd_army == ' ' ? '~' : llp->land.lnd_army,
2373                     llp->land.lnd_effic);
2374             *answerp = att_prompt(prompt, llp->land.lnd_army);
2375             if (player->aborted || att_get_combat(def, 0) < 0)
2376                 *answerp = 'N';
2377             if (!get_land(A_ATTACK, def, llp->land.lnd_uid, llp, 0))
2378                 continue;
2379         }
2380         if (*answerp == 'y' || *answerp == 'Y')
2381             continue;
2382         sprintf(buf, "stays in %s",
2383                 xyas(llp->land.lnd_x, llp->land.lnd_y, player->cnum));
2384         lnd_delete(llp, buf);
2385     }
2386     if (QEMPTY(olist))
2387         return;
2388     if (att_get_combat(def, 0) < 0) {
2389         for (qp = olist->q_forw; qp != olist; qp = next) {
2390             next = qp->q_forw;
2391             llp = (struct llist *)qp;
2392             if (!get_land(A_ATTACK, def, llp->land.lnd_uid, llp, 0))
2393                 continue;
2394             sprintf(buf, "stays in %s",
2395                     xyas(llp->land.lnd_x, llp->land.lnd_y, player->cnum));
2396             lnd_delete(llp, buf);
2397         }
2398         return;
2399     }
2400     if (opt_INTERDICT_ATT)
2401         lnd_interdict(olist, def->x, def->y, player->cnum);
2402     move_in_land(A_ATTACK, off, olist, def);
2403 }
2404
2405 /* Move offensive land units to the conquered sector or ship */
2406
2407 static void
2408 move_in_land(int combat_mode, struct combat *off, struct emp_qelem *olist,
2409              struct combat *def)
2410 {
2411     struct emp_qelem *qp, *next;
2412     struct llist *llp;
2413     s_char buf[512];
2414
2415     if (QEMPTY(olist))
2416         return;
2417     for (qp = olist->q_forw; qp != olist; qp = next) {
2418         next = qp->q_forw;
2419         llp = (struct llist *)qp;
2420         if (!get_land(combat_mode, def, llp->land.lnd_uid, llp, 0))
2421             continue;
2422         take_move_in_mob(combat_mode, llp, off, def);
2423         llp->land.lnd_x = def->x;
2424         llp->land.lnd_y = def->y;
2425         if (def->type == EF_SHIP)
2426             llp->land.lnd_ship = def->shp_uid;
2427         else
2428             llp->land.lnd_ship = -1;
2429     }
2430     if (QEMPTY(olist))
2431         return;
2432     if (def->type == EF_SECTOR) {
2433         if (opt_INTERDICT_ATT) {
2434             lnd_sweep(olist, 0, 0, def->own);
2435             lnd_check_mines(olist);
2436         }
2437         sprintf(buf, "now occupies %s", prcom(0, def));
2438     } else {
2439         sprintf(buf, "boards %s", prcom(0, def));
2440     }
2441     if (QEMPTY(olist))
2442         return;
2443     for (qp = olist->q_forw; qp != olist; qp = next) {
2444         next = qp->q_forw;
2445         llp = (struct llist *)qp;
2446         lnd_print(llp, buf);
2447     }
2448     if (QEMPTY(olist))
2449         return;
2450     lnd_put(olist, 0);
2451 }
2452
2453 /*
2454  * Move assaulting, paradropping, or boarding mil & units into def
2455  * If the mil are coming from a ship, then pack a lunch.
2456  */
2457
2458 void
2459 att_move_in_off(int combat_mode, struct combat *off,
2460                 struct emp_qelem *olist, struct combat *def)
2461 {
2462     struct sctstr sect;
2463     struct shpstr ship;
2464     int troops;
2465     int n, lunchbox;
2466
2467     move_in_land(combat_mode, off, olist, def);
2468
2469     getsect(def->x, def->y, &sect);
2470
2471     for (n = 0; n <= off->last; ++n) {
2472         if (off[n].type == EF_BAD || !off[n].troops)
2473             continue;
2474         troops = off[n].troops;
2475         off[n].troops = 0;
2476         off[n].mil -= troops;
2477         def->mil += troops;
2478         put_combat(off + n);
2479         if (combat_mode == A_ASSAULT) {
2480             if (off[n].type != EF_SHIP || def->type != EF_SECTOR) {
2481                 logerror("att_move_in_off: strange assault");
2482                 continue;
2483             }
2484             getship(off[n].shp_uid, &ship);
2485             lunchbox = (int)((troops + 1) * ship.shp_item[I_FOOD]
2486                              / (ship.shp_item[I_MILIT] + troops
2487                                 + ship.shp_item[I_CIVIL] + 0.5));
2488             if (lunchbox > ITEM_MAX - sect.sct_item[I_FOOD])
2489                 lunchbox = ITEM_MAX - sect.sct_item[I_FOOD];
2490
2491             ship.shp_item[I_FOOD] -= lunchbox;
2492             sect.sct_item[I_FOOD] += lunchbox;
2493             putship(ship.shp_uid, &ship);
2494         }
2495     }
2496
2497     putsect(&sect);
2498     put_combat(def);
2499 }
2500
2501
2502 /* Ask how many mil to move in from each sector */
2503
2504 static void
2505 ask_move_in_off(struct combat *off, struct combat *def)
2506 {
2507     int mob_support;
2508     int num_mil, dam = 0, left;
2509     double d, weight;
2510     s_char prompt[512];
2511     s_char buf[1024];
2512     s_char *p;
2513
2514     if (att_get_combat(off, 0) <= 0)
2515         return;
2516     if (att_get_combat(def, 0) < 0)
2517         return;
2518     if (off->own != player->cnum)
2519         return;
2520     d = sector_mcost(getsectp(def->x, def->y), MOB_ROAD);
2521     if ((mob_support = min(off->troops, (int)(off->mob / d))) <= 0)
2522         return;
2523     sprintf(prompt, "How many mil to move in from %s (%d max)? ",
2524             xyas(off->x, off->y, player->cnum), mob_support);
2525     if (!(p = getstring(prompt, buf)) || !*p || (num_mil = atoi(p)) <= 0) {
2526         num_mil = 0;
2527         return;
2528     }
2529 /* Make sure we don't move in more than we can support mobility-wise */
2530     if (num_mil > mob_support)
2531         num_mil = mob_support;
2532     if (att_get_combat(off, 0) <= 0)
2533         return;
2534     if (att_get_combat(def, 0) < 0)
2535         return;
2536     if ((num_mil = min(off->troops, num_mil)) <= 0) {
2537         pr("No mil moved in from %s\n",
2538            xyas(off->x, off->y, player->cnum));
2539         return;
2540     }
2541     mob_support = max(1, (int)(num_mil * d));
2542     off->mob -= min(off->mob, mob_support);
2543     off->mil -= num_mil;
2544     off->troops -= num_mil;
2545     put_combat(off);
2546     left = num_mil;
2547     weight = num_mil * ichr[I_MILIT].i_lbs;
2548     if (opt_INTERDICT_ATT && chance(weight / 200.0)) {
2549         if (chance(weight / 100.0))
2550             dam +=
2551                 ground_interdict(def->x, def->y, player->cnum, "military");
2552         dam += check_lmines(def->x, def->y, weight);
2553     }
2554
2555     if (dam) {
2556         left = commdamage(num_mil, dam, I_MILIT);
2557         if (left < num_mil) {
2558             if (left) {
2559                 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));
2560             } else {
2561                 pr("All of the mil you were moving were destroyed!\n");
2562             }
2563         }
2564         /* maybe got nuked */
2565         if (att_get_combat(def, 0) < 0)
2566             return;
2567     }
2568     def->mil += left;
2569     put_combat(def);
2570 }
2571
2572
2573 /* Charge land units for moving into a sector or onto a ship */
2574
2575 static void
2576 take_move_in_mob(int combat_mode, struct llist *llp, struct combat *off,
2577                  struct combat *def)
2578 {
2579     int mobcost;
2580     int new;
2581
2582     switch (combat_mode) {
2583     case A_ATTACK:
2584         mobcost =
2585             lnd_mobcost(&llp->land, getsectp(def->x, def->y), MOB_NONE);
2586         new = llp->land.lnd_mobil - mobcost;
2587         if (new < -127)
2588             new = -127;
2589         llp->land.lnd_mobil = new;
2590         break;
2591     case A_ASSAULT:
2592         if (off->shp_mcp->m_flags & M_LAND) {
2593             if (llp->lcp->l_flags & L_MARINE)
2594                 llp->land.lnd_mobil -=
2595                     ((float)etu_per_update * land_mob_scale * 0.5);
2596             else
2597                 llp->land.lnd_mobil -= ((float)etu_per_update
2598                                         * land_mob_scale);
2599         } else {
2600             if (llp->lcp->l_flags & L_MARINE)
2601                 llp->land.lnd_mobil = 0;
2602             else
2603                 llp->land.lnd_mobil = (((float)etu_per_update
2604                                         * land_mob_scale) * (-1));
2605         }
2606         break;
2607     case A_BOARD:
2608         /* I arbitrarily chose the numbers 10 and 40 below -KHS */
2609         if (llp->lcp->l_flags & L_MARINE)
2610             llp->land.lnd_mobil -= 10;
2611         else
2612             llp->land.lnd_mobil -= 40;
2613         break;
2614     }
2615     llp->land.lnd_harden = 0;
2616 }
2617
2618 static void
2619 free_list(struct emp_qelem *list)
2620 {
2621     struct emp_qelem *qp, *next;
2622
2623     if (!list || QEMPTY(list))
2624         return;
2625
2626     qp = list->q_forw;
2627     while (qp != list) {
2628         next = qp->q_forw;
2629         emp_remque(qp);
2630         free(qp);
2631         qp = next;
2632     }
2633 }
2634
2635 int
2636 att_free_lists(struct emp_qelem *olist, struct emp_qelem *dlist)
2637 {
2638     free_list(olist);
2639     free_list(dlist);
2640     return RET_OK;
2641 }
2642
2643 /*
2644  * sector_strength - Everyone starts at 1.  You can get up to a max
2645  *                   of d_dstr, depending on how much you build up the
2646  *                   defenses of the sector. 
2647  */
2648
2649 double
2650 sector_strength(struct sctstr *sp)
2651 {
2652     double d;
2653
2654     d = 1.0;
2655
2656     if (sp->sct_type == SCT_MOUNT)
2657         d = 2.0;
2658
2659     d = d + ((double)(dchr[sp->sct_type].d_dstr - d) *
2660              ((double)sp->sct_defense / 100.0));
2661
2662     if (d > dchr[sp->sct_type].d_dstr)
2663         d = dchr[sp->sct_type].d_dstr;
2664     if (d < 0.1)
2665         d = 0.1;
2666     return d;
2667 }