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