2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2010, 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-2009
51 #include "prototypes.h"
56 #define FLAK_GUN_MAX 14
58 static void sam_intercept(struct emp_qelem *, struct emp_qelem *,
59 natid, natid, coord, coord, int);
60 static void ac_intercept(struct emp_qelem *, struct emp_qelem *,
61 struct emp_qelem *, natid, coord, coord, int);
62 static void ac_combat_headers(natid, natid);
63 static void ac_airtoair(struct emp_qelem *, struct emp_qelem *);
64 static void ac_dog(struct plist *, struct plist *);
65 static void ac_planedamage(struct plist *, natid, int, natid, int,
67 static void ac_doflak(struct emp_qelem *, struct sctstr *);
68 static void ac_landflak(struct emp_qelem *, coord, coord);
69 static void ac_shipflak(struct emp_qelem *, coord, coord);
70 static void ac_fireflak(struct emp_qelem *, natid, int);
71 static void getilists(struct emp_qelem *, unsigned char *, natid);
72 static int do_evade(struct emp_qelem *, struct emp_qelem *);
75 ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
76 coord x, coord y, char *path, int mission_flags)
80 unsigned char gotships[MAXNOC];
81 unsigned char gotlands[MAXNOC];
82 unsigned char rel[MAXNOC];
85 struct emp_qelem ilist[MAXNOC];
95 struct shiplist *head = NULL;
97 /* We want to only intercept once per sector per owner. So, if we overfly
98 a sector, and then overfly some land units or ships, we don't want to
99 potentially intercept 3 times. */
101 plp = (struct plist *)bomb_list->q_forw;
102 plane_owner = plp->plane.pln_own;
104 memset(overfly, 0, sizeof(overfly));
105 getilists(ilist, rel, plane_owner);
107 if (mission_flags & PM_R) {
108 flags = pln_caps(bomb_list);
110 PR(plane_owner, "\nSPY Plane report\n");
113 } else if (flags & P_A) {
114 PR(plane_owner, "\nAnti-Sub Patrol report\n");
116 PR(plane_owner, "\nReconnaissance report\n");
122 getsect(x, y, §);
123 memset(gotships, 0, sizeof(gotships));
124 snxtitem_xy(&ni, EF_SHIP, x, y);
125 while (nxtitem(&ni, &ship)) {
126 if (mchr[(int)ship.shp_type].m_flags & M_SUB)
128 gotships[ship.shp_own] = 1;
130 memset(gotlands, 0, sizeof(gotlands));
131 snxtitem_xy(&ni, EF_LAND, x, y);
132 while (nxtitem(&ni, &land)) {
133 if (land.lnd_ship >= 0 || land.lnd_land >= 0)
135 gotlands[land.lnd_own] = 1;
138 if (mission_flags & PM_R) {
139 flags = pln_caps(bomb_list);
141 setcont(plane_owner, sect.sct_own, FOUND_FLY);
142 if (sect.sct_type == SCT_WATER) {
143 PR(plane_owner, "flying over %s at %s\n",
144 dchr[sect.sct_type].d_name, xyas(x, y, plane_owner));
145 if (mission_flags & PM_S)
146 plane_sweep(bomb_list, x, y);
148 plane_sona(bomb_list, x, y, &head);
149 changed += map_set(plane_owner,
150 sect.sct_x, sect.sct_y,
151 dchr[sect.sct_type].d_mnem, 0);
152 } else if (flags & P_S) {
153 satdisp_sect(§, flags & P_I ? 10 : 50);
155 /* This is borrowed from lookout */
156 if (sect.sct_own == plane_owner)
157 PR(plane_owner, "Your ");
159 PR(plane_owner, "%s (#%d) ",
160 cname(sect.sct_own), sect.sct_own);
161 PR(plane_owner, "%s", dchr[sect.sct_type].d_name);
162 changed += map_set(plane_owner,
163 sect.sct_x, sect.sct_y,
164 dchr[sect.sct_type].d_mnem, 0);
165 PR(plane_owner, " %d%% efficient ",
166 (sect.sct_own == plane_owner) ?
167 sect.sct_effic : roundintby((int)sect.sct_effic, 25));
168 civ = sect.sct_item[I_CIVIL];
169 mil = sect.sct_item[I_MILIT];
171 PR(plane_owner, "with %s%d civ ",
172 (sect.sct_own == plane_owner) ?
174 (sect.sct_own == plane_owner) ?
175 civ : roundintby(civ, 25));
177 PR(plane_owner, "with %s%d mil ",
178 (sect.sct_own == plane_owner) ?
180 (sect.sct_own == plane_owner) ?
181 mil : roundintby(mil, 25));
182 PR(plane_owner, "@ %s\n", xyas(x, y, plane_owner));
185 satdisp_units(sect.sct_x, sect.sct_y);
187 for (cn = 1; cn < MAXNOC; cn++) {
188 if (cn == plane_owner)
191 PR(plane_owner, "Flying over %s ships in %s\n",
192 cname(cn), xyas(x, y, plane_owner));
194 PR(plane_owner, "Flying over %s land units in %s\n",
195 cname(cn), xyas(x, y, plane_owner));
199 PR(plane_owner, "flying over %s at %s\n",
200 dchr[sect.sct_type].d_name, xyas(x, y, plane_owner));
201 changed += map_set(plane_owner, sect.sct_x, sect.sct_y,
202 dchr[sect.sct_type].d_mnem, 0);
205 evaded = do_evade(bomb_list, esc_list);
207 overfly[sect.sct_own]++;
208 for (cn = 1; cn < MAXNOC; cn++) {
209 if (cn == plane_owner || rel[cn] == ALLIED)
211 if (cn != sect.sct_own && !gotships[cn] && !gotlands[cn])
213 PR(cn, "%s planes spotted over %s\n",
214 cname(plane_owner), xyas(x, y, cn));
216 setcont(cn, plane_owner, FOUND_FLY);
220 if (rel[sect.sct_own] <= HOSTILE)
221 ac_doflak(bomb_list, §);
222 /* If bombers left, fire flak from units and ships */
223 if (!QEMPTY(bomb_list))
224 ac_landflak(bomb_list, x, y);
225 if (!QEMPTY(bomb_list))
226 ac_shipflak(bomb_list, x, y);
227 /* mission planes aborted due to flak -- don't send escorts */
228 if (QEMPTY(bomb_list))
231 for (cn = 1; cn < MAXNOC && !QEMPTY(bomb_list); cn++) {
232 if (rel[cn] > HOSTILE)
234 ac_intercept(bomb_list, esc_list, &ilist[cn], cn, x, y,
236 || gotships[cn] || gotlands[cn]));
241 if (!dir || QEMPTY(bomb_list) || (val = diridx(dir)) == DIR_STOP)
243 x = xnorm(x + diroff[val][0]);
244 y = ynorm(y + diroff[val][1]);
247 /* Let's report all of the overflights even if aborted */
248 for (cn = 1; cn < MAXNOC; cn++) {
249 if (plane_owner == cn)
251 if (overfly[cn] > 0 && rel[cn] != ALLIED)
252 nreport(plane_owner, N_OVFLY_SECT, cn, overfly[cn]);
254 /* If the map changed, update it */
256 writemap(player->cnum);
258 free_shiplist(&head);
259 for (cn = 1; cn < MAXNOC; cn++)
264 sam_intercept(struct emp_qelem *att_list, struct emp_qelem *def_list,
265 natid def_own, natid plane_owner, coord x, coord y,
268 struct emp_qelem *aqp;
269 struct emp_qelem *anext;
270 struct emp_qelem *dqp;
271 struct emp_qelem *dnext;
277 for (aqp = att_list->q_forw,
278 dqp = def_list->q_forw;
279 aqp != att_list && dqp != def_list; aqp = anext) {
281 aplp = (struct plist *)aqp;
282 if (aplp->pcp->pl_cost < 1000)
284 for (; dqp != def_list; dqp = dnext) {
286 dplp = (struct plist *)dqp;
288 if (!(dplp->pcp->pl_flags & P_M))
290 if (only_mission && !pp->pln_mission)
292 if (pp->pln_range < mapdist(x, y, pp->pln_x, pp->pln_y))
295 && pp->pln_radius < mapdist(x, y, pp->pln_opx, pp->pln_opy))
297 if (CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED)
298 || mission_pln_equip(dplp, NULL, 0) < 0) {
303 pp->pln_flags |= PLN_LAUNCHED;
304 putplane(pp->pln_uid, pp);
307 PR(plane_owner, "%s launches SAMs!\n", cname(def_own));
308 PR(def_own, "Launching SAMs at %s planes over %s!\n",
309 cname(plane_owner), xyas(x, y, def_own));
310 ac_combat_headers(plane_owner, def_own);
318 PR(plane_owner, "\n");
324 ac_intercept(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
325 struct emp_qelem *def_list, natid def_own, coord x, coord y,
331 struct emp_qelem *next;
332 struct emp_qelem *qp;
333 struct emp_qelem int_list;
338 plp = (struct plist *)bomb_list->q_forw;
339 plane_owner = plp->plane.pln_own;
341 sam_intercept(bomb_list, def_list, def_own, plane_owner, x, y,
343 sam_intercept(esc_list, def_list, def_own, plane_owner, x, y,
347 for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw)
349 for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw)
354 emp_initque(&int_list);
356 for (qp = def_list->q_forw; qp != def_list; qp = next) {
358 plp = (struct plist *)qp;
360 /* SAMs interdict separately */
361 if (plp->pcp->pl_flags & P_M)
363 if (only_mission && !pp->pln_mission)
365 dist = mapdist(x, y, pp->pln_x, pp->pln_y) * 2;
366 if (pp->pln_range < dist)
369 && pp->pln_radius < mapdist(x, y, pp->pln_opx, pp->pln_opy))
371 if (CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED)
372 || mission_pln_equip(plp, NULL, 0) < 0) {
377 /* got one; delete from def_list, add to int_list */
379 emp_insque(qp, &int_list);
380 pp->pln_flags |= PLN_LAUNCHED;
381 pp->pln_mobil -= pln_mobcost(dist, pp, 0);
382 putplane(pp->pln_uid, pp);
384 if (icount > att_count)
389 PR(plane_owner, "%d %s fighter%s rising to intercept!\n",
390 icount, cname(def_own), icount == 1 ? " is" : "s are");
391 PR(def_own, "%d fighter%s intercepting %s planes over %s!\n",
392 icount, icount == 1 ? " is" : "s are", cname(plane_owner),
393 xyas(x, y, def_own));
394 ac_combat_headers(plane_owner, def_own);
395 ac_airtoair(esc_list, &int_list);
396 ac_airtoair(bomb_list, &int_list);
397 PR(plane_owner, "\n");
403 ac_combat_headers(natid plane_owner, natid def_own)
406 " %-10.10s %-10.10s strength int odds damage results\n",
407 cname(plane_owner), cname(def_own));
409 " %-10.10s %-10.10s strength int odds damage results\n",
410 cname(def_own), cname(plane_owner));
417 ac_airtoair(struct emp_qelem *att_list, struct emp_qelem *int_list)
419 struct plist *attacker;
420 struct plist *interceptor;
421 struct emp_qelem *att;
422 struct emp_qelem *in;
425 struct emp_qelem *att_next;
426 struct emp_qelem *in_next;
428 att = att_list->q_forw;
429 in = int_list->q_forw;
432 if (QEMPTY(att_list) || QEMPTY(int_list)) {
436 while (more_att || more_int) {
437 in_next = in->q_forw;
438 att_next = att->q_forw;
439 attacker = (struct plist *)att;
440 interceptor = (struct plist *)in;
441 ac_dog(attacker, interceptor);
444 if (att == att_list) {
446 if (QEMPTY(att_list))
451 if (in == int_list) {
453 if (QEMPTY(int_list))
462 ac_dog(struct plist *ap, struct plist *dp)
467 natid att_own, def_own;
472 att_own = ap->plane.pln_own;
473 def_own = dp->plane.pln_own;
475 PR(att_own, " %3.3s #%-4d %3.3s #%-4d",
477 ap->plane.pln_uid, dp->pcp->pl_name, dp->plane.pln_uid);
479 PR(def_own, " %3.3s #%-4d %3.3s #%-4d",
481 dp->plane.pln_uid, ap->pcp->pl_name, ap->plane.pln_uid);
482 att = pln_att(&ap->plane);
484 att = pln_def(&ap->plane);
485 att = att * ap->plane.pln_effic / 100;
486 att = MAX(att, ap->pcp->pl_def / 2);
488 def = pln_def(&dp->plane) * dp->plane.pln_effic / 100;
489 def = MAX(def, dp->pcp->pl_def / 2);
491 if ((ap->pcp->pl_flags & P_F) && ap->load != 0)
493 if ((dp->pcp->pl_flags & P_F) && dp->load != 0)
495 att += ap->pcp->pl_stealth / 25.0;
496 def += dp->pcp->pl_stealth / 25.0;
505 odds = ((double)att / ((double)def + (double)att));
508 intensity = roll(20) + roll(20) + roll(20) + roll(20) + 1;
510 PR(att_own, " %3d/%-3d %3d %3.2f ", att, def, intensity, odds);
511 PR(def_own, " %3d/%-3d %3d %3.2f ", def, att, intensity, odds);
515 while ((intensity--) > 0) {
519 if ((dp->plane.pln_effic - ddam) < PLANE_MINEFF)
523 if ((ap->plane.pln_effic - adam) < PLANE_MINEFF)
528 if (dp->pcp->pl_flags & P_M)
531 PR(att_own, "%3d/%-3d", adam, ddam);
532 PR(def_own, "%3d/%-3d", ddam, adam);
533 ac_planedamage(ap, def_own, adam, def_own, 1, 0, mesg);
534 strncpy(temp, mesg, 14);
535 ac_planedamage(dp, att_own, ddam, att_own, 1, 0, mesg);
536 PR(att_own, "%-13.13s %-13.13s\n", temp, mesg);
537 PR(def_own, "%-13.13s %-13.13s\n", mesg, temp);
540 setcont(att_own, def_own, FOUND_FLY);
541 setcont(def_own, att_own, FOUND_FLY);
546 * zap plane associated with plp.
547 * Damaging country is "from", damage is "dam".
548 * def_own is the country on the other side of the conflict from the plane
549 * owner. The only time def_own != from is when the interceptor is getting
552 * NOTE: This routine removes the appropriate plane element from the
553 * queue if it gets destroyed. That means that the caller must assume
554 * that the current queue pointer is invalid on return from the ac_planedamage
555 * call. (this has caused bugs in the past)
558 ac_planedamage(struct plist *plp, natid from, int dam, natid other,
559 int checkabort, int show, char *mesg)
569 plane_owner = pp->pln_own;
571 sprintf(dmess, " no damage");
576 memset(dmess, 0, sizeof(dmess));
580 if (eff < PLANE_MINEFF) {
581 sprintf(dmess, " shot down");
583 } else if (eff < 80 && chance((80 - eff) / 100.0) && checkabort) {
584 sprintf(dmess, " aborted @%2d%%", eff);
586 } else if (show == 0) {
587 sprintf(dmess, " cleared");
590 if ((plp->pcp->pl_flags & P_M) == 0) {
592 PR(plane_owner, " %s %s takes %d%s.\n",
593 cname(pp->pln_own), prplane(pp), dam, dmess);
595 PR(other, " %s %s takes %d%s.\n",
596 cname(pp->pln_own), prplane(pp), dam, dmess);
599 if (show && checkabort == 1) {
600 PR(plane_owner, "\n");
606 pp->pln_mobil -= MIN(32 + pp->pln_mobil, dam / 2);
608 if (disp == 1 && from != 0 && (plp->pcp->pl_flags & P_M) == 0)
609 nreport(from, N_DOWN_PLANE, pp->pln_own, 1);
612 putplane(pp->pln_uid, pp);
617 ac_doflak(struct emp_qelem *list, struct sctstr *from)
623 plp = (struct plist *)list->q_forw;
624 plane_owner = plp->plane.pln_own;
626 gun = MIN(FLAK_GUN_MAX, from->sct_item[I_GUN]);
627 gun = roundavg(tfact(from->sct_own, 2.0 * gun));
629 PR(plane_owner, "firing %d flak guns in %s...\n",
630 gun, xyas(from->sct_x, from->sct_y, plane_owner));
631 if (from->sct_own != 0)
632 PR(from->sct_own, "firing %d flak guns in %s...\n",
633 gun, xyas(from->sct_x, from->sct_y, from->sct_own));
634 ac_fireflak(list, from->sct_own, gun);
639 ac_shipflak(struct emp_qelem *list, coord x, coord y)
644 double flak, total, ngun;
651 plp = (struct plist *)list->q_forw;
652 plane_owner = plp->plane.pln_own;
655 snxtitem_xy(&ni, EF_SHIP, x, y);
656 while (!QEMPTY(list) && nxtitem(&ni, &ship)) {
657 if (ship.shp_own == 0 || ship.shp_own == plane_owner)
659 mcp = &mchr[(int)ship.shp_type];
660 if (mcp->m_flags & M_SUB)
662 rel = getrel(getnatp(ship.shp_own), plane_owner);
665 gun = shp_usable_guns(&ship);
668 flak = gun * (ship.shp_effic / 100.0);
670 total += techfact(ship.shp_tech, flak * 2.0);
672 PR(ship.shp_own, "firing %.0f flak guns from %s...\n",
673 flak, prship(&ship));
677 /* Limit to FLAK_GUN_MAX guns of average tech factor */
678 if (ngun > FLAK_GUN_MAX)
679 total *= FLAK_GUN_MAX / ngun;
681 gun = roundavg(total);
683 PR(plane_owner, "Flak! Ships firing %d flak guns...\n", gun);
684 ac_fireflak(list, from, gun);
689 ac_landflak(struct emp_qelem *list, coord x, coord y)
694 double flak, total, ngun;
701 plp = (struct plist *)list->q_forw;
702 plane_owner = plp->plane.pln_own;
705 snxtitem_xy(&ni, EF_LAND, x, y);
706 while (!QEMPTY(list) && nxtitem(&ni, &land)) {
707 if (land.lnd_own == 0 || land.lnd_own == plane_owner)
709 lcp = &lchr[(int)land.lnd_type];
710 aaf = lnd_aaf(&land);
711 if ((lcp->l_flags & L_FLAK) == 0 || aaf == 0)
713 if (land.lnd_ship >= 0 || land.lnd_land >= 0)
715 rel = getrel(getnatp(land.lnd_own), plane_owner);
718 flak = aaf * 1.5 * land.lnd_effic / 100.0;
720 total += techfact(land.lnd_tech, flak * 2.0);
722 PR(land.lnd_own, "firing flak guns from unit %s (aa rating %d)\n",
727 /* Limit to FLAK_GUN_MAX guns of average tech factor */
728 if (ngun > FLAK_GUN_MAX)
729 total *= FLAK_GUN_MAX / ngun;
731 gun = roundavg(total);
733 PR(plane_owner, "Flak! Land units firing %d flak guns...\n", gun);
734 ac_fireflak(list, from, gun);
739 * Called from shipflak, landflak, and doflak.
742 ac_fireflak(struct emp_qelem *list, natid from, int guns)
746 struct emp_qelem *qp;
747 struct emp_qelem *next;
750 for (qp = list->q_forw; qp != list; qp = next) {
752 plp = (struct plist *)qp;
753 n = ac_flak_dam(guns, pln_def(&plp->plane), plp->pcp->pl_flags);
754 ac_planedamage(plp, from, n, 0, 2, 1, msg);
759 * Calculate flak damage
762 ac_flak_dam(int guns, int def, int pl_flags)
766 /* <-7 -7 -6 -5 -4 */
767 static float flaktable[18] = { 0.132f, 0.20f, 0.20f, 0.25f, 0.30f,
768 /* -3 -2 -1 0 +1 +2 +3 +4 */
769 0.35f, 0.40f, 0.45f, 0.50f, 0.50f, 0.55f, 0.60f, 0.65f,
770 /* +5 +6 +7 +8 >+8 */
771 0.70f,0.75f, 0.80f, 0.85f, 1.1305f };
772 enum { FLAK_MAX = sizeof(flaktable)/sizeof(flaktable[0]) - 1 };
775 if ((pl_flags & P_T) == 0)
779 mult = flaktable[FLAK_MAX];
784 mult = flaktable[flak];
787 dam = (int)((roll(8) + 2) * mult);
794 * Get planes available for interception duties.
797 getilists(struct emp_qelem *list, unsigned char *rel, natid intruder)
800 struct plchrstr *pcp;
806 for (cn = 1; cn < MAXNOC; cn++) {
807 rel[cn] = getrel(getnatp(cn), intruder);
808 emp_initque(&list[cn]);
811 snxtitem_all(&ni, EF_PLANE);
812 while (nxtitem(&ni, &plane)) {
813 if (rel[plane.pln_own] > HOSTILE)
815 pcp = &plchr[(int)plane.pln_type];
816 if ((pcp->pl_flags & P_F) == 0)
818 if (plane.pln_flags & PLN_LAUNCHED)
820 if (plane.pln_mission && plane.pln_mission != MI_AIR_DEFENSE)
822 if (plane.pln_mobil <= 0)
824 if (plane.pln_effic < 40)
826 if (!pln_airbase_ok(&plane, 0, 0))
829 ip = malloc(sizeof(*ip));
831 ip->pcp = &plchr[(int)plane.pln_type];
833 emp_insque(&ip->queue, &list[plane.pln_own]);
838 do_evade(struct emp_qelem *bomb_list, struct emp_qelem *esc_list)
840 struct emp_qelem *qp;
845 for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw) {
846 plp = (struct plist *)qp;
847 if (evade > plp->pcp->pl_stealth / 100.0)
848 evade = plp->pcp->pl_stealth / 100.0;
850 for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw) {
851 plp = (struct plist *)qp;
852 if (evade > plp->pcp->pl_stealth / 100.0)
853 evade = plp->pcp->pl_stealth / 100.0;