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 if (flightpath[ap_to_target - 1] == 'h')
122 pr("range to target is %d\n", ap_to_target);
124 * select planes within range
126 pln_sel(&ni_bomb, &bomb_list, &ap_sect, ap_to_target, 2,
127 P_B | P_T, P_M | P_O);
128 pln_sel(&ni_esc, &esc_list, &ap_sect, ap_to_target, 2,
129 P_ESC | P_F, P_M | P_O);
131 * now arm and equip the bombers, transports, whatever.
133 pln_arm(&bomb_list, 2 * ap_to_target, mission, NULL);
134 if (QEMPTY(&bomb_list)) {
135 pr("No planes could be equipped for the mission.\n");
138 pln_arm(&esc_list, 2 * ap_to_target, 'e', NULL);
139 ac_encounter(&bomb_list, &esc_list, ax, ay, flightpath, 0);
140 if (QEMPTY(&bomb_list)) {
141 pr("No planes got through fighter defenses\n");
142 } else if (target.sct_type == SCT_SANCT) {
143 pr("You can't bomb that sector!\n");
147 pin_bomb(&bomb_list, &target);
148 for (qp = bomb_list.q_forw; qp != &bomb_list; qp = next) {
150 plp = (struct plist *)qp;
151 changed_plane_aborts(plp);
153 for (qp = esc_list.q_forw; qp != &esc_list; qp = next) {
155 plp = (struct plist *)qp;
156 changed_plane_aborts(plp);
160 nreport(player->cnum, N_SCT_BOMB, target.sct_own, 1);
161 strat_bomb(&bomb_list, &target);
173 pin_bomb(struct emp_qelem *list, struct sctstr *target)
187 type = target->sct_type;
189 pr("Target sector is a %s constructed %s\n",
190 effadv((int)target->sct_effic), dcp->d_name);
192 nships = shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
193 if (pln_caps(list) & P_A) {
194 nsubs = shipsatxy(target->sct_x, target->sct_y, M_SUB, 0, 1);
196 pr("Some subs are present in the sector.\n");
198 nplanes = planesatxy(target->sct_x, target->sct_y, 0, 0);
199 nunits = unitsatxy(target->sct_x, target->sct_y, 0, 0, 0);
201 p = getstring("Bomb what? (ship, plane, land unit, efficiency, commodities) ",
214 pr("no units there\n");
217 land_bomb(list, target);
221 pr("no planes there\n");
224 plane_bomb(list, target);
228 if (pln_caps(list) & P_A) {
230 pr("no ships there\n");
234 pr("no ships there\n");
238 ship_bomb(list, target);
241 for (i = 0; i < nbomb; i++) {
242 if (!target->sct_item[bombcomm[i]])
247 pr("No bombable commodities in %s\n",
248 xyas(target->sct_x, target->sct_y, player->cnum));
251 comm_bomb(list, target);
254 eff_bomb(list, target);
257 pr("Aborting mission.\n");
260 pr("Bad target type.\n");
266 eff_bomb(struct emp_qelem *list, struct sctstr *target)
269 struct emp_qelem *qp, *next;
273 for (qp = list->q_forw; qp != list; qp = next) {
275 plp = (struct plist *)qp;
276 if (changed_plane_aborts(plp))
278 dam += pln_damage(&plp->plane, 'p', "");
280 getsect(target->sct_x, target->sct_y, §);
282 oldeff = target->sct_effic;
283 target->sct_effic = effdamage(target->sct_effic, dam);
284 target->sct_avail = effdamage(target->sct_avail, dam);
285 target->sct_road = effdamage(target->sct_road, dam);
286 target->sct_rail = effdamage(target->sct_rail, dam);
287 target->sct_defense = effdamage(target->sct_defense, dam);
288 pr("did %d%% damage to efficiency in %s\n",
289 oldeff - target->sct_effic,
290 xyas(target->sct_x, target->sct_y, player->cnum));
292 wu(0, target->sct_own,
293 "%s bombing raid did %d%% damage in %s\n",
294 cname(player->cnum), oldeff - target->sct_effic,
295 xyas(target->sct_x, target->sct_y, target->sct_own));
296 bridge_damaged(target);
298 collateral_damage(target->sct_x, target->sct_y, dam);
302 comm_bomb(struct emp_qelem *list, struct sctstr *target)
309 struct emp_qelem *qp, *next;
313 for (i = 0; i < nbomb; i++) {
314 if (target->sct_item[bombcomm[i]] == 0)
316 if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
318 ip = &ichr[bombcomm[i]];
319 pr("some %s\n", ip->i_name);
322 ip = whatitem(NULL, "commodity to bomb? ");
328 for (i = 0; i < nbomb; i++) {
329 if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
331 if (&ichr[bombcomm[i]] == ip)
335 pr("You can't bomb %s!\n", ip->i_name);
336 for (i = 0; i < nbomb; i++) {
337 if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
339 pr("%s%s", i == 0 ? "Bombable: " : ", ",
340 ichr[bombcomm[i]].i_name);
346 for (qp = list->q_forw; qp != list; qp = next) {
348 plp = (struct plist *)qp;
349 if (changed_plane_aborts(plp))
351 dam += pln_damage(&plp->plane, 'p', "");
353 getsect(target->sct_x, target->sct_y, §);
355 before = target->sct_item[ip->i_uid];
356 target->sct_item[ip->i_uid] = amt = commdamage(before, dam, ip->i_uid);
358 b = 100.0 * (1.0 - (double)amt / (double)before);
361 pr("did %.2f%% damage to %s in %s\n",
362 b, ip->i_name, xyas(target->sct_x, target->sct_y, player->cnum));
363 nreport(player->cnum, N_SCT_BOMB, target->sct_own, 1);
364 if (target->sct_own != 0)
365 wu(0, target->sct_own,
366 "%s precision bombing raid did %.2f%% damage to %s in %s\n",
367 cname(player->cnum), b, ip->i_name,
368 xyas(target->sct_x, target->sct_y, target->sct_own));
370 collateral_damage(target->sct_x, target->sct_y, dam);
374 ship_bomb(struct emp_qelem *list, struct sctstr *target)
381 struct emp_qelem *qp, *next;
385 struct shiplist *head = NULL;
392 for (qp = list->q_forw; qp != list; qp = next) {
394 free_shiplist(&head);
395 plp = (struct plist *)qp;
396 if (changed_plane_aborts(plp))
398 if (plp->pcp->pl_flags & P_A)
399 nships = asw_shipsatxy(target->sct_x, target->sct_y, 0, 0,
402 nships = shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
404 pr("%s could not find any ships!\n", prplane(&plp->plane));
407 (void)sprintf(prompt, "%s, %d bombs. Target ('~' to skip)? ",
408 prplane(&plp->plane), plp->load);
411 if (!(q = getstring(prompt, buf)))
418 if (plp->pcp->pl_flags & P_A)
419 print_shiplist(head);
421 shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
427 if ((!(plp->pcp->pl_flags & P_A) || on_shiplist(n, head)) &&
428 getship(n, &ship) && ship.shp_own &&
429 ship.shp_x == target->sct_x && ship.shp_y == target->sct_y)
432 pr("Ship #%d not spotted\n", n);
436 if ((plp->pcp->pl_flags & P_A) && !on_shiplist(shipno, head))
438 if (changed_plane_aborts(plp))
441 gun = shp_usable_guns(&ship);
442 mcp = &mchr[(int)ship.shp_type];
443 if (gun > 0 && !(mcp->m_flags & M_SUB)) {
444 flak = (int)(techfact(ship.shp_tech, gun) * 2.0);
445 mpr(ship.shp_own, "Flak! Firing %d guns from ship %s\n",
446 flak, prship(&ship));
447 if (pinflak_planedamage(&plp->plane, plp->pcp, ship.shp_own, flak))
451 if (nuk_on_plane(&plp->plane) >= 0)
454 hitchance = pln_hitchance(&plp->plane,
455 shp_hardtarget(&ship), EF_SHIP);
456 pr("%d%% hitchance...", hitchance);
458 if (pct_chance(hitchance)) {
459 /* pinbombing is more accurate than normal bombing */
460 dam = 2 * pln_damage(&plp->plane, 'p', "");
463 /* Bombs that miss have to land somewhere! */
464 dam = pln_damage(&plp->plane, 'p', 0);
465 collateral_damage(target->sct_x, target->sct_y, dam);
468 if (mcp->m_flags & M_SUB)
469 nreport(player->cnum, N_SUB_BOMB, ship.shp_own, 1);
471 nreport(player->cnum, N_SHP_BOMB, ship.shp_own, 1);
473 wu(0, ship.shp_own, "%s bombs did %d damage to %s at %s\n",
474 cname(player->cnum), dam,
476 xyas(target->sct_x, target->sct_y, ship.shp_own));
479 shipdamage(&ship, dam);
480 if (ship.shp_effic < SHIP_MINEFF)
481 pr("%s at %s sunk!\n",
483 xyas(target->sct_x, target->sct_y, player->cnum));
484 if (dam && (ship.shp_rflags & RET_INJURED))
485 retreat_ship(&ship, 'i');
486 else if (ship.shp_rflags & RET_BOMBED)
487 retreat_ship(&ship, 'b');
488 putship(ship.shp_uid, &ship);
489 collateral_damage(target->sct_x, target->sct_y, dam / 2);
492 free_shiplist(&head);
496 plane_bomb(struct emp_qelem *list, struct sctstr *target)
503 struct emp_qelem *qp, *next;
511 for (qp = list->q_forw; qp != list; qp = next) {
513 plp = (struct plist *)qp;
514 if (changed_plane_aborts(plp))
516 nplanes = planesatxy(target->sct_x, target->sct_y, 0, 0);
518 pr("%s could not find any planes!\n", prplane(&plp->plane));
521 (void)sprintf(prompt, "%s, %d bombs. Target ('~' to skip)? ",
522 prplane(&plp->plane), plp->load);
524 while (planeno < 0) {
525 if (!(q = getstring(prompt, buf)))
532 planesatxy(target->sct_x, target->sct_y, 0, 0);
538 if (getplane(n, &plane) && plane.pln_own &&
539 plane.pln_x == target->sct_x &&
540 plane.pln_y == target->sct_y &&
541 plane.pln_ship < 0 && plane.pln_land < 0 &&
542 !(plane.pln_flags & PLN_LAUNCHED))
545 pr("Plane #%d not spotted\n", n);
549 if (changed_plane_aborts(plp))
551 if (nuk_on_plane(&plp->plane) >= 0)
554 hitchance = pln_hitchance(&plp->plane, 0, EF_PLANE);
555 pr("%d%% hitchance...", hitchance);
557 if (pct_chance(hitchance)) {
558 /* pinbombing is more accurate than normal bombing */
559 dam = 2 * pln_damage(&plp->plane, 'p', "");
562 /* Bombs that miss have to land somewhere! */
563 dam = pln_damage(&plp->plane, 'p', 0);
564 collateral_damage(target->sct_x, target->sct_y, dam);
570 if (dam > plane.pln_effic)
573 plane.pln_effic -= dam;
574 plane.pln_mobil = (dam * plane.pln_mobil / 100.0);
575 if (own == player->cnum) {
576 pr("%s reports %d%% damage\n", prplane(&plane), dam);
580 "%s pinpoint bombing raid did %d%% damage to %s\n",
581 cname(player->cnum), dam, prplane(&plane));
583 nreport(player->cnum, N_DOWN_PLANE, own, 1);
585 wu(0, own, "%s bombs did %d%% damage to %s at %s\n",
586 cname(player->cnum), dam, prplane(&plane),
587 xyas(target->sct_x, target->sct_y, own));
588 putplane(plane.pln_uid, &plane);
589 collateral_damage(target->sct_x, target->sct_y, dam);
594 land_bomb(struct emp_qelem *list, struct sctstr *target)
603 struct emp_qelem *qp, *next;
605 int aaf, flak, hitchance;
609 for (qp = list->q_forw; qp != list; qp = next) {
611 plp = (struct plist *)qp;
612 if (changed_plane_aborts(plp))
614 nunits = unitsatxy(target->sct_x, target->sct_y, 0, 0, 0);
616 pr("%s could not find any units!\n", prplane(&plp->plane));
619 (void)sprintf(prompt, "%s, %d bombs. Target ('~' to skip)? ",
620 prplane(&plp->plane), plp->load);
623 if (!(q = getstring(prompt, buf)))
630 unitsatxy(target->sct_x, target->sct_y, 0, 0, 0);
636 if (getland(n, &land) && land.lnd_own &&
637 land.lnd_ship < 0 && land.lnd_land < 0 &&
638 land.lnd_x == target->sct_x && land.lnd_y == target->sct_y)
641 pr("Unit #%d not spotted\n", n);
645 if (changed_plane_aborts(plp))
648 aaf = lnd_aaf(&land);
650 flak = roundavg(techfact(land.lnd_tech,
651 aaf * 3.0 * land.lnd_effic / 100.0));
653 "Flak! Firing flak guns from unit %s (aa rating %d)\n",
655 if (pinflak_planedamage(&plp->plane, plp->pcp, land.lnd_own, flak))
659 if (nuk_on_plane(&plp->plane) >= 0)
662 hitchance = pln_hitchance(&plp->plane,
663 lnd_hardtarget(&land), EF_LAND);
664 pr("%d%% hitchance...", hitchance);
666 if (pct_chance(hitchance)) {
667 dam = 2 * pln_damage(&plp->plane, 'p', "");
670 /* Bombs that miss have to land somewhere! */
671 dam = pln_damage(&plp->plane, 'p', 0);
672 collateral_damage(target->sct_x, target->sct_y, dam);
678 mpr(own, "%s pinpoint bombing raid did %d damage to %s\n",
679 cname(player->cnum), dam, prland(&land));
680 landdamage(&land, dam);
681 if (dam && (land.lnd_rflags & RET_INJURED))
682 retreat_land(&land, 'i');
683 else if (land.lnd_rflags & RET_BOMBED)
684 retreat_land(&land, 'b');
685 nreport(player->cnum, N_UNIT_BOMB, own, 1);
686 putland(land.lnd_uid, &land);
687 collateral_damage(target->sct_x, target->sct_y, dam);
692 strat_bomb(struct emp_qelem *list, struct sctstr *target)
696 struct emp_qelem *qp;
700 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
701 plp = (struct plist *)qp;
702 if (getnuke(nuk_on_plane(&plp->plane), &nuke))
703 detonate(&nuke, target->sct_x, target->sct_y,
704 plp->plane.pln_flags & PLN_AIRBURST);
706 dam += pln_damage(&plp->plane, 's', "");
710 getsect(target->sct_x, target->sct_y, §);
713 wu(0, target->sct_own, "%s bombing raid did %d damage in %s\n",
714 cname(player->cnum), PERCENT_DAMAGE(dam),
715 xyas(target->sct_x, target->sct_y, target->sct_own));
717 sectdamage(target, dam);
719 pr("did %d damage in %s\n", PERCENT_DAMAGE(dam),
720 xyas(target->sct_x, target->sct_y, player->cnum));
725 changed_plane_aborts(struct plist *plp)
727 if (check_plane_ok(&plp->plane))
729 getplane(plp->plane.pln_uid, &plp->plane);
735 pinflak_planedamage(struct plnstr *pp, struct plchrstr *pcp, natid from,
742 dam = ac_flak_dam(flak, pln_def(pp), pcp->pl_flags);
745 disp = ac_damage_plane(pp, from, dam, 1, dmess);
746 mpr(pp->pln_own, " Flak! %s takes %d%s%s.\n",
747 prplane(pp), dam, *dmess ? " --" : "", dmess);
749 putplane(pp->pln_uid, pp);