]> git.pond.sub.org Git - empserver/blob - src/lib/subs/aircombat.c
(plist): Member state is used write-only, remove.
[empserver] / src / lib / subs / aircombat.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  *  aircombat.c: Deal with air to air combat
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1986
32  *     Thomas Ruschak, 1992
33  *     Steve McClure, 1996
34  */
35
36 #include <string.h>
37
38 #include "misc.h"
39 #include "player.h"
40 #include "news.h"
41 #include "land.h"
42 #include "sect.h"
43 #include "plane.h"
44 #include "ship.h"
45 #include "nat.h"
46 #include "file.h"
47 #include "xy.h"
48 #include "nsc.h"
49 #include "path.h"
50 #include "prototypes.h"
51 #include "optlist.h"
52 #include "server.h"
53
54
55 #ifndef MAX
56 #define MAX(x,y) ((x) < (y) ? (y) : (x))
57 #endif
58
59 static void getilist(struct emp_qelem *list, natid own,
60                      struct emp_qelem *a, struct emp_qelem *b,
61                      struct emp_qelem *c, struct emp_qelem *d);
62 static void ac_dog(struct plist *ap, struct plist *dp);
63
64 void
65 ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
66              coord x, coord y, s_char *path, int mission_flags,
67              int no_air_defense, struct emp_qelem *obomb,
68              struct emp_qelem *oesc)
69 {
70     int val, non_missiles;
71     int rel;
72     int dir;
73     int nats[MAXNOC];
74     int lnats[MAXNOC];
75     int gotilist[MAXNOC];
76     int unfriendly[MAXNOC];
77     int overfly[MAXNOC];
78     struct emp_qelem ilist[MAXNOC], *qp;
79     s_char mypath[1024];
80     int myp;
81     int civ, mil;
82     natid plane_owner;
83     struct sctstr sect;
84     struct shpstr ship;
85     struct lndstr land;
86     struct nstr_item ni;
87     natid cn;
88     struct natstr *over, *mynatp;
89     struct plist *plp;
90     int evaded;
91     struct shiplist *head = NULL;
92     int changed = 0;
93     int intown = 0;             /* Last owner to intercept */
94 /* We want to only intercept once per sector per owner.  So, if we overfly
95    a sector, and then overfly some land units or ships, we don't want to
96    potentially intercept 3 times. */
97
98     plp = (struct plist *)bomb_list->q_forw;
99     plane_owner = plp->plane.pln_own;
100
101     strncpy(mypath, path, sizeof(mypath));
102     myp = 0;
103
104     memset(overfly, 0, sizeof(overfly));
105     memset(gotilist, 0, sizeof(gotilist));
106     memset(unfriendly, 0, sizeof(unfriendly));
107     for (cn = 1; cn < MAXNOC; cn++) {
108         if ((mynatp = getnatp(cn)) == 0)
109             continue;
110         rel = getrel(mynatp, plane_owner);
111         if (rel > HOSTILE)
112             continue;
113         if (plane_owner == cn)
114             continue;
115         unfriendly[cn]++;
116     }
117     if (mission_flags & PM_R) {
118         if (mission_flags & P_S) {
119             PR(plane_owner, "\nSPY Plane report\n");
120             PRdate(plane_owner);
121             sathead();
122         } else if (mission_flags & P_A) {
123             PR(plane_owner, "\nAnti-Sub Patrol report\n");
124         } else {
125             PR(plane_owner, "\nReconnaissance report\n");
126             PRdate(plane_owner);
127         }
128     }
129
130     pln_removedupes(bomb_list, esc_list);
131     while ((dir = mypath[myp++]) && !QEMPTY(bomb_list)) {
132         if ((val = diridx(dir)) == DIR_STOP)
133             break;
134         /* XXX using xnorm is probably bad */
135         x = xnorm(x + diroff[val][0]);
136         y = ynorm(y + diroff[val][1]);
137         getsect(x, y, &sect);
138         over = getnatp(sect.sct_own);
139         if (opt_HIDDEN) {
140             setcont(plane_owner, sect.sct_own, FOUND_FLY);
141         }
142
143         if (mission_flags & PM_R) {
144             if (sect.sct_type == SCT_WATER) {
145                 PR(plane_owner, "flying over %s at %s\n",
146                    dchr[sect.sct_type].d_name, xyas(x, y, plane_owner));
147                 if (mission_flags & PM_S)
148                     plane_sweep(bomb_list, x, y);
149                 if (mission_flags & P_A) {
150                     plane_sona(bomb_list, x, y, &head);
151                 }
152                 changed += map_set(plane_owner,
153                                    sect.sct_x, sect.sct_y,
154                                    dchr[sect.sct_type].d_mnem, 0);
155             } else if (mission_flags & P_S) {
156                 satdisp(&sect, (mission_flags & P_I) ? 10 : 50, 1);
157             } else {
158                 /* This is borrowed from lookout */
159                 if (sect.sct_own == plane_owner)
160                     PR(plane_owner, "Your ");
161                 else
162                     PR(plane_owner,
163                        "%s (#%d) ", cname(sect.sct_own), sect.sct_own);
164                 PR(plane_owner, dchr[sect.sct_type].d_name);
165                 changed += map_set(plane_owner,
166                                    sect.sct_x, sect.sct_y,
167                                    dchr[sect.sct_type].d_mnem, 0);
168                 PR(plane_owner, " %d%% efficient ",
169                    (sect.sct_own == plane_owner) ?
170                    sect.sct_effic : roundintby((int)sect.sct_effic, 25));
171                 civ = sect.sct_item[I_CIVIL];
172                 mil = sect.sct_item[I_MILIT];
173                 if (civ)
174                     PR(plane_owner, "with %s%d civ ",
175                        (sect.sct_own == plane_owner) ?
176                        "" : "approx ",
177                        (sect.sct_own == plane_owner) ?
178                        civ : roundintby(civ, 25));
179                 if (mil)
180                     PR(plane_owner, "with %s%d mil ",
181                        (sect.sct_own == plane_owner) ?
182                        "" : "approx ",
183                        (sect.sct_own == plane_owner) ?
184                        mil : roundintby(mil, 25));
185                 PR(plane_owner, "@ %s\n", xyas(x, y, plane_owner));
186             }
187         } else {
188             PR(plane_owner, "flying over %s at %s\n",
189                dchr[sect.sct_type].d_name, xyas(x, y, plane_owner));
190             changed += map_set(plane_owner, sect.sct_x,
191                                sect.sct_y, dchr[sect.sct_type].d_mnem, 0);
192         }
193         if ((rel = getrel(over, plane_owner)) == ALLIED)
194             continue;
195
196         evaded = do_evade(bomb_list, esc_list);
197
198         if (sect.sct_own != 0 && sect.sct_own != plane_owner && (!evaded)) {
199             /* We only show planes overhead if they didn't
200              * evade radar */
201             overfly[sect.sct_own]++;
202             PR(sect.sct_own, "%s planes spotted over %s\n",
203                cname(plane_owner), xyas(x, y, sect.sct_own));
204         }
205
206         if (!evaded) {
207             /* Fire flak */
208             if (unfriendly[sect.sct_own])
209                 ac_doflak(bomb_list, &sect);
210             /* If bombers left, fire flak from units and ships */
211             if (!QEMPTY(bomb_list))
212                 ac_landflak(bomb_list, x, y);
213             if (!QEMPTY(bomb_list))
214                 ac_shipflak(bomb_list, x, y);
215         }
216         /* mission planes aborted due to flak -- don't send escorts */
217         if (QEMPTY(bomb_list))
218             break;
219         if ((!no_air_defense) && (!evaded))
220             air_defense(x, y, plane_owner, bomb_list, esc_list);
221
222         if (sect.sct_own == 0 || sect.sct_own == plane_owner)
223             continue;
224
225         if (evaded)
226             continue;
227
228         non_missiles = 0;
229         for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw) {
230             struct plist *ip = (struct plist *)qp;
231             if (!(plchr[(int)ip->plane.pln_type].pl_flags & P_M))
232                 non_missiles = 1;
233         }
234
235         if (!non_missiles)
236             continue;
237
238         if (unfriendly[sect.sct_own] && !gotilist[sect.sct_own]) {
239             getilist(&ilist[sect.sct_own], sect.sct_own,
240                      bomb_list, esc_list, obomb, oesc);
241             gotilist[sect.sct_own]++;
242         }
243         if (rel > HOSTILE)
244             continue;
245         ac_intercept(bomb_list, esc_list, &ilist[sect.sct_own],
246                      sect.sct_own, x, y);
247         intown = sect.sct_own;
248     }
249
250     /* Let's report all of the overflights even if aborted */
251     for (cn = 1; cn < MAXNOC; cn++) {
252         if (plane_owner == cn)
253             continue;
254         if (overfly[cn] > 0)
255             nreport(plane_owner, N_OVFLY_SECT, cn, overfly[cn]);
256     }
257     /* If the map changed, update it */
258     if (changed)
259         if (!update_pending && plane_owner == player->cnum)
260             writemap(player->cnum);
261     /* Now, if the bomber and escort lists are empty, we are done */
262     if (QEMPTY(bomb_list) && QEMPTY(esc_list)) {
263         if (mission_flags & P_A)
264             free_shiplist(&head);
265         return;
266     }
267
268     /* Something made it through */
269     /* Go figure out if there are ships in this sector, and who's they are */
270     memset(nats, 0, sizeof(nats));
271     snxtitem_xy(&ni, EF_SHIP, x, y);
272     while (nxtitem(&ni, &ship)) {
273         if (mchr[(int)ship.shp_type].m_flags & M_SUB)
274             continue;
275         nats[ship.shp_own]++;
276     }
277     /* Go figure out if there are units in this sector, and who's they are */
278     memset(lnats, 0, sizeof(lnats));
279     snxtitem_xy(&ni, EF_LAND, x, y);
280     while (nxtitem(&ni, &land)) {
281         if (land.lnd_ship >= 0 || land.lnd_land >= 0)
282             continue;
283         lnats[land.lnd_own]++;
284     }
285
286     /* Now, let's make life a little rougher. */
287     for (cn = 1; cn < MAXNOC && !QEMPTY(bomb_list); cn++) {
288         if (plane_owner == cn)
289             continue;
290         intown = -1;
291         /* Are there ships owned by this country? */
292         if (nats[cn] != 0) {
293             /* Yes. */
294             if (cn != 0)
295                 PR(cn, "%s planes spotted over ships in %s\n",
296                    cname(plane_owner), xyas(x, y, cn));
297             if (unfriendly[cn]) {
298                 /* They are unfriendly too */
299                 if (!gotilist[cn]) {
300                     getilist(&ilist[cn], cn, bomb_list, esc_list, obomb,
301                              oesc);
302                     gotilist[cn]++;
303                 }
304                 PR(plane_owner, "Flying over %s ships in %s\n", cname(cn),
305                    xyas(x, y, plane_owner));
306                 /* This makes going for ships in harbors tough */
307                 if (!evaded) {
308                     /* We already fired flak up above.  Now we intercept again if we haven't already */
309                     /* Flag that we intercepted */
310                     intown = 1;
311                     /* And now intercept again */
312                     ac_intercept(bomb_list, esc_list, &ilist[cn], cn, x,
313                                  y);
314                 }
315             }
316         }
317         /* Are there units owned by this country? */
318         if (lnats[cn] != 0) {
319             /* Yes. */
320             if (cn != 0)
321                 PR(cn, "%s planes spotted over land units in %s\n",
322                    cname(plane_owner), xyas(x, y, cn));
323             if (unfriendly[cn]) {
324                 /* They are unfriendly too */
325                 if (!gotilist[cn]) {
326                     getilist(&ilist[cn], cn, bomb_list, esc_list, obomb,
327                              oesc);
328                     gotilist[cn]++;
329                 }
330                 PR(plane_owner, "Flying over %s land units in %s\n",
331                    cname(cn), xyas(x, y, plane_owner));
332                 if (!evaded) {
333                     if (intown == -1) {
334                         /* We haven't intercepted yet, so intercept */
335                         ac_intercept(bomb_list, esc_list, &ilist[cn], cn,
336                                      x, y);
337                     }
338                 }
339             }
340         }
341     }
342     if (mission_flags & P_A)
343         free_shiplist(&head);
344 }
345
346 static int
347 count_non_missiles(struct emp_qelem *list)
348 {
349     struct emp_qelem *qp;
350     struct plist *plp;
351     int att_count = 0;
352
353     /* don't intercept missiles */
354     for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
355         plp = (struct plist *)qp;
356         if (!(plp->pcp->pl_flags & P_M))
357             att_count++;
358     }
359     return att_count;
360 }
361
362 void
363 sam_intercept(struct emp_qelem *att_list, struct emp_qelem *def_list,
364               natid def_own, natid plane_owner, coord x, coord y,
365               int delete_missiles)
366 {
367     struct emp_qelem *aqp;
368     struct emp_qelem *anext;
369     struct emp_qelem *dqp;
370     struct emp_qelem *dnext;
371     struct plist *aplp;
372     struct plist *dplp;
373     int first = 1;
374
375     for (aqp = att_list->q_forw,
376          dqp = def_list->q_forw;
377          aqp != att_list && dqp != def_list; aqp = anext) {
378         anext = aqp->q_forw;
379         aplp = (struct plist *)aqp;
380         if (aplp->pcp->pl_flags & P_M)
381             continue;
382         if (aplp->pcp->pl_cost < 1000)
383             continue;
384         for (; dqp != def_list; dqp = dnext) {
385             dnext = dqp->q_forw;
386             dplp = (struct plist *)dqp;
387             if (!(dplp->pcp->pl_flags & P_M))
388                 continue;
389
390             if (dplp->plane.pln_range <
391                 mapdist(x, y, dplp->plane.pln_x, dplp->plane.pln_y)) {
392                 emp_remque(dqp);
393                 free(dqp);
394                 continue;
395             }
396             if (mission_pln_equip(dplp, 0, P_F, 0) < 0) {
397                 emp_remque(dqp);
398                 free(dqp);
399                 continue;
400             }
401             if (first) {
402                 first = 0;
403                 PR(plane_owner, "%s launches SAMs!\n", cname(def_own));
404                 PR(def_own, "Launching SAMs at %s planes!\n",
405                    cname(plane_owner));
406                 ac_combat_headers(plane_owner, def_own);
407             }
408             ac_dog(aplp, dplp);
409             dqp = dnext;
410             break;
411         }
412     }
413     if (!first) {
414         PR(plane_owner, "\n");
415         PR(def_own, "\n");
416     }
417     if (delete_missiles) {
418         for (; dqp != def_list; dqp = dnext) {
419             dnext = dqp->q_forw;
420             dplp = (struct plist *)dqp;
421             if (!(dplp->pcp->pl_flags & P_M))
422                 continue;
423             emp_remque(dqp);
424             free(dqp);
425             continue;
426         }
427     }
428 }
429
430 void
431 ac_intercept(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
432              struct emp_qelem *def_list, natid def_own, coord x, coord y)
433 {
434     struct plnstr *pp;
435     struct plist *plp;
436     int icount;
437     struct emp_qelem *next;
438     struct emp_qelem *qp;
439     struct emp_qelem int_list;
440     int att_count;
441     natid plane_owner;
442     int dist;
443
444     plp = (struct plist *)bomb_list->q_forw;
445     plane_owner = plp->plane.pln_own;
446
447     icount = 0;
448
449     sam_intercept(bomb_list, def_list, def_own, plane_owner, x, y, 0);
450     sam_intercept(esc_list, def_list, def_own, plane_owner, x, y, 1);
451     if (!(att_count = count_non_missiles(bomb_list) +
452           count_non_missiles(esc_list)))
453         return;
454
455     emp_initque(&int_list);
456     for (qp = def_list->q_forw; qp != def_list; qp = next) {
457         next = qp->q_forw;
458         plp = (struct plist *)qp;
459         pp = &plp->plane;
460         /* SAMs interdict separately */
461         if (plp->pcp->pl_flags & P_M)
462             continue;
463         dist = mapdist(x, y, pp->pln_x, pp->pln_y) * 2;
464         if (pp->pln_range < dist)
465             continue;
466         if (mission_pln_equip(plp, 0, P_F, 0) < 0) {
467             emp_remque(qp);
468             free(qp);
469             continue;
470         }
471         /* got one; delete from def_list, add to int_list */
472         emp_remque(qp);
473         emp_insque(qp, &int_list);
474         pp->pln_mobil -= pln_mobcost(dist, pp, P_F);
475         putplane(pp->pln_uid, pp);
476         icount++;
477         if (icount > att_count)
478             break;
479     }
480     if (icount == 0)
481         return;
482     PR(plane_owner, "%d %s fighter%s rising to intercept!\n", icount,
483        cname(def_own), icount == 1 ? " is" : "s are");
484     PR(def_own, "%d fighter%s intercepting %s planes!\n", icount,
485        icount == 1 ? " is" : "s are", cname(plane_owner));
486     ac_combat_headers(plane_owner, def_own);
487     ac_airtoair(esc_list, &int_list);
488     ac_airtoair(bomb_list, &int_list);
489     PR(plane_owner, "\n");
490     PR(def_own, "\n");
491 }
492
493 void
494 ac_combat_headers(natid plane_owner, natid def_own)
495 {
496     PR(plane_owner,
497        " %-10.10s %-10.10s  strength int odds  damage           results\n",
498        cname(plane_owner), cname(def_own));
499     PR(def_own,
500        " %-10.10s %-10.10s  strength int odds  damage           results\n",
501        cname(def_own), cname(plane_owner));
502 }
503
504 /*
505  * air-to-air combat.
506  */
507 void
508 ac_airtoair(struct emp_qelem *att_list, struct emp_qelem *int_list)
509 {
510     struct plist *attacker;
511     struct plist *interceptor;
512     struct emp_qelem *att;
513     struct emp_qelem *in;
514     int nplanes;
515     int more_att;
516     int more_int;
517     struct emp_qelem *att_next;
518     struct emp_qelem *in_next;
519
520     att = att_list->q_forw;
521     in = int_list->q_forw;
522     more_att = 1;
523     more_int = 1;
524     if (QEMPTY(att_list) || QEMPTY(int_list)) {
525         more_att = 0;
526         more_int = 0;
527     }
528     while (more_att || more_int) {
529         in_next = in->q_forw;
530         att_next = att->q_forw;
531         attacker = (struct plist *)att;
532
533         /* skip missiles. If only missiles left, we're done */
534         if (plchr[(int)attacker->plane.pln_type].pl_flags & P_M) {
535             att = att_next;
536             if (att == att_list) {
537                 more_att = 0;
538                 if (QEMPTY(att_list))
539                     more_int = 0;
540                 else
541                     att = att->q_forw;
542             }
543             if (all_missiles(att_list))
544                 more_att = 0;
545             continue;
546         }
547         interceptor = (struct plist *)in;
548         nplanes = attacker->plane.pln_effic;
549         if (nplanes > interceptor->plane.pln_effic)
550             nplanes = interceptor->plane.pln_effic;
551         ac_dog(attacker, interceptor);
552         in = in_next;
553         att = att_next;
554         if (att == att_list) {
555             more_att = 0;
556             if (QEMPTY(att_list))
557                 more_int = 0;
558             else
559                 att = att->q_forw;
560         }
561         if (in == int_list) {
562             more_int = 0;
563             if (QEMPTY(int_list))
564                 more_att = 0;
565             else
566                 in = in->q_forw;
567         }
568     }
569 }
570
571 int
572 all_missiles(struct emp_qelem *att_list)
573 {
574     struct emp_qelem *qp;
575     struct plist *p;
576
577     qp = att_list->q_forw;
578     while (qp != att_list) {
579         p = (struct plist *)qp;
580         if (!(plchr[(int)p->plane.pln_type].pl_flags & P_M))
581             return 0;
582
583         qp = qp->q_forw;
584     }
585     return 1;
586 }
587
588 static void
589 ac_dog(struct plist *ap, struct plist *dp)
590 {
591     int att, def;
592     double odds;
593     int intensity;
594     natid att_own, def_own;
595     int adam, ddam;
596     s_char mesg[1024];
597     s_char temp[14];
598
599     att_own = ap->plane.pln_own;
600     def_own = dp->plane.pln_own;
601
602     PR(att_own, " %3.3s #%-4d  %3.3s #%-4d",
603        ap->pcp->pl_name,
604        ap->plane.pln_uid, dp->pcp->pl_name, dp->plane.pln_uid);
605     if (def_own)
606         PR(def_own, " %3.3s #%-4d  %3.3s #%-4d",
607            dp->pcp->pl_name,
608            dp->plane.pln_uid, ap->pcp->pl_name, ap->plane.pln_uid);
609     if (ap->plane.pln_att == 0) {
610         att = ap->plane.pln_def * ap->plane.pln_effic / 100;
611         att = MAX(att, ap->pcp->pl_def / 2);
612     } else {
613         att = ap->plane.pln_att * ap->plane.pln_effic / 100;
614         att = MAX(att, ap->pcp->pl_att / 2);
615     }
616
617     def = dp->plane.pln_def * dp->plane.pln_effic / 100;
618     def = MAX(def, dp->pcp->pl_def / 2);
619
620     if ((ap->pcp->pl_flags & P_F) && ap->bombs != 0)
621         att -= 2;
622     if ((dp->pcp->pl_flags & P_F) && dp->bombs != 0)
623         def -= 2;
624     att += ((float)ap->pcp->pl_stealth / 25.0);
625     def += ((float)dp->pcp->pl_stealth / 25.0);
626     if (att < 1) {
627         def += 1 - att;
628         att = 1;
629     }
630     if (def < 1) {
631         att += 1 - def;
632         def = 1;
633     }
634     odds = ((double)att / ((double)def + (double)att));
635     if (odds <= 0.05)
636         odds = 0.05;
637     intensity = roll(20) + roll(20) + roll(20) + roll(20) + 1;
638
639     PR(att_own, "   %3d/%-3d %3d  %3.2f  ", att, def, intensity, odds);
640     PR(def_own, "   %3d/%-3d %3d  %3.2f  ", def, att, intensity, odds);
641
642     adam = 0;
643     ddam = 0;
644     while ((intensity--) > 0) {
645
646         if (chance(odds)) {
647             ddam += 1;
648             if ((dp->plane.pln_effic - ddam) < PLANE_MINEFF)
649                 intensity = 0;
650         } else {
651             adam += 1;
652             if ((ap->plane.pln_effic - adam) < PLANE_MINEFF)
653                 intensity = 0;
654         }
655     }
656
657     if (dp->pcp->pl_flags & P_M)
658         ddam = 100;
659
660     PR(att_own, "%3d/%-3d", adam, ddam);
661     PR(def_own, "%3d/%-3d", ddam, adam);
662     ac_planedamage(ap, dp->plane.pln_own, adam, def_own, 1, 0, mesg);
663     strncpy(temp, mesg, 14);
664     ac_planedamage(dp, ap->plane.pln_own, ddam, att_own, 1, 0, mesg);
665     PR(att_own, "%-13.13s %-13.13s\n", temp, mesg);
666     PR(def_own, "%-13.13s %-13.13s\n", mesg, temp);
667 }
668
669 /*
670  * zap plane associated with plp.
671  * Damaging country is "from", damage is "dam".
672  * def_own is the country on the other side of the conflict from the plane
673  * owner. The only time def_own != from is when the interceptor is getting
674  * damaged.
675  *
676  * NOTE: This routine removes the appropriate plane element from the
677  * queue if it gets destroyed.  That means that the caller must assume
678  * that the current queue pointer is invalid on return from the ac_planedamage
679  * call.  (this has caused bugs in the past)
680  */
681 void
682 ac_planedamage(struct plist *plp, natid from, int dam, natid other,
683                int checkabort, int show, s_char *mesg)
684 {
685     struct plnstr *pp;
686     int disp;
687     s_char dmess[255];
688     int eff;
689     struct shpstr ship;
690     struct lndstr land;
691     /* s_char *sprintf();           already in misc.h [JFW] */
692     natid plane_owner;
693
694     disp = 0;
695     pp = &plp->plane;
696     plane_owner = pp->pln_own;
697     eff = pp->pln_effic;
698     sprintf(dmess, " no damage");
699     if (dam <= 0) {
700         strcpy(mesg, dmess);
701         return;
702     }
703     memset(dmess, 0, sizeof(dmess));
704     eff -= dam;
705     if (eff < 0)
706         eff = 0;
707     if (eff < PLANE_MINEFF) {
708         sprintf(dmess, " shot down");
709         disp = 1;
710     } else if (eff < 80 && chance((100 - eff) / 100.0) && checkabort) {
711         sprintf(dmess, " aborted @%2d%%", eff);
712         disp = 2;
713     } else if (show == 0) {
714         sprintf(dmess, " cleared");
715     }
716
717     if ((plp->pcp->pl_flags & P_M) == 0) {
718         if (show) {
719             PR(plane_owner, "    %s %s takes %d%s.\n",
720                cname(pp->pln_own), prplane(pp), dam, dmess);
721             if (other)
722                 PR(other, "    %s %s takes %d%s.\n",
723                    cname(pp->pln_own), prplane(pp), dam, dmess);
724         }
725     }
726     if (show && checkabort == 1) {
727         PR(plane_owner, "\n");
728         if (other)
729             PR(other, "\n");
730     }
731
732     pp->pln_effic = eff;
733     pp->pln_mobil -= min(32 + pp->pln_mobil, dam / 2);
734     if (disp == 1) {
735         if (from != 0 && (plp->pcp->pl_flags & P_M) == 0)
736             nreport(from, N_DOWN_PLANE, pp->pln_own, 1);
737         if (pp->pln_ship >= 0) {
738             getship(pp->pln_ship, &ship);
739             take_plane_off_ship(pp, &ship);
740         }
741         if (pp->pln_land >= 0) {
742             getland(pp->pln_land, &land);
743             take_plane_off_land(pp, &land);
744         }
745         makelost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x, pp->pln_y);
746         pp->pln_own = 0;
747         putplane(pp->pln_uid, pp);
748         emp_remque(&plp->queue);
749         free(plp);
750     } else if (disp == 2) {
751         putplane(pp->pln_uid, pp);
752         emp_remque(&plp->queue);
753         free(plp);
754     } else
755         putplane(pp->pln_uid, pp);
756     strcpy(mesg, dmess);
757 }
758
759 void
760 ac_doflak(struct emp_qelem *list, struct sctstr *from)
761 {
762     int shell;
763     int gun;
764     natid plane_owner;
765     struct plist *plp;
766
767     plp = (struct plist *)list->q_forw;
768     plane_owner = plp->plane.pln_own;
769
770     gun = from->sct_item[I_GUN];
771     shell = from->sct_item[I_SHELL];
772     if (gun > 14)
773         gun = 14;
774     if (gun > shell * 2) {
775         shell += supply_commod(from->sct_own, from->sct_x, from->sct_y,
776                                I_SHELL, (gun + 1) / 2 - shell);
777         from->sct_item[I_SHELL] = shell;
778         putsect(from);
779     }
780     if (gun > shell * 2)
781         gun = shell * 2;
782
783     gun = 2.0 * tfact(from->sct_own, gun);
784     if (gun > 0) {
785         PR(plane_owner, "firing %d flak guns in %s...\n",
786            gun, xyas(from->sct_x, from->sct_y, plane_owner));
787         if (from->sct_own != 0)
788             PR(from->sct_own, "firing %d flak guns in %s...\n",
789                gun, xyas(from->sct_x, from->sct_y, from->sct_own));
790         ac_fireflak(list, from->sct_own, 0, gun);
791     }
792 }
793
794 void
795 ac_shipflak(struct emp_qelem *list, coord x, coord y)
796 {
797     struct nstr_item ni;
798     struct shpstr ship;
799     struct mchrstr *mcp;
800     int firing;
801     int guns;
802     int gun;
803     int shell;
804     int rel;
805     struct plist *plp;
806     natid plane_owner;
807     natid from;
808     int nats[MAXNOC];
809
810     plp = (struct plist *)list->q_forw;
811     plane_owner = plp->plane.pln_own;
812
813     memset(nats, 0, sizeof(nats));
814     guns = 0;
815     snxtitem_xy(&ni, EF_SHIP, x, y);
816     while (!QEMPTY(list) && nxtitem(&ni, &ship)) {
817         if (ship.shp_own == 0 || ship.shp_own == plane_owner)
818             continue;
819         if (guns >= 14)
820             break;
821         mcp = &mchr[(int)ship.shp_type];
822         if (mcp->m_flags & M_SUB)
823             continue;
824         rel = getrel(getnatp(ship.shp_own), plane_owner);
825         if (rel > HOSTILE)
826             continue;
827         shell = gun = 0;
828         gun = min(ship.shp_item[I_GUN], ship.shp_glim);
829         if (gun) {
830             shell = ship.shp_item[I_SHELL];
831             if (shell <= 0) {
832                 shell = supply_commod(ship.shp_own, ship.shp_x,
833                                       ship.shp_y, I_SHELL, 1);
834                 ship.shp_item[I_SHELL] = shell;
835                 putship(ship.shp_uid, &ship);
836             }
837         }
838         if (gun == 0 || shell == 0)
839             continue;
840         firing = (int)(techfact(ship.shp_tech, (double)gun) * 2.0);
841         guns += firing;
842
843         if (!nats[ship.shp_own]) {
844             /* First time here, print the message */
845             PR(ship.shp_own, "%s planes spotted over ships in %s\n",
846                cname(plane_owner), xyas(x, y, ship.shp_own));
847             PR(plane_owner, "Flying over %s ships in %s\n",
848                cname(ship.shp_own), xyas(x, y, plane_owner));
849             nats[ship.shp_own] = 1;
850         }
851         PR(ship.shp_own, "firing %d flak guns from %s...\n",
852            firing, prship(&ship));
853         from = ship.shp_own;
854     }
855     if (guns > 0) {
856         if (guns > 14)
857             guns = 14;
858         guns = 2.0 * tfact(from, (double)guns);
859         PR(plane_owner, "Flak!  Ships firing %d flak guns...\n", guns);
860         ac_fireflak(list, from, 0, guns);
861     }
862 }
863
864 void
865 ac_landflak(struct emp_qelem *list, coord x, coord y)
866 {
867     struct nstr_item ni;
868     struct lndstr land;
869     struct lchrstr *lcp;
870     int firing;
871     int guns;
872     int rel;
873     natid from;
874     struct plist *plp;
875     natid plane_owner;
876     int nats[MAXNOC];
877
878     plp = (struct plist *)list->q_forw;
879     plane_owner = plp->plane.pln_own;
880
881     memset(nats, 0, sizeof(nats));
882     guns = 0;
883     snxtitem_xy(&ni, EF_LAND, x, y);
884     while (!QEMPTY(list) && nxtitem(&ni, &land)) {
885         if (land.lnd_own == 0 || land.lnd_own == plane_owner)
886             continue;
887         if (guns >= 14)
888             break;
889         lcp = &lchr[(int)land.lnd_type];
890
891         if ((lcp->l_flags & L_FLAK) == 0)
892             continue;
893
894         if (land.lnd_aaf == 0)
895             continue;
896
897         rel = getrel(getnatp(land.lnd_own), plane_owner);
898         if (rel > HOSTILE)
899             continue;
900         firing =
901             (int)(techfact(land.lnd_tech, (double)land.lnd_aaf) * 3.0);
902         guns += firing;
903
904         if (!nats[land.lnd_own]) {
905             /* First time here, print the message */
906             PR(land.lnd_own, "%s planes spotted over land units in %s\n",
907                cname(plane_owner), xyas(x, y, land.lnd_own));
908             PR(plane_owner, "Flying over %s land units in %s\n",
909                cname(land.lnd_own), xyas(x, y, plane_owner));
910             nats[land.lnd_own] = 1;
911         }
912         PR(land.lnd_own, "firing flak guns from unit %s (aa rating %d)\n",
913            prland(&land), land.lnd_aaf);
914
915         from = land.lnd_own;    /* We always use the last owner as the from */
916     }
917     if (guns > 0) {
918         if (guns > 14)
919             guns = 14;
920         guns = 2.0 * tfact(from, (double)guns);
921         PR(plane_owner, "Flak!  Land units firing %d flak guns...\n",
922            guns);
923         ac_fireflak(list, from, 0, guns);
924     }
925 }
926
927 /*
928  * Called from shipflak, landflak, and doflak.
929  */
930 void
931 ac_fireflak(struct emp_qelem *list, natid from, natid other, int guns)
932 {
933     struct plnstr *pp;
934     struct plist *plp;
935     int n;
936     int diff;
937     struct emp_qelem *qp;
938     struct emp_qelem *next;
939     s_char msg[255];
940
941     plp = (struct plist *)list->q_forw;
942
943     for (qp = list->q_forw; qp != list; qp = next) {
944         /*
945          * fighters don't get shot at by flak
946          * non-tactical bombers are harder to hit with flak.
947          * ('Cause they're not dive-bombing?)
948          */
949         next = qp->q_forw;
950         plp = (struct plist *)qp;
951         pp = &plp->plane;
952         diff = guns - pp->pln_def;
953         if ((plp->pcp->pl_flags & P_T) == 0)
954             diff--;
955         if (plp->pcp->pl_flags & P_X)
956             diff -= 2;
957         if (plp->pcp->pl_flags & P_H)
958             diff -= 1;
959         n = ac_flak_dam(diff);
960         ac_planedamage(plp, from, n, other, 2, 1, msg);
961     }
962 }
963
964 /*
965  * Calculate flak damage
966  */
967 int
968 ac_flak_dam(int flak)
969 {
970     int dam;
971     float mult;
972     /*                             <-7      -7     -6     -5     -4 */
973     static float flaktable[18] = { 0.132f, 0.20f, 0.20f, 0.25f, 0.30f,
974     /*    -3     -2     -1      0     +1     +2     +3     +4 */
975          0.35f, 0.40f, 0.45f, 0.50f, 0.50f, 0.55f, 0.60f, 0.65f,
976     /*    +5    +6     +7     +8    >+8 */
977          0.70f,0.75f, 0.80f, 0.85f, 1.1305f };
978     enum { FLAK_MAX = sizeof(flaktable)/sizeof(flaktable[0]) };
979
980     if (flak > 8)
981         mult = flaktable[FLAK_MAX];
982     else if (flak < -7)
983         mult = flaktable[0];
984     else {
985         flak += 8;
986         mult = flaktable[flak];
987     }
988     mult *= flakscale;
989     dam = (int)((roll(8) + 2) * mult);
990     if (dam > 100)
991         dam = 100;
992     return(dam);
993 }
994
995 /*
996  * See if this plane is flying in this list
997  */
998 int
999 ac_isflying(struct plnstr *plane, struct emp_qelem *list)
1000 {
1001     struct emp_qelem *qp;
1002     struct emp_qelem *next;
1003     struct plnstr *pp;
1004     struct plist *plp;
1005
1006     if (!list)
1007         return 0;
1008     for (qp = list->q_forw; qp != list; qp = next) {
1009         next = qp->q_forw;
1010         plp = (struct plist *)qp;
1011         pp = &plp->plane;
1012         if (plane->pln_uid == pp->pln_uid)
1013             return 1;
1014     }
1015     return 0;
1016 }
1017
1018
1019 /*
1020  * Get a list of planes available for interception duties.
1021  */
1022 static void
1023 getilist(struct emp_qelem *list, natid own, struct emp_qelem *a,
1024          struct emp_qelem *b, struct emp_qelem *c, struct emp_qelem *d)
1025 {
1026     struct plchrstr *pcp;
1027     struct plnstr plane;
1028     struct shpstr ship;
1029     struct lndstr land;
1030     struct sctstr sect;
1031     struct nstr_item ni;
1032     int petrol;
1033     struct plist *ip;
1034
1035     emp_initque(list);
1036     snxtitem_all(&ni, EF_PLANE);
1037     while (nxtitem(&ni, &plane)) {
1038         if (plane.pln_own != own)
1039             continue;
1040         pcp = &plchr[(int)plane.pln_type];
1041         if ((pcp->pl_flags & P_F) == 0)
1042             continue;
1043         if (plane.pln_mission != 0)
1044             continue;
1045         if (plane.pln_mobil <= 0)
1046             continue;
1047         if (plane.pln_effic < 40)
1048             continue;
1049         if (plane.pln_ship >= 0) {
1050             if (!can_fly(plane.pln_uid))
1051                 continue;
1052             getship(plane.pln_ship, &ship);
1053             petrol = ship.shp_item[I_PETROL];
1054         } else if (plane.pln_land >= 0) {
1055             if (!can_fly(plane.pln_uid))
1056                 continue;
1057             getland(plane.pln_land, &land);
1058             petrol = land.lnd_item[I_PETROL];
1059         } else {
1060             getsect(plane.pln_x, plane.pln_y, &sect);
1061             petrol = sect.sct_item[I_PETROL];
1062             if ((sect.sct_effic < 60 || sect.sct_type != SCT_AIRPT)
1063                 && (pcp->pl_flags & P_V) == 0)
1064                 continue;
1065         }
1066         if ((float)petrol < (float)pcp->pl_fuel / 2.0)
1067             continue;
1068         /* Finally, is it in the list of planes already in
1069            flight? */
1070         if (ac_isflying(&plane, a))
1071             continue;
1072         if (ac_isflying(&plane, b))
1073             continue;
1074         if (ac_isflying(&plane, c))
1075             continue;
1076         if (ac_isflying(&plane, d))
1077             continue;
1078         /* got one! */
1079         ip = malloc(sizeof(*ip));
1080         ip->bombs = 0;
1081         ip->misc = 0;
1082         ip->pcp = &plchr[(int)plane.pln_type];
1083         ip->plane = plane;
1084         emp_insque(&ip->queue, list);
1085     }
1086 }
1087
1088
1089
1090 int
1091 can_fly(int p)
1092 {                               /* Can this plane fly from the ship or land unit it is on? */
1093     struct plnstr plane;
1094     struct shpstr ship;
1095     struct lndstr land;
1096     struct plchrstr *pcp;
1097     struct mchrstr *scp;
1098     struct lchrstr *lcp;
1099
1100     getplane(p, &plane);
1101     pcp = &plchr[(int)plane.pln_type];
1102
1103     if (plane.pln_ship >= 0) {
1104         if (!(pcp->pl_flags & P_L) && !(pcp->pl_flags & P_M)
1105             && !(pcp->pl_flags & P_K)
1106             && !(pcp->pl_flags & P_E)
1107             )
1108             return 0;
1109
1110         getship(plane.pln_ship, &ship);
1111         scp = &mchr[(int)ship.shp_type];
1112
1113         if ((pcp->pl_flags & P_L) && (scp->m_flags & M_FLY)) {
1114             return 1;
1115         }
1116
1117         if ((pcp->pl_flags & P_M) && (scp->m_flags & M_MSL)) {
1118             return 1;
1119         }
1120
1121         if ((pcp->pl_flags & P_K) && (scp->m_flags & M_CHOPPER)) {
1122             return 1;
1123         }
1124
1125         if ((pcp->pl_flags & P_E) && (scp->m_flags & M_XLIGHT)) {
1126             return 1;
1127         }
1128     }
1129
1130     if (plane.pln_land >= 0) {
1131         if (!(pcp->pl_flags & P_E))
1132             return 0;
1133
1134         getland(plane.pln_land, &land);
1135         lcp = &lchr[(int)land.lnd_type];
1136
1137         if ((pcp->pl_flags & P_E) && (lcp->l_flags & L_XLIGHT)) {
1138             return 1;
1139         }
1140     }
1141
1142     return 0;
1143 }
1144
1145 int
1146 do_evade(struct emp_qelem *bomb_list, struct emp_qelem *esc_list)
1147 {
1148     struct emp_qelem *qp;
1149     double evade;
1150     struct plist *plp;
1151
1152     evade = 100.0;
1153     for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw) {
1154         plp = (struct plist *)qp;
1155         if (evade > ((float)plp->pcp->pl_stealth / 100.0))
1156             evade = (plp->pcp->pl_stealth / 100.0);
1157     }
1158     for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw) {
1159         plp = (struct plist *)qp;
1160         if (evade > plp->pcp->pl_stealth / 100.0)
1161             evade = (plp->pcp->pl_stealth / 100.0);
1162     }
1163
1164     if (chance(evade))
1165         return 1;
1166
1167     return 0;
1168 }