2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2014, 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
52 #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, int);
61 static void ac_combat_headers(natid, natid);
62 static void ac_airtoair(struct emp_qelem *, struct emp_qelem *);
63 static void ac_dog(struct plist *, struct plist *);
64 static void ac_putplane(struct plist *, 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 getilists(struct emp_qelem *, unsigned char *, natid);
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)
78 unsigned char gotships[MAXNOC];
79 unsigned char gotlands[MAXNOC];
80 unsigned char rel[MAXNOC];
83 struct emp_qelem ilist[MAXNOC];
92 struct shiplist *head = NULL;
94 /* We want to only intercept once per sector per owner. So, if we overfly
95 a sector, and then overfly some land units or ships, we don't want to
96 potentially intercept 3 times. */
98 plp = (struct plist *)bomb_list->q_forw;
99 plane_owner = plp->plane.pln_own;
101 memset(overfly, 0, sizeof(overfly));
102 getilists(ilist, rel, plane_owner);
104 if (CANT_HAPPEN(mission_flags && plane_owner != player->cnum))
107 if (mission_flags & PM_R) {
108 flags = pln_caps(bomb_list);
110 pr("\nSPY Plane report\n");
113 } else if (flags & P_A) {
114 pr("\nAnti-Sub Patrol report\n");
116 pr("\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(player->cnum, sect.sct_own, FOUND_FLY);
142 if (sect.sct_type == SCT_WATER)
143 pr("flying over %s at %s\n",
144 dchr[sect.sct_type].d_name, xyas(x, y, player->cnum));
145 else if (flags & P_S)
146 satdisp_sect(§, flags & P_I ? 10 : 50);
148 look_at_sect(§, 25);
151 satdisp_units(sect.sct_x, sect.sct_y);
153 for (cn = 1; cn < MAXNOC; cn++) {
154 if (cn == player->cnum)
157 pr("Flying over %s ships in %s\n",
158 cname(cn), xyas(x, y, player->cnum));
160 pr("Flying over %s land units in %s\n",
161 cname(cn), xyas(x, y, player->cnum));
165 mpr(plane_owner, "flying over %s at %s\n",
166 dchr[sect.sct_type].d_name, xyas(x, y, plane_owner));
167 changed += map_set(plane_owner, sect.sct_x, sect.sct_y,
168 dchr[sect.sct_type].d_mnem, 0);
170 evaded = do_evade(bomb_list, esc_list);
172 overfly[sect.sct_own]++;
173 for (cn = 1; cn < MAXNOC; cn++) {
174 if (rel[cn] == ALLIED)
176 if (cn != sect.sct_own && !gotships[cn] && !gotlands[cn])
178 mpr(cn, "%s planes spotted over %s\n",
179 cname(plane_owner), xyas(x, y, cn));
181 setcont(cn, plane_owner, FOUND_FLY);
185 if (rel[sect.sct_own] <= HOSTILE)
186 ac_doflak(bomb_list, §);
187 /* If bombers left, fire flak from units and ships */
188 if (!QEMPTY(bomb_list))
189 ac_landflak(bomb_list, x, y);
190 if (!QEMPTY(bomb_list))
191 ac_shipflak(bomb_list, x, y);
192 /* mission planes aborted due to flak -- don't send escorts */
193 if (QEMPTY(bomb_list))
196 for (cn = 1; cn < MAXNOC && !QEMPTY(bomb_list); cn++) {
197 if (rel[cn] > HOSTILE)
199 ac_intercept(bomb_list, esc_list, &ilist[cn], cn, x, y,
201 || gotships[cn] || gotlands[cn]));
205 if (mission_flags & PM_R) {
206 flags = pln_caps(bomb_list);
207 if (sect.sct_type == SCT_WATER && mission_flags & PM_S)
208 plane_sweep(bomb_list, x, y);
209 if (sect.sct_type == SCT_WATER && flags & P_A)
210 plane_sona(bomb_list, x, y, &head);
214 if (!dir || QEMPTY(bomb_list) || (val = diridx(dir)) == DIR_STOP)
216 x = xnorm(x + diroff[val][0]);
217 y = ynorm(y + diroff[val][1]);
220 /* Let's report all of the overflights even if aborted */
221 for (cn = 1; cn < MAXNOC; cn++) {
222 if (overfly[cn] > 0 && rel[cn] != ALLIED)
223 nreport(plane_owner, N_OVFLY_SECT, cn, overfly[cn]);
225 /* If the map changed, update it */
227 writemap(plane_owner);
229 free_shiplist(&head);
230 for (cn = 1; cn < MAXNOC; cn++)
235 sam_intercept(struct emp_qelem *att_list, struct emp_qelem *def_list,
236 natid def_own, natid plane_owner, coord x, coord y,
239 struct emp_qelem *aqp;
240 struct emp_qelem *anext;
241 struct emp_qelem *dqp;
242 struct emp_qelem *dnext;
248 for (aqp = att_list->q_forw,
249 dqp = def_list->q_forw;
250 aqp != att_list && dqp != def_list; aqp = anext) {
252 aplp = (struct plist *)aqp;
253 if (aplp->pcp->pl_cost < 1000)
255 for (; dqp != def_list; dqp = dnext) {
257 dplp = (struct plist *)dqp;
259 if (!(dplp->pcp->pl_flags & P_M))
261 if (only_mission && !pp->pln_mission)
263 if (pp->pln_range < mapdist(x, y, pp->pln_x, pp->pln_y))
266 && pp->pln_radius < mapdist(x, y, pp->pln_opx, pp->pln_opy))
268 if (CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED)
269 || mission_pln_equip(dplp, NULL, 0) < 0) {
274 pp->pln_flags |= PLN_LAUNCHED;
275 putplane(pp->pln_uid, pp);
278 mpr(plane_owner, "%s launches SAMs!\n", cname(def_own));
279 mpr(def_own, "Launching SAMs at %s planes over %s!\n",
280 cname(plane_owner), xyas(x, y, def_own));
281 ac_combat_headers(plane_owner, def_own);
289 mpr(plane_owner, "\n");
295 ac_intercept(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
296 struct emp_qelem *def_list, natid def_own, coord x, coord y,
302 struct emp_qelem *next;
303 struct emp_qelem *qp;
304 struct emp_qelem int_list;
309 plp = (struct plist *)bomb_list->q_forw;
310 plane_owner = plp->plane.pln_own;
312 sam_intercept(bomb_list, def_list, def_own, plane_owner, x, y,
314 sam_intercept(esc_list, def_list, def_own, plane_owner, x, y,
317 att_count = emp_quelen(bomb_list) + emp_quelen(esc_list);
321 emp_initque(&int_list);
323 for (qp = def_list->q_forw; qp != def_list; qp = next) {
325 plp = (struct plist *)qp;
327 /* SAMs interdict separately */
328 if (plp->pcp->pl_flags & P_M)
330 if (only_mission && !pp->pln_mission)
332 dist = mapdist(x, y, pp->pln_x, pp->pln_y) * 2;
333 if (pp->pln_range < dist)
336 && pp->pln_radius < mapdist(x, y, pp->pln_opx, pp->pln_opy))
338 if (CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED)
339 || mission_pln_equip(plp, NULL, 0) < 0) {
344 /* got one; delete from def_list, add to int_list */
346 emp_insque(qp, &int_list);
347 pp->pln_flags |= PLN_LAUNCHED;
348 pp->pln_mobil -= pln_mobcost(dist, pp, 0);
349 putplane(pp->pln_uid, pp);
351 if (icount > att_count)
356 mpr(plane_owner, "%d %s fighter%s rising to intercept!\n",
357 icount, cname(def_own), icount == 1 ? " is" : "s are");
358 mpr(def_own, "%d fighter%s intercepting %s planes over %s!\n",
359 icount, icount == 1 ? " is" : "s are", cname(plane_owner),
360 xyas(x, y, def_own));
361 ac_combat_headers(plane_owner, def_own);
362 ac_airtoair(esc_list, &int_list);
363 ac_airtoair(bomb_list, &int_list);
364 mpr(plane_owner, "\n");
370 ac_combat_headers(natid plane_owner, natid def_own)
373 " %-10.10s %-10.10s strength int odds damage results\n",
374 cname(plane_owner), cname(def_own));
376 " %-10.10s %-10.10s strength int odds damage results\n",
377 cname(def_own), cname(plane_owner));
384 ac_airtoair(struct emp_qelem *att_list, struct emp_qelem *int_list)
386 struct plist *attacker;
387 struct plist *interceptor;
388 struct emp_qelem *att;
389 struct emp_qelem *in;
392 struct emp_qelem *att_next;
393 struct emp_qelem *in_next;
395 att = att_list->q_forw;
396 in = int_list->q_forw;
399 if (QEMPTY(att_list) || QEMPTY(int_list)) {
403 while (more_att || more_int) {
404 in_next = in->q_forw;
405 att_next = att->q_forw;
406 attacker = (struct plist *)att;
407 interceptor = (struct plist *)in;
408 ac_dog(attacker, interceptor);
411 if (att == att_list) {
413 if (QEMPTY(att_list))
418 if (in == int_list) {
420 if (QEMPTY(int_list))
429 ac_dog_report(natid to, int intensity, double odds,
430 struct plist *p1, int val1, int dam1, char *dam_mesg1,
431 struct plist *p2, int val2, int dam2, char *dam_mesg2)
433 mpr(to, " %3.3s #%-4d %3.3s #%-4d %3d/%-3d %3d %3.2f %3d/%-3d"
434 "%-13.13s %-13.13s\n",
435 p1->pcp->pl_name, p1->plane.pln_uid,
436 p2->pcp->pl_name, p2->plane.pln_uid,
437 val1, val2, intensity, odds, dam1, dam2,
438 dam_mesg1, dam_mesg2);
442 ac_dog(struct plist *ap, struct plist *dp)
447 natid att_own, def_own;
448 int adam, ddam, adisp, ddisp;
449 char adam_mesg[14], ddam_mesg[14];
451 att_own = ap->plane.pln_own;
452 def_own = dp->plane.pln_own;
454 att = pln_att(&ap->plane);
456 att = pln_def(&ap->plane);
457 att = att * ap->plane.pln_effic / 100;
458 att = MAX(att, ap->pcp->pl_def / 2);
460 def = pln_def(&dp->plane) * dp->plane.pln_effic / 100;
461 def = MAX(def, dp->pcp->pl_def / 2);
463 if ((ap->pcp->pl_flags & P_F) && ap->load != 0)
465 if ((dp->pcp->pl_flags & P_F) && dp->load != 0)
467 att += ap->pcp->pl_stealth / 25.0;
468 def += dp->pcp->pl_stealth / 25.0;
477 odds = ((double)att / ((double)def + (double)att));
480 intensity = roll(20) + roll(20) + roll(20) + roll(20) + 1;
484 for (i = 0; i < intensity; i++) {
487 if (dp->plane.pln_effic - ddam < PLANE_MINEFF)
491 if (ap->plane.pln_effic - adam < PLANE_MINEFF)
496 if (dp->pcp->pl_flags & P_M)
499 adisp = ac_damage_plane(&ap->plane, def_own, adam, 0, adam_mesg);
500 ddisp = ac_damage_plane(&dp->plane, att_own, ddam, 0, ddam_mesg);
501 ac_dog_report(att_own, intensity, odds,
502 ap, att, adam, adam_mesg,
503 dp, def, ddam, ddam_mesg);
504 ac_dog_report(def_own, intensity, odds,
505 dp, def, ddam, ddam_mesg,
506 ap, att, adam, adam_mesg);
507 ac_putplane(ap, adisp);
508 ac_putplane(dp, ddisp);
511 setcont(att_own, def_own, FOUND_FLY);
512 setcont(def_own, att_own, FOUND_FLY);
517 ac_damage_plane(struct plnstr *pp, natid from, int dam, int flak,
525 snprintf(mesg, 14, " no damage");
529 eff = pp->pln_effic - dam;
534 if (eff < PLANE_MINEFF) {
535 snprintf(mesg, 14, " shot down");
537 } else if (eff < 80 && chance((80 - eff) / 100.0)) {
538 snprintf(mesg, 14, " aborted @%2d%%", eff);
541 snprintf(mesg, 14, " cleared");
544 pp->pln_mobil -= MIN(32 + pp->pln_mobil, dam / 2);
546 if (disp == 1 && from != 0 && !(plchr[pp->pln_type].pl_flags & P_M))
547 nreport(from, N_DOWN_PLANE, pp->pln_own, 1);
552 * NOTE: This routine may remove 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
555 * call. (this has caused bugs in the past)
558 ac_putplane(struct plist *plp, int disp)
563 putplane(plp->plane.pln_uid, &plp->plane);
567 ac_doflak(struct emp_qelem *list, struct sctstr *from)
573 plp = (struct plist *)list->q_forw;
574 plane_owner = plp->plane.pln_own;
576 gun = MIN(FLAK_GUN_MAX, from->sct_item[I_GUN]);
577 gun = roundavg(tfact(from->sct_own, 2.0 * gun));
579 mpr(plane_owner, "firing %d flak guns in %s...\n",
580 gun, xyas(from->sct_x, from->sct_y, plane_owner));
581 mpr(from->sct_own, "firing %d flak guns in %s...\n",
582 gun, xyas(from->sct_x, from->sct_y, from->sct_own));
583 ac_fireflak(list, from->sct_own, gun);
588 ac_shipflak(struct emp_qelem *list, coord x, coord y)
593 double flak, total, ngun;
599 plp = (struct plist *)list->q_forw;
600 plane_owner = plp->plane.pln_own;
603 snxtitem_xy(&ni, EF_SHIP, x, y);
604 while (!QEMPTY(list) && nxtitem(&ni, &ship)) {
605 if (ship.shp_own == 0 || ship.shp_own == plane_owner)
607 mcp = &mchr[(int)ship.shp_type];
608 if (mcp->m_flags & M_SUB)
610 if (relations_with(ship.shp_own, plane_owner) > HOSTILE)
612 gun = shp_usable_guns(&ship);
615 flak = gun * (ship.shp_effic / 100.0);
617 total += techfact(ship.shp_tech, flak * 2.0);
619 mpr(ship.shp_own, "firing %.0f flak guns from %s...\n",
620 flak, prship(&ship));
624 /* Limit to FLAK_GUN_MAX guns of average tech factor */
625 if (ngun > FLAK_GUN_MAX)
626 total *= FLAK_GUN_MAX / ngun;
628 gun = roundavg(total);
630 mpr(plane_owner, "Flak! Ships firing %d flak guns...\n", gun);
631 ac_fireflak(list, from, gun);
636 ac_landflak(struct emp_qelem *list, coord x, coord y)
641 double flak, total, ngun;
647 plp = (struct plist *)list->q_forw;
648 plane_owner = plp->plane.pln_own;
651 snxtitem_xy(&ni, EF_LAND, x, y);
652 while (!QEMPTY(list) && nxtitem(&ni, &land)) {
653 if (land.lnd_own == 0 || land.lnd_own == plane_owner)
655 lcp = &lchr[(int)land.lnd_type];
656 aaf = lnd_aaf(&land);
657 if ((lcp->l_flags & L_FLAK) == 0 || aaf == 0)
659 if (land.lnd_ship >= 0 || land.lnd_land >= 0)
661 if (relations_with(land.lnd_own, plane_owner) > HOSTILE)
663 flak = aaf * 1.5 * land.lnd_effic / 100.0;
665 total += techfact(land.lnd_tech, flak * 2.0);
667 mpr(land.lnd_own, "firing flak guns from unit %s (aa rating %d)\n",
672 /* Limit to FLAK_GUN_MAX guns of average tech factor */
673 if (ngun > FLAK_GUN_MAX)
674 total *= FLAK_GUN_MAX / ngun;
676 gun = roundavg(total);
678 mpr(plane_owner, "Flak! Land units firing %d flak guns...\n", gun);
679 ac_fireflak(list, from, gun);
684 * Called from shipflak, landflak, and doflak.
687 ac_fireflak(struct emp_qelem *list, natid from, int guns)
691 struct emp_qelem *qp;
692 struct emp_qelem *next;
695 for (qp = list->q_forw; qp != list; qp = next) {
697 plp = (struct plist *)qp;
698 n = ac_flak_dam(guns, pln_def(&plp->plane), plp->pcp->pl_flags);
699 disp = ac_damage_plane(&plp->plane, from, n, 1, msg);
700 mpr(plp->plane.pln_own, " %s takes %d%s%s.\n",
701 prplane(&plp->plane), n, *msg ? " --" : "", msg);
702 ac_putplane(plp, disp);
707 * Calculate flak damage
710 ac_flak_dam(int guns, int def, int pl_flags)
714 /* <-7 -7 -6 -5 -4 */
715 static float flaktable[18] = { 0.132f, 0.20f, 0.20f, 0.25f, 0.30f,
716 /* -3 -2 -1 0 +1 +2 +3 +4 */
717 0.35f, 0.40f, 0.45f, 0.50f, 0.50f, 0.55f, 0.60f, 0.65f,
718 /* +5 +6 +7 +8 >+8 */
719 0.70f,0.75f, 0.80f, 0.85f, 1.1305f };
720 enum { FLAK_MAX = sizeof(flaktable)/sizeof(flaktable[0]) - 1 };
723 if ((pl_flags & P_T) == 0)
727 mult = flaktable[FLAK_MAX];
732 mult = flaktable[flak];
735 dam = (int)((roll(8) + 2) * mult);
742 * Get planes available for interception duties.
745 getilists(struct emp_qelem *list, unsigned char *rel, natid intruder)
748 struct plchrstr *pcp;
754 for (cn = 1; cn < MAXNOC; cn++) {
755 rel[cn] = relations_with(cn, intruder);
756 emp_initque(&list[cn]);
759 snxtitem_all(&ni, EF_PLANE);
760 while (nxtitem(&ni, &plane)) {
761 if (rel[plane.pln_own] > HOSTILE)
763 pcp = &plchr[(int)plane.pln_type];
764 if ((pcp->pl_flags & P_F) == 0)
766 if (plane.pln_flags & PLN_LAUNCHED)
768 if (plane.pln_mission && plane.pln_mission != MI_AIR_DEFENSE)
770 if (plane.pln_mobil <= 0)
772 if (plane.pln_effic < 40)
775 if (ontradingblock(EF_PLANE, &plane))
778 if (!pln_airbase_ok(&plane, 0, 0))
781 ip = malloc(sizeof(*ip));
783 ip->pstage = PLG_HEALTHY;
784 ip->pcp = &plchr[(int)plane.pln_type];
786 emp_insque(&ip->queue, &list[plane.pln_own]);
791 do_evade(struct emp_qelem *bomb_list, struct emp_qelem *esc_list)
793 struct emp_qelem *qp;
798 for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw) {
799 plp = (struct plist *)qp;
800 if (evade > plp->pcp->pl_stealth / 100.0)
801 evade = plp->pcp->pl_stealth / 100.0;
803 for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw) {
804 plp = (struct plist *)qp;
805 if (evade > plp->pcp->pl_stealth / 100.0)
806 evade = plp->pcp->pl_stealth / 100.0;