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;
99 struct emp_qelem *qp, *next;
105 if (get_planes(&ni_bomb, &ni_esc, player->argp[1], player->argp[2]) < 0)
107 p = getstarg(player->argp[3], "pinpoint, or strategic? ", buf);
111 if (!strchr("ps", mission))
113 if (!get_assembly_point(player->argp[4], &ap_sect, buf))
117 if (!getpath(flightpath, player->argp[5], ax, ay, 0, 0, P_FLYING)
122 (void)pathtoxy(flightpath, &tx, &ty, fcost);
123 pr("target sector is %s\n", xyas(tx, ty, player->cnum));
124 getsect(tx, ty, &target);
126 ap_to_target = strlen(flightpath);
127 if (flightpath[ap_to_target - 1] == 'h')
129 pr("range to target is %d\n", ap_to_target);
131 * select planes within range
133 pln_sel(&ni_bomb, &bomb_list, &ap_sect, ap_to_target,
134 2, wantflags, P_M | P_O);
135 pln_sel(&ni_esc, &esc_list, &ap_sect, ap_to_target,
136 2, wantflags | P_F | P_ESC, P_M | P_O);
138 * now arm and equip the bombers, transports, whatever.
140 pln_arm(&bomb_list, 2 * ap_to_target, mission, ip, 0);
141 if (QEMPTY(&bomb_list)) {
142 pr("No planes could be equipped for the mission.\n");
145 pln_arm(&esc_list, 2 * ap_to_target, mission, ip, P_F | P_ESC);
146 ac_encounter(&bomb_list, &esc_list, ax, ay, flightpath, 0);
147 if (QEMPTY(&bomb_list)) {
148 pr("No planes got through fighter defenses\n");
149 } else if (target.sct_type == SCT_SANCT) {
150 pr("You can't bomb that sector!\n");
154 pin_bomb(&bomb_list, &target);
155 for (qp = bomb_list.q_forw; qp != &bomb_list; qp = next) {
157 plp = (struct plist *)qp;
158 changed_plane_aborts(plp);
160 for (qp = esc_list.q_forw; qp != &esc_list; qp = next) {
162 plp = (struct plist *)qp;
163 changed_plane_aborts(plp);
168 natp = getnatp(player->cnum);
169 if (target.sct_own) {
170 rel = getrel(natp, target.sct_own);
171 if ((rel != AT_WAR) && (player->cnum != target.sct_own)
172 && (target.sct_own) &&
173 (target.sct_oldown != player->cnum)) {
174 pr("You're not at war with them!\n");
181 nreport(player->cnum, N_SCT_BOMB, target.sct_own, 1);
182 strat_bomb(&bomb_list, &target);
194 pin_bomb(struct emp_qelem *list, struct sctstr *target)
210 type = target->sct_type;
212 pr("Target sector is a %s constructed %s\n",
213 effadv((int)target->sct_effic), dcp->d_name);
215 nships = shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
216 if (pln_caps(list) & P_A) {
217 nsubs = shipsatxy(target->sct_x, target->sct_y, M_SUB, 0, 1);
219 pr("Some subs are present in the sector.\n");
221 nplanes = planesatxy(target->sct_x, target->sct_y, 0, 0);
222 nunits = unitsatxy(target->sct_x, target->sct_y, 0, 0);
224 p = getstring("Bomb what? (ship, plane, land unit, efficiency, commodities) ",
237 natp = getnatp(player->cnum);
238 if (target->sct_own) {
239 rel = getrel(natp, target->sct_own);
240 if ((rel != AT_WAR) && (player->cnum != target->sct_own)
241 && (target->sct_own) &&
242 (target->sct_oldown != player->cnum)) {
243 pr("You're not at war with them!\n");
249 pr("no units there\n");
252 land_bomb(list, target);
256 natp = getnatp(player->cnum);
257 if (target->sct_own) {
258 rel = getrel(natp, target->sct_own);
259 if ((rel != AT_WAR) && (player->cnum != target->sct_own)
260 && (target->sct_own) &&
261 (target->sct_oldown != player->cnum)) {
262 pr("You're not at war with them!\n");
268 pr("no planes there\n");
271 plane_bomb(list, target);
275 if (pln_caps(list) & P_A) {
277 pr("no ships there\n");
281 pr("no ships there\n");
285 ship_bomb(list, target);
289 natp = getnatp(player->cnum);
290 if (target->sct_own) {
291 rel = getrel(natp, target->sct_own);
292 if ((rel != AT_WAR) && (player->cnum != target->sct_own)
293 && (target->sct_own) &&
294 (target->sct_oldown != player->cnum)) {
295 pr("You're not at war with them!\n");
301 for (i = 0; i < nbomb; i++) {
302 if (!target->sct_item[bombcomm[i]])
307 pr("No bombable commodities in %s\n",
308 xyas(target->sct_x, target->sct_y, player->cnum));
311 comm_bomb(list, target);
315 natp = getnatp(player->cnum);
316 if (target->sct_own) {
317 rel = getrel(natp, target->sct_own);
318 if ((rel != AT_WAR) && (player->cnum != target->sct_own)
319 && (target->sct_own) &&
320 (target->sct_oldown != player->cnum)) {
321 pr("You're not at war with them!\n");
326 eff_bomb(list, target);
329 pr("Aborting mission.\n");
332 pr("Bad target type.\n");
338 eff_bomb(struct emp_qelem *list, struct sctstr *target)
341 struct emp_qelem *qp, *next;
346 for (qp = list->q_forw; qp != list; qp = next) {
348 plp = (struct plist *)qp;
349 if (changed_plane_aborts(plp))
351 if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
353 if (plp->bombs || nuk_on_plane(&plp->plane) >= 0)
354 dam += pln_damage(&plp->plane, target->sct_x, target->sct_y,
357 if (dam <= 0) /* dam == 0 if only nukes were delivered */
359 getsect(target->sct_x, target->sct_y, §);
361 oldeff = target->sct_effic;
362 target->sct_effic = effdamage(target->sct_effic, dam);
363 target->sct_avail = effdamage(target->sct_avail, dam);
364 target->sct_road = effdamage(target->sct_road, dam);
365 target->sct_rail = effdamage(target->sct_rail, dam);
366 target->sct_defense = effdamage(target->sct_defense, dam);
367 pr("did %d%% damage to efficiency in %s\n",
368 oldeff - target->sct_effic,
369 xyas(target->sct_x, target->sct_y, player->cnum));
371 wu(0, target->sct_own,
372 "%s bombing raid did %d%% damage in %s\n",
373 cname(player->cnum), oldeff - target->sct_effic,
374 xyas(target->sct_x, target->sct_y, target->sct_own));
375 bridge_damaged(target);
377 collateral_damage(target->sct_x, target->sct_y, dam);
381 comm_bomb(struct emp_qelem *list, struct sctstr *target)
388 struct emp_qelem *qp, *next;
393 for (i = 0; i < nbomb; i++) {
394 if (target->sct_item[bombcomm[i]] == 0)
396 if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
398 ip = &ichr[bombcomm[i]];
399 pr("some %s\n", ip->i_name);
402 ip = whatitem(NULL, "commodity to bomb? ");
408 for (i = 0; i < nbomb; i++) {
409 if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
411 if (&ichr[bombcomm[i]] == ip)
415 pr("You can't bomb %s!\n", ip->i_name);
416 for (i = 0; i < nbomb; i++) {
417 if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
419 pr("%s%s", i == 0 ? "Bombable: " : ", ",
420 ichr[bombcomm[i]].i_name);
426 for (qp = list->q_forw; qp != list; qp = next) {
428 plp = (struct plist *)qp;
429 if (changed_plane_aborts(plp))
431 if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
433 if (plp->bombs || nuk_on_plane(&plp->plane) >= 0)
434 dam += pln_damage(&plp->plane, target->sct_x, target->sct_y,
437 if (dam <= 0) /* dam == 0 if only nukes were delivered */
439 getsect(target->sct_x, target->sct_y, §);
441 before = target->sct_item[ip->i_uid];
442 target->sct_item[ip->i_uid] = amt = commdamage(before, dam, ip->i_uid);
444 b = 100.0 * (1.0 - (double)amt / (double)before);
447 pr("did %.2f%% damage to %s in %s\n",
448 b, ip->i_name, xyas(target->sct_x, target->sct_y, player->cnum));
449 nreport(player->cnum, N_SCT_BOMB, target->sct_own, 1);
450 if (target->sct_own != 0)
451 wu(0, target->sct_own,
452 "%s precision bombing raid did %.2f%% damage to %s in %s\n",
453 cname(player->cnum), b, ip->i_name,
454 xyas(target->sct_x, target->sct_y, target->sct_own));
456 collateral_damage(target->sct_x, target->sct_y, dam);
460 ship_bomb(struct emp_qelem *list, struct sctstr *target)
467 struct emp_qelem *qp, *next;
471 struct shiplist *head = NULL;
479 for (qp = list->q_forw; qp != list; qp = next) {
481 free_shiplist(&head);
482 plp = (struct plist *)qp;
483 if (changed_plane_aborts(plp))
485 if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
487 if (plp->pcp->pl_flags & P_A)
488 nships = asw_shipsatxy(target->sct_x, target->sct_y, 0, 0,
491 nships = shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
493 pr("%s could not find any ships!\n", prplane(&plp->plane));
496 (void)sprintf(prompt, "%s, %d bombs. Target ('~' to skip)? ",
497 prplane(&plp->plane), plp->bombs);
500 if (!(q = getstring(prompt, buf)))
507 if (plp->pcp->pl_flags & P_A)
508 print_shiplist(head);
510 shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
516 if ((!(plp->pcp->pl_flags & P_A) || on_shiplist(n, head)) &&
517 getship(n, &ship) && ship.shp_own &&
518 ship.shp_x == target->sct_x && ship.shp_y == target->sct_y)
521 pr("Ship #%d not spotted\n", n);
525 if ((plp->pcp->pl_flags & P_A) && !on_shiplist(shipno, head))
527 if (changed_plane_aborts(plp))
530 gun = shp_usable_guns(&ship);
531 mcp = &mchr[(int)ship.shp_type];
532 if (gun > 0 && !(mcp->m_flags & M_SUB)) {
533 flak = (int)(techfact(ship.shp_tech, gun) * 2.0);
534 PR(ship.shp_own, "Flak! Firing %d guns from ship %s\n",
535 flak, prship(&ship));
536 if (pinflak_planedamage(&plp->plane, plp->pcp, ship.shp_own, flak))
541 if (nuk_on_plane(&plp->plane) >= 0)
544 hitchance = pln_hitchance(&plp->plane,
545 shp_hardtarget(&ship), EF_SHIP);
546 pr("%d%% hitchance...", hitchance);
548 if (roll(100) <= hitchance) {
549 /* pinbombing is more accurate than normal bombing */
550 dam = 2 * pln_damage(&plp->plane, target->sct_x, target->sct_y,
554 /* Bombs that miss have to land somewhere! */
555 dam = pln_damage(&plp->plane, target->sct_x, target->sct_y,
557 collateral_damage(target->sct_x, target->sct_y, dam);
560 if (dam <= 0) /* dam == 0 if only nukes were delivered */
562 if (mcp->m_flags & M_SUB)
563 nreport(player->cnum, N_SUB_BOMB, ship.shp_own, 1);
565 nreport(player->cnum, N_SHP_BOMB, ship.shp_own, 1);
567 wu(0, ship.shp_own, "%s bombs did %d damage to %s at %s\n",
568 cname(player->cnum), dam,
570 xyas(target->sct_x, target->sct_y, ship.shp_own));
573 check_retreat_and_do_shipdamage(&ship, dam);
574 if (ship.shp_rflags & RET_BOMBED)
575 if (((ship.shp_rflags & RET_INJURED) == 0) || !dam)
576 retreat_ship(&ship, 'b');
577 putship(ship.shp_uid, &ship);
578 getship(ship.shp_uid, &ship);
580 pr("%s at %s sunk!\n",
582 xyas(target->sct_x, target->sct_y, player->cnum));
584 collateral_damage(target->sct_x, target->sct_y, dam / 2);
587 free_shiplist(&head);
591 plane_bomb(struct emp_qelem *list, struct sctstr *target)
598 struct emp_qelem *qp, *next;
607 for (qp = list->q_forw; qp != list; qp = next) {
609 plp = (struct plist *)qp;
610 if (changed_plane_aborts(plp))
612 if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
614 nplanes = planesatxy(target->sct_x, target->sct_y, 0, 0);
616 pr("%s could not find any planes!\n", prplane(&plp->plane));
619 (void)sprintf(prompt, "%s, %d bombs. Target ('~' to skip)? ",
620 prplane(&plp->plane), plp->bombs);
622 while (planeno < 0) {
623 if (!(q = getstring(prompt, buf)))
630 planesatxy(target->sct_x, target->sct_y, 0, 0);
636 if (getplane(n, &plane) &&
637 plane.pln_x == target->sct_x &&
638 plane.pln_y == target->sct_y &&
639 plane.pln_ship < 0 && plane.pln_land < 0 &&
640 !(plane.pln_flags & PLN_LAUNCHED))
643 pr("Plane #%d not spotted\n", n);
647 if (changed_plane_aborts(plp))
650 if (nuk_on_plane(&plp->plane) >= 0)
653 hitchance = pln_hitchance(&plp->plane, 0, EF_PLANE);
654 pr("%d%% hitchance...", hitchance);
656 if (roll(100) <= hitchance) {
657 /* pinbombing is more accurate than normal bombing */
658 dam = 2 * pln_damage(&plp->plane, target->sct_x, target->sct_y,
662 /* Bombs that miss have to land somewhere! */
663 dam = pln_damage(&plp->plane, target->sct_x, target->sct_y,
665 collateral_damage(target->sct_x, target->sct_y, dam);
668 if (dam <= 0) /* dam == 0 if only nukes were delivered */
673 if (dam > plane.pln_effic)
676 plane.pln_effic -= dam;
677 plane.pln_mobil = (dam * plane.pln_mobil / 100.0);
678 if (own == player->cnum) {
679 pr("%s reports %d%% damage\n", prplane(&plane), dam);
683 "%s pinpoint bombing raid did %d%% damage to %s\n",
684 cname(player->cnum), dam, prplane(&plane));
686 nreport(player->cnum, N_DOWN_PLANE, own, 1);
688 wu(0, own, "%s bombs did %d%% damage to %s at %s\n",
689 cname(player->cnum), dam, prplane(&plane),
690 xyas(target->sct_x, target->sct_y, own));
691 putplane(plane.pln_uid, &plane);
692 collateral_damage(target->sct_x, target->sct_y, dam);
697 land_bomb(struct emp_qelem *list, struct sctstr *target)
706 struct emp_qelem *qp, *next;
708 int aaf, flak, hitchance;
713 for (qp = list->q_forw; qp != list; qp = next) {
715 plp = (struct plist *)qp;
716 if (changed_plane_aborts(plp))
718 if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
720 nunits = unitsatxy(target->sct_x, target->sct_y, 0, 0);
722 pr("%s could not find any units!\n", prplane(&plp->plane));
725 (void)sprintf(prompt, "%s, %d bombs. Target ('~' to skip)? ",
726 prplane(&plp->plane), plp->bombs);
729 if (!(q = getstring(prompt, buf)))
736 unitsatxy(target->sct_x, target->sct_y, 0, 0);
742 if (getland(n, &land) && land.lnd_own &&
743 land.lnd_ship < 0 && land.lnd_land < 0 &&
744 land.lnd_x == target->sct_x && land.lnd_y == target->sct_y)
747 pr("Unit #%d not spotted\n", n);
751 if (changed_plane_aborts(plp))
754 aaf = lnd_aaf(&land);
756 flak = roundavg(techfact(land.lnd_tech,
757 aaf * 3.0 * land.lnd_effic / 100.0));
759 "Flak! Firing flak guns from unit %s (aa rating %d)\n",
761 if (pinflak_planedamage(&plp->plane, plp->pcp, land.lnd_own, flak))
766 if (nuk_on_plane(&plp->plane) >= 0)
769 hitchance = pln_hitchance(&plp->plane,
770 lnd_hardtarget(&land), EF_LAND);
771 pr("%d%% hitchance...", hitchance);
773 if (roll(100) <= hitchance) {
774 dam = 2 * pln_damage(&plp->plane, target->sct_x, target->sct_y,
778 /* Bombs that miss have to land somewhere! */
779 dam = pln_damage(&plp->plane, target->sct_x, target->sct_y,
781 collateral_damage(target->sct_x, target->sct_y, dam);
784 if (dam <= 0) /* dam == 0 if only nukes were delivered */
789 mpr(own, "%s pinpoint bombing raid did %d damage to %s\n",
790 cname(player->cnum), dam, prland(&land));
791 check_retreat_and_do_landdamage(&land, dam);
793 if (land.lnd_rflags & RET_BOMBED)
794 if (((land.lnd_rflags & RET_INJURED) == 0) || !dam)
795 retreat_land(&land, 'b');
796 nreport(player->cnum, N_UNIT_BOMB, own, 1);
797 putland(land.lnd_uid, &land);
798 collateral_damage(target->sct_x, target->sct_y, dam);
803 strat_bomb(struct emp_qelem *list, struct sctstr *target)
807 struct emp_qelem *qp;
811 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
812 plp = (struct plist *)qp;
813 if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
815 if (plp->bombs || nuk_on_plane(&plp->plane) >= 0)
816 dam += pln_damage(&plp->plane, target->sct_x, target->sct_y,
819 if (dam <= 0) /* dam == 0 if only nukes were delivered */
821 getsect(target->sct_x, target->sct_y, §);
824 wu(0, target->sct_own, "%s bombing raid did %d damage in %s\n",
825 cname(player->cnum), PERCENT_DAMAGE(dam),
826 xyas(target->sct_x, target->sct_y, target->sct_own));
828 sectdamage(target, dam);
830 pr("did %d damage in %s\n", PERCENT_DAMAGE(dam),
831 xyas(target->sct_x, target->sct_y, player->cnum));
836 changed_plane_aborts(struct plist *plp)
838 if (check_plane_ok(&plp->plane))
840 getplane(plp->plane.pln_uid, &plp->plane);
846 pinflak_planedamage(struct plnstr *pp, struct plchrstr *pcp, natid from,
855 dam = ac_flak_dam(flak, pln_def(pp), pcp->pl_flags);
857 plane_owner = pp->pln_own;
861 memset(dmess, 0, sizeof(dmess));
865 if (eff < PLANE_MINEFF) {
866 sprintf(dmess, " -- shot down");
868 } else if (chance((100 - eff) / 100.0)) {
869 sprintf(dmess, " -- aborted with %d%% damage", 100 - eff);
872 PR(plane_owner, " Flak! %s %s takes %d%s.\n",
873 cname(pp->pln_own), prplane(pp), dam, dmess);
878 nreport(from, N_DOWN_PLANE, pp->pln_own, 1);
880 putplane(pp->pln_uid, pp);