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 ac_intercept(struct emp_qelem *, struct emp_qelem *,
57 struct emp_qelem *, natid, coord, coord);
58 static int all_missiles(struct emp_qelem *);
59 static void ac_dog(struct plist *, struct plist *);
60 static void ac_planedamage(struct plist *, natid, int, natid, int,
62 static void ac_doflak(struct emp_qelem *, struct sctstr *);
63 static void ac_landflak(struct emp_qelem *, coord, coord);
64 static void ac_shipflak(struct emp_qelem *, coord, coord);
65 static void ac_fireflak(struct emp_qelem *, natid, natid, int);
66 static void getilist(struct emp_qelem *, natid,
67 struct emp_qelem *, struct emp_qelem *,
68 struct emp_qelem *, struct emp_qelem *);
69 static int can_fly(int);
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(§, (mission_flags & P_I) ? 10 : 50, 1);
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));
195 PR(plane_owner, "flying over %s at %s\n",
196 dchr[sect.sct_type].d_name, xyas(x, y, plane_owner));
197 changed += map_set(plane_owner, sect.sct_x, sect.sct_y,
198 dchr[sect.sct_type].d_mnem, 0);
200 if ((rel = getrel(over, plane_owner)) == ALLIED)
203 evaded = do_evade(bomb_list, esc_list);
205 if (sect.sct_own != 0 && sect.sct_own != plane_owner && !evaded) {
206 /* We only show planes overhead if they didn't
208 overfly[sect.sct_own]++;
209 PR(sect.sct_own, "%s planes spotted over %s\n",
210 cname(plane_owner), xyas(x, y, sect.sct_own));
212 setcont(cn, plane_owner, FOUND_FLY);
217 if (unfriendly[sect.sct_own])
218 ac_doflak(bomb_list, §);
219 /* If bombers left, fire flak from units and ships */
220 if (!QEMPTY(bomb_list))
221 ac_landflak(bomb_list, x, y);
222 if (!QEMPTY(bomb_list))
223 ac_shipflak(bomb_list, x, y);
225 /* mission planes aborted due to flak -- don't send escorts */
226 if (QEMPTY(bomb_list))
228 if (!no_air_defense && !evaded)
229 air_defense(x, y, plane_owner, bomb_list, esc_list);
231 if (sect.sct_own == 0 || sect.sct_own == plane_owner)
238 for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw) {
239 struct plist *ip = (struct plist *)qp;
240 if (!(plchr[(int)ip->plane.pln_type].pl_flags & P_M))
247 if (unfriendly[sect.sct_own] && !gotilist[sect.sct_own]) {
248 getilist(&ilist[sect.sct_own], sect.sct_own,
249 bomb_list, esc_list, obomb, oesc);
250 gotilist[sect.sct_own]++;
254 ac_intercept(bomb_list, esc_list, &ilist[sect.sct_own],
256 intown = sect.sct_own;
259 /* Let's report all of the overflights even if aborted */
260 for (cn = 1; cn < MAXNOC; cn++) {
261 if (plane_owner == cn)
264 nreport(plane_owner, N_OVFLY_SECT, cn, overfly[cn]);
266 /* If the map changed, update it */
268 if (!update_pending && plane_owner == player->cnum)
269 writemap(player->cnum);
270 /* Now, if the bomber and escort lists are empty, we are done */
271 if (QEMPTY(bomb_list) && QEMPTY(esc_list)) {
272 if (mission_flags & P_A)
273 free_shiplist(&head);
277 /* Something made it through */
278 /* Go figure out if there are ships in this sector, and who's they are */
279 memset(nats, 0, sizeof(nats));
280 snxtitem_xy(&ni, EF_SHIP, x, y);
281 while (nxtitem(&ni, &ship)) {
282 if (mchr[(int)ship.shp_type].m_flags & M_SUB)
284 nats[ship.shp_own]++;
286 /* Go figure out if there are units in this sector, and who's they are */
287 memset(lnats, 0, sizeof(lnats));
288 snxtitem_xy(&ni, EF_LAND, x, y);
289 while (nxtitem(&ni, &land)) {
290 if (land.lnd_ship >= 0 || land.lnd_land >= 0)
292 lnats[land.lnd_own]++;
295 /* Now, let's make life a little rougher. */
296 for (cn = 1; cn < MAXNOC && !QEMPTY(bomb_list); cn++) {
297 if (plane_owner == cn)
300 /* Are there ships owned by this country? */
304 PR(cn, "%s planes spotted over ships in %s\n",
305 cname(plane_owner), xyas(x, y, cn));
307 setcont(cn, plane_owner, FOUND_FLY);
309 if (unfriendly[cn]) {
310 /* They are unfriendly too */
312 getilist(&ilist[cn], cn, bomb_list, esc_list, obomb,
316 PR(plane_owner, "Flying over %s ships in %s\n",
317 cname(cn), xyas(x, y, plane_owner));
318 /* This makes going for ships in harbors tough */
320 /* We already fired flak up above. Now we intercept again if we haven't already */
321 /* Flag that we intercepted */
323 /* And now intercept again */
324 ac_intercept(bomb_list, esc_list, &ilist[cn],
329 /* Are there units owned by this country? */
330 if (lnats[cn] != 0) {
333 PR(cn, "%s planes spotted over land units in %s\n",
334 cname(plane_owner), xyas(x, y, cn));
336 setcont(cn, plane_owner, FOUND_FLY);
338 if (unfriendly[cn]) {
339 /* They are unfriendly too */
341 getilist(&ilist[cn], cn, bomb_list, esc_list, obomb,
345 PR(plane_owner, "Flying over %s land units in %s\n",
346 cname(cn), xyas(x, y, plane_owner));
349 /* We haven't intercepted yet, so intercept */
350 ac_intercept(bomb_list, esc_list, &ilist[cn],
357 if (mission_flags & P_A)
358 free_shiplist(&head);
362 count_non_missiles(struct emp_qelem *list)
364 struct emp_qelem *qp;
368 /* don't intercept missiles */
369 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
370 plp = (struct plist *)qp;
371 if (!(plp->pcp->pl_flags & P_M))
378 sam_intercept(struct emp_qelem *att_list, struct emp_qelem *def_list,
379 natid def_own, natid plane_owner, coord x, coord y,
382 struct emp_qelem *aqp;
383 struct emp_qelem *anext;
384 struct emp_qelem *dqp;
385 struct emp_qelem *dnext;
390 for (aqp = att_list->q_forw,
391 dqp = def_list->q_forw;
392 aqp != att_list && dqp != def_list; aqp = anext) {
394 aplp = (struct plist *)aqp;
395 if (aplp->pcp->pl_flags & P_M)
397 if (aplp->pcp->pl_cost < 1000)
399 for (; dqp != def_list; dqp = dnext) {
401 dplp = (struct plist *)dqp;
402 if (!(dplp->pcp->pl_flags & P_M))
405 if (dplp->plane.pln_range <
406 mapdist(x, y, dplp->plane.pln_x, dplp->plane.pln_y)) {
411 if (mission_pln_equip(dplp, 0, P_F, 0) < 0) {
418 PR(plane_owner, "%s launches SAMs!\n", cname(def_own));
419 PR(def_own, "Launching SAMs at %s planes!\n",
421 ac_combat_headers(plane_owner, def_own);
429 PR(plane_owner, "\n");
432 if (delete_missiles) {
433 for (; dqp != def_list; dqp = dnext) {
435 dplp = (struct plist *)dqp;
436 if (!(dplp->pcp->pl_flags & P_M))
446 ac_intercept(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
447 struct emp_qelem *def_list, natid def_own, coord x, coord y)
452 struct emp_qelem *next;
453 struct emp_qelem *qp;
454 struct emp_qelem int_list;
459 plp = (struct plist *)bomb_list->q_forw;
460 plane_owner = plp->plane.pln_own;
464 sam_intercept(bomb_list, def_list, def_own, plane_owner, x, y, 0);
465 sam_intercept(esc_list, def_list, def_own, plane_owner, x, y, 1);
466 if (!(att_count = count_non_missiles(bomb_list) +
467 count_non_missiles(esc_list)))
470 emp_initque(&int_list);
471 for (qp = def_list->q_forw; qp != def_list; qp = next) {
473 plp = (struct plist *)qp;
475 /* SAMs interdict separately */
476 if (plp->pcp->pl_flags & P_M)
478 dist = mapdist(x, y, pp->pln_x, pp->pln_y) * 2;
479 if (pp->pln_range < dist)
481 if (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_mobil -= pln_mobcost(dist, pp, P_F);
490 putplane(pp->pln_uid, pp);
492 if (icount > att_count)
497 PR(plane_owner, "%d %s fighter%s rising to intercept!\n",
498 icount, cname(def_own), icount == 1 ? " is" : "s are");
499 PR(def_own, "%d fighter%s intercepting %s planes!\n",
500 icount, icount == 1 ? " is" : "s are", cname(plane_owner));
501 ac_combat_headers(plane_owner, def_own);
502 ac_airtoair(esc_list, &int_list);
503 ac_airtoair(bomb_list, &int_list);
504 PR(plane_owner, "\n");
509 ac_combat_headers(natid plane_owner, natid def_own)
512 " %-10.10s %-10.10s strength int odds damage results\n",
513 cname(plane_owner), cname(def_own));
515 " %-10.10s %-10.10s strength int odds damage results\n",
516 cname(def_own), cname(plane_owner));
523 ac_airtoair(struct emp_qelem *att_list, struct emp_qelem *int_list)
525 struct plist *attacker;
526 struct plist *interceptor;
527 struct emp_qelem *att;
528 struct emp_qelem *in;
532 struct emp_qelem *att_next;
533 struct emp_qelem *in_next;
535 att = att_list->q_forw;
536 in = int_list->q_forw;
539 if (QEMPTY(att_list) || QEMPTY(int_list)) {
543 while (more_att || more_int) {
544 in_next = in->q_forw;
545 att_next = att->q_forw;
546 attacker = (struct plist *)att;
548 /* skip missiles. If only missiles left, we're done */
549 if (plchr[(int)attacker->plane.pln_type].pl_flags & P_M) {
551 if (att == att_list) {
553 if (QEMPTY(att_list))
558 if (all_missiles(att_list))
562 interceptor = (struct plist *)in;
563 nplanes = attacker->plane.pln_effic;
564 if (nplanes > interceptor->plane.pln_effic)
565 nplanes = interceptor->plane.pln_effic;
566 ac_dog(attacker, interceptor);
569 if (att == att_list) {
571 if (QEMPTY(att_list))
576 if (in == int_list) {
578 if (QEMPTY(int_list))
587 all_missiles(struct emp_qelem *att_list)
589 struct emp_qelem *qp;
592 qp = att_list->q_forw;
593 while (qp != att_list) {
594 p = (struct plist *)qp;
595 if (!(plchr[(int)p->plane.pln_type].pl_flags & P_M))
604 ac_dog(struct plist *ap, struct plist *dp)
609 natid att_own, def_own;
614 att_own = ap->plane.pln_own;
615 def_own = dp->plane.pln_own;
617 PR(att_own, " %3.3s #%-4d %3.3s #%-4d",
619 ap->plane.pln_uid, dp->pcp->pl_name, dp->plane.pln_uid);
621 PR(def_own, " %3.3s #%-4d %3.3s #%-4d",
623 dp->plane.pln_uid, ap->pcp->pl_name, ap->plane.pln_uid);
624 if (ap->plane.pln_att == 0) {
625 att = ap->plane.pln_def * ap->plane.pln_effic / 100;
626 att = MAX(att, ap->pcp->pl_def / 2);
628 att = ap->plane.pln_att * ap->plane.pln_effic / 100;
629 att = MAX(att, ap->pcp->pl_att / 2);
632 def = dp->plane.pln_def * dp->plane.pln_effic / 100;
633 def = MAX(def, dp->pcp->pl_def / 2);
635 if ((ap->pcp->pl_flags & P_F) && ap->bombs != 0)
637 if ((dp->pcp->pl_flags & P_F) && dp->bombs != 0)
639 att += ap->pcp->pl_stealth / 25.0;
640 def += dp->pcp->pl_stealth / 25.0;
649 odds = ((double)att / ((double)def + (double)att));
652 intensity = roll(20) + roll(20) + roll(20) + roll(20) + 1;
654 PR(att_own, " %3d/%-3d %3d %3.2f ", att, def, intensity, odds);
655 PR(def_own, " %3d/%-3d %3d %3.2f ", def, att, intensity, odds);
659 while ((intensity--) > 0) {
663 if ((dp->plane.pln_effic - ddam) < PLANE_MINEFF)
667 if ((ap->plane.pln_effic - adam) < PLANE_MINEFF)
672 if (dp->pcp->pl_flags & P_M)
675 PR(att_own, "%3d/%-3d", adam, ddam);
676 PR(def_own, "%3d/%-3d", ddam, adam);
677 ac_planedamage(ap, def_own, adam, def_own, 1, 0, mesg);
678 strncpy(temp, mesg, 14);
679 ac_planedamage(dp, att_own, ddam, att_own, 1, 0, mesg);
680 PR(att_own, "%-13.13s %-13.13s\n", temp, mesg);
681 PR(def_own, "%-13.13s %-13.13s\n", mesg, temp);
684 setcont(att_own, def_own, FOUND_FLY);
685 setcont(def_own, att_own, FOUND_FLY);
690 * zap plane associated with plp.
691 * Damaging country is "from", damage is "dam".
692 * def_own is the country on the other side of the conflict from the plane
693 * owner. The only time def_own != from is when the interceptor is getting
696 * NOTE: This routine removes the appropriate plane element from the
697 * queue if it gets destroyed. That means that the caller must assume
698 * that the current queue pointer is invalid on return from the ac_planedamage
699 * call. (this has caused bugs in the past)
702 ac_planedamage(struct plist *plp, natid from, int dam, natid other,
703 int checkabort, int show, char *mesg)
715 plane_owner = pp->pln_own;
717 sprintf(dmess, " no damage");
722 memset(dmess, 0, sizeof(dmess));
726 if (eff < PLANE_MINEFF) {
727 sprintf(dmess, " shot down");
729 } else if (eff < 80 && chance((100 - eff) / 100.0) && checkabort) {
730 sprintf(dmess, " aborted @%2d%%", eff);
732 } else if (show == 0) {
733 sprintf(dmess, " cleared");
736 if ((plp->pcp->pl_flags & P_M) == 0) {
738 PR(plane_owner, " %s %s takes %d%s.\n",
739 cname(pp->pln_own), prplane(pp), dam, dmess);
741 PR(other, " %s %s takes %d%s.\n",
742 cname(pp->pln_own), prplane(pp), dam, dmess);
745 if (show && checkabort == 1) {
746 PR(plane_owner, "\n");
752 pp->pln_mobil -= MIN(32 + pp->pln_mobil, dam / 2);
754 if (from != 0 && (plp->pcp->pl_flags & P_M) == 0)
755 nreport(from, N_DOWN_PLANE, pp->pln_own, 1);
756 if (pp->pln_ship >= 0) {
757 getship(pp->pln_ship, &ship);
758 take_plane_off_ship(pp, &ship);
760 if (pp->pln_land >= 0) {
761 getland(pp->pln_land, &land);
762 take_plane_off_land(pp, &land);
764 makelost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x, pp->pln_y);
766 putplane(pp->pln_uid, pp);
767 emp_remque(&plp->queue);
769 } else if (disp == 2) {
770 putplane(pp->pln_uid, pp);
771 emp_remque(&plp->queue);
774 putplane(pp->pln_uid, pp);
779 ac_doflak(struct emp_qelem *list, struct sctstr *from)
786 plp = (struct plist *)list->q_forw;
787 plane_owner = plp->plane.pln_own;
789 gun = from->sct_item[I_GUN];
790 shell = from->sct_item[I_SHELL];
793 if (gun > shell * 2) {
794 shell += supply_commod(from->sct_own, from->sct_x, from->sct_y,
795 I_SHELL, (gun + 1) / 2 - shell);
796 from->sct_item[I_SHELL] = shell;
802 gun = 2.0 * tfact(from->sct_own, gun);
804 PR(plane_owner, "firing %d flak guns in %s...\n",
805 gun, xyas(from->sct_x, from->sct_y, plane_owner));
806 if (from->sct_own != 0)
807 PR(from->sct_own, "firing %d flak guns in %s...\n",
808 gun, xyas(from->sct_x, from->sct_y, from->sct_own));
809 ac_fireflak(list, from->sct_own, 0, gun);
814 ac_shipflak(struct emp_qelem *list, coord x, coord y)
829 plp = (struct plist *)list->q_forw;
830 plane_owner = plp->plane.pln_own;
832 memset(nats, 0, sizeof(nats));
834 snxtitem_xy(&ni, EF_SHIP, x, y);
835 while (!QEMPTY(list) && nxtitem(&ni, &ship)) {
836 if (ship.shp_own == 0 || ship.shp_own == plane_owner)
840 mcp = &mchr[(int)ship.shp_type];
841 if (mcp->m_flags & M_SUB)
843 rel = getrel(getnatp(ship.shp_own), plane_owner);
847 gun = MIN(ship.shp_item[I_GUN], ship.shp_glim);
849 shell = ship.shp_item[I_SHELL];
851 shell = supply_commod(ship.shp_own, ship.shp_x, ship.shp_y,
853 ship.shp_item[I_SHELL] = shell;
854 putship(ship.shp_uid, &ship);
857 if (gun == 0 || shell == 0)
859 firing = (int)(techfact(ship.shp_tech, gun) * 2.0);
862 if (!nats[ship.shp_own]) {
863 /* First time here, print the message */
864 PR(ship.shp_own, "%s planes spotted over ships in %s\n",
865 cname(plane_owner), xyas(x, y, ship.shp_own));
866 PR(plane_owner, "Flying over %s ships in %s\n",
867 cname(ship.shp_own), xyas(x, y, plane_owner));
868 nats[ship.shp_own] = 1;
870 PR(ship.shp_own, "firing %d flak guns from %s...\n",
871 firing, prship(&ship));
877 guns = 2.0 * tfact(from, guns);
878 PR(plane_owner, "Flak! Ships firing %d flak guns...\n", guns);
879 ac_fireflak(list, from, 0, guns);
884 ac_landflak(struct emp_qelem *list, coord x, coord y)
897 plp = (struct plist *)list->q_forw;
898 plane_owner = plp->plane.pln_own;
900 memset(nats, 0, sizeof(nats));
902 snxtitem_xy(&ni, EF_LAND, x, y);
903 while (!QEMPTY(list) && nxtitem(&ni, &land)) {
904 if (land.lnd_own == 0 || land.lnd_own == plane_owner)
908 lcp = &lchr[(int)land.lnd_type];
910 if ((lcp->l_flags & L_FLAK) == 0)
913 if (land.lnd_aaf == 0)
916 if (land.lnd_ship >= 0 || land.lnd_land >= 0)
919 rel = getrel(getnatp(land.lnd_own), plane_owner);
922 firing = (int)(techfact(land.lnd_tech, land.lnd_aaf) * 3.0);
925 if (!nats[land.lnd_own]) {
926 /* First time here, print the message */
927 PR(land.lnd_own, "%s planes spotted over land units in %s\n",
928 cname(plane_owner), xyas(x, y, land.lnd_own));
929 PR(plane_owner, "Flying over %s land units in %s\n",
930 cname(land.lnd_own), xyas(x, y, plane_owner));
931 nats[land.lnd_own] = 1;
933 PR(land.lnd_own, "firing flak guns from unit %s (aa rating %d)\n",
934 prland(&land), land.lnd_aaf);
936 from = land.lnd_own; /* We always use the last owner as the from */
941 guns = 2.0 * tfact(from, guns);
942 PR(plane_owner, "Flak! Land units firing %d flak guns...\n",
944 ac_fireflak(list, from, 0, guns);
949 * Called from shipflak, landflak, and doflak.
952 ac_fireflak(struct emp_qelem *list, natid from, natid other, int guns)
958 struct emp_qelem *qp;
959 struct emp_qelem *next;
962 plp = (struct plist *)list->q_forw;
964 for (qp = list->q_forw; qp != list; qp = next) {
966 * fighters don't get shot at by flak
967 * non-tactical bombers are harder to hit with flak.
968 * ('Cause they're not dive-bombing?)
971 plp = (struct plist *)qp;
973 diff = guns - pp->pln_def;
974 if ((plp->pcp->pl_flags & P_T) == 0)
976 if (plp->pcp->pl_flags & P_X)
978 if (plp->pcp->pl_flags & P_H)
980 n = ac_flak_dam(diff);
981 ac_planedamage(plp, from, n, other, 2, 1, msg);
986 * Calculate flak damage
989 ac_flak_dam(int flak)
993 /* <-7 -7 -6 -5 -4 */
994 static float flaktable[18] = { 0.132f, 0.20f, 0.20f, 0.25f, 0.30f,
995 /* -3 -2 -1 0 +1 +2 +3 +4 */
996 0.35f, 0.40f, 0.45f, 0.50f, 0.50f, 0.55f, 0.60f, 0.65f,
997 /* +5 +6 +7 +8 >+8 */
998 0.70f,0.75f, 0.80f, 0.85f, 1.1305f };
999 enum { FLAK_MAX = sizeof(flaktable)/sizeof(flaktable[0]) - 1 };
1002 mult = flaktable[FLAK_MAX];
1004 mult = flaktable[0];
1007 mult = flaktable[flak];
1010 dam = (int)((roll(8) + 2) * mult);
1017 * See if this plane is flying in this list
1020 ac_isflying(struct plnstr *plane, struct emp_qelem *list)
1022 struct emp_qelem *qp;
1023 struct emp_qelem *next;
1029 for (qp = list->q_forw; qp != list; qp = next) {
1031 plp = (struct plist *)qp;
1033 if (plane->pln_uid == pp->pln_uid)
1041 * Get a list of planes available for interception duties.
1044 getilist(struct emp_qelem *list, natid own, struct emp_qelem *a,
1045 struct emp_qelem *b, struct emp_qelem *c, struct emp_qelem *d)
1047 struct plchrstr *pcp;
1048 struct plnstr plane;
1052 struct nstr_item ni;
1057 snxtitem_all(&ni, EF_PLANE);
1058 while (nxtitem(&ni, &plane)) {
1059 if (plane.pln_own != own)
1061 pcp = &plchr[(int)plane.pln_type];
1062 if ((pcp->pl_flags & P_F) == 0)
1064 if (plane.pln_mission != 0)
1066 if (plane.pln_mobil <= 0)
1068 if (plane.pln_effic < 40)
1070 if (plane.pln_ship >= 0) {
1071 if (!can_fly(plane.pln_uid))
1073 getship(plane.pln_ship, &ship);
1074 petrol = ship.shp_item[I_PETROL];
1075 } else if (plane.pln_land >= 0) {
1076 if (!can_fly(plane.pln_uid))
1078 getland(plane.pln_land, &land);
1079 petrol = land.lnd_item[I_PETROL];
1081 getsect(plane.pln_x, plane.pln_y, §);
1082 petrol = sect.sct_item[I_PETROL];
1083 if ((sect.sct_effic < 60 || sect.sct_type != SCT_AIRPT)
1084 && (pcp->pl_flags & P_V) == 0)
1087 if (petrol <= pcp->pl_fuel / 2)
1089 /* Finally, is it in the list of planes already in
1091 if (ac_isflying(&plane, a))
1093 if (ac_isflying(&plane, b))
1095 if (ac_isflying(&plane, c))
1097 if (ac_isflying(&plane, d))
1100 ip = malloc(sizeof(*ip));
1103 ip->pcp = &plchr[(int)plane.pln_type];
1105 emp_insque(&ip->queue, list);
1113 { /* Can this plane fly from the ship or land unit it is on? */
1114 struct plnstr plane;
1117 struct plchrstr *pcp;
1118 struct mchrstr *scp;
1120 getplane(p, &plane);
1121 pcp = &plchr[(int)plane.pln_type];
1123 if (plane.pln_ship >= 0) {
1124 if (!(pcp->pl_flags & P_L) && !(pcp->pl_flags & P_M)
1125 && !(pcp->pl_flags & P_K)
1126 && !(pcp->pl_flags & P_E)
1130 getship(plane.pln_ship, &ship);
1131 scp = &mchr[(int)ship.shp_type];
1133 if ((pcp->pl_flags & P_L) && (scp->m_flags & M_FLY)) {
1137 if ((pcp->pl_flags & P_M) && (scp->m_flags & M_MSL)) {
1141 if ((pcp->pl_flags & P_K) && (scp->m_flags & M_CHOPPER)) {
1145 if ((pcp->pl_flags & P_E) && (scp->m_flags & M_XLIGHT)) {
1150 if (plane.pln_land >= 0) {
1151 if (!(pcp->pl_flags & P_E))
1154 getland(plane.pln_land, &land);
1156 if ((pcp->pl_flags & P_E) && land.lnd_maxlight) {
1165 do_evade(struct emp_qelem *bomb_list, struct emp_qelem *esc_list)
1167 struct emp_qelem *qp;
1172 for (qp = bomb_list->q_forw; qp != bomb_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;
1177 for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw) {
1178 plp = (struct plist *)qp;
1179 if (evade > plp->pcp->pl_stealth / 100.0)
1180 evade = plp->pcp->pl_stealth / 100.0;