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-2008
52 static void pin_bomb(struct emp_qelem *list, struct sctstr *target);
53 static void strat_bomb(struct emp_qelem *list, struct sctstr *target);
54 static void comm_bomb(struct emp_qelem *list, struct sctstr *target);
55 static void eff_bomb(struct emp_qelem *list, struct sctstr *target);
56 static int pinflak_planedamage(struct plnstr *pp, struct plchrstr *pcp,
57 natid from, int flak);
58 static void plane_bomb(struct emp_qelem *list, struct sctstr *target);
59 static void land_bomb(struct emp_qelem *list, struct sctstr *target);
60 static void ship_bomb(struct emp_qelem *list, struct sctstr *target);
62 static i_type bombcomm[] = {
78 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;
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) == 0)
110 if (!get_assembly_point(player->argp[4], &ap_sect, buf))
114 if (getpath(flightpath, player->argp[5], ax, ay, 0, 0, P_FLYING) == 0
119 (void)pathtoxy(flightpath, &tx, &ty, fcost);
120 pr("target sector is %s\n", xyas(tx, ty, player->cnum));
121 getsect(tx, ty, &target);
123 ap_to_target = strlen(flightpath);
124 if (flightpath[ap_to_target - 1] == 'h')
126 pr("range to target is %d\n", ap_to_target);
128 * select planes within range
130 pln_sel(&ni_bomb, &bomb_list, &ap_sect, ap_to_target,
131 2, wantflags, P_M | P_O);
132 pln_sel(&ni_esc, &esc_list, &ap_sect, ap_to_target,
133 2, wantflags | P_F | P_ESC, P_M | P_O);
135 * now arm and equip the bombers, transports, whatever.
137 pln_arm(&bomb_list, 2 * ap_to_target, mission, ip, 0);
138 if (QEMPTY(&bomb_list)) {
139 pr("No planes could be equipped for the mission.\n");
142 pln_arm(&esc_list, 2 * ap_to_target, mission, ip, P_F | P_ESC);
143 ac_encounter(&bomb_list, &esc_list, ax, ay, flightpath, 0);
144 if (QEMPTY(&bomb_list)) {
145 pr("No planes got through fighter defenses\n");
146 } else if (target.sct_type == SCT_SANCT) {
147 pr("You can't bomb that sector!\n");
151 pin_bomb(&bomb_list, &target);
155 natp = getnatp(player->cnum);
156 if (target.sct_own) {
157 rel = getrel(natp, target.sct_own);
158 if ((rel != AT_WAR) && (player->cnum != target.sct_own)
159 && (target.sct_own) &&
160 (target.sct_oldown != player->cnum)) {
161 pr("You're not at war with them!\n");
168 nreport(player->cnum, N_SCT_BOMB, target.sct_own, 1);
169 strat_bomb(&bomb_list, &target);
181 pin_bomb(struct emp_qelem *list, struct sctstr *target)
198 type = target->sct_type;
200 pr("Target sector is a %s constructed %s\n",
201 effadv((int)target->sct_effic), dcp->d_name);
203 plp = (struct plist *)list->q_forw;
204 nships = shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
205 if (plp->pcp->pl_flags & P_A) {
206 nsubs = shipsatxy(target->sct_x, target->sct_y, M_SUB, 0, 1);
208 pr("Some subs are present in the sector.\n");
210 nplanes = planesatxy(target->sct_x, target->sct_y, 0, 0);
211 nunits = unitsatxy(target->sct_x, target->sct_y, 0, 0);
213 p = getstring("Bomb what? (ship, plane, land unit, efficiency, commodities) ",
226 natp = getnatp(player->cnum);
227 if (target->sct_own) {
228 rel = getrel(natp, target->sct_own);
229 if ((rel != AT_WAR) && (player->cnum != target->sct_own)
230 && (target->sct_own) &&
231 (target->sct_oldown != player->cnum)) {
232 pr("You're not at war with them!\n");
238 pr("no units there\n");
241 land_bomb(list, target);
245 natp = getnatp(player->cnum);
246 if (target->sct_own) {
247 rel = getrel(natp, target->sct_own);
248 if ((rel != AT_WAR) && (player->cnum != target->sct_own)
249 && (target->sct_own) &&
250 (target->sct_oldown != player->cnum)) {
251 pr("You're not at war with them!\n");
257 pr("no planes there\n");
260 plane_bomb(list, target);
264 if (((struct plist *)list->q_forw)->pcp->pl_flags & P_A) {
266 pr("no ships there\n");
270 pr("no ships there\n");
274 ship_bomb(list, target);
278 natp = getnatp(player->cnum);
279 if (target->sct_own) {
280 rel = getrel(natp, target->sct_own);
281 if ((rel != AT_WAR) && (player->cnum != target->sct_own)
282 && (target->sct_own) &&
283 (target->sct_oldown != player->cnum)) {
284 pr("You're not at war with them!\n");
290 for (i = 0; i < nbomb; i++) {
291 if (!target->sct_item[bombcomm[i]])
296 pr("No bombable commodities in %s\n",
297 xyas(target->sct_x, target->sct_y, player->cnum));
300 comm_bomb(list, target);
304 natp = getnatp(player->cnum);
305 if (target->sct_own) {
306 rel = getrel(natp, target->sct_own);
307 if ((rel != AT_WAR) && (player->cnum != target->sct_own)
308 && (target->sct_own) &&
309 (target->sct_oldown != player->cnum)) {
310 pr("You're not at war with them!\n");
315 eff_bomb(list, target);
318 pr("Aborting mission.\n");
321 pr("Bad target type.\n");
327 eff_bomb(struct emp_qelem *list, struct sctstr *target)
330 struct emp_qelem *qp;
335 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
336 plp = (struct plist *)qp;
337 if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
339 if (plp->bombs || nuk_on_plane(&plp->plane) >= 0)
340 dam += pln_damage(&plp->plane, target->sct_x, target->sct_y,
343 if (dam <= 0) /* dam == 0 if only nukes were delivered */
345 getsect(target->sct_x, target->sct_y, §);
347 oldeff = target->sct_effic;
348 target->sct_effic = effdamage(target->sct_effic, dam);
349 target->sct_avail = effdamage(target->sct_avail, dam);
350 target->sct_road = effdamage(target->sct_road, dam);
351 target->sct_rail = effdamage(target->sct_rail, dam);
352 target->sct_defense = effdamage(target->sct_defense, dam);
353 pr("did %d%% damage to efficiency in %s\n",
354 oldeff - target->sct_effic,
355 xyas(target->sct_x, target->sct_y, player->cnum));
357 wu(0, target->sct_own,
358 "%s bombing raid did %d%% damage in %s\n",
359 cname(player->cnum), oldeff - target->sct_effic,
360 xyas(target->sct_x, target->sct_y, target->sct_own));
361 bridge_damaged(target);
363 collateral_damage(target->sct_x, target->sct_y, dam);
367 comm_bomb(struct emp_qelem *list, struct sctstr *target)
374 struct emp_qelem *qp;
379 for (i = 0; i < nbomb; i++) {
380 if (target->sct_item[bombcomm[i]] == 0)
382 if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
384 ip = &ichr[bombcomm[i]];
385 pr("some %s\n", ip->i_name);
388 ip = whatitem(NULL, "commodity to bomb? ");
394 for (i = 0; i < nbomb; i++) {
395 if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
397 if (&ichr[bombcomm[i]] == ip)
401 pr("You can't bomb %s!\n", ip->i_name);
402 for (i = 0; i < nbomb; i++) {
403 if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
405 pr("%s%s", i == 0 ? "Bombable: " : ", ",
406 ichr[bombcomm[i]].i_name);
412 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
413 plp = (struct plist *)qp;
414 if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
416 if (plp->bombs || nuk_on_plane(&plp->plane) >= 0)
417 dam += pln_damage(&plp->plane, target->sct_x, target->sct_y,
420 if (dam <= 0) /* dam == 0 if only nukes were delivered */
422 getsect(target->sct_x, target->sct_y, §);
424 before = target->sct_item[ip->i_uid];
425 target->sct_item[ip->i_uid] = amt = commdamage(before, dam, ip->i_uid);
427 b = 100.0 * (1.0 - (double)amt / (double)before);
430 pr("did %.2f%% damage to %s in %s\n",
431 b, ip->i_name, xyas(target->sct_x, target->sct_y, player->cnum));
432 nreport(player->cnum, N_SCT_BOMB, target->sct_own, 1);
433 if (target->sct_own != 0)
434 wu(0, target->sct_own,
435 "%s precision bombing raid did %.2f%% damage to %s in %s\n",
436 cname(player->cnum), b, ip->i_name,
437 xyas(target->sct_x, target->sct_y, target->sct_own));
439 collateral_damage(target->sct_x, target->sct_y, dam);
443 ship_bomb(struct emp_qelem *list, struct sctstr *target)
450 struct emp_qelem *qp;
454 struct shiplist *head = NULL;
462 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
463 free_shiplist(&head);
464 plp = (struct plist *)qp;
465 if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
467 if (plp->pcp->pl_flags & P_A)
468 nships = asw_shipsatxy(target->sct_x, target->sct_y, 0, 0,
471 nships = shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
473 pr("%s could not find any ships!\n", prplane(&plp->plane));
476 (void)sprintf(prompt, "%s, %d bombs. Target ('~' to skip)? ",
477 prplane(&plp->plane), plp->bombs);
480 if ((q = getstring(prompt, buf)) == 0)
487 if (plp->pcp->pl_flags & P_A)
488 print_shiplist(head);
490 shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
496 if ((!(plp->pcp->pl_flags & P_A) || on_shiplist(n, head)) &&
497 getship(n, &ship) && ship.shp_own &&
498 ship.shp_x == target->sct_x && ship.shp_y == target->sct_y)
501 pr("Ship #%d not spotted\n", n);
505 if ((plp->pcp->pl_flags & P_A) && !on_shiplist(shipno, head))
508 gun = shp_usable_guns(&ship);
509 mcp = &mchr[(int)ship.shp_type];
510 if (gun > 0 && !(mcp->m_flags & M_SUB)) {
511 flak = (int)(techfact(ship.shp_tech, gun) * 2.0);
512 PR(ship.shp_own, "Flak! Firing %d guns from ship %s\n",
513 flak, prship(&ship));
514 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, target->sct_x, target->sct_y,
532 /* Bombs that miss have to land somewhere! */
533 dam = pln_damage(&plp->plane, target->sct_x, target->sct_y,
535 collateral_damage(target->sct_x, target->sct_y, dam);
538 if (dam <= 0) /* dam == 0 if only nukes were delivered */
540 if (mcp->m_flags & M_SUB)
541 nreport(player->cnum, N_SUB_BOMB, ship.shp_own, 1);
543 nreport(player->cnum, N_SHP_BOMB, ship.shp_own, 1);
545 wu(0, ship.shp_own, "%s bombs did %d damage to %s at %s\n",
546 cname(player->cnum), dam,
548 xyas(target->sct_x, target->sct_y, ship.shp_own));
551 check_retreat_and_do_shipdamage(&ship, dam);
552 if (ship.shp_rflags & RET_BOMBED)
553 if (((ship.shp_rflags & RET_INJURED) == 0) || !dam)
554 retreat_ship(&ship, 'b');
555 putship(ship.shp_uid, &ship);
556 getship(ship.shp_uid, &ship);
558 pr("%s at %s sunk!\n",
560 xyas(target->sct_x, target->sct_y, player->cnum));
562 collateral_damage(target->sct_x, target->sct_y, dam / 2);
565 free_shiplist(&head);
569 plane_bomb(struct emp_qelem *list, struct sctstr *target)
576 struct emp_qelem *qp;
585 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
586 plp = (struct plist *)qp;
587 if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
589 nplanes = planesatxy(target->sct_x, target->sct_y, 0, 0);
591 pr("%s could not find any planes!\n", prplane(&plp->plane));
594 (void)sprintf(prompt, "%s, %d bombs. Target ('~' to skip)? ",
595 prplane(&plp->plane), plp->bombs);
597 while (planeno < 0) {
598 if ((q = getstring(prompt, buf)) == 0)
605 planesatxy(target->sct_x, target->sct_y, 0, 0);
611 if (getplane(n, &plane) &&
612 plane.pln_x == target->sct_x &&
613 plane.pln_y == target->sct_y &&
614 plane.pln_ship < 0 && plane.pln_land < 0 &&
615 !(plane.pln_flags & PLN_LAUNCHED))
618 pr("Plane #%d not spotted\n", n);
623 if (nuk_on_plane(&plp->plane) >= 0)
626 hitchance = pln_hitchance(&plp->plane, 0, EF_PLANE);
627 pr("%d%% hitchance...", hitchance);
629 if (roll(100) <= hitchance) {
630 /* pinbombing is more accurate than normal bombing */
631 dam = 2 * pln_damage(&plp->plane, target->sct_x, target->sct_y,
635 /* Bombs that miss have to land somewhere! */
636 dam = pln_damage(&plp->plane, target->sct_x, target->sct_y,
638 collateral_damage(target->sct_x, target->sct_y, dam);
641 if (dam <= 0) /* dam == 0 if only nukes were delivered */
646 if (dam > plane.pln_effic)
649 plane.pln_effic -= dam;
650 plane.pln_mobil = (dam * plane.pln_mobil / 100.0);
651 if (own == player->cnum) {
652 pr("%s reports %d%% damage\n", prplane(&plane), dam);
656 "%s pinpoint bombing raid did %d%% damage to %s\n",
657 cname(player->cnum), dam, prplane(&plane));
659 nreport(player->cnum, N_DOWN_PLANE, own, 1);
661 wu(0, own, "%s bombs did %d%% damage to %s at %s\n",
662 cname(player->cnum), dam, prplane(&plane),
663 xyas(target->sct_x, target->sct_y, own));
664 putplane(plane.pln_uid, &plane);
665 collateral_damage(target->sct_x, target->sct_y, dam);
670 land_bomb(struct emp_qelem *list, struct sctstr *target)
679 struct emp_qelem *qp;
681 int aaf, flak, hitchance;
686 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
687 plp = (struct plist *)qp;
688 if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
690 nunits = unitsatxy(target->sct_x, target->sct_y, 0, 0);
692 pr("%s could not find any units!\n", prplane(&plp->plane));
695 (void)sprintf(prompt, "%s, %d bombs. Target ('~' to skip)? ",
696 prplane(&plp->plane), plp->bombs);
699 if ((q = getstring(prompt, buf)) == 0)
706 unitsatxy(target->sct_x, target->sct_y, 0, 0);
712 if (getland(n, &land) && land.lnd_own &&
713 land.lnd_ship < 0 && land.lnd_land < 0 &&
714 land.lnd_x == target->sct_x && land.lnd_y == target->sct_y)
717 pr("Unit #%d not spotted\n", n);
722 aaf = lnd_aaf(&land);
724 flak = roundavg(techfact(land.lnd_tech,
725 aaf * 3.0 * land.lnd_effic / 100.0));
727 "Flak! Firing flak guns from unit %s (aa rating %d)\n",
729 if (pinflak_planedamage(&plp->plane, plp->pcp, land.lnd_own, flak))
734 if (nuk_on_plane(&plp->plane) >= 0)
737 hitchance = pln_hitchance(&plp->plane,
738 lnd_hardtarget(&land), EF_LAND);
739 pr("%d%% hitchance...", hitchance);
741 if (roll(100) <= hitchance) {
742 dam = 2 * pln_damage(&plp->plane, target->sct_x, target->sct_y,
746 /* Bombs that miss have to land somewhere! */
747 dam = pln_damage(&plp->plane, target->sct_x, target->sct_y,
749 collateral_damage(target->sct_x, target->sct_y, dam);
752 if (dam <= 0) /* dam == 0 if only nukes were delivered */
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 ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
783 if (plp->bombs || nuk_on_plane(&plp->plane) >= 0)
784 dam += pln_damage(&plp->plane, target->sct_x, target->sct_y,
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 pinflak_planedamage(struct plnstr *pp, struct plchrstr *pcp, natid from,
813 dam = ac_flak_dam(flak, pln_def(pp), pcp->pl_flags);
815 plane_owner = pp->pln_own;
819 memset(dmess, 0, sizeof(dmess));
823 if (eff < PLANE_MINEFF) {
824 sprintf(dmess, " -- shot down");
826 } else if (chance((100 - eff) / 100.0)) {
827 sprintf(dmess, " -- aborted with %d%% damage", 100 - eff);
830 PR(plane_owner, " Flak! %s %s takes %d%s.\n",
831 cname(pp->pln_own), prplane(pp), dam, dmess);
836 nreport(from, N_DOWN_PLANE, pp->pln_own, 1);
838 putplane(pp->pln_uid, pp);