2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
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.
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.
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
22 * See files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * aircombat.c: Deal with air to air combat
30 * Known contributors to this file:
32 * Thomas Ruschak, 1992
52 #include "prototypes.h"
56 static void getilist(struct emp_qelem *list, natid own,
57 struct emp_qelem *a, struct emp_qelem *b,
58 struct emp_qelem *c, struct emp_qelem *d);
59 static void ac_dog(struct plist *ap, struct plist *dp);
62 ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
63 coord x, coord y, char *path, int mission_flags,
64 int no_air_defense, struct emp_qelem *obomb,
65 struct emp_qelem *oesc)
67 int val, non_missiles;
73 int unfriendly[MAXNOC];
75 struct emp_qelem ilist[MAXNOC], *qp;
85 struct natstr *over, *mynatp;
88 struct shiplist *head = NULL;
90 int intown = 0; /* Last owner to intercept */
91 /* We want to only intercept once per sector per owner. So, if we overfly
92 a sector, and then overfly some land units or ships, we don't want to
93 potentially intercept 3 times. */
95 plp = (struct plist *)bomb_list->q_forw;
96 plane_owner = plp->plane.pln_own;
98 strncpy(mypath, path, sizeof(mypath));
101 memset(overfly, 0, sizeof(overfly));
102 memset(gotilist, 0, sizeof(gotilist));
103 memset(unfriendly, 0, sizeof(unfriendly));
104 for (cn = 1; cn < MAXNOC; cn++) {
105 if ((mynatp = getnatp(cn)) == 0)
107 rel = getrel(mynatp, plane_owner);
110 if (plane_owner == cn)
114 if (mission_flags & PM_R) {
115 if (mission_flags & P_S) {
116 PR(plane_owner, "\nSPY Plane report\n");
119 } else if (mission_flags & P_A) {
120 PR(plane_owner, "\nAnti-Sub Patrol report\n");
122 PR(plane_owner, "\nReconnaissance report\n");
127 pln_removedupes(bomb_list, esc_list);
128 while ((dir = mypath[myp++]) && !QEMPTY(bomb_list)) {
129 if ((val = diridx(dir)) == DIR_STOP)
131 /* XXX using xnorm is probably bad */
132 x = xnorm(x + diroff[val][0]);
133 y = ynorm(y + diroff[val][1]);
134 getsect(x, y, §);
135 over = getnatp(sect.sct_own);
137 if (mission_flags & PM_R) {
139 setcont(plane_owner, sect.sct_own, FOUND_FLY);
140 if (sect.sct_type == SCT_WATER) {
141 PR(plane_owner, "flying over %s at %s\n",
142 dchr[sect.sct_type].d_name, xyas(x, y, plane_owner));
143 if (mission_flags & PM_S)
144 plane_sweep(bomb_list, x, y);
145 if (mission_flags & P_A) {
146 plane_sona(bomb_list, x, y, &head);
148 changed += map_set(plane_owner,
149 sect.sct_x, sect.sct_y,
150 dchr[sect.sct_type].d_mnem, 0);
151 } else if (mission_flags & P_S) {
152 satdisp(§, (mission_flags & P_I) ? 10 : 50, 1);
154 /* This is borrowed from lookout */
155 if (sect.sct_own == plane_owner)
156 PR(plane_owner, "Your ");
158 PR(plane_owner, "%s (#%d) ",
159 cname(sect.sct_own), sect.sct_own);
160 PR(plane_owner, "%s", dchr[sect.sct_type].d_name);
161 changed += map_set(plane_owner,
162 sect.sct_x, sect.sct_y,
163 dchr[sect.sct_type].d_mnem, 0);
164 PR(plane_owner, " %d%% efficient ",
165 (sect.sct_own == plane_owner) ?
166 sect.sct_effic : roundintby((int)sect.sct_effic, 25));
167 civ = sect.sct_item[I_CIVIL];
168 mil = sect.sct_item[I_MILIT];
170 PR(plane_owner, "with %s%d civ ",
171 (sect.sct_own == plane_owner) ?
173 (sect.sct_own == plane_owner) ?
174 civ : roundintby(civ, 25));
176 PR(plane_owner, "with %s%d mil ",
177 (sect.sct_own == plane_owner) ?
179 (sect.sct_own == plane_owner) ?
180 mil : roundintby(mil, 25));
181 PR(plane_owner, "@ %s\n", xyas(x, y, plane_owner));
184 PR(plane_owner, "flying over %s at %s\n",
185 dchr[sect.sct_type].d_name, xyas(x, y, plane_owner));
186 changed += map_set(plane_owner, sect.sct_x,
187 sect.sct_y, dchr[sect.sct_type].d_mnem, 0);
189 if ((rel = getrel(over, plane_owner)) == ALLIED)
192 evaded = do_evade(bomb_list, esc_list);
194 if (sect.sct_own != 0 && sect.sct_own != plane_owner && !evaded) {
195 /* We only show planes overhead if they didn't
197 overfly[sect.sct_own]++;
198 PR(sect.sct_own, "%s planes spotted over %s\n",
199 cname(plane_owner), xyas(x, y, sect.sct_own));
201 setcont(cn, plane_owner, FOUND_FLY);
206 if (unfriendly[sect.sct_own])
207 ac_doflak(bomb_list, §);
208 /* If bombers left, fire flak from units and ships */
209 if (!QEMPTY(bomb_list))
210 ac_landflak(bomb_list, x, y);
211 if (!QEMPTY(bomb_list))
212 ac_shipflak(bomb_list, x, y);
214 /* mission planes aborted due to flak -- don't send escorts */
215 if (QEMPTY(bomb_list))
217 if (!no_air_defense && !evaded)
218 air_defense(x, y, plane_owner, bomb_list, esc_list);
220 if (sect.sct_own == 0 || sect.sct_own == plane_owner)
227 for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw) {
228 struct plist *ip = (struct plist *)qp;
229 if (!(plchr[(int)ip->plane.pln_type].pl_flags & P_M))
236 if (unfriendly[sect.sct_own] && !gotilist[sect.sct_own]) {
237 getilist(&ilist[sect.sct_own], sect.sct_own,
238 bomb_list, esc_list, obomb, oesc);
239 gotilist[sect.sct_own]++;
243 ac_intercept(bomb_list, esc_list, &ilist[sect.sct_own],
245 intown = sect.sct_own;
248 /* Let's report all of the overflights even if aborted */
249 for (cn = 1; cn < MAXNOC; cn++) {
250 if (plane_owner == cn)
253 nreport(plane_owner, N_OVFLY_SECT, cn, overfly[cn]);
255 /* If the map changed, update it */
257 if (!update_pending && plane_owner == player->cnum)
258 writemap(player->cnum);
259 /* Now, if the bomber and escort lists are empty, we are done */
260 if (QEMPTY(bomb_list) && QEMPTY(esc_list)) {
261 if (mission_flags & P_A)
262 free_shiplist(&head);
266 /* Something made it through */
267 /* Go figure out if there are ships in this sector, and who's they are */
268 memset(nats, 0, sizeof(nats));
269 snxtitem_xy(&ni, EF_SHIP, x, y);
270 while (nxtitem(&ni, &ship)) {
271 if (mchr[(int)ship.shp_type].m_flags & M_SUB)
273 nats[ship.shp_own]++;
275 /* Go figure out if there are units in this sector, and who's they are */
276 memset(lnats, 0, sizeof(lnats));
277 snxtitem_xy(&ni, EF_LAND, x, y);
278 while (nxtitem(&ni, &land)) {
279 if (land.lnd_ship >= 0 || land.lnd_land >= 0)
281 lnats[land.lnd_own]++;
284 /* Now, let's make life a little rougher. */
285 for (cn = 1; cn < MAXNOC && !QEMPTY(bomb_list); cn++) {
286 if (plane_owner == cn)
289 /* Are there ships owned by this country? */
293 PR(cn, "%s planes spotted over ships in %s\n",
294 cname(plane_owner), xyas(x, y, cn));
296 setcont(cn, plane_owner, FOUND_FLY);
298 if (unfriendly[cn]) {
299 /* They are unfriendly too */
301 getilist(&ilist[cn], cn, bomb_list, esc_list, obomb,
305 PR(plane_owner, "Flying over %s ships in %s\n", cname(cn),
306 xyas(x, y, plane_owner));
307 /* This makes going for ships in harbors tough */
309 /* We already fired flak up above. Now we intercept again if we haven't already */
310 /* Flag that we intercepted */
312 /* And now intercept again */
313 ac_intercept(bomb_list, esc_list, &ilist[cn], cn, x,
318 /* Are there units owned by this country? */
319 if (lnats[cn] != 0) {
322 PR(cn, "%s planes spotted over land units in %s\n",
323 cname(plane_owner), xyas(x, y, cn));
325 setcont(cn, plane_owner, FOUND_FLY);
327 if (unfriendly[cn]) {
328 /* They are unfriendly too */
330 getilist(&ilist[cn], cn, bomb_list, esc_list, obomb,
334 PR(plane_owner, "Flying over %s land units in %s\n",
335 cname(cn), xyas(x, y, plane_owner));
338 /* We haven't intercepted yet, so intercept */
339 ac_intercept(bomb_list, esc_list, &ilist[cn], cn,
346 if (mission_flags & P_A)
347 free_shiplist(&head);
351 count_non_missiles(struct emp_qelem *list)
353 struct emp_qelem *qp;
357 /* don't intercept missiles */
358 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
359 plp = (struct plist *)qp;
360 if (!(plp->pcp->pl_flags & P_M))
367 sam_intercept(struct emp_qelem *att_list, struct emp_qelem *def_list,
368 natid def_own, natid plane_owner, coord x, coord y,
371 struct emp_qelem *aqp;
372 struct emp_qelem *anext;
373 struct emp_qelem *dqp;
374 struct emp_qelem *dnext;
379 for (aqp = att_list->q_forw,
380 dqp = def_list->q_forw;
381 aqp != att_list && dqp != def_list; aqp = anext) {
383 aplp = (struct plist *)aqp;
384 if (aplp->pcp->pl_flags & P_M)
386 if (aplp->pcp->pl_cost < 1000)
388 for (; dqp != def_list; dqp = dnext) {
390 dplp = (struct plist *)dqp;
391 if (!(dplp->pcp->pl_flags & P_M))
394 if (dplp->plane.pln_range <
395 mapdist(x, y, dplp->plane.pln_x, dplp->plane.pln_y)) {
400 if (mission_pln_equip(dplp, 0, P_F, 0) < 0) {
407 PR(plane_owner, "%s launches SAMs!\n", cname(def_own));
408 PR(def_own, "Launching SAMs at %s planes!\n",
410 ac_combat_headers(plane_owner, def_own);
418 PR(plane_owner, "\n");
421 if (delete_missiles) {
422 for (; dqp != def_list; dqp = dnext) {
424 dplp = (struct plist *)dqp;
425 if (!(dplp->pcp->pl_flags & P_M))
435 ac_intercept(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
436 struct emp_qelem *def_list, natid def_own, coord x, coord y)
441 struct emp_qelem *next;
442 struct emp_qelem *qp;
443 struct emp_qelem int_list;
448 plp = (struct plist *)bomb_list->q_forw;
449 plane_owner = plp->plane.pln_own;
453 sam_intercept(bomb_list, def_list, def_own, plane_owner, x, y, 0);
454 sam_intercept(esc_list, def_list, def_own, plane_owner, x, y, 1);
455 if (!(att_count = count_non_missiles(bomb_list) +
456 count_non_missiles(esc_list)))
459 emp_initque(&int_list);
460 for (qp = def_list->q_forw; qp != def_list; qp = next) {
462 plp = (struct plist *)qp;
464 /* SAMs interdict separately */
465 if (plp->pcp->pl_flags & P_M)
467 dist = mapdist(x, y, pp->pln_x, pp->pln_y) * 2;
468 if (pp->pln_range < dist)
470 if (mission_pln_equip(plp, 0, P_F, 0) < 0) {
475 /* got one; delete from def_list, add to int_list */
477 emp_insque(qp, &int_list);
478 pp->pln_mobil -= pln_mobcost(dist, pp, P_F);
479 putplane(pp->pln_uid, pp);
481 if (icount > att_count)
486 PR(plane_owner, "%d %s fighter%s rising to intercept!\n", icount,
487 cname(def_own), icount == 1 ? " is" : "s are");
488 PR(def_own, "%d fighter%s intercepting %s planes!\n", icount,
489 icount == 1 ? " is" : "s are", cname(plane_owner));
490 ac_combat_headers(plane_owner, def_own);
491 ac_airtoair(esc_list, &int_list);
492 ac_airtoair(bomb_list, &int_list);
493 PR(plane_owner, "\n");
498 ac_combat_headers(natid plane_owner, natid def_own)
501 " %-10.10s %-10.10s strength int odds damage results\n",
502 cname(plane_owner), cname(def_own));
504 " %-10.10s %-10.10s strength int odds damage results\n",
505 cname(def_own), cname(plane_owner));
512 ac_airtoair(struct emp_qelem *att_list, struct emp_qelem *int_list)
514 struct plist *attacker;
515 struct plist *interceptor;
516 struct emp_qelem *att;
517 struct emp_qelem *in;
521 struct emp_qelem *att_next;
522 struct emp_qelem *in_next;
524 att = att_list->q_forw;
525 in = int_list->q_forw;
528 if (QEMPTY(att_list) || QEMPTY(int_list)) {
532 while (more_att || more_int) {
533 in_next = in->q_forw;
534 att_next = att->q_forw;
535 attacker = (struct plist *)att;
537 /* skip missiles. If only missiles left, we're done */
538 if (plchr[(int)attacker->plane.pln_type].pl_flags & P_M) {
540 if (att == att_list) {
542 if (QEMPTY(att_list))
547 if (all_missiles(att_list))
551 interceptor = (struct plist *)in;
552 nplanes = attacker->plane.pln_effic;
553 if (nplanes > interceptor->plane.pln_effic)
554 nplanes = interceptor->plane.pln_effic;
555 ac_dog(attacker, interceptor);
558 if (att == att_list) {
560 if (QEMPTY(att_list))
565 if (in == int_list) {
567 if (QEMPTY(int_list))
576 all_missiles(struct emp_qelem *att_list)
578 struct emp_qelem *qp;
581 qp = att_list->q_forw;
582 while (qp != att_list) {
583 p = (struct plist *)qp;
584 if (!(plchr[(int)p->plane.pln_type].pl_flags & P_M))
593 ac_dog(struct plist *ap, struct plist *dp)
598 natid att_own, def_own;
603 att_own = ap->plane.pln_own;
604 def_own = dp->plane.pln_own;
606 PR(att_own, " %3.3s #%-4d %3.3s #%-4d",
608 ap->plane.pln_uid, dp->pcp->pl_name, dp->plane.pln_uid);
610 PR(def_own, " %3.3s #%-4d %3.3s #%-4d",
612 dp->plane.pln_uid, ap->pcp->pl_name, ap->plane.pln_uid);
613 if (ap->plane.pln_att == 0) {
614 att = ap->plane.pln_def * ap->plane.pln_effic / 100;
615 att = MAX(att, ap->pcp->pl_def / 2);
617 att = ap->plane.pln_att * ap->plane.pln_effic / 100;
618 att = MAX(att, ap->pcp->pl_att / 2);
621 def = dp->plane.pln_def * dp->plane.pln_effic / 100;
622 def = MAX(def, dp->pcp->pl_def / 2);
624 if ((ap->pcp->pl_flags & P_F) && ap->bombs != 0)
626 if ((dp->pcp->pl_flags & P_F) && dp->bombs != 0)
628 att += ap->pcp->pl_stealth / 25.0;
629 def += dp->pcp->pl_stealth / 25.0;
638 odds = ((double)att / ((double)def + (double)att));
641 intensity = roll(20) + roll(20) + roll(20) + roll(20) + 1;
643 PR(att_own, " %3d/%-3d %3d %3.2f ", att, def, intensity, odds);
644 PR(def_own, " %3d/%-3d %3d %3.2f ", def, att, intensity, odds);
648 while ((intensity--) > 0) {
652 if ((dp->plane.pln_effic - ddam) < PLANE_MINEFF)
656 if ((ap->plane.pln_effic - adam) < PLANE_MINEFF)
661 if (dp->pcp->pl_flags & P_M)
664 PR(att_own, "%3d/%-3d", adam, ddam);
665 PR(def_own, "%3d/%-3d", ddam, adam);
666 ac_planedamage(ap, def_own, adam, def_own, 1, 0, mesg);
667 strncpy(temp, mesg, 14);
668 ac_planedamage(dp, att_own, ddam, att_own, 1, 0, mesg);
669 PR(att_own, "%-13.13s %-13.13s\n", temp, mesg);
670 PR(def_own, "%-13.13s %-13.13s\n", mesg, temp);
673 setcont(att_own, def_own, FOUND_FLY);
674 setcont(def_own, att_own, FOUND_FLY);
679 * zap plane associated with plp.
680 * Damaging country is "from", damage is "dam".
681 * def_own is the country on the other side of the conflict from the plane
682 * owner. The only time def_own != from is when the interceptor is getting
685 * NOTE: This routine removes the appropriate plane element from the
686 * queue if it gets destroyed. That means that the caller must assume
687 * that the current queue pointer is invalid on return from the ac_planedamage
688 * call. (this has caused bugs in the past)
691 ac_planedamage(struct plist *plp, natid from, int dam, natid other,
692 int checkabort, int show, char *mesg)
704 plane_owner = pp->pln_own;
706 sprintf(dmess, " no damage");
711 memset(dmess, 0, sizeof(dmess));
715 if (eff < PLANE_MINEFF) {
716 sprintf(dmess, " shot down");
718 } else if (eff < 80 && chance((100 - eff) / 100.0) && checkabort) {
719 sprintf(dmess, " aborted @%2d%%", eff);
721 } else if (show == 0) {
722 sprintf(dmess, " cleared");
725 if ((plp->pcp->pl_flags & P_M) == 0) {
727 PR(plane_owner, " %s %s takes %d%s.\n",
728 cname(pp->pln_own), prplane(pp), dam, dmess);
730 PR(other, " %s %s takes %d%s.\n",
731 cname(pp->pln_own), prplane(pp), dam, dmess);
734 if (show && checkabort == 1) {
735 PR(plane_owner, "\n");
741 pp->pln_mobil -= MIN(32 + pp->pln_mobil, dam / 2);
743 if (from != 0 && (plp->pcp->pl_flags & P_M) == 0)
744 nreport(from, N_DOWN_PLANE, pp->pln_own, 1);
745 if (pp->pln_ship >= 0) {
746 getship(pp->pln_ship, &ship);
747 take_plane_off_ship(pp, &ship);
749 if (pp->pln_land >= 0) {
750 getland(pp->pln_land, &land);
751 take_plane_off_land(pp, &land);
753 makelost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x, pp->pln_y);
755 putplane(pp->pln_uid, pp);
756 emp_remque(&plp->queue);
758 } else if (disp == 2) {
759 putplane(pp->pln_uid, pp);
760 emp_remque(&plp->queue);
763 putplane(pp->pln_uid, pp);
768 ac_doflak(struct emp_qelem *list, struct sctstr *from)
775 plp = (struct plist *)list->q_forw;
776 plane_owner = plp->plane.pln_own;
778 gun = from->sct_item[I_GUN];
779 shell = from->sct_item[I_SHELL];
782 if (gun > shell * 2) {
783 shell += supply_commod(from->sct_own, from->sct_x, from->sct_y,
784 I_SHELL, (gun + 1) / 2 - shell);
785 from->sct_item[I_SHELL] = shell;
791 gun = 2.0 * tfact(from->sct_own, gun);
793 PR(plane_owner, "firing %d flak guns in %s...\n",
794 gun, xyas(from->sct_x, from->sct_y, plane_owner));
795 if (from->sct_own != 0)
796 PR(from->sct_own, "firing %d flak guns in %s...\n",
797 gun, xyas(from->sct_x, from->sct_y, from->sct_own));
798 ac_fireflak(list, from->sct_own, 0, gun);
803 ac_shipflak(struct emp_qelem *list, coord x, coord y)
818 plp = (struct plist *)list->q_forw;
819 plane_owner = plp->plane.pln_own;
821 memset(nats, 0, sizeof(nats));
823 snxtitem_xy(&ni, EF_SHIP, x, y);
824 while (!QEMPTY(list) && nxtitem(&ni, &ship)) {
825 if (ship.shp_own == 0 || ship.shp_own == plane_owner)
829 mcp = &mchr[(int)ship.shp_type];
830 if (mcp->m_flags & M_SUB)
832 rel = getrel(getnatp(ship.shp_own), plane_owner);
836 gun = MIN(ship.shp_item[I_GUN], ship.shp_glim);
838 shell = ship.shp_item[I_SHELL];
840 shell = supply_commod(ship.shp_own, ship.shp_x,
841 ship.shp_y, I_SHELL, 1);
842 ship.shp_item[I_SHELL] = shell;
843 putship(ship.shp_uid, &ship);
846 if (gun == 0 || shell == 0)
848 firing = (int)(techfact(ship.shp_tech, gun) * 2.0);
851 if (!nats[ship.shp_own]) {
852 /* First time here, print the message */
853 PR(ship.shp_own, "%s planes spotted over ships in %s\n",
854 cname(plane_owner), xyas(x, y, ship.shp_own));
855 PR(plane_owner, "Flying over %s ships in %s\n",
856 cname(ship.shp_own), xyas(x, y, plane_owner));
857 nats[ship.shp_own] = 1;
859 PR(ship.shp_own, "firing %d flak guns from %s...\n",
860 firing, prship(&ship));
866 guns = 2.0 * tfact(from, guns);
867 PR(plane_owner, "Flak! Ships firing %d flak guns...\n", guns);
868 ac_fireflak(list, from, 0, guns);
873 ac_landflak(struct emp_qelem *list, coord x, coord y)
886 plp = (struct plist *)list->q_forw;
887 plane_owner = plp->plane.pln_own;
889 memset(nats, 0, sizeof(nats));
891 snxtitem_xy(&ni, EF_LAND, x, y);
892 while (!QEMPTY(list) && nxtitem(&ni, &land)) {
893 if (land.lnd_own == 0 || land.lnd_own == plane_owner)
897 lcp = &lchr[(int)land.lnd_type];
899 if ((lcp->l_flags & L_FLAK) == 0)
902 if (land.lnd_aaf == 0)
905 if (land.lnd_ship >= 0 || land.lnd_land >= 0)
908 rel = getrel(getnatp(land.lnd_own), plane_owner);
911 firing = (int)(techfact(land.lnd_tech, land.lnd_aaf) * 3.0);
914 if (!nats[land.lnd_own]) {
915 /* First time here, print the message */
916 PR(land.lnd_own, "%s planes spotted over land units in %s\n",
917 cname(plane_owner), xyas(x, y, land.lnd_own));
918 PR(plane_owner, "Flying over %s land units in %s\n",
919 cname(land.lnd_own), xyas(x, y, plane_owner));
920 nats[land.lnd_own] = 1;
922 PR(land.lnd_own, "firing flak guns from unit %s (aa rating %d)\n",
923 prland(&land), land.lnd_aaf);
925 from = land.lnd_own; /* We always use the last owner as the from */
930 guns = 2.0 * tfact(from, guns);
931 PR(plane_owner, "Flak! Land units firing %d flak guns...\n",
933 ac_fireflak(list, from, 0, guns);
938 * Called from shipflak, landflak, and doflak.
941 ac_fireflak(struct emp_qelem *list, natid from, natid other, int guns)
947 struct emp_qelem *qp;
948 struct emp_qelem *next;
951 plp = (struct plist *)list->q_forw;
953 for (qp = list->q_forw; qp != list; qp = next) {
955 * fighters don't get shot at by flak
956 * non-tactical bombers are harder to hit with flak.
957 * ('Cause they're not dive-bombing?)
960 plp = (struct plist *)qp;
962 diff = guns - pp->pln_def;
963 if ((plp->pcp->pl_flags & P_T) == 0)
965 if (plp->pcp->pl_flags & P_X)
967 if (plp->pcp->pl_flags & P_H)
969 n = ac_flak_dam(diff);
970 ac_planedamage(plp, from, n, other, 2, 1, msg);
975 * Calculate flak damage
978 ac_flak_dam(int flak)
982 /* <-7 -7 -6 -5 -4 */
983 static float flaktable[18] = { 0.132f, 0.20f, 0.20f, 0.25f, 0.30f,
984 /* -3 -2 -1 0 +1 +2 +3 +4 */
985 0.35f, 0.40f, 0.45f, 0.50f, 0.50f, 0.55f, 0.60f, 0.65f,
986 /* +5 +6 +7 +8 >+8 */
987 0.70f,0.75f, 0.80f, 0.85f, 1.1305f };
988 enum { FLAK_MAX = sizeof(flaktable)/sizeof(flaktable[0]) - 1 };
991 mult = flaktable[FLAK_MAX];
996 mult = flaktable[flak];
999 dam = (int)((roll(8) + 2) * mult);
1006 * See if this plane is flying in this list
1009 ac_isflying(struct plnstr *plane, struct emp_qelem *list)
1011 struct emp_qelem *qp;
1012 struct emp_qelem *next;
1018 for (qp = list->q_forw; qp != list; qp = next) {
1020 plp = (struct plist *)qp;
1022 if (plane->pln_uid == pp->pln_uid)
1030 * Get a list of planes available for interception duties.
1033 getilist(struct emp_qelem *list, natid own, struct emp_qelem *a,
1034 struct emp_qelem *b, struct emp_qelem *c, struct emp_qelem *d)
1036 struct plchrstr *pcp;
1037 struct plnstr plane;
1041 struct nstr_item ni;
1046 snxtitem_all(&ni, EF_PLANE);
1047 while (nxtitem(&ni, &plane)) {
1048 if (plane.pln_own != own)
1050 pcp = &plchr[(int)plane.pln_type];
1051 if ((pcp->pl_flags & P_F) == 0)
1053 if (plane.pln_mission != 0)
1055 if (plane.pln_mobil <= 0)
1057 if (plane.pln_effic < 40)
1059 if (plane.pln_ship >= 0) {
1060 if (!can_fly(plane.pln_uid))
1062 getship(plane.pln_ship, &ship);
1063 petrol = ship.shp_item[I_PETROL];
1064 } else if (plane.pln_land >= 0) {
1065 if (!can_fly(plane.pln_uid))
1067 getland(plane.pln_land, &land);
1068 petrol = land.lnd_item[I_PETROL];
1070 getsect(plane.pln_x, plane.pln_y, §);
1071 petrol = sect.sct_item[I_PETROL];
1072 if ((sect.sct_effic < 60 || sect.sct_type != SCT_AIRPT)
1073 && (pcp->pl_flags & P_V) == 0)
1076 if (petrol <= pcp->pl_fuel / 2)
1078 /* Finally, is it in the list of planes already in
1080 if (ac_isflying(&plane, a))
1082 if (ac_isflying(&plane, b))
1084 if (ac_isflying(&plane, c))
1086 if (ac_isflying(&plane, d))
1089 ip = malloc(sizeof(*ip));
1092 ip->pcp = &plchr[(int)plane.pln_type];
1094 emp_insque(&ip->queue, list);
1102 { /* Can this plane fly from the ship or land unit it is on? */
1103 struct plnstr plane;
1106 struct plchrstr *pcp;
1107 struct mchrstr *scp;
1109 getplane(p, &plane);
1110 pcp = &plchr[(int)plane.pln_type];
1112 if (plane.pln_ship >= 0) {
1113 if (!(pcp->pl_flags & P_L) && !(pcp->pl_flags & P_M)
1114 && !(pcp->pl_flags & P_K)
1115 && !(pcp->pl_flags & P_E)
1119 getship(plane.pln_ship, &ship);
1120 scp = &mchr[(int)ship.shp_type];
1122 if ((pcp->pl_flags & P_L) && (scp->m_flags & M_FLY)) {
1126 if ((pcp->pl_flags & P_M) && (scp->m_flags & M_MSL)) {
1130 if ((pcp->pl_flags & P_K) && (scp->m_flags & M_CHOPPER)) {
1134 if ((pcp->pl_flags & P_E) && (scp->m_flags & M_XLIGHT)) {
1139 if (plane.pln_land >= 0) {
1140 if (!(pcp->pl_flags & P_E))
1143 getland(plane.pln_land, &land);
1145 if ((pcp->pl_flags & P_E) && land.lnd_maxlight) {
1154 do_evade(struct emp_qelem *bomb_list, struct emp_qelem *esc_list)
1156 struct emp_qelem *qp;
1161 for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw) {
1162 plp = (struct plist *)qp;
1163 if (evade > plp->pcp->pl_stealth / 100.0)
1164 evade = plp->pcp->pl_stealth / 100.0;
1166 for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw) {
1167 plp = (struct plist *)qp;
1168 if (evade > plp->pcp->pl_stealth / 100.0)
1169 evade = plp->pcp->pl_stealth / 100.0;