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, int, char *);
66 static void ac_doflak(struct emp_qelem *, struct sctstr *);
67 static void ac_landflak(struct emp_qelem *, coord, coord);
68 static void ac_shipflak(struct emp_qelem *, coord, coord);
69 static void ac_fireflak(struct emp_qelem *, natid, int);
70 static void getilists(struct emp_qelem *, unsigned char *, natid);
71 static int do_evade(struct emp_qelem *, struct emp_qelem *);
74 ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
75 coord x, coord y, char *path, int mission_flags)
79 unsigned char gotships[MAXNOC];
80 unsigned char gotlands[MAXNOC];
81 unsigned char rel[MAXNOC];
84 struct emp_qelem ilist[MAXNOC];
93 struct shiplist *head = NULL;
95 /* We want to only intercept once per sector per owner. So, if we overfly
96 a sector, and then overfly some land units or ships, we don't want to
97 potentially intercept 3 times. */
99 plp = (struct plist *)bomb_list->q_forw;
100 plane_owner = plp->plane.pln_own;
102 memset(overfly, 0, sizeof(overfly));
103 getilists(ilist, rel, plane_owner);
105 if (CANT_HAPPEN(mission_flags && plane_owner != player->cnum))
108 if (mission_flags & PM_R) {
109 flags = pln_caps(bomb_list);
111 pr("\nSPY Plane report\n");
114 } else if (flags & P_A) {
115 pr("\nAnti-Sub Patrol report\n");
117 pr("\nReconnaissance report\n");
123 getsect(x, y, §);
124 memset(gotships, 0, sizeof(gotships));
125 snxtitem_xy(&ni, EF_SHIP, x, y);
126 while (nxtitem(&ni, &ship)) {
127 if (mchr[(int)ship.shp_type].m_flags & M_SUB)
129 gotships[ship.shp_own] = 1;
131 memset(gotlands, 0, sizeof(gotlands));
132 snxtitem_xy(&ni, EF_LAND, x, y);
133 while (nxtitem(&ni, &land)) {
134 if (land.lnd_ship >= 0 || land.lnd_land >= 0)
136 gotlands[land.lnd_own] = 1;
139 if (mission_flags & PM_R) {
140 flags = pln_caps(bomb_list);
142 setcont(player->cnum, sect.sct_own, FOUND_FLY);
143 if (sect.sct_type == SCT_WATER)
144 pr("flying over %s at %s\n",
145 dchr[sect.sct_type].d_name, xyas(x, y, player->cnum));
146 else if (flags & P_S)
147 satdisp_sect(§, flags & P_I ? 10 : 50);
149 look_at_sect(§, 25);
152 satdisp_units(sect.sct_x, sect.sct_y);
154 for (cn = 1; cn < MAXNOC; cn++) {
155 if (cn == player->cnum)
158 pr("Flying over %s ships in %s\n",
159 cname(cn), xyas(x, y, player->cnum));
161 pr("Flying over %s land units in %s\n",
162 cname(cn), xyas(x, y, player->cnum));
166 mpr(plane_owner, "flying over %s at %s\n",
167 dchr[sect.sct_type].d_name, xyas(x, y, plane_owner));
168 changed += map_set(plane_owner, sect.sct_x, sect.sct_y,
169 dchr[sect.sct_type].d_mnem, 0);
171 evaded = do_evade(bomb_list, esc_list);
173 overfly[sect.sct_own]++;
174 for (cn = 1; cn < MAXNOC; cn++) {
175 if (rel[cn] == ALLIED)
177 if (cn != sect.sct_own && !gotships[cn] && !gotlands[cn])
179 mpr(cn, "%s planes spotted over %s\n",
180 cname(plane_owner), xyas(x, y, cn));
182 setcont(cn, plane_owner, FOUND_FLY);
186 if (rel[sect.sct_own] <= HOSTILE)
187 ac_doflak(bomb_list, §);
188 /* If bombers left, fire flak from units and ships */
189 if (!QEMPTY(bomb_list))
190 ac_landflak(bomb_list, x, y);
191 if (!QEMPTY(bomb_list))
192 ac_shipflak(bomb_list, x, y);
193 /* mission planes aborted due to flak -- don't send escorts */
194 if (QEMPTY(bomb_list))
197 for (cn = 1; cn < MAXNOC && !QEMPTY(bomb_list); cn++) {
198 if (rel[cn] > HOSTILE)
200 ac_intercept(bomb_list, esc_list, &ilist[cn], cn, x, y,
202 || gotships[cn] || gotlands[cn]));
206 if (mission_flags & PM_R) {
207 flags = pln_caps(bomb_list);
208 if (sect.sct_type == SCT_WATER && mission_flags & PM_S)
209 plane_sweep(bomb_list, x, y);
210 if (sect.sct_type == SCT_WATER && flags & P_A)
211 plane_sona(bomb_list, x, y, &head);
215 if (!dir || QEMPTY(bomb_list) || (val = diridx(dir)) == DIR_STOP)
217 x = xnorm(x + diroff[val][0]);
218 y = ynorm(y + diroff[val][1]);
221 /* Let's report all of the overflights even if aborted */
222 for (cn = 1; cn < MAXNOC; cn++) {
223 if (overfly[cn] > 0 && rel[cn] != ALLIED)
224 nreport(plane_owner, N_OVFLY_SECT, cn, overfly[cn]);
226 /* If the map changed, update it */
228 writemap(plane_owner);
230 free_shiplist(&head);
231 for (cn = 1; cn < MAXNOC; cn++)
236 sam_intercept(struct emp_qelem *att_list, struct emp_qelem *def_list,
237 natid def_own, natid plane_owner, coord x, coord y,
240 struct emp_qelem *aqp;
241 struct emp_qelem *anext;
242 struct emp_qelem *dqp;
243 struct emp_qelem *dnext;
249 for (aqp = att_list->q_forw,
250 dqp = def_list->q_forw;
251 aqp != att_list && dqp != def_list; aqp = anext) {
253 aplp = (struct plist *)aqp;
254 if (aplp->pcp->pl_cost < 1000)
256 for (; dqp != def_list; dqp = dnext) {
258 dplp = (struct plist *)dqp;
260 if (!(dplp->pcp->pl_flags & P_M))
262 if (only_mission && !pp->pln_mission)
264 if (pp->pln_range < mapdist(x, y, pp->pln_x, pp->pln_y))
267 && pp->pln_radius < mapdist(x, y, pp->pln_opx, pp->pln_opy))
269 if (CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED)
270 || mission_pln_equip(dplp, NULL, 0) < 0) {
275 pp->pln_flags |= PLN_LAUNCHED;
276 putplane(pp->pln_uid, pp);
279 mpr(plane_owner, "%s launches SAMs!\n", cname(def_own));
280 mpr(def_own, "Launching SAMs at %s planes over %s!\n",
281 cname(plane_owner), xyas(x, y, def_own));
282 ac_combat_headers(plane_owner, def_own);
290 mpr(plane_owner, "\n");
296 ac_intercept(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
297 struct emp_qelem *def_list, natid def_own, coord x, coord y,
303 struct emp_qelem *next;
304 struct emp_qelem *qp;
305 struct emp_qelem int_list;
310 plp = (struct plist *)bomb_list->q_forw;
311 plane_owner = plp->plane.pln_own;
313 sam_intercept(bomb_list, def_list, def_own, plane_owner, x, y,
315 sam_intercept(esc_list, def_list, def_own, plane_owner, x, y,
319 for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw)
321 for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw)
326 emp_initque(&int_list);
328 for (qp = def_list->q_forw; qp != def_list; qp = next) {
330 plp = (struct plist *)qp;
332 /* SAMs interdict separately */
333 if (plp->pcp->pl_flags & P_M)
335 if (only_mission && !pp->pln_mission)
337 dist = mapdist(x, y, pp->pln_x, pp->pln_y) * 2;
338 if (pp->pln_range < dist)
341 && pp->pln_radius < mapdist(x, y, pp->pln_opx, pp->pln_opy))
343 if (CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED)
344 || mission_pln_equip(plp, NULL, 0) < 0) {
349 /* got one; delete from def_list, add to int_list */
351 emp_insque(qp, &int_list);
352 pp->pln_flags |= PLN_LAUNCHED;
353 pp->pln_mobil -= pln_mobcost(dist, pp, 0);
354 putplane(pp->pln_uid, pp);
356 if (icount > att_count)
361 mpr(plane_owner, "%d %s fighter%s rising to intercept!\n",
362 icount, cname(def_own), icount == 1 ? " is" : "s are");
363 mpr(def_own, "%d fighter%s intercepting %s planes over %s!\n",
364 icount, icount == 1 ? " is" : "s are", cname(plane_owner),
365 xyas(x, y, def_own));
366 ac_combat_headers(plane_owner, def_own);
367 ac_airtoair(esc_list, &int_list);
368 ac_airtoair(bomb_list, &int_list);
369 mpr(plane_owner, "\n");
375 ac_combat_headers(natid plane_owner, natid def_own)
378 " %-10.10s %-10.10s strength int odds damage results\n",
379 cname(plane_owner), cname(def_own));
381 " %-10.10s %-10.10s strength int odds damage results\n",
382 cname(def_own), cname(plane_owner));
389 ac_airtoair(struct emp_qelem *att_list, struct emp_qelem *int_list)
391 struct plist *attacker;
392 struct plist *interceptor;
393 struct emp_qelem *att;
394 struct emp_qelem *in;
397 struct emp_qelem *att_next;
398 struct emp_qelem *in_next;
400 att = att_list->q_forw;
401 in = int_list->q_forw;
404 if (QEMPTY(att_list) || QEMPTY(int_list)) {
408 while (more_att || more_int) {
409 in_next = in->q_forw;
410 att_next = att->q_forw;
411 attacker = (struct plist *)att;
412 interceptor = (struct plist *)in;
413 ac_dog(attacker, interceptor);
416 if (att == att_list) {
418 if (QEMPTY(att_list))
423 if (in == int_list) {
425 if (QEMPTY(int_list))
434 ac_dog_report(natid to, int intensity, double odds,
435 struct plist *p1, int val1, int dam1, char *dam_mesg1,
436 struct plist *p2, int val2, int dam2, char *dam_mesg2)
438 mpr(to, " %3.3s #%-4d %3.3s #%-4d %3d/%-3d %3d %3.2f %3d/%-3d"
439 "%-13.13s %-13.13s\n",
440 p1->pcp->pl_name, p1->plane.pln_uid,
441 p2->pcp->pl_name, p2->plane.pln_uid,
442 val1, val2, intensity, odds, dam1, dam2,
443 dam_mesg1, dam_mesg2);
447 ac_dog(struct plist *ap, struct plist *dp)
452 natid att_own, def_own;
454 char adam_mesg[14], ddam_mesg[14];
456 att_own = ap->plane.pln_own;
457 def_own = dp->plane.pln_own;
459 att = pln_att(&ap->plane);
461 att = pln_def(&ap->plane);
462 att = att * ap->plane.pln_effic / 100;
463 att = MAX(att, ap->pcp->pl_def / 2);
465 def = pln_def(&dp->plane) * dp->plane.pln_effic / 100;
466 def = MAX(def, dp->pcp->pl_def / 2);
468 if ((ap->pcp->pl_flags & P_F) && ap->load != 0)
470 if ((dp->pcp->pl_flags & P_F) && dp->load != 0)
472 att += ap->pcp->pl_stealth / 25.0;
473 def += dp->pcp->pl_stealth / 25.0;
482 odds = ((double)att / ((double)def + (double)att));
485 intensity = roll(20) + roll(20) + roll(20) + roll(20) + 1;
489 for (i = 0; i < intensity; i++) {
492 if (dp->plane.pln_effic - ddam < PLANE_MINEFF)
496 if (ap->plane.pln_effic - adam < PLANE_MINEFF)
501 if (dp->pcp->pl_flags & P_M)
504 ac_planedamage(ap, def_own, adam, 0, adam_mesg);
505 ac_planedamage(dp, att_own, ddam, 0, ddam_mesg);
506 ac_dog_report(att_own, intensity, odds,
507 ap, att, adam, adam_mesg,
508 dp, def, ddam, ddam_mesg);
509 ac_dog_report(def_own, intensity, odds,
510 dp, def, ddam, ddam_mesg,
511 ap, att, adam, adam_mesg);
514 setcont(att_own, def_own, FOUND_FLY);
515 setcont(def_own, att_own, FOUND_FLY);
520 * zap plane associated with plp.
521 * Damaging country is "from", damage is "dam".
523 * NOTE: This routine removes the appropriate plane element from the
524 * queue if it gets destroyed. That means that the caller must assume
525 * that the current queue pointer is invalid on return from the ac_planedamage
526 * call. (this has caused bugs in the past)
529 ac_planedamage(struct plist *plp, natid from, int dam, int flak,
532 int disp = ac_damage_plane(&plp->plane, from, dam, flak, mesg);
537 putplane(plp->plane.pln_uid, &plp->plane);
541 ac_damage_plane(struct plnstr *pp, natid from, int dam, int flak,
549 snprintf(mesg, 14, " no damage");
553 eff = pp->pln_effic - dam;
558 if (eff < PLANE_MINEFF) {
559 snprintf(mesg, 14, " shot down");
561 } else if (eff < 80 && chance((80 - eff) / 100.0)) {
562 snprintf(mesg, 14, " aborted @%2d%%", eff);
565 snprintf(mesg, 14, " cleared");
568 pp->pln_mobil -= MIN(32 + pp->pln_mobil, dam / 2);
570 if (disp == 1 && from != 0 && !(plchr[pp->pln_type].pl_flags & P_M))
571 nreport(from, N_DOWN_PLANE, pp->pln_own, 1);
576 ac_doflak(struct emp_qelem *list, struct sctstr *from)
582 plp = (struct plist *)list->q_forw;
583 plane_owner = plp->plane.pln_own;
585 gun = MIN(FLAK_GUN_MAX, from->sct_item[I_GUN]);
586 gun = roundavg(tfact(from->sct_own, 2.0 * gun));
588 mpr(plane_owner, "firing %d flak guns in %s...\n",
589 gun, xyas(from->sct_x, from->sct_y, plane_owner));
590 mpr(from->sct_own, "firing %d flak guns in %s...\n",
591 gun, xyas(from->sct_x, from->sct_y, from->sct_own));
592 ac_fireflak(list, from->sct_own, gun);
597 ac_shipflak(struct emp_qelem *list, coord x, coord y)
602 double flak, total, ngun;
608 plp = (struct plist *)list->q_forw;
609 plane_owner = plp->plane.pln_own;
612 snxtitem_xy(&ni, EF_SHIP, x, y);
613 while (!QEMPTY(list) && nxtitem(&ni, &ship)) {
614 if (ship.shp_own == 0 || ship.shp_own == plane_owner)
616 mcp = &mchr[(int)ship.shp_type];
617 if (mcp->m_flags & M_SUB)
619 if (relations_with(ship.shp_own, plane_owner) > HOSTILE)
621 gun = shp_usable_guns(&ship);
624 flak = gun * (ship.shp_effic / 100.0);
626 total += techfact(ship.shp_tech, flak * 2.0);
628 mpr(ship.shp_own, "firing %.0f flak guns from %s...\n",
629 flak, prship(&ship));
633 /* Limit to FLAK_GUN_MAX guns of average tech factor */
634 if (ngun > FLAK_GUN_MAX)
635 total *= FLAK_GUN_MAX / ngun;
637 gun = roundavg(total);
639 mpr(plane_owner, "Flak! Ships firing %d flak guns...\n", gun);
640 ac_fireflak(list, from, gun);
645 ac_landflak(struct emp_qelem *list, coord x, coord y)
650 double flak, total, ngun;
656 plp = (struct plist *)list->q_forw;
657 plane_owner = plp->plane.pln_own;
660 snxtitem_xy(&ni, EF_LAND, x, y);
661 while (!QEMPTY(list) && nxtitem(&ni, &land)) {
662 if (land.lnd_own == 0 || land.lnd_own == plane_owner)
664 lcp = &lchr[(int)land.lnd_type];
665 aaf = lnd_aaf(&land);
666 if ((lcp->l_flags & L_FLAK) == 0 || aaf == 0)
668 if (land.lnd_ship >= 0 || land.lnd_land >= 0)
670 if (relations_with(land.lnd_own, plane_owner) > HOSTILE)
672 flak = aaf * 1.5 * land.lnd_effic / 100.0;
674 total += techfact(land.lnd_tech, flak * 2.0);
676 mpr(land.lnd_own, "firing flak guns from unit %s (aa rating %d)\n",
681 /* Limit to FLAK_GUN_MAX guns of average tech factor */
682 if (ngun > FLAK_GUN_MAX)
683 total *= FLAK_GUN_MAX / ngun;
685 gun = roundavg(total);
687 mpr(plane_owner, "Flak! Land units firing %d flak guns...\n", gun);
688 ac_fireflak(list, from, gun);
693 * Called from shipflak, landflak, and doflak.
696 ac_fireflak(struct emp_qelem *list, natid from, int guns)
700 struct emp_qelem *qp;
701 struct emp_qelem *next;
704 for (qp = list->q_forw; qp != list; qp = next) {
706 plp = (struct plist *)qp;
707 n = ac_flak_dam(guns, pln_def(&plp->plane), plp->pcp->pl_flags);
708 ac_planedamage(plp, from, n, 1, msg);
709 mpr(plp->plane.pln_own, " %s takes %d%s%s.\n",
710 prplane(&plp->plane), n, *msg ? " --" : "", msg);
715 * Calculate flak damage
718 ac_flak_dam(int guns, int def, int pl_flags)
722 /* <-7 -7 -6 -5 -4 */
723 static float flaktable[18] = { 0.132f, 0.20f, 0.20f, 0.25f, 0.30f,
724 /* -3 -2 -1 0 +1 +2 +3 +4 */
725 0.35f, 0.40f, 0.45f, 0.50f, 0.50f, 0.55f, 0.60f, 0.65f,
726 /* +5 +6 +7 +8 >+8 */
727 0.70f,0.75f, 0.80f, 0.85f, 1.1305f };
728 enum { FLAK_MAX = sizeof(flaktable)/sizeof(flaktable[0]) - 1 };
731 if ((pl_flags & P_T) == 0)
735 mult = flaktable[FLAK_MAX];
740 mult = flaktable[flak];
743 dam = (int)((roll(8) + 2) * mult);
750 * Get planes available for interception duties.
753 getilists(struct emp_qelem *list, unsigned char *rel, natid intruder)
756 struct plchrstr *pcp;
762 for (cn = 1; cn < MAXNOC; cn++) {
763 rel[cn] = relations_with(cn, intruder);
764 emp_initque(&list[cn]);
767 snxtitem_all(&ni, EF_PLANE);
768 while (nxtitem(&ni, &plane)) {
769 if (rel[plane.pln_own] > HOSTILE)
771 pcp = &plchr[(int)plane.pln_type];
772 if ((pcp->pl_flags & P_F) == 0)
774 if (plane.pln_flags & PLN_LAUNCHED)
776 if (plane.pln_mission && plane.pln_mission != MI_AIR_DEFENSE)
778 if (plane.pln_mobil <= 0)
780 if (plane.pln_effic < 40)
782 if (!pln_airbase_ok(&plane, 0, 0))
785 ip = malloc(sizeof(*ip));
787 ip->pcp = &plchr[(int)plane.pln_type];
789 emp_insque(&ip->queue, &list[plane.pln_own]);
794 do_evade(struct emp_qelem *bomb_list, struct emp_qelem *esc_list)
796 struct emp_qelem *qp;
801 for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw) {
802 plp = (struct plist *)qp;
803 if (evade > plp->pcp->pl_stealth / 100.0)
804 evade = plp->pcp->pl_stealth / 100.0;
806 for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw) {
807 plp = (struct plist *)qp;
808 if (evade > plp->pcp->pl_stealth / 100.0)
809 evade = plp->pcp->pl_stealth / 100.0;