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