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