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
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 struct emp_qelem *, struct emp_qelem *,
69 struct emp_qelem *, struct emp_qelem *);
70 static int do_evade(struct emp_qelem *, struct emp_qelem *);
73 ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
74 coord x, coord y, char *path, int mission_flags,
75 int no_air_defense, struct emp_qelem *obomb,
76 struct emp_qelem *oesc)
78 int val, non_missiles;
84 int unfriendly[MAXNOC];
86 struct emp_qelem ilist[MAXNOC], *qp;
96 struct natstr *over, *mynatp;
99 struct shiplist *head = NULL;
101 int intown = 0; /* Last owner to intercept */
102 /* We want to only intercept once per sector per owner. So, if we overfly
103 a sector, and then overfly some land units or ships, we don't want to
104 potentially intercept 3 times. */
106 plp = (struct plist *)bomb_list->q_forw;
107 plane_owner = plp->plane.pln_own;
109 strncpy(mypath, path, sizeof(mypath));
112 memset(overfly, 0, sizeof(overfly));
113 memset(gotilist, 0, sizeof(gotilist));
114 memset(unfriendly, 0, sizeof(unfriendly));
115 for (cn = 1; cn < MAXNOC; cn++) {
116 if ((mynatp = getnatp(cn)) == 0)
118 rel = getrel(mynatp, plane_owner);
121 if (plane_owner == cn)
125 if (mission_flags & PM_R) {
126 if (mission_flags & P_S) {
127 PR(plane_owner, "\nSPY Plane report\n");
130 } else if (mission_flags & P_A) {
131 PR(plane_owner, "\nAnti-Sub Patrol report\n");
133 PR(plane_owner, "\nReconnaissance report\n");
138 pln_removedupes(bomb_list, esc_list);
139 while ((dir = mypath[myp++]) && !QEMPTY(bomb_list)) {
140 if ((val = diridx(dir)) == DIR_STOP)
142 /* XXX using xnorm is probably bad */
143 x = xnorm(x + diroff[val][0]);
144 y = ynorm(y + diroff[val][1]);
145 getsect(x, y, §);
146 over = getnatp(sect.sct_own);
148 if (mission_flags & PM_R) {
150 setcont(plane_owner, sect.sct_own, FOUND_FLY);
151 if (sect.sct_type == SCT_WATER) {
152 PR(plane_owner, "flying over %s at %s\n",
153 dchr[sect.sct_type].d_name, xyas(x, y, plane_owner));
154 if (mission_flags & PM_S)
155 plane_sweep(bomb_list, x, y);
156 if (mission_flags & P_A) {
157 plane_sona(bomb_list, x, y, &head);
159 changed += map_set(plane_owner,
160 sect.sct_x, sect.sct_y,
161 dchr[sect.sct_type].d_mnem, 0);
162 } else if (mission_flags & P_S) {
163 satdisp_sect(§, (mission_flags & P_I) ? 10 : 50);
165 /* This is borrowed from lookout */
166 if (sect.sct_own == plane_owner)
167 PR(plane_owner, "Your ");
169 PR(plane_owner, "%s (#%d) ",
170 cname(sect.sct_own), sect.sct_own);
171 PR(plane_owner, "%s", dchr[sect.sct_type].d_name);
172 changed += map_set(plane_owner,
173 sect.sct_x, sect.sct_y,
174 dchr[sect.sct_type].d_mnem, 0);
175 PR(plane_owner, " %d%% efficient ",
176 (sect.sct_own == plane_owner) ?
177 sect.sct_effic : roundintby((int)sect.sct_effic, 25));
178 civ = sect.sct_item[I_CIVIL];
179 mil = sect.sct_item[I_MILIT];
181 PR(plane_owner, "with %s%d civ ",
182 (sect.sct_own == plane_owner) ?
184 (sect.sct_own == plane_owner) ?
185 civ : roundintby(civ, 25));
187 PR(plane_owner, "with %s%d mil ",
188 (sect.sct_own == plane_owner) ?
190 (sect.sct_own == plane_owner) ?
191 mil : roundintby(mil, 25));
192 PR(plane_owner, "@ %s\n", xyas(x, y, plane_owner));
194 if (mission_flags & P_S)
195 satdisp_units(sect.sct_x, sect.sct_y);
197 PR(plane_owner, "flying over %s at %s\n",
198 dchr[sect.sct_type].d_name, xyas(x, y, plane_owner));
199 changed += map_set(plane_owner, sect.sct_x, sect.sct_y,
200 dchr[sect.sct_type].d_mnem, 0);
202 if ((rel = getrel(over, plane_owner)) == ALLIED)
205 evaded = do_evade(bomb_list, esc_list);
207 if (sect.sct_own != 0 && sect.sct_own != plane_owner && !evaded) {
208 /* We only show planes overhead if they didn't
210 overfly[sect.sct_own]++;
211 PR(sect.sct_own, "%s planes spotted over %s\n",
212 cname(plane_owner), xyas(x, y, sect.sct_own));
214 setcont(cn, plane_owner, FOUND_FLY);
219 if (unfriendly[sect.sct_own])
220 ac_doflak(bomb_list, §);
221 /* If bombers left, fire flak from units and ships */
222 if (!QEMPTY(bomb_list))
223 ac_landflak(bomb_list, x, y);
224 if (!QEMPTY(bomb_list))
225 ac_shipflak(bomb_list, x, y);
227 /* mission planes aborted due to flak -- don't send escorts */
228 if (QEMPTY(bomb_list))
230 if (!no_air_defense && !evaded)
231 air_defense(x, y, plane_owner, bomb_list, esc_list);
233 if (sect.sct_own == 0 || sect.sct_own == plane_owner)
240 for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw) {
241 struct plist *ip = (struct plist *)qp;
242 if (!(plchr[(int)ip->plane.pln_type].pl_flags & P_M))
249 if (unfriendly[sect.sct_own] && !gotilist[sect.sct_own]) {
250 getilist(&ilist[sect.sct_own], sect.sct_own,
251 bomb_list, esc_list, obomb, oesc);
252 gotilist[sect.sct_own]++;
256 ac_intercept(bomb_list, esc_list, &ilist[sect.sct_own],
258 intown = sect.sct_own;
261 /* Let's report all of the overflights even if aborted */
262 for (cn = 1; cn < MAXNOC; cn++) {
263 if (plane_owner == cn)
266 nreport(plane_owner, N_OVFLY_SECT, cn, overfly[cn]);
268 /* If the map changed, update it */
270 writemap(player->cnum);
271 /* Now, if the bomber and escort lists are empty, we are done */
272 if (QEMPTY(bomb_list) && QEMPTY(esc_list)) {
273 if (mission_flags & P_A)
274 free_shiplist(&head);
278 /* Something made it through */
279 /* Go figure out if there are ships in this sector, and who's they are */
280 memset(nats, 0, sizeof(nats));
281 snxtitem_xy(&ni, EF_SHIP, x, y);
282 while (nxtitem(&ni, &ship)) {
283 if (mchr[(int)ship.shp_type].m_flags & M_SUB)
285 nats[ship.shp_own]++;
287 /* Go figure out if there are units in this sector, and who's they are */
288 memset(lnats, 0, sizeof(lnats));
289 snxtitem_xy(&ni, EF_LAND, x, y);
290 while (nxtitem(&ni, &land)) {
291 if (land.lnd_ship >= 0 || land.lnd_land >= 0)
293 lnats[land.lnd_own]++;
296 /* Now, let's make life a little rougher. */
297 for (cn = 1; cn < MAXNOC && !QEMPTY(bomb_list); cn++) {
298 if (plane_owner == cn)
301 /* Are there ships owned by this country? */
305 PR(cn, "%s planes spotted over ships in %s\n",
306 cname(plane_owner), xyas(x, y, cn));
308 setcont(cn, plane_owner, FOUND_FLY);
310 if (unfriendly[cn]) {
311 /* They are unfriendly too */
313 getilist(&ilist[cn], cn, bomb_list, esc_list, obomb,
317 PR(plane_owner, "Flying over %s ships in %s\n",
318 cname(cn), xyas(x, y, plane_owner));
319 /* This makes going for ships in harbors tough */
321 /* We already fired flak up above. Now we intercept again if we haven't already */
322 /* Flag that we intercepted */
324 /* And now intercept again */
325 ac_intercept(bomb_list, esc_list, &ilist[cn],
330 /* Are there units owned by this country? */
331 if (lnats[cn] != 0) {
334 PR(cn, "%s planes spotted over land units in %s\n",
335 cname(plane_owner), xyas(x, y, cn));
337 setcont(cn, plane_owner, FOUND_FLY);
339 if (unfriendly[cn]) {
340 /* They are unfriendly too */
342 getilist(&ilist[cn], cn, bomb_list, esc_list, obomb,
346 PR(plane_owner, "Flying over %s land units in %s\n",
347 cname(cn), xyas(x, y, plane_owner));
350 /* We haven't intercepted yet, so intercept */
351 ac_intercept(bomb_list, esc_list, &ilist[cn],
358 if (mission_flags & P_A)
359 free_shiplist(&head);
363 count_non_missiles(struct emp_qelem *list)
365 struct emp_qelem *qp;
369 /* don't intercept missiles */
370 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
371 plp = (struct plist *)qp;
372 if (!(plp->pcp->pl_flags & P_M))
379 sam_intercept(struct emp_qelem *att_list, struct emp_qelem *def_list,
380 natid def_own, natid plane_owner, coord x, coord y,
383 struct emp_qelem *aqp;
384 struct emp_qelem *anext;
385 struct emp_qelem *dqp;
386 struct emp_qelem *dnext;
391 for (aqp = att_list->q_forw,
392 dqp = def_list->q_forw;
393 aqp != att_list && dqp != def_list; aqp = anext) {
395 aplp = (struct plist *)aqp;
396 if (aplp->pcp->pl_flags & P_M)
398 if (aplp->pcp->pl_cost < 1000)
400 for (; dqp != def_list; dqp = dnext) {
402 dplp = (struct plist *)dqp;
403 if (!(dplp->pcp->pl_flags & P_M))
406 if (dplp->plane.pln_range <
407 mapdist(x, y, dplp->plane.pln_x, dplp->plane.pln_y)) {
412 if (mission_pln_equip(dplp, 0, P_F, 0) < 0) {
419 PR(plane_owner, "%s launches SAMs!\n", cname(def_own));
420 PR(def_own, "Launching SAMs at %s planes!\n",
422 ac_combat_headers(plane_owner, def_own);
430 PR(plane_owner, "\n");
433 if (delete_missiles) {
434 for (; dqp != def_list; dqp = dnext) {
436 dplp = (struct plist *)dqp;
437 if (!(dplp->pcp->pl_flags & P_M))
447 ac_intercept(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
448 struct emp_qelem *def_list, natid def_own, coord x, coord y)
453 struct emp_qelem *next;
454 struct emp_qelem *qp;
455 struct emp_qelem int_list;
460 plp = (struct plist *)bomb_list->q_forw;
461 plane_owner = plp->plane.pln_own;
465 sam_intercept(bomb_list, def_list, def_own, plane_owner, x, y, 0);
466 sam_intercept(esc_list, def_list, def_own, plane_owner, x, y, 1);
467 if (!(att_count = count_non_missiles(bomb_list) +
468 count_non_missiles(esc_list)))
471 emp_initque(&int_list);
472 for (qp = def_list->q_forw; qp != def_list; qp = next) {
474 plp = (struct plist *)qp;
476 /* SAMs interdict separately */
477 if (plp->pcp->pl_flags & P_M)
479 dist = mapdist(x, y, pp->pln_x, pp->pln_y) * 2;
480 if (pp->pln_range < dist)
482 if (mission_pln_equip(plp, 0, P_F, 0) < 0) {
487 /* got one; delete from def_list, add to int_list */
489 emp_insque(qp, &int_list);
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!\n",
501 icount, icount == 1 ? " is" : "s are", cname(plane_owner));
502 ac_combat_headers(plane_owner, def_own);
503 ac_airtoair(esc_list, &int_list);
504 ac_airtoair(bomb_list, &int_list);
505 PR(plane_owner, "\n");
510 ac_combat_headers(natid plane_owner, natid def_own)
513 " %-10.10s %-10.10s strength int odds damage results\n",
514 cname(plane_owner), cname(def_own));
516 " %-10.10s %-10.10s strength int odds damage results\n",
517 cname(def_own), cname(plane_owner));
524 ac_airtoair(struct emp_qelem *att_list, struct emp_qelem *int_list)
526 struct plist *attacker;
527 struct plist *interceptor;
528 struct emp_qelem *att;
529 struct emp_qelem *in;
533 struct emp_qelem *att_next;
534 struct emp_qelem *in_next;
536 att = att_list->q_forw;
537 in = int_list->q_forw;
540 if (QEMPTY(att_list) || QEMPTY(int_list)) {
544 while (more_att || more_int) {
545 in_next = in->q_forw;
546 att_next = att->q_forw;
547 attacker = (struct plist *)att;
549 /* skip missiles. If only missiles left, we're done */
550 if (plchr[(int)attacker->plane.pln_type].pl_flags & P_M) {
552 if (att == att_list) {
554 if (QEMPTY(att_list))
559 if (all_missiles(att_list))
563 interceptor = (struct plist *)in;
564 nplanes = attacker->plane.pln_effic;
565 if (nplanes > interceptor->plane.pln_effic)
566 nplanes = interceptor->plane.pln_effic;
567 ac_dog(attacker, interceptor);
570 if (att == att_list) {
572 if (QEMPTY(att_list))
577 if (in == int_list) {
579 if (QEMPTY(int_list))
588 all_missiles(struct emp_qelem *att_list)
590 struct emp_qelem *qp;
593 qp = att_list->q_forw;
594 while (qp != att_list) {
595 p = (struct plist *)qp;
596 if (!(plchr[(int)p->plane.pln_type].pl_flags & P_M))
605 ac_dog(struct plist *ap, struct plist *dp)
610 natid att_own, def_own;
615 att_own = ap->plane.pln_own;
616 def_own = dp->plane.pln_own;
618 PR(att_own, " %3.3s #%-4d %3.3s #%-4d",
620 ap->plane.pln_uid, dp->pcp->pl_name, dp->plane.pln_uid);
622 PR(def_own, " %3.3s #%-4d %3.3s #%-4d",
624 dp->plane.pln_uid, ap->pcp->pl_name, ap->plane.pln_uid);
625 att = pln_att(&ap->plane);
627 att = pln_def(&ap->plane);
628 att = att * ap->plane.pln_effic / 100;
629 att = MAX(att, ap->pcp->pl_def / 2);
631 def = pln_def(&dp->plane) * dp->plane.pln_effic / 100;
632 def = MAX(def, dp->pcp->pl_def / 2);
634 if ((ap->pcp->pl_flags & P_F) && ap->bombs != 0)
636 if ((dp->pcp->pl_flags & P_F) && dp->bombs != 0)
638 att += ap->pcp->pl_stealth / 25.0;
639 def += dp->pcp->pl_stealth / 25.0;
648 odds = ((double)att / ((double)def + (double)att));
651 intensity = roll(20) + roll(20) + roll(20) + roll(20) + 1;
653 PR(att_own, " %3d/%-3d %3d %3.2f ", att, def, intensity, odds);
654 PR(def_own, " %3d/%-3d %3d %3.2f ", def, att, intensity, odds);
658 while ((intensity--) > 0) {
662 if ((dp->plane.pln_effic - ddam) < PLANE_MINEFF)
666 if ((ap->plane.pln_effic - adam) < PLANE_MINEFF)
671 if (dp->pcp->pl_flags & P_M)
674 PR(att_own, "%3d/%-3d", adam, ddam);
675 PR(def_own, "%3d/%-3d", ddam, adam);
676 ac_planedamage(ap, def_own, adam, def_own, 1, 0, mesg);
677 strncpy(temp, mesg, 14);
678 ac_planedamage(dp, att_own, ddam, att_own, 1, 0, mesg);
679 PR(att_own, "%-13.13s %-13.13s\n", temp, mesg);
680 PR(def_own, "%-13.13s %-13.13s\n", mesg, temp);
683 setcont(att_own, def_own, FOUND_FLY);
684 setcont(def_own, att_own, FOUND_FLY);
689 * zap plane associated with plp.
690 * Damaging country is "from", damage is "dam".
691 * def_own is the country on the other side of the conflict from the plane
692 * owner. The only time def_own != from is when the interceptor is getting
695 * NOTE: This routine removes the appropriate plane element from the
696 * queue if it gets destroyed. That means that the caller must assume
697 * that the current queue pointer is invalid on return from the ac_planedamage
698 * call. (this has caused bugs in the past)
701 ac_planedamage(struct plist *plp, natid from, int dam, natid other,
702 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 (from != 0 && (plp->pcp->pl_flags & P_M) == 0)
754 nreport(from, N_DOWN_PLANE, pp->pln_own, 1);
755 if (pp->pln_ship >= 0) {
756 getship(pp->pln_ship, &ship);
757 take_plane_off_ship(pp, &ship);
759 if (pp->pln_land >= 0) {
760 getland(pp->pln_land, &land);
761 take_plane_off_land(pp, &land);
764 putplane(pp->pln_uid, pp);
765 emp_remque(&plp->queue);
767 } else if (disp == 2) {
768 putplane(pp->pln_uid, pp);
769 emp_remque(&plp->queue);
772 putplane(pp->pln_uid, pp);
777 ac_doflak(struct emp_qelem *list, struct sctstr *from)
784 plp = (struct plist *)list->q_forw;
785 plane_owner = plp->plane.pln_own;
787 gun = MIN(FLAK_GUN_MAX, from->sct_item[I_GUN]);
788 shell = from->sct_item[I_SHELL];
789 if (gun > shell * 2) {
790 shell += supply_commod(from->sct_own, from->sct_x, from->sct_y,
791 I_SHELL, (gun + 1) / 2 - shell);
792 from->sct_item[I_SHELL] = shell;
798 gun = roundavg(tfact(from->sct_own, 2.0 * gun));
800 PR(plane_owner, "firing %d flak guns in %s...\n",
801 gun, xyas(from->sct_x, from->sct_y, plane_owner));
802 if (from->sct_own != 0)
803 PR(from->sct_own, "firing %d flak guns in %s...\n",
804 gun, xyas(from->sct_x, from->sct_y, from->sct_own));
805 ac_fireflak(list, from->sct_own, gun);
810 ac_shipflak(struct emp_qelem *list, coord x, coord y)
815 double flak, total, ngun;
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)
832 mcp = &mchr[(int)ship.shp_type];
833 if (mcp->m_flags & M_SUB)
835 rel = getrel(getnatp(ship.shp_own), plane_owner);
839 gun = shp_usable_guns(&ship);
841 shell = ship.shp_item[I_SHELL];
843 shell = supply_commod(ship.shp_own, ship.shp_x, ship.shp_y,
845 ship.shp_item[I_SHELL] = shell;
846 putship(ship.shp_uid, &ship);
849 if (gun == 0 || shell == 0)
851 flak = gun * (ship.shp_effic / 100.0);
853 total += techfact(ship.shp_tech, flak * 2.0);
855 if (!nats[ship.shp_own]) {
856 /* First time here, print the message */
857 PR(ship.shp_own, "%s planes spotted over ships in %s\n",
858 cname(plane_owner), xyas(x, y, ship.shp_own));
859 PR(plane_owner, "Flying over %s ships in %s\n",
860 cname(ship.shp_own), xyas(x, y, plane_owner));
861 nats[ship.shp_own] = 1;
863 PR(ship.shp_own, "firing %.0f flak guns from %s...\n",
864 flak, prship(&ship));
868 /* Limit to FLAK_GUN_MAX guns of average tech factor */
869 if (ngun > FLAK_GUN_MAX)
870 total *= FLAK_GUN_MAX / ngun;
872 gun = roundavg(total);
874 PR(plane_owner, "Flak! Ships firing %d flak guns...\n", gun);
875 ac_fireflak(list, from, gun);
880 ac_landflak(struct emp_qelem *list, coord x, coord y)
885 double flak, total, ngun;
893 plp = (struct plist *)list->q_forw;
894 plane_owner = plp->plane.pln_own;
896 memset(nats, 0, sizeof(nats));
898 snxtitem_xy(&ni, EF_LAND, x, y);
899 while (!QEMPTY(list) && nxtitem(&ni, &land)) {
900 if (land.lnd_own == 0 || land.lnd_own == plane_owner)
902 lcp = &lchr[(int)land.lnd_type];
903 if ((lcp->l_flags & L_FLAK) == 0 || land.lnd_aaf == 0)
905 if (land.lnd_ship >= 0 || land.lnd_land >= 0)
907 rel = getrel(getnatp(land.lnd_own), plane_owner);
910 flak = land.lnd_aaf * 1.5 * land.lnd_effic / 100.0;
912 total += techfact(land.lnd_tech, flak * 2.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);
927 /* Limit to FLAK_GUN_MAX guns of average tech factor */
928 if (ngun > FLAK_GUN_MAX)
929 total *= FLAK_GUN_MAX / ngun;
931 gun = roundavg(total);
933 PR(plane_owner, "Flak! Land units firing %d flak guns...\n", gun);
934 ac_fireflak(list, from, gun);
939 * Called from shipflak, landflak, and doflak.
942 ac_fireflak(struct emp_qelem *list, natid from, int guns)
946 struct emp_qelem *qp;
947 struct emp_qelem *next;
950 plp = (struct plist *)list->q_forw;
952 for (qp = list->q_forw; qp != list; qp = next) {
954 plp = (struct plist *)qp;
955 n = ac_flak_dam(guns, pln_def(&plp->plane), plp->pcp->pl_flags);
956 ac_planedamage(plp, from, n, 0, 2, 1, msg);
961 * Calculate flak damage
964 ac_flak_dam(int guns, int def, int pl_flags)
968 /* <-7 -7 -6 -5 -4 */
969 static float flaktable[18] = { 0.132f, 0.20f, 0.20f, 0.25f, 0.30f,
970 /* -3 -2 -1 0 +1 +2 +3 +4 */
971 0.35f, 0.40f, 0.45f, 0.50f, 0.50f, 0.55f, 0.60f, 0.65f,
972 /* +5 +6 +7 +8 >+8 */
973 0.70f,0.75f, 0.80f, 0.85f, 1.1305f };
974 enum { FLAK_MAX = sizeof(flaktable)/sizeof(flaktable[0]) - 1 };
977 if ((pl_flags & P_T) == 0)
985 mult = flaktable[FLAK_MAX];
990 mult = flaktable[flak];
993 dam = (int)((roll(8) + 2) * mult);
1000 * See if this plane is flying in this list
1003 ac_isflying(struct plnstr *plane, struct emp_qelem *list)
1005 struct emp_qelem *qp;
1006 struct emp_qelem *next;
1012 for (qp = list->q_forw; qp != list; qp = next) {
1014 plp = (struct plist *)qp;
1016 if (plane->pln_uid == pp->pln_uid)
1024 * Get a list of planes available for interception duties.
1027 getilist(struct emp_qelem *list, natid own, struct emp_qelem *a,
1028 struct emp_qelem *b, struct emp_qelem *c, struct emp_qelem *d)
1030 struct plchrstr *pcp;
1031 struct plnstr plane;
1032 struct nstr_item ni;
1036 snxtitem_all(&ni, EF_PLANE);
1037 while (nxtitem(&ni, &plane)) {
1038 if (plane.pln_own != own)
1040 pcp = &plchr[(int)plane.pln_type];
1041 if ((pcp->pl_flags & P_F) == 0)
1043 if (plane.pln_mission != 0)
1045 if (plane.pln_mobil <= 0)
1047 if (plane.pln_effic < 40)
1049 if (!pln_airbase_ok(&plane, 0, 0))
1051 /* Finally, is it in the list of planes already in
1053 if (ac_isflying(&plane, a))
1055 if (ac_isflying(&plane, b))
1057 if (ac_isflying(&plane, c))
1059 if (ac_isflying(&plane, d))
1062 ip = malloc(sizeof(*ip));
1065 ip->pcp = &plchr[(int)plane.pln_type];
1067 emp_insque(&ip->queue, list);
1072 do_evade(struct emp_qelem *bomb_list, struct emp_qelem *esc_list)
1074 struct emp_qelem *qp;
1079 for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw) {
1080 plp = (struct plist *)qp;
1081 if (evade > plp->pcp->pl_stealth / 100.0)
1082 evade = plp->pcp->pl_stealth / 100.0;
1084 for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw) {
1085 plp = (struct plist *)qp;
1086 if (evade > plp->pcp->pl_stealth / 100.0)
1087 evade = plp->pcp->pl_stealth / 100.0;