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