]> git.pond.sub.org Git - empserver/blob - src/lib/commands/bomb.c
Update copyright notice
[empserver] / src / lib / commands / bomb.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2010, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  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  *     Markus Armbruster, 2004-2009
35  */
36
37 #include <config.h>
38
39 #include <ctype.h>
40 #include "commands.h"
41 #include "damage.h"
42 #include "item.h"
43 #include "land.h"
44 #include "news.h"
45 #include "nuke.h"
46 #include "optlist.h"
47 #include "path.h"
48 #include "plane.h"
49 #include "retreat.h"
50 #include "ship.h"
51
52 static void pin_bomb(struct emp_qelem *, struct sctstr *);
53 static void eff_bomb(struct emp_qelem *, struct sctstr *);
54 static void comm_bomb(struct emp_qelem *, struct sctstr *);
55 static void ship_bomb(struct emp_qelem *, struct sctstr *);
56 static void plane_bomb(struct emp_qelem *, struct sctstr *);
57 static void land_bomb(struct emp_qelem *, struct sctstr *);
58 static void strat_bomb(struct emp_qelem *, struct sctstr *);
59 static int changed_plane_aborts(struct plist *);
60 static int pinflak_planedamage(struct plnstr *, struct plchrstr *,
61                                natid, int);
62
63 static i_type bombcomm[] = {
64     I_CIVIL,
65     I_MILIT,
66     I_SHELL,
67     I_GUN,
68     I_PETROL,
69     I_IRON,
70     I_DUST,
71     I_BAR,
72     I_FOOD,
73     I_OIL,
74     I_LCM,
75     I_HCM,
76     I_UW,
77     I_RAD
78 };
79 static int nbomb = sizeof(bombcomm) / sizeof(*bombcomm);
80
81 int
82 bomb(void)
83 {
84     char *p;
85     coord tx, ty;
86     coord ax, ay;
87     int ap_to_target;
88     char flightpath[MAX_PATH_LEN];
89     struct nstr_item ni_bomb;
90     struct nstr_item ni_esc;
91     struct sctstr target;
92     struct emp_qelem bomb_list;
93     struct emp_qelem esc_list;
94     struct sctstr ap_sect;
95     char mission;
96     struct plist *plp;
97     struct emp_qelem *qp, *next;
98     int rel;
99     struct natstr *natp;
100     char buf[1024];
101
102     if (get_planes(&ni_bomb, &ni_esc, player->argp[1], player->argp[2]) < 0)
103         return RET_SYN;
104     p = getstarg(player->argp[3], "pinpoint, or strategic? ", buf);
105     if (!p || !*p)
106         return RET_SYN;
107     mission = *p;
108     if (!strchr("ps", mission))
109         return RET_SYN;
110     if (!get_assembly_point(player->argp[4], &ap_sect, buf))
111         return RET_SYN;
112     ax = ap_sect.sct_x;
113     ay = ap_sect.sct_y;
114     if (!getpath(flightpath, player->argp[5], ax, ay, 0, 0, P_FLYING)
115         || *flightpath == 0)
116         return RET_SYN;
117     tx = ax;
118     ty = ay;
119     (void)pathtoxy(flightpath, &tx, &ty, fcost);
120     pr("target sector is %s\n", xyas(tx, ty, player->cnum));
121     getsect(tx, ty, &target);
122     ap_to_target = strlen(flightpath);
123     if (flightpath[ap_to_target - 1] == 'h')
124         ap_to_target--;
125     pr("range to target is %d\n", ap_to_target);
126     /*
127      * select planes within range
128      */
129     pln_sel(&ni_bomb, &bomb_list, &ap_sect, ap_to_target, 2,
130             P_B | P_T, P_M | P_O);
131     pln_sel(&ni_esc, &esc_list, &ap_sect, ap_to_target, 2,
132             P_ESC | P_F, P_M | P_O);
133     /*
134      * now arm and equip the bombers, transports, whatever.
135      */
136     pln_arm(&bomb_list, 2 * ap_to_target, mission, NULL);
137     if (QEMPTY(&bomb_list)) {
138         pr("No planes could be equipped for the mission.\n");
139         return RET_FAIL;
140     }
141     pln_arm(&esc_list, 2 * ap_to_target, 'e', NULL);
142     ac_encounter(&bomb_list, &esc_list, ax, ay, flightpath, 0);
143     if (QEMPTY(&bomb_list)) {
144         pr("No planes got through fighter defenses\n");
145     } else if (target.sct_type == SCT_SANCT) {
146         pr("You can't bomb that sector!\n");
147     } else {
148         switch (mission) {
149         case 'p':
150             pin_bomb(&bomb_list, &target);
151             for (qp = bomb_list.q_forw; qp != &bomb_list; qp = next) {
152                 next = qp->q_forw;
153                 plp = (struct plist *)qp;
154                 changed_plane_aborts(plp);
155             }
156             for (qp = esc_list.q_forw; qp != &esc_list; qp = next) {
157                 next = qp->q_forw;
158                 plp = (struct plist *)qp;
159                 changed_plane_aborts(plp);
160             }
161             break;
162         case 's':
163             if (opt_SLOW_WAR) {
164                 natp = getnatp(player->cnum);
165                 if (target.sct_own) {
166                     rel = getrel(natp, target.sct_own);
167                     if ((rel != AT_WAR) && (player->cnum != target.sct_own)
168                         && (target.sct_own) &&
169                         (target.sct_oldown != player->cnum)) {
170                         pr("You're not at war with them!\n");
171                         pln_put(&bomb_list);
172                         pln_put(&esc_list);
173                         return RET_FAIL;
174                     }
175                 }
176             }
177             nreport(player->cnum, N_SCT_BOMB, target.sct_own, 1);
178             strat_bomb(&bomb_list, &target);
179             break;
180         default:
181             CANT_REACH();
182         }
183     }
184     pln_put(&bomb_list);
185     pln_put(&esc_list);
186     return RET_OK;
187 }
188
189 static void
190 pin_bomb(struct emp_qelem *list, struct sctstr *target)
191 {
192     struct dchrstr *dcp;
193     int nplanes;
194     int nships;
195     int type;
196     int bad;
197     char *p;
198     int nsubs;
199     int nunits;
200     struct natstr *natp;
201     int rel;
202     char buf[1024];
203     int i;
204
205     bad = 0;
206     type = target->sct_type;
207     dcp = &dchr[type];
208     pr("Target sector is a %s constructed %s\n",
209        effadv((int)target->sct_effic), dcp->d_name);
210     nsubs = 0;
211     nships = shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
212     if (pln_caps(list) & P_A) {
213         nsubs = shipsatxy(target->sct_x, target->sct_y, M_SUB, 0, 1);
214         if (nsubs > 0)
215             pr("Some subs are present in the sector.\n");
216     }
217     nplanes = planesatxy(target->sct_x, target->sct_y, 0, 0);
218     nunits = unitsatxy(target->sct_x, target->sct_y, 0, 0);
219   retry:
220     p = getstring("Bomb what? (ship, plane, land unit, efficiency, commodities) ",
221                   buf);
222     if (!p)
223        return;
224     if (!*p) {
225         bad++;
226         if (bad > 2)
227             return;
228         goto retry;
229     }
230     switch (*p) {
231     case 'l':
232         if (opt_SLOW_WAR) {
233             natp = getnatp(player->cnum);
234             if (target->sct_own) {
235                 rel = getrel(natp, target->sct_own);
236                 if ((rel != AT_WAR) && (player->cnum != target->sct_own)
237                     && (target->sct_own) &&
238                     (target->sct_oldown != player->cnum)) {
239                     pr("You're not at war with them!\n");
240                     goto retry;
241                 }
242             }
243         }
244         if (nunits == 0) {
245             pr("no units there\n");
246             goto retry;
247         }
248         land_bomb(list, target);
249         break;
250     case 'p':
251         if (opt_SLOW_WAR) {
252             natp = getnatp(player->cnum);
253             if (target->sct_own) {
254                 rel = getrel(natp, target->sct_own);
255                 if ((rel != AT_WAR) && (player->cnum != target->sct_own)
256                     && (target->sct_own) &&
257                     (target->sct_oldown != player->cnum)) {
258                     pr("You're not at war with them!\n");
259                     goto retry;
260                 }
261             }
262         }
263         if (nplanes == 0) {
264             pr("no planes there\n");
265             goto retry;
266         }
267         plane_bomb(list, target);
268         break;
269     case 's':
270         if (nships == 0) {
271             if (pln_caps(list) & P_A) {
272                 if (nsubs == 0) {
273                     pr("no ships there\n");
274                     goto retry;
275                 }
276             } else {
277                 pr("no ships there\n");
278                 goto retry;
279             }
280         }
281         ship_bomb(list, target);
282         break;
283     case 'c':
284         if (opt_SLOW_WAR) {
285             natp = getnatp(player->cnum);
286             if (target->sct_own) {
287                 rel = getrel(natp, target->sct_own);
288                 if ((rel != AT_WAR) && (player->cnum != target->sct_own)
289                     && (target->sct_own) &&
290                     (target->sct_oldown != player->cnum)) {
291                     pr("You're not at war with them!\n");
292                     goto retry;
293                 }
294             }
295         }
296
297         for (i = 0; i < nbomb; i++) {
298             if (!target->sct_item[bombcomm[i]])
299                 continue;
300             break;
301         }
302         if (i >= nbomb) {
303             pr("No bombable commodities in %s\n",
304                xyas(target->sct_x, target->sct_y, player->cnum));
305             goto retry;
306         }
307         comm_bomb(list, target);
308         break;
309     case 'e':
310         if (opt_SLOW_WAR) {
311             natp = getnatp(player->cnum);
312             if (target->sct_own) {
313                 rel = getrel(natp, target->sct_own);
314                 if ((rel != AT_WAR) && (player->cnum != target->sct_own)
315                     && (target->sct_own) &&
316                     (target->sct_oldown != player->cnum)) {
317                     pr("You're not at war with them!\n");
318                     goto retry;
319                 }
320             }
321         }
322         eff_bomb(list, target);
323         break;
324     case 'q':
325         pr("Aborting mission.\n");
326         return;
327     default:
328         pr("Bad target type.\n");
329         goto retry;
330     }
331 }
332
333 static void
334 eff_bomb(struct emp_qelem *list, struct sctstr *target)
335 {
336     struct plist *plp;
337     struct emp_qelem *qp, *next;
338     struct sctstr sect;
339     int oldeff, dam = 0;
340
341     for (qp = list->q_forw; qp != list; qp = next) {
342         next = qp->q_forw;
343         plp = (struct plist *)qp;
344         if (changed_plane_aborts(plp))
345             continue;
346         dam += pln_damage(&plp->plane, 'p', 1);
347     }
348     if (dam <= 0)
349         return;
350     getsect(target->sct_x, target->sct_y, &sect);
351     target = &sect;
352     oldeff = target->sct_effic;
353     target->sct_effic = effdamage(target->sct_effic, dam);
354     target->sct_avail = effdamage(target->sct_avail, dam);
355     target->sct_road = effdamage(target->sct_road, dam);
356     target->sct_rail = effdamage(target->sct_rail, dam);
357     target->sct_defense = effdamage(target->sct_defense, dam);
358     pr("did %d%% damage to efficiency in %s\n",
359        oldeff - target->sct_effic,
360        xyas(target->sct_x, target->sct_y, player->cnum));
361     if (target->sct_own)
362         wu(0, target->sct_own,
363            "%s bombing raid did %d%% damage in %s\n",
364            cname(player->cnum), oldeff - target->sct_effic,
365            xyas(target->sct_x, target->sct_y, target->sct_own));
366     bridge_damaged(target);
367     putsect(&sect);
368     collateral_damage(target->sct_x, target->sct_y, dam);
369 }
370
371 static void
372 comm_bomb(struct emp_qelem *list, struct sctstr *target)
373 {
374     struct plist *plp;
375     double b;
376     int i;
377     int amt, before;
378     struct ichrstr *ip;
379     struct emp_qelem *qp, *next;
380     struct sctstr sect;
381     int dam = 0;
382
383     for (i = 0; i < nbomb; i++) {
384         if (target->sct_item[bombcomm[i]] == 0)
385             continue;
386         if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
387             continue;
388         ip = &ichr[bombcomm[i]];
389         pr("some %s\n", ip->i_name);
390     }
391     for (;;) {
392         ip = whatitem(NULL, "commodity to bomb? ");
393         if (player->aborted)
394             return;
395         if (!ip)
396             continue;
397
398         for (i = 0; i < nbomb; i++) {
399             if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
400                 continue;
401             if (&ichr[bombcomm[i]] == ip)
402                 break;
403         }
404         if (i == nbomb) {
405             pr("You can't bomb %s!\n", ip->i_name);
406             for (i = 0; i < nbomb; i++) {
407                 if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
408                     continue;
409                 pr("%s%s", i == 0 ? "Bombable: " : ", ",
410                    ichr[bombcomm[i]].i_name);
411             }
412             pr("\n");
413         } else
414             break;
415     }
416     for (qp = list->q_forw; qp != list; qp = next) {
417         next = qp->q_forw;
418         plp = (struct plist *)qp;
419         if (changed_plane_aborts(plp))
420             continue;
421         dam += pln_damage(&plp->plane, 'p', 1);
422     }
423     if (dam <= 0)
424         return;
425     getsect(target->sct_x, target->sct_y, &sect);
426     target = &sect;
427     before = target->sct_item[ip->i_uid];
428     target->sct_item[ip->i_uid] = amt = commdamage(before, dam, ip->i_uid);
429     if (before > 0.0)
430         b = 100.0 * (1.0 - (double)amt / (double)before);
431     else
432         b = 0.0;
433     pr("did %.2f%% damage to %s in %s\n",
434        b, ip->i_name, xyas(target->sct_x, target->sct_y, player->cnum));
435     nreport(player->cnum, N_SCT_BOMB, target->sct_own, 1);
436     if (target->sct_own != 0)
437         wu(0, target->sct_own,
438            "%s precision bombing raid did %.2f%% damage to %s in %s\n",
439            cname(player->cnum), b, ip->i_name,
440            xyas(target->sct_x, target->sct_y, target->sct_own));
441     putsect(&sect);
442     collateral_damage(target->sct_x, target->sct_y, dam);
443 }
444
445 static void
446 ship_bomb(struct emp_qelem *list, struct sctstr *target)
447 {
448     struct plist *plp;
449     struct mchrstr *mcp;
450     int dam;
451     char *q;
452     int n;
453     struct emp_qelem *qp, *next;
454     int shipno;
455     struct shpstr ship;
456     int nships = 0;
457     struct shiplist *head = NULL;
458     char buf[1024];
459     char prompt[128];
460     int hitchance;
461     int flak;
462     int gun;
463
464     for (qp = list->q_forw; qp != list; qp = next) {
465         next = qp->q_forw;
466         free_shiplist(&head);
467         plp = (struct plist *)qp;
468         if (changed_plane_aborts(plp))
469             continue;
470         if (plp->pcp->pl_flags & P_A)
471             nships = asw_shipsatxy(target->sct_x, target->sct_y, 0, 0,
472                                    &plp->plane, &head);
473         else
474             nships = shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
475         if (nships == 0) {
476             pr("%s could not find any ships!\n", prplane(&plp->plane));
477             continue;
478         }
479         (void)sprintf(prompt, "%s, %d bombs.  Target ('~' to skip)? ",
480                       prplane(&plp->plane), plp->load);
481         shipno = -1;
482         while (shipno < 0) {
483             if (!(q = getstring(prompt, buf)))
484                 goto out;
485             if (*q == 0)
486                 continue;
487             if (*q == '~')
488                 break;
489             if (*q == '?') {
490                 if (plp->pcp->pl_flags & P_A)
491                     print_shiplist(head);
492                 else
493                     shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
494                 continue;
495             }
496             n = atoi(q);
497             if (n < 0)
498                 continue;
499             if ((!(plp->pcp->pl_flags & P_A) || on_shiplist(n, head)) &&
500                 getship(n, &ship) && ship.shp_own &&
501                 ship.shp_x == target->sct_x && ship.shp_y == target->sct_y)
502                 shipno = n;
503             else
504                 pr("Ship #%d not spotted\n", n);
505         }
506         if (shipno < 0)
507             continue;
508         if ((plp->pcp->pl_flags & P_A) && !on_shiplist(shipno, head))
509             continue;
510         if (changed_plane_aborts(plp))
511             continue;
512
513         gun = shp_usable_guns(&ship);
514         mcp = &mchr[(int)ship.shp_type];
515         if (gun > 0 && !(mcp->m_flags & M_SUB)) {
516             flak = (int)(techfact(ship.shp_tech, gun) * 2.0);
517             PR(ship.shp_own, "Flak! Firing %d guns from ship %s\n",
518                flak, prship(&ship));
519             if (pinflak_planedamage(&plp->plane, plp->pcp, ship.shp_own, flak))
520                 continue;
521         }
522
523         dam = 0;
524         if (nuk_on_plane(&plp->plane) >= 0)
525             hitchance = 100;
526         else {
527             hitchance = pln_hitchance(&plp->plane,
528                                       shp_hardtarget(&ship), EF_SHIP);
529             pr("%d%% hitchance...", hitchance);
530         }
531         if (roll(100) <= hitchance) {
532             /* pinbombing is more accurate than normal bombing */
533             dam = 2 * pln_damage(&plp->plane, 'p', 1);
534         } else {
535             pr("splash\n");
536             /* Bombs that miss have to land somewhere! */
537             dam = pln_damage(&plp->plane, 'p', 0);
538             collateral_damage(target->sct_x, target->sct_y, dam);
539             dam = 0;
540         }
541         if (dam <= 0)
542             continue;
543         if (mcp->m_flags & M_SUB)
544             nreport(player->cnum, N_SUB_BOMB, ship.shp_own, 1);
545         else
546             nreport(player->cnum, N_SHP_BOMB, ship.shp_own, 1);
547         if (ship.shp_own) {
548             wu(0, ship.shp_own, "%s bombs did %d damage to %s at %s\n",
549                cname(player->cnum), dam,
550                prship(&ship),
551                xyas(target->sct_x, target->sct_y, ship.shp_own));
552         }
553         pr("\n");
554         check_retreat_and_do_shipdamage(&ship, dam);
555         if (ship.shp_rflags & RET_BOMBED)
556             if (((ship.shp_rflags & RET_INJURED) == 0) || !dam)
557                 retreat_ship(&ship, 'b');
558         putship(ship.shp_uid, &ship);
559         getship(ship.shp_uid, &ship);
560         if (!ship.shp_own) {
561             pr("%s at %s sunk!\n",
562                prship(&ship),
563                xyas(target->sct_x, target->sct_y, player->cnum));
564         }
565         collateral_damage(target->sct_x, target->sct_y, dam / 2);
566     }
567 out:
568     free_shiplist(&head);
569 }
570
571 static void
572 plane_bomb(struct emp_qelem *list, struct sctstr *target)
573 {
574     int dam;
575     char *q;
576     int n;
577     natid own;
578     struct plnstr plane;
579     struct emp_qelem *qp, *next;
580     int planeno;
581     struct plist *plp;
582     char prompt[128];
583     char buf[1024];
584     int hitchance;
585     int nplanes;
586
587     for (qp = list->q_forw; qp != list; qp = next) {
588         next = qp->q_forw;
589         plp = (struct plist *)qp;
590         if (changed_plane_aborts(plp))
591             continue;
592         nplanes = planesatxy(target->sct_x, target->sct_y, 0, 0);
593         if (nplanes == 0) {
594             pr("%s could not find any planes!\n", prplane(&plp->plane));
595             continue;
596         }
597         (void)sprintf(prompt, "%s, %d bombs.  Target ('~' to skip)? ",
598                       prplane(&plp->plane), plp->load);
599         planeno = -1;
600         while (planeno < 0) {
601             if (!(q = getstring(prompt, buf)))
602                 return;
603             if (*q == 0)
604                 continue;
605             if (*q == '~')
606                 break;
607             if (*q == '?') {
608                 planesatxy(target->sct_x, target->sct_y, 0, 0);
609                 continue;
610             }
611             n = atoi(q);
612             if (n < 0)
613                 continue;
614             if (getplane(n, &plane) &&
615                 plane.pln_x == target->sct_x &&
616                 plane.pln_y == target->sct_y &&
617                 plane.pln_ship < 0 && plane.pln_land < 0 &&
618                 !(plane.pln_flags & PLN_LAUNCHED))
619                 planeno = n;
620             else
621                 pr("Plane #%d not spotted\n", n);
622         }
623         if (planeno < 0)
624             continue;
625         if (changed_plane_aborts(plp))
626             continue;
627         dam = 0;
628         if (nuk_on_plane(&plp->plane) >= 0)
629             hitchance = 100;
630         else {
631             hitchance = pln_hitchance(&plp->plane, 0, EF_PLANE);
632             pr("%d%% hitchance...", hitchance);
633         }
634         if (roll(100) <= hitchance) {
635             /* pinbombing is more accurate than normal bombing */
636             dam = 2 * pln_damage(&plp->plane, 'p', 1);
637         } else {
638             pr("thud\n");
639             /* Bombs that miss have to land somewhere! */
640             dam = pln_damage(&plp->plane, 'p', 0);
641             collateral_damage(target->sct_x, target->sct_y, dam);
642             dam = 0;
643         }
644         if (dam <= 0)
645             continue;
646         if (dam > 100)
647             dam = 100;
648         own = plane.pln_own;
649         if (dam > plane.pln_effic)
650             plane.pln_effic = 0;
651         else
652             plane.pln_effic -= dam;
653         plane.pln_mobil = (dam * plane.pln_mobil / 100.0);
654         if (own == player->cnum) {
655             pr("%s reports %d%% damage\n", prplane(&plane), dam);
656         } else {
657             if (own != 0)
658                 wu(0, own,
659                    "%s pinpoint bombing raid did %d%% damage to %s\n",
660                    cname(player->cnum), dam, prplane(&plane));
661         }
662         nreport(player->cnum, N_DOWN_PLANE, own, 1);
663         if (own != 0)
664             wu(0, own, "%s bombs did %d%% damage to %s at %s\n",
665                cname(player->cnum), dam, prplane(&plane),
666                xyas(target->sct_x, target->sct_y, own));
667         putplane(plane.pln_uid, &plane);
668         collateral_damage(target->sct_x, target->sct_y, dam);
669     }
670 }
671
672 static void
673 land_bomb(struct emp_qelem *list, struct sctstr *target)
674 {
675     int dam;
676     char *q;
677     int n;
678     natid own;
679     char prompt[128];
680     char buf[1024];
681     struct lndstr land;
682     struct emp_qelem *qp, *next;
683     int unitno;
684     int aaf, flak, hitchance;
685     struct plist *plp;
686     int nunits;
687
688     for (qp = list->q_forw; qp != list; qp = next) {
689         next = qp->q_forw;
690         plp = (struct plist *)qp;
691         if (changed_plane_aborts(plp))
692             continue;
693         nunits = unitsatxy(target->sct_x, target->sct_y, 0, 0);
694         if (nunits == 0) {
695             pr("%s could not find any units!\n", prplane(&plp->plane));
696             continue;
697         }
698         (void)sprintf(prompt, "%s, %d bombs.  Target ('~' to skip)? ",
699                       prplane(&plp->plane), plp->load);
700         unitno = -1;
701         while (unitno < 0) {
702             if (!(q = getstring(prompt, buf)))
703                 return;
704             if (*q == 0)
705                 continue;
706             if (*q == '~')
707                 break;
708             if (*q == '?') {
709                 unitsatxy(target->sct_x, target->sct_y, 0, 0);
710                 continue;
711             }
712             n = atoi(q);
713             if (n < 0)
714                 continue;
715             if (getland(n, &land) && land.lnd_own &&
716                 land.lnd_ship < 0 && land.lnd_land < 0 &&
717                 land.lnd_x == target->sct_x && land.lnd_y == target->sct_y)
718                 unitno = n;
719             else
720                 pr("Unit #%d not spotted\n", n);
721         }
722         if (unitno < 0)
723             continue;
724         if (changed_plane_aborts(plp))
725             continue;
726
727         aaf = lnd_aaf(&land);
728         if (aaf) {
729             flak = roundavg(techfact(land.lnd_tech,
730                                      aaf * 3.0 * land.lnd_effic / 100.0));
731             PR(land.lnd_own,
732                "Flak! Firing flak guns from unit %s (aa rating %d)\n",
733                prland(&land), aaf);
734             if (pinflak_planedamage(&plp->plane, plp->pcp, land.lnd_own, flak))
735                 continue;
736         }
737
738         dam = 0;
739         if (nuk_on_plane(&plp->plane) >= 0)
740             hitchance = 100;
741         else {
742             hitchance = pln_hitchance(&plp->plane,
743                                       lnd_hardtarget(&land), EF_LAND);
744             pr("%d%% hitchance...", hitchance);
745         }
746         if (roll(100) <= hitchance) {
747             dam = 2 * pln_damage(&plp->plane, 'p', 1);
748         } else {
749             pr("thud\n");
750             /* Bombs that miss have to land somewhere! */
751             dam = pln_damage(&plp->plane, 'p', 0);
752             collateral_damage(target->sct_x, target->sct_y, dam);
753             dam = 0;
754         }
755         if (dam <= 0)
756             continue;
757         if (dam > 100)
758             dam = 100;
759         own = land.lnd_own;
760         mpr(own, "%s pinpoint bombing raid did %d damage to %s\n",
761             cname(player->cnum), dam, prland(&land));
762         check_retreat_and_do_landdamage(&land, dam);
763
764         if (land.lnd_rflags & RET_BOMBED)
765             if (((land.lnd_rflags & RET_INJURED) == 0) || !dam)
766                 retreat_land(&land, 'b');
767         nreport(player->cnum, N_UNIT_BOMB, own, 1);
768         putland(land.lnd_uid, &land);
769         collateral_damage(target->sct_x, target->sct_y, dam);
770     }
771 }
772
773 static void
774 strat_bomb(struct emp_qelem *list, struct sctstr *target)
775 {
776     struct plist *plp;
777     int dam = 0;
778     struct emp_qelem *qp;
779     struct sctstr sect;
780     struct nukstr nuke;
781
782     for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
783         plp = (struct plist *)qp;
784         if (getnuke(nuk_on_plane(&plp->plane), &nuke))
785             detonate(&nuke, target->sct_x, target->sct_y,
786                      plp->plane.pln_flags & PLN_AIRBURST);
787         else
788             dam += pln_damage(&plp->plane, 's', 1);
789     }
790     if (dam <= 0)               /* dam == 0 if only nukes were delivered */
791         return;
792     getsect(target->sct_x, target->sct_y, &sect);
793     target = &sect;
794     if (target->sct_own)
795         wu(0, target->sct_own, "%s bombing raid did %d damage in %s\n",
796            cname(player->cnum), PERCENT_DAMAGE(dam),
797            xyas(target->sct_x, target->sct_y, target->sct_own));
798
799     sectdamage(target, dam);
800
801     pr("did %d damage in %s\n", PERCENT_DAMAGE(dam),
802        xyas(target->sct_x, target->sct_y, player->cnum));
803     putsect(&sect);
804 }
805
806 static int
807 changed_plane_aborts(struct plist *plp)
808 {
809     if (check_plane_ok(&plp->plane))
810         return 0;
811     getplane(plp->plane.pln_uid, &plp->plane);
812     pln_put1(plp);
813     return 1;
814 }
815
816 static int
817 pinflak_planedamage(struct plnstr *pp, struct plchrstr *pcp, natid from,
818                     int flak)
819 {
820     int disp;
821     char dmess[255];
822     int eff;
823     natid plane_owner;
824     int dam;
825
826     dam = ac_flak_dam(flak, pln_def(pp), pcp->pl_flags);
827     disp = 0;
828     plane_owner = pp->pln_own;
829     eff = pp->pln_effic;
830     if (dam <= 0)
831         return 0;
832     memset(dmess, 0, sizeof(dmess));
833     eff -= dam;
834     if (eff < 0)
835         eff = 0;
836     if (eff < PLANE_MINEFF) {
837         sprintf(dmess, " -- shot down");
838         disp = 1;
839     } else if (chance((100 - eff) / 100.0)) {
840         sprintf(dmess, " -- aborted with %d%% damage", 100 - eff);
841         disp = 2;
842     }
843     PR(plane_owner, "    Flak! %s %s takes %d%s.\n",
844        cname(pp->pln_own), prplane(pp), dam, dmess);
845
846     pp->pln_effic = eff;
847     if (disp == 1) {
848         if (from != 0)
849             nreport(from, N_DOWN_PLANE, pp->pln_own, 1);
850     }
851     putplane(pp->pln_uid, pp);
852
853     if (disp > 0)
854         return 1;
855     return 0;
856 }