]> git.pond.sub.org Git - empserver/blob - src/lib/commands/bomb.c
Sectors need space for items, deliveries and distribution thresholds.
[empserver] / src / lib / commands / bomb.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  *  bomb.c: Fly bombing missions
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1986
32  *     Ken Stevens, 1995
33  *     Steve McClure, 1998-2000
34  */
35
36 #include <ctype.h>
37 #include <string.h>
38 #include "misc.h"
39 #include "player.h"
40 #include "var.h"
41 #include "sect.h"
42 #include "ship.h"
43 #include "land.h"
44 #include "item.h"
45 #include "plane.h"
46 #include "nuke.h"
47 #include "retreat.h"
48 #include "xy.h"
49 #include "nsc.h"
50 #include "news.h"
51 #include "file.h"
52 #include "nat.h"
53 #include "path.h"
54 #include "optlist.h"
55 #include "damage.h"
56 #include "commands.h"
57
58 static void pin_bomb(struct emp_qelem *list, struct sctstr *target);
59 static void strat_bomb(struct emp_qelem *list, struct sctstr *target);
60 static void comm_bomb(struct emp_qelem *list, struct sctstr *target);
61 static void eff_bomb(struct emp_qelem *list, struct sctstr *target);
62 static int pinflak_planedamage(struct plnstr *pp, struct plchrstr *pcp,
63                                natid from, int flak);
64 static void plane_bomb(struct emp_qelem *list, struct sctstr *target);
65 static void land_bomb(struct emp_qelem *list, struct sctstr *target);
66 static void ship_bomb(struct emp_qelem *list, struct sctstr *target);
67
68 static int bombcomm[] = {
69     I_CIVIL,
70     I_MILIT,
71     I_SHELL,
72     I_GUN,
73     I_PETROL,
74     I_IRON,
75     I_DUST,
76     I_BAR,
77     I_FOOD,
78     I_OIL,
79     I_LCM,
80     I_HCM,
81     I_UW,
82     I_RAD
83 };
84 static int nbomb = sizeof(bombcomm) / sizeof(int);
85
86 int
87 bomb(void)
88 {
89     s_char *p;
90     int mission_flags;
91     int tech;
92     coord tx, ty;
93     coord ax, ay;
94     int ap_to_target;
95     struct ichrstr *ip;
96     s_char flightpath[MAX_PATH_LEN];
97     struct nstr_item ni_bomb;
98     struct nstr_item ni_esc;
99     coord x, y;
100     struct sctstr target;
101     struct emp_qelem bomb_list;
102     struct emp_qelem esc_list;
103     int wantflags;
104     struct sctstr ap_sect;
105     s_char mission;
106     int rel;
107     struct natstr *natp;
108     s_char buf[1024];
109
110     wantflags = 0;
111     if (!snxtitem(&ni_bomb, EF_PLANE, player->argp[1]))
112         return RET_SYN;
113     if (!snxtitem(&ni_esc, EF_PLANE,
114                   getstarg(player->argp[2], "escort(s)? ", buf)))
115         pr("No escorts...\n");
116     if ((p =
117          getstarg(player->argp[3], "pinpoint, or strategic? ", buf)) == 0)
118         return RET_SYN;
119     mission = *p;
120     if (strchr("ps", mission) == 0)
121         return RET_SYN;
122     if ((p = getstarg(player->argp[4], "assembly point? ", buf)) == 0
123         || *p == 0)
124         return RET_SYN;
125     if (!sarg_xy(p, &x, &y) || !getsect(x, y, &ap_sect))
126         return RET_SYN;
127     if (ap_sect.sct_own && ap_sect.sct_own != player->cnum &&
128         getrel(getnatp(ap_sect.sct_own), player->cnum) != ALLIED) {
129         pr("Assembly point not owned by you or an ally!\n");
130         return RET_SYN;
131     }
132     ax = x;
133     ay = y;
134     if (getpath(flightpath, player->argp[5], ax, ay, 0, 0,
135                 0, P_FLYING) == 0 || *flightpath == 0)
136         return RET_SYN;
137     tx = ax;
138     ty = ay;
139     (void)pathtoxy(flightpath, &tx, &ty, fcost);
140     pr("target sector is %s\n", xyas(tx, ty, player->cnum));
141     getsect(tx, ty, &target);
142     if (target.sct_type == SCT_SANCT) {
143         pr("You can't bomb that sector!\n");
144         return RET_SYN;
145     }
146     ip = 0;
147     ap_to_target = strlen(flightpath);
148     if (*(flightpath + strlen(flightpath) - 1) == 'h')
149         ap_to_target--;
150     pr("range to target is %d\n", ap_to_target);
151     /*
152      * select planes within range
153      */
154     mission_flags = 0;
155     pln_sel(&ni_bomb, &bomb_list, &ap_sect, ap_to_target,
156             2, wantflags, P_M | P_O);
157     pln_sel(&ni_esc, &esc_list, &ap_sect, ap_to_target,
158             2, wantflags | P_F | P_ESC, P_M | P_O);
159     /*
160      * now arm and equip the bombers, transports, whatever.
161      * tech is stored in high 16 bits of mission_flags.
162      * yuck.
163      */
164     tech = 0;
165     mission_flags |= P_X;       /* stealth (shhh) */
166     mission_flags |= P_H;       /* gets turned off if not all choppers */
167     mission_flags =
168         pln_arm(&bomb_list, 2 * ap_to_target, mission, ip, 0,
169                 mission_flags, &tech);
170     if (QEMPTY(&bomb_list)) {
171         pr("No planes could be equipped for the mission.\n");
172         return RET_FAIL;
173     }
174     mission_flags =
175         pln_arm(&esc_list, 2 * ap_to_target, mission, ip, P_F | P_ESC,
176                 mission_flags, &tech);
177     ac_encounter(&bomb_list, &esc_list, ax, ay, flightpath, mission_flags,
178                  0, 0, 0);
179     if (QEMPTY(&bomb_list)) {
180         pr("No planes got through fighter defenses\n");
181     } else {
182         switch (mission) {
183         case 'p':
184             pin_bomb(&bomb_list, &target);
185             break;
186         case 's':
187             if (opt_SLOW_WAR) {
188                 natp = getnatp(player->cnum);
189                 if (target.sct_own) {
190                     rel = getrel(natp, target.sct_own);
191                     if ((rel != AT_WAR) && (player->cnum != target.sct_own)
192                         && (target.sct_own) &&
193                         (target.sct_oldown != player->cnum)) {
194                         pr("You're not at war with them!\n");
195                         pln_put(&bomb_list);
196                         pln_put(&esc_list);
197                         return RET_FAIL;
198                     }
199                 }
200             }
201             nreport(player->cnum, N_SCT_BOMB, target.sct_own, 1);
202             strat_bomb(&bomb_list, &target);
203             break;
204         default:
205             pr("Bad mission %c\n", mission);
206             break;
207         }
208     }
209     pln_put(&bomb_list);
210     pln_put(&esc_list);
211     return RET_OK;
212 }
213
214 static void
215 pin_bomb(struct emp_qelem *list, struct sctstr *target)
216 {
217     struct dchrstr *dcp;
218     int nplanes;
219     int nships;
220     int type;
221     int bad;
222     s_char *p;
223     int vec[I_MAX + 1];
224     struct plist *plp;
225     struct emp_qelem *qp;
226     int bestacc;
227     int nsubs;
228     int nunits;
229     struct natstr *natp;
230     int rel;
231     s_char buf[1024];
232     int i;
233
234     bad = 0;
235     type = target->sct_type;
236     dcp = &dchr[type];
237     pr("Target sector is a %s constructed %s\n",
238        effadv((int)target->sct_effic), dcp->d_name);
239     nsubs = 0;
240     plp = (struct plist *)list->q_forw;
241     if (plp->pcp->pl_flags & P_A) {
242         bestacc = 0;
243         for (qp = list->q_forw; qp != list; qp = qp->q_forw)
244             plp = (struct plist *)qp;
245         if (plp->plane.pln_acc < bestacc)
246             bestacc = plp->plane.pln_acc;
247         nships = num_shipsatxy(target->sct_x, target->sct_y, 0, 0);
248         nsubs = nships - shipsatxy(target->sct_x, target->sct_y, 0, M_SUB);
249         if (nsubs > 0)
250             pr("Some subs are present in the sector.\n");
251     } else {
252         nships = shipsatxy(target->sct_x, target->sct_y, 0, M_SUB);
253     }
254     nplanes = planesatxy(target->sct_x, target->sct_y, 0, 0, list);
255     nunits = unitsatxy(target->sct_x, target->sct_y, 0, 0);
256     getvec(VT_ITEM, vec, (s_char *)target, EF_SECTOR);
257   retry:
258     p = getstring("Bomb what? (ship, plane, land unit, efficiency, commodities) ",
259                   buf);
260     if (p == 0 || *p == 0) {
261         if (player->aborted)
262             return;
263         bad++;
264         if (bad > 2)
265             return;
266         goto retry;
267     }
268     switch (*p) {
269     case 'l':
270         if (opt_SLOW_WAR) {
271             natp = getnatp(player->cnum);
272             if (target->sct_own) {
273                 rel = getrel(natp, target->sct_own);
274                 if ((rel != AT_WAR) && (player->cnum != target->sct_own)
275                     && (target->sct_own) &&
276                     (target->sct_oldown != player->cnum)) {
277                     pr("You're not at war with them!\n");
278                     goto retry;
279                 }
280             }
281         }
282         if (nunits == 0) {
283             pr("no units there\n");
284             goto retry;
285         }
286         land_bomb(list, target);
287         break;
288     case 'p':
289         if (opt_SLOW_WAR) {
290             natp = getnatp(player->cnum);
291             if (target->sct_own) {
292                 rel = getrel(natp, target->sct_own);
293                 if ((rel != AT_WAR) && (player->cnum != target->sct_own)
294                     && (target->sct_own) &&
295                     (target->sct_oldown != player->cnum)) {
296                     pr("You're not at war with them!\n");
297                     goto retry;
298                 }
299             }
300         }
301         if (nplanes == 0) {
302             pr("no planes there\n");
303             goto retry;
304         }
305         plane_bomb(list, target);
306         break;
307     case 's':
308         if (nships == 0) {
309             if (((struct plist *)list->q_forw)->pcp->pl_flags & P_A) {
310                 if (nsubs == 0) {
311                     pr("no ships there\n");
312                     goto retry;
313                 }
314             } else {
315                 pr("no ships there\n");
316                 goto retry;
317             }
318         }
319         ship_bomb(list, target);
320         break;
321     case 'c':
322         if (opt_SLOW_WAR) {
323             natp = getnatp(player->cnum);
324             if (target->sct_own) {
325                 rel = getrel(natp, target->sct_own);
326                 if ((rel != AT_WAR) && (player->cnum != target->sct_own)
327                     && (target->sct_own) &&
328                     (target->sct_oldown != player->cnum)) {
329                     pr("You're not at war with them!\n");
330                     goto retry;
331                 }
332             }
333         }
334
335         for (i = 0; i < nbomb; i++) {
336             if (!vec[bombcomm[i]])
337                 continue;
338             break;
339         }
340         if (i >= nbomb) {
341             pr("No bombable commodities in %s\n",
342                xyas(target->sct_x, target->sct_y, player->cnum));
343             goto retry;
344         }
345         comm_bomb(list, target);
346         break;
347     case 'e':
348         if (opt_SLOW_WAR) {
349             natp = getnatp(player->cnum);
350             if (target->sct_own) {
351                 rel = getrel(natp, target->sct_own);
352                 if ((rel != AT_WAR) && (player->cnum != target->sct_own)
353                     && (target->sct_own) &&
354                     (target->sct_oldown != player->cnum)) {
355                     pr("You're not at war with them!\n");
356                     goto retry;
357                 }
358             }
359         }
360         eff_bomb(list, target);
361         break;
362     case 'q':
363         pr("Aborting mission.\n");
364         return;
365     default:
366         pr("Bad target type.\n");
367         goto retry;
368     }
369 }
370
371 static void
372 eff_bomb(struct emp_qelem *list, struct sctstr *target)
373 {
374     register struct plist *plp;
375     struct emp_qelem *qp;
376     struct sctstr sect;
377     int oldeff, dam = 0;
378     int nukedam;
379
380     for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
381         plp = (struct plist *)qp;
382         if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
383             continue;
384         if (plp->bombs || plp->plane.pln_nuketype != -1)
385             dam +=
386                 pln_damage(&plp->plane, target->sct_x, target->sct_y, 'p',
387                            &nukedam, 1);
388     }
389     if (dam <= 0)               /* dam == 0 if only nukes were delivered */
390         return;
391     getsect(target->sct_x, target->sct_y, &sect);
392     target = &sect;
393     oldeff = target->sct_effic;
394     target->sct_effic = effdamage(target->sct_effic, dam);
395     target->sct_road = effdamage(target->sct_road, dam);
396     target->sct_rail = effdamage(target->sct_rail, dam);
397     target->sct_defense = effdamage(target->sct_defense, dam);
398     if (!opt_DEFENSE_INFRA)
399         target->sct_defense = target->sct_effic;
400     pr("did %d%% damage to efficiency in %s\n",
401        oldeff - target->sct_effic,
402        xyas(target->sct_x, target->sct_y, player->cnum));
403     if (target->sct_own)
404         wu(0, target->sct_own,
405            "%s bombing raid did %d%% damage in %s\n",
406            cname(player->cnum), oldeff - target->sct_effic,
407            xyas(target->sct_x, target->sct_y, target->sct_own));
408     if (target->sct_effic < 20) {
409         if (target->sct_type == SCT_BSPAN)
410             knockdown(target, list);
411         else if (target->sct_type == SCT_BTOWER) {
412             knockdown(target, list);
413             bridgefall(target, list);
414         }
415     }
416     putsect(&sect);
417     collateral_damage(target->sct_x, target->sct_y, dam, list);
418 }
419
420 static void
421 comm_bomb(struct emp_qelem *list, struct sctstr *target)
422 {
423     register struct plist *plp;
424     float b;
425     int i;
426     int amt, before;
427     struct ichrstr *ip;
428     int vec[I_MAX + 1];
429     struct emp_qelem *qp;
430     struct sctstr sect;
431     int dam = 0;
432     int nukedam;
433
434     getvec(VT_ITEM, vec, (s_char *)target, EF_SECTOR);
435     for (i = 0; i < nbomb; i++) {
436         if (vec[bombcomm[i]] == 0)
437             continue;
438         if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
439             continue;
440         ip = &ichr[bombcomm[i]];
441         pr("some %s\n", ip->i_name);
442     }
443     for (;;) {
444         ip = whatitem((s_char *)0, "commodity to bomb? ");
445         if (player->aborted)
446             return;
447         if (!ip)
448             continue;
449
450         for (i = 0; i < nbomb; i++) {
451             if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
452                 continue;
453             if (&ichr[bombcomm[i]] == ip)
454                 break;
455         }
456         if (i == nbomb) {
457             pr("You can't bomb %s!\n", ip->i_name);
458             for (i = 0; i < nbomb; i++) {
459                 if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
460                     continue;
461                 pr(i == 0 ? "Bombable: " : ", ");
462                 pr(ichr[bombcomm[i]].i_name);
463             }
464             pr("\n");
465         } else
466             break;
467     }
468     for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
469         plp = (struct plist *)qp;
470         if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
471             continue;
472         if (plp->bombs || plp->plane.pln_nuketype != -1)
473             dam +=
474                 pln_damage(&plp->plane, target->sct_x, target->sct_y, 'p',
475                            &nukedam, 1);
476     }
477     if (dam <= 0)               /* dam == 0 if only nukes were delivered */
478         return;
479     getsect(target->sct_x, target->sct_y, &sect);
480     target = &sect;
481     before = amt = target->sct_item[ip->i_vtype];
482     target->sct_item[ip->i_vtype] = amt = commdamage(amt, dam, ip->i_vtype);
483     if (before > 0.0)
484         b = 100.0 * (1.0 - ((float)amt / (float)before));
485     else
486         b = 0.0;
487     pr("did %.2f%% damage to %s in %s\n",
488        b, ip->i_name, xyas(target->sct_x, target->sct_y, player->cnum));
489     nreport(player->cnum, N_SCT_BOMB, target->sct_own, 1);
490     if (target->sct_own != 0)
491         wu(0, target->sct_own,
492            "%s precision bombing raid did %.2f%% damage to %s in %s\n",
493            cname(player->cnum), b, ip->i_name,
494            xyas(target->sct_x, target->sct_y, target->sct_own));
495     putsect(&sect);
496     collateral_damage(target->sct_x, target->sct_y, dam, list);
497 }
498
499 static void
500 ship_bomb(struct emp_qelem *list, struct sctstr *target)
501 {
502     struct plist *plp;
503     int onsea;
504     struct mchrstr *mcp;
505     int dam;
506     s_char *q;
507     int n;
508     struct emp_qelem *qp;
509     int shipno;
510     int ignore;
511     struct shpstr ship;
512     int nships = 0;
513     struct shiplook head, *s, *s2;
514     s_char buf[1024];
515     s_char prompt[128];
516     s_char msg[128];
517     int hitchance;
518     int nukedam;
519     int flak;
520     int gun;
521     int shell;
522
523     memset(&head, 0, sizeof(struct shiplook));
524     head.uid = -1;
525     onsea = (target->sct_type == SCT_WATER) ? 1 : 0;
526     for (qp = list->q_forw; qp != list && !player->aborted;
527          qp = qp->q_forw) {
528         if (head.uid != -1) {
529             s = head.next;
530             while (s != (struct shiplook *)0) {
531                 s2 = s;
532                 s = s->next;
533                 free(s2);
534             }
535         }
536         memset(&head, 0, sizeof(struct shiplook));
537         head.uid = -1;
538         plp = (struct plist *)qp;
539         if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
540             continue;
541         if (plp->pcp->pl_flags & P_A)
542             nships = asw_shipsatxy(target->sct_x, target->sct_y, 0, 0,
543                                    &plp->plane, &head);
544         else
545             nships = shipsatxy(target->sct_x, target->sct_y, 0, M_SUB);
546         if (nships == 0) {
547             pr("%s could not find any ships!\n", prplane(&plp->plane));
548             continue;
549         }
550         (void)sprintf(prompt, "%s, %d bombs.  Target ('~' to skip)? ",
551                       prplane(&plp->plane), plp->bombs);
552         ignore = 0;
553         shipno = -1;
554         while (shipno < 0 && !player->aborted && !ignore) {
555             if ((q = getstring(prompt, buf)) == 0 || *q == 0)
556                 continue;
557             if (*q == '~') {
558                 ignore = 1;
559                 continue;
560             }
561             if (*q == '?') {
562                 if (plp->pcp->pl_flags & P_A)
563                     print_found(&head);
564                 else
565                     shipsatxy(target->sct_x, target->sct_y, 0, M_SUB);
566                 continue;
567             }
568             if (*q == 'd')
569                 goto next;
570             if (!isdigit(*q))
571                 continue;
572             n = atoi(q);
573             if (n < 0)
574                 continue;
575             if (getship(n, &ship) && ship.shp_x == target->sct_x &&
576                 ship.shp_y == target->sct_y)
577                 shipno = n;
578         }
579         if (shipno < 0)
580             continue;
581
582         shell = gun = 0;
583         gun = min(ship.shp_item[I_GUN], ship.shp_glim);
584         if (gun > 0) {
585             shell = ship.shp_item[I_SHELL];
586             if (shell <= 0)
587                 shell = supply_commod(ship.shp_own, ship.shp_x,
588                                       ship.shp_y, I_SHELL, 1);
589         }
590         mcp = &mchr[(int)ship.shp_type];
591         if (gun > 0 && shell > 0 && !(mcp->m_flags & M_SUB)) {
592             flak = (int)(techfact(ship.shp_tech, (double)gun) * 2.0);
593             ship.shp_item[I_SHELL] = shell;
594             putship(ship.shp_uid, &ship);
595             sprintf(msg, "Flak! Firing %d guns from ship %s\n",
596                     flak, prship(&ship));
597             PR(ship.shp_own, msg);
598             if (pinflak_planedamage(&plp->plane, plp->pcp, ship.shp_own, flak))
599                 continue;
600         }
601
602         dam = 0;
603         if (plp->plane.pln_nuketype != -1)
604             hitchance = 100;
605         else {
606             hitchance =
607                 pln_hitchance(&plp->plane, shp_hardtarget(&ship), EF_SHIP);
608             pr("%d%% hitchance...", hitchance);
609         }
610         if (roll(100) <= hitchance) {
611             /* pinbombing is more accurate than normal bombing */
612             dam =
613                 2 * pln_damage(&plp->plane, target->sct_x, target->sct_y,
614                                'p', &nukedam, 1);
615         } else {
616             pr("splash\n");
617             /* Bombs that miss have to land somewhere! */
618             dam =
619                 pln_damage(&plp->plane, target->sct_x, target->sct_y, 'p',
620                            &nukedam, 0);
621             collateral_damage(target->sct_x, target->sct_y, dam, list);
622             dam = 0;
623         }
624         if (dam <= 0)           /* dam == 0 if only nukes were delivered */
625             continue;
626         if (mcp->m_flags & M_SUB)
627             nreport(player->cnum, N_SUB_BOMB, ship.shp_own, 1);
628         else
629             nreport(player->cnum, N_SHP_BOMB, ship.shp_own, 1);
630         if (ship.shp_own) {
631             wu(0, ship.shp_own, "%s bombs did %d damage to %s at %s\n",
632                cname(player->cnum), dam,
633                prship(&ship),
634                xyas(target->sct_x, target->sct_y, ship.shp_own));
635         }
636         pr("\n");
637         check_retreat_and_do_shipdamage(&ship, dam);
638         if (ship.shp_rflags & RET_BOMBED)
639             if (((ship.shp_rflags & RET_INJURED) == 0) || !dam)
640                 retreat_ship(&ship, 'b');
641         putship(ship.shp_uid, &ship);
642         getship(n, &ship);
643         if (!ship.shp_own) {
644             pr("%s at %s sunk!\n",
645                prship(&ship),
646                xyas(target->sct_x, target->sct_y, player->cnum));
647         }
648         collateral_damage(target->sct_x, target->sct_y, dam / 2, list);
649       next:
650         ;
651     }
652     s = head.next;
653     while (s != (struct shiplook *)0) {
654         s2 = s;
655         s = s->next;
656         free(s2);
657     }
658 }
659
660 static void
661 plane_bomb(struct emp_qelem *list, struct sctstr *target)
662 {
663     int dam;
664     s_char *q;
665     int n;
666     natid own;
667     struct plnstr plane;
668     struct emp_qelem *qp;
669     int planeno;
670     int ignore;
671     struct plist *plp;
672     s_char prompt[128];
673     s_char buf[1024];
674     int hitchance;
675     int nukedam;
676     int nplanes;
677
678     for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
679         plp = (struct plist *)qp;
680         if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
681             continue;
682         nplanes = planesatxy(target->sct_x, target->sct_y, 0, 0, list);
683         if (nplanes == 0) {
684             pr("%s could not find any planes!\n", prplane(&plp->plane));
685             continue;
686         }
687         (void)sprintf(prompt, "%s, %d bombs.  Target ('~' to skip)? ",
688                       prplane(&plp->plane), plp->bombs);
689         planeno = -1;
690         ignore = 0;
691         while (planeno < 0 && !player->aborted && !ignore) {
692             if ((q = getstring(prompt, buf)) == 0 || *q == 0)
693                 continue;
694             if (*q == '~') {
695                 ignore = 1;
696                 continue;
697             }
698             if (*q == '?') {
699                 planesatxy(target->sct_x, target->sct_y, 0, 0, list);
700                 continue;
701             }
702             if (*q == 'd')
703                 goto next;
704             n = atoi(q);
705             if (n < 0)
706                 continue;
707             if (getplane(n, &plane) &&
708                 plane.pln_x == target->sct_x &&
709                 plane.pln_y == target->sct_y &&
710                 ((plane.pln_flags & PLN_LAUNCHED) == 0) &&
711                 (!ac_isflying(&plane, list)))
712                 planeno = n;
713             else
714                 pr("Plane #%d not spotted\n", n);
715         }
716         if (planeno < 0)
717             continue;
718         dam = 0;
719         if (plp->plane.pln_nuketype != -1)
720             hitchance = 100;
721         else {
722             hitchance = pln_hitchance(&plp->plane, 0, EF_PLANE);
723             pr("%d%% hitchance...", hitchance);
724         }
725         if (roll(100) <= hitchance) {
726             /* pinbombing is more accurate than normal bombing */
727             dam =
728                 2 * pln_damage(&plp->plane, target->sct_x, target->sct_y,
729                                'p', &nukedam, 1);
730         } else {
731             pr("thud\n");
732             /* Bombs that miss have to land somewhere! */
733             dam =
734                 pln_damage(&plp->plane, target->sct_x, target->sct_y, 'p',
735                            &nukedam, 0);
736             collateral_damage(target->sct_x, target->sct_y, dam, list);
737             dam = 0;
738         }
739         if (dam <= 0)           /* dam == 0 if only nukes were delivered */
740             continue;
741         if (dam > 100)
742             dam = 100;
743         own = plane.pln_own;
744         if (dam > plane.pln_effic) {
745             plane.pln_effic = 0;
746             makelost(EF_PLANE, plane.pln_own, plane.pln_uid, plane.pln_x,
747                      plane.pln_y);
748             plane.pln_own = 0;
749         } else
750             plane.pln_effic -= dam;
751         plane.pln_mobil = (dam * plane.pln_mobil / 100.0);
752         if (own == player->cnum) {
753             pr("%s reports %d%% damage\n", prplane(&plane), dam);
754         } else {
755             if (own != 0)
756                 wu(0, own,
757                    "%s pinpoint bombing raid did %d%% damage to %s\n",
758                    cname(player->cnum), dam, prplane(&plane));
759         }
760         nreport(player->cnum, N_DOWN_PLANE, own, 1);
761         if (own != 0)
762             wu(0, own, "%s bombs did %d%% damage to %s at %s\n",
763                cname(player->cnum), dam, prplane(&plane),
764                xyas(target->sct_x, target->sct_y, own));
765         putplane(plane.pln_uid, &plane);
766         collateral_damage(plane.pln_x, plane.pln_y, dam, list);
767       next:
768         ;
769     }
770 }
771
772 static void
773 land_bomb(struct emp_qelem *list, struct sctstr *target)
774 {
775     int dam;
776     s_char *q;
777     int n;
778     natid own;
779     s_char prompt[128];
780     s_char buf[1024];
781     s_char msg[128];
782     struct lndstr land;
783     struct lchrstr *lcp;
784     struct emp_qelem *qp;
785     int unitno;
786     int ignore, flak, hitchance;
787     struct plist *plp;
788     int nukedam;
789     int nunits;
790
791     for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
792         plp = (struct plist *)qp;
793         if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
794             continue;
795         nunits = unitsatxy(target->sct_x, target->sct_y, 0, 0);
796         if (nunits == 0) {
797             pr("%s could not find any units!\n", prplane(&plp->plane));
798             continue;
799         }
800         (void)sprintf(prompt, "%s, %d bombs.  Target ('~' to skip)? ",
801                       prplane(&plp->plane), plp->bombs);
802         unitno = -1;
803         ignore = 0;
804         while (unitno < 0 && !player->aborted && !ignore) {
805             if ((q = getstring(prompt, buf)) == 0 || *q == 0)
806                 continue;
807             if (*q == '~') {
808                 ignore = 1;
809                 continue;
810             }
811             if (*q == '?') {
812                 unitsatxy(target->sct_x, target->sct_y, 0, 0);
813                 continue;
814             }
815             if (*q == 'd')
816                 goto next;
817             n = atoi(q);
818             if (n < 0)
819                 continue;
820             if (getland(n, &land) &&
821                 land.lnd_x == target->sct_x && land.lnd_y == target->sct_y)
822                 unitno = n;
823             else
824                 pr("Unit #%d not spotted\n", n);
825         }
826         if (unitno < 0)
827             continue;
828
829         lcp = &lchr[(int)land.lnd_type];
830
831         flak = (int)(techfact(land.lnd_tech, (double)land.lnd_aaf) * 3.0);
832         if (flak) {
833             sprintf(msg,
834                     "Flak! Firing flak guns from unit %s (aa rating %d)\n",
835                     prland(&land), land.lnd_aaf);
836             PR(land.lnd_own, msg);
837             if (pinflak_planedamage(&plp->plane, plp->pcp, land.lnd_own, flak))
838                 continue;
839         }
840
841         dam = 0;
842         if (plp->plane.pln_nuketype != -1)
843             hitchance = 100;
844         else {
845             hitchance =
846                 pln_hitchance(&plp->plane, lnd_hardtarget(&land), EF_LAND);
847             pr("%d%% hitchance...", hitchance);
848         }
849         if (roll(100) <= hitchance) {
850             dam =
851                 2 * pln_damage(&plp->plane, target->sct_x, target->sct_y,
852                                'p', &nukedam, 1);
853         } else {
854             pr("thud\n");
855             /* Bombs that miss have to land somewhere! */
856             dam =
857                 pln_damage(&plp->plane, target->sct_x, target->sct_y, 'p',
858                            &nukedam, 0);
859             collateral_damage(target->sct_x, target->sct_y, dam, list);
860             dam = 0;
861         }
862         if (dam <= 0)           /* dam == 0 if only nukes were delivered */
863             continue;
864         if (dam > 100)
865             dam = 100;
866         own = land.lnd_own;
867         if (own != 0)
868             mpr(own,
869                 "%s pinpoint bombing raid did %d damage to %s\n",
870                 cname(player->cnum), dam, prland(&land));
871         check_retreat_and_do_landdamage(&land, dam);
872
873         if (land.lnd_rflags & RET_BOMBED)
874             if (((land.lnd_rflags & RET_INJURED) == 0) || !dam)
875                 retreat_land(&land, 'b');
876         nreport(player->cnum, N_UNIT_BOMB, own, 1);
877         putland(land.lnd_uid, &land);
878         collateral_damage(land.lnd_x, land.lnd_y, dam, list);
879       next:
880         ;
881     }
882 }
883
884 static void
885 strat_bomb(struct emp_qelem *list, struct sctstr *target)
886 {
887     register struct plist *plp;
888     int dam = 0;
889     struct emp_qelem *qp;
890     struct sctstr sect;
891     int nukedam;
892
893     for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
894         plp = (struct plist *)qp;
895         if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
896             continue;
897         if (plp->bombs || plp->plane.pln_nuketype != -1)
898             dam +=
899                 pln_damage(&plp->plane, target->sct_x, target->sct_y, 's',
900                            &nukedam, 1);
901     }
902     if (dam <= 0)               /* dam == 0 if only nukes were delivered */
903         return;
904     getsect(target->sct_x, target->sct_y, &sect);
905     target = &sect;
906     if (target->sct_own)
907         wu(0, target->sct_own, "%s bombing raid did %d damage in %s\n",
908            cname(player->cnum), PERCENT_DAMAGE(dam),
909            xyas(target->sct_x, target->sct_y, target->sct_own));
910
911     sectdamage(target, dam, list);
912
913     pr("did %d damage in %s\n", PERCENT_DAMAGE(dam),
914        xyas(target->sct_x, target->sct_y, player->cnum));
915     putsect(&sect);
916 }
917
918 #define FLAK_MAX 15
919 float lflaktable[16] = { 0.20, 0.20, 0.25, 0.30, 0.35, 0.40, 0.45, 0.50,
920     0.50, 0.55, 0.60, 0.65, 0.70, 0.75, 0.80, 0.85,
921 };
922
923 static int
924 pinflak_planedamage(struct plnstr *pp, struct plchrstr *pcp, natid from,
925                     int flak)
926 {
927     int disp;
928     s_char dmess[255];
929     int eff;
930     s_char mesg[128];
931     struct shpstr ship;
932     struct lndstr land;
933     natid plane_owner;
934     int dam;
935     float mult;
936
937     flak -= (pp->pln_def + 1);
938     if (pcp->pl_flags & P_X)
939         flak -= 2;
940     if (pcp->pl_flags & P_H)
941         flak -= 1;
942     if (flak > 8)
943         mult = lflaktable[FLAK_MAX] * 1.33;
944     else if (flak < -7)
945         mult = lflaktable[0] * 0.66;
946     else {
947         flak += 7;
948         mult = lflaktable[flak];
949     }
950     mult *= flakscale;
951     dam = (int)((roll(8) + 2) * mult);
952     if (dam > 100)
953         dam = 100;
954
955     disp = 0;
956     plane_owner = pp->pln_own;
957     eff = pp->pln_effic;
958     if (dam <= 0)
959         return 0;
960     memset(dmess, 0, sizeof(dmess));
961     eff -= dam;
962     if (eff < 0)
963         eff = 0;
964     if (eff < PLANE_MINEFF) {
965         sprintf(dmess, " -- shot down");
966         disp = 1;
967     } else if (chance((100 - eff) / 100.0)) {
968         sprintf(dmess, " -- aborted with %d%%%% damage", 100 - eff);
969         disp = 2;
970     }
971     sprintf(mesg, "    Flak! %s %s takes %d%s.\n",
972             cname(pp->pln_own), prplane(pp), dam, dmess);
973     PR(plane_owner, mesg);
974
975     pp->pln_effic = eff;
976     if (disp == 1) {
977         if (from != 0)
978             nreport(from, N_DOWN_PLANE, pp->pln_own, 1);
979         if (pp->pln_ship >= 0) {
980             getship(pp->pln_ship, &ship);
981             take_plane_off_ship(pp, &ship);
982         }
983         if (pp->pln_land >= 0) {
984             getland(pp->pln_land, &land);
985             take_plane_off_land(pp, &land);
986         }
987         makelost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x, pp->pln_y);
988         pp->pln_own = 0;
989         putplane(pp->pln_uid, pp);
990     } else
991         putplane(pp->pln_uid, pp);
992
993     if (disp > 0)
994         return 1;
995     return 0;
996 }