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