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