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