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 *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)
197 type = target->sct_type;
199 pr("Target sector is a %s constructed %s\n",
200 effadv((int)target->sct_effic), dcp->d_name);
202 nships = shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
203 if (pln_caps(list) & P_A) {
204 nsubs = shipsatxy(target->sct_x, target->sct_y, M_SUB, 0, 1);
206 pr("Some subs are present in the sector.\n");
208 nplanes = planesatxy(target->sct_x, target->sct_y, 0, 0);
209 nunits = unitsatxy(target->sct_x, target->sct_y, 0, 0);
211 p = getstring("Bomb what? (ship, plane, land unit, efficiency, commodities) ",
224 natp = getnatp(player->cnum);
225 if (target->sct_own) {
226 rel = getrel(natp, target->sct_own);
227 if ((rel != AT_WAR) && (player->cnum != target->sct_own)
228 && (target->sct_own) &&
229 (target->sct_oldown != player->cnum)) {
230 pr("You're not at war with them!\n");
236 pr("no units there\n");
239 land_bomb(list, target);
243 natp = getnatp(player->cnum);
244 if (target->sct_own) {
245 rel = getrel(natp, target->sct_own);
246 if ((rel != AT_WAR) && (player->cnum != target->sct_own)
247 && (target->sct_own) &&
248 (target->sct_oldown != player->cnum)) {
249 pr("You're not at war with them!\n");
255 pr("no planes there\n");
258 plane_bomb(list, target);
262 if (pln_caps(list) & P_A) {
264 pr("no ships there\n");
268 pr("no ships there\n");
272 ship_bomb(list, target);
276 natp = getnatp(player->cnum);
277 if (target->sct_own) {
278 rel = getrel(natp, target->sct_own);
279 if ((rel != AT_WAR) && (player->cnum != target->sct_own)
280 && (target->sct_own) &&
281 (target->sct_oldown != player->cnum)) {
282 pr("You're not at war with them!\n");
288 for (i = 0; i < nbomb; i++) {
289 if (!target->sct_item[bombcomm[i]])
294 pr("No bombable commodities in %s\n",
295 xyas(target->sct_x, target->sct_y, player->cnum));
298 comm_bomb(list, target);
302 natp = getnatp(player->cnum);
303 if (target->sct_own) {
304 rel = getrel(natp, target->sct_own);
305 if ((rel != AT_WAR) && (player->cnum != target->sct_own)
306 && (target->sct_own) &&
307 (target->sct_oldown != player->cnum)) {
308 pr("You're not at war with them!\n");
313 eff_bomb(list, target);
316 pr("Aborting mission.\n");
319 pr("Bad target type.\n");
325 eff_bomb(struct emp_qelem *list, struct sctstr *target)
328 struct emp_qelem *qp;
333 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
334 plp = (struct plist *)qp;
335 if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
337 if (plp->bombs || nuk_on_plane(&plp->plane) >= 0)
338 dam += pln_damage(&plp->plane, target->sct_x, target->sct_y,
341 if (dam <= 0) /* dam == 0 if only nukes were delivered */
343 getsect(target->sct_x, target->sct_y, §);
345 oldeff = target->sct_effic;
346 target->sct_effic = effdamage(target->sct_effic, dam);
347 target->sct_avail = effdamage(target->sct_avail, dam);
348 target->sct_road = effdamage(target->sct_road, dam);
349 target->sct_rail = effdamage(target->sct_rail, dam);
350 target->sct_defense = effdamage(target->sct_defense, dam);
351 pr("did %d%% damage to efficiency in %s\n",
352 oldeff - target->sct_effic,
353 xyas(target->sct_x, target->sct_y, player->cnum));
355 wu(0, target->sct_own,
356 "%s bombing raid did %d%% damage in %s\n",
357 cname(player->cnum), oldeff - target->sct_effic,
358 xyas(target->sct_x, target->sct_y, target->sct_own));
359 bridge_damaged(target);
361 collateral_damage(target->sct_x, target->sct_y, dam);
365 comm_bomb(struct emp_qelem *list, struct sctstr *target)
372 struct emp_qelem *qp;
377 for (i = 0; i < nbomb; i++) {
378 if (target->sct_item[bombcomm[i]] == 0)
380 if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
382 ip = &ichr[bombcomm[i]];
383 pr("some %s\n", ip->i_name);
386 ip = whatitem(NULL, "commodity to bomb? ");
392 for (i = 0; i < nbomb; i++) {
393 if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
395 if (&ichr[bombcomm[i]] == ip)
399 pr("You can't bomb %s!\n", ip->i_name);
400 for (i = 0; i < nbomb; i++) {
401 if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
403 pr("%s%s", i == 0 ? "Bombable: " : ", ",
404 ichr[bombcomm[i]].i_name);
410 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
411 plp = (struct plist *)qp;
412 if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
414 if (plp->bombs || nuk_on_plane(&plp->plane) >= 0)
415 dam += pln_damage(&plp->plane, target->sct_x, target->sct_y,
418 if (dam <= 0) /* dam == 0 if only nukes were delivered */
420 getsect(target->sct_x, target->sct_y, §);
422 before = target->sct_item[ip->i_uid];
423 target->sct_item[ip->i_uid] = amt = commdamage(before, dam, ip->i_uid);
425 b = 100.0 * (1.0 - (double)amt / (double)before);
428 pr("did %.2f%% damage to %s in %s\n",
429 b, ip->i_name, xyas(target->sct_x, target->sct_y, player->cnum));
430 nreport(player->cnum, N_SCT_BOMB, target->sct_own, 1);
431 if (target->sct_own != 0)
432 wu(0, target->sct_own,
433 "%s precision bombing raid did %.2f%% damage to %s in %s\n",
434 cname(player->cnum), b, ip->i_name,
435 xyas(target->sct_x, target->sct_y, target->sct_own));
437 collateral_damage(target->sct_x, target->sct_y, dam);
441 ship_bomb(struct emp_qelem *list, struct sctstr *target)
448 struct emp_qelem *qp;
452 struct shiplist *head = NULL;
460 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
461 free_shiplist(&head);
462 plp = (struct plist *)qp;
463 if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
465 if (plp->pcp->pl_flags & P_A)
466 nships = asw_shipsatxy(target->sct_x, target->sct_y, 0, 0,
469 nships = shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
471 pr("%s could not find any ships!\n", prplane(&plp->plane));
474 (void)sprintf(prompt, "%s, %d bombs. Target ('~' to skip)? ",
475 prplane(&plp->plane), plp->bombs);
478 if ((q = getstring(prompt, buf)) == 0)
485 if (plp->pcp->pl_flags & P_A)
486 print_shiplist(head);
488 shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
494 if ((!(plp->pcp->pl_flags & P_A) || on_shiplist(n, head)) &&
495 getship(n, &ship) && ship.shp_own &&
496 ship.shp_x == target->sct_x && ship.shp_y == target->sct_y)
499 pr("Ship #%d not spotted\n", n);
503 if ((plp->pcp->pl_flags & P_A) && !on_shiplist(shipno, head))
506 gun = shp_usable_guns(&ship);
507 mcp = &mchr[(int)ship.shp_type];
508 if (gun > 0 && !(mcp->m_flags & M_SUB)) {
509 flak = (int)(techfact(ship.shp_tech, gun) * 2.0);
510 PR(ship.shp_own, "Flak! Firing %d guns from ship %s\n",
511 flak, prship(&ship));
512 if (pinflak_planedamage(&plp->plane, plp->pcp, ship.shp_own, flak))
517 if (nuk_on_plane(&plp->plane) >= 0)
520 hitchance = pln_hitchance(&plp->plane,
521 shp_hardtarget(&ship), EF_SHIP);
522 pr("%d%% hitchance...", hitchance);
524 if (roll(100) <= hitchance) {
525 /* pinbombing is more accurate than normal bombing */
526 dam = 2 * pln_damage(&plp->plane, target->sct_x, target->sct_y,
530 /* Bombs that miss have to land somewhere! */
531 dam = pln_damage(&plp->plane, target->sct_x, target->sct_y,
533 collateral_damage(target->sct_x, target->sct_y, dam);
536 if (dam <= 0) /* dam == 0 if only nukes were delivered */
538 if (mcp->m_flags & M_SUB)
539 nreport(player->cnum, N_SUB_BOMB, ship.shp_own, 1);
541 nreport(player->cnum, N_SHP_BOMB, ship.shp_own, 1);
543 wu(0, ship.shp_own, "%s bombs did %d damage to %s at %s\n",
544 cname(player->cnum), dam,
546 xyas(target->sct_x, target->sct_y, ship.shp_own));
549 check_retreat_and_do_shipdamage(&ship, dam);
550 if (ship.shp_rflags & RET_BOMBED)
551 if (((ship.shp_rflags & RET_INJURED) == 0) || !dam)
552 retreat_ship(&ship, 'b');
553 putship(ship.shp_uid, &ship);
554 getship(ship.shp_uid, &ship);
556 pr("%s at %s sunk!\n",
558 xyas(target->sct_x, target->sct_y, player->cnum));
560 collateral_damage(target->sct_x, target->sct_y, dam / 2);
563 free_shiplist(&head);
567 plane_bomb(struct emp_qelem *list, struct sctstr *target)
574 struct emp_qelem *qp;
583 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
584 plp = (struct plist *)qp;
585 if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
587 nplanes = planesatxy(target->sct_x, target->sct_y, 0, 0);
589 pr("%s could not find any planes!\n", prplane(&plp->plane));
592 (void)sprintf(prompt, "%s, %d bombs. Target ('~' to skip)? ",
593 prplane(&plp->plane), plp->bombs);
595 while (planeno < 0) {
596 if ((q = getstring(prompt, buf)) == 0)
603 planesatxy(target->sct_x, target->sct_y, 0, 0);
609 if (getplane(n, &plane) &&
610 plane.pln_x == target->sct_x &&
611 plane.pln_y == target->sct_y &&
612 plane.pln_ship < 0 && plane.pln_land < 0 &&
613 !(plane.pln_flags & PLN_LAUNCHED))
616 pr("Plane #%d not spotted\n", n);
621 if (nuk_on_plane(&plp->plane) >= 0)
624 hitchance = pln_hitchance(&plp->plane, 0, EF_PLANE);
625 pr("%d%% hitchance...", hitchance);
627 if (roll(100) <= hitchance) {
628 /* pinbombing is more accurate than normal bombing */
629 dam = 2 * pln_damage(&plp->plane, target->sct_x, target->sct_y,
633 /* Bombs that miss have to land somewhere! */
634 dam = pln_damage(&plp->plane, target->sct_x, target->sct_y,
636 collateral_damage(target->sct_x, target->sct_y, dam);
639 if (dam <= 0) /* dam == 0 if only nukes were delivered */
644 if (dam > plane.pln_effic)
647 plane.pln_effic -= dam;
648 plane.pln_mobil = (dam * plane.pln_mobil / 100.0);
649 if (own == player->cnum) {
650 pr("%s reports %d%% damage\n", prplane(&plane), dam);
654 "%s pinpoint bombing raid did %d%% damage to %s\n",
655 cname(player->cnum), dam, prplane(&plane));
657 nreport(player->cnum, N_DOWN_PLANE, own, 1);
659 wu(0, own, "%s bombs did %d%% damage to %s at %s\n",
660 cname(player->cnum), dam, prplane(&plane),
661 xyas(target->sct_x, target->sct_y, own));
662 putplane(plane.pln_uid, &plane);
663 collateral_damage(target->sct_x, target->sct_y, dam);
668 land_bomb(struct emp_qelem *list, struct sctstr *target)
677 struct emp_qelem *qp;
679 int aaf, flak, hitchance;
684 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
685 plp = (struct plist *)qp;
686 if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
688 nunits = unitsatxy(target->sct_x, target->sct_y, 0, 0);
690 pr("%s could not find any units!\n", prplane(&plp->plane));
693 (void)sprintf(prompt, "%s, %d bombs. Target ('~' to skip)? ",
694 prplane(&plp->plane), plp->bombs);
697 if ((q = getstring(prompt, buf)) == 0)
704 unitsatxy(target->sct_x, target->sct_y, 0, 0);
710 if (getland(n, &land) && land.lnd_own &&
711 land.lnd_ship < 0 && land.lnd_land < 0 &&
712 land.lnd_x == target->sct_x && land.lnd_y == target->sct_y)
715 pr("Unit #%d not spotted\n", n);
720 aaf = lnd_aaf(&land);
722 flak = roundavg(techfact(land.lnd_tech,
723 aaf * 3.0 * land.lnd_effic / 100.0));
725 "Flak! Firing flak guns from unit %s (aa rating %d)\n",
727 if (pinflak_planedamage(&plp->plane, plp->pcp, land.lnd_own, flak))
732 if (nuk_on_plane(&plp->plane) >= 0)
735 hitchance = pln_hitchance(&plp->plane,
736 lnd_hardtarget(&land), EF_LAND);
737 pr("%d%% hitchance...", hitchance);
739 if (roll(100) <= hitchance) {
740 dam = 2 * pln_damage(&plp->plane, target->sct_x, target->sct_y,
744 /* Bombs that miss have to land somewhere! */
745 dam = pln_damage(&plp->plane, target->sct_x, target->sct_y,
747 collateral_damage(target->sct_x, target->sct_y, dam);
750 if (dam <= 0) /* dam == 0 if only nukes were delivered */
755 mpr(own, "%s pinpoint bombing raid did %d damage to %s\n",
756 cname(player->cnum), dam, prland(&land));
757 check_retreat_and_do_landdamage(&land, dam);
759 if (land.lnd_rflags & RET_BOMBED)
760 if (((land.lnd_rflags & RET_INJURED) == 0) || !dam)
761 retreat_land(&land, 'b');
762 nreport(player->cnum, N_UNIT_BOMB, own, 1);
763 putland(land.lnd_uid, &land);
764 collateral_damage(target->sct_x, target->sct_y, dam);
769 strat_bomb(struct emp_qelem *list, struct sctstr *target)
773 struct emp_qelem *qp;
777 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
778 plp = (struct plist *)qp;
779 if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
781 if (plp->bombs || nuk_on_plane(&plp->plane) >= 0)
782 dam += pln_damage(&plp->plane, target->sct_x, target->sct_y,
785 if (dam <= 0) /* dam == 0 if only nukes were delivered */
787 getsect(target->sct_x, target->sct_y, §);
790 wu(0, target->sct_own, "%s bombing raid did %d damage in %s\n",
791 cname(player->cnum), PERCENT_DAMAGE(dam),
792 xyas(target->sct_x, target->sct_y, target->sct_own));
794 sectdamage(target, dam);
796 pr("did %d damage in %s\n", PERCENT_DAMAGE(dam),
797 xyas(target->sct_x, target->sct_y, player->cnum));
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);