2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2009, 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 * bomb.c: Fly bombing missions
30 * Known contributors to this file:
33 * Steve McClure, 1998-2000
34 * Markus Armbruster, 2004-2009
52 static void pin_bomb(struct emp_qelem *, struct sctstr *);
53 static void eff_bomb(struct emp_qelem *, struct sctstr *);
54 static void comm_bomb(struct emp_qelem *, struct sctstr *);
55 static void ship_bomb(struct emp_qelem *, struct sctstr *);
56 static void plane_bomb(struct emp_qelem *, struct sctstr *);
57 static void land_bomb(struct emp_qelem *, struct sctstr *);
58 static void strat_bomb(struct emp_qelem *, struct sctstr *);
59 static int changed_plane_aborts(struct plist *);
60 static int pinflak_planedamage(struct plnstr *, struct plchrstr *,
63 static i_type bombcomm[] = {
79 static int nbomb = sizeof(bombcomm) / sizeof(*bombcomm);
88 char flightpath[MAX_PATH_LEN];
89 struct nstr_item ni_bomb;
90 struct nstr_item ni_esc;
92 struct emp_qelem bomb_list;
93 struct emp_qelem esc_list;
94 struct sctstr ap_sect;
97 struct emp_qelem *qp, *next;
102 if (get_planes(&ni_bomb, &ni_esc, player->argp[1], player->argp[2]) < 0)
104 p = getstarg(player->argp[3], "pinpoint, or strategic? ", buf);
108 if (!strchr("ps", mission))
110 if (!get_assembly_point(player->argp[4], &ap_sect, buf))
114 if (!getpath(flightpath, player->argp[5], ax, ay, 0, 0, P_FLYING)
119 (void)pathtoxy(flightpath, &tx, &ty, fcost);
120 pr("target sector is %s\n", xyas(tx, ty, player->cnum));
121 getsect(tx, ty, &target);
122 ap_to_target = strlen(flightpath);
123 if (flightpath[ap_to_target - 1] == 'h')
125 pr("range to target is %d\n", ap_to_target);
127 * select planes within range
129 pln_sel(&ni_bomb, &bomb_list, &ap_sect, ap_to_target,
130 2, P_B | P_T, P_M | P_O);
131 pln_sel(&ni_esc, &esc_list, &ap_sect, ap_to_target,
132 2, P_ESC | P_F, P_M | P_O);
134 * now arm and equip the bombers, transports, whatever.
136 pln_arm(&bomb_list, 2 * ap_to_target, mission, NULL);
137 if (QEMPTY(&bomb_list)) {
138 pr("No planes could be equipped for the mission.\n");
141 pln_arm(&esc_list, 2 * ap_to_target, 'e', NULL);
142 ac_encounter(&bomb_list, &esc_list, ax, ay, flightpath, 0);
143 if (QEMPTY(&bomb_list)) {
144 pr("No planes got through fighter defenses\n");
145 } else if (target.sct_type == SCT_SANCT) {
146 pr("You can't bomb that sector!\n");
150 pin_bomb(&bomb_list, &target);
151 for (qp = bomb_list.q_forw; qp != &bomb_list; qp = next) {
153 plp = (struct plist *)qp;
154 changed_plane_aborts(plp);
156 for (qp = esc_list.q_forw; qp != &esc_list; qp = next) {
158 plp = (struct plist *)qp;
159 changed_plane_aborts(plp);
164 natp = getnatp(player->cnum);
165 if (target.sct_own) {
166 rel = getrel(natp, target.sct_own);
167 if ((rel != AT_WAR) && (player->cnum != target.sct_own)
168 && (target.sct_own) &&
169 (target.sct_oldown != player->cnum)) {
170 pr("You're not at war with them!\n");
177 nreport(player->cnum, N_SCT_BOMB, target.sct_own, 1);
178 strat_bomb(&bomb_list, &target);
190 pin_bomb(struct emp_qelem *list, struct sctstr *target)
206 type = target->sct_type;
208 pr("Target sector is a %s constructed %s\n",
209 effadv((int)target->sct_effic), dcp->d_name);
211 nships = shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
212 if (pln_caps(list) & P_A) {
213 nsubs = shipsatxy(target->sct_x, target->sct_y, M_SUB, 0, 1);
215 pr("Some subs are present in the sector.\n");
217 nplanes = planesatxy(target->sct_x, target->sct_y, 0, 0);
218 nunits = unitsatxy(target->sct_x, target->sct_y, 0, 0);
220 p = getstring("Bomb what? (ship, plane, land unit, efficiency, commodities) ",
233 natp = getnatp(player->cnum);
234 if (target->sct_own) {
235 rel = getrel(natp, target->sct_own);
236 if ((rel != AT_WAR) && (player->cnum != target->sct_own)
237 && (target->sct_own) &&
238 (target->sct_oldown != player->cnum)) {
239 pr("You're not at war with them!\n");
245 pr("no units there\n");
248 land_bomb(list, target);
252 natp = getnatp(player->cnum);
253 if (target->sct_own) {
254 rel = getrel(natp, target->sct_own);
255 if ((rel != AT_WAR) && (player->cnum != target->sct_own)
256 && (target->sct_own) &&
257 (target->sct_oldown != player->cnum)) {
258 pr("You're not at war with them!\n");
264 pr("no planes there\n");
267 plane_bomb(list, target);
271 if (pln_caps(list) & P_A) {
273 pr("no ships there\n");
277 pr("no ships there\n");
281 ship_bomb(list, target);
285 natp = getnatp(player->cnum);
286 if (target->sct_own) {
287 rel = getrel(natp, target->sct_own);
288 if ((rel != AT_WAR) && (player->cnum != target->sct_own)
289 && (target->sct_own) &&
290 (target->sct_oldown != player->cnum)) {
291 pr("You're not at war with them!\n");
297 for (i = 0; i < nbomb; i++) {
298 if (!target->sct_item[bombcomm[i]])
303 pr("No bombable commodities in %s\n",
304 xyas(target->sct_x, target->sct_y, player->cnum));
307 comm_bomb(list, target);
311 natp = getnatp(player->cnum);
312 if (target->sct_own) {
313 rel = getrel(natp, target->sct_own);
314 if ((rel != AT_WAR) && (player->cnum != target->sct_own)
315 && (target->sct_own) &&
316 (target->sct_oldown != player->cnum)) {
317 pr("You're not at war with them!\n");
322 eff_bomb(list, target);
325 pr("Aborting mission.\n");
328 pr("Bad target type.\n");
334 eff_bomb(struct emp_qelem *list, struct sctstr *target)
337 struct emp_qelem *qp, *next;
342 for (qp = list->q_forw; qp != list; qp = next) {
344 plp = (struct plist *)qp;
345 if (changed_plane_aborts(plp))
347 if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
349 if (plp->bombs || nuk_on_plane(&plp->plane) >= 0)
350 dam += pln_damage(&plp->plane, target->sct_x, target->sct_y,
353 if (dam <= 0) /* dam == 0 if only nukes were delivered */
355 getsect(target->sct_x, target->sct_y, §);
357 oldeff = target->sct_effic;
358 target->sct_effic = effdamage(target->sct_effic, dam);
359 target->sct_avail = effdamage(target->sct_avail, dam);
360 target->sct_road = effdamage(target->sct_road, dam);
361 target->sct_rail = effdamage(target->sct_rail, dam);
362 target->sct_defense = effdamage(target->sct_defense, dam);
363 pr("did %d%% damage to efficiency in %s\n",
364 oldeff - target->sct_effic,
365 xyas(target->sct_x, target->sct_y, player->cnum));
367 wu(0, target->sct_own,
368 "%s bombing raid did %d%% damage in %s\n",
369 cname(player->cnum), oldeff - target->sct_effic,
370 xyas(target->sct_x, target->sct_y, target->sct_own));
371 bridge_damaged(target);
373 collateral_damage(target->sct_x, target->sct_y, dam);
377 comm_bomb(struct emp_qelem *list, struct sctstr *target)
384 struct emp_qelem *qp, *next;
389 for (i = 0; i < nbomb; i++) {
390 if (target->sct_item[bombcomm[i]] == 0)
392 if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
394 ip = &ichr[bombcomm[i]];
395 pr("some %s\n", ip->i_name);
398 ip = whatitem(NULL, "commodity to bomb? ");
404 for (i = 0; i < nbomb; i++) {
405 if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
407 if (&ichr[bombcomm[i]] == ip)
411 pr("You can't bomb %s!\n", ip->i_name);
412 for (i = 0; i < nbomb; i++) {
413 if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
415 pr("%s%s", i == 0 ? "Bombable: " : ", ",
416 ichr[bombcomm[i]].i_name);
422 for (qp = list->q_forw; qp != list; qp = next) {
424 plp = (struct plist *)qp;
425 if (changed_plane_aborts(plp))
427 if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
429 if (plp->bombs || nuk_on_plane(&plp->plane) >= 0)
430 dam += pln_damage(&plp->plane, target->sct_x, target->sct_y,
433 if (dam <= 0) /* dam == 0 if only nukes were delivered */
435 getsect(target->sct_x, target->sct_y, §);
437 before = target->sct_item[ip->i_uid];
438 target->sct_item[ip->i_uid] = amt = commdamage(before, dam, ip->i_uid);
440 b = 100.0 * (1.0 - (double)amt / (double)before);
443 pr("did %.2f%% damage to %s in %s\n",
444 b, ip->i_name, xyas(target->sct_x, target->sct_y, player->cnum));
445 nreport(player->cnum, N_SCT_BOMB, target->sct_own, 1);
446 if (target->sct_own != 0)
447 wu(0, target->sct_own,
448 "%s precision bombing raid did %.2f%% damage to %s in %s\n",
449 cname(player->cnum), b, ip->i_name,
450 xyas(target->sct_x, target->sct_y, target->sct_own));
452 collateral_damage(target->sct_x, target->sct_y, dam);
456 ship_bomb(struct emp_qelem *list, struct sctstr *target)
463 struct emp_qelem *qp, *next;
467 struct shiplist *head = NULL;
475 for (qp = list->q_forw; qp != list; qp = next) {
477 free_shiplist(&head);
478 plp = (struct plist *)qp;
479 if (changed_plane_aborts(plp))
481 if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
483 if (plp->pcp->pl_flags & P_A)
484 nships = asw_shipsatxy(target->sct_x, target->sct_y, 0, 0,
487 nships = shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
489 pr("%s could not find any ships!\n", prplane(&plp->plane));
492 (void)sprintf(prompt, "%s, %d bombs. Target ('~' to skip)? ",
493 prplane(&plp->plane), plp->bombs);
496 if (!(q = getstring(prompt, buf)))
503 if (plp->pcp->pl_flags & P_A)
504 print_shiplist(head);
506 shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
512 if ((!(plp->pcp->pl_flags & P_A) || on_shiplist(n, head)) &&
513 getship(n, &ship) && ship.shp_own &&
514 ship.shp_x == target->sct_x && ship.shp_y == target->sct_y)
517 pr("Ship #%d not spotted\n", n);
521 if ((plp->pcp->pl_flags & P_A) && !on_shiplist(shipno, head))
523 if (changed_plane_aborts(plp))
526 gun = shp_usable_guns(&ship);
527 mcp = &mchr[(int)ship.shp_type];
528 if (gun > 0 && !(mcp->m_flags & M_SUB)) {
529 flak = (int)(techfact(ship.shp_tech, gun) * 2.0);
530 PR(ship.shp_own, "Flak! Firing %d guns from ship %s\n",
531 flak, prship(&ship));
532 if (pinflak_planedamage(&plp->plane, plp->pcp, ship.shp_own, flak))
537 if (nuk_on_plane(&plp->plane) >= 0)
540 hitchance = pln_hitchance(&plp->plane,
541 shp_hardtarget(&ship), EF_SHIP);
542 pr("%d%% hitchance...", hitchance);
544 if (roll(100) <= hitchance) {
545 /* pinbombing is more accurate than normal bombing */
546 dam = 2 * pln_damage(&plp->plane, target->sct_x, target->sct_y,
550 /* Bombs that miss have to land somewhere! */
551 dam = pln_damage(&plp->plane, target->sct_x, target->sct_y,
553 collateral_damage(target->sct_x, target->sct_y, dam);
556 if (dam <= 0) /* dam == 0 if only nukes were delivered */
558 if (mcp->m_flags & M_SUB)
559 nreport(player->cnum, N_SUB_BOMB, ship.shp_own, 1);
561 nreport(player->cnum, N_SHP_BOMB, ship.shp_own, 1);
563 wu(0, ship.shp_own, "%s bombs did %d damage to %s at %s\n",
564 cname(player->cnum), dam,
566 xyas(target->sct_x, target->sct_y, ship.shp_own));
569 check_retreat_and_do_shipdamage(&ship, dam);
570 if (ship.shp_rflags & RET_BOMBED)
571 if (((ship.shp_rflags & RET_INJURED) == 0) || !dam)
572 retreat_ship(&ship, 'b');
573 putship(ship.shp_uid, &ship);
574 getship(ship.shp_uid, &ship);
576 pr("%s at %s sunk!\n",
578 xyas(target->sct_x, target->sct_y, player->cnum));
580 collateral_damage(target->sct_x, target->sct_y, dam / 2);
583 free_shiplist(&head);
587 plane_bomb(struct emp_qelem *list, struct sctstr *target)
594 struct emp_qelem *qp, *next;
603 for (qp = list->q_forw; qp != list; qp = next) {
605 plp = (struct plist *)qp;
606 if (changed_plane_aborts(plp))
608 if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
610 nplanes = planesatxy(target->sct_x, target->sct_y, 0, 0);
612 pr("%s could not find any planes!\n", prplane(&plp->plane));
615 (void)sprintf(prompt, "%s, %d bombs. Target ('~' to skip)? ",
616 prplane(&plp->plane), plp->bombs);
618 while (planeno < 0) {
619 if (!(q = getstring(prompt, buf)))
626 planesatxy(target->sct_x, target->sct_y, 0, 0);
632 if (getplane(n, &plane) &&
633 plane.pln_x == target->sct_x &&
634 plane.pln_y == target->sct_y &&
635 plane.pln_ship < 0 && plane.pln_land < 0 &&
636 !(plane.pln_flags & PLN_LAUNCHED))
639 pr("Plane #%d not spotted\n", n);
643 if (changed_plane_aborts(plp))
646 if (nuk_on_plane(&plp->plane) >= 0)
649 hitchance = pln_hitchance(&plp->plane, 0, EF_PLANE);
650 pr("%d%% hitchance...", hitchance);
652 if (roll(100) <= hitchance) {
653 /* pinbombing is more accurate than normal bombing */
654 dam = 2 * pln_damage(&plp->plane, target->sct_x, target->sct_y,
658 /* Bombs that miss have to land somewhere! */
659 dam = pln_damage(&plp->plane, target->sct_x, target->sct_y,
661 collateral_damage(target->sct_x, target->sct_y, dam);
664 if (dam <= 0) /* dam == 0 if only nukes were delivered */
669 if (dam > plane.pln_effic)
672 plane.pln_effic -= dam;
673 plane.pln_mobil = (dam * plane.pln_mobil / 100.0);
674 if (own == player->cnum) {
675 pr("%s reports %d%% damage\n", prplane(&plane), dam);
679 "%s pinpoint bombing raid did %d%% damage to %s\n",
680 cname(player->cnum), dam, prplane(&plane));
682 nreport(player->cnum, N_DOWN_PLANE, own, 1);
684 wu(0, own, "%s bombs did %d%% damage to %s at %s\n",
685 cname(player->cnum), dam, prplane(&plane),
686 xyas(target->sct_x, target->sct_y, own));
687 putplane(plane.pln_uid, &plane);
688 collateral_damage(target->sct_x, target->sct_y, dam);
693 land_bomb(struct emp_qelem *list, struct sctstr *target)
702 struct emp_qelem *qp, *next;
704 int aaf, flak, hitchance;
709 for (qp = list->q_forw; qp != list; qp = next) {
711 plp = (struct plist *)qp;
712 if (changed_plane_aborts(plp))
714 if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
716 nunits = unitsatxy(target->sct_x, target->sct_y, 0, 0);
718 pr("%s could not find any units!\n", prplane(&plp->plane));
721 (void)sprintf(prompt, "%s, %d bombs. Target ('~' to skip)? ",
722 prplane(&plp->plane), plp->bombs);
725 if (!(q = getstring(prompt, buf)))
732 unitsatxy(target->sct_x, target->sct_y, 0, 0);
738 if (getland(n, &land) && land.lnd_own &&
739 land.lnd_ship < 0 && land.lnd_land < 0 &&
740 land.lnd_x == target->sct_x && land.lnd_y == target->sct_y)
743 pr("Unit #%d not spotted\n", n);
747 if (changed_plane_aborts(plp))
750 aaf = lnd_aaf(&land);
752 flak = roundavg(techfact(land.lnd_tech,
753 aaf * 3.0 * land.lnd_effic / 100.0));
755 "Flak! Firing flak guns from unit %s (aa rating %d)\n",
757 if (pinflak_planedamage(&plp->plane, plp->pcp, land.lnd_own, flak))
762 if (nuk_on_plane(&plp->plane) >= 0)
765 hitchance = pln_hitchance(&plp->plane,
766 lnd_hardtarget(&land), EF_LAND);
767 pr("%d%% hitchance...", hitchance);
769 if (roll(100) <= hitchance) {
770 dam = 2 * pln_damage(&plp->plane, target->sct_x, target->sct_y,
774 /* Bombs that miss have to land somewhere! */
775 dam = pln_damage(&plp->plane, target->sct_x, target->sct_y,
777 collateral_damage(target->sct_x, target->sct_y, dam);
780 if (dam <= 0) /* dam == 0 if only nukes were delivered */
785 mpr(own, "%s pinpoint bombing raid did %d damage to %s\n",
786 cname(player->cnum), dam, prland(&land));
787 check_retreat_and_do_landdamage(&land, dam);
789 if (land.lnd_rflags & RET_BOMBED)
790 if (((land.lnd_rflags & RET_INJURED) == 0) || !dam)
791 retreat_land(&land, 'b');
792 nreport(player->cnum, N_UNIT_BOMB, own, 1);
793 putland(land.lnd_uid, &land);
794 collateral_damage(target->sct_x, target->sct_y, dam);
799 strat_bomb(struct emp_qelem *list, struct sctstr *target)
803 struct emp_qelem *qp;
807 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
808 plp = (struct plist *)qp;
809 if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
811 if (plp->bombs || nuk_on_plane(&plp->plane) >= 0)
812 dam += pln_damage(&plp->plane, target->sct_x, target->sct_y,
815 if (dam <= 0) /* dam == 0 if only nukes were delivered */
817 getsect(target->sct_x, target->sct_y, §);
820 wu(0, target->sct_own, "%s bombing raid did %d damage in %s\n",
821 cname(player->cnum), PERCENT_DAMAGE(dam),
822 xyas(target->sct_x, target->sct_y, target->sct_own));
824 sectdamage(target, dam);
826 pr("did %d damage in %s\n", PERCENT_DAMAGE(dam),
827 xyas(target->sct_x, target->sct_y, player->cnum));
832 changed_plane_aborts(struct plist *plp)
834 if (check_plane_ok(&plp->plane))
836 getplane(plp->plane.pln_uid, &plp->plane);
842 pinflak_planedamage(struct plnstr *pp, struct plchrstr *pcp, natid from,
851 dam = ac_flak_dam(flak, pln_def(pp), pcp->pl_flags);
853 plane_owner = pp->pln_own;
857 memset(dmess, 0, sizeof(dmess));
861 if (eff < PLANE_MINEFF) {
862 sprintf(dmess, " -- shot down");
864 } else if (chance((100 - eff) / 100.0)) {
865 sprintf(dmess, " -- aborted with %d%% damage", 100 - eff);
868 PR(plane_owner, " Flak! %s %s takes %d%s.\n",
869 cname(pp->pln_own), prplane(pp), dam, dmess);
874 nreport(from, N_DOWN_PLANE, pp->pln_own, 1);
876 putplane(pp->pln_uid, pp);