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