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