2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2008, 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
34 * Markus Armbruster, 2006-2008
50 #include "prototypes.h"
55 #define FLAK_GUN_MAX 14
57 static void ac_intercept(struct emp_qelem *, struct emp_qelem *,
58 struct emp_qelem *, natid, coord, coord);
59 static int all_missiles(struct emp_qelem *);
60 static void ac_dog(struct plist *, struct plist *);
61 static void ac_planedamage(struct plist *, natid, int, natid, int,
63 static void ac_doflak(struct emp_qelem *, struct sctstr *);
64 static void ac_landflak(struct emp_qelem *, coord, coord);
65 static void ac_shipflak(struct emp_qelem *, coord, coord);
66 static void ac_fireflak(struct emp_qelem *, natid, int);
67 static void getilist(struct emp_qelem *, natid);
68 static int do_evade(struct emp_qelem *, struct emp_qelem *);
71 ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
72 coord x, coord y, char *path, int mission_flags,
75 int val, non_missiles;
81 int unfriendly[MAXNOC];
83 struct emp_qelem ilist[MAXNOC], *qp;
93 struct natstr *over, *mynatp;
96 struct shiplist *head = NULL;
98 int intown = 0; /* Last owner to intercept */
99 /* We want to only intercept once per sector per owner. So, if we overfly
100 a sector, and then overfly some land units or ships, we don't want to
101 potentially intercept 3 times. */
103 plp = (struct plist *)bomb_list->q_forw;
104 plane_owner = plp->plane.pln_own;
106 strncpy(mypath, path, sizeof(mypath));
109 memset(overfly, 0, sizeof(overfly));
110 memset(gotilist, 0, sizeof(gotilist));
111 memset(unfriendly, 0, sizeof(unfriendly));
112 for (cn = 1; cn < MAXNOC; cn++) {
113 if ((mynatp = getnatp(cn)) == 0)
115 rel = getrel(mynatp, plane_owner);
118 if (plane_owner == cn)
122 if (mission_flags & PM_R) {
123 if (mission_flags & P_S) {
124 PR(plane_owner, "\nSPY Plane report\n");
127 } else if (mission_flags & P_A) {
128 PR(plane_owner, "\nAnti-Sub Patrol report\n");
130 PR(plane_owner, "\nReconnaissance report\n");
135 while ((dir = mypath[myp++]) && !QEMPTY(bomb_list)) {
136 if ((val = diridx(dir)) == DIR_STOP)
138 /* XXX using xnorm is probably bad */
139 x = xnorm(x + diroff[val][0]);
140 y = ynorm(y + diroff[val][1]);
141 getsect(x, y, §);
142 over = getnatp(sect.sct_own);
144 if (mission_flags & PM_R) {
146 setcont(plane_owner, sect.sct_own, FOUND_FLY);
147 if (sect.sct_type == SCT_WATER) {
148 PR(plane_owner, "flying over %s at %s\n",
149 dchr[sect.sct_type].d_name, xyas(x, y, plane_owner));
150 if (mission_flags & PM_S)
151 plane_sweep(bomb_list, x, y);
152 if (mission_flags & P_A) {
153 plane_sona(bomb_list, x, y, &head);
155 changed += map_set(plane_owner,
156 sect.sct_x, sect.sct_y,
157 dchr[sect.sct_type].d_mnem, 0);
158 } else if (mission_flags & P_S) {
159 satdisp_sect(§, (mission_flags & P_I) ? 10 : 50);
161 /* This is borrowed from lookout */
162 if (sect.sct_own == plane_owner)
163 PR(plane_owner, "Your ");
165 PR(plane_owner, "%s (#%d) ",
166 cname(sect.sct_own), sect.sct_own);
167 PR(plane_owner, "%s", dchr[sect.sct_type].d_name);
168 changed += map_set(plane_owner,
169 sect.sct_x, sect.sct_y,
170 dchr[sect.sct_type].d_mnem, 0);
171 PR(plane_owner, " %d%% efficient ",
172 (sect.sct_own == plane_owner) ?
173 sect.sct_effic : roundintby((int)sect.sct_effic, 25));
174 civ = sect.sct_item[I_CIVIL];
175 mil = sect.sct_item[I_MILIT];
177 PR(plane_owner, "with %s%d civ ",
178 (sect.sct_own == plane_owner) ?
180 (sect.sct_own == plane_owner) ?
181 civ : roundintby(civ, 25));
183 PR(plane_owner, "with %s%d mil ",
184 (sect.sct_own == plane_owner) ?
186 (sect.sct_own == plane_owner) ?
187 mil : roundintby(mil, 25));
188 PR(plane_owner, "@ %s\n", xyas(x, y, plane_owner));
190 if (mission_flags & P_S)
191 satdisp_units(sect.sct_x, sect.sct_y);
193 PR(plane_owner, "flying over %s at %s\n",
194 dchr[sect.sct_type].d_name, xyas(x, y, plane_owner));
195 changed += map_set(plane_owner, sect.sct_x, sect.sct_y,
196 dchr[sect.sct_type].d_mnem, 0);
198 if ((rel = getrel(over, plane_owner)) == ALLIED)
201 evaded = do_evade(bomb_list, esc_list);
203 if (sect.sct_own != 0 && sect.sct_own != plane_owner && !evaded) {
204 /* We only show planes overhead if they didn't
206 overfly[sect.sct_own]++;
207 PR(sect.sct_own, "%s planes spotted over %s\n",
208 cname(plane_owner), xyas(x, y, sect.sct_own));
210 setcont(cn, plane_owner, FOUND_FLY);
215 if (unfriendly[sect.sct_own])
216 ac_doflak(bomb_list, §);
217 /* If bombers left, fire flak from units and ships */
218 if (!QEMPTY(bomb_list))
219 ac_landflak(bomb_list, x, y);
220 if (!QEMPTY(bomb_list))
221 ac_shipflak(bomb_list, x, y);
223 /* mission planes aborted due to flak -- don't send escorts */
224 if (QEMPTY(bomb_list))
226 if (!no_air_defense && !evaded)
227 air_defense(x, y, plane_owner, bomb_list, esc_list);
229 if (sect.sct_own == 0 || sect.sct_own == plane_owner)
236 for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw) {
237 struct plist *ip = (struct plist *)qp;
238 if (!(plchr[(int)ip->plane.pln_type].pl_flags & P_M))
245 if (unfriendly[sect.sct_own] && !gotilist[sect.sct_own]) {
246 getilist(&ilist[sect.sct_own], sect.sct_own);
247 gotilist[sect.sct_own]++;
251 ac_intercept(bomb_list, esc_list, &ilist[sect.sct_own],
253 intown = sect.sct_own;
256 /* Let's report all of the overflights even if aborted */
257 for (cn = 1; cn < MAXNOC; cn++) {
258 if (plane_owner == cn)
261 nreport(plane_owner, N_OVFLY_SECT, cn, overfly[cn]);
263 /* If the map changed, update it */
265 writemap(player->cnum);
266 /* Now, if the bomber and escort lists are empty, we are done */
267 if (QEMPTY(bomb_list) && QEMPTY(esc_list))
270 /* Something made it through */
271 /* Go figure out if there are ships in this sector, and who's they are */
272 memset(nats, 0, sizeof(nats));
273 snxtitem_xy(&ni, EF_SHIP, x, y);
274 while (nxtitem(&ni, &ship)) {
275 if (mchr[(int)ship.shp_type].m_flags & M_SUB)
277 nats[ship.shp_own]++;
279 /* Go figure out if there are units in this sector, and who's they are */
280 memset(lnats, 0, sizeof(lnats));
281 snxtitem_xy(&ni, EF_LAND, x, y);
282 while (nxtitem(&ni, &land)) {
283 if (land.lnd_ship >= 0 || land.lnd_land >= 0)
285 lnats[land.lnd_own]++;
288 /* Now, let's make life a little rougher. */
289 for (cn = 1; cn < MAXNOC && !QEMPTY(bomb_list); cn++) {
290 if (plane_owner == cn)
293 /* Are there ships owned by this country? */
297 PR(cn, "%s planes spotted over ships in %s\n",
298 cname(plane_owner), xyas(x, y, cn));
300 setcont(cn, plane_owner, FOUND_FLY);
302 if (unfriendly[cn]) {
303 /* They are unfriendly too */
305 getilist(&ilist[cn], cn);
308 PR(plane_owner, "Flying over %s ships in %s\n",
309 cname(cn), xyas(x, y, plane_owner));
310 /* This makes going for ships in harbors tough */
312 /* We already fired flak up above. Now we intercept again if we haven't already */
313 /* Flag that we intercepted */
315 /* And now intercept again */
316 ac_intercept(bomb_list, esc_list, &ilist[cn],
321 /* Are there units owned by this country? */
322 if (lnats[cn] != 0) {
325 PR(cn, "%s planes spotted over land units in %s\n",
326 cname(plane_owner), xyas(x, y, cn));
328 setcont(cn, plane_owner, FOUND_FLY);
330 if (unfriendly[cn]) {
331 /* They are unfriendly too */
333 getilist(&ilist[cn], cn);
336 PR(plane_owner, "Flying over %s land units in %s\n",
337 cname(cn), xyas(x, y, plane_owner));
340 /* We haven't intercepted yet, so intercept */
341 ac_intercept(bomb_list, esc_list, &ilist[cn],
349 if (mission_flags & P_A)
350 free_shiplist(&head);
351 for (cn = 1; cn < MAXNOC; cn++) {
358 count_non_missiles(struct emp_qelem *list)
360 struct emp_qelem *qp;
364 /* don't intercept missiles */
365 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
366 plp = (struct plist *)qp;
367 if (!(plp->pcp->pl_flags & P_M))
374 sam_intercept(struct emp_qelem *att_list, struct emp_qelem *def_list,
375 natid def_own, natid plane_owner, coord x, coord y,
378 struct emp_qelem *aqp;
379 struct emp_qelem *anext;
380 struct emp_qelem *dqp;
381 struct emp_qelem *dnext;
386 for (aqp = att_list->q_forw,
387 dqp = def_list->q_forw;
388 aqp != att_list && dqp != def_list; aqp = anext) {
390 aplp = (struct plist *)aqp;
391 if (aplp->pcp->pl_flags & P_M)
393 if (aplp->pcp->pl_cost < 1000)
395 for (; dqp != def_list; dqp = dnext) {
397 dplp = (struct plist *)dqp;
398 if (!(dplp->pcp->pl_flags & P_M))
401 if (dplp->plane.pln_range <
402 mapdist(x, y, dplp->plane.pln_x, dplp->plane.pln_y)) {
407 if (CANT_HAPPEN(dplp->plane.pln_flags & PLN_LAUNCHED)
408 || mission_pln_equip(dplp, 0, P_F, 0) < 0) {
413 dplp->plane.pln_flags |= PLN_LAUNCHED;
414 putplane(dplp->plane.pln_uid, &dplp->plane);
417 PR(plane_owner, "%s launches SAMs!\n", cname(def_own));
418 PR(def_own, "Launching SAMs at %s planes over %s!\n",
419 cname(plane_owner), xyas(x, y, def_own));
420 ac_combat_headers(plane_owner, def_own);
428 PR(plane_owner, "\n");
431 if (delete_missiles) {
432 for (; dqp != def_list; dqp = dnext) {
434 dplp = (struct plist *)dqp;
435 if (!(dplp->pcp->pl_flags & P_M))
445 ac_intercept(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
446 struct emp_qelem *def_list, natid def_own, coord x, coord y)
451 struct emp_qelem *next;
452 struct emp_qelem *qp;
453 struct emp_qelem int_list;
458 plp = (struct plist *)bomb_list->q_forw;
459 plane_owner = plp->plane.pln_own;
463 sam_intercept(bomb_list, def_list, def_own, plane_owner, x, y, 0);
464 sam_intercept(esc_list, def_list, def_own, plane_owner, x, y, 1);
465 if (!(att_count = count_non_missiles(bomb_list) +
466 count_non_missiles(esc_list)))
469 emp_initque(&int_list);
470 for (qp = def_list->q_forw; qp != def_list; qp = next) {
472 plp = (struct plist *)qp;
474 /* SAMs interdict separately */
475 if (plp->pcp->pl_flags & P_M)
477 dist = mapdist(x, y, pp->pln_x, pp->pln_y) * 2;
478 if (pp->pln_range < dist)
480 if (CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED)
481 || mission_pln_equip(plp, 0, P_F, 0) < 0) {
486 /* got one; delete from def_list, add to int_list */
488 emp_insque(qp, &int_list);
489 pp->pln_flags |= PLN_LAUNCHED;
490 pp->pln_mobil -= pln_mobcost(dist, pp, P_F);
491 putplane(pp->pln_uid, pp);
493 if (icount > att_count)
498 PR(plane_owner, "%d %s fighter%s rising to intercept!\n",
499 icount, cname(def_own), icount == 1 ? " is" : "s are");
500 PR(def_own, "%d fighter%s intercepting %s planes over %s!\n",
501 icount, icount == 1 ? " is" : "s are", cname(plane_owner),
502 xyas(x, y, def_own));
503 ac_combat_headers(plane_owner, def_own);
504 ac_airtoair(esc_list, &int_list);
505 ac_airtoair(bomb_list, &int_list);
506 PR(plane_owner, "\n");
512 ac_combat_headers(natid plane_owner, natid def_own)
515 " %-10.10s %-10.10s strength int odds damage results\n",
516 cname(plane_owner), cname(def_own));
518 " %-10.10s %-10.10s strength int odds damage results\n",
519 cname(def_own), cname(plane_owner));
526 ac_airtoair(struct emp_qelem *att_list, struct emp_qelem *int_list)
528 struct plist *attacker;
529 struct plist *interceptor;
530 struct emp_qelem *att;
531 struct emp_qelem *in;
535 struct emp_qelem *att_next;
536 struct emp_qelem *in_next;
538 att = att_list->q_forw;
539 in = int_list->q_forw;
542 if (QEMPTY(att_list) || QEMPTY(int_list)) {
546 while (more_att || more_int) {
547 in_next = in->q_forw;
548 att_next = att->q_forw;
549 attacker = (struct plist *)att;
551 /* skip missiles. If only missiles left, we're done */
552 if (plchr[(int)attacker->plane.pln_type].pl_flags & P_M) {
554 if (att == att_list) {
556 if (QEMPTY(att_list))
561 if (all_missiles(att_list))
565 interceptor = (struct plist *)in;
566 nplanes = attacker->plane.pln_effic;
567 if (nplanes > interceptor->plane.pln_effic)
568 nplanes = interceptor->plane.pln_effic;
569 ac_dog(attacker, interceptor);
572 if (att == att_list) {
574 if (QEMPTY(att_list))
579 if (in == int_list) {
581 if (QEMPTY(int_list))
590 all_missiles(struct emp_qelem *att_list)
592 struct emp_qelem *qp;
595 qp = att_list->q_forw;
596 while (qp != att_list) {
597 p = (struct plist *)qp;
598 if (!(plchr[(int)p->plane.pln_type].pl_flags & P_M))
607 ac_dog(struct plist *ap, struct plist *dp)
612 natid att_own, def_own;
617 att_own = ap->plane.pln_own;
618 def_own = dp->plane.pln_own;
620 PR(att_own, " %3.3s #%-4d %3.3s #%-4d",
622 ap->plane.pln_uid, dp->pcp->pl_name, dp->plane.pln_uid);
624 PR(def_own, " %3.3s #%-4d %3.3s #%-4d",
626 dp->plane.pln_uid, ap->pcp->pl_name, ap->plane.pln_uid);
627 att = pln_att(&ap->plane);
629 att = pln_def(&ap->plane);
630 att = att * ap->plane.pln_effic / 100;
631 att = MAX(att, ap->pcp->pl_def / 2);
633 def = pln_def(&dp->plane) * dp->plane.pln_effic / 100;
634 def = MAX(def, dp->pcp->pl_def / 2);
636 if ((ap->pcp->pl_flags & P_F) && ap->bombs != 0)
638 if ((dp->pcp->pl_flags & P_F) && dp->bombs != 0)
640 att += ap->pcp->pl_stealth / 25.0;
641 def += dp->pcp->pl_stealth / 25.0;
650 odds = ((double)att / ((double)def + (double)att));
653 intensity = roll(20) + roll(20) + roll(20) + roll(20) + 1;
655 PR(att_own, " %3d/%-3d %3d %3.2f ", att, def, intensity, odds);
656 PR(def_own, " %3d/%-3d %3d %3.2f ", def, att, intensity, odds);
660 while ((intensity--) > 0) {
664 if ((dp->plane.pln_effic - ddam) < PLANE_MINEFF)
668 if ((ap->plane.pln_effic - adam) < PLANE_MINEFF)
673 if (dp->pcp->pl_flags & P_M)
676 PR(att_own, "%3d/%-3d", adam, ddam);
677 PR(def_own, "%3d/%-3d", ddam, adam);
678 ac_planedamage(ap, def_own, adam, def_own, 1, 0, mesg);
679 strncpy(temp, mesg, 14);
680 ac_planedamage(dp, att_own, ddam, att_own, 1, 0, mesg);
681 PR(att_own, "%-13.13s %-13.13s\n", temp, mesg);
682 PR(def_own, "%-13.13s %-13.13s\n", mesg, temp);
685 setcont(att_own, def_own, FOUND_FLY);
686 setcont(def_own, att_own, FOUND_FLY);
691 * zap plane associated with plp.
692 * Damaging country is "from", damage is "dam".
693 * def_own is the country on the other side of the conflict from the plane
694 * owner. The only time def_own != from is when the interceptor is getting
697 * NOTE: This routine removes the appropriate plane element from the
698 * queue if it gets destroyed. That means that the caller must assume
699 * that the current queue pointer is invalid on return from the ac_planedamage
700 * call. (this has caused bugs in the past)
703 ac_planedamage(struct plist *plp, natid from, int dam, natid other,
704 int checkabort, int show, char *mesg)
714 plane_owner = pp->pln_own;
716 sprintf(dmess, " no damage");
721 memset(dmess, 0, sizeof(dmess));
725 if (eff < PLANE_MINEFF) {
726 sprintf(dmess, " shot down");
728 } else if (eff < 80 && chance((80 - eff) / 100.0) && checkabort) {
729 sprintf(dmess, " aborted @%2d%%", eff);
731 } else if (show == 0) {
732 sprintf(dmess, " cleared");
735 if ((plp->pcp->pl_flags & P_M) == 0) {
737 PR(plane_owner, " %s %s takes %d%s.\n",
738 cname(pp->pln_own), prplane(pp), dam, dmess);
740 PR(other, " %s %s takes %d%s.\n",
741 cname(pp->pln_own), prplane(pp), dam, dmess);
744 if (show && checkabort == 1) {
745 PR(plane_owner, "\n");
751 pp->pln_mobil -= MIN(32 + pp->pln_mobil, dam / 2);
753 if (disp == 1 && from != 0 && (plp->pcp->pl_flags & P_M) == 0)
754 nreport(from, N_DOWN_PLANE, pp->pln_own, 1);
757 putplane(pp->pln_uid, pp);
762 ac_doflak(struct emp_qelem *list, struct sctstr *from)
769 plp = (struct plist *)list->q_forw;
770 plane_owner = plp->plane.pln_own;
772 gun = MIN(FLAK_GUN_MAX, from->sct_item[I_GUN]);
773 shell = from->sct_item[I_SHELL];
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;
783 gun = roundavg(tfact(from->sct_own, 2.0 * gun));
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, gun);
795 ac_shipflak(struct emp_qelem *list, coord x, coord y)
800 double flak, total, ngun;
808 plp = (struct plist *)list->q_forw;
809 plane_owner = plp->plane.pln_own;
811 memset(nats, 0, sizeof(nats));
813 snxtitem_xy(&ni, EF_SHIP, x, y);
814 while (!QEMPTY(list) && nxtitem(&ni, &ship)) {
815 if (ship.shp_own == 0 || ship.shp_own == plane_owner)
817 mcp = &mchr[(int)ship.shp_type];
818 if (mcp->m_flags & M_SUB)
820 rel = getrel(getnatp(ship.shp_own), plane_owner);
824 gun = shp_usable_guns(&ship);
826 shell = ship.shp_item[I_SHELL];
828 shell = supply_commod(ship.shp_own, ship.shp_x, ship.shp_y,
830 ship.shp_item[I_SHELL] = shell;
831 putship(ship.shp_uid, &ship);
834 if (gun == 0 || shell == 0)
836 flak = gun * (ship.shp_effic / 100.0);
838 total += techfact(ship.shp_tech, flak * 2.0);
840 if (!nats[ship.shp_own]) {
841 /* First time here, print the message */
842 PR(ship.shp_own, "%s planes spotted over ships in %s\n",
843 cname(plane_owner), xyas(x, y, ship.shp_own));
844 PR(plane_owner, "Flying over %s ships in %s\n",
845 cname(ship.shp_own), xyas(x, y, plane_owner));
846 nats[ship.shp_own] = 1;
848 PR(ship.shp_own, "firing %.0f flak guns from %s...\n",
849 flak, prship(&ship));
853 /* Limit to FLAK_GUN_MAX guns of average tech factor */
854 if (ngun > FLAK_GUN_MAX)
855 total *= FLAK_GUN_MAX / ngun;
857 gun = roundavg(total);
859 PR(plane_owner, "Flak! Ships firing %d flak guns...\n", gun);
860 ac_fireflak(list, from, gun);
865 ac_landflak(struct emp_qelem *list, coord x, coord y)
870 double flak, total, ngun;
878 plp = (struct plist *)list->q_forw;
879 plane_owner = plp->plane.pln_own;
881 memset(nats, 0, sizeof(nats));
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)
887 lcp = &lchr[(int)land.lnd_type];
888 aaf = lnd_aaf(&land);
889 if ((lcp->l_flags & L_FLAK) == 0 || aaf == 0)
891 if (land.lnd_ship >= 0 || land.lnd_land >= 0)
893 rel = getrel(getnatp(land.lnd_own), plane_owner);
896 flak = aaf * 1.5 * land.lnd_effic / 100.0;
898 total += techfact(land.lnd_tech, flak * 2.0);
900 if (!nats[land.lnd_own]) {
901 /* First time here, print the message */
902 PR(land.lnd_own, "%s planes spotted over land units in %s\n",
903 cname(plane_owner), xyas(x, y, land.lnd_own));
904 PR(plane_owner, "Flying over %s land units in %s\n",
905 cname(land.lnd_own), xyas(x, y, plane_owner));
906 nats[land.lnd_own] = 1;
908 PR(land.lnd_own, "firing flak guns from unit %s (aa rating %d)\n",
913 /* Limit to FLAK_GUN_MAX guns of average tech factor */
914 if (ngun > FLAK_GUN_MAX)
915 total *= FLAK_GUN_MAX / ngun;
917 gun = roundavg(total);
919 PR(plane_owner, "Flak! Land units firing %d flak guns...\n", gun);
920 ac_fireflak(list, from, gun);
925 * Called from shipflak, landflak, and doflak.
928 ac_fireflak(struct emp_qelem *list, natid from, int guns)
932 struct emp_qelem *qp;
933 struct emp_qelem *next;
936 plp = (struct plist *)list->q_forw;
938 for (qp = list->q_forw; qp != list; qp = next) {
940 plp = (struct plist *)qp;
941 n = ac_flak_dam(guns, pln_def(&plp->plane), plp->pcp->pl_flags);
942 ac_planedamage(plp, from, n, 0, 2, 1, msg);
947 * Calculate flak damage
950 ac_flak_dam(int guns, int def, int pl_flags)
954 /* <-7 -7 -6 -5 -4 */
955 static float flaktable[18] = { 0.132f, 0.20f, 0.20f, 0.25f, 0.30f,
956 /* -3 -2 -1 0 +1 +2 +3 +4 */
957 0.35f, 0.40f, 0.45f, 0.50f, 0.50f, 0.55f, 0.60f, 0.65f,
958 /* +5 +6 +7 +8 >+8 */
959 0.70f,0.75f, 0.80f, 0.85f, 1.1305f };
960 enum { FLAK_MAX = sizeof(flaktable)/sizeof(flaktable[0]) - 1 };
963 if ((pl_flags & P_T) == 0)
971 mult = flaktable[FLAK_MAX];
976 mult = flaktable[flak];
979 dam = (int)((roll(8) + 2) * mult);
986 * Get a list of planes available for interception duties.
989 getilist(struct emp_qelem *list, natid own)
991 struct plchrstr *pcp;
997 snxtitem_all(&ni, EF_PLANE);
998 while (nxtitem(&ni, &plane)) {
999 if (plane.pln_own != own)
1001 pcp = &plchr[(int)plane.pln_type];
1002 if ((pcp->pl_flags & P_F) == 0)
1004 if (plane.pln_flags & PLN_LAUNCHED)
1006 if (plane.pln_mission != 0)
1008 if (plane.pln_mobil <= 0)
1010 if (plane.pln_effic < 40)
1012 if (!pln_airbase_ok(&plane, 0, 0))
1015 ip = malloc(sizeof(*ip));
1018 ip->pcp = &plchr[(int)plane.pln_type];
1020 emp_insque(&ip->queue, list);
1025 do_evade(struct emp_qelem *bomb_list, struct emp_qelem *esc_list)
1027 struct emp_qelem *qp;
1032 for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw) {
1033 plp = (struct plist *)qp;
1034 if (evade > plp->pcp->pl_stealth / 100.0)
1035 evade = plp->pcp->pl_stealth / 100.0;
1037 for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw) {
1038 plp = (struct plist *)qp;
1039 if (evade > plp->pcp->pl_stealth / 100.0)
1040 evade = plp->pcp->pl_stealth / 100.0;