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, s_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 ");
164 "%s (#%d) ", cname(sect.sct_own), sect.sct_own);
165 PR(plane_owner, 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, dp->plane.pln_own, adam, def_own, 1, 0, mesg);
672 strncpy(temp, mesg, 14);
673 ac_planedamage(dp, ap->plane.pln_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, s_char *mesg)
705 /* s_char *sprintf(); already in misc.h [JFW] */
710 plane_owner = pp->pln_own;
712 sprintf(dmess, " no damage");
717 memset(dmess, 0, sizeof(dmess));
721 if (eff < PLANE_MINEFF) {
722 sprintf(dmess, " shot down");
724 } else if (eff < 80 && chance((100 - eff) / 100.0) && checkabort) {
725 sprintf(dmess, " aborted @%2d%%", eff);
727 } else if (show == 0) {
728 sprintf(dmess, " cleared");
731 if ((plp->pcp->pl_flags & P_M) == 0) {
733 PR(plane_owner, " %s %s takes %d%s.\n",
734 cname(pp->pln_own), prplane(pp), dam, dmess);
736 PR(other, " %s %s takes %d%s.\n",
737 cname(pp->pln_own), prplane(pp), dam, dmess);
740 if (show && checkabort == 1) {
741 PR(plane_owner, "\n");
747 pp->pln_mobil -= MIN(32 + pp->pln_mobil, dam / 2);
749 if (from != 0 && (plp->pcp->pl_flags & P_M) == 0)
750 nreport(from, N_DOWN_PLANE, pp->pln_own, 1);
751 if (pp->pln_ship >= 0) {
752 getship(pp->pln_ship, &ship);
753 take_plane_off_ship(pp, &ship);
755 if (pp->pln_land >= 0) {
756 getland(pp->pln_land, &land);
757 take_plane_off_land(pp, &land);
759 makelost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x, pp->pln_y);
761 putplane(pp->pln_uid, pp);
762 emp_remque(&plp->queue);
764 } else if (disp == 2) {
765 putplane(pp->pln_uid, pp);
766 emp_remque(&plp->queue);
769 putplane(pp->pln_uid, pp);
774 ac_doflak(struct emp_qelem *list, struct sctstr *from)
781 plp = (struct plist *)list->q_forw;
782 plane_owner = plp->plane.pln_own;
784 gun = from->sct_item[I_GUN];
785 shell = from->sct_item[I_SHELL];
788 if (gun > shell * 2) {
789 shell += supply_commod(from->sct_own, from->sct_x, from->sct_y,
790 I_SHELL, (gun + 1) / 2 - shell);
791 from->sct_item[I_SHELL] = shell;
797 gun = 2.0 * tfact(from->sct_own, gun);
799 PR(plane_owner, "firing %d flak guns in %s...\n",
800 gun, xyas(from->sct_x, from->sct_y, plane_owner));
801 if (from->sct_own != 0)
802 PR(from->sct_own, "firing %d flak guns in %s...\n",
803 gun, xyas(from->sct_x, from->sct_y, from->sct_own));
804 ac_fireflak(list, from->sct_own, 0, gun);
809 ac_shipflak(struct emp_qelem *list, coord x, coord y)
824 plp = (struct plist *)list->q_forw;
825 plane_owner = plp->plane.pln_own;
827 memset(nats, 0, sizeof(nats));
829 snxtitem_xy(&ni, EF_SHIP, x, y);
830 while (!QEMPTY(list) && nxtitem(&ni, &ship)) {
831 if (ship.shp_own == 0 || ship.shp_own == plane_owner)
835 mcp = &mchr[(int)ship.shp_type];
836 if (mcp->m_flags & M_SUB)
838 rel = getrel(getnatp(ship.shp_own), plane_owner);
842 gun = MIN(ship.shp_item[I_GUN], ship.shp_glim);
844 shell = ship.shp_item[I_SHELL];
846 shell = supply_commod(ship.shp_own, ship.shp_x,
847 ship.shp_y, I_SHELL, 1);
848 ship.shp_item[I_SHELL] = shell;
849 putship(ship.shp_uid, &ship);
852 if (gun == 0 || shell == 0)
854 firing = (int)(techfact(ship.shp_tech, (double)gun) * 2.0);
857 if (!nats[ship.shp_own]) {
858 /* First time here, print the message */
859 PR(ship.shp_own, "%s planes spotted over ships in %s\n",
860 cname(plane_owner), xyas(x, y, ship.shp_own));
861 PR(plane_owner, "Flying over %s ships in %s\n",
862 cname(ship.shp_own), xyas(x, y, plane_owner));
863 nats[ship.shp_own] = 1;
865 PR(ship.shp_own, "firing %d flak guns from %s...\n",
866 firing, prship(&ship));
872 guns = 2.0 * tfact(from, (double)guns);
873 PR(plane_owner, "Flak! Ships firing %d flak guns...\n", guns);
874 ac_fireflak(list, from, 0, guns);
879 ac_landflak(struct emp_qelem *list, coord x, coord y)
892 plp = (struct plist *)list->q_forw;
893 plane_owner = plp->plane.pln_own;
895 memset(nats, 0, sizeof(nats));
897 snxtitem_xy(&ni, EF_LAND, x, y);
898 while (!QEMPTY(list) && nxtitem(&ni, &land)) {
899 if (land.lnd_own == 0 || land.lnd_own == plane_owner)
903 lcp = &lchr[(int)land.lnd_type];
905 if ((lcp->l_flags & L_FLAK) == 0)
908 if (land.lnd_aaf == 0)
911 if (land.lnd_ship >= 0 || land.lnd_land >= 0)
914 rel = getrel(getnatp(land.lnd_own), plane_owner);
918 (int)(techfact(land.lnd_tech, (double)land.lnd_aaf) * 3.0);
921 if (!nats[land.lnd_own]) {
922 /* First time here, print the message */
923 PR(land.lnd_own, "%s planes spotted over land units in %s\n",
924 cname(plane_owner), xyas(x, y, land.lnd_own));
925 PR(plane_owner, "Flying over %s land units in %s\n",
926 cname(land.lnd_own), xyas(x, y, plane_owner));
927 nats[land.lnd_own] = 1;
929 PR(land.lnd_own, "firing flak guns from unit %s (aa rating %d)\n",
930 prland(&land), land.lnd_aaf);
932 from = land.lnd_own; /* We always use the last owner as the from */
937 guns = 2.0 * tfact(from, (double)guns);
938 PR(plane_owner, "Flak! Land units firing %d flak guns...\n",
940 ac_fireflak(list, from, 0, guns);
945 * Called from shipflak, landflak, and doflak.
948 ac_fireflak(struct emp_qelem *list, natid from, natid other, int guns)
954 struct emp_qelem *qp;
955 struct emp_qelem *next;
958 plp = (struct plist *)list->q_forw;
960 for (qp = list->q_forw; qp != list; qp = next) {
962 * fighters don't get shot at by flak
963 * non-tactical bombers are harder to hit with flak.
964 * ('Cause they're not dive-bombing?)
967 plp = (struct plist *)qp;
969 diff = guns - pp->pln_def;
970 if ((plp->pcp->pl_flags & P_T) == 0)
972 if (plp->pcp->pl_flags & P_X)
974 if (plp->pcp->pl_flags & P_H)
976 n = ac_flak_dam(diff);
977 ac_planedamage(plp, from, n, other, 2, 1, msg);
982 * Calculate flak damage
985 ac_flak_dam(int flak)
989 /* <-7 -7 -6 -5 -4 */
990 static float flaktable[18] = { 0.132f, 0.20f, 0.20f, 0.25f, 0.30f,
991 /* -3 -2 -1 0 +1 +2 +3 +4 */
992 0.35f, 0.40f, 0.45f, 0.50f, 0.50f, 0.55f, 0.60f, 0.65f,
993 /* +5 +6 +7 +8 >+8 */
994 0.70f,0.75f, 0.80f, 0.85f, 1.1305f };
995 enum { FLAK_MAX = sizeof(flaktable)/sizeof(flaktable[0]) - 1 };
998 mult = flaktable[FLAK_MAX];
1000 mult = flaktable[0];
1003 mult = flaktable[flak];
1006 dam = (int)((roll(8) + 2) * mult);
1013 * See if this plane is flying in this list
1016 ac_isflying(struct plnstr *plane, struct emp_qelem *list)
1018 struct emp_qelem *qp;
1019 struct emp_qelem *next;
1025 for (qp = list->q_forw; qp != list; qp = next) {
1027 plp = (struct plist *)qp;
1029 if (plane->pln_uid == pp->pln_uid)
1037 * Get a list of planes available for interception duties.
1040 getilist(struct emp_qelem *list, natid own, struct emp_qelem *a,
1041 struct emp_qelem *b, struct emp_qelem *c, struct emp_qelem *d)
1043 struct plchrstr *pcp;
1044 struct plnstr plane;
1048 struct nstr_item ni;
1053 snxtitem_all(&ni, EF_PLANE);
1054 while (nxtitem(&ni, &plane)) {
1055 if (plane.pln_own != own)
1057 pcp = &plchr[(int)plane.pln_type];
1058 if ((pcp->pl_flags & P_F) == 0)
1060 if (plane.pln_mission != 0)
1062 if (plane.pln_mobil <= 0)
1064 if (plane.pln_effic < 40)
1066 if (plane.pln_ship >= 0) {
1067 if (!can_fly(plane.pln_uid))
1069 getship(plane.pln_ship, &ship);
1070 petrol = ship.shp_item[I_PETROL];
1071 } else if (plane.pln_land >= 0) {
1072 if (!can_fly(plane.pln_uid))
1074 getland(plane.pln_land, &land);
1075 petrol = land.lnd_item[I_PETROL];
1077 getsect(plane.pln_x, plane.pln_y, §);
1078 petrol = sect.sct_item[I_PETROL];
1079 if ((sect.sct_effic < 60 || sect.sct_type != SCT_AIRPT)
1080 && (pcp->pl_flags & P_V) == 0)
1083 if ((float)petrol < (float)pcp->pl_fuel / 2.0)
1085 /* Finally, is it in the list of planes already in
1087 if (ac_isflying(&plane, a))
1089 if (ac_isflying(&plane, b))
1091 if (ac_isflying(&plane, c))
1093 if (ac_isflying(&plane, d))
1096 ip = malloc(sizeof(*ip));
1099 ip->pcp = &plchr[(int)plane.pln_type];
1101 emp_insque(&ip->queue, list);
1109 { /* Can this plane fly from the ship or land unit it is on? */
1110 struct plnstr plane;
1113 struct plchrstr *pcp;
1114 struct mchrstr *scp;
1116 getplane(p, &plane);
1117 pcp = &plchr[(int)plane.pln_type];
1119 if (plane.pln_ship >= 0) {
1120 if (!(pcp->pl_flags & P_L) && !(pcp->pl_flags & P_M)
1121 && !(pcp->pl_flags & P_K)
1122 && !(pcp->pl_flags & P_E)
1126 getship(plane.pln_ship, &ship);
1127 scp = &mchr[(int)ship.shp_type];
1129 if ((pcp->pl_flags & P_L) && (scp->m_flags & M_FLY)) {
1133 if ((pcp->pl_flags & P_M) && (scp->m_flags & M_MSL)) {
1137 if ((pcp->pl_flags & P_K) && (scp->m_flags & M_CHOPPER)) {
1141 if ((pcp->pl_flags & P_E) && (scp->m_flags & M_XLIGHT)) {
1146 if (plane.pln_land >= 0) {
1147 if (!(pcp->pl_flags & P_E))
1150 getland(plane.pln_land, &land);
1152 if ((pcp->pl_flags & P_E) && land.lnd_maxlight) {
1161 do_evade(struct emp_qelem *bomb_list, struct emp_qelem *esc_list)
1163 struct emp_qelem *qp;
1168 for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw) {
1169 plp = (struct plist *)qp;
1170 if (evade > ((float)plp->pcp->pl_stealth / 100.0))
1171 evade = (plp->pcp->pl_stealth / 100.0);
1173 for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw) {
1174 plp = (struct plist *)qp;
1175 if (evade > plp->pcp->pl_stealth / 100.0)
1176 evade = (plp->pcp->pl_stealth / 100.0);