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