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