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