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