]> git.pond.sub.org Git - empserver/blob - src/lib/commands/bomb.c
850f0f1371cb4b89a81e37cca02ae3a15709e596
[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     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     int nukedam;
341
342     for (qp = list->q_forw; qp != list; qp = next) {
343         next = qp->q_forw;
344         plp = (struct plist *)qp;
345         if (changed_plane_aborts(plp))
346             continue;
347         dam += pln_damage(&plp->plane, target->sct_x, target->sct_y,
348                           'p', &nukedam, 1);
349     }
350     if (dam <= 0)               /* dam == 0 if only nukes were delivered */
351         return;
352     getsect(target->sct_x, target->sct_y, &sect);
353     target = &sect;
354     oldeff = target->sct_effic;
355     target->sct_effic = effdamage(target->sct_effic, dam);
356     target->sct_avail = effdamage(target->sct_avail, dam);
357     target->sct_road = effdamage(target->sct_road, dam);
358     target->sct_rail = effdamage(target->sct_rail, dam);
359     target->sct_defense = effdamage(target->sct_defense, dam);
360     pr("did %d%% damage to efficiency in %s\n",
361        oldeff - target->sct_effic,
362        xyas(target->sct_x, target->sct_y, player->cnum));
363     if (target->sct_own)
364         wu(0, target->sct_own,
365            "%s bombing raid did %d%% damage in %s\n",
366            cname(player->cnum), oldeff - target->sct_effic,
367            xyas(target->sct_x, target->sct_y, target->sct_own));
368     bridge_damaged(target);
369     putsect(&sect);
370     collateral_damage(target->sct_x, target->sct_y, dam);
371 }
372
373 static void
374 comm_bomb(struct emp_qelem *list, struct sctstr *target)
375 {
376     struct plist *plp;
377     double b;
378     int i;
379     int amt, before;
380     struct ichrstr *ip;
381     struct emp_qelem *qp, *next;
382     struct sctstr sect;
383     int dam = 0;
384     int nukedam;
385
386     for (i = 0; i < nbomb; i++) {
387         if (target->sct_item[bombcomm[i]] == 0)
388             continue;
389         if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
390             continue;
391         ip = &ichr[bombcomm[i]];
392         pr("some %s\n", ip->i_name);
393     }
394     for (;;) {
395         ip = whatitem(NULL, "commodity to bomb? ");
396         if (player->aborted)
397             return;
398         if (!ip)
399             continue;
400
401         for (i = 0; i < nbomb; i++) {
402             if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
403                 continue;
404             if (&ichr[bombcomm[i]] == ip)
405                 break;
406         }
407         if (i == nbomb) {
408             pr("You can't bomb %s!\n", ip->i_name);
409             for (i = 0; i < nbomb; i++) {
410                 if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
411                     continue;
412                 pr("%s%s", i == 0 ? "Bombable: " : ", ",
413                    ichr[bombcomm[i]].i_name);
414             }
415             pr("\n");
416         } else
417             break;
418     }
419     for (qp = list->q_forw; qp != list; qp = next) {
420         next = qp->q_forw;
421         plp = (struct plist *)qp;
422         if (changed_plane_aborts(plp))
423             continue;
424         dam += pln_damage(&plp->plane, target->sct_x, target->sct_y,
425                           'p', &nukedam, 1);
426     }
427     if (dam <= 0)               /* dam == 0 if only nukes were delivered */
428         return;
429     getsect(target->sct_x, target->sct_y, &sect);
430     target = &sect;
431     before = target->sct_item[ip->i_uid];
432     target->sct_item[ip->i_uid] = amt = commdamage(before, dam, ip->i_uid);
433     if (before > 0.0)
434         b = 100.0 * (1.0 - (double)amt / (double)before);
435     else
436         b = 0.0;
437     pr("did %.2f%% damage to %s in %s\n",
438        b, ip->i_name, xyas(target->sct_x, target->sct_y, player->cnum));
439     nreport(player->cnum, N_SCT_BOMB, target->sct_own, 1);
440     if (target->sct_own != 0)
441         wu(0, target->sct_own,
442            "%s precision bombing raid did %.2f%% damage to %s in %s\n",
443            cname(player->cnum), b, ip->i_name,
444            xyas(target->sct_x, target->sct_y, target->sct_own));
445     putsect(&sect);
446     collateral_damage(target->sct_x, target->sct_y, dam);
447 }
448
449 static void
450 ship_bomb(struct emp_qelem *list, struct sctstr *target)
451 {
452     struct plist *plp;
453     struct mchrstr *mcp;
454     int dam;
455     char *q;
456     int n;
457     struct emp_qelem *qp, *next;
458     int shipno;
459     struct shpstr ship;
460     int nships = 0;
461     struct shiplist *head = NULL;
462     char buf[1024];
463     char prompt[128];
464     int hitchance;
465     int nukedam;
466     int flak;
467     int gun;
468
469     for (qp = list->q_forw; qp != list; qp = next) {
470         next = qp->q_forw;
471         free_shiplist(&head);
472         plp = (struct plist *)qp;
473         if (changed_plane_aborts(plp))
474             continue;
475         if (plp->pcp->pl_flags & P_A)
476             nships = asw_shipsatxy(target->sct_x, target->sct_y, 0, 0,
477                                    &plp->plane, &head);
478         else
479             nships = shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
480         if (nships == 0) {
481             pr("%s could not find any ships!\n", prplane(&plp->plane));
482             continue;
483         }
484         (void)sprintf(prompt, "%s, %d bombs.  Target ('~' to skip)? ",
485                       prplane(&plp->plane), plp->load);
486         shipno = -1;
487         while (shipno < 0) {
488             if (!(q = getstring(prompt, buf)))
489                 goto out;
490             if (*q == 0)
491                 continue;
492             if (*q == '~')
493                 break;
494             if (*q == '?') {
495                 if (plp->pcp->pl_flags & P_A)
496                     print_shiplist(head);
497                 else
498                     shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
499                 continue;
500             }
501             n = atoi(q);
502             if (n < 0)
503                 continue;
504             if ((!(plp->pcp->pl_flags & P_A) || on_shiplist(n, head)) &&
505                 getship(n, &ship) && ship.shp_own &&
506                 ship.shp_x == target->sct_x && ship.shp_y == target->sct_y)
507                 shipno = n;
508             else
509                 pr("Ship #%d not spotted\n", n);
510         }
511         if (shipno < 0)
512             continue;
513         if ((plp->pcp->pl_flags & P_A) && !on_shiplist(shipno, head))
514             continue;
515         if (changed_plane_aborts(plp))
516             continue;
517
518         gun = shp_usable_guns(&ship);
519         mcp = &mchr[(int)ship.shp_type];
520         if (gun > 0 && !(mcp->m_flags & M_SUB)) {
521             flak = (int)(techfact(ship.shp_tech, gun) * 2.0);
522             PR(ship.shp_own, "Flak! Firing %d guns from ship %s\n",
523                flak, prship(&ship));
524             if (pinflak_planedamage(&plp->plane, plp->pcp, ship.shp_own, flak))
525                 continue;
526         }
527
528         dam = 0;
529         if (nuk_on_plane(&plp->plane) >= 0)
530             hitchance = 100;
531         else {
532             hitchance = pln_hitchance(&plp->plane,
533                                       shp_hardtarget(&ship), EF_SHIP);
534             pr("%d%% hitchance...", hitchance);
535         }
536         if (roll(100) <= hitchance) {
537             /* pinbombing is more accurate than normal bombing */
538             dam = 2 * pln_damage(&plp->plane, target->sct_x, target->sct_y,
539                                  'p', &nukedam, 1);
540         } else {
541             pr("splash\n");
542             /* Bombs that miss have to land somewhere! */
543             dam = pln_damage(&plp->plane, target->sct_x, target->sct_y,
544                              'p', &nukedam, 0);
545             collateral_damage(target->sct_x, target->sct_y, dam);
546             dam = 0;
547         }
548         if (dam <= 0)           /* dam == 0 if only nukes were delivered */
549             continue;
550         if (mcp->m_flags & M_SUB)
551             nreport(player->cnum, N_SUB_BOMB, ship.shp_own, 1);
552         else
553             nreport(player->cnum, N_SHP_BOMB, ship.shp_own, 1);
554         if (ship.shp_own) {
555             wu(0, ship.shp_own, "%s bombs did %d damage to %s at %s\n",
556                cname(player->cnum), dam,
557                prship(&ship),
558                xyas(target->sct_x, target->sct_y, ship.shp_own));
559         }
560         pr("\n");
561         check_retreat_and_do_shipdamage(&ship, dam);
562         if (ship.shp_rflags & RET_BOMBED)
563             if (((ship.shp_rflags & RET_INJURED) == 0) || !dam)
564                 retreat_ship(&ship, 'b');
565         putship(ship.shp_uid, &ship);
566         getship(ship.shp_uid, &ship);
567         if (!ship.shp_own) {
568             pr("%s at %s sunk!\n",
569                prship(&ship),
570                xyas(target->sct_x, target->sct_y, player->cnum));
571         }
572         collateral_damage(target->sct_x, target->sct_y, dam / 2);
573     }
574 out:
575     free_shiplist(&head);
576 }
577
578 static void
579 plane_bomb(struct emp_qelem *list, struct sctstr *target)
580 {
581     int dam;
582     char *q;
583     int n;
584     natid own;
585     struct plnstr plane;
586     struct emp_qelem *qp, *next;
587     int planeno;
588     struct plist *plp;
589     char prompt[128];
590     char buf[1024];
591     int hitchance;
592     int nukedam;
593     int nplanes;
594
595     for (qp = list->q_forw; qp != list; qp = next) {
596         next = qp->q_forw;
597         plp = (struct plist *)qp;
598         if (changed_plane_aborts(plp))
599             continue;
600         nplanes = planesatxy(target->sct_x, target->sct_y, 0, 0);
601         if (nplanes == 0) {
602             pr("%s could not find any planes!\n", prplane(&plp->plane));
603             continue;
604         }
605         (void)sprintf(prompt, "%s, %d bombs.  Target ('~' to skip)? ",
606                       prplane(&plp->plane), plp->load);
607         planeno = -1;
608         while (planeno < 0) {
609             if (!(q = getstring(prompt, buf)))
610                 return;
611             if (*q == 0)
612                 continue;
613             if (*q == '~')
614                 break;
615             if (*q == '?') {
616                 planesatxy(target->sct_x, target->sct_y, 0, 0);
617                 continue;
618             }
619             n = atoi(q);
620             if (n < 0)
621                 continue;
622             if (getplane(n, &plane) &&
623                 plane.pln_x == target->sct_x &&
624                 plane.pln_y == target->sct_y &&
625                 plane.pln_ship < 0 && plane.pln_land < 0 &&
626                 !(plane.pln_flags & PLN_LAUNCHED))
627                 planeno = n;
628             else
629                 pr("Plane #%d not spotted\n", n);
630         }
631         if (planeno < 0)
632             continue;
633         if (changed_plane_aborts(plp))
634             continue;
635         dam = 0;
636         if (nuk_on_plane(&plp->plane) >= 0)
637             hitchance = 100;
638         else {
639             hitchance = pln_hitchance(&plp->plane, 0, EF_PLANE);
640             pr("%d%% hitchance...", hitchance);
641         }
642         if (roll(100) <= hitchance) {
643             /* pinbombing is more accurate than normal bombing */
644             dam = 2 * pln_damage(&plp->plane, target->sct_x, target->sct_y,
645                                  'p', &nukedam, 1);
646         } else {
647             pr("thud\n");
648             /* Bombs that miss have to land somewhere! */
649             dam = pln_damage(&plp->plane, target->sct_x, target->sct_y,
650                              'p', &nukedam, 0);
651             collateral_damage(target->sct_x, target->sct_y, dam);
652             dam = 0;
653         }
654         if (dam <= 0)           /* dam == 0 if only nukes were delivered */
655             continue;
656         if (dam > 100)
657             dam = 100;
658         own = plane.pln_own;
659         if (dam > plane.pln_effic)
660             plane.pln_effic = 0;
661         else
662             plane.pln_effic -= dam;
663         plane.pln_mobil = (dam * plane.pln_mobil / 100.0);
664         if (own == player->cnum) {
665             pr("%s reports %d%% damage\n", prplane(&plane), dam);
666         } else {
667             if (own != 0)
668                 wu(0, own,
669                    "%s pinpoint bombing raid did %d%% damage to %s\n",
670                    cname(player->cnum), dam, prplane(&plane));
671         }
672         nreport(player->cnum, N_DOWN_PLANE, own, 1);
673         if (own != 0)
674             wu(0, own, "%s bombs did %d%% damage to %s at %s\n",
675                cname(player->cnum), dam, prplane(&plane),
676                xyas(target->sct_x, target->sct_y, own));
677         putplane(plane.pln_uid, &plane);
678         collateral_damage(target->sct_x, target->sct_y, dam);
679     }
680 }
681
682 static void
683 land_bomb(struct emp_qelem *list, struct sctstr *target)
684 {
685     int dam;
686     char *q;
687     int n;
688     natid own;
689     char prompt[128];
690     char buf[1024];
691     struct lndstr land;
692     struct emp_qelem *qp, *next;
693     int unitno;
694     int aaf, flak, hitchance;
695     struct plist *plp;
696     int nukedam;
697     int nunits;
698
699     for (qp = list->q_forw; qp != list; qp = next) {
700         next = qp->q_forw;
701         plp = (struct plist *)qp;
702         if (changed_plane_aborts(plp))
703             continue;
704         nunits = unitsatxy(target->sct_x, target->sct_y, 0, 0);
705         if (nunits == 0) {
706             pr("%s could not find any units!\n", prplane(&plp->plane));
707             continue;
708         }
709         (void)sprintf(prompt, "%s, %d bombs.  Target ('~' to skip)? ",
710                       prplane(&plp->plane), plp->load);
711         unitno = -1;
712         while (unitno < 0) {
713             if (!(q = getstring(prompt, buf)))
714                 return;
715             if (*q == 0)
716                 continue;
717             if (*q == '~')
718                 break;
719             if (*q == '?') {
720                 unitsatxy(target->sct_x, target->sct_y, 0, 0);
721                 continue;
722             }
723             n = atoi(q);
724             if (n < 0)
725                 continue;
726             if (getland(n, &land) && land.lnd_own &&
727                 land.lnd_ship < 0 && land.lnd_land < 0 &&
728                 land.lnd_x == target->sct_x && land.lnd_y == target->sct_y)
729                 unitno = n;
730             else
731                 pr("Unit #%d not spotted\n", n);
732         }
733         if (unitno < 0)
734             continue;
735         if (changed_plane_aborts(plp))
736             continue;
737
738         aaf = lnd_aaf(&land);
739         if (aaf) {
740             flak = roundavg(techfact(land.lnd_tech,
741                                      aaf * 3.0 * land.lnd_effic / 100.0));
742             PR(land.lnd_own,
743                "Flak! Firing flak guns from unit %s (aa rating %d)\n",
744                prland(&land), aaf);
745             if (pinflak_planedamage(&plp->plane, plp->pcp, land.lnd_own, flak))
746                 continue;
747         }
748
749         dam = 0;
750         if (nuk_on_plane(&plp->plane) >= 0)
751             hitchance = 100;
752         else {
753             hitchance = pln_hitchance(&plp->plane,
754                                       lnd_hardtarget(&land), EF_LAND);
755             pr("%d%% hitchance...", hitchance);
756         }
757         if (roll(100) <= hitchance) {
758             dam = 2 * pln_damage(&plp->plane, target->sct_x, target->sct_y,
759                                  'p', &nukedam, 1);
760         } else {
761             pr("thud\n");
762             /* Bombs that miss have to land somewhere! */
763             dam = pln_damage(&plp->plane, target->sct_x, target->sct_y,
764                              'p', &nukedam, 0);
765             collateral_damage(target->sct_x, target->sct_y, dam);
766             dam = 0;
767         }
768         if (dam <= 0)           /* dam == 0 if only nukes were delivered */
769             continue;
770         if (dam > 100)
771             dam = 100;
772         own = land.lnd_own;
773         mpr(own, "%s pinpoint bombing raid did %d damage to %s\n",
774             cname(player->cnum), dam, prland(&land));
775         check_retreat_and_do_landdamage(&land, dam);
776
777         if (land.lnd_rflags & RET_BOMBED)
778             if (((land.lnd_rflags & RET_INJURED) == 0) || !dam)
779                 retreat_land(&land, 'b');
780         nreport(player->cnum, N_UNIT_BOMB, own, 1);
781         putland(land.lnd_uid, &land);
782         collateral_damage(target->sct_x, target->sct_y, dam);
783     }
784 }
785
786 static void
787 strat_bomb(struct emp_qelem *list, struct sctstr *target)
788 {
789     struct plist *plp;
790     int dam = 0;
791     struct emp_qelem *qp;
792     struct sctstr sect;
793     int nukedam;
794
795     for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
796         plp = (struct plist *)qp;
797         dam += pln_damage(&plp->plane, target->sct_x, target->sct_y,
798                           's', &nukedam, 1);
799     }
800     if (dam <= 0)               /* dam == 0 if only nukes were delivered */
801         return;
802     getsect(target->sct_x, target->sct_y, &sect);
803     target = &sect;
804     if (target->sct_own)
805         wu(0, target->sct_own, "%s bombing raid did %d damage in %s\n",
806            cname(player->cnum), PERCENT_DAMAGE(dam),
807            xyas(target->sct_x, target->sct_y, target->sct_own));
808
809     sectdamage(target, dam);
810
811     pr("did %d damage in %s\n", PERCENT_DAMAGE(dam),
812        xyas(target->sct_x, target->sct_y, player->cnum));
813     putsect(&sect);
814 }
815
816 static int
817 changed_plane_aborts(struct plist *plp)
818 {
819     if (check_plane_ok(&plp->plane))
820         return 0;
821     getplane(plp->plane.pln_uid, &plp->plane);
822     pln_put1(plp);
823     return 1;
824 }
825
826 static int
827 pinflak_planedamage(struct plnstr *pp, struct plchrstr *pcp, natid from,
828                     int flak)
829 {
830     int disp;
831     char dmess[255];
832     int eff;
833     natid plane_owner;
834     int dam;
835
836     dam = ac_flak_dam(flak, pln_def(pp), pcp->pl_flags);
837     disp = 0;
838     plane_owner = pp->pln_own;
839     eff = pp->pln_effic;
840     if (dam <= 0)
841         return 0;
842     memset(dmess, 0, sizeof(dmess));
843     eff -= dam;
844     if (eff < 0)
845         eff = 0;
846     if (eff < PLANE_MINEFF) {
847         sprintf(dmess, " -- shot down");
848         disp = 1;
849     } else if (chance((100 - eff) / 100.0)) {
850         sprintf(dmess, " -- aborted with %d%% damage", 100 - eff);
851         disp = 2;
852     }
853     PR(plane_owner, "    Flak! %s %s takes %d%s.\n",
854        cname(pp->pln_own), prplane(pp), dam, dmess);
855
856     pp->pln_effic = eff;
857     if (disp == 1) {
858         if (from != 0)
859             nreport(from, N_DOWN_PLANE, pp->pln_own, 1);
860     }
861     putplane(pp->pln_uid, pp);
862
863     if (disp > 0)
864         return 1;
865     return 0;
866 }