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