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