2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2017, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire 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 3 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, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * aircombat.c: Deal with air to air combat
29 * Known contributors to this file:
31 * Thomas Ruschak, 1992
33 * Markus Armbruster, 2006-2014
51 #include "prototypes.h"
56 #define FLAK_GUN_MAX 14
58 static void ac_intercept(struct emp_qelem *, struct emp_qelem *,
59 struct emp_qelem *, natid, coord, coord, int);
60 static void ac_combat_headers(natid, natid);
61 static void ac_airtoair(struct emp_qelem *, struct emp_qelem *);
62 static void ac_dog(struct plist *, struct plist *);
63 static void ac_putplane(struct plist *, int);
64 static void ac_doflak(struct emp_qelem *, struct sctstr *);
65 static void ac_landflak(struct emp_qelem *, coord, coord);
66 static void ac_shipflak(struct emp_qelem *, coord, coord);
67 static void ac_fireflak(struct emp_qelem *, natid, int);
68 static void getilists(struct emp_qelem *, enum relations[], natid);
69 static int do_evade(struct emp_qelem *, struct emp_qelem *);
72 ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
73 coord x, coord y, char *path, int mission_flags)
77 unsigned char gotships[MAXNOC];
78 unsigned char gotlands[MAXNOC];
79 enum relations rel[MAXNOC];
82 struct emp_qelem ilist[MAXNOC];
91 struct shiplist *head = NULL;
93 /* We want to only intercept once per sector per owner. So, if we overfly
94 a sector, and then overfly some land units or ships, we don't want to
95 potentially intercept 3 times. */
97 plp = (struct plist *)bomb_list->q_forw;
98 plane_owner = plp->plane.pln_own;
100 memset(overfly, 0, sizeof(overfly));
101 getilists(ilist, rel, plane_owner);
103 if (CANT_HAPPEN(mission_flags && plane_owner != player->cnum))
106 if (mission_flags & PM_R) {
107 flags = pln_caps(bomb_list);
109 pr("\nSPY Plane report\n");
112 } else if (flags & P_A) {
113 pr("\nAnti-Sub Patrol report\n");
115 pr("\nReconnaissance report\n");
121 getsect(x, y, §);
122 memset(gotships, 0, sizeof(gotships));
123 snxtitem_xy(&ni, EF_SHIP, x, y);
124 while (nxtitem(&ni, &ship)) {
125 if (mchr[(int)ship.shp_type].m_flags & M_SUB)
127 gotships[ship.shp_own] = 1;
129 memset(gotlands, 0, sizeof(gotlands));
130 snxtitem_xy(&ni, EF_LAND, x, y);
131 while (nxtitem(&ni, &land)) {
132 if (land.lnd_ship >= 0 || land.lnd_land >= 0)
134 gotlands[land.lnd_own] = 1;
137 if (mission_flags & PM_R) {
138 flags = pln_caps(bomb_list);
140 setcont(player->cnum, sect.sct_own, FOUND_FLY);
141 if (sect.sct_type == SCT_WATER)
142 pr("flying over %s at %s\n",
143 dchr[sect.sct_type].d_name, xyas(x, y, player->cnum));
144 else if (flags & P_S)
145 satdisp_sect(§, flags & P_I ? 10 : 50);
147 look_at_sect(§, 25);
150 satdisp_units(sect.sct_x, sect.sct_y);
152 for (cn = 1; cn < MAXNOC; cn++) {
153 if (cn == player->cnum)
156 pr("Flying over %s ships in %s\n",
157 cname(cn), xyas(x, y, player->cnum));
159 pr("Flying over %s land units in %s\n",
160 cname(cn), xyas(x, y, player->cnum));
164 mpr(plane_owner, "flying over %s at %s\n",
165 dchr[sect.sct_type].d_name, xyas(x, y, plane_owner));
166 changed += map_set(plane_owner, sect.sct_x, sect.sct_y,
167 dchr[sect.sct_type].d_mnem, 0);
169 evaded = do_evade(bomb_list, esc_list);
171 overfly[sect.sct_own]++;
172 for (cn = 1; cn < MAXNOC; cn++) {
173 if (rel[cn] == ALLIED)
175 if (cn != sect.sct_own && !gotships[cn] && !gotlands[cn])
177 mpr(cn, "%s planes spotted over %s\n",
178 cname(plane_owner), xyas(x, y, cn));
180 setcont(cn, plane_owner, FOUND_FLY);
184 if (rel[sect.sct_own] <= HOSTILE)
185 ac_doflak(bomb_list, §);
186 /* If bombers left, fire flak from units and ships */
187 if (!QEMPTY(bomb_list))
188 ac_landflak(bomb_list, x, y);
189 if (!QEMPTY(bomb_list))
190 ac_shipflak(bomb_list, x, y);
191 /* mission planes aborted due to flak -- don't send escorts */
192 if (QEMPTY(bomb_list))
195 for (cn = 1; cn < MAXNOC && !QEMPTY(bomb_list); cn++) {
196 if (rel[cn] > HOSTILE)
198 ac_intercept(bomb_list, esc_list, &ilist[cn], cn, x, y,
200 || gotships[cn] || gotlands[cn]));
204 if (mission_flags & PM_R) {
205 flags = pln_caps(bomb_list);
206 if (sect.sct_type == SCT_WATER && mission_flags & PM_S)
207 plane_sweep(bomb_list, x, y);
208 if (sect.sct_type == SCT_WATER && flags & P_A)
209 plane_sona(bomb_list, x, y, &head);
213 if (!dir || QEMPTY(bomb_list) || (val = diridx(dir)) == DIR_STOP)
215 x = xnorm(x + diroff[val][0]);
216 y = ynorm(y + diroff[val][1]);
219 /* Let's report all of the overflights even if aborted */
220 for (cn = 1; cn < MAXNOC; cn++) {
221 if (overfly[cn] > 0 && rel[cn] != ALLIED)
222 nreport(plane_owner, N_OVFLY_SECT, cn, overfly[cn]);
224 /* If the map changed, update it */
226 writemap(plane_owner);
228 free_shiplist(&head);
229 for (cn = 1; cn < MAXNOC; cn++)
234 sam_intercept(struct emp_qelem *att_list, struct emp_qelem *def_list,
235 natid def_own, natid plane_owner, coord x, coord y,
238 struct emp_qelem *aqp;
239 struct emp_qelem *anext;
240 struct emp_qelem *dqp;
241 struct emp_qelem *dnext;
247 for (aqp = att_list->q_forw,
248 dqp = def_list->q_forw;
249 aqp != att_list && dqp != def_list; aqp = anext) {
251 aplp = (struct plist *)aqp;
252 if (aplp->pcp->pl_cost < 1000)
254 for (; dqp != def_list; dqp = dnext) {
256 dplp = (struct plist *)dqp;
258 if (!(dplp->pcp->pl_flags & P_M))
260 if (only_mission && !pp->pln_mission)
262 if (pp->pln_range < mapdist(x, y, pp->pln_x, pp->pln_y))
265 && pp->pln_radius < mapdist(x, y, pp->pln_opx, pp->pln_opy))
267 if (CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED)
268 || mission_pln_equip(dplp, NULL, 0) < 0) {
273 pp->pln_flags |= PLN_LAUNCHED;
274 putplane(pp->pln_uid, pp);
277 mpr(plane_owner, "%s launches SAMs!\n", cname(def_own));
278 mpr(def_own, "Launching SAMs at %s planes over %s!\n",
279 cname(plane_owner), xyas(x, y, def_own));
280 ac_combat_headers(plane_owner, def_own);
288 mpr(plane_owner, "\n");
294 ac_intercept(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
295 struct emp_qelem *def_list, natid def_own, coord x, coord y,
301 struct emp_qelem *next;
302 struct emp_qelem *qp;
303 struct emp_qelem int_list;
308 plp = (struct plist *)bomb_list->q_forw;
309 plane_owner = plp->plane.pln_own;
311 sam_intercept(bomb_list, def_list, def_own, plane_owner, x, y,
313 sam_intercept(esc_list, def_list, def_own, plane_owner, x, y,
316 att_count = emp_quelen(bomb_list) + emp_quelen(esc_list);
320 emp_initque(&int_list);
322 for (qp = def_list->q_forw; qp != def_list; qp = next) {
324 plp = (struct plist *)qp;
326 /* SAMs interdict separately */
327 if (plp->pcp->pl_flags & P_M)
329 if (only_mission && !pp->pln_mission)
331 dist = mapdist(x, y, pp->pln_x, pp->pln_y) * 2;
332 if (pp->pln_range < dist)
335 && pp->pln_radius < mapdist(x, y, pp->pln_opx, pp->pln_opy))
337 if (CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED)
338 || mission_pln_equip(plp, NULL, 0) < 0) {
343 /* got one; delete from def_list, add to int_list */
345 emp_insque(qp, &int_list);
346 pp->pln_flags |= PLN_LAUNCHED;
347 pp->pln_mobil -= pln_mobcost(dist, pp, 0);
348 putplane(pp->pln_uid, pp);
350 if (icount > att_count)
355 mpr(plane_owner, "%d %s fighter%s rising to intercept!\n",
356 icount, cname(def_own), icount == 1 ? " is" : "s are");
357 mpr(def_own, "%d fighter%s intercepting %s planes over %s!\n",
358 icount, icount == 1 ? " is" : "s are", cname(plane_owner),
359 xyas(x, y, def_own));
360 ac_combat_headers(plane_owner, def_own);
361 ac_airtoair(esc_list, &int_list);
362 ac_airtoair(bomb_list, &int_list);
363 mpr(plane_owner, "\n");
369 ac_combat_headers(natid plane_owner, natid def_own)
372 " %-10.10s %-10.10s strength int odds damage results\n",
373 cname(plane_owner), cname(def_own));
375 " %-10.10s %-10.10s strength int odds damage results\n",
376 cname(def_own), cname(plane_owner));
383 ac_airtoair(struct emp_qelem *att_list, struct emp_qelem *int_list)
385 struct plist *attacker;
386 struct plist *interceptor;
387 struct emp_qelem *att;
388 struct emp_qelem *in;
391 struct emp_qelem *att_next;
392 struct emp_qelem *in_next;
394 att = att_list->q_forw;
395 in = int_list->q_forw;
398 if (QEMPTY(att_list) || QEMPTY(int_list)) {
402 while (more_att || more_int) {
403 in_next = in->q_forw;
404 att_next = att->q_forw;
405 attacker = (struct plist *)att;
406 interceptor = (struct plist *)in;
407 ac_dog(attacker, interceptor);
410 if (att == att_list) {
412 if (QEMPTY(att_list))
417 if (in == int_list) {
419 if (QEMPTY(int_list))
428 ac_dog_report(natid to, int intensity, double odds,
429 struct plist *p1, int val1, int dam1, char *dam_mesg1,
430 struct plist *p2, int val2, int dam2, char *dam_mesg2)
432 mpr(to, " %3.3s #%-4d %3.3s #%-4d %3d/%-3d %3d %3.2f %3d/%-3d"
433 "%-13.13s %-13.13s\n",
434 p1->pcp->pl_name, p1->plane.pln_uid,
435 p2->pcp->pl_name, p2->plane.pln_uid,
436 val1, val2, intensity, odds, dam1, dam2,
437 dam_mesg1, dam_mesg2);
441 ac_dog(struct plist *ap, struct plist *dp)
446 natid att_own, def_own;
447 int adam, ddam, adisp, ddisp;
448 char adam_mesg[14], ddam_mesg[14];
450 att_own = ap->plane.pln_own;
451 def_own = dp->plane.pln_own;
453 att = pln_att(&ap->plane);
455 att = pln_def(&ap->plane);
456 att = att * ap->plane.pln_effic / 100;
457 att = MAX(att, ap->pcp->pl_def / 2);
459 def = pln_def(&dp->plane) * dp->plane.pln_effic / 100;
460 def = MAX(def, dp->pcp->pl_def / 2);
462 if ((ap->pcp->pl_flags & P_F) && ap->load != 0)
464 if ((dp->pcp->pl_flags & P_F) && dp->load != 0)
466 att += ap->pcp->pl_stealth / 25.0;
467 def += dp->pcp->pl_stealth / 25.0;
476 odds = ((double)att / ((double)def + (double)att));
479 intensity = roll(20) + roll(20) + roll(20) + roll(20) + 1;
483 for (i = 0; i < intensity; i++) {
486 if (dp->plane.pln_effic - ddam < PLANE_MINEFF)
490 if (ap->plane.pln_effic - adam < PLANE_MINEFF)
495 if (dp->pcp->pl_flags & P_M)
498 adisp = ac_damage_plane(&ap->plane, def_own, adam, 0, adam_mesg);
499 ddisp = ac_damage_plane(&dp->plane, att_own, ddam, 0, ddam_mesg);
500 ac_dog_report(att_own, intensity, odds,
501 ap, att, adam, adam_mesg,
502 dp, def, ddam, ddam_mesg);
503 ac_dog_report(def_own, intensity, odds,
504 dp, def, ddam, ddam_mesg,
505 ap, att, adam, adam_mesg);
506 ac_putplane(ap, adisp);
507 ac_putplane(dp, ddisp);
510 setcont(att_own, def_own, FOUND_FLY);
511 setcont(def_own, att_own, FOUND_FLY);
516 ac_damage_plane(struct plnstr *pp, natid from, int dam, int flak,
524 snprintf(mesg, 14, " no damage");
528 eff = pp->pln_effic - dam;
533 if (eff < PLANE_MINEFF) {
534 snprintf(mesg, 14, " shot down");
536 } else if (eff < 80 && chance((80 - eff) / 100.0)) {
537 snprintf(mesg, 14, " aborted @%2d%%", eff);
540 snprintf(mesg, 14, " cleared");
543 pp->pln_mobil -= MIN(32 + pp->pln_mobil, dam / 2);
545 if (disp == 1 && from != 0 && !(plchr[pp->pln_type].pl_flags & P_M))
546 nreport(from, N_DOWN_PLANE, pp->pln_own, 1);
551 * NOTE: This routine may remove the appropriate plane element from the
552 * queue if it gets destroyed. That means that the caller must assume
553 * that the current queue pointer is invalid on return from the
554 * call. (this has caused bugs in the past)
557 ac_putplane(struct plist *plp, int disp)
562 putplane(plp->plane.pln_uid, &plp->plane);
566 ac_doflak(struct emp_qelem *list, struct sctstr *from)
572 plp = (struct plist *)list->q_forw;
573 plane_owner = plp->plane.pln_own;
575 gun = MIN(FLAK_GUN_MAX, from->sct_item[I_GUN]);
576 gun = roundavg(tfact(from->sct_own, 2.0 * gun));
578 mpr(plane_owner, "firing %d flak guns in %s...\n",
579 gun, xyas(from->sct_x, from->sct_y, plane_owner));
580 mpr(from->sct_own, "firing %d flak guns in %s...\n",
581 gun, xyas(from->sct_x, from->sct_y, from->sct_own));
582 ac_fireflak(list, from->sct_own, gun);
587 ac_shipflak(struct emp_qelem *list, coord x, coord y)
592 double flak, total, ngun;
598 plp = (struct plist *)list->q_forw;
599 plane_owner = plp->plane.pln_own;
602 snxtitem_xy(&ni, EF_SHIP, x, y);
603 while (!QEMPTY(list) && nxtitem(&ni, &ship)) {
604 if (ship.shp_own == 0 || ship.shp_own == plane_owner)
606 mcp = &mchr[(int)ship.shp_type];
607 if (mcp->m_flags & M_SUB)
609 if (relations_with(ship.shp_own, plane_owner) > HOSTILE)
611 gun = shp_usable_guns(&ship);
614 flak = gun * (ship.shp_effic / 100.0);
616 total += techfact(ship.shp_tech, flak * 2.0);
618 mpr(ship.shp_own, "firing %.0f flak guns from %s...\n",
619 flak, prship(&ship));
623 /* Limit to FLAK_GUN_MAX guns of average tech factor */
624 if (ngun > FLAK_GUN_MAX)
625 total *= FLAK_GUN_MAX / ngun;
627 gun = roundavg(total);
629 mpr(plane_owner, "Flak! Ships firing %d flak guns...\n", gun);
630 ac_fireflak(list, from, gun);
635 ac_landflak(struct emp_qelem *list, coord x, coord y)
640 double flak, total, ngun;
646 plp = (struct plist *)list->q_forw;
647 plane_owner = plp->plane.pln_own;
650 snxtitem_xy(&ni, EF_LAND, x, y);
651 while (!QEMPTY(list) && nxtitem(&ni, &land)) {
652 if (land.lnd_own == 0 || land.lnd_own == plane_owner)
654 lcp = &lchr[(int)land.lnd_type];
655 aaf = lnd_aaf(&land);
656 if ((lcp->l_flags & L_FLAK) == 0 || aaf == 0)
658 if (land.lnd_ship >= 0 || land.lnd_land >= 0)
660 if (relations_with(land.lnd_own, plane_owner) > HOSTILE)
662 flak = aaf * 1.5 * land.lnd_effic / 100.0;
664 total += techfact(land.lnd_tech, flak * 2.0);
666 mpr(land.lnd_own, "firing flak guns from unit %s (AA rating %d)\n",
671 /* Limit to FLAK_GUN_MAX guns of average tech factor */
672 if (ngun > FLAK_GUN_MAX)
673 total *= FLAK_GUN_MAX / ngun;
675 gun = roundavg(total);
677 mpr(plane_owner, "Flak! Land units firing %d flak guns...\n", gun);
678 ac_fireflak(list, from, gun);
683 * Called from shipflak, landflak, and doflak.
686 ac_fireflak(struct emp_qelem *list, natid from, int guns)
690 struct emp_qelem *qp;
691 struct emp_qelem *next;
694 for (qp = list->q_forw; qp != list; qp = next) {
696 plp = (struct plist *)qp;
697 n = ac_flak_dam(guns, pln_def(&plp->plane), plp->pcp->pl_flags);
698 disp = ac_damage_plane(&plp->plane, from, n, 1, msg);
699 mpr(plp->plane.pln_own, " %s takes %d%s%s.\n",
700 prplane(&plp->plane), n, *msg ? " --" : "", msg);
701 ac_putplane(plp, disp);
706 * Calculate flak damage
709 ac_flak_dam(int guns, int def, int pl_flags)
713 /* <-7 -7 -6 -5 -4 */
714 static float flaktable[18] = { 0.132f, 0.20f, 0.20f, 0.25f, 0.30f,
715 /* -3 -2 -1 0 +1 +2 +3 +4 */
716 0.35f, 0.40f, 0.45f, 0.50f, 0.50f, 0.55f, 0.60f, 0.65f,
717 /* +5 +6 +7 +8 >+8 */
718 0.70f,0.75f, 0.80f, 0.85f, 1.1305f };
719 enum { FLAK_MAX = sizeof(flaktable)/sizeof(flaktable[0]) - 1 };
722 if ((pl_flags & P_T) == 0)
726 mult = flaktable[FLAK_MAX];
731 mult = flaktable[flak];
734 dam = (int)((roll(8) + 2) * mult);
741 * Get planes available for interception duties.
744 getilists(struct emp_qelem *list, enum relations rel[], natid intruder)
747 struct plchrstr *pcp;
753 for (cn = 1; cn < MAXNOC; cn++) {
754 rel[cn] = relations_with(cn, intruder);
755 emp_initque(&list[cn]);
758 snxtitem_all(&ni, EF_PLANE);
759 while (nxtitem(&ni, &plane)) {
760 if (rel[plane.pln_own] > HOSTILE)
762 pcp = &plchr[(int)plane.pln_type];
763 if ((pcp->pl_flags & P_F) == 0)
765 if (plane.pln_flags & PLN_LAUNCHED)
767 if (plane.pln_mission && plane.pln_mission != MI_AIR_DEFENSE)
769 if (plane.pln_mobil <= 0)
771 if (plane.pln_effic < 40)
774 if (ontradingblock(EF_PLANE, &plane))
777 if (!pln_airbase_ok(&plane, 0, 0))
780 ip = malloc(sizeof(*ip));
782 ip->pstage = PLG_HEALTHY;
783 ip->pcp = &plchr[(int)plane.pln_type];
785 emp_insque(&ip->queue, &list[plane.pln_own]);
790 do_evade(struct emp_qelem *bomb_list, struct emp_qelem *esc_list)
792 struct emp_qelem *qp;
797 for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw) {
798 plp = (struct plist *)qp;
799 if (evade > plp->pcp->pl_stealth / 100.0)
800 evade = plp->pcp->pl_stealth / 100.0;
802 for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw) {
803 plp = (struct plist *)qp;
804 if (evade > plp->pcp->pl_stealth / 100.0)
805 evade = plp->pcp->pl_stealth / 100.0;