2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2013, 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-2012
52 #include "prototypes.h"
57 #define FLAK_GUN_MAX 14
59 static void sam_intercept(struct emp_qelem *, struct emp_qelem *,
60 natid, natid, coord, coord, int);
61 static void ac_intercept(struct emp_qelem *, struct emp_qelem *,
62 struct emp_qelem *, natid, coord, coord, int);
63 static void ac_combat_headers(natid, natid);
64 static void ac_airtoair(struct emp_qelem *, struct emp_qelem *);
65 static void ac_dog(struct plist *, struct plist *);
66 static void ac_putplane(struct plist *, 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];
94 struct shiplist *head = NULL;
96 /* We want to only intercept once per sector per owner. So, if we overfly
97 a sector, and then overfly some land units or ships, we don't want to
98 potentially intercept 3 times. */
100 plp = (struct plist *)bomb_list->q_forw;
101 plane_owner = plp->plane.pln_own;
103 memset(overfly, 0, sizeof(overfly));
104 getilists(ilist, rel, plane_owner);
106 if (CANT_HAPPEN(mission_flags && plane_owner != player->cnum))
109 if (mission_flags & PM_R) {
110 flags = pln_caps(bomb_list);
112 pr("\nSPY Plane report\n");
115 } else if (flags & P_A) {
116 pr("\nAnti-Sub Patrol report\n");
118 pr("\nReconnaissance report\n");
124 getsect(x, y, §);
125 memset(gotships, 0, sizeof(gotships));
126 snxtitem_xy(&ni, EF_SHIP, x, y);
127 while (nxtitem(&ni, &ship)) {
128 if (mchr[(int)ship.shp_type].m_flags & M_SUB)
130 gotships[ship.shp_own] = 1;
132 memset(gotlands, 0, sizeof(gotlands));
133 snxtitem_xy(&ni, EF_LAND, x, y);
134 while (nxtitem(&ni, &land)) {
135 if (land.lnd_ship >= 0 || land.lnd_land >= 0)
137 gotlands[land.lnd_own] = 1;
140 if (mission_flags & PM_R) {
141 flags = pln_caps(bomb_list);
143 setcont(player->cnum, sect.sct_own, FOUND_FLY);
144 if (sect.sct_type == SCT_WATER)
145 pr("flying over %s at %s\n",
146 dchr[sect.sct_type].d_name, xyas(x, y, player->cnum));
147 else if (flags & P_S)
148 satdisp_sect(§, flags & P_I ? 10 : 50);
150 look_at_sect(§, 25);
153 satdisp_units(sect.sct_x, sect.sct_y);
155 for (cn = 1; cn < MAXNOC; cn++) {
156 if (cn == player->cnum)
159 pr("Flying over %s ships in %s\n",
160 cname(cn), xyas(x, y, player->cnum));
162 pr("Flying over %s land units in %s\n",
163 cname(cn), xyas(x, y, player->cnum));
167 mpr(plane_owner, "flying over %s at %s\n",
168 dchr[sect.sct_type].d_name, xyas(x, y, plane_owner));
169 changed += map_set(plane_owner, sect.sct_x, sect.sct_y,
170 dchr[sect.sct_type].d_mnem, 0);
172 evaded = do_evade(bomb_list, esc_list);
174 overfly[sect.sct_own]++;
175 for (cn = 1; cn < MAXNOC; cn++) {
176 if (rel[cn] == ALLIED)
178 if (cn != sect.sct_own && !gotships[cn] && !gotlands[cn])
180 mpr(cn, "%s planes spotted over %s\n",
181 cname(plane_owner), xyas(x, y, cn));
183 setcont(cn, plane_owner, FOUND_FLY);
187 if (rel[sect.sct_own] <= HOSTILE)
188 ac_doflak(bomb_list, §);
189 /* If bombers left, fire flak from units and ships */
190 if (!QEMPTY(bomb_list))
191 ac_landflak(bomb_list, x, y);
192 if (!QEMPTY(bomb_list))
193 ac_shipflak(bomb_list, x, y);
194 /* mission planes aborted due to flak -- don't send escorts */
195 if (QEMPTY(bomb_list))
198 for (cn = 1; cn < MAXNOC && !QEMPTY(bomb_list); cn++) {
199 if (rel[cn] > HOSTILE)
201 ac_intercept(bomb_list, esc_list, &ilist[cn], cn, x, y,
203 || gotships[cn] || gotlands[cn]));
207 if (mission_flags & PM_R) {
208 flags = pln_caps(bomb_list);
209 if (sect.sct_type == SCT_WATER && mission_flags & PM_S)
210 plane_sweep(bomb_list, x, y);
211 if (sect.sct_type == SCT_WATER && flags & P_A)
212 plane_sona(bomb_list, x, y, &head);
216 if (!dir || QEMPTY(bomb_list) || (val = diridx(dir)) == DIR_STOP)
218 x = xnorm(x + diroff[val][0]);
219 y = ynorm(y + diroff[val][1]);
222 /* Let's report all of the overflights even if aborted */
223 for (cn = 1; cn < MAXNOC; cn++) {
224 if (overfly[cn] > 0 && rel[cn] != ALLIED)
225 nreport(plane_owner, N_OVFLY_SECT, cn, overfly[cn]);
227 /* If the map changed, update it */
229 writemap(plane_owner);
231 free_shiplist(&head);
232 for (cn = 1; cn < MAXNOC; cn++)
237 sam_intercept(struct emp_qelem *att_list, struct emp_qelem *def_list,
238 natid def_own, natid plane_owner, coord x, coord y,
241 struct emp_qelem *aqp;
242 struct emp_qelem *anext;
243 struct emp_qelem *dqp;
244 struct emp_qelem *dnext;
250 for (aqp = att_list->q_forw,
251 dqp = def_list->q_forw;
252 aqp != att_list && dqp != def_list; aqp = anext) {
254 aplp = (struct plist *)aqp;
255 if (aplp->pcp->pl_cost < 1000)
257 for (; dqp != def_list; dqp = dnext) {
259 dplp = (struct plist *)dqp;
261 if (!(dplp->pcp->pl_flags & P_M))
263 if (only_mission && !pp->pln_mission)
265 if (pp->pln_range < mapdist(x, y, pp->pln_x, pp->pln_y))
268 && pp->pln_radius < mapdist(x, y, pp->pln_opx, pp->pln_opy))
270 if (CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED)
271 || mission_pln_equip(dplp, NULL, 0) < 0) {
276 pp->pln_flags |= PLN_LAUNCHED;
277 putplane(pp->pln_uid, pp);
280 mpr(plane_owner, "%s launches SAMs!\n", cname(def_own));
281 mpr(def_own, "Launching SAMs at %s planes over %s!\n",
282 cname(plane_owner), xyas(x, y, def_own));
283 ac_combat_headers(plane_owner, def_own);
291 mpr(plane_owner, "\n");
297 ac_intercept(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
298 struct emp_qelem *def_list, natid def_own, coord x, coord y,
304 struct emp_qelem *next;
305 struct emp_qelem *qp;
306 struct emp_qelem int_list;
311 plp = (struct plist *)bomb_list->q_forw;
312 plane_owner = plp->plane.pln_own;
314 sam_intercept(bomb_list, def_list, def_own, plane_owner, x, y,
316 sam_intercept(esc_list, def_list, def_own, plane_owner, x, y,
320 for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw)
322 for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw)
327 emp_initque(&int_list);
329 for (qp = def_list->q_forw; qp != def_list; qp = next) {
331 plp = (struct plist *)qp;
333 /* SAMs interdict separately */
334 if (plp->pcp->pl_flags & P_M)
336 if (only_mission && !pp->pln_mission)
338 dist = mapdist(x, y, pp->pln_x, pp->pln_y) * 2;
339 if (pp->pln_range < dist)
342 && pp->pln_radius < mapdist(x, y, pp->pln_opx, pp->pln_opy))
344 if (CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED)
345 || mission_pln_equip(plp, NULL, 0) < 0) {
350 /* got one; delete from def_list, add to int_list */
352 emp_insque(qp, &int_list);
353 pp->pln_flags |= PLN_LAUNCHED;
354 pp->pln_mobil -= pln_mobcost(dist, pp, 0);
355 putplane(pp->pln_uid, pp);
357 if (icount > att_count)
362 mpr(plane_owner, "%d %s fighter%s rising to intercept!\n",
363 icount, cname(def_own), icount == 1 ? " is" : "s are");
364 mpr(def_own, "%d fighter%s intercepting %s planes over %s!\n",
365 icount, icount == 1 ? " is" : "s are", cname(plane_owner),
366 xyas(x, y, def_own));
367 ac_combat_headers(plane_owner, def_own);
368 ac_airtoair(esc_list, &int_list);
369 ac_airtoair(bomb_list, &int_list);
370 mpr(plane_owner, "\n");
376 ac_combat_headers(natid plane_owner, natid def_own)
379 " %-10.10s %-10.10s strength int odds damage results\n",
380 cname(plane_owner), cname(def_own));
382 " %-10.10s %-10.10s strength int odds damage results\n",
383 cname(def_own), cname(plane_owner));
390 ac_airtoair(struct emp_qelem *att_list, struct emp_qelem *int_list)
392 struct plist *attacker;
393 struct plist *interceptor;
394 struct emp_qelem *att;
395 struct emp_qelem *in;
398 struct emp_qelem *att_next;
399 struct emp_qelem *in_next;
401 att = att_list->q_forw;
402 in = int_list->q_forw;
405 if (QEMPTY(att_list) || QEMPTY(int_list)) {
409 while (more_att || more_int) {
410 in_next = in->q_forw;
411 att_next = att->q_forw;
412 attacker = (struct plist *)att;
413 interceptor = (struct plist *)in;
414 ac_dog(attacker, interceptor);
417 if (att == att_list) {
419 if (QEMPTY(att_list))
424 if (in == int_list) {
426 if (QEMPTY(int_list))
435 ac_dog_report(natid to, int intensity, double odds,
436 struct plist *p1, int val1, int dam1, char *dam_mesg1,
437 struct plist *p2, int val2, int dam2, char *dam_mesg2)
439 mpr(to, " %3.3s #%-4d %3.3s #%-4d %3d/%-3d %3d %3.2f %3d/%-3d"
440 "%-13.13s %-13.13s\n",
441 p1->pcp->pl_name, p1->plane.pln_uid,
442 p2->pcp->pl_name, p2->plane.pln_uid,
443 val1, val2, intensity, odds, dam1, dam2,
444 dam_mesg1, dam_mesg2);
448 ac_dog(struct plist *ap, struct plist *dp)
453 natid att_own, def_own;
454 int adam, ddam, adisp, ddisp;
455 char adam_mesg[14], ddam_mesg[14];
457 att_own = ap->plane.pln_own;
458 def_own = dp->plane.pln_own;
460 att = pln_att(&ap->plane);
462 att = pln_def(&ap->plane);
463 att = att * ap->plane.pln_effic / 100;
464 att = MAX(att, ap->pcp->pl_def / 2);
466 def = pln_def(&dp->plane) * dp->plane.pln_effic / 100;
467 def = MAX(def, dp->pcp->pl_def / 2);
469 if ((ap->pcp->pl_flags & P_F) && ap->load != 0)
471 if ((dp->pcp->pl_flags & P_F) && dp->load != 0)
473 att += ap->pcp->pl_stealth / 25.0;
474 def += dp->pcp->pl_stealth / 25.0;
483 odds = ((double)att / ((double)def + (double)att));
486 intensity = roll(20) + roll(20) + roll(20) + roll(20) + 1;
490 for (i = 0; i < intensity; i++) {
493 if (dp->plane.pln_effic - ddam < PLANE_MINEFF)
497 if (ap->plane.pln_effic - adam < PLANE_MINEFF)
502 if (dp->pcp->pl_flags & P_M)
505 adisp = ac_damage_plane(&ap->plane, def_own, adam, 0, adam_mesg);
506 ddisp = ac_damage_plane(&dp->plane, att_own, ddam, 0, ddam_mesg);
507 ac_dog_report(att_own, intensity, odds,
508 ap, att, adam, adam_mesg,
509 dp, def, ddam, ddam_mesg);
510 ac_dog_report(def_own, intensity, odds,
511 dp, def, ddam, ddam_mesg,
512 ap, att, adam, adam_mesg);
513 ac_putplane(ap, adisp);
514 ac_putplane(dp, ddisp);
517 setcont(att_own, def_own, FOUND_FLY);
518 setcont(def_own, att_own, FOUND_FLY);
523 ac_damage_plane(struct plnstr *pp, natid from, int dam, int flak,
531 snprintf(mesg, 14, " no damage");
535 eff = pp->pln_effic - dam;
540 if (eff < PLANE_MINEFF) {
541 snprintf(mesg, 14, " shot down");
543 } else if (eff < 80 && chance((80 - eff) / 100.0)) {
544 snprintf(mesg, 14, " aborted @%2d%%", eff);
547 snprintf(mesg, 14, " cleared");
550 pp->pln_mobil -= MIN(32 + pp->pln_mobil, dam / 2);
552 if (disp == 1 && from != 0 && !(plchr[pp->pln_type].pl_flags & P_M))
553 nreport(from, N_DOWN_PLANE, pp->pln_own, 1);
558 * NOTE: This routine may remove the appropriate plane element from the
559 * queue if it gets destroyed. That means that the caller must assume
560 * that the current queue pointer is invalid on return from the
561 * call. (this has caused bugs in the past)
564 ac_putplane(struct plist *plp, int disp)
569 putplane(plp->plane.pln_uid, &plp->plane);
573 ac_doflak(struct emp_qelem *list, struct sctstr *from)
579 plp = (struct plist *)list->q_forw;
580 plane_owner = plp->plane.pln_own;
582 gun = MIN(FLAK_GUN_MAX, from->sct_item[I_GUN]);
583 gun = roundavg(tfact(from->sct_own, 2.0 * gun));
585 mpr(plane_owner, "firing %d flak guns in %s...\n",
586 gun, xyas(from->sct_x, from->sct_y, plane_owner));
587 mpr(from->sct_own, "firing %d flak guns in %s...\n",
588 gun, xyas(from->sct_x, from->sct_y, from->sct_own));
589 ac_fireflak(list, from->sct_own, gun);
594 ac_shipflak(struct emp_qelem *list, coord x, coord y)
599 double flak, total, ngun;
605 plp = (struct plist *)list->q_forw;
606 plane_owner = plp->plane.pln_own;
609 snxtitem_xy(&ni, EF_SHIP, x, y);
610 while (!QEMPTY(list) && nxtitem(&ni, &ship)) {
611 if (ship.shp_own == 0 || ship.shp_own == plane_owner)
613 mcp = &mchr[(int)ship.shp_type];
614 if (mcp->m_flags & M_SUB)
616 if (relations_with(ship.shp_own, plane_owner) > HOSTILE)
618 gun = shp_usable_guns(&ship);
621 flak = gun * (ship.shp_effic / 100.0);
623 total += techfact(ship.shp_tech, flak * 2.0);
625 mpr(ship.shp_own, "firing %.0f flak guns from %s...\n",
626 flak, prship(&ship));
630 /* Limit to FLAK_GUN_MAX guns of average tech factor */
631 if (ngun > FLAK_GUN_MAX)
632 total *= FLAK_GUN_MAX / ngun;
634 gun = roundavg(total);
636 mpr(plane_owner, "Flak! Ships firing %d flak guns...\n", gun);
637 ac_fireflak(list, from, gun);
642 ac_landflak(struct emp_qelem *list, coord x, coord y)
647 double flak, total, ngun;
653 plp = (struct plist *)list->q_forw;
654 plane_owner = plp->plane.pln_own;
657 snxtitem_xy(&ni, EF_LAND, x, y);
658 while (!QEMPTY(list) && nxtitem(&ni, &land)) {
659 if (land.lnd_own == 0 || land.lnd_own == plane_owner)
661 lcp = &lchr[(int)land.lnd_type];
662 aaf = lnd_aaf(&land);
663 if ((lcp->l_flags & L_FLAK) == 0 || aaf == 0)
665 if (land.lnd_ship >= 0 || land.lnd_land >= 0)
667 if (relations_with(land.lnd_own, plane_owner) > HOSTILE)
669 flak = aaf * 1.5 * land.lnd_effic / 100.0;
671 total += techfact(land.lnd_tech, flak * 2.0);
673 mpr(land.lnd_own, "firing flak guns from unit %s (aa rating %d)\n",
678 /* Limit to FLAK_GUN_MAX guns of average tech factor */
679 if (ngun > FLAK_GUN_MAX)
680 total *= FLAK_GUN_MAX / ngun;
682 gun = roundavg(total);
684 mpr(plane_owner, "Flak! Land units firing %d flak guns...\n", gun);
685 ac_fireflak(list, from, gun);
690 * Called from shipflak, landflak, and doflak.
693 ac_fireflak(struct emp_qelem *list, natid from, int guns)
697 struct emp_qelem *qp;
698 struct emp_qelem *next;
701 for (qp = list->q_forw; qp != list; qp = next) {
703 plp = (struct plist *)qp;
704 n = ac_flak_dam(guns, pln_def(&plp->plane), plp->pcp->pl_flags);
705 disp = ac_damage_plane(&plp->plane, from, n, 1, msg);
706 mpr(plp->plane.pln_own, " %s takes %d%s%s.\n",
707 prplane(&plp->plane), n, *msg ? " --" : "", msg);
708 ac_putplane(plp, disp);
713 * Calculate flak damage
716 ac_flak_dam(int guns, int def, int pl_flags)
720 /* <-7 -7 -6 -5 -4 */
721 static float flaktable[18] = { 0.132f, 0.20f, 0.20f, 0.25f, 0.30f,
722 /* -3 -2 -1 0 +1 +2 +3 +4 */
723 0.35f, 0.40f, 0.45f, 0.50f, 0.50f, 0.55f, 0.60f, 0.65f,
724 /* +5 +6 +7 +8 >+8 */
725 0.70f,0.75f, 0.80f, 0.85f, 1.1305f };
726 enum { FLAK_MAX = sizeof(flaktable)/sizeof(flaktable[0]) - 1 };
729 if ((pl_flags & P_T) == 0)
733 mult = flaktable[FLAK_MAX];
738 mult = flaktable[flak];
741 dam = (int)((roll(8) + 2) * mult);
748 * Get planes available for interception duties.
751 getilists(struct emp_qelem *list, unsigned char *rel, natid intruder)
754 struct plchrstr *pcp;
760 for (cn = 1; cn < MAXNOC; cn++) {
761 rel[cn] = relations_with(cn, intruder);
762 emp_initque(&list[cn]);
765 snxtitem_all(&ni, EF_PLANE);
766 while (nxtitem(&ni, &plane)) {
767 if (rel[plane.pln_own] > HOSTILE)
769 pcp = &plchr[(int)plane.pln_type];
770 if ((pcp->pl_flags & P_F) == 0)
772 if (plane.pln_flags & PLN_LAUNCHED)
774 if (plane.pln_mission && plane.pln_mission != MI_AIR_DEFENSE)
776 if (plane.pln_mobil <= 0)
778 if (plane.pln_effic < 40)
781 if (ontradingblock(EF_PLANE, &plane))
784 if (!pln_airbase_ok(&plane, 0, 0))
787 ip = malloc(sizeof(*ip));
789 ip->pstage = PLG_HEALTHY;
790 ip->pcp = &plchr[(int)plane.pln_type];
792 emp_insque(&ip->queue, &list[plane.pln_own]);
797 do_evade(struct emp_qelem *bomb_list, struct emp_qelem *esc_list)
799 struct emp_qelem *qp;
804 for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw) {
805 plp = (struct plist *)qp;
806 if (evade > plp->pcp->pl_stealth / 100.0)
807 evade = plp->pcp->pl_stealth / 100.0;
809 for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw) {
810 plp = (struct plist *)qp;
811 if (evade > plp->pcp->pl_stealth / 100.0)
812 evade = plp->pcp->pl_stealth / 100.0;