]> git.pond.sub.org Git - empserver/blob - src/lib/commands/bomb.c
Fix bomb not to wipe out plane updates while asking for targets
[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     int rel;
99     struct natstr *natp;
100     char buf[1024];
101
102     wantflags = 0;
103     if (get_planes(&ni_bomb, &ni_esc, player->argp[1], player->argp[2]) < 0)
104         return RET_SYN;
105     p = getstarg(player->argp[3], "pinpoint, or strategic? ", buf);
106     if (!p || !*p)
107         return RET_SYN;
108     mission = *p;
109     if (strchr("ps", mission) == 0)
110         return RET_SYN;
111     if (!get_assembly_point(player->argp[4], &ap_sect, buf))
112         return RET_SYN;
113     ax = ap_sect.sct_x;
114     ay = ap_sect.sct_y;
115     if (getpath(flightpath, player->argp[5], ax, ay, 0, 0, P_FLYING) == 0
116         || *flightpath == 0)
117         return RET_SYN;
118     tx = ax;
119     ty = ay;
120     (void)pathtoxy(flightpath, &tx, &ty, fcost);
121     pr("target sector is %s\n", xyas(tx, ty, player->cnum));
122     getsect(tx, ty, &target);
123     ip = 0;
124     ap_to_target = strlen(flightpath);
125     if (flightpath[ap_to_target - 1] == 'h')
126         ap_to_target--;
127     pr("range to target is %d\n", ap_to_target);
128     /*
129      * select planes within range
130      */
131     pln_sel(&ni_bomb, &bomb_list, &ap_sect, ap_to_target,
132             2, wantflags, P_M | P_O);
133     pln_sel(&ni_esc, &esc_list, &ap_sect, ap_to_target,
134             2, wantflags | P_F | P_ESC, P_M | P_O);
135     /*
136      * now arm and equip the bombers, transports, whatever.
137      */
138     pln_arm(&bomb_list, 2 * ap_to_target, mission, ip, 0);
139     if (QEMPTY(&bomb_list)) {
140         pr("No planes could be equipped for the mission.\n");
141         return RET_FAIL;
142     }
143     pln_arm(&esc_list, 2 * ap_to_target, mission, ip, P_F | P_ESC);
144     ac_encounter(&bomb_list, &esc_list, ax, ay, flightpath, 0);
145     if (QEMPTY(&bomb_list)) {
146         pr("No planes got through fighter defenses\n");
147     } else if (target.sct_type == SCT_SANCT) {
148         pr("You can't bomb that sector!\n");
149     } else {
150         switch (mission) {
151         case 'p':
152             pin_bomb(&bomb_list, &target);
153             break;
154         case 's':
155             if (opt_SLOW_WAR) {
156                 natp = getnatp(player->cnum);
157                 if (target.sct_own) {
158                     rel = getrel(natp, target.sct_own);
159                     if ((rel != AT_WAR) && (player->cnum != target.sct_own)
160                         && (target.sct_own) &&
161                         (target.sct_oldown != player->cnum)) {
162                         pr("You're not at war with them!\n");
163                         pln_put(&bomb_list);
164                         pln_put(&esc_list);
165                         return RET_FAIL;
166                     }
167                 }
168             }
169             nreport(player->cnum, N_SCT_BOMB, target.sct_own, 1);
170             strat_bomb(&bomb_list, &target);
171             break;
172         default:
173             CANT_REACH();
174         }
175     }
176     pln_put(&bomb_list);
177     pln_put(&esc_list);
178     return RET_OK;
179 }
180
181 static void
182 pin_bomb(struct emp_qelem *list, struct sctstr *target)
183 {
184     struct dchrstr *dcp;
185     int nplanes;
186     int nships;
187     int type;
188     int bad;
189     char *p;
190     int nsubs;
191     int nunits;
192     struct natstr *natp;
193     int rel;
194     char buf[1024];
195     int i;
196
197     bad = 0;
198     type = target->sct_type;
199     dcp = &dchr[type];
200     pr("Target sector is a %s constructed %s\n",
201        effadv((int)target->sct_effic), dcp->d_name);
202     nsubs = 0;
203     nships = shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
204     if (pln_caps(list) & P_A) {
205         nsubs = shipsatxy(target->sct_x, target->sct_y, M_SUB, 0, 1);
206         if (nsubs > 0)
207             pr("Some subs are present in the sector.\n");
208     }
209     nplanes = planesatxy(target->sct_x, target->sct_y, 0, 0);
210     nunits = unitsatxy(target->sct_x, target->sct_y, 0, 0);
211   retry:
212     p = getstring("Bomb what? (ship, plane, land unit, efficiency, commodities) ",
213                   buf);
214     if (!p)
215        return;
216     if (!*p) {
217         bad++;
218         if (bad > 2)
219             return;
220         goto retry;
221     }
222     switch (*p) {
223     case 'l':
224         if (opt_SLOW_WAR) {
225             natp = getnatp(player->cnum);
226             if (target->sct_own) {
227                 rel = getrel(natp, target->sct_own);
228                 if ((rel != AT_WAR) && (player->cnum != target->sct_own)
229                     && (target->sct_own) &&
230                     (target->sct_oldown != player->cnum)) {
231                     pr("You're not at war with them!\n");
232                     goto retry;
233                 }
234             }
235         }
236         if (nunits == 0) {
237             pr("no units there\n");
238             goto retry;
239         }
240         land_bomb(list, target);
241         break;
242     case 'p':
243         if (opt_SLOW_WAR) {
244             natp = getnatp(player->cnum);
245             if (target->sct_own) {
246                 rel = getrel(natp, target->sct_own);
247                 if ((rel != AT_WAR) && (player->cnum != target->sct_own)
248                     && (target->sct_own) &&
249                     (target->sct_oldown != player->cnum)) {
250                     pr("You're not at war with them!\n");
251                     goto retry;
252                 }
253             }
254         }
255         if (nplanes == 0) {
256             pr("no planes there\n");
257             goto retry;
258         }
259         plane_bomb(list, target);
260         break;
261     case 's':
262         if (nships == 0) {
263             if (pln_caps(list) & P_A) {
264                 if (nsubs == 0) {
265                     pr("no ships there\n");
266                     goto retry;
267                 }
268             } else {
269                 pr("no ships there\n");
270                 goto retry;
271             }
272         }
273         ship_bomb(list, target);
274         break;
275     case 'c':
276         if (opt_SLOW_WAR) {
277             natp = getnatp(player->cnum);
278             if (target->sct_own) {
279                 rel = getrel(natp, target->sct_own);
280                 if ((rel != AT_WAR) && (player->cnum != target->sct_own)
281                     && (target->sct_own) &&
282                     (target->sct_oldown != player->cnum)) {
283                     pr("You're not at war with them!\n");
284                     goto retry;
285                 }
286             }
287         }
288
289         for (i = 0; i < nbomb; i++) {
290             if (!target->sct_item[bombcomm[i]])
291                 continue;
292             break;
293         }
294         if (i >= nbomb) {
295             pr("No bombable commodities in %s\n",
296                xyas(target->sct_x, target->sct_y, player->cnum));
297             goto retry;
298         }
299         comm_bomb(list, target);
300         break;
301     case 'e':
302         if (opt_SLOW_WAR) {
303             natp = getnatp(player->cnum);
304             if (target->sct_own) {
305                 rel = getrel(natp, target->sct_own);
306                 if ((rel != AT_WAR) && (player->cnum != target->sct_own)
307                     && (target->sct_own) &&
308                     (target->sct_oldown != player->cnum)) {
309                     pr("You're not at war with them!\n");
310                     goto retry;
311                 }
312             }
313         }
314         eff_bomb(list, target);
315         break;
316     case 'q':
317         pr("Aborting mission.\n");
318         return;
319     default:
320         pr("Bad target type.\n");
321         goto retry;
322     }
323 }
324
325 static void
326 eff_bomb(struct emp_qelem *list, struct sctstr *target)
327 {
328     struct plist *plp;
329     struct emp_qelem *qp, *next;
330     struct sctstr sect;
331     int oldeff, dam = 0;
332     int nukedam;
333
334     for (qp = list->q_forw; qp != list; qp = next) {
335         next = qp->q_forw;
336         plp = (struct plist *)qp;
337         if (changed_plane_aborts(plp))
338             continue;
339         if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
340             continue;
341         if (plp->bombs || nuk_on_plane(&plp->plane) >= 0)
342             dam += pln_damage(&plp->plane, target->sct_x, target->sct_y,
343                               'p', &nukedam, 1);
344     }
345     if (dam <= 0)               /* dam == 0 if only nukes were delivered */
346         return;
347     getsect(target->sct_x, target->sct_y, &sect);
348     target = &sect;
349     oldeff = target->sct_effic;
350     target->sct_effic = effdamage(target->sct_effic, dam);
351     target->sct_avail = effdamage(target->sct_avail, dam);
352     target->sct_road = effdamage(target->sct_road, dam);
353     target->sct_rail = effdamage(target->sct_rail, dam);
354     target->sct_defense = effdamage(target->sct_defense, dam);
355     pr("did %d%% damage to efficiency in %s\n",
356        oldeff - target->sct_effic,
357        xyas(target->sct_x, target->sct_y, player->cnum));
358     if (target->sct_own)
359         wu(0, target->sct_own,
360            "%s bombing raid did %d%% damage in %s\n",
361            cname(player->cnum), oldeff - target->sct_effic,
362            xyas(target->sct_x, target->sct_y, target->sct_own));
363     bridge_damaged(target);
364     putsect(&sect);
365     collateral_damage(target->sct_x, target->sct_y, dam);
366 }
367
368 static void
369 comm_bomb(struct emp_qelem *list, struct sctstr *target)
370 {
371     struct plist *plp;
372     double b;
373     int i;
374     int amt, before;
375     struct ichrstr *ip;
376     struct emp_qelem *qp, *next;
377     struct sctstr sect;
378     int dam = 0;
379     int nukedam;
380
381     for (i = 0; i < nbomb; i++) {
382         if (target->sct_item[bombcomm[i]] == 0)
383             continue;
384         if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
385             continue;
386         ip = &ichr[bombcomm[i]];
387         pr("some %s\n", ip->i_name);
388     }
389     for (;;) {
390         ip = whatitem(NULL, "commodity to bomb? ");
391         if (player->aborted)
392             return;
393         if (!ip)
394             continue;
395
396         for (i = 0; i < nbomb; i++) {
397             if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
398                 continue;
399             if (&ichr[bombcomm[i]] == ip)
400                 break;
401         }
402         if (i == nbomb) {
403             pr("You can't bomb %s!\n", ip->i_name);
404             for (i = 0; i < nbomb; i++) {
405                 if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
406                     continue;
407                 pr("%s%s", i == 0 ? "Bombable: " : ", ",
408                    ichr[bombcomm[i]].i_name);
409             }
410             pr("\n");
411         } else
412             break;
413     }
414     for (qp = list->q_forw; qp != list; qp = next) {
415         next = qp->q_forw;
416         plp = (struct plist *)qp;
417         if (changed_plane_aborts(plp))
418             continue;
419         if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
420             continue;
421         if (plp->bombs || nuk_on_plane(&plp->plane) >= 0)
422             dam += pln_damage(&plp->plane, target->sct_x, target->sct_y,
423                               'p', &nukedam, 1);
424     }
425     if (dam <= 0)               /* dam == 0 if only nukes were delivered */
426         return;
427     getsect(target->sct_x, target->sct_y, &sect);
428     target = &sect;
429     before = target->sct_item[ip->i_uid];
430     target->sct_item[ip->i_uid] = amt = commdamage(before, dam, ip->i_uid);
431     if (before > 0.0)
432         b = 100.0 * (1.0 - (double)amt / (double)before);
433     else
434         b = 0.0;
435     pr("did %.2f%% damage to %s in %s\n",
436        b, ip->i_name, xyas(target->sct_x, target->sct_y, player->cnum));
437     nreport(player->cnum, N_SCT_BOMB, target->sct_own, 1);
438     if (target->sct_own != 0)
439         wu(0, target->sct_own,
440            "%s precision bombing raid did %.2f%% damage to %s in %s\n",
441            cname(player->cnum), b, ip->i_name,
442            xyas(target->sct_x, target->sct_y, target->sct_own));
443     putsect(&sect);
444     collateral_damage(target->sct_x, target->sct_y, dam);
445 }
446
447 static void
448 ship_bomb(struct emp_qelem *list, struct sctstr *target)
449 {
450     struct plist *plp;
451     struct mchrstr *mcp;
452     int dam;
453     char *q;
454     int n;
455     struct emp_qelem *qp, *next;
456     int shipno;
457     struct shpstr ship;
458     int nships = 0;
459     struct shiplist *head = NULL;
460     char buf[1024];
461     char prompt[128];
462     int hitchance;
463     int nukedam;
464     int flak;
465     int gun;
466
467     for (qp = list->q_forw; qp != list; qp = next) {
468         next = qp->q_forw;
469         free_shiplist(&head);
470         plp = (struct plist *)qp;
471         if (changed_plane_aborts(plp))
472             continue;
473         if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
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->bombs);
486         shipno = -1;
487         while (shipno < 0) {
488             if ((q = getstring(prompt, buf)) == 0)
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         if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
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->bombs);
609         planeno = -1;
610         while (planeno < 0) {
611             if ((q = getstring(prompt, buf)) == 0)
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         if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
707             continue;
708         nunits = unitsatxy(target->sct_x, target->sct_y, 0, 0);
709         if (nunits == 0) {
710             pr("%s could not find any units!\n", prplane(&plp->plane));
711             continue;
712         }
713         (void)sprintf(prompt, "%s, %d bombs.  Target ('~' to skip)? ",
714                       prplane(&plp->plane), plp->bombs);
715         unitno = -1;
716         while (unitno < 0) {
717             if ((q = getstring(prompt, buf)) == 0)
718                 return;
719             if (*q == 0)
720                 continue;
721             if (*q == '~')
722                 break;
723             if (*q == '?') {
724                 unitsatxy(target->sct_x, target->sct_y, 0, 0);
725                 continue;
726             }
727             n = atoi(q);
728             if (n < 0)
729                 continue;
730             if (getland(n, &land) && land.lnd_own &&
731                 land.lnd_ship < 0 && land.lnd_land < 0 &&
732                 land.lnd_x == target->sct_x && land.lnd_y == target->sct_y)
733                 unitno = n;
734             else
735                 pr("Unit #%d not spotted\n", n);
736         }
737         if (unitno < 0)
738             continue;
739         if (changed_plane_aborts(plp))
740             continue;
741
742         aaf = lnd_aaf(&land);
743         if (aaf) {
744             flak = roundavg(techfact(land.lnd_tech,
745                                      aaf * 3.0 * land.lnd_effic / 100.0));
746             PR(land.lnd_own,
747                "Flak! Firing flak guns from unit %s (aa rating %d)\n",
748                prland(&land), aaf);
749             if (pinflak_planedamage(&plp->plane, plp->pcp, land.lnd_own, flak))
750                 continue;
751         }
752
753         dam = 0;
754         if (nuk_on_plane(&plp->plane) >= 0)
755             hitchance = 100;
756         else {
757             hitchance = pln_hitchance(&plp->plane,
758                                       lnd_hardtarget(&land), EF_LAND);
759             pr("%d%% hitchance...", hitchance);
760         }
761         if (roll(100) <= hitchance) {
762             dam = 2 * pln_damage(&plp->plane, target->sct_x, target->sct_y,
763                                  'p', &nukedam, 1);
764         } else {
765             pr("thud\n");
766             /* Bombs that miss have to land somewhere! */
767             dam = pln_damage(&plp->plane, target->sct_x, target->sct_y,
768                              'p', &nukedam, 0);
769             collateral_damage(target->sct_x, target->sct_y, dam);
770             dam = 0;
771         }
772         if (dam <= 0)           /* dam == 0 if only nukes were delivered */
773             continue;
774         if (dam > 100)
775             dam = 100;
776         own = land.lnd_own;
777         mpr(own, "%s pinpoint bombing raid did %d damage to %s\n",
778             cname(player->cnum), dam, prland(&land));
779         check_retreat_and_do_landdamage(&land, dam);
780
781         if (land.lnd_rflags & RET_BOMBED)
782             if (((land.lnd_rflags & RET_INJURED) == 0) || !dam)
783                 retreat_land(&land, 'b');
784         nreport(player->cnum, N_UNIT_BOMB, own, 1);
785         putland(land.lnd_uid, &land);
786         collateral_damage(target->sct_x, target->sct_y, dam);
787     }
788 }
789
790 static void
791 strat_bomb(struct emp_qelem *list, struct sctstr *target)
792 {
793     struct plist *plp;
794     int dam = 0;
795     struct emp_qelem *qp;
796     struct sctstr sect;
797     int nukedam;
798
799     for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
800         plp = (struct plist *)qp;
801         if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
802             continue;
803         if (plp->bombs || nuk_on_plane(&plp->plane) >= 0)
804             dam += pln_damage(&plp->plane, target->sct_x, target->sct_y,
805                               's', &nukedam, 1);
806     }
807     if (dam <= 0)               /* dam == 0 if only nukes were delivered */
808         return;
809     getsect(target->sct_x, target->sct_y, &sect);
810     target = &sect;
811     if (target->sct_own)
812         wu(0, target->sct_own, "%s bombing raid did %d damage in %s\n",
813            cname(player->cnum), PERCENT_DAMAGE(dam),
814            xyas(target->sct_x, target->sct_y, target->sct_own));
815
816     sectdamage(target, dam);
817
818     pr("did %d damage in %s\n", PERCENT_DAMAGE(dam),
819        xyas(target->sct_x, target->sct_y, player->cnum));
820     putsect(&sect);
821 }
822
823 static int
824 changed_plane_aborts(struct plist *plp)
825 {
826     if (check_plane_ok(&plp->plane))
827         return 0;
828     getplane(plp->plane.pln_uid, &plp->plane);
829     pln_put1(plp);
830     return 1;
831 }
832
833 static int
834 pinflak_planedamage(struct plnstr *pp, struct plchrstr *pcp, natid from,
835                     int flak)
836 {
837     int disp;
838     char dmess[255];
839     int eff;
840     natid plane_owner;
841     int dam;
842
843     dam = ac_flak_dam(flak, pln_def(pp), pcp->pl_flags);
844     disp = 0;
845     plane_owner = pp->pln_own;
846     eff = pp->pln_effic;
847     if (dam <= 0)
848         return 0;
849     memset(dmess, 0, sizeof(dmess));
850     eff -= dam;
851     if (eff < 0)
852         eff = 0;
853     if (eff < PLANE_MINEFF) {
854         sprintf(dmess, " -- shot down");
855         disp = 1;
856     } else if (chance((100 - eff) / 100.0)) {
857         sprintf(dmess, " -- aborted with %d%% damage", 100 - eff);
858         disp = 2;
859     }
860     PR(plane_owner, "    Flak! %s %s takes %d%s.\n",
861        cname(pp->pln_own), prplane(pp), dam, dmess);
862
863     pp->pln_effic = eff;
864     if (disp == 1) {
865         if (from != 0)
866             nreport(from, N_DOWN_PLANE, pp->pln_own, 1);
867     }
868     putplane(pp->pln_uid, pp);
869
870     if (disp > 0)
871         return 1;
872     return 0;
873 }