2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2010, 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);
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, P_FLYING)
117 (void)pathtoxy(flightpath, &tx, &ty, fcost);
118 pr("target sector is %s\n", xyas(tx, ty, player->cnum));
119 getsect(tx, ty, &target);
120 ap_to_target = strlen(flightpath);
121 if (flightpath[ap_to_target - 1] == 'h')
123 pr("range to target is %d\n", ap_to_target);
125 * select planes within range
127 pln_sel(&ni_bomb, &bomb_list, &ap_sect, ap_to_target, 2,
128 P_B | P_T, P_M | P_O);
129 pln_sel(&ni_esc, &esc_list, &ap_sect, ap_to_target, 2,
130 P_ESC | P_F, P_M | P_O);
132 * now arm and equip the bombers, transports, whatever.
134 pln_arm(&bomb_list, 2 * ap_to_target, mission, NULL);
135 if (QEMPTY(&bomb_list)) {
136 pr("No planes could be equipped for the mission.\n");
139 pln_arm(&esc_list, 2 * ap_to_target, 'e', NULL);
140 ac_encounter(&bomb_list, &esc_list, ax, ay, flightpath, 0);
141 if (QEMPTY(&bomb_list)) {
142 pr("No planes got through fighter defenses\n");
143 } else if (target.sct_type == SCT_SANCT) {
144 pr("You can't bomb that sector!\n");
148 pin_bomb(&bomb_list, &target);
149 for (qp = bomb_list.q_forw; qp != &bomb_list; qp = next) {
151 plp = (struct plist *)qp;
152 changed_plane_aborts(plp);
154 for (qp = esc_list.q_forw; qp != &esc_list; qp = next) {
156 plp = (struct plist *)qp;
157 changed_plane_aborts(plp);
161 nreport(player->cnum, N_SCT_BOMB, target.sct_own, 1);
162 strat_bomb(&bomb_list, &target);
174 pin_bomb(struct emp_qelem *list, struct sctstr *target)
188 type = target->sct_type;
190 pr("Target sector is a %s constructed %s\n",
191 effadv((int)target->sct_effic), dcp->d_name);
193 nships = shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
194 if (pln_caps(list) & P_A) {
195 nsubs = shipsatxy(target->sct_x, target->sct_y, M_SUB, 0, 1);
197 pr("Some subs are present in the sector.\n");
199 nplanes = planesatxy(target->sct_x, target->sct_y, 0, 0);
200 nunits = unitsatxy(target->sct_x, target->sct_y, 0, 0);
202 p = getstring("Bomb what? (ship, plane, land unit, efficiency, commodities) ",
215 pr("no units there\n");
218 land_bomb(list, target);
222 pr("no planes there\n");
225 plane_bomb(list, target);
229 if (pln_caps(list) & P_A) {
231 pr("no ships there\n");
235 pr("no ships there\n");
239 ship_bomb(list, target);
242 for (i = 0; i < nbomb; i++) {
243 if (!target->sct_item[bombcomm[i]])
248 pr("No bombable commodities in %s\n",
249 xyas(target->sct_x, target->sct_y, player->cnum));
252 comm_bomb(list, target);
255 eff_bomb(list, target);
258 pr("Aborting mission.\n");
261 pr("Bad target type.\n");
267 eff_bomb(struct emp_qelem *list, struct sctstr *target)
270 struct emp_qelem *qp, *next;
274 for (qp = list->q_forw; qp != list; qp = next) {
276 plp = (struct plist *)qp;
277 if (changed_plane_aborts(plp))
279 dam += pln_damage(&plp->plane, 'p', 1);
281 getsect(target->sct_x, target->sct_y, §);
283 oldeff = target->sct_effic;
284 target->sct_effic = effdamage(target->sct_effic, dam);
285 target->sct_avail = effdamage(target->sct_avail, dam);
286 target->sct_road = effdamage(target->sct_road, dam);
287 target->sct_rail = effdamage(target->sct_rail, dam);
288 target->sct_defense = effdamage(target->sct_defense, dam);
289 pr("did %d%% damage to efficiency in %s\n",
290 oldeff - target->sct_effic,
291 xyas(target->sct_x, target->sct_y, player->cnum));
293 wu(0, target->sct_own,
294 "%s bombing raid did %d%% damage in %s\n",
295 cname(player->cnum), oldeff - target->sct_effic,
296 xyas(target->sct_x, target->sct_y, target->sct_own));
297 bridge_damaged(target);
299 collateral_damage(target->sct_x, target->sct_y, dam);
303 comm_bomb(struct emp_qelem *list, struct sctstr *target)
310 struct emp_qelem *qp, *next;
314 for (i = 0; i < nbomb; i++) {
315 if (target->sct_item[bombcomm[i]] == 0)
317 if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
319 ip = &ichr[bombcomm[i]];
320 pr("some %s\n", ip->i_name);
323 ip = whatitem(NULL, "commodity to bomb? ");
329 for (i = 0; i < nbomb; i++) {
330 if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
332 if (&ichr[bombcomm[i]] == ip)
336 pr("You can't bomb %s!\n", ip->i_name);
337 for (i = 0; i < nbomb; i++) {
338 if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
340 pr("%s%s", i == 0 ? "Bombable: " : ", ",
341 ichr[bombcomm[i]].i_name);
347 for (qp = list->q_forw; qp != list; qp = next) {
349 plp = (struct plist *)qp;
350 if (changed_plane_aborts(plp))
352 dam += pln_damage(&plp->plane, 'p', 1);
354 getsect(target->sct_x, target->sct_y, §);
356 before = target->sct_item[ip->i_uid];
357 target->sct_item[ip->i_uid] = amt = commdamage(before, dam, ip->i_uid);
359 b = 100.0 * (1.0 - (double)amt / (double)before);
362 pr("did %.2f%% damage to %s in %s\n",
363 b, ip->i_name, xyas(target->sct_x, target->sct_y, player->cnum));
364 nreport(player->cnum, N_SCT_BOMB, target->sct_own, 1);
365 if (target->sct_own != 0)
366 wu(0, target->sct_own,
367 "%s precision bombing raid did %.2f%% damage to %s in %s\n",
368 cname(player->cnum), b, ip->i_name,
369 xyas(target->sct_x, target->sct_y, target->sct_own));
371 collateral_damage(target->sct_x, target->sct_y, dam);
375 ship_bomb(struct emp_qelem *list, struct sctstr *target)
382 struct emp_qelem *qp, *next;
386 struct shiplist *head = NULL;
393 for (qp = list->q_forw; qp != list; qp = next) {
395 free_shiplist(&head);
396 plp = (struct plist *)qp;
397 if (changed_plane_aborts(plp))
399 if (plp->pcp->pl_flags & P_A)
400 nships = asw_shipsatxy(target->sct_x, target->sct_y, 0, 0,
403 nships = shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
405 pr("%s could not find any ships!\n", prplane(&plp->plane));
408 (void)sprintf(prompt, "%s, %d bombs. Target ('~' to skip)? ",
409 prplane(&plp->plane), plp->load);
412 if (!(q = getstring(prompt, buf)))
419 if (plp->pcp->pl_flags & P_A)
420 print_shiplist(head);
422 shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
428 if ((!(plp->pcp->pl_flags & P_A) || on_shiplist(n, head)) &&
429 getship(n, &ship) && ship.shp_own &&
430 ship.shp_x == target->sct_x && ship.shp_y == target->sct_y)
433 pr("Ship #%d not spotted\n", n);
437 if ((plp->pcp->pl_flags & P_A) && !on_shiplist(shipno, head))
439 if (changed_plane_aborts(plp))
442 gun = shp_usable_guns(&ship);
443 mcp = &mchr[(int)ship.shp_type];
444 if (gun > 0 && !(mcp->m_flags & M_SUB)) {
445 flak = (int)(techfact(ship.shp_tech, gun) * 2.0);
446 mpr(ship.shp_own, "Flak! Firing %d guns from ship %s\n",
447 flak, prship(&ship));
448 if (pinflak_planedamage(&plp->plane, plp->pcp, ship.shp_own, flak))
452 if (nuk_on_plane(&plp->plane) >= 0)
455 hitchance = pln_hitchance(&plp->plane,
456 shp_hardtarget(&ship), EF_SHIP);
457 pr("%d%% hitchance...", hitchance);
459 if (roll(100) <= hitchance) {
460 /* pinbombing is more accurate than normal bombing */
461 dam = 2 * pln_damage(&plp->plane, 'p', 1);
464 /* Bombs that miss have to land somewhere! */
465 dam = pln_damage(&plp->plane, 'p', 0);
466 collateral_damage(target->sct_x, target->sct_y, dam);
469 if (mcp->m_flags & M_SUB)
470 nreport(player->cnum, N_SUB_BOMB, ship.shp_own, 1);
472 nreport(player->cnum, N_SHP_BOMB, ship.shp_own, 1);
474 wu(0, ship.shp_own, "%s bombs did %d damage to %s at %s\n",
475 cname(player->cnum), dam,
477 xyas(target->sct_x, target->sct_y, ship.shp_own));
480 check_retreat_and_do_shipdamage(&ship, dam);
481 if (ship.shp_rflags & RET_BOMBED)
482 if (((ship.shp_rflags & RET_INJURED) == 0) || !dam)
483 retreat_ship(&ship, 'b');
484 putship(ship.shp_uid, &ship);
485 getship(ship.shp_uid, &ship);
487 pr("%s at %s sunk!\n",
489 xyas(target->sct_x, target->sct_y, player->cnum));
491 collateral_damage(target->sct_x, target->sct_y, dam / 2);
494 free_shiplist(&head);
498 plane_bomb(struct emp_qelem *list, struct sctstr *target)
505 struct emp_qelem *qp, *next;
513 for (qp = list->q_forw; qp != list; qp = next) {
515 plp = (struct plist *)qp;
516 if (changed_plane_aborts(plp))
518 nplanes = planesatxy(target->sct_x, target->sct_y, 0, 0);
520 pr("%s could not find any planes!\n", prplane(&plp->plane));
523 (void)sprintf(prompt, "%s, %d bombs. Target ('~' to skip)? ",
524 prplane(&plp->plane), plp->load);
526 while (planeno < 0) {
527 if (!(q = getstring(prompt, buf)))
534 planesatxy(target->sct_x, target->sct_y, 0, 0);
540 if (getplane(n, &plane) &&
541 plane.pln_x == target->sct_x &&
542 plane.pln_y == target->sct_y &&
543 plane.pln_ship < 0 && plane.pln_land < 0 &&
544 !(plane.pln_flags & PLN_LAUNCHED))
547 pr("Plane #%d not spotted\n", n);
551 if (changed_plane_aborts(plp))
553 if (nuk_on_plane(&plp->plane) >= 0)
556 hitchance = pln_hitchance(&plp->plane, 0, EF_PLANE);
557 pr("%d%% hitchance...", hitchance);
559 if (roll(100) <= hitchance) {
560 /* pinbombing is more accurate than normal bombing */
561 dam = 2 * pln_damage(&plp->plane, 'p', 1);
564 /* Bombs that miss have to land somewhere! */
565 dam = pln_damage(&plp->plane, 'p', 0);
566 collateral_damage(target->sct_x, target->sct_y, dam);
572 if (dam > plane.pln_effic)
575 plane.pln_effic -= dam;
576 plane.pln_mobil = (dam * plane.pln_mobil / 100.0);
577 if (own == player->cnum) {
578 pr("%s reports %d%% damage\n", prplane(&plane), dam);
582 "%s pinpoint bombing raid did %d%% damage to %s\n",
583 cname(player->cnum), dam, prplane(&plane));
585 nreport(player->cnum, N_DOWN_PLANE, own, 1);
587 wu(0, own, "%s bombs did %d%% damage to %s at %s\n",
588 cname(player->cnum), dam, prplane(&plane),
589 xyas(target->sct_x, target->sct_y, own));
590 putplane(plane.pln_uid, &plane);
591 collateral_damage(target->sct_x, target->sct_y, dam);
596 land_bomb(struct emp_qelem *list, struct sctstr *target)
605 struct emp_qelem *qp, *next;
607 int aaf, flak, hitchance;
611 for (qp = list->q_forw; qp != list; qp = next) {
613 plp = (struct plist *)qp;
614 if (changed_plane_aborts(plp))
616 nunits = unitsatxy(target->sct_x, target->sct_y, 0, 0);
618 pr("%s could not find any units!\n", prplane(&plp->plane));
621 (void)sprintf(prompt, "%s, %d bombs. Target ('~' to skip)? ",
622 prplane(&plp->plane), plp->load);
625 if (!(q = getstring(prompt, buf)))
632 unitsatxy(target->sct_x, target->sct_y, 0, 0);
638 if (getland(n, &land) && land.lnd_own &&
639 land.lnd_ship < 0 && land.lnd_land < 0 &&
640 land.lnd_x == target->sct_x && land.lnd_y == target->sct_y)
643 pr("Unit #%d not spotted\n", n);
647 if (changed_plane_aborts(plp))
650 aaf = lnd_aaf(&land);
652 flak = roundavg(techfact(land.lnd_tech,
653 aaf * 3.0 * land.lnd_effic / 100.0));
655 "Flak! Firing flak guns from unit %s (aa rating %d)\n",
657 if (pinflak_planedamage(&plp->plane, plp->pcp, land.lnd_own, flak))
661 if (nuk_on_plane(&plp->plane) >= 0)
664 hitchance = pln_hitchance(&plp->plane,
665 lnd_hardtarget(&land), EF_LAND);
666 pr("%d%% hitchance...", hitchance);
668 if (roll(100) <= hitchance) {
669 dam = 2 * pln_damage(&plp->plane, 'p', 1);
672 /* Bombs that miss have to land somewhere! */
673 dam = pln_damage(&plp->plane, 'p', 0);
674 collateral_damage(target->sct_x, target->sct_y, dam);
680 mpr(own, "%s pinpoint bombing raid did %d damage to %s\n",
681 cname(player->cnum), dam, prland(&land));
682 check_retreat_and_do_landdamage(&land, dam);
684 if (land.lnd_rflags & RET_BOMBED)
685 if (((land.lnd_rflags & RET_INJURED) == 0) || !dam)
686 retreat_land(&land, 'b');
687 nreport(player->cnum, N_UNIT_BOMB, own, 1);
688 putland(land.lnd_uid, &land);
689 collateral_damage(target->sct_x, target->sct_y, dam);
694 strat_bomb(struct emp_qelem *list, struct sctstr *target)
698 struct emp_qelem *qp;
702 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
703 plp = (struct plist *)qp;
704 if (getnuke(nuk_on_plane(&plp->plane), &nuke))
705 detonate(&nuke, target->sct_x, target->sct_y,
706 plp->plane.pln_flags & PLN_AIRBURST);
708 dam += pln_damage(&plp->plane, 's', 1);
712 getsect(target->sct_x, target->sct_y, §);
715 wu(0, target->sct_own, "%s bombing raid did %d damage in %s\n",
716 cname(player->cnum), PERCENT_DAMAGE(dam),
717 xyas(target->sct_x, target->sct_y, target->sct_own));
719 sectdamage(target, dam);
721 pr("did %d damage in %s\n", PERCENT_DAMAGE(dam),
722 xyas(target->sct_x, target->sct_y, player->cnum));
727 changed_plane_aborts(struct plist *plp)
729 if (check_plane_ok(&plp->plane))
731 getplane(plp->plane.pln_uid, &plp->plane);
737 pinflak_planedamage(struct plnstr *pp, struct plchrstr *pcp, natid from,
744 dam = ac_flak_dam(flak, pln_def(pp), pcp->pl_flags);
747 disp = ac_damage_plane(pp, from, dam, 1, dmess);
748 mpr(pp->pln_own, " Flak! %s takes %d%s%s.\n",
749 prplane(pp), dam, *dmess ? " --" : "", dmess);
751 putplane(pp->pln_uid, pp);