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
34 * Markus Armbruster, 2006
53 #include "prototypes.h"
57 #define FLAK_GUN_MAX 14
59 static void ac_intercept(struct emp_qelem *, struct emp_qelem *,
60 struct emp_qelem *, natid, coord, coord);
61 static int all_missiles(struct emp_qelem *);
62 static void ac_dog(struct plist *, struct plist *);
63 static void ac_planedamage(struct plist *, natid, int, natid, int,
65 static void ac_doflak(struct emp_qelem *, struct sctstr *);
66 static void ac_landflak(struct emp_qelem *, coord, coord);
67 static void ac_shipflak(struct emp_qelem *, coord, coord);
68 static void ac_fireflak(struct emp_qelem *, natid, int);
69 static void getilist(struct emp_qelem *, natid,
70 struct emp_qelem *, struct emp_qelem *,
71 struct emp_qelem *, struct emp_qelem *);
72 static int can_fly(int);
73 static int do_evade(struct emp_qelem *, struct emp_qelem *);
76 ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
77 coord x, coord y, char *path, int mission_flags,
78 int no_air_defense, struct emp_qelem *obomb,
79 struct emp_qelem *oesc)
81 int val, non_missiles;
87 int unfriendly[MAXNOC];
89 struct emp_qelem ilist[MAXNOC], *qp;
99 struct natstr *over, *mynatp;
102 struct shiplist *head = NULL;
104 int intown = 0; /* Last owner to intercept */
105 /* We want to only intercept once per sector per owner. So, if we overfly
106 a sector, and then overfly some land units or ships, we don't want to
107 potentially intercept 3 times. */
109 plp = (struct plist *)bomb_list->q_forw;
110 plane_owner = plp->plane.pln_own;
112 strncpy(mypath, path, sizeof(mypath));
115 memset(overfly, 0, sizeof(overfly));
116 memset(gotilist, 0, sizeof(gotilist));
117 memset(unfriendly, 0, sizeof(unfriendly));
118 for (cn = 1; cn < MAXNOC; cn++) {
119 if ((mynatp = getnatp(cn)) == 0)
121 rel = getrel(mynatp, plane_owner);
124 if (plane_owner == cn)
128 if (mission_flags & PM_R) {
129 if (mission_flags & P_S) {
130 PR(plane_owner, "\nSPY Plane report\n");
133 } else if (mission_flags & P_A) {
134 PR(plane_owner, "\nAnti-Sub Patrol report\n");
136 PR(plane_owner, "\nReconnaissance report\n");
141 pln_removedupes(bomb_list, esc_list);
142 while ((dir = mypath[myp++]) && !QEMPTY(bomb_list)) {
143 if ((val = diridx(dir)) == DIR_STOP)
145 /* XXX using xnorm is probably bad */
146 x = xnorm(x + diroff[val][0]);
147 y = ynorm(y + diroff[val][1]);
148 getsect(x, y, §);
149 over = getnatp(sect.sct_own);
151 if (mission_flags & PM_R) {
153 setcont(plane_owner, sect.sct_own, FOUND_FLY);
154 if (sect.sct_type == SCT_WATER) {
155 PR(plane_owner, "flying over %s at %s\n",
156 dchr[sect.sct_type].d_name, xyas(x, y, plane_owner));
157 if (mission_flags & PM_S)
158 plane_sweep(bomb_list, x, y);
159 if (mission_flags & P_A) {
160 plane_sona(bomb_list, x, y, &head);
162 changed += map_set(plane_owner,
163 sect.sct_x, sect.sct_y,
164 dchr[sect.sct_type].d_mnem, 0);
165 } else if (mission_flags & P_S) {
166 satdisp_sect(§, (mission_flags & P_I) ? 10 : 50);
168 /* This is borrowed from lookout */
169 if (sect.sct_own == plane_owner)
170 PR(plane_owner, "Your ");
172 PR(plane_owner, "%s (#%d) ",
173 cname(sect.sct_own), sect.sct_own);
174 PR(plane_owner, "%s", dchr[sect.sct_type].d_name);
175 changed += map_set(plane_owner,
176 sect.sct_x, sect.sct_y,
177 dchr[sect.sct_type].d_mnem, 0);
178 PR(plane_owner, " %d%% efficient ",
179 (sect.sct_own == plane_owner) ?
180 sect.sct_effic : roundintby((int)sect.sct_effic, 25));
181 civ = sect.sct_item[I_CIVIL];
182 mil = sect.sct_item[I_MILIT];
184 PR(plane_owner, "with %s%d civ ",
185 (sect.sct_own == plane_owner) ?
187 (sect.sct_own == plane_owner) ?
188 civ : roundintby(civ, 25));
190 PR(plane_owner, "with %s%d mil ",
191 (sect.sct_own == plane_owner) ?
193 (sect.sct_own == plane_owner) ?
194 mil : roundintby(mil, 25));
195 PR(plane_owner, "@ %s\n", xyas(x, y, plane_owner));
197 if (mission_flags & P_S)
198 satdisp_units(sect.sct_x, sect.sct_y);
200 PR(plane_owner, "flying over %s at %s\n",
201 dchr[sect.sct_type].d_name, xyas(x, y, plane_owner));
202 changed += map_set(plane_owner, sect.sct_x, sect.sct_y,
203 dchr[sect.sct_type].d_mnem, 0);
205 if ((rel = getrel(over, plane_owner)) == ALLIED)
208 evaded = do_evade(bomb_list, esc_list);
210 if (sect.sct_own != 0 && sect.sct_own != plane_owner && !evaded) {
211 /* We only show planes overhead if they didn't
213 overfly[sect.sct_own]++;
214 PR(sect.sct_own, "%s planes spotted over %s\n",
215 cname(plane_owner), xyas(x, y, sect.sct_own));
217 setcont(cn, plane_owner, FOUND_FLY);
222 if (unfriendly[sect.sct_own])
223 ac_doflak(bomb_list, §);
224 /* If bombers left, fire flak from units and ships */
225 if (!QEMPTY(bomb_list))
226 ac_landflak(bomb_list, x, y);
227 if (!QEMPTY(bomb_list))
228 ac_shipflak(bomb_list, x, y);
230 /* mission planes aborted due to flak -- don't send escorts */
231 if (QEMPTY(bomb_list))
233 if (!no_air_defense && !evaded)
234 air_defense(x, y, plane_owner, bomb_list, esc_list);
236 if (sect.sct_own == 0 || sect.sct_own == plane_owner)
243 for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw) {
244 struct plist *ip = (struct plist *)qp;
245 if (!(plchr[(int)ip->plane.pln_type].pl_flags & P_M))
252 if (unfriendly[sect.sct_own] && !gotilist[sect.sct_own]) {
253 getilist(&ilist[sect.sct_own], sect.sct_own,
254 bomb_list, esc_list, obomb, oesc);
255 gotilist[sect.sct_own]++;
259 ac_intercept(bomb_list, esc_list, &ilist[sect.sct_own],
261 intown = sect.sct_own;
264 /* Let's report all of the overflights even if aborted */
265 for (cn = 1; cn < MAXNOC; cn++) {
266 if (plane_owner == cn)
269 nreport(plane_owner, N_OVFLY_SECT, cn, overfly[cn]);
271 /* If the map changed, update it */
273 if (!update_pending && plane_owner == player->cnum)
274 writemap(player->cnum);
275 /* Now, if the bomber and escort lists are empty, we are done */
276 if (QEMPTY(bomb_list) && QEMPTY(esc_list)) {
277 if (mission_flags & P_A)
278 free_shiplist(&head);
282 /* Something made it through */
283 /* Go figure out if there are ships in this sector, and who's they are */
284 memset(nats, 0, sizeof(nats));
285 snxtitem_xy(&ni, EF_SHIP, x, y);
286 while (nxtitem(&ni, &ship)) {
287 if (mchr[(int)ship.shp_type].m_flags & M_SUB)
289 nats[ship.shp_own]++;
291 /* Go figure out if there are units in this sector, and who's they are */
292 memset(lnats, 0, sizeof(lnats));
293 snxtitem_xy(&ni, EF_LAND, x, y);
294 while (nxtitem(&ni, &land)) {
295 if (land.lnd_ship >= 0 || land.lnd_land >= 0)
297 lnats[land.lnd_own]++;
300 /* Now, let's make life a little rougher. */
301 for (cn = 1; cn < MAXNOC && !QEMPTY(bomb_list); cn++) {
302 if (plane_owner == cn)
305 /* Are there ships owned by this country? */
309 PR(cn, "%s planes spotted over ships in %s\n",
310 cname(plane_owner), xyas(x, y, cn));
312 setcont(cn, plane_owner, FOUND_FLY);
314 if (unfriendly[cn]) {
315 /* They are unfriendly too */
317 getilist(&ilist[cn], cn, bomb_list, esc_list, obomb,
321 PR(plane_owner, "Flying over %s ships in %s\n",
322 cname(cn), xyas(x, y, plane_owner));
323 /* This makes going for ships in harbors tough */
325 /* We already fired flak up above. Now we intercept again if we haven't already */
326 /* Flag that we intercepted */
328 /* And now intercept again */
329 ac_intercept(bomb_list, esc_list, &ilist[cn],
334 /* Are there units owned by this country? */
335 if (lnats[cn] != 0) {
338 PR(cn, "%s planes spotted over land units in %s\n",
339 cname(plane_owner), xyas(x, y, cn));
341 setcont(cn, plane_owner, FOUND_FLY);
343 if (unfriendly[cn]) {
344 /* They are unfriendly too */
346 getilist(&ilist[cn], cn, bomb_list, esc_list, obomb,
350 PR(plane_owner, "Flying over %s land units in %s\n",
351 cname(cn), xyas(x, y, plane_owner));
354 /* We haven't intercepted yet, so intercept */
355 ac_intercept(bomb_list, esc_list, &ilist[cn],
362 if (mission_flags & P_A)
363 free_shiplist(&head);
367 count_non_missiles(struct emp_qelem *list)
369 struct emp_qelem *qp;
373 /* don't intercept missiles */
374 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
375 plp = (struct plist *)qp;
376 if (!(plp->pcp->pl_flags & P_M))
383 sam_intercept(struct emp_qelem *att_list, struct emp_qelem *def_list,
384 natid def_own, natid plane_owner, coord x, coord y,
387 struct emp_qelem *aqp;
388 struct emp_qelem *anext;
389 struct emp_qelem *dqp;
390 struct emp_qelem *dnext;
395 for (aqp = att_list->q_forw,
396 dqp = def_list->q_forw;
397 aqp != att_list && dqp != def_list; aqp = anext) {
399 aplp = (struct plist *)aqp;
400 if (aplp->pcp->pl_flags & P_M)
402 if (aplp->pcp->pl_cost < 1000)
404 for (; dqp != def_list; dqp = dnext) {
406 dplp = (struct plist *)dqp;
407 if (!(dplp->pcp->pl_flags & P_M))
410 if (dplp->plane.pln_range <
411 mapdist(x, y, dplp->plane.pln_x, dplp->plane.pln_y)) {
416 if (mission_pln_equip(dplp, 0, P_F, 0) < 0) {
423 PR(plane_owner, "%s launches SAMs!\n", cname(def_own));
424 PR(def_own, "Launching SAMs at %s planes!\n",
426 ac_combat_headers(plane_owner, def_own);
434 PR(plane_owner, "\n");
437 if (delete_missiles) {
438 for (; dqp != def_list; dqp = dnext) {
440 dplp = (struct plist *)dqp;
441 if (!(dplp->pcp->pl_flags & P_M))
451 ac_intercept(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
452 struct emp_qelem *def_list, natid def_own, coord x, coord y)
457 struct emp_qelem *next;
458 struct emp_qelem *qp;
459 struct emp_qelem int_list;
464 plp = (struct plist *)bomb_list->q_forw;
465 plane_owner = plp->plane.pln_own;
469 sam_intercept(bomb_list, def_list, def_own, plane_owner, x, y, 0);
470 sam_intercept(esc_list, def_list, def_own, plane_owner, x, y, 1);
471 if (!(att_count = count_non_missiles(bomb_list) +
472 count_non_missiles(esc_list)))
475 emp_initque(&int_list);
476 for (qp = def_list->q_forw; qp != def_list; qp = next) {
478 plp = (struct plist *)qp;
480 /* SAMs interdict separately */
481 if (plp->pcp->pl_flags & P_M)
483 dist = mapdist(x, y, pp->pln_x, pp->pln_y) * 2;
484 if (pp->pln_range < dist)
486 if (mission_pln_equip(plp, 0, P_F, 0) < 0) {
491 /* got one; delete from def_list, add to int_list */
493 emp_insque(qp, &int_list);
494 pp->pln_mobil -= pln_mobcost(dist, pp, P_F);
495 putplane(pp->pln_uid, pp);
497 if (icount > att_count)
502 PR(plane_owner, "%d %s fighter%s rising to intercept!\n",
503 icount, cname(def_own), icount == 1 ? " is" : "s are");
504 PR(def_own, "%d fighter%s intercepting %s planes!\n",
505 icount, icount == 1 ? " is" : "s are", cname(plane_owner));
506 ac_combat_headers(plane_owner, def_own);
507 ac_airtoair(esc_list, &int_list);
508 ac_airtoair(bomb_list, &int_list);
509 PR(plane_owner, "\n");
514 ac_combat_headers(natid plane_owner, natid def_own)
517 " %-10.10s %-10.10s strength int odds damage results\n",
518 cname(plane_owner), cname(def_own));
520 " %-10.10s %-10.10s strength int odds damage results\n",
521 cname(def_own), cname(plane_owner));
528 ac_airtoair(struct emp_qelem *att_list, struct emp_qelem *int_list)
530 struct plist *attacker;
531 struct plist *interceptor;
532 struct emp_qelem *att;
533 struct emp_qelem *in;
537 struct emp_qelem *att_next;
538 struct emp_qelem *in_next;
540 att = att_list->q_forw;
541 in = int_list->q_forw;
544 if (QEMPTY(att_list) || QEMPTY(int_list)) {
548 while (more_att || more_int) {
549 in_next = in->q_forw;
550 att_next = att->q_forw;
551 attacker = (struct plist *)att;
553 /* skip missiles. If only missiles left, we're done */
554 if (plchr[(int)attacker->plane.pln_type].pl_flags & P_M) {
556 if (att == att_list) {
558 if (QEMPTY(att_list))
563 if (all_missiles(att_list))
567 interceptor = (struct plist *)in;
568 nplanes = attacker->plane.pln_effic;
569 if (nplanes > interceptor->plane.pln_effic)
570 nplanes = interceptor->plane.pln_effic;
571 ac_dog(attacker, interceptor);
574 if (att == att_list) {
576 if (QEMPTY(att_list))
581 if (in == int_list) {
583 if (QEMPTY(int_list))
592 all_missiles(struct emp_qelem *att_list)
594 struct emp_qelem *qp;
597 qp = att_list->q_forw;
598 while (qp != att_list) {
599 p = (struct plist *)qp;
600 if (!(plchr[(int)p->plane.pln_type].pl_flags & P_M))
609 ac_dog(struct plist *ap, struct plist *dp)
614 natid att_own, def_own;
619 att_own = ap->plane.pln_own;
620 def_own = dp->plane.pln_own;
622 PR(att_own, " %3.3s #%-4d %3.3s #%-4d",
624 ap->plane.pln_uid, dp->pcp->pl_name, dp->plane.pln_uid);
626 PR(def_own, " %3.3s #%-4d %3.3s #%-4d",
628 dp->plane.pln_uid, ap->pcp->pl_name, ap->plane.pln_uid);
629 if (ap->plane.pln_att == 0) {
630 att = ap->plane.pln_def * ap->plane.pln_effic / 100;
631 att = MAX(att, ap->pcp->pl_def / 2);
633 att = ap->plane.pln_att * ap->plane.pln_effic / 100;
634 att = MAX(att, ap->pcp->pl_att / 2);
637 def = dp->plane.pln_def * dp->plane.pln_effic / 100;
638 def = MAX(def, dp->pcp->pl_def / 2);
640 if ((ap->pcp->pl_flags & P_F) && ap->bombs != 0)
642 if ((dp->pcp->pl_flags & P_F) && dp->bombs != 0)
644 att += ap->pcp->pl_stealth / 25.0;
645 def += dp->pcp->pl_stealth / 25.0;
654 odds = ((double)att / ((double)def + (double)att));
657 intensity = roll(20) + roll(20) + roll(20) + roll(20) + 1;
659 PR(att_own, " %3d/%-3d %3d %3.2f ", att, def, intensity, odds);
660 PR(def_own, " %3d/%-3d %3d %3.2f ", def, att, intensity, odds);
664 while ((intensity--) > 0) {
668 if ((dp->plane.pln_effic - ddam) < PLANE_MINEFF)
672 if ((ap->plane.pln_effic - adam) < PLANE_MINEFF)
677 if (dp->pcp->pl_flags & P_M)
680 PR(att_own, "%3d/%-3d", adam, ddam);
681 PR(def_own, "%3d/%-3d", ddam, adam);
682 ac_planedamage(ap, def_own, adam, def_own, 1, 0, mesg);
683 strncpy(temp, mesg, 14);
684 ac_planedamage(dp, att_own, ddam, att_own, 1, 0, mesg);
685 PR(att_own, "%-13.13s %-13.13s\n", temp, mesg);
686 PR(def_own, "%-13.13s %-13.13s\n", mesg, temp);
689 setcont(att_own, def_own, FOUND_FLY);
690 setcont(def_own, att_own, FOUND_FLY);
695 * zap plane associated with plp.
696 * Damaging country is "from", damage is "dam".
697 * def_own is the country on the other side of the conflict from the plane
698 * owner. The only time def_own != from is when the interceptor is getting
701 * NOTE: This routine removes the appropriate plane element from the
702 * queue if it gets destroyed. That means that the caller must assume
703 * that the current queue pointer is invalid on return from the ac_planedamage
704 * call. (this has caused bugs in the past)
707 ac_planedamage(struct plist *plp, natid from, int dam, natid other,
708 int checkabort, int show, char *mesg)
720 plane_owner = pp->pln_own;
722 sprintf(dmess, " no damage");
727 memset(dmess, 0, sizeof(dmess));
731 if (eff < PLANE_MINEFF) {
732 sprintf(dmess, " shot down");
734 } else if (eff < 80 && chance((80 - eff) / 100.0) && checkabort) {
735 sprintf(dmess, " aborted @%2d%%", eff);
737 } else if (show == 0) {
738 sprintf(dmess, " cleared");
741 if ((plp->pcp->pl_flags & P_M) == 0) {
743 PR(plane_owner, " %s %s takes %d%s.\n",
744 cname(pp->pln_own), prplane(pp), dam, dmess);
746 PR(other, " %s %s takes %d%s.\n",
747 cname(pp->pln_own), prplane(pp), dam, dmess);
750 if (show && checkabort == 1) {
751 PR(plane_owner, "\n");
757 pp->pln_mobil -= MIN(32 + pp->pln_mobil, dam / 2);
759 if (from != 0 && (plp->pcp->pl_flags & P_M) == 0)
760 nreport(from, N_DOWN_PLANE, pp->pln_own, 1);
761 if (pp->pln_ship >= 0) {
762 getship(pp->pln_ship, &ship);
763 take_plane_off_ship(pp, &ship);
765 if (pp->pln_land >= 0) {
766 getland(pp->pln_land, &land);
767 take_plane_off_land(pp, &land);
770 putplane(pp->pln_uid, pp);
771 emp_remque(&plp->queue);
773 } else if (disp == 2) {
774 putplane(pp->pln_uid, pp);
775 emp_remque(&plp->queue);
778 putplane(pp->pln_uid, pp);
783 ac_doflak(struct emp_qelem *list, struct sctstr *from)
790 plp = (struct plist *)list->q_forw;
791 plane_owner = plp->plane.pln_own;
793 gun = MIN(FLAK_GUN_MAX, from->sct_item[I_GUN]);
794 shell = from->sct_item[I_SHELL];
795 if (gun > shell * 2) {
796 shell += supply_commod(from->sct_own, from->sct_x, from->sct_y,
797 I_SHELL, (gun + 1) / 2 - shell);
798 from->sct_item[I_SHELL] = shell;
804 gun = roundavg(tfact(from->sct_own, 2.0 * gun));
806 PR(plane_owner, "firing %d flak guns in %s...\n",
807 gun, xyas(from->sct_x, from->sct_y, plane_owner));
808 if (from->sct_own != 0)
809 PR(from->sct_own, "firing %d flak guns in %s...\n",
810 gun, xyas(from->sct_x, from->sct_y, from->sct_own));
811 ac_fireflak(list, from->sct_own, gun);
816 ac_shipflak(struct emp_qelem *list, coord x, coord y)
821 double flak, total, ngun;
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)
838 mcp = &mchr[(int)ship.shp_type];
839 if (mcp->m_flags & M_SUB)
841 rel = getrel(getnatp(ship.shp_own), plane_owner);
845 gun = MIN(ship.shp_item[I_GUN], ship.shp_glim);
847 shell = ship.shp_item[I_SHELL];
849 shell = supply_commod(ship.shp_own, ship.shp_x, ship.shp_y,
851 ship.shp_item[I_SHELL] = shell;
852 putship(ship.shp_uid, &ship);
855 if (gun == 0 || shell == 0)
857 flak = gun * (ship.shp_effic / 100.0);
859 total += techfact(ship.shp_tech, flak * 2.0);
861 if (!nats[ship.shp_own]) {
862 /* First time here, print the message */
863 PR(ship.shp_own, "%s planes spotted over ships in %s\n",
864 cname(plane_owner), xyas(x, y, ship.shp_own));
865 PR(plane_owner, "Flying over %s ships in %s\n",
866 cname(ship.shp_own), xyas(x, y, plane_owner));
867 nats[ship.shp_own] = 1;
869 PR(ship.shp_own, "firing %.0f flak guns from %s...\n",
870 flak, prship(&ship));
874 /* Limit to FLAK_GUN_MAX guns of average tech factor */
875 if (ngun > FLAK_GUN_MAX)
876 total *= FLAK_GUN_MAX / ngun;
878 gun = roundavg(total);
880 PR(plane_owner, "Flak! Ships firing %d flak guns...\n", gun);
881 ac_fireflak(list, from, gun);
886 ac_landflak(struct emp_qelem *list, coord x, coord y)
891 double flak, total, ngun;
899 plp = (struct plist *)list->q_forw;
900 plane_owner = plp->plane.pln_own;
902 memset(nats, 0, sizeof(nats));
904 snxtitem_xy(&ni, EF_LAND, x, y);
905 while (!QEMPTY(list) && nxtitem(&ni, &land)) {
906 if (land.lnd_own == 0 || land.lnd_own == plane_owner)
908 lcp = &lchr[(int)land.lnd_type];
909 if ((lcp->l_flags & L_FLAK) == 0 || land.lnd_aaf == 0)
911 if (land.lnd_ship >= 0 || land.lnd_land >= 0)
913 rel = getrel(getnatp(land.lnd_own), plane_owner);
916 flak = land.lnd_aaf * 1.5 * land.lnd_effic / 100.0;
918 total += techfact(land.lnd_tech, flak * 2.0);
920 if (!nats[land.lnd_own]) {
921 /* First time here, print the message */
922 PR(land.lnd_own, "%s planes spotted over land units in %s\n",
923 cname(plane_owner), xyas(x, y, land.lnd_own));
924 PR(plane_owner, "Flying over %s land units in %s\n",
925 cname(land.lnd_own), xyas(x, y, plane_owner));
926 nats[land.lnd_own] = 1;
928 PR(land.lnd_own, "firing flak guns from unit %s (aa rating %d)\n",
929 prland(&land), land.lnd_aaf);
933 /* Limit to FLAK_GUN_MAX guns of average tech factor */
934 if (ngun > FLAK_GUN_MAX)
935 total *= FLAK_GUN_MAX / ngun;
937 gun = roundavg(total);
939 PR(plane_owner, "Flak! Land units firing %d flak guns...\n", gun);
940 ac_fireflak(list, from, gun);
945 * Called from shipflak, landflak, and doflak.
948 ac_fireflak(struct emp_qelem *list, natid from, 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 plp = (struct plist *)qp;
963 n = ac_flak_dam(guns, plp->plane.pln_def, plp->pcp->pl_flags);
964 ac_planedamage(plp, from, n, 0, 2, 1, msg);
969 * Calculate flak damage
972 ac_flak_dam(int guns, int def, int pl_flags)
976 /* <-7 -7 -6 -5 -4 */
977 static float flaktable[18] = { 0.132f, 0.20f, 0.20f, 0.25f, 0.30f,
978 /* -3 -2 -1 0 +1 +2 +3 +4 */
979 0.35f, 0.40f, 0.45f, 0.50f, 0.50f, 0.55f, 0.60f, 0.65f,
980 /* +5 +6 +7 +8 >+8 */
981 0.70f,0.75f, 0.80f, 0.85f, 1.1305f };
982 enum { FLAK_MAX = sizeof(flaktable)/sizeof(flaktable[0]) - 1 };
985 if ((pl_flags & P_T) == 0)
993 mult = flaktable[FLAK_MAX];
998 mult = flaktable[flak];
1001 dam = (int)((roll(8) + 2) * mult);
1008 * See if this plane is flying in this list
1011 ac_isflying(struct plnstr *plane, struct emp_qelem *list)
1013 struct emp_qelem *qp;
1014 struct emp_qelem *next;
1020 for (qp = list->q_forw; qp != list; qp = next) {
1022 plp = (struct plist *)qp;
1024 if (plane->pln_uid == pp->pln_uid)
1032 * Get a list of planes available for interception duties.
1035 getilist(struct emp_qelem *list, natid own, struct emp_qelem *a,
1036 struct emp_qelem *b, struct emp_qelem *c, struct emp_qelem *d)
1038 struct plchrstr *pcp;
1039 struct plnstr plane;
1043 struct nstr_item ni;
1048 snxtitem_all(&ni, EF_PLANE);
1049 while (nxtitem(&ni, &plane)) {
1050 if (plane.pln_own != own)
1052 pcp = &plchr[(int)plane.pln_type];
1053 if ((pcp->pl_flags & P_F) == 0)
1055 if (plane.pln_mission != 0)
1057 if (plane.pln_mobil <= 0)
1059 if (plane.pln_effic < 40)
1061 if (plane.pln_ship >= 0) {
1062 if (!can_fly(plane.pln_uid))
1064 getship(plane.pln_ship, &ship);
1065 petrol = ship.shp_item[I_PETROL];
1066 } else if (plane.pln_land >= 0) {
1067 if (!can_fly(plane.pln_uid))
1069 getland(plane.pln_land, &land);
1070 petrol = land.lnd_item[I_PETROL];
1072 getsect(plane.pln_x, plane.pln_y, §);
1073 petrol = sect.sct_item[I_PETROL];
1074 if ((sect.sct_effic < 60 || sect.sct_type != SCT_AIRPT)
1075 && (pcp->pl_flags & P_V) == 0)
1078 if (petrol <= pcp->pl_fuel / 2)
1080 /* Finally, is it in the list of planes already in
1082 if (ac_isflying(&plane, a))
1084 if (ac_isflying(&plane, b))
1086 if (ac_isflying(&plane, c))
1088 if (ac_isflying(&plane, d))
1091 ip = malloc(sizeof(*ip));
1094 ip->pcp = &plchr[(int)plane.pln_type];
1096 emp_insque(&ip->queue, list);
1104 { /* Can this plane fly from the ship or land unit it is on? */
1105 struct plnstr plane;
1108 struct plchrstr *pcp;
1109 struct mchrstr *scp;
1111 getplane(p, &plane);
1112 pcp = &plchr[(int)plane.pln_type];
1114 if (plane.pln_ship >= 0) {
1115 if (!(pcp->pl_flags & P_L) && !(pcp->pl_flags & P_M)
1116 && !(pcp->pl_flags & P_K)
1117 && !(pcp->pl_flags & P_E)
1121 getship(plane.pln_ship, &ship);
1122 scp = &mchr[(int)ship.shp_type];
1124 if ((pcp->pl_flags & P_L) && (scp->m_flags & M_FLY)) {
1128 if ((pcp->pl_flags & P_M) && (scp->m_flags & M_MSL)) {
1132 if ((pcp->pl_flags & P_K) && (scp->m_flags & M_CHOPPER)) {
1136 if ((pcp->pl_flags & P_E) && (scp->m_flags & M_XLIGHT)) {
1141 if (plane.pln_land >= 0) {
1142 if (!(pcp->pl_flags & P_E))
1145 getland(plane.pln_land, &land);
1147 if ((pcp->pl_flags & P_E) && land.lnd_maxlight) {
1156 do_evade(struct emp_qelem *bomb_list, struct emp_qelem *esc_list)
1158 struct emp_qelem *qp;
1163 for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw) {
1164 plp = (struct plist *)qp;
1165 if (evade > plp->pcp->pl_stealth / 100.0)
1166 evade = plp->pcp->pl_stealth / 100.0;
1168 for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw) {
1169 plp = (struct plist *)qp;
1170 if (evade > plp->pcp->pl_stealth / 100.0)
1171 evade = plp->pcp->pl_stealth / 100.0;