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"
58 #define MAX(x,y) ((x) < (y) ? (y) : (x))
61 static void getilist(struct emp_qelem *list, natid own,
62 struct emp_qelem *a, struct emp_qelem *b,
63 struct emp_qelem *c, struct emp_qelem *d);
64 static void ac_dog(struct plist *ap, struct plist *dp);
67 ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
68 coord x, coord y, char *path, int mission_flags,
69 int no_air_defense, struct emp_qelem *obomb,
70 struct emp_qelem *oesc)
72 int val, non_missiles;
78 int unfriendly[MAXNOC];
80 struct emp_qelem ilist[MAXNOC], *qp;
90 struct natstr *over, *mynatp;
93 struct shiplist *head = NULL;
95 int intown = 0; /* Last owner to intercept */
96 /* We want to only intercept once per sector per owner. So, if we overfly
97 a sector, and then overfly some land units or ships, we don't want to
98 potentially intercept 3 times. */
100 plp = (struct plist *)bomb_list->q_forw;
101 plane_owner = plp->plane.pln_own;
103 strncpy(mypath, path, sizeof(mypath));
106 memset(overfly, 0, sizeof(overfly));
107 memset(gotilist, 0, sizeof(gotilist));
108 memset(unfriendly, 0, sizeof(unfriendly));
109 for (cn = 1; cn < MAXNOC; cn++) {
110 if ((mynatp = getnatp(cn)) == 0)
112 rel = getrel(mynatp, plane_owner);
115 if (plane_owner == cn)
119 if (mission_flags & PM_R) {
120 if (mission_flags & P_S) {
121 PR(plane_owner, "\nSPY Plane report\n");
124 } else if (mission_flags & P_A) {
125 PR(plane_owner, "\nAnti-Sub Patrol report\n");
127 PR(plane_owner, "\nReconnaissance report\n");
132 pln_removedupes(bomb_list, esc_list);
133 while ((dir = mypath[myp++]) && !QEMPTY(bomb_list)) {
134 if ((val = diridx(dir)) == DIR_STOP)
136 /* XXX using xnorm is probably bad */
137 x = xnorm(x + diroff[val][0]);
138 y = ynorm(y + diroff[val][1]);
139 getsect(x, y, §);
140 over = getnatp(sect.sct_own);
142 if (mission_flags & PM_R) {
144 setcont(plane_owner, sect.sct_own, FOUND_FLY);
145 if (sect.sct_type == SCT_WATER) {
146 PR(plane_owner, "flying over %s at %s\n",
147 dchr[sect.sct_type].d_name, xyas(x, y, plane_owner));
148 if (mission_flags & PM_S)
149 plane_sweep(bomb_list, x, y);
150 if (mission_flags & P_A) {
151 plane_sona(bomb_list, x, y, &head);
153 changed += map_set(plane_owner,
154 sect.sct_x, sect.sct_y,
155 dchr[sect.sct_type].d_mnem, 0);
156 } else if (mission_flags & P_S) {
157 satdisp(§, (mission_flags & P_I) ? 10 : 50, 1);
159 /* This is borrowed from lookout */
160 if (sect.sct_own == plane_owner)
161 PR(plane_owner, "Your ");
163 PR(plane_owner, "%s (#%d) ",
164 cname(sect.sct_own), sect.sct_own);
165 PR(plane_owner, "%s", dchr[sect.sct_type].d_name);
166 changed += map_set(plane_owner,
167 sect.sct_x, sect.sct_y,
168 dchr[sect.sct_type].d_mnem, 0);
169 PR(plane_owner, " %d%% efficient ",
170 (sect.sct_own == plane_owner) ?
171 sect.sct_effic : roundintby((int)sect.sct_effic, 25));
172 civ = sect.sct_item[I_CIVIL];
173 mil = sect.sct_item[I_MILIT];
175 PR(plane_owner, "with %s%d civ ",
176 (sect.sct_own == plane_owner) ?
178 (sect.sct_own == plane_owner) ?
179 civ : roundintby(civ, 25));
181 PR(plane_owner, "with %s%d mil ",
182 (sect.sct_own == plane_owner) ?
184 (sect.sct_own == plane_owner) ?
185 mil : roundintby(mil, 25));
186 PR(plane_owner, "@ %s\n", xyas(x, y, plane_owner));
189 PR(plane_owner, "flying over %s at %s\n",
190 dchr[sect.sct_type].d_name, xyas(x, y, plane_owner));
191 changed += map_set(plane_owner, sect.sct_x,
192 sect.sct_y, dchr[sect.sct_type].d_mnem, 0);
194 if ((rel = getrel(over, plane_owner)) == ALLIED)
197 evaded = do_evade(bomb_list, esc_list);
199 if (sect.sct_own != 0 && sect.sct_own != plane_owner && !evaded) {
200 /* We only show planes overhead if they didn't
202 overfly[sect.sct_own]++;
203 PR(sect.sct_own, "%s planes spotted over %s\n",
204 cname(plane_owner), xyas(x, y, sect.sct_own));
206 setcont(cn, plane_owner, FOUND_FLY);
211 if (unfriendly[sect.sct_own])
212 ac_doflak(bomb_list, §);
213 /* If bombers left, fire flak from units and ships */
214 if (!QEMPTY(bomb_list))
215 ac_landflak(bomb_list, x, y);
216 if (!QEMPTY(bomb_list))
217 ac_shipflak(bomb_list, x, y);
219 /* mission planes aborted due to flak -- don't send escorts */
220 if (QEMPTY(bomb_list))
222 if (!no_air_defense && !evaded)
223 air_defense(x, y, plane_owner, bomb_list, esc_list);
225 if (sect.sct_own == 0 || sect.sct_own == plane_owner)
232 for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw) {
233 struct plist *ip = (struct plist *)qp;
234 if (!(plchr[(int)ip->plane.pln_type].pl_flags & P_M))
241 if (unfriendly[sect.sct_own] && !gotilist[sect.sct_own]) {
242 getilist(&ilist[sect.sct_own], sect.sct_own,
243 bomb_list, esc_list, obomb, oesc);
244 gotilist[sect.sct_own]++;
248 ac_intercept(bomb_list, esc_list, &ilist[sect.sct_own],
250 intown = sect.sct_own;
253 /* Let's report all of the overflights even if aborted */
254 for (cn = 1; cn < MAXNOC; cn++) {
255 if (plane_owner == cn)
258 nreport(plane_owner, N_OVFLY_SECT, cn, overfly[cn]);
260 /* If the map changed, update it */
262 if (!update_pending && plane_owner == player->cnum)
263 writemap(player->cnum);
264 /* Now, if the bomber and escort lists are empty, we are done */
265 if (QEMPTY(bomb_list) && QEMPTY(esc_list)) {
266 if (mission_flags & P_A)
267 free_shiplist(&head);
271 /* Something made it through */
272 /* Go figure out if there are ships in this sector, and who's they are */
273 memset(nats, 0, sizeof(nats));
274 snxtitem_xy(&ni, EF_SHIP, x, y);
275 while (nxtitem(&ni, &ship)) {
276 if (mchr[(int)ship.shp_type].m_flags & M_SUB)
278 nats[ship.shp_own]++;
280 /* Go figure out if there are units in this sector, and who's they are */
281 memset(lnats, 0, sizeof(lnats));
282 snxtitem_xy(&ni, EF_LAND, x, y);
283 while (nxtitem(&ni, &land)) {
284 if (land.lnd_ship >= 0 || land.lnd_land >= 0)
286 lnats[land.lnd_own]++;
289 /* Now, let's make life a little rougher. */
290 for (cn = 1; cn < MAXNOC && !QEMPTY(bomb_list); cn++) {
291 if (plane_owner == cn)
294 /* Are there ships owned by this country? */
298 PR(cn, "%s planes spotted over ships in %s\n",
299 cname(plane_owner), xyas(x, y, cn));
301 setcont(cn, plane_owner, FOUND_FLY);
303 if (unfriendly[cn]) {
304 /* They are unfriendly too */
306 getilist(&ilist[cn], cn, bomb_list, esc_list, obomb,
310 PR(plane_owner, "Flying over %s ships in %s\n", cname(cn),
311 xyas(x, y, plane_owner));
312 /* This makes going for ships in harbors tough */
314 /* We already fired flak up above. Now we intercept again if we haven't already */
315 /* Flag that we intercepted */
317 /* And now intercept again */
318 ac_intercept(bomb_list, esc_list, &ilist[cn], cn, x,
323 /* Are there units owned by this country? */
324 if (lnats[cn] != 0) {
327 PR(cn, "%s planes spotted over land units in %s\n",
328 cname(plane_owner), xyas(x, y, cn));
330 setcont(cn, plane_owner, FOUND_FLY);
332 if (unfriendly[cn]) {
333 /* They are unfriendly too */
335 getilist(&ilist[cn], cn, bomb_list, esc_list, obomb,
339 PR(plane_owner, "Flying over %s land units in %s\n",
340 cname(cn), xyas(x, y, plane_owner));
343 /* We haven't intercepted yet, so intercept */
344 ac_intercept(bomb_list, esc_list, &ilist[cn], cn,
351 if (mission_flags & P_A)
352 free_shiplist(&head);
356 count_non_missiles(struct emp_qelem *list)
358 struct emp_qelem *qp;
362 /* don't intercept missiles */
363 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
364 plp = (struct plist *)qp;
365 if (!(plp->pcp->pl_flags & P_M))
372 sam_intercept(struct emp_qelem *att_list, struct emp_qelem *def_list,
373 natid def_own, natid plane_owner, coord x, coord y,
376 struct emp_qelem *aqp;
377 struct emp_qelem *anext;
378 struct emp_qelem *dqp;
379 struct emp_qelem *dnext;
384 for (aqp = att_list->q_forw,
385 dqp = def_list->q_forw;
386 aqp != att_list && dqp != def_list; aqp = anext) {
388 aplp = (struct plist *)aqp;
389 if (aplp->pcp->pl_flags & P_M)
391 if (aplp->pcp->pl_cost < 1000)
393 for (; dqp != def_list; dqp = dnext) {
395 dplp = (struct plist *)dqp;
396 if (!(dplp->pcp->pl_flags & P_M))
399 if (dplp->plane.pln_range <
400 mapdist(x, y, dplp->plane.pln_x, dplp->plane.pln_y)) {
405 if (mission_pln_equip(dplp, 0, P_F, 0) < 0) {
412 PR(plane_owner, "%s launches SAMs!\n", cname(def_own));
413 PR(def_own, "Launching SAMs at %s planes!\n",
415 ac_combat_headers(plane_owner, def_own);
423 PR(plane_owner, "\n");
426 if (delete_missiles) {
427 for (; dqp != def_list; dqp = dnext) {
429 dplp = (struct plist *)dqp;
430 if (!(dplp->pcp->pl_flags & P_M))
440 ac_intercept(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
441 struct emp_qelem *def_list, natid def_own, coord x, coord y)
446 struct emp_qelem *next;
447 struct emp_qelem *qp;
448 struct emp_qelem int_list;
453 plp = (struct plist *)bomb_list->q_forw;
454 plane_owner = plp->plane.pln_own;
458 sam_intercept(bomb_list, def_list, def_own, plane_owner, x, y, 0);
459 sam_intercept(esc_list, def_list, def_own, plane_owner, x, y, 1);
460 if (!(att_count = count_non_missiles(bomb_list) +
461 count_non_missiles(esc_list)))
464 emp_initque(&int_list);
465 for (qp = def_list->q_forw; qp != def_list; qp = next) {
467 plp = (struct plist *)qp;
469 /* SAMs interdict separately */
470 if (plp->pcp->pl_flags & P_M)
472 dist = mapdist(x, y, pp->pln_x, pp->pln_y) * 2;
473 if (pp->pln_range < dist)
475 if (mission_pln_equip(plp, 0, P_F, 0) < 0) {
480 /* got one; delete from def_list, add to int_list */
482 emp_insque(qp, &int_list);
483 pp->pln_mobil -= pln_mobcost(dist, pp, P_F);
484 putplane(pp->pln_uid, pp);
486 if (icount > att_count)
491 PR(plane_owner, "%d %s fighter%s rising to intercept!\n", icount,
492 cname(def_own), icount == 1 ? " is" : "s are");
493 PR(def_own, "%d fighter%s intercepting %s planes!\n", icount,
494 icount == 1 ? " is" : "s are", cname(plane_owner));
495 ac_combat_headers(plane_owner, def_own);
496 ac_airtoair(esc_list, &int_list);
497 ac_airtoair(bomb_list, &int_list);
498 PR(plane_owner, "\n");
503 ac_combat_headers(natid plane_owner, natid def_own)
506 " %-10.10s %-10.10s strength int odds damage results\n",
507 cname(plane_owner), cname(def_own));
509 " %-10.10s %-10.10s strength int odds damage results\n",
510 cname(def_own), cname(plane_owner));
517 ac_airtoair(struct emp_qelem *att_list, struct emp_qelem *int_list)
519 struct plist *attacker;
520 struct plist *interceptor;
521 struct emp_qelem *att;
522 struct emp_qelem *in;
526 struct emp_qelem *att_next;
527 struct emp_qelem *in_next;
529 att = att_list->q_forw;
530 in = int_list->q_forw;
533 if (QEMPTY(att_list) || QEMPTY(int_list)) {
537 while (more_att || more_int) {
538 in_next = in->q_forw;
539 att_next = att->q_forw;
540 attacker = (struct plist *)att;
542 /* skip missiles. If only missiles left, we're done */
543 if (plchr[(int)attacker->plane.pln_type].pl_flags & P_M) {
545 if (att == att_list) {
547 if (QEMPTY(att_list))
552 if (all_missiles(att_list))
556 interceptor = (struct plist *)in;
557 nplanes = attacker->plane.pln_effic;
558 if (nplanes > interceptor->plane.pln_effic)
559 nplanes = interceptor->plane.pln_effic;
560 ac_dog(attacker, interceptor);
563 if (att == att_list) {
565 if (QEMPTY(att_list))
570 if (in == int_list) {
572 if (QEMPTY(int_list))
581 all_missiles(struct emp_qelem *att_list)
583 struct emp_qelem *qp;
586 qp = att_list->q_forw;
587 while (qp != att_list) {
588 p = (struct plist *)qp;
589 if (!(plchr[(int)p->plane.pln_type].pl_flags & P_M))
598 ac_dog(struct plist *ap, struct plist *dp)
603 natid att_own, def_own;
608 att_own = ap->plane.pln_own;
609 def_own = dp->plane.pln_own;
611 PR(att_own, " %3.3s #%-4d %3.3s #%-4d",
613 ap->plane.pln_uid, dp->pcp->pl_name, dp->plane.pln_uid);
615 PR(def_own, " %3.3s #%-4d %3.3s #%-4d",
617 dp->plane.pln_uid, ap->pcp->pl_name, ap->plane.pln_uid);
618 if (ap->plane.pln_att == 0) {
619 att = ap->plane.pln_def * ap->plane.pln_effic / 100;
620 att = MAX(att, ap->pcp->pl_def / 2);
622 att = ap->plane.pln_att * ap->plane.pln_effic / 100;
623 att = MAX(att, ap->pcp->pl_att / 2);
626 def = dp->plane.pln_def * dp->plane.pln_effic / 100;
627 def = MAX(def, dp->pcp->pl_def / 2);
629 if ((ap->pcp->pl_flags & P_F) && ap->bombs != 0)
631 if ((dp->pcp->pl_flags & P_F) && dp->bombs != 0)
633 att += ((float)ap->pcp->pl_stealth / 25.0);
634 def += ((float)dp->pcp->pl_stealth / 25.0);
643 odds = ((double)att / ((double)def + (double)att));
646 intensity = roll(20) + roll(20) + roll(20) + roll(20) + 1;
648 PR(att_own, " %3d/%-3d %3d %3.2f ", att, def, intensity, odds);
649 PR(def_own, " %3d/%-3d %3d %3.2f ", def, att, intensity, odds);
653 while ((intensity--) > 0) {
657 if ((dp->plane.pln_effic - ddam) < PLANE_MINEFF)
661 if ((ap->plane.pln_effic - adam) < PLANE_MINEFF)
666 if (dp->pcp->pl_flags & P_M)
669 PR(att_own, "%3d/%-3d", adam, ddam);
670 PR(def_own, "%3d/%-3d", ddam, adam);
671 ac_planedamage(ap, def_own, adam, def_own, 1, 0, mesg);
672 strncpy(temp, mesg, 14);
673 ac_planedamage(dp, att_own, ddam, att_own, 1, 0, mesg);
674 PR(att_own, "%-13.13s %-13.13s\n", temp, mesg);
675 PR(def_own, "%-13.13s %-13.13s\n", mesg, temp);
678 setcont(att_own, def_own, FOUND_FLY);
679 setcont(def_own, att_own, FOUND_FLY);
684 * zap plane associated with plp.
685 * Damaging country is "from", damage is "dam".
686 * def_own is the country on the other side of the conflict from the plane
687 * owner. The only time def_own != from is when the interceptor is getting
690 * NOTE: This routine removes the appropriate plane element from the
691 * queue if it gets destroyed. That means that the caller must assume
692 * that the current queue pointer is invalid on return from the ac_planedamage
693 * call. (this has caused bugs in the past)
696 ac_planedamage(struct plist *plp, natid from, int dam, natid other,
697 int checkabort, int show, char *mesg)
709 plane_owner = pp->pln_own;
711 sprintf(dmess, " no damage");
716 memset(dmess, 0, sizeof(dmess));
720 if (eff < PLANE_MINEFF) {
721 sprintf(dmess, " shot down");
723 } else if (eff < 80 && chance((100 - eff) / 100.0) && checkabort) {
724 sprintf(dmess, " aborted @%2d%%", eff);
726 } else if (show == 0) {
727 sprintf(dmess, " cleared");
730 if ((plp->pcp->pl_flags & P_M) == 0) {
732 PR(plane_owner, " %s %s takes %d%s.\n",
733 cname(pp->pln_own), prplane(pp), dam, dmess);
735 PR(other, " %s %s takes %d%s.\n",
736 cname(pp->pln_own), prplane(pp), dam, dmess);
739 if (show && checkabort == 1) {
740 PR(plane_owner, "\n");
746 pp->pln_mobil -= MIN(32 + pp->pln_mobil, dam / 2);
748 if (from != 0 && (plp->pcp->pl_flags & P_M) == 0)
749 nreport(from, N_DOWN_PLANE, pp->pln_own, 1);
750 if (pp->pln_ship >= 0) {
751 getship(pp->pln_ship, &ship);
752 take_plane_off_ship(pp, &ship);
754 if (pp->pln_land >= 0) {
755 getland(pp->pln_land, &land);
756 take_plane_off_land(pp, &land);
758 makelost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x, pp->pln_y);
760 putplane(pp->pln_uid, pp);
761 emp_remque(&plp->queue);
763 } else if (disp == 2) {
764 putplane(pp->pln_uid, pp);
765 emp_remque(&plp->queue);
768 putplane(pp->pln_uid, pp);
773 ac_doflak(struct emp_qelem *list, struct sctstr *from)
780 plp = (struct plist *)list->q_forw;
781 plane_owner = plp->plane.pln_own;
783 gun = from->sct_item[I_GUN];
784 shell = from->sct_item[I_SHELL];
787 if (gun > shell * 2) {
788 shell += supply_commod(from->sct_own, from->sct_x, from->sct_y,
789 I_SHELL, (gun + 1) / 2 - shell);
790 from->sct_item[I_SHELL] = shell;
796 gun = 2.0 * tfact(from->sct_own, gun);
798 PR(plane_owner, "firing %d flak guns in %s...\n",
799 gun, xyas(from->sct_x, from->sct_y, plane_owner));
800 if (from->sct_own != 0)
801 PR(from->sct_own, "firing %d flak guns in %s...\n",
802 gun, xyas(from->sct_x, from->sct_y, from->sct_own));
803 ac_fireflak(list, from->sct_own, 0, gun);
808 ac_shipflak(struct emp_qelem *list, coord x, coord y)
823 plp = (struct plist *)list->q_forw;
824 plane_owner = plp->plane.pln_own;
826 memset(nats, 0, sizeof(nats));
828 snxtitem_xy(&ni, EF_SHIP, x, y);
829 while (!QEMPTY(list) && nxtitem(&ni, &ship)) {
830 if (ship.shp_own == 0 || ship.shp_own == plane_owner)
834 mcp = &mchr[(int)ship.shp_type];
835 if (mcp->m_flags & M_SUB)
837 rel = getrel(getnatp(ship.shp_own), plane_owner);
841 gun = MIN(ship.shp_item[I_GUN], ship.shp_glim);
843 shell = ship.shp_item[I_SHELL];
845 shell = supply_commod(ship.shp_own, ship.shp_x,
846 ship.shp_y, I_SHELL, 1);
847 ship.shp_item[I_SHELL] = shell;
848 putship(ship.shp_uid, &ship);
851 if (gun == 0 || shell == 0)
853 firing = (int)(techfact(ship.shp_tech, (double)gun) * 2.0);
856 if (!nats[ship.shp_own]) {
857 /* First time here, print the message */
858 PR(ship.shp_own, "%s planes spotted over ships in %s\n",
859 cname(plane_owner), xyas(x, y, ship.shp_own));
860 PR(plane_owner, "Flying over %s ships in %s\n",
861 cname(ship.shp_own), xyas(x, y, plane_owner));
862 nats[ship.shp_own] = 1;
864 PR(ship.shp_own, "firing %d flak guns from %s...\n",
865 firing, prship(&ship));
871 guns = 2.0 * tfact(from, (double)guns);
872 PR(plane_owner, "Flak! Ships firing %d flak guns...\n", guns);
873 ac_fireflak(list, from, 0, guns);
878 ac_landflak(struct emp_qelem *list, coord x, coord y)
891 plp = (struct plist *)list->q_forw;
892 plane_owner = plp->plane.pln_own;
894 memset(nats, 0, sizeof(nats));
896 snxtitem_xy(&ni, EF_LAND, x, y);
897 while (!QEMPTY(list) && nxtitem(&ni, &land)) {
898 if (land.lnd_own == 0 || land.lnd_own == plane_owner)
902 lcp = &lchr[(int)land.lnd_type];
904 if ((lcp->l_flags & L_FLAK) == 0)
907 if (land.lnd_aaf == 0)
910 if (land.lnd_ship >= 0 || land.lnd_land >= 0)
913 rel = getrel(getnatp(land.lnd_own), plane_owner);
917 (int)(techfact(land.lnd_tech, (double)land.lnd_aaf) * 3.0);
920 if (!nats[land.lnd_own]) {
921 /* First time here, print the message */
922 PR(land.lnd_own, "%s planes spotted over land units in %s\n",
923 cname(plane_owner), xyas(x, y, land.lnd_own));
924 PR(plane_owner, "Flying over %s land units in %s\n",
925 cname(land.lnd_own), xyas(x, y, plane_owner));
926 nats[land.lnd_own] = 1;
928 PR(land.lnd_own, "firing flak guns from unit %s (aa rating %d)\n",
929 prland(&land), land.lnd_aaf);
931 from = land.lnd_own; /* We always use the last owner as the from */
936 guns = 2.0 * tfact(from, (double)guns);
937 PR(plane_owner, "Flak! Land units firing %d flak guns...\n",
939 ac_fireflak(list, from, 0, guns);
944 * Called from shipflak, landflak, and doflak.
947 ac_fireflak(struct emp_qelem *list, natid from, natid other, int guns)
953 struct emp_qelem *qp;
954 struct emp_qelem *next;
957 plp = (struct plist *)list->q_forw;
959 for (qp = list->q_forw; qp != list; qp = next) {
961 * fighters don't get shot at by flak
962 * non-tactical bombers are harder to hit with flak.
963 * ('Cause they're not dive-bombing?)
966 plp = (struct plist *)qp;
968 diff = guns - pp->pln_def;
969 if ((plp->pcp->pl_flags & P_T) == 0)
971 if (plp->pcp->pl_flags & P_X)
973 if (plp->pcp->pl_flags & P_H)
975 n = ac_flak_dam(diff);
976 ac_planedamage(plp, from, n, other, 2, 1, msg);
981 * Calculate flak damage
984 ac_flak_dam(int flak)
988 /* <-7 -7 -6 -5 -4 */
989 static float flaktable[18] = { 0.132f, 0.20f, 0.20f, 0.25f, 0.30f,
990 /* -3 -2 -1 0 +1 +2 +3 +4 */
991 0.35f, 0.40f, 0.45f, 0.50f, 0.50f, 0.55f, 0.60f, 0.65f,
992 /* +5 +6 +7 +8 >+8 */
993 0.70f,0.75f, 0.80f, 0.85f, 1.1305f };
994 enum { FLAK_MAX = sizeof(flaktable)/sizeof(flaktable[0]) - 1 };
997 mult = flaktable[FLAK_MAX];
1002 mult = flaktable[flak];
1005 dam = (int)((roll(8) + 2) * mult);
1012 * See if this plane is flying in this list
1015 ac_isflying(struct plnstr *plane, struct emp_qelem *list)
1017 struct emp_qelem *qp;
1018 struct emp_qelem *next;
1024 for (qp = list->q_forw; qp != list; qp = next) {
1026 plp = (struct plist *)qp;
1028 if (plane->pln_uid == pp->pln_uid)
1036 * Get a list of planes available for interception duties.
1039 getilist(struct emp_qelem *list, natid own, struct emp_qelem *a,
1040 struct emp_qelem *b, struct emp_qelem *c, struct emp_qelem *d)
1042 struct plchrstr *pcp;
1043 struct plnstr plane;
1047 struct nstr_item ni;
1052 snxtitem_all(&ni, EF_PLANE);
1053 while (nxtitem(&ni, &plane)) {
1054 if (plane.pln_own != own)
1056 pcp = &plchr[(int)plane.pln_type];
1057 if ((pcp->pl_flags & P_F) == 0)
1059 if (plane.pln_mission != 0)
1061 if (plane.pln_mobil <= 0)
1063 if (plane.pln_effic < 40)
1065 if (plane.pln_ship >= 0) {
1066 if (!can_fly(plane.pln_uid))
1068 getship(plane.pln_ship, &ship);
1069 petrol = ship.shp_item[I_PETROL];
1070 } else if (plane.pln_land >= 0) {
1071 if (!can_fly(plane.pln_uid))
1073 getland(plane.pln_land, &land);
1074 petrol = land.lnd_item[I_PETROL];
1076 getsect(plane.pln_x, plane.pln_y, §);
1077 petrol = sect.sct_item[I_PETROL];
1078 if ((sect.sct_effic < 60 || sect.sct_type != SCT_AIRPT)
1079 && (pcp->pl_flags & P_V) == 0)
1082 if ((float)petrol < (float)pcp->pl_fuel / 2.0)
1084 /* Finally, is it in the list of planes already in
1086 if (ac_isflying(&plane, a))
1088 if (ac_isflying(&plane, b))
1090 if (ac_isflying(&plane, c))
1092 if (ac_isflying(&plane, d))
1095 ip = malloc(sizeof(*ip));
1098 ip->pcp = &plchr[(int)plane.pln_type];
1100 emp_insque(&ip->queue, list);
1108 { /* Can this plane fly from the ship or land unit it is on? */
1109 struct plnstr plane;
1112 struct plchrstr *pcp;
1113 struct mchrstr *scp;
1115 getplane(p, &plane);
1116 pcp = &plchr[(int)plane.pln_type];
1118 if (plane.pln_ship >= 0) {
1119 if (!(pcp->pl_flags & P_L) && !(pcp->pl_flags & P_M)
1120 && !(pcp->pl_flags & P_K)
1121 && !(pcp->pl_flags & P_E)
1125 getship(plane.pln_ship, &ship);
1126 scp = &mchr[(int)ship.shp_type];
1128 if ((pcp->pl_flags & P_L) && (scp->m_flags & M_FLY)) {
1132 if ((pcp->pl_flags & P_M) && (scp->m_flags & M_MSL)) {
1136 if ((pcp->pl_flags & P_K) && (scp->m_flags & M_CHOPPER)) {
1140 if ((pcp->pl_flags & P_E) && (scp->m_flags & M_XLIGHT)) {
1145 if (plane.pln_land >= 0) {
1146 if (!(pcp->pl_flags & P_E))
1149 getland(plane.pln_land, &land);
1151 if ((pcp->pl_flags & P_E) && land.lnd_maxlight) {
1160 do_evade(struct emp_qelem *bomb_list, struct emp_qelem *esc_list)
1162 struct emp_qelem *qp;
1167 for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw) {
1168 plp = (struct plist *)qp;
1169 if (evade > ((float)plp->pcp->pl_stealth / 100.0))
1170 evade = (plp->pcp->pl_stealth / 100.0);
1172 for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw) {
1173 plp = (struct plist *)qp;
1174 if (evade > plp->pcp->pl_stealth / 100.0)
1175 evade = (plp->pcp->pl_stealth / 100.0);