2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2013, 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-2011
51 static void pin_bomb(struct emp_qelem *, struct sctstr *);
52 static void eff_bomb(struct emp_qelem *, struct sctstr *);
53 static void comm_bomb(struct emp_qelem *, struct sctstr *);
54 static void ship_bomb(struct emp_qelem *, struct sctstr *);
55 static void plane_bomb(struct emp_qelem *, struct sctstr *);
56 static void land_bomb(struct emp_qelem *, struct sctstr *);
57 static void strat_bomb(struct emp_qelem *, struct sctstr *);
58 static int changed_plane_aborts(struct plist *);
59 static int pinflak_planedamage(struct plnstr *, struct plchrstr *,
62 static i_type bombcomm[] = {
78 static int nbomb = sizeof(bombcomm) / sizeof(*bombcomm);
87 char flightpath[MAX_PATH_LEN];
88 struct nstr_item ni_bomb;
89 struct nstr_item ni_esc;
91 struct emp_qelem bomb_list;
92 struct emp_qelem esc_list;
93 struct sctstr ap_sect;
96 struct emp_qelem *qp, *next;
99 if (get_planes(&ni_bomb, &ni_esc, player->argp[1], player->argp[2]) < 0)
101 p = getstarg(player->argp[3], "pinpoint, or strategic? ", buf);
105 if (!strchr("ps", mission))
107 if (!get_assembly_point(player->argp[4], &ap_sect, buf))
111 if (!getpath(flightpath, player->argp[5], ax, ay, 0, 0, MOB_FLY))
115 (void)pathtoxy(flightpath, &tx, &ty, fcost);
116 pr("target sector is %s\n", xyas(tx, ty, player->cnum));
117 getsect(tx, ty, &target);
118 ap_to_target = strlen(flightpath);
119 if (flightpath[ap_to_target - 1] == 'h')
121 pr("range to target is %d\n", ap_to_target);
123 * select planes within range
125 pln_sel(&ni_bomb, &bomb_list, &ap_sect, ap_to_target, 2,
126 P_B | P_T, P_M | P_O);
127 pln_sel(&ni_esc, &esc_list, &ap_sect, ap_to_target, 2,
128 P_ESC | P_F, P_M | P_O);
130 * now arm and equip the bombers, transports, whatever.
132 pln_arm(&bomb_list, 2 * ap_to_target, mission, NULL);
133 if (QEMPTY(&bomb_list)) {
134 pr("No planes could be equipped for the mission.\n");
137 pln_arm(&esc_list, 2 * ap_to_target, 'e', NULL);
138 ac_encounter(&bomb_list, &esc_list, ax, ay, flightpath, 0);
139 if (QEMPTY(&bomb_list)) {
140 pr("No planes got through fighter defenses\n");
141 } else if (target.sct_type == SCT_SANCT) {
142 pr("You can't bomb that sector!\n");
146 pin_bomb(&bomb_list, &target);
147 for (qp = bomb_list.q_forw; qp != &bomb_list; qp = next) {
149 plp = (struct plist *)qp;
150 changed_plane_aborts(plp);
152 for (qp = esc_list.q_forw; qp != &esc_list; qp = next) {
154 plp = (struct plist *)qp;
155 changed_plane_aborts(plp);
159 nreport(player->cnum, N_SCT_BOMB, target.sct_own, 1);
160 strat_bomb(&bomb_list, &target);
172 pin_bomb(struct emp_qelem *list, struct sctstr *target)
186 type = target->sct_type;
188 pr("Target sector is a %s constructed %s\n",
189 effadv((int)target->sct_effic), dcp->d_name);
191 nships = shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
192 if (pln_caps(list) & P_A) {
193 nsubs = shipsatxy(target->sct_x, target->sct_y, M_SUB, 0, 1);
195 pr("Some subs are present in the sector.\n");
197 nplanes = planesatxy(target->sct_x, target->sct_y, 0, 0);
198 nunits = unitsatxy(target->sct_x, target->sct_y, 0, 0);
200 p = getstring("Bomb what? (ship, plane, land unit, efficiency, commodities) ",
213 pr("no units there\n");
216 land_bomb(list, target);
220 pr("no planes there\n");
223 plane_bomb(list, target);
227 if (pln_caps(list) & P_A) {
229 pr("no ships there\n");
233 pr("no ships there\n");
237 ship_bomb(list, target);
240 for (i = 0; i < nbomb; i++) {
241 if (!target->sct_item[bombcomm[i]])
246 pr("No bombable commodities in %s\n",
247 xyas(target->sct_x, target->sct_y, player->cnum));
250 comm_bomb(list, target);
253 eff_bomb(list, target);
256 pr("Aborting mission.\n");
259 pr("Bad target type.\n");
265 eff_bomb(struct emp_qelem *list, struct sctstr *target)
268 struct emp_qelem *qp, *next;
272 for (qp = list->q_forw; qp != list; qp = next) {
274 plp = (struct plist *)qp;
275 if (changed_plane_aborts(plp))
277 dam += pln_damage(&plp->plane, 'p', 1);
279 getsect(target->sct_x, target->sct_y, §);
281 oldeff = target->sct_effic;
282 target->sct_effic = effdamage(target->sct_effic, dam);
283 target->sct_avail = effdamage(target->sct_avail, dam);
284 target->sct_road = effdamage(target->sct_road, dam);
285 target->sct_rail = effdamage(target->sct_rail, dam);
286 target->sct_defense = effdamage(target->sct_defense, dam);
287 pr("did %d%% damage to efficiency in %s\n",
288 oldeff - target->sct_effic,
289 xyas(target->sct_x, target->sct_y, player->cnum));
291 wu(0, target->sct_own,
292 "%s bombing raid did %d%% damage in %s\n",
293 cname(player->cnum), oldeff - target->sct_effic,
294 xyas(target->sct_x, target->sct_y, target->sct_own));
295 bridge_damaged(target);
297 collateral_damage(target->sct_x, target->sct_y, dam);
301 comm_bomb(struct emp_qelem *list, struct sctstr *target)
308 struct emp_qelem *qp, *next;
312 for (i = 0; i < nbomb; i++) {
313 if (target->sct_item[bombcomm[i]] == 0)
315 if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
317 ip = &ichr[bombcomm[i]];
318 pr("some %s\n", ip->i_name);
321 ip = whatitem(NULL, "commodity to bomb? ");
327 for (i = 0; i < nbomb; i++) {
328 if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
330 if (&ichr[bombcomm[i]] == ip)
334 pr("You can't bomb %s!\n", ip->i_name);
335 for (i = 0; i < nbomb; i++) {
336 if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
338 pr("%s%s", i == 0 ? "Bombable: " : ", ",
339 ichr[bombcomm[i]].i_name);
345 for (qp = list->q_forw; qp != list; qp = next) {
347 plp = (struct plist *)qp;
348 if (changed_plane_aborts(plp))
350 dam += pln_damage(&plp->plane, 'p', 1);
352 getsect(target->sct_x, target->sct_y, §);
354 before = target->sct_item[ip->i_uid];
355 target->sct_item[ip->i_uid] = amt = commdamage(before, dam, ip->i_uid);
357 b = 100.0 * (1.0 - (double)amt / (double)before);
360 pr("did %.2f%% damage to %s in %s\n",
361 b, ip->i_name, xyas(target->sct_x, target->sct_y, player->cnum));
362 nreport(player->cnum, N_SCT_BOMB, target->sct_own, 1);
363 if (target->sct_own != 0)
364 wu(0, target->sct_own,
365 "%s precision bombing raid did %.2f%% damage to %s in %s\n",
366 cname(player->cnum), b, ip->i_name,
367 xyas(target->sct_x, target->sct_y, target->sct_own));
369 collateral_damage(target->sct_x, target->sct_y, dam);
373 ship_bomb(struct emp_qelem *list, struct sctstr *target)
380 struct emp_qelem *qp, *next;
384 struct shiplist *head = NULL;
391 for (qp = list->q_forw; qp != list; qp = next) {
393 free_shiplist(&head);
394 plp = (struct plist *)qp;
395 if (changed_plane_aborts(plp))
397 if (plp->pcp->pl_flags & P_A)
398 nships = asw_shipsatxy(target->sct_x, target->sct_y, 0, 0,
401 nships = shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
403 pr("%s could not find any ships!\n", prplane(&plp->plane));
406 (void)sprintf(prompt, "%s, %d bombs. Target ('~' to skip)? ",
407 prplane(&plp->plane), plp->load);
410 if (!(q = getstring(prompt, buf)))
417 if (plp->pcp->pl_flags & P_A)
418 print_shiplist(head);
420 shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
426 if ((!(plp->pcp->pl_flags & P_A) || on_shiplist(n, head)) &&
427 getship(n, &ship) && ship.shp_own &&
428 ship.shp_x == target->sct_x && ship.shp_y == target->sct_y)
431 pr("Ship #%d not spotted\n", n);
435 if ((plp->pcp->pl_flags & P_A) && !on_shiplist(shipno, head))
437 if (changed_plane_aborts(plp))
440 gun = shp_usable_guns(&ship);
441 mcp = &mchr[(int)ship.shp_type];
442 if (gun > 0 && !(mcp->m_flags & M_SUB)) {
443 flak = (int)(techfact(ship.shp_tech, gun) * 2.0);
444 mpr(ship.shp_own, "Flak! Firing %d guns from ship %s\n",
445 flak, prship(&ship));
446 if (pinflak_planedamage(&plp->plane, plp->pcp, ship.shp_own, flak))
450 if (nuk_on_plane(&plp->plane) >= 0)
453 hitchance = pln_hitchance(&plp->plane,
454 shp_hardtarget(&ship), EF_SHIP);
455 pr("%d%% hitchance...", hitchance);
457 if (roll(100) <= hitchance) {
458 /* pinbombing is more accurate than normal bombing */
459 dam = 2 * pln_damage(&plp->plane, 'p', 1);
462 /* Bombs that miss have to land somewhere! */
463 dam = pln_damage(&plp->plane, 'p', 0);
464 collateral_damage(target->sct_x, target->sct_y, dam);
467 if (mcp->m_flags & M_SUB)
468 nreport(player->cnum, N_SUB_BOMB, ship.shp_own, 1);
470 nreport(player->cnum, N_SHP_BOMB, ship.shp_own, 1);
472 wu(0, ship.shp_own, "%s bombs did %d damage to %s at %s\n",
473 cname(player->cnum), dam,
475 xyas(target->sct_x, target->sct_y, ship.shp_own));
478 check_retreat_and_do_shipdamage(&ship, dam);
479 if (ship.shp_rflags & RET_BOMBED)
480 if (((ship.shp_rflags & RET_INJURED) == 0) || !dam)
481 retreat_ship(&ship, 'b');
482 putship(ship.shp_uid, &ship);
483 getship(ship.shp_uid, &ship);
485 pr("%s at %s sunk!\n",
487 xyas(target->sct_x, target->sct_y, player->cnum));
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) &&
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 (roll(100) <= hitchance) {
558 /* pinbombing is more accurate than normal bombing */
559 dam = 2 * pln_damage(&plp->plane, 'p', 1);
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);
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);
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 (roll(100) <= hitchance) {
667 dam = 2 * pln_damage(&plp->plane, 'p', 1);
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 check_retreat_and_do_landdamage(&land, dam);
682 if (land.lnd_rflags & RET_BOMBED)
683 if (((land.lnd_rflags & RET_INJURED) == 0) || !dam)
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', 1);
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);