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