2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2015, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire 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 3 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, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * bomb.c: Fly bombing missions
29 * Known contributors to this file:
32 * Steve McClure, 1998-2000
33 * Markus Armbruster, 2004-2015
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;
100 if (get_planes(&ni_bomb, &ni_esc, player->argp[1], player->argp[2]) < 0)
102 p = getstarg(player->argp[3], "pinpoint, or strategic? ", buf);
106 if (!strchr("ps", mission))
108 if (!get_assembly_point(player->argp[4], &ap_sect, buf))
112 if (!getpath(flightpath, player->argp[5], ax, ay, 0, 0, MOB_FLY))
116 (void)pathtoxy(flightpath, &tx, &ty, fcost);
117 pr("target sector is %s\n", xyas(tx, ty, player->cnum));
118 getsect(tx, ty, &target);
119 ap_to_target = strlen(flightpath);
120 pr("range to target is %d\n", ap_to_target);
122 * select planes within range
124 pln_sel(&ni_bomb, &bomb_list, &ap_sect, ap_to_target, 2,
125 P_B | P_T, P_M | P_O);
126 pln_sel(&ni_esc, &esc_list, &ap_sect, ap_to_target, 2,
127 P_ESC | P_F, P_M | P_O);
129 * now arm and equip the bombers, transports, whatever.
131 pln_arm(&bomb_list, 2 * ap_to_target, mission, NULL);
132 if (QEMPTY(&bomb_list)) {
133 pr("No planes could be equipped for the mission.\n");
136 pln_arm(&esc_list, 2 * ap_to_target, 'e', NULL);
137 ac_encounter(&bomb_list, &esc_list, ax, ay, flightpath, 0);
138 if (QEMPTY(&bomb_list)) {
139 pr("No planes got through fighter defenses\n");
140 } else if (target.sct_type == SCT_SANCT) {
141 pr("You can't bomb that sector!\n");
145 pin_bomb(&bomb_list, &target);
146 for (qp = bomb_list.q_forw; qp != &bomb_list; qp = next) {
148 plp = (struct plist *)qp;
149 changed_plane_aborts(plp);
151 for (qp = esc_list.q_forw; qp != &esc_list; qp = next) {
153 plp = (struct plist *)qp;
154 changed_plane_aborts(plp);
158 nreport(player->cnum, N_SCT_BOMB, target.sct_own, 1);
159 strat_bomb(&bomb_list, &target);
171 pin_bomb(struct emp_qelem *list, struct sctstr *target)
185 type = target->sct_type;
187 pr("Target sector is a %s constructed %s\n",
188 effadv((int)target->sct_effic), dcp->d_name);
190 nships = shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
191 if (pln_caps(list) & P_A) {
192 nsubs = shipsatxy(target->sct_x, target->sct_y, M_SUB, 0, 1);
194 pr("Some subs are present in the sector.\n");
196 nplanes = planesatxy(target->sct_x, target->sct_y, 0, 0);
197 nunits = unitsatxy(target->sct_x, target->sct_y, 0, 0, 0);
199 p = getstring("Bomb what? (ship, plane, land unit, efficiency, commodities) ",
212 pr("no units there\n");
215 land_bomb(list, target);
219 pr("no planes there\n");
222 plane_bomb(list, target);
226 if (pln_caps(list) & P_A) {
228 pr("no ships there\n");
232 pr("no ships there\n");
236 ship_bomb(list, target);
239 for (i = 0; i < nbomb; i++) {
240 if (!target->sct_item[bombcomm[i]])
245 pr("No bombable commodities in %s\n",
246 xyas(target->sct_x, target->sct_y, player->cnum));
249 comm_bomb(list, target);
252 eff_bomb(list, target);
255 pr("Aborting mission.\n");
258 pr("Bad target type.\n");
264 eff_bomb(struct emp_qelem *list, struct sctstr *target)
267 struct emp_qelem *qp, *next;
271 for (qp = list->q_forw; qp != list; qp = next) {
273 plp = (struct plist *)qp;
274 if (changed_plane_aborts(plp))
276 dam += pln_damage(&plp->plane, 'p', "");
278 getsect(target->sct_x, target->sct_y, §);
280 oldeff = target->sct_effic;
281 target->sct_effic = effdamage(target->sct_effic, dam);
282 target->sct_avail = effdamage(target->sct_avail, dam);
283 target->sct_road = effdamage(target->sct_road, dam);
284 target->sct_rail = effdamage(target->sct_rail, dam);
285 target->sct_defense = effdamage(target->sct_defense, dam);
286 pr("did %d%% damage to efficiency in %s\n",
287 oldeff - target->sct_effic,
288 xyas(target->sct_x, target->sct_y, player->cnum));
289 if (target->sct_own != player->cnum)
291 "%s bombing raid did %d%% damage in %s\n",
292 cname(player->cnum), oldeff - target->sct_effic,
293 xyas(target->sct_x, target->sct_y, target->sct_own));
294 bridge_damaged(target);
296 collateral_damage(target->sct_x, target->sct_y, dam);
300 comm_bomb(struct emp_qelem *list, struct sctstr *target)
307 struct emp_qelem *qp, *next;
311 for (i = 0; i < nbomb; i++) {
312 if (target->sct_item[bombcomm[i]] == 0)
314 if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
316 ip = &ichr[bombcomm[i]];
317 pr("some %s\n", ip->i_name);
320 ip = whatitem(NULL, "commodity to bomb? ");
326 for (i = 0; i < nbomb; i++) {
327 if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
329 if (&ichr[bombcomm[i]] == ip)
333 pr("You can't bomb %s!\n", ip->i_name);
334 for (i = 0; i < nbomb; i++) {
335 if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
337 pr("%s%s", i == 0 ? "Bombable: " : ", ",
338 ichr[bombcomm[i]].i_name);
344 for (qp = list->q_forw; qp != list; qp = next) {
346 plp = (struct plist *)qp;
347 if (changed_plane_aborts(plp))
349 dam += pln_damage(&plp->plane, 'p', "");
351 getsect(target->sct_x, target->sct_y, §);
353 before = target->sct_item[ip->i_uid];
354 target->sct_item[ip->i_uid] = amt = commdamage(before, dam, ip->i_uid);
356 b = 100.0 * (1.0 - (double)amt / (double)before);
359 pr("did %.2f%% damage to %s in %s\n",
360 b, ip->i_name, xyas(target->sct_x, target->sct_y, player->cnum));
361 nreport(player->cnum, N_SCT_BOMB, target->sct_own, 1);
362 if (target->sct_own != player->cnum)
364 "%s precision bombing raid did %.2f%% damage to %s in %s\n",
365 cname(player->cnum), b, ip->i_name,
366 xyas(target->sct_x, target->sct_y, target->sct_own));
368 collateral_damage(target->sct_x, target->sct_y, dam);
372 ship_bomb(struct emp_qelem *list, struct sctstr *target)
379 struct emp_qelem *qp, *next;
383 struct shiplist *head = NULL;
390 for (qp = list->q_forw; qp != list; qp = next) {
392 free_shiplist(&head);
393 plp = (struct plist *)qp;
394 if (changed_plane_aborts(plp))
396 if (plp->pcp->pl_flags & P_A)
397 nships = asw_shipsatxy(target->sct_x, target->sct_y, 0, 0,
400 nships = shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
402 pr("%s could not find any ships!\n", prplane(&plp->plane));
405 (void)sprintf(prompt, "%s, %d bombs. Target ('~' to skip)? ",
406 prplane(&plp->plane), plp->load);
409 if (!(q = getstring(prompt, buf)))
416 if (plp->pcp->pl_flags & P_A)
417 print_shiplist(head);
419 shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
425 if ((!(plp->pcp->pl_flags & P_A) || on_shiplist(n, head)) &&
426 getship(n, &ship) && ship.shp_own &&
427 ship.shp_x == target->sct_x && ship.shp_y == target->sct_y)
430 pr("Ship #%d not spotted\n", n);
434 if ((plp->pcp->pl_flags & P_A) && !on_shiplist(shipno, head))
436 if (changed_plane_aborts(plp))
439 gun = shp_usable_guns(&ship);
440 mcp = &mchr[(int)ship.shp_type];
441 if (gun > 0 && !(mcp->m_flags & M_SUB)) {
442 flak = (int)(techfact(ship.shp_tech, gun) * 2.0);
443 mpr(ship.shp_own, "Flak! Firing %d guns from ship %s\n",
444 flak, prship(&ship));
445 if (pinflak_planedamage(&plp->plane, plp->pcp, ship.shp_own, flak))
449 if (nuk_on_plane(&plp->plane) >= 0)
452 hitchance = pln_hitchance(&plp->plane,
453 shp_hardtarget(&ship), EF_SHIP);
454 pr("%d%% hitchance...", hitchance);
456 if (pct_chance(hitchance)) {
457 /* pinbombing is more accurate than normal bombing */
458 dam = 2 * pln_damage(&plp->plane, 'p', "");
461 /* Bombs that miss have to land somewhere! */
462 dam = pln_damage(&plp->plane, 'p', 0);
463 collateral_damage(target->sct_x, target->sct_y, dam);
466 if (mcp->m_flags & M_SUB)
467 nreport(player->cnum, N_SUB_BOMB, ship.shp_own, 1);
469 nreport(player->cnum, N_SHP_BOMB, ship.shp_own, 1);
470 if (ship.shp_own != player->cnum) {
471 mpr(ship.shp_own, "%s bombs did %d damage to %s at %s\n",
472 cname(player->cnum), dam,
474 xyas(target->sct_x, target->sct_y, ship.shp_own));
476 shipdamage(&ship, dam);
477 if (ship.shp_effic < SHIP_MINEFF)
478 pr("%s at %s sunk!\n",
480 xyas(target->sct_x, target->sct_y, player->cnum));
481 if (dam && (ship.shp_rflags & RET_INJURED))
482 retreat_ship(&ship, ship.shp_own, 'i');
483 else if (ship.shp_rflags & RET_BOMBED)
484 retreat_ship(&ship, ship.shp_own, 'b');
485 putship(ship.shp_uid, &ship);
486 collateral_damage(target->sct_x, target->sct_y, dam / 2);
489 free_shiplist(&head);
493 plane_bomb(struct emp_qelem *list, struct sctstr *target)
500 struct emp_qelem *qp, *next;
508 for (qp = list->q_forw; qp != list; qp = next) {
510 plp = (struct plist *)qp;
511 if (changed_plane_aborts(plp))
513 nplanes = planesatxy(target->sct_x, target->sct_y, 0, 0);
515 pr("%s could not find any planes!\n", prplane(&plp->plane));
518 (void)sprintf(prompt, "%s, %d bombs. Target ('~' to skip)? ",
519 prplane(&plp->plane), plp->load);
521 while (planeno < 0) {
522 if (!(q = getstring(prompt, buf)))
529 planesatxy(target->sct_x, target->sct_y, 0, 0);
535 if (getplane(n, &plane) && plane.pln_own &&
536 plane.pln_x == target->sct_x &&
537 plane.pln_y == target->sct_y &&
538 plane.pln_ship < 0 && plane.pln_land < 0 &&
539 !(plane.pln_flags & PLN_LAUNCHED))
542 pr("Plane #%d not spotted\n", n);
546 if (changed_plane_aborts(plp))
548 if (nuk_on_plane(&plp->plane) >= 0)
551 hitchance = pln_hitchance(&plp->plane, 0, EF_PLANE);
552 pr("%d%% hitchance...", hitchance);
554 if (pct_chance(hitchance)) {
555 /* pinbombing is more accurate than normal bombing */
556 dam = 2 * pln_damage(&plp->plane, 'p', "");
559 /* Bombs that miss have to land somewhere! */
560 dam = pln_damage(&plp->plane, 'p', 0);
561 collateral_damage(target->sct_x, target->sct_y, dam);
567 if (dam > plane.pln_effic)
570 plane.pln_effic -= dam;
571 plane.pln_mobil = damage(plane.pln_mobil, dam);
572 mpr(own, "%s bombs did %d%% damage to %s at %s\n",
573 cname(player->cnum), dam, prplane(&plane),
574 xyas(target->sct_x, target->sct_y, own));
575 nreport(player->cnum, N_DOWN_PLANE, own, 1);
576 putplane(plane.pln_uid, &plane);
577 collateral_damage(target->sct_x, target->sct_y, dam);
582 land_bomb(struct emp_qelem *list, struct sctstr *target)
591 struct emp_qelem *qp, *next;
593 int aaf, flak, hitchance;
597 for (qp = list->q_forw; qp != list; qp = next) {
599 plp = (struct plist *)qp;
600 if (changed_plane_aborts(plp))
602 nunits = unitsatxy(target->sct_x, target->sct_y, 0, 0, 0);
604 pr("%s could not find any units!\n", prplane(&plp->plane));
607 (void)sprintf(prompt, "%s, %d bombs. Target ('~' to skip)? ",
608 prplane(&plp->plane), plp->load);
611 if (!(q = getstring(prompt, buf)))
618 unitsatxy(target->sct_x, target->sct_y, 0, 0, 0);
624 if (getland(n, &land) && land.lnd_own &&
625 land.lnd_ship < 0 && land.lnd_land < 0 &&
626 land.lnd_x == target->sct_x && land.lnd_y == target->sct_y)
629 pr("Unit #%d not spotted\n", n);
633 if (changed_plane_aborts(plp))
636 aaf = lnd_aaf(&land);
638 flak = roundavg(techfact(land.lnd_tech,
639 aaf * 3.0 * land.lnd_effic / 100.0));
641 "Flak! Firing flak guns from unit %s (aa rating %d)\n",
643 if (pinflak_planedamage(&plp->plane, plp->pcp, land.lnd_own, flak))
647 if (nuk_on_plane(&plp->plane) >= 0)
650 hitchance = pln_hitchance(&plp->plane,
651 lnd_hardtarget(&land), EF_LAND);
652 pr("%d%% hitchance...", hitchance);
654 if (pct_chance(hitchance)) {
655 dam = 2 * pln_damage(&plp->plane, 'p', "");
658 /* Bombs that miss have to land somewhere! */
659 dam = pln_damage(&plp->plane, 'p', 0);
660 collateral_damage(target->sct_x, target->sct_y, dam);
666 if (own != player->cnum)
667 mpr(own, "%s bombs did %d%% damage to %s at %s\n",
668 cname(player->cnum), dam, prland(&land),
669 xyas(target->sct_x, target->sct_y, own));
670 landdamage(&land, dam);
671 if (dam && (land.lnd_rflags & RET_INJURED))
672 retreat_land(&land, own, 'i');
673 else if (land.lnd_rflags & RET_BOMBED)
674 retreat_land(&land, own, 'b');
675 nreport(player->cnum, N_UNIT_BOMB, own, 1);
676 putland(land.lnd_uid, &land);
677 collateral_damage(target->sct_x, target->sct_y, dam);
682 strat_bomb(struct emp_qelem *list, struct sctstr *target)
686 struct emp_qelem *qp;
690 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
691 plp = (struct plist *)qp;
692 if (getnuke(nuk_on_plane(&plp->plane), &nuke))
693 detonate(&nuke, target->sct_x, target->sct_y,
694 plp->plane.pln_flags & PLN_AIRBURST);
696 dam += pln_damage(&plp->plane, 's', "");
700 getsect(target->sct_x, target->sct_y, §);
702 if (target->sct_own != player->cnum)
703 mpr(target->sct_own, "%s bombing raid did %d damage in %s\n",
704 cname(player->cnum), PERCENT_DAMAGE(dam),
705 xyas(target->sct_x, target->sct_y, target->sct_own));
707 sectdamage(target, dam);
709 pr("did %d damage in %s\n", PERCENT_DAMAGE(dam),
710 xyas(target->sct_x, target->sct_y, player->cnum));
715 changed_plane_aborts(struct plist *plp)
717 if (check_plane_ok(&plp->plane))
719 getplane(plp->plane.pln_uid, &plp->plane);
725 pinflak_planedamage(struct plnstr *pp, struct plchrstr *pcp, natid from,
732 dam = ac_flak_dam(flak, pln_def(pp), pcp->pl_flags);
735 disp = ac_damage_plane(pp, from, dam, 1, dmess);
736 mpr(pp->pln_own, " Flak! %s takes %d%s%s.\n",
737 prplane(pp), dam, *dmess ? " --" : "", dmess);
739 putplane(pp->pln_uid, pp);