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