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);
89 char flightpath[MAX_PATH_LEN];
90 struct nstr_item ni_bomb;
91 struct nstr_item ni_esc;
93 struct emp_qelem bomb_list;
94 struct emp_qelem esc_list;
96 struct sctstr ap_sect;
103 if (get_planes(&ni_bomb, &ni_esc, player->argp[1], player->argp[2]) < 0)
105 p = getstarg(player->argp[3], "pinpoint, or strategic? ", buf);
109 if (strchr("ps", mission) == 0)
111 if (!get_assembly_point(player->argp[4], &ap_sect, buf))
115 if (getpath(flightpath, player->argp[5], ax, ay, 0, 0, P_FLYING) == 0
120 (void)pathtoxy(flightpath, &tx, &ty, fcost);
121 pr("target sector is %s\n", xyas(tx, ty, player->cnum));
122 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, ip, 0);
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, mission, ip, P_F | P_ESC);
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);
156 natp = getnatp(player->cnum);
157 if (target.sct_own) {
158 rel = getrel(natp, target.sct_own);
159 if ((rel != AT_WAR) && (player->cnum != target.sct_own)
160 && (target.sct_own) &&
161 (target.sct_oldown != player->cnum)) {
162 pr("You're not at war with them!\n");
169 nreport(player->cnum, N_SCT_BOMB, target.sct_own, 1);
170 strat_bomb(&bomb_list, &target);
182 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 nships = shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
204 if (pln_caps(list) & P_A) {
205 nsubs = shipsatxy(target->sct_x, target->sct_y, M_SUB, 0, 1);
207 pr("Some subs are present in the sector.\n");
209 nplanes = planesatxy(target->sct_x, target->sct_y, 0, 0);
210 nunits = unitsatxy(target->sct_x, target->sct_y, 0, 0);
212 p = getstring("Bomb what? (ship, plane, land unit, efficiency, commodities) ",
225 natp = getnatp(player->cnum);
226 if (target->sct_own) {
227 rel = getrel(natp, target->sct_own);
228 if ((rel != AT_WAR) && (player->cnum != target->sct_own)
229 && (target->sct_own) &&
230 (target->sct_oldown != player->cnum)) {
231 pr("You're not at war with them!\n");
237 pr("no units there\n");
240 land_bomb(list, target);
244 natp = getnatp(player->cnum);
245 if (target->sct_own) {
246 rel = getrel(natp, target->sct_own);
247 if ((rel != AT_WAR) && (player->cnum != target->sct_own)
248 && (target->sct_own) &&
249 (target->sct_oldown != player->cnum)) {
250 pr("You're not at war with them!\n");
256 pr("no planes there\n");
259 plane_bomb(list, target);
263 if (pln_caps(list) & P_A) {
265 pr("no ships there\n");
269 pr("no ships there\n");
273 ship_bomb(list, target);
277 natp = getnatp(player->cnum);
278 if (target->sct_own) {
279 rel = getrel(natp, target->sct_own);
280 if ((rel != AT_WAR) && (player->cnum != target->sct_own)
281 && (target->sct_own) &&
282 (target->sct_oldown != player->cnum)) {
283 pr("You're not at war with them!\n");
289 for (i = 0; i < nbomb; i++) {
290 if (!target->sct_item[bombcomm[i]])
295 pr("No bombable commodities in %s\n",
296 xyas(target->sct_x, target->sct_y, player->cnum));
299 comm_bomb(list, target);
303 natp = getnatp(player->cnum);
304 if (target->sct_own) {
305 rel = getrel(natp, target->sct_own);
306 if ((rel != AT_WAR) && (player->cnum != target->sct_own)
307 && (target->sct_own) &&
308 (target->sct_oldown != player->cnum)) {
309 pr("You're not at war with them!\n");
314 eff_bomb(list, target);
317 pr("Aborting mission.\n");
320 pr("Bad target type.\n");
326 eff_bomb(struct emp_qelem *list, struct sctstr *target)
329 struct emp_qelem *qp, *next;
334 for (qp = list->q_forw; qp != list; qp = next) {
336 plp = (struct plist *)qp;
337 if (changed_plane_aborts(plp))
339 if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
341 if (plp->bombs || nuk_on_plane(&plp->plane) >= 0)
342 dam += pln_damage(&plp->plane, target->sct_x, target->sct_y,
345 if (dam <= 0) /* dam == 0 if only nukes were delivered */
347 getsect(target->sct_x, target->sct_y, §);
349 oldeff = target->sct_effic;
350 target->sct_effic = effdamage(target->sct_effic, dam);
351 target->sct_avail = effdamage(target->sct_avail, dam);
352 target->sct_road = effdamage(target->sct_road, dam);
353 target->sct_rail = effdamage(target->sct_rail, dam);
354 target->sct_defense = effdamage(target->sct_defense, dam);
355 pr("did %d%% damage to efficiency in %s\n",
356 oldeff - target->sct_effic,
357 xyas(target->sct_x, target->sct_y, player->cnum));
359 wu(0, target->sct_own,
360 "%s bombing raid did %d%% damage in %s\n",
361 cname(player->cnum), oldeff - target->sct_effic,
362 xyas(target->sct_x, target->sct_y, target->sct_own));
363 bridge_damaged(target);
365 collateral_damage(target->sct_x, target->sct_y, dam);
369 comm_bomb(struct emp_qelem *list, struct sctstr *target)
376 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 if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
421 if (plp->bombs || nuk_on_plane(&plp->plane) >= 0)
422 dam += pln_damage(&plp->plane, target->sct_x, target->sct_y,
425 if (dam <= 0) /* dam == 0 if only nukes were delivered */
427 getsect(target->sct_x, target->sct_y, §);
429 before = target->sct_item[ip->i_uid];
430 target->sct_item[ip->i_uid] = amt = commdamage(before, dam, ip->i_uid);
432 b = 100.0 * (1.0 - (double)amt / (double)before);
435 pr("did %.2f%% damage to %s in %s\n",
436 b, ip->i_name, xyas(target->sct_x, target->sct_y, player->cnum));
437 nreport(player->cnum, N_SCT_BOMB, target->sct_own, 1);
438 if (target->sct_own != 0)
439 wu(0, target->sct_own,
440 "%s precision bombing raid did %.2f%% damage to %s in %s\n",
441 cname(player->cnum), b, ip->i_name,
442 xyas(target->sct_x, target->sct_y, target->sct_own));
444 collateral_damage(target->sct_x, target->sct_y, dam);
448 ship_bomb(struct emp_qelem *list, struct sctstr *target)
455 struct emp_qelem *qp, *next;
459 struct shiplist *head = NULL;
467 for (qp = list->q_forw; qp != list; qp = next) {
469 free_shiplist(&head);
470 plp = (struct plist *)qp;
471 if (changed_plane_aborts(plp))
473 if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
475 if (plp->pcp->pl_flags & P_A)
476 nships = asw_shipsatxy(target->sct_x, target->sct_y, 0, 0,
479 nships = shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
481 pr("%s could not find any ships!\n", prplane(&plp->plane));
484 (void)sprintf(prompt, "%s, %d bombs. Target ('~' to skip)? ",
485 prplane(&plp->plane), plp->bombs);
488 if ((q = getstring(prompt, buf)) == 0)
495 if (plp->pcp->pl_flags & P_A)
496 print_shiplist(head);
498 shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
504 if ((!(plp->pcp->pl_flags & P_A) || on_shiplist(n, head)) &&
505 getship(n, &ship) && ship.shp_own &&
506 ship.shp_x == target->sct_x && ship.shp_y == target->sct_y)
509 pr("Ship #%d not spotted\n", n);
513 if ((plp->pcp->pl_flags & P_A) && !on_shiplist(shipno, head))
515 if (changed_plane_aborts(plp))
518 gun = shp_usable_guns(&ship);
519 mcp = &mchr[(int)ship.shp_type];
520 if (gun > 0 && !(mcp->m_flags & M_SUB)) {
521 flak = (int)(techfact(ship.shp_tech, gun) * 2.0);
522 PR(ship.shp_own, "Flak! Firing %d guns from ship %s\n",
523 flak, prship(&ship));
524 if (pinflak_planedamage(&plp->plane, plp->pcp, ship.shp_own, flak))
529 if (nuk_on_plane(&plp->plane) >= 0)
532 hitchance = pln_hitchance(&plp->plane,
533 shp_hardtarget(&ship), EF_SHIP);
534 pr("%d%% hitchance...", hitchance);
536 if (roll(100) <= hitchance) {
537 /* pinbombing is more accurate than normal bombing */
538 dam = 2 * pln_damage(&plp->plane, target->sct_x, target->sct_y,
542 /* Bombs that miss have to land somewhere! */
543 dam = pln_damage(&plp->plane, target->sct_x, target->sct_y,
545 collateral_damage(target->sct_x, target->sct_y, dam);
548 if (dam <= 0) /* dam == 0 if only nukes were delivered */
550 if (mcp->m_flags & M_SUB)
551 nreport(player->cnum, N_SUB_BOMB, ship.shp_own, 1);
553 nreport(player->cnum, N_SHP_BOMB, ship.shp_own, 1);
555 wu(0, ship.shp_own, "%s bombs did %d damage to %s at %s\n",
556 cname(player->cnum), dam,
558 xyas(target->sct_x, target->sct_y, ship.shp_own));
561 check_retreat_and_do_shipdamage(&ship, dam);
562 if (ship.shp_rflags & RET_BOMBED)
563 if (((ship.shp_rflags & RET_INJURED) == 0) || !dam)
564 retreat_ship(&ship, 'b');
565 putship(ship.shp_uid, &ship);
566 getship(ship.shp_uid, &ship);
568 pr("%s at %s sunk!\n",
570 xyas(target->sct_x, target->sct_y, player->cnum));
572 collateral_damage(target->sct_x, target->sct_y, dam / 2);
575 free_shiplist(&head);
579 plane_bomb(struct emp_qelem *list, struct sctstr *target)
586 struct emp_qelem *qp, *next;
595 for (qp = list->q_forw; qp != list; qp = next) {
597 plp = (struct plist *)qp;
598 if (changed_plane_aborts(plp))
600 if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
602 nplanes = planesatxy(target->sct_x, target->sct_y, 0, 0);
604 pr("%s could not find any planes!\n", prplane(&plp->plane));
607 (void)sprintf(prompt, "%s, %d bombs. Target ('~' to skip)? ",
608 prplane(&plp->plane), plp->bombs);
610 while (planeno < 0) {
611 if ((q = getstring(prompt, buf)) == 0)
618 planesatxy(target->sct_x, target->sct_y, 0, 0);
624 if (getplane(n, &plane) &&
625 plane.pln_x == target->sct_x &&
626 plane.pln_y == target->sct_y &&
627 plane.pln_ship < 0 && plane.pln_land < 0 &&
628 !(plane.pln_flags & PLN_LAUNCHED))
631 pr("Plane #%d not spotted\n", n);
635 if (changed_plane_aborts(plp))
638 if (nuk_on_plane(&plp->plane) >= 0)
641 hitchance = pln_hitchance(&plp->plane, 0, EF_PLANE);
642 pr("%d%% hitchance...", hitchance);
644 if (roll(100) <= hitchance) {
645 /* pinbombing is more accurate than normal bombing */
646 dam = 2 * pln_damage(&plp->plane, target->sct_x, target->sct_y,
650 /* Bombs that miss have to land somewhere! */
651 dam = pln_damage(&plp->plane, target->sct_x, target->sct_y,
653 collateral_damage(target->sct_x, target->sct_y, dam);
656 if (dam <= 0) /* dam == 0 if only nukes were delivered */
661 if (dam > plane.pln_effic)
664 plane.pln_effic -= dam;
665 plane.pln_mobil = (dam * plane.pln_mobil / 100.0);
666 if (own == player->cnum) {
667 pr("%s reports %d%% damage\n", prplane(&plane), dam);
671 "%s pinpoint bombing raid did %d%% damage to %s\n",
672 cname(player->cnum), dam, prplane(&plane));
674 nreport(player->cnum, N_DOWN_PLANE, own, 1);
676 wu(0, own, "%s bombs did %d%% damage to %s at %s\n",
677 cname(player->cnum), dam, prplane(&plane),
678 xyas(target->sct_x, target->sct_y, own));
679 putplane(plane.pln_uid, &plane);
680 collateral_damage(target->sct_x, target->sct_y, dam);
685 land_bomb(struct emp_qelem *list, struct sctstr *target)
694 struct emp_qelem *qp, *next;
696 int aaf, flak, hitchance;
701 for (qp = list->q_forw; qp != list; qp = next) {
703 plp = (struct plist *)qp;
704 if (changed_plane_aborts(plp))
706 if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
708 nunits = unitsatxy(target->sct_x, target->sct_y, 0, 0);
710 pr("%s could not find any units!\n", prplane(&plp->plane));
713 (void)sprintf(prompt, "%s, %d bombs. Target ('~' to skip)? ",
714 prplane(&plp->plane), plp->bombs);
717 if ((q = getstring(prompt, buf)) == 0)
724 unitsatxy(target->sct_x, target->sct_y, 0, 0);
730 if (getland(n, &land) && land.lnd_own &&
731 land.lnd_ship < 0 && land.lnd_land < 0 &&
732 land.lnd_x == target->sct_x && land.lnd_y == target->sct_y)
735 pr("Unit #%d not spotted\n", n);
739 if (changed_plane_aborts(plp))
742 aaf = lnd_aaf(&land);
744 flak = roundavg(techfact(land.lnd_tech,
745 aaf * 3.0 * land.lnd_effic / 100.0));
747 "Flak! Firing flak guns from unit %s (aa rating %d)\n",
749 if (pinflak_planedamage(&plp->plane, plp->pcp, land.lnd_own, flak))
754 if (nuk_on_plane(&plp->plane) >= 0)
757 hitchance = pln_hitchance(&plp->plane,
758 lnd_hardtarget(&land), EF_LAND);
759 pr("%d%% hitchance...", hitchance);
761 if (roll(100) <= hitchance) {
762 dam = 2 * pln_damage(&plp->plane, target->sct_x, target->sct_y,
766 /* Bombs that miss have to land somewhere! */
767 dam = pln_damage(&plp->plane, target->sct_x, target->sct_y,
769 collateral_damage(target->sct_x, target->sct_y, dam);
772 if (dam <= 0) /* dam == 0 if only nukes were delivered */
777 mpr(own, "%s pinpoint bombing raid did %d damage to %s\n",
778 cname(player->cnum), dam, prland(&land));
779 check_retreat_and_do_landdamage(&land, dam);
781 if (land.lnd_rflags & RET_BOMBED)
782 if (((land.lnd_rflags & RET_INJURED) == 0) || !dam)
783 retreat_land(&land, 'b');
784 nreport(player->cnum, N_UNIT_BOMB, own, 1);
785 putland(land.lnd_uid, &land);
786 collateral_damage(target->sct_x, target->sct_y, dam);
791 strat_bomb(struct emp_qelem *list, struct sctstr *target)
795 struct emp_qelem *qp;
799 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
800 plp = (struct plist *)qp;
801 if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
803 if (plp->bombs || nuk_on_plane(&plp->plane) >= 0)
804 dam += pln_damage(&plp->plane, target->sct_x, target->sct_y,
807 if (dam <= 0) /* dam == 0 if only nukes were delivered */
809 getsect(target->sct_x, target->sct_y, §);
812 wu(0, target->sct_own, "%s bombing raid did %d damage in %s\n",
813 cname(player->cnum), PERCENT_DAMAGE(dam),
814 xyas(target->sct_x, target->sct_y, target->sct_own));
816 sectdamage(target, dam);
818 pr("did %d damage in %s\n", PERCENT_DAMAGE(dam),
819 xyas(target->sct_x, target->sct_y, player->cnum));
824 changed_plane_aborts(struct plist *plp)
826 if (check_plane_ok(&plp->plane))
828 getplane(plp->plane.pln_uid, &plp->plane);
834 pinflak_planedamage(struct plnstr *pp, struct plchrstr *pcp, natid from,
843 dam = ac_flak_dam(flak, pln_def(pp), pcp->pl_flags);
845 plane_owner = pp->pln_own;
849 memset(dmess, 0, sizeof(dmess));
853 if (eff < PLANE_MINEFF) {
854 sprintf(dmess, " -- shot down");
856 } else if (chance((100 - eff) / 100.0)) {
857 sprintf(dmess, " -- aborted with %d%% damage", 100 - eff);
860 PR(plane_owner, " Flak! %s %s takes %d%s.\n",
861 cname(pp->pln_own), prplane(pp), dam, dmess);
866 nreport(from, N_DOWN_PLANE, pp->pln_own, 1);
868 putplane(pp->pln_uid, pp);