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;
95 struct sctstr ap_sect;
98 struct emp_qelem *qp, *next;
104 if (get_planes(&ni_bomb, &ni_esc, player->argp[1], player->argp[2]) < 0)
106 p = getstarg(player->argp[3], "pinpoint, or strategic? ", buf);
110 if (!strchr("ps", mission))
112 if (!get_assembly_point(player->argp[4], &ap_sect, buf))
116 if (!getpath(flightpath, player->argp[5], ax, ay, 0, 0, P_FLYING)
121 (void)pathtoxy(flightpath, &tx, &ty, fcost);
122 pr("target sector is %s\n", xyas(tx, ty, player->cnum));
123 getsect(tx, ty, &target);
124 ap_to_target = strlen(flightpath);
125 if (flightpath[ap_to_target - 1] == 'h')
127 pr("range to target is %d\n", ap_to_target);
129 * select planes within range
131 pln_sel(&ni_bomb, &bomb_list, &ap_sect, ap_to_target,
132 2, wantflags, P_M | P_O);
133 pln_sel(&ni_esc, &esc_list, &ap_sect, ap_to_target,
134 2, wantflags | P_F | P_ESC, P_M | P_O);
136 * now arm and equip the bombers, transports, whatever.
138 pln_arm(&bomb_list, 2 * ap_to_target, mission, NULL);
139 if (QEMPTY(&bomb_list)) {
140 pr("No planes could be equipped for the mission.\n");
143 pln_arm(&esc_list, 2 * ap_to_target, 'e', NULL);
144 ac_encounter(&bomb_list, &esc_list, ax, ay, flightpath, 0);
145 if (QEMPTY(&bomb_list)) {
146 pr("No planes got through fighter defenses\n");
147 } else if (target.sct_type == SCT_SANCT) {
148 pr("You can't bomb that sector!\n");
152 pin_bomb(&bomb_list, &target);
153 for (qp = bomb_list.q_forw; qp != &bomb_list; qp = next) {
155 plp = (struct plist *)qp;
156 changed_plane_aborts(plp);
158 for (qp = esc_list.q_forw; qp != &esc_list; qp = next) {
160 plp = (struct plist *)qp;
161 changed_plane_aborts(plp);
166 natp = getnatp(player->cnum);
167 if (target.sct_own) {
168 rel = getrel(natp, target.sct_own);
169 if ((rel != AT_WAR) && (player->cnum != target.sct_own)
170 && (target.sct_own) &&
171 (target.sct_oldown != player->cnum)) {
172 pr("You're not at war with them!\n");
179 nreport(player->cnum, N_SCT_BOMB, target.sct_own, 1);
180 strat_bomb(&bomb_list, &target);
192 pin_bomb(struct emp_qelem *list, struct sctstr *target)
208 type = target->sct_type;
210 pr("Target sector is a %s constructed %s\n",
211 effadv((int)target->sct_effic), dcp->d_name);
213 nships = shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
214 if (pln_caps(list) & P_A) {
215 nsubs = shipsatxy(target->sct_x, target->sct_y, M_SUB, 0, 1);
217 pr("Some subs are present in the sector.\n");
219 nplanes = planesatxy(target->sct_x, target->sct_y, 0, 0);
220 nunits = unitsatxy(target->sct_x, target->sct_y, 0, 0);
222 p = getstring("Bomb what? (ship, plane, land unit, efficiency, commodities) ",
235 natp = getnatp(player->cnum);
236 if (target->sct_own) {
237 rel = getrel(natp, target->sct_own);
238 if ((rel != AT_WAR) && (player->cnum != target->sct_own)
239 && (target->sct_own) &&
240 (target->sct_oldown != player->cnum)) {
241 pr("You're not at war with them!\n");
247 pr("no units there\n");
250 land_bomb(list, target);
254 natp = getnatp(player->cnum);
255 if (target->sct_own) {
256 rel = getrel(natp, target->sct_own);
257 if ((rel != AT_WAR) && (player->cnum != target->sct_own)
258 && (target->sct_own) &&
259 (target->sct_oldown != player->cnum)) {
260 pr("You're not at war with them!\n");
266 pr("no planes there\n");
269 plane_bomb(list, target);
273 if (pln_caps(list) & P_A) {
275 pr("no ships there\n");
279 pr("no ships there\n");
283 ship_bomb(list, target);
287 natp = getnatp(player->cnum);
288 if (target->sct_own) {
289 rel = getrel(natp, target->sct_own);
290 if ((rel != AT_WAR) && (player->cnum != target->sct_own)
291 && (target->sct_own) &&
292 (target->sct_oldown != player->cnum)) {
293 pr("You're not at war with them!\n");
299 for (i = 0; i < nbomb; i++) {
300 if (!target->sct_item[bombcomm[i]])
305 pr("No bombable commodities in %s\n",
306 xyas(target->sct_x, target->sct_y, player->cnum));
309 comm_bomb(list, target);
313 natp = getnatp(player->cnum);
314 if (target->sct_own) {
315 rel = getrel(natp, target->sct_own);
316 if ((rel != AT_WAR) && (player->cnum != target->sct_own)
317 && (target->sct_own) &&
318 (target->sct_oldown != player->cnum)) {
319 pr("You're not at war with them!\n");
324 eff_bomb(list, target);
327 pr("Aborting mission.\n");
330 pr("Bad target type.\n");
336 eff_bomb(struct emp_qelem *list, struct sctstr *target)
339 struct emp_qelem *qp, *next;
344 for (qp = list->q_forw; qp != list; qp = next) {
346 plp = (struct plist *)qp;
347 if (changed_plane_aborts(plp))
349 if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
351 if (plp->bombs || nuk_on_plane(&plp->plane) >= 0)
352 dam += pln_damage(&plp->plane, target->sct_x, target->sct_y,
355 if (dam <= 0) /* dam == 0 if only nukes were delivered */
357 getsect(target->sct_x, target->sct_y, §);
359 oldeff = target->sct_effic;
360 target->sct_effic = effdamage(target->sct_effic, dam);
361 target->sct_avail = effdamage(target->sct_avail, dam);
362 target->sct_road = effdamage(target->sct_road, dam);
363 target->sct_rail = effdamage(target->sct_rail, dam);
364 target->sct_defense = effdamage(target->sct_defense, dam);
365 pr("did %d%% damage to efficiency in %s\n",
366 oldeff - target->sct_effic,
367 xyas(target->sct_x, target->sct_y, player->cnum));
369 wu(0, target->sct_own,
370 "%s bombing raid did %d%% damage in %s\n",
371 cname(player->cnum), oldeff - target->sct_effic,
372 xyas(target->sct_x, target->sct_y, target->sct_own));
373 bridge_damaged(target);
375 collateral_damage(target->sct_x, target->sct_y, dam);
379 comm_bomb(struct emp_qelem *list, struct sctstr *target)
386 struct emp_qelem *qp, *next;
391 for (i = 0; i < nbomb; i++) {
392 if (target->sct_item[bombcomm[i]] == 0)
394 if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
396 ip = &ichr[bombcomm[i]];
397 pr("some %s\n", ip->i_name);
400 ip = whatitem(NULL, "commodity to bomb? ");
406 for (i = 0; i < nbomb; i++) {
407 if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
409 if (&ichr[bombcomm[i]] == ip)
413 pr("You can't bomb %s!\n", ip->i_name);
414 for (i = 0; i < nbomb; i++) {
415 if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
417 pr("%s%s", i == 0 ? "Bombable: " : ", ",
418 ichr[bombcomm[i]].i_name);
424 for (qp = list->q_forw; qp != list; qp = next) {
426 plp = (struct plist *)qp;
427 if (changed_plane_aborts(plp))
429 if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
431 if (plp->bombs || nuk_on_plane(&plp->plane) >= 0)
432 dam += pln_damage(&plp->plane, target->sct_x, target->sct_y,
435 if (dam <= 0) /* dam == 0 if only nukes were delivered */
437 getsect(target->sct_x, target->sct_y, §);
439 before = target->sct_item[ip->i_uid];
440 target->sct_item[ip->i_uid] = amt = commdamage(before, dam, ip->i_uid);
442 b = 100.0 * (1.0 - (double)amt / (double)before);
445 pr("did %.2f%% damage to %s in %s\n",
446 b, ip->i_name, xyas(target->sct_x, target->sct_y, player->cnum));
447 nreport(player->cnum, N_SCT_BOMB, target->sct_own, 1);
448 if (target->sct_own != 0)
449 wu(0, target->sct_own,
450 "%s precision bombing raid did %.2f%% damage to %s in %s\n",
451 cname(player->cnum), b, ip->i_name,
452 xyas(target->sct_x, target->sct_y, target->sct_own));
454 collateral_damage(target->sct_x, target->sct_y, dam);
458 ship_bomb(struct emp_qelem *list, struct sctstr *target)
465 struct emp_qelem *qp, *next;
469 struct shiplist *head = NULL;
477 for (qp = list->q_forw; qp != list; qp = next) {
479 free_shiplist(&head);
480 plp = (struct plist *)qp;
481 if (changed_plane_aborts(plp))
483 if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
485 if (plp->pcp->pl_flags & P_A)
486 nships = asw_shipsatxy(target->sct_x, target->sct_y, 0, 0,
489 nships = shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
491 pr("%s could not find any ships!\n", prplane(&plp->plane));
494 (void)sprintf(prompt, "%s, %d bombs. Target ('~' to skip)? ",
495 prplane(&plp->plane), plp->bombs);
498 if (!(q = getstring(prompt, buf)))
505 if (plp->pcp->pl_flags & P_A)
506 print_shiplist(head);
508 shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
514 if ((!(plp->pcp->pl_flags & P_A) || on_shiplist(n, head)) &&
515 getship(n, &ship) && ship.shp_own &&
516 ship.shp_x == target->sct_x && ship.shp_y == target->sct_y)
519 pr("Ship #%d not spotted\n", n);
523 if ((plp->pcp->pl_flags & P_A) && !on_shiplist(shipno, head))
525 if (changed_plane_aborts(plp))
528 gun = shp_usable_guns(&ship);
529 mcp = &mchr[(int)ship.shp_type];
530 if (gun > 0 && !(mcp->m_flags & M_SUB)) {
531 flak = (int)(techfact(ship.shp_tech, gun) * 2.0);
532 PR(ship.shp_own, "Flak! Firing %d guns from ship %s\n",
533 flak, prship(&ship));
534 if (pinflak_planedamage(&plp->plane, plp->pcp, ship.shp_own, flak))
539 if (nuk_on_plane(&plp->plane) >= 0)
542 hitchance = pln_hitchance(&plp->plane,
543 shp_hardtarget(&ship), EF_SHIP);
544 pr("%d%% hitchance...", hitchance);
546 if (roll(100) <= hitchance) {
547 /* pinbombing is more accurate than normal bombing */
548 dam = 2 * pln_damage(&plp->plane, target->sct_x, target->sct_y,
552 /* Bombs that miss have to land somewhere! */
553 dam = pln_damage(&plp->plane, target->sct_x, target->sct_y,
555 collateral_damage(target->sct_x, target->sct_y, dam);
558 if (dam <= 0) /* dam == 0 if only nukes were delivered */
560 if (mcp->m_flags & M_SUB)
561 nreport(player->cnum, N_SUB_BOMB, ship.shp_own, 1);
563 nreport(player->cnum, N_SHP_BOMB, ship.shp_own, 1);
565 wu(0, ship.shp_own, "%s bombs did %d damage to %s at %s\n",
566 cname(player->cnum), dam,
568 xyas(target->sct_x, target->sct_y, ship.shp_own));
571 check_retreat_and_do_shipdamage(&ship, dam);
572 if (ship.shp_rflags & RET_BOMBED)
573 if (((ship.shp_rflags & RET_INJURED) == 0) || !dam)
574 retreat_ship(&ship, 'b');
575 putship(ship.shp_uid, &ship);
576 getship(ship.shp_uid, &ship);
578 pr("%s at %s sunk!\n",
580 xyas(target->sct_x, target->sct_y, player->cnum));
582 collateral_damage(target->sct_x, target->sct_y, dam / 2);
585 free_shiplist(&head);
589 plane_bomb(struct emp_qelem *list, struct sctstr *target)
596 struct emp_qelem *qp, *next;
605 for (qp = list->q_forw; qp != list; qp = next) {
607 plp = (struct plist *)qp;
608 if (changed_plane_aborts(plp))
610 if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
612 nplanes = planesatxy(target->sct_x, target->sct_y, 0, 0);
614 pr("%s could not find any planes!\n", prplane(&plp->plane));
617 (void)sprintf(prompt, "%s, %d bombs. Target ('~' to skip)? ",
618 prplane(&plp->plane), plp->bombs);
620 while (planeno < 0) {
621 if (!(q = getstring(prompt, buf)))
628 planesatxy(target->sct_x, target->sct_y, 0, 0);
634 if (getplane(n, &plane) &&
635 plane.pln_x == target->sct_x &&
636 plane.pln_y == target->sct_y &&
637 plane.pln_ship < 0 && plane.pln_land < 0 &&
638 !(plane.pln_flags & PLN_LAUNCHED))
641 pr("Plane #%d not spotted\n", n);
645 if (changed_plane_aborts(plp))
648 if (nuk_on_plane(&plp->plane) >= 0)
651 hitchance = pln_hitchance(&plp->plane, 0, EF_PLANE);
652 pr("%d%% hitchance...", hitchance);
654 if (roll(100) <= hitchance) {
655 /* pinbombing is more accurate than normal bombing */
656 dam = 2 * pln_damage(&plp->plane, target->sct_x, target->sct_y,
660 /* Bombs that miss have to land somewhere! */
661 dam = pln_damage(&plp->plane, target->sct_x, target->sct_y,
663 collateral_damage(target->sct_x, target->sct_y, dam);
666 if (dam <= 0) /* dam == 0 if only nukes were delivered */
671 if (dam > plane.pln_effic)
674 plane.pln_effic -= dam;
675 plane.pln_mobil = (dam * plane.pln_mobil / 100.0);
676 if (own == player->cnum) {
677 pr("%s reports %d%% damage\n", prplane(&plane), dam);
681 "%s pinpoint bombing raid did %d%% damage to %s\n",
682 cname(player->cnum), dam, prplane(&plane));
684 nreport(player->cnum, N_DOWN_PLANE, own, 1);
686 wu(0, own, "%s bombs did %d%% damage to %s at %s\n",
687 cname(player->cnum), dam, prplane(&plane),
688 xyas(target->sct_x, target->sct_y, own));
689 putplane(plane.pln_uid, &plane);
690 collateral_damage(target->sct_x, target->sct_y, dam);
695 land_bomb(struct emp_qelem *list, struct sctstr *target)
704 struct emp_qelem *qp, *next;
706 int aaf, flak, hitchance;
711 for (qp = list->q_forw; qp != list; qp = next) {
713 plp = (struct plist *)qp;
714 if (changed_plane_aborts(plp))
716 if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
718 nunits = unitsatxy(target->sct_x, target->sct_y, 0, 0);
720 pr("%s could not find any units!\n", prplane(&plp->plane));
723 (void)sprintf(prompt, "%s, %d bombs. Target ('~' to skip)? ",
724 prplane(&plp->plane), plp->bombs);
727 if (!(q = getstring(prompt, buf)))
734 unitsatxy(target->sct_x, target->sct_y, 0, 0);
740 if (getland(n, &land) && land.lnd_own &&
741 land.lnd_ship < 0 && land.lnd_land < 0 &&
742 land.lnd_x == target->sct_x && land.lnd_y == target->sct_y)
745 pr("Unit #%d not spotted\n", n);
749 if (changed_plane_aborts(plp))
752 aaf = lnd_aaf(&land);
754 flak = roundavg(techfact(land.lnd_tech,
755 aaf * 3.0 * land.lnd_effic / 100.0));
757 "Flak! Firing flak guns from unit %s (aa rating %d)\n",
759 if (pinflak_planedamage(&plp->plane, plp->pcp, land.lnd_own, flak))
764 if (nuk_on_plane(&plp->plane) >= 0)
767 hitchance = pln_hitchance(&plp->plane,
768 lnd_hardtarget(&land), EF_LAND);
769 pr("%d%% hitchance...", hitchance);
771 if (roll(100) <= hitchance) {
772 dam = 2 * pln_damage(&plp->plane, target->sct_x, target->sct_y,
776 /* Bombs that miss have to land somewhere! */
777 dam = pln_damage(&plp->plane, target->sct_x, target->sct_y,
779 collateral_damage(target->sct_x, target->sct_y, dam);
782 if (dam <= 0) /* dam == 0 if only nukes were delivered */
787 mpr(own, "%s pinpoint bombing raid did %d damage to %s\n",
788 cname(player->cnum), dam, prland(&land));
789 check_retreat_and_do_landdamage(&land, dam);
791 if (land.lnd_rflags & RET_BOMBED)
792 if (((land.lnd_rflags & RET_INJURED) == 0) || !dam)
793 retreat_land(&land, 'b');
794 nreport(player->cnum, N_UNIT_BOMB, own, 1);
795 putland(land.lnd_uid, &land);
796 collateral_damage(target->sct_x, target->sct_y, dam);
801 strat_bomb(struct emp_qelem *list, struct sctstr *target)
805 struct emp_qelem *qp;
809 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
810 plp = (struct plist *)qp;
811 if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
813 if (plp->bombs || nuk_on_plane(&plp->plane) >= 0)
814 dam += pln_damage(&plp->plane, target->sct_x, target->sct_y,
817 if (dam <= 0) /* dam == 0 if only nukes were delivered */
819 getsect(target->sct_x, target->sct_y, §);
822 wu(0, target->sct_own, "%s bombing raid did %d damage in %s\n",
823 cname(player->cnum), PERCENT_DAMAGE(dam),
824 xyas(target->sct_x, target->sct_y, target->sct_own));
826 sectdamage(target, dam);
828 pr("did %d damage in %s\n", PERCENT_DAMAGE(dam),
829 xyas(target->sct_x, target->sct_y, player->cnum));
834 changed_plane_aborts(struct plist *plp)
836 if (check_plane_ok(&plp->plane))
838 getplane(plp->plane.pln_uid, &plp->plane);
844 pinflak_planedamage(struct plnstr *pp, struct plchrstr *pcp, natid from,
853 dam = ac_flak_dam(flak, pln_def(pp), pcp->pl_flags);
855 plane_owner = pp->pln_own;
859 memset(dmess, 0, sizeof(dmess));
863 if (eff < PLANE_MINEFF) {
864 sprintf(dmess, " -- shot down");
866 } else if (chance((100 - eff) / 100.0)) {
867 sprintf(dmess, " -- aborted with %d%% damage", 100 - eff);
870 PR(plane_owner, " Flak! %s %s takes %d%s.\n",
871 cname(pp->pln_own), prplane(pp), dam, dmess);
876 nreport(from, N_DOWN_PLANE, pp->pln_own, 1);
878 putplane(pp->pln_uid, pp);