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