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 * 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, 2,
130 P_B | P_T, P_M | P_O);
131 pln_sel(&ni_esc, &esc_list, &ap_sect, ap_to_target, 2,
132 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;
341 for (qp = list->q_forw; qp != list; qp = next) {
343 plp = (struct plist *)qp;
344 if (changed_plane_aborts(plp))
346 dam += pln_damage(&plp->plane, 'p', 1);
350 getsect(target->sct_x, target->sct_y, §);
352 oldeff = target->sct_effic;
353 target->sct_effic = effdamage(target->sct_effic, dam);
354 target->sct_avail = effdamage(target->sct_avail, dam);
355 target->sct_road = effdamage(target->sct_road, dam);
356 target->sct_rail = effdamage(target->sct_rail, dam);
357 target->sct_defense = effdamage(target->sct_defense, dam);
358 pr("did %d%% damage to efficiency in %s\n",
359 oldeff - target->sct_effic,
360 xyas(target->sct_x, target->sct_y, player->cnum));
362 wu(0, target->sct_own,
363 "%s bombing raid did %d%% damage in %s\n",
364 cname(player->cnum), oldeff - target->sct_effic,
365 xyas(target->sct_x, target->sct_y, target->sct_own));
366 bridge_damaged(target);
368 collateral_damage(target->sct_x, target->sct_y, dam);
372 comm_bomb(struct emp_qelem *list, struct sctstr *target)
379 struct emp_qelem *qp, *next;
383 for (i = 0; i < nbomb; i++) {
384 if (target->sct_item[bombcomm[i]] == 0)
386 if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
388 ip = &ichr[bombcomm[i]];
389 pr("some %s\n", ip->i_name);
392 ip = whatitem(NULL, "commodity to bomb? ");
398 for (i = 0; i < nbomb; i++) {
399 if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
401 if (&ichr[bombcomm[i]] == ip)
405 pr("You can't bomb %s!\n", ip->i_name);
406 for (i = 0; i < nbomb; i++) {
407 if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
409 pr("%s%s", i == 0 ? "Bombable: " : ", ",
410 ichr[bombcomm[i]].i_name);
416 for (qp = list->q_forw; qp != list; qp = next) {
418 plp = (struct plist *)qp;
419 if (changed_plane_aborts(plp))
421 dam += pln_damage(&plp->plane, 'p', 1);
425 getsect(target->sct_x, target->sct_y, §);
427 before = target->sct_item[ip->i_uid];
428 target->sct_item[ip->i_uid] = amt = commdamage(before, dam, ip->i_uid);
430 b = 100.0 * (1.0 - (double)amt / (double)before);
433 pr("did %.2f%% damage to %s in %s\n",
434 b, ip->i_name, xyas(target->sct_x, target->sct_y, player->cnum));
435 nreport(player->cnum, N_SCT_BOMB, target->sct_own, 1);
436 if (target->sct_own != 0)
437 wu(0, target->sct_own,
438 "%s precision bombing raid did %.2f%% damage to %s in %s\n",
439 cname(player->cnum), b, ip->i_name,
440 xyas(target->sct_x, target->sct_y, target->sct_own));
442 collateral_damage(target->sct_x, target->sct_y, dam);
446 ship_bomb(struct emp_qelem *list, struct sctstr *target)
453 struct emp_qelem *qp, *next;
457 struct shiplist *head = NULL;
464 for (qp = list->q_forw; qp != list; qp = next) {
466 free_shiplist(&head);
467 plp = (struct plist *)qp;
468 if (changed_plane_aborts(plp))
470 if (plp->pcp->pl_flags & P_A)
471 nships = asw_shipsatxy(target->sct_x, target->sct_y, 0, 0,
474 nships = shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
476 pr("%s could not find any ships!\n", prplane(&plp->plane));
479 (void)sprintf(prompt, "%s, %d bombs. Target ('~' to skip)? ",
480 prplane(&plp->plane), plp->load);
483 if (!(q = getstring(prompt, buf)))
490 if (plp->pcp->pl_flags & P_A)
491 print_shiplist(head);
493 shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
499 if ((!(plp->pcp->pl_flags & P_A) || on_shiplist(n, head)) &&
500 getship(n, &ship) && ship.shp_own &&
501 ship.shp_x == target->sct_x && ship.shp_y == target->sct_y)
504 pr("Ship #%d not spotted\n", n);
508 if ((plp->pcp->pl_flags & P_A) && !on_shiplist(shipno, head))
510 if (changed_plane_aborts(plp))
513 gun = shp_usable_guns(&ship);
514 mcp = &mchr[(int)ship.shp_type];
515 if (gun > 0 && !(mcp->m_flags & M_SUB)) {
516 flak = (int)(techfact(ship.shp_tech, gun) * 2.0);
517 PR(ship.shp_own, "Flak! Firing %d guns from ship %s\n",
518 flak, prship(&ship));
519 if (pinflak_planedamage(&plp->plane, plp->pcp, ship.shp_own, flak))
523 if (nuk_on_plane(&plp->plane) >= 0)
526 hitchance = pln_hitchance(&plp->plane,
527 shp_hardtarget(&ship), EF_SHIP);
528 pr("%d%% hitchance...", hitchance);
530 if (roll(100) <= hitchance) {
531 /* pinbombing is more accurate than normal bombing */
532 dam = 2 * pln_damage(&plp->plane, 'p', 1);
535 /* Bombs that miss have to land somewhere! */
536 dam = pln_damage(&plp->plane, 'p', 0);
537 collateral_damage(target->sct_x, target->sct_y, dam);
542 if (mcp->m_flags & M_SUB)
543 nreport(player->cnum, N_SUB_BOMB, ship.shp_own, 1);
545 nreport(player->cnum, N_SHP_BOMB, ship.shp_own, 1);
547 wu(0, ship.shp_own, "%s bombs did %d damage to %s at %s\n",
548 cname(player->cnum), dam,
550 xyas(target->sct_x, target->sct_y, ship.shp_own));
553 check_retreat_and_do_shipdamage(&ship, dam);
554 if (ship.shp_rflags & RET_BOMBED)
555 if (((ship.shp_rflags & RET_INJURED) == 0) || !dam)
556 retreat_ship(&ship, 'b');
557 putship(ship.shp_uid, &ship);
558 getship(ship.shp_uid, &ship);
560 pr("%s at %s sunk!\n",
562 xyas(target->sct_x, target->sct_y, player->cnum));
564 collateral_damage(target->sct_x, target->sct_y, dam / 2);
567 free_shiplist(&head);
571 plane_bomb(struct emp_qelem *list, struct sctstr *target)
578 struct emp_qelem *qp, *next;
586 for (qp = list->q_forw; qp != list; qp = next) {
588 plp = (struct plist *)qp;
589 if (changed_plane_aborts(plp))
591 nplanes = planesatxy(target->sct_x, target->sct_y, 0, 0);
593 pr("%s could not find any planes!\n", prplane(&plp->plane));
596 (void)sprintf(prompt, "%s, %d bombs. Target ('~' to skip)? ",
597 prplane(&plp->plane), plp->load);
599 while (planeno < 0) {
600 if (!(q = getstring(prompt, buf)))
607 planesatxy(target->sct_x, target->sct_y, 0, 0);
613 if (getplane(n, &plane) &&
614 plane.pln_x == target->sct_x &&
615 plane.pln_y == target->sct_y &&
616 plane.pln_ship < 0 && plane.pln_land < 0 &&
617 !(plane.pln_flags & PLN_LAUNCHED))
620 pr("Plane #%d not spotted\n", n);
624 if (changed_plane_aborts(plp))
626 if (nuk_on_plane(&plp->plane) >= 0)
629 hitchance = pln_hitchance(&plp->plane, 0, EF_PLANE);
630 pr("%d%% hitchance...", hitchance);
632 if (roll(100) <= hitchance) {
633 /* pinbombing is more accurate than normal bombing */
634 dam = 2 * pln_damage(&plp->plane, 'p', 1);
637 /* Bombs that miss have to land somewhere! */
638 dam = pln_damage(&plp->plane, 'p', 0);
639 collateral_damage(target->sct_x, target->sct_y, dam);
647 if (dam > plane.pln_effic)
650 plane.pln_effic -= dam;
651 plane.pln_mobil = (dam * plane.pln_mobil / 100.0);
652 if (own == player->cnum) {
653 pr("%s reports %d%% damage\n", prplane(&plane), dam);
657 "%s pinpoint bombing raid did %d%% damage to %s\n",
658 cname(player->cnum), dam, prplane(&plane));
660 nreport(player->cnum, N_DOWN_PLANE, own, 1);
662 wu(0, own, "%s bombs did %d%% damage to %s at %s\n",
663 cname(player->cnum), dam, prplane(&plane),
664 xyas(target->sct_x, target->sct_y, own));
665 putplane(plane.pln_uid, &plane);
666 collateral_damage(target->sct_x, target->sct_y, dam);
671 land_bomb(struct emp_qelem *list, struct sctstr *target)
680 struct emp_qelem *qp, *next;
682 int aaf, flak, hitchance;
686 for (qp = list->q_forw; qp != list; qp = next) {
688 plp = (struct plist *)qp;
689 if (changed_plane_aborts(plp))
691 nunits = unitsatxy(target->sct_x, target->sct_y, 0, 0);
693 pr("%s could not find any units!\n", prplane(&plp->plane));
696 (void)sprintf(prompt, "%s, %d bombs. Target ('~' to skip)? ",
697 prplane(&plp->plane), plp->load);
700 if (!(q = getstring(prompt, buf)))
707 unitsatxy(target->sct_x, target->sct_y, 0, 0);
713 if (getland(n, &land) && land.lnd_own &&
714 land.lnd_ship < 0 && land.lnd_land < 0 &&
715 land.lnd_x == target->sct_x && land.lnd_y == target->sct_y)
718 pr("Unit #%d not spotted\n", n);
722 if (changed_plane_aborts(plp))
725 aaf = lnd_aaf(&land);
727 flak = roundavg(techfact(land.lnd_tech,
728 aaf * 3.0 * land.lnd_effic / 100.0));
730 "Flak! Firing flak guns from unit %s (aa rating %d)\n",
732 if (pinflak_planedamage(&plp->plane, plp->pcp, land.lnd_own, flak))
736 if (nuk_on_plane(&plp->plane) >= 0)
739 hitchance = pln_hitchance(&plp->plane,
740 lnd_hardtarget(&land), EF_LAND);
741 pr("%d%% hitchance...", hitchance);
743 if (roll(100) <= hitchance) {
744 dam = 2 * pln_damage(&plp->plane, 'p', 1);
747 /* Bombs that miss have to land somewhere! */
748 dam = pln_damage(&plp->plane, 'p', 0);
749 collateral_damage(target->sct_x, target->sct_y, dam);
757 mpr(own, "%s pinpoint bombing raid did %d damage to %s\n",
758 cname(player->cnum), dam, prland(&land));
759 check_retreat_and_do_landdamage(&land, dam);
761 if (land.lnd_rflags & RET_BOMBED)
762 if (((land.lnd_rflags & RET_INJURED) == 0) || !dam)
763 retreat_land(&land, 'b');
764 nreport(player->cnum, N_UNIT_BOMB, own, 1);
765 putland(land.lnd_uid, &land);
766 collateral_damage(target->sct_x, target->sct_y, dam);
771 strat_bomb(struct emp_qelem *list, struct sctstr *target)
775 struct emp_qelem *qp;
779 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
780 plp = (struct plist *)qp;
781 if (getnuke(nuk_on_plane(&plp->plane), &nuke))
782 detonate(&nuke, target->sct_x, target->sct_y,
783 plp->plane.pln_flags & PLN_AIRBURST);
785 dam += pln_damage(&plp->plane, 's', 1);
787 if (dam <= 0) /* dam == 0 if only nukes were delivered */
789 getsect(target->sct_x, target->sct_y, §);
792 wu(0, target->sct_own, "%s bombing raid did %d damage in %s\n",
793 cname(player->cnum), PERCENT_DAMAGE(dam),
794 xyas(target->sct_x, target->sct_y, target->sct_own));
796 sectdamage(target, dam);
798 pr("did %d damage in %s\n", PERCENT_DAMAGE(dam),
799 xyas(target->sct_x, target->sct_y, player->cnum));
804 changed_plane_aborts(struct plist *plp)
806 if (check_plane_ok(&plp->plane))
808 getplane(plp->plane.pln_uid, &plp->plane);
814 pinflak_planedamage(struct plnstr *pp, struct plchrstr *pcp, natid from,
823 dam = ac_flak_dam(flak, pln_def(pp), pcp->pl_flags);
825 plane_owner = pp->pln_own;
829 memset(dmess, 0, sizeof(dmess));
833 if (eff < PLANE_MINEFF) {
834 sprintf(dmess, " -- shot down");
836 } else if (chance((100 - eff) / 100.0)) {
837 sprintf(dmess, " -- aborted with %d%% damage", 100 - eff);
840 PR(plane_owner, " Flak! %s %s takes %d%s.\n",
841 cname(pp->pln_own), prplane(pp), dam, dmess);
846 nreport(from, N_DOWN_PLANE, pp->pln_own, 1);
848 putplane(pp->pln_uid, pp);