2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2016, 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-2016
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 *,
70 char flightpath[MAX_PATH_LEN];
71 struct nstr_item ni_bomb;
72 struct nstr_item ni_esc;
74 struct emp_qelem bomb_list;
75 struct emp_qelem esc_list;
76 struct sctstr ap_sect;
79 struct emp_qelem *qp, *next;
82 if (get_planes(&ni_bomb, &ni_esc, player->argp[1], player->argp[2]) < 0)
84 p = getstarg(player->argp[3], "pinpoint, or strategic? ", buf);
88 if (!strchr("ps", mission))
90 if (!get_assembly_point(player->argp[4], &ap_sect, buf))
94 if (!getpath(flightpath, player->argp[5], ax, ay, 0, 0, MOB_FLY))
98 (void)pathtoxy(flightpath, &tx, &ty, fcost);
99 pr("target sector is %s\n", xyas(tx, ty, player->cnum));
100 getsect(tx, ty, &target);
101 ap_to_target = strlen(flightpath);
102 pr("range to target is %d\n", ap_to_target);
104 * select planes within range
106 pln_sel(&ni_bomb, &bomb_list, &ap_sect, ap_to_target, 2,
107 P_B | P_T, P_M | P_O);
108 pln_sel(&ni_esc, &esc_list, &ap_sect, ap_to_target, 2,
109 P_ESC | P_F, P_M | P_O);
111 * now arm and equip the bombers, transports, whatever.
113 pln_arm(&bomb_list, 2 * ap_to_target, mission, NULL);
114 if (QEMPTY(&bomb_list)) {
115 pr("No planes could be equipped for the mission.\n");
118 pln_arm(&esc_list, 2 * ap_to_target, 'e', NULL);
119 ac_encounter(&bomb_list, &esc_list, ax, ay, flightpath, 0);
120 if (QEMPTY(&bomb_list)) {
121 pr("No planes got through fighter defenses\n");
122 } else if (target.sct_type == SCT_SANCT) {
123 pr("You can't bomb that sector!\n");
127 pin_bomb(&bomb_list, &target);
128 for (qp = bomb_list.q_forw; qp != &bomb_list; qp = next) {
130 plp = (struct plist *)qp;
131 changed_plane_aborts(plp);
133 for (qp = esc_list.q_forw; qp != &esc_list; qp = next) {
135 plp = (struct plist *)qp;
136 changed_plane_aborts(plp);
140 nreport(player->cnum, N_SCT_BOMB, target.sct_own, 1);
141 strat_bomb(&bomb_list, &target);
153 pin_bomb(struct emp_qelem *list, struct sctstr *target)
167 type = target->sct_type;
169 pr("Target sector is a %s constructed %s\n",
170 effadv((int)target->sct_effic), dcp->d_name);
172 nships = shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
173 if (pln_caps(list) & P_A) {
174 nsubs = shipsatxy(target->sct_x, target->sct_y, M_SUB, 0, 1);
176 pr("Some subs are present in the sector.\n");
178 nplanes = planesatxy(target->sct_x, target->sct_y, 0, 0);
179 nunits = unitsatxy(target->sct_x, target->sct_y, 0, L_SPY);
181 p = getstring("Bomb what? (ship, plane, land unit, efficiency, commodities) ",
194 pr("no units there\n");
197 land_bomb(list, target);
201 pr("no planes there\n");
204 plane_bomb(list, target);
208 if (pln_caps(list) & P_A) {
210 pr("no ships there\n");
214 pr("no ships there\n");
218 ship_bomb(list, target);
221 for (i = I_NONE + 1; i <= I_MAX; i++) {
222 if (target->sct_item[i])
226 pr("No bombable commodities in %s\n",
227 xyas(target->sct_x, target->sct_y, player->cnum));
230 comm_bomb(list, target);
233 eff_bomb(list, target);
236 pr("Aborting mission.\n");
239 pr("Bad target type.\n");
245 eff_bomb(struct emp_qelem *list, struct sctstr *target)
248 struct emp_qelem *qp, *next;
252 for (qp = list->q_forw; qp != list; qp = next) {
254 plp = (struct plist *)qp;
255 if (changed_plane_aborts(plp))
257 dam += pln_damage(&plp->plane, 'p', "");
259 getsect(target->sct_x, target->sct_y, §);
261 oldeff = target->sct_effic;
262 target->sct_effic = effdamage(target->sct_effic, dam);
263 target->sct_avail = effdamage(target->sct_avail, dam);
264 target->sct_road = effdamage(target->sct_road, dam);
265 target->sct_rail = effdamage(target->sct_rail, dam);
266 target->sct_defense = effdamage(target->sct_defense, dam);
267 pr("did %d%% damage to efficiency in %s\n",
268 oldeff - target->sct_effic,
269 xyas(target->sct_x, target->sct_y, player->cnum));
270 if (target->sct_own != player->cnum)
272 "%s bombing raid did %d%% damage in %s\n",
273 cname(player->cnum), oldeff - target->sct_effic,
274 xyas(target->sct_x, target->sct_y, target->sct_own));
275 bridge_damaged(target);
277 collateral_damage(target->sct_x, target->sct_y, dam);
281 comm_bomb(struct emp_qelem *list, struct sctstr *target)
288 struct emp_qelem *qp, *next;
292 for (i = I_NONE + 1; i <= I_MAX; i++) {
293 if (!target->sct_item[i])
295 if (opt_SUPER_BARS && i == I_BAR)
297 pr("some %s\n", ichr[i].i_name);
300 ip = whatitem(NULL, "commodity to bomb? ");
305 if (opt_SUPER_BARS && ip->i_uid == I_BAR) {
306 pr("You can't bomb %s!\n", ip->i_name);
311 for (qp = list->q_forw; qp != list; qp = next) {
313 plp = (struct plist *)qp;
314 if (changed_plane_aborts(plp))
316 dam += pln_damage(&plp->plane, 'p', "");
318 getsect(target->sct_x, target->sct_y, §);
320 before = target->sct_item[ip->i_uid];
321 target->sct_item[ip->i_uid] = amt = commdamage(before, dam, ip->i_uid);
323 b = 100.0 * (1.0 - (double)amt / (double)before);
326 pr("did %.2f%% damage to %s in %s\n",
327 b, ip->i_name, xyas(target->sct_x, target->sct_y, player->cnum));
328 nreport(player->cnum, N_SCT_BOMB, target->sct_own, 1);
329 if (target->sct_own != player->cnum)
331 "%s precision bombing raid did %.2f%% damage to %s in %s\n",
332 cname(player->cnum), b, ip->i_name,
333 xyas(target->sct_x, target->sct_y, target->sct_own));
335 collateral_damage(target->sct_x, target->sct_y, dam);
339 ship_bomb(struct emp_qelem *list, struct sctstr *target)
346 struct emp_qelem *qp, *next;
350 struct shiplist *head = NULL;
357 for (qp = list->q_forw; qp != list; qp = next) {
359 free_shiplist(&head);
360 plp = (struct plist *)qp;
361 if (changed_plane_aborts(plp))
363 if (plp->pcp->pl_flags & P_A)
364 nships = asw_shipsatxy(target->sct_x, target->sct_y, 0, 0,
367 nships = shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
369 pr("%s could not find any ships!\n", prplane(&plp->plane));
372 (void)sprintf(prompt, "%s, %d bombs. Target ('~' to skip)? ",
373 prplane(&plp->plane), plp->load);
376 if (!(q = getstring(prompt, buf)))
383 if (plp->pcp->pl_flags & P_A)
384 print_shiplist(head);
386 shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
392 if ((!(plp->pcp->pl_flags & P_A) || on_shiplist(n, head)) &&
393 getship(n, &ship) && ship.shp_own &&
394 ship.shp_x == target->sct_x && ship.shp_y == target->sct_y)
397 pr("Ship #%d not spotted\n", n);
401 if ((plp->pcp->pl_flags & P_A) && !on_shiplist(shipno, head))
403 if (changed_plane_aborts(plp))
406 gun = shp_usable_guns(&ship);
407 mcp = &mchr[(int)ship.shp_type];
408 if (gun > 0 && !(mcp->m_flags & M_SUB)) {
409 flak = (int)(techfact(ship.shp_tech, gun) * 2.0);
410 mpr(ship.shp_own, "Flak! Firing %d guns from ship %s\n",
411 flak, prship(&ship));
412 if (pinflak_planedamage(&plp->plane, plp->pcp, ship.shp_own, flak))
416 if (nuk_on_plane(&plp->plane) >= 0)
419 hitchance = pln_hitchance(&plp->plane,
420 shp_hardtarget(&ship), EF_SHIP);
421 pr("%d%% hitchance...", hitchance);
423 if (pct_chance(hitchance)) {
424 /* pinbombing is more accurate than normal bombing */
425 dam = 2 * pln_damage(&plp->plane, 'p', "");
428 /* Bombs that miss have to land somewhere! */
429 dam = pln_damage(&plp->plane, 'p', 0);
430 collateral_damage(target->sct_x, target->sct_y, dam);
433 if (mcp->m_flags & M_SUB)
434 nreport(player->cnum, N_SUB_BOMB, ship.shp_own, 1);
436 nreport(player->cnum, N_SHP_BOMB, ship.shp_own, 1);
437 if (ship.shp_own != player->cnum) {
438 mpr(ship.shp_own, "%s bombs did %d damage to %s at %s\n",
439 cname(player->cnum), dam,
441 xyas(target->sct_x, target->sct_y, ship.shp_own));
443 shipdamage(&ship, dam);
444 if (ship.shp_effic < SHIP_MINEFF)
445 pr("%s at %s sunk!\n",
447 xyas(target->sct_x, target->sct_y, player->cnum));
448 if (dam && (ship.shp_rflags & RET_INJURED))
449 retreat_ship(&ship, ship.shp_own, 'i');
450 else if (ship.shp_rflags & RET_BOMBED)
451 retreat_ship(&ship, ship.shp_own, 'b');
452 putship(ship.shp_uid, &ship);
453 collateral_damage(target->sct_x, target->sct_y, dam / 2);
456 free_shiplist(&head);
460 plane_bomb(struct emp_qelem *list, struct sctstr *target)
467 struct emp_qelem *qp, *next;
475 for (qp = list->q_forw; qp != list; qp = next) {
477 plp = (struct plist *)qp;
478 if (changed_plane_aborts(plp))
480 nplanes = planesatxy(target->sct_x, target->sct_y, 0, 0);
482 pr("%s could not find any planes!\n", prplane(&plp->plane));
485 (void)sprintf(prompt, "%s, %d bombs. Target ('~' to skip)? ",
486 prplane(&plp->plane), plp->load);
488 while (planeno < 0) {
489 if (!(q = getstring(prompt, buf)))
496 planesatxy(target->sct_x, target->sct_y, 0, 0);
502 if (getplane(n, &plane) && plane.pln_own &&
503 plane.pln_x == target->sct_x &&
504 plane.pln_y == target->sct_y &&
505 plane.pln_ship < 0 && plane.pln_land < 0 &&
506 !(plane.pln_flags & PLN_LAUNCHED))
509 pr("Plane #%d not spotted\n", n);
513 if (changed_plane_aborts(plp))
515 if (nuk_on_plane(&plp->plane) >= 0)
518 hitchance = pln_hitchance(&plp->plane, 0, EF_PLANE);
519 pr("%d%% hitchance...", hitchance);
521 if (pct_chance(hitchance)) {
522 /* pinbombing is more accurate than normal bombing */
523 dam = 2 * pln_damage(&plp->plane, 'p', "");
526 /* Bombs that miss have to land somewhere! */
527 dam = pln_damage(&plp->plane, 'p', 0);
528 collateral_damage(target->sct_x, target->sct_y, dam);
534 if (dam > plane.pln_effic)
537 plane.pln_effic -= dam;
538 plane.pln_mobil = damage(plane.pln_mobil, dam);
539 mpr(own, "%s bombs did %d%% damage to %s at %s\n",
540 cname(player->cnum), dam, prplane(&plane),
541 xyas(target->sct_x, target->sct_y, own));
542 nreport(player->cnum, N_DOWN_PLANE, own, 1);
543 putplane(plane.pln_uid, &plane);
544 collateral_damage(target->sct_x, target->sct_y, dam);
549 land_bomb(struct emp_qelem *list, struct sctstr *target)
558 struct emp_qelem *qp, *next;
560 int aaf, flak, hitchance;
564 for (qp = list->q_forw; qp != list; qp = next) {
566 plp = (struct plist *)qp;
567 if (changed_plane_aborts(plp))
569 nunits = unitsatxy(target->sct_x, target->sct_y, 0, L_SPY);
571 pr("%s could not find any units!\n", prplane(&plp->plane));
574 (void)sprintf(prompt, "%s, %d bombs. Target ('~' to skip)? ",
575 prplane(&plp->plane), plp->load);
578 if (!(q = getstring(prompt, buf)))
585 unitsatxy(target->sct_x, target->sct_y, 0, L_SPY);
591 if (getland(n, &land) && land.lnd_own &&
592 land.lnd_ship < 0 && land.lnd_land < 0 &&
593 !(lchr[land.lnd_type].l_flags & L_SPY) &&
594 land.lnd_x == target->sct_x && land.lnd_y == target->sct_y)
597 pr("Unit #%d not spotted\n", n);
601 if (changed_plane_aborts(plp))
604 aaf = lnd_aaf(&land);
606 flak = roundavg(techfact(land.lnd_tech,
607 aaf * 3.0 * land.lnd_effic / 100.0));
609 "Flak! Firing flak guns from unit %s (aa rating %d)\n",
611 if (pinflak_planedamage(&plp->plane, plp->pcp, land.lnd_own, flak))
615 if (nuk_on_plane(&plp->plane) >= 0)
618 hitchance = pln_hitchance(&plp->plane,
619 lnd_hardtarget(&land), EF_LAND);
620 pr("%d%% hitchance...", hitchance);
622 if (pct_chance(hitchance)) {
623 dam = 2 * pln_damage(&plp->plane, 'p', "");
626 /* Bombs that miss have to land somewhere! */
627 dam = pln_damage(&plp->plane, 'p', 0);
628 collateral_damage(target->sct_x, target->sct_y, dam);
634 if (own != player->cnum)
635 mpr(own, "%s bombs did %d%% damage to %s at %s\n",
636 cname(player->cnum), dam, prland(&land),
637 xyas(target->sct_x, target->sct_y, own));
638 landdamage(&land, dam);
639 if (dam && (land.lnd_rflags & RET_INJURED))
640 retreat_land(&land, own, 'i');
641 else if (land.lnd_rflags & RET_BOMBED)
642 retreat_land(&land, own, 'b');
643 nreport(player->cnum, N_UNIT_BOMB, own, 1);
644 putland(land.lnd_uid, &land);
645 collateral_damage(target->sct_x, target->sct_y, dam);
650 strat_bomb(struct emp_qelem *list, struct sctstr *target)
654 struct emp_qelem *qp;
658 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
659 plp = (struct plist *)qp;
660 if (getnuke(nuk_on_plane(&plp->plane), &nuke))
661 detonate(&nuke, target->sct_x, target->sct_y,
662 plp->plane.pln_flags & PLN_AIRBURST);
664 dam += pln_damage(&plp->plane, 's', "");
668 getsect(target->sct_x, target->sct_y, §);
670 if (target->sct_own != player->cnum)
671 mpr(target->sct_own, "%s bombing raid did %d damage in %s\n",
672 cname(player->cnum), PERCENT_DAMAGE(dam),
673 xyas(target->sct_x, target->sct_y, target->sct_own));
675 sectdamage(target, dam);
677 pr("did %d damage in %s\n", PERCENT_DAMAGE(dam),
678 xyas(target->sct_x, target->sct_y, player->cnum));
683 changed_plane_aborts(struct plist *plp)
685 if (check_plane_ok(&plp->plane))
687 getplane(plp->plane.pln_uid, &plp->plane);
693 pinflak_planedamage(struct plnstr *pp, struct plchrstr *pcp, natid from,
700 dam = ac_flak_dam(flak, pln_def(pp), pcp->pl_flags);
703 disp = ac_damage_plane(pp, from, dam, 1, dmess);
704 mpr(pp->pln_own, " Flak! %s takes %d%s%s.\n",
705 prplane(pp), dam, *dmess ? " --" : "", dmess);
707 putplane(pp->pln_uid, pp);