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);
348 getsect(target->sct_x, target->sct_y, §);
350 oldeff = target->sct_effic;
351 target->sct_effic = effdamage(target->sct_effic, dam);
352 target->sct_avail = effdamage(target->sct_avail, dam);
353 target->sct_road = effdamage(target->sct_road, dam);
354 target->sct_rail = effdamage(target->sct_rail, dam);
355 target->sct_defense = effdamage(target->sct_defense, dam);
356 pr("did %d%% damage to efficiency in %s\n",
357 oldeff - target->sct_effic,
358 xyas(target->sct_x, target->sct_y, player->cnum));
360 wu(0, target->sct_own,
361 "%s bombing raid did %d%% damage in %s\n",
362 cname(player->cnum), oldeff - target->sct_effic,
363 xyas(target->sct_x, target->sct_y, target->sct_own));
364 bridge_damaged(target);
366 collateral_damage(target->sct_x, target->sct_y, dam);
370 comm_bomb(struct emp_qelem *list, struct sctstr *target)
377 struct emp_qelem *qp, *next;
381 for (i = 0; i < nbomb; i++) {
382 if (target->sct_item[bombcomm[i]] == 0)
384 if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
386 ip = &ichr[bombcomm[i]];
387 pr("some %s\n", ip->i_name);
390 ip = whatitem(NULL, "commodity to bomb? ");
396 for (i = 0; i < nbomb; i++) {
397 if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
399 if (&ichr[bombcomm[i]] == ip)
403 pr("You can't bomb %s!\n", ip->i_name);
404 for (i = 0; i < nbomb; i++) {
405 if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
407 pr("%s%s", i == 0 ? "Bombable: " : ", ",
408 ichr[bombcomm[i]].i_name);
414 for (qp = list->q_forw; qp != list; qp = next) {
416 plp = (struct plist *)qp;
417 if (changed_plane_aborts(plp))
419 dam += pln_damage(&plp->plane, 'p', 1);
421 getsect(target->sct_x, target->sct_y, §);
423 before = target->sct_item[ip->i_uid];
424 target->sct_item[ip->i_uid] = amt = commdamage(before, dam, ip->i_uid);
426 b = 100.0 * (1.0 - (double)amt / (double)before);
429 pr("did %.2f%% damage to %s in %s\n",
430 b, ip->i_name, xyas(target->sct_x, target->sct_y, player->cnum));
431 nreport(player->cnum, N_SCT_BOMB, target->sct_own, 1);
432 if (target->sct_own != 0)
433 wu(0, target->sct_own,
434 "%s precision bombing raid did %.2f%% damage to %s in %s\n",
435 cname(player->cnum), b, ip->i_name,
436 xyas(target->sct_x, target->sct_y, target->sct_own));
438 collateral_damage(target->sct_x, target->sct_y, dam);
442 ship_bomb(struct emp_qelem *list, struct sctstr *target)
449 struct emp_qelem *qp, *next;
453 struct shiplist *head = NULL;
460 for (qp = list->q_forw; qp != list; qp = next) {
462 free_shiplist(&head);
463 plp = (struct plist *)qp;
464 if (changed_plane_aborts(plp))
466 if (plp->pcp->pl_flags & P_A)
467 nships = asw_shipsatxy(target->sct_x, target->sct_y, 0, 0,
470 nships = shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
472 pr("%s could not find any ships!\n", prplane(&plp->plane));
475 (void)sprintf(prompt, "%s, %d bombs. Target ('~' to skip)? ",
476 prplane(&plp->plane), plp->load);
479 if (!(q = getstring(prompt, buf)))
486 if (plp->pcp->pl_flags & P_A)
487 print_shiplist(head);
489 shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
495 if ((!(plp->pcp->pl_flags & P_A) || on_shiplist(n, head)) &&
496 getship(n, &ship) && ship.shp_own &&
497 ship.shp_x == target->sct_x && ship.shp_y == target->sct_y)
500 pr("Ship #%d not spotted\n", n);
504 if ((plp->pcp->pl_flags & P_A) && !on_shiplist(shipno, head))
506 if (changed_plane_aborts(plp))
509 gun = shp_usable_guns(&ship);
510 mcp = &mchr[(int)ship.shp_type];
511 if (gun > 0 && !(mcp->m_flags & M_SUB)) {
512 flak = (int)(techfact(ship.shp_tech, gun) * 2.0);
513 PR(ship.shp_own, "Flak! Firing %d guns from ship %s\n",
514 flak, prship(&ship));
515 if (pinflak_planedamage(&plp->plane, plp->pcp, ship.shp_own, flak))
519 if (nuk_on_plane(&plp->plane) >= 0)
522 hitchance = pln_hitchance(&plp->plane,
523 shp_hardtarget(&ship), EF_SHIP);
524 pr("%d%% hitchance...", hitchance);
526 if (roll(100) <= hitchance) {
527 /* pinbombing is more accurate than normal bombing */
528 dam = 2 * pln_damage(&plp->plane, 'p', 1);
531 /* Bombs that miss have to land somewhere! */
532 dam = pln_damage(&plp->plane, 'p', 0);
533 collateral_damage(target->sct_x, target->sct_y, dam);
536 if (mcp->m_flags & M_SUB)
537 nreport(player->cnum, N_SUB_BOMB, ship.shp_own, 1);
539 nreport(player->cnum, N_SHP_BOMB, ship.shp_own, 1);
541 wu(0, ship.shp_own, "%s bombs did %d damage to %s at %s\n",
542 cname(player->cnum), dam,
544 xyas(target->sct_x, target->sct_y, ship.shp_own));
547 check_retreat_and_do_shipdamage(&ship, dam);
548 if (ship.shp_rflags & RET_BOMBED)
549 if (((ship.shp_rflags & RET_INJURED) == 0) || !dam)
550 retreat_ship(&ship, 'b');
551 putship(ship.shp_uid, &ship);
552 getship(ship.shp_uid, &ship);
554 pr("%s at %s sunk!\n",
556 xyas(target->sct_x, target->sct_y, player->cnum));
558 collateral_damage(target->sct_x, target->sct_y, dam / 2);
561 free_shiplist(&head);
565 plane_bomb(struct emp_qelem *list, struct sctstr *target)
572 struct emp_qelem *qp, *next;
580 for (qp = list->q_forw; qp != list; qp = next) {
582 plp = (struct plist *)qp;
583 if (changed_plane_aborts(plp))
585 nplanes = planesatxy(target->sct_x, target->sct_y, 0, 0);
587 pr("%s could not find any planes!\n", prplane(&plp->plane));
590 (void)sprintf(prompt, "%s, %d bombs. Target ('~' to skip)? ",
591 prplane(&plp->plane), plp->load);
593 while (planeno < 0) {
594 if (!(q = getstring(prompt, buf)))
601 planesatxy(target->sct_x, target->sct_y, 0, 0);
607 if (getplane(n, &plane) &&
608 plane.pln_x == target->sct_x &&
609 plane.pln_y == target->sct_y &&
610 plane.pln_ship < 0 && plane.pln_land < 0 &&
611 !(plane.pln_flags & PLN_LAUNCHED))
614 pr("Plane #%d not spotted\n", n);
618 if (changed_plane_aborts(plp))
620 if (nuk_on_plane(&plp->plane) >= 0)
623 hitchance = pln_hitchance(&plp->plane, 0, EF_PLANE);
624 pr("%d%% hitchance...", hitchance);
626 if (roll(100) <= hitchance) {
627 /* pinbombing is more accurate than normal bombing */
628 dam = 2 * pln_damage(&plp->plane, 'p', 1);
631 /* Bombs that miss have to land somewhere! */
632 dam = pln_damage(&plp->plane, 'p', 0);
633 collateral_damage(target->sct_x, target->sct_y, dam);
639 if (dam > plane.pln_effic)
642 plane.pln_effic -= dam;
643 plane.pln_mobil = (dam * plane.pln_mobil / 100.0);
644 if (own == player->cnum) {
645 pr("%s reports %d%% damage\n", prplane(&plane), dam);
649 "%s pinpoint bombing raid did %d%% damage to %s\n",
650 cname(player->cnum), dam, prplane(&plane));
652 nreport(player->cnum, N_DOWN_PLANE, own, 1);
654 wu(0, own, "%s bombs did %d%% damage to %s at %s\n",
655 cname(player->cnum), dam, prplane(&plane),
656 xyas(target->sct_x, target->sct_y, own));
657 putplane(plane.pln_uid, &plane);
658 collateral_damage(target->sct_x, target->sct_y, dam);
663 land_bomb(struct emp_qelem *list, struct sctstr *target)
672 struct emp_qelem *qp, *next;
674 int aaf, flak, hitchance;
678 for (qp = list->q_forw; qp != list; qp = next) {
680 plp = (struct plist *)qp;
681 if (changed_plane_aborts(plp))
683 nunits = unitsatxy(target->sct_x, target->sct_y, 0, 0);
685 pr("%s could not find any units!\n", prplane(&plp->plane));
688 (void)sprintf(prompt, "%s, %d bombs. Target ('~' to skip)? ",
689 prplane(&plp->plane), plp->load);
692 if (!(q = getstring(prompt, buf)))
699 unitsatxy(target->sct_x, target->sct_y, 0, 0);
705 if (getland(n, &land) && land.lnd_own &&
706 land.lnd_ship < 0 && land.lnd_land < 0 &&
707 land.lnd_x == target->sct_x && land.lnd_y == target->sct_y)
710 pr("Unit #%d not spotted\n", n);
714 if (changed_plane_aborts(plp))
717 aaf = lnd_aaf(&land);
719 flak = roundavg(techfact(land.lnd_tech,
720 aaf * 3.0 * land.lnd_effic / 100.0));
722 "Flak! Firing flak guns from unit %s (aa rating %d)\n",
724 if (pinflak_planedamage(&plp->plane, plp->pcp, land.lnd_own, flak))
728 if (nuk_on_plane(&plp->plane) >= 0)
731 hitchance = pln_hitchance(&plp->plane,
732 lnd_hardtarget(&land), EF_LAND);
733 pr("%d%% hitchance...", hitchance);
735 if (roll(100) <= hitchance) {
736 dam = 2 * pln_damage(&plp->plane, 'p', 1);
739 /* Bombs that miss have to land somewhere! */
740 dam = pln_damage(&plp->plane, 'p', 0);
741 collateral_damage(target->sct_x, target->sct_y, dam);
747 mpr(own, "%s pinpoint bombing raid did %d damage to %s\n",
748 cname(player->cnum), dam, prland(&land));
749 check_retreat_and_do_landdamage(&land, dam);
751 if (land.lnd_rflags & RET_BOMBED)
752 if (((land.lnd_rflags & RET_INJURED) == 0) || !dam)
753 retreat_land(&land, 'b');
754 nreport(player->cnum, N_UNIT_BOMB, own, 1);
755 putland(land.lnd_uid, &land);
756 collateral_damage(target->sct_x, target->sct_y, dam);
761 strat_bomb(struct emp_qelem *list, struct sctstr *target)
765 struct emp_qelem *qp;
769 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
770 plp = (struct plist *)qp;
771 if (getnuke(nuk_on_plane(&plp->plane), &nuke))
772 detonate(&nuke, target->sct_x, target->sct_y,
773 plp->plane.pln_flags & PLN_AIRBURST);
775 dam += pln_damage(&plp->plane, 's', 1);
777 getsect(target->sct_x, target->sct_y, §);
780 wu(0, target->sct_own, "%s bombing raid did %d damage in %s\n",
781 cname(player->cnum), PERCENT_DAMAGE(dam),
782 xyas(target->sct_x, target->sct_y, target->sct_own));
784 sectdamage(target, dam);
786 pr("did %d damage in %s\n", PERCENT_DAMAGE(dam),
787 xyas(target->sct_x, target->sct_y, player->cnum));
792 changed_plane_aborts(struct plist *plp)
794 if (check_plane_ok(&plp->plane))
796 getplane(plp->plane.pln_uid, &plp->plane);
802 pinflak_planedamage(struct plnstr *pp, struct plchrstr *pcp, natid from,
811 dam = ac_flak_dam(flak, pln_def(pp), pcp->pl_flags);
813 plane_owner = pp->pln_own;
817 memset(dmess, 0, sizeof(dmess));
821 if (eff < PLANE_MINEFF) {
822 sprintf(dmess, " -- shot down");
824 } else if (chance((100 - eff) / 100.0)) {
825 sprintf(dmess, " -- aborted with %d%% damage", 100 - eff);
828 PR(plane_owner, " Flak! %s %s takes %d%s.\n",
829 cname(pp->pln_own), prplane(pp), dam, dmess);
834 nreport(from, N_DOWN_PLANE, pp->pln_own, 1);
836 putplane(pp->pln_uid, pp);