2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2008, 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-2008
52 static void pin_bomb(struct emp_qelem *list, struct sctstr *target);
53 static void strat_bomb(struct emp_qelem *list, struct sctstr *target);
54 static void comm_bomb(struct emp_qelem *list, struct sctstr *target);
55 static void eff_bomb(struct emp_qelem *list, struct sctstr *target);
56 static int pinflak_planedamage(struct plnstr *pp, struct plchrstr *pcp,
57 natid from, int flak);
58 static void plane_bomb(struct emp_qelem *list, struct sctstr *target);
59 static void land_bomb(struct emp_qelem *list, struct sctstr *target);
60 static void ship_bomb(struct emp_qelem *list, struct sctstr *target);
62 static i_type bombcomm[] = {
78 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;
95 struct sctstr ap_sect;
102 if (get_planes(&ni_bomb, &ni_esc, player->argp[1], player->argp[2]) < 0)
104 p = getstarg(player->argp[3], "pinpoint, or strategic? ", buf);
108 if (strchr("ps", mission) == 0)
110 if (!get_assembly_point(player->argp[4], &ap_sect, buf))
114 if (getpath(flightpath, player->argp[5], ax, ay, 0, 0, P_FLYING) == 0
119 (void)pathtoxy(flightpath, &tx, &ty, fcost);
120 pr("target sector is %s\n", xyas(tx, ty, player->cnum));
121 getsect(tx, ty, &target);
123 ap_to_target = strlen(flightpath);
124 if (flightpath[ap_to_target - 1] == 'h')
126 pr("range to target is %d\n", ap_to_target);
128 * select planes within range
130 pln_sel(&ni_bomb, &bomb_list, &ap_sect, ap_to_target,
131 2, wantflags, P_M | P_O);
132 pln_sel(&ni_esc, &esc_list, &ap_sect, ap_to_target,
133 2, wantflags | P_F | P_ESC, P_M | P_O);
135 * now arm and equip the bombers, transports, whatever.
137 pln_arm(&bomb_list, 2 * ap_to_target, mission, ip, 0);
138 if (QEMPTY(&bomb_list)) {
139 pr("No planes could be equipped for the mission.\n");
142 pln_arm(&esc_list, 2 * ap_to_target, mission, ip, P_F | P_ESC);
143 ac_encounter(&bomb_list, &esc_list, ax, ay, flightpath, 0);
144 if (QEMPTY(&bomb_list)) {
145 pr("No planes got through fighter defenses\n");
146 } else if (target.sct_type == SCT_SANCT) {
147 pr("You can't bomb that sector!\n");
151 pin_bomb(&bomb_list, &target);
155 natp = getnatp(player->cnum);
156 if (target.sct_own) {
157 rel = getrel(natp, target.sct_own);
158 if ((rel != AT_WAR) && (player->cnum != target.sct_own)
159 && (target.sct_own) &&
160 (target.sct_oldown != player->cnum)) {
161 pr("You're not at war with them!\n");
168 nreport(player->cnum, N_SCT_BOMB, target.sct_own, 1);
169 strat_bomb(&bomb_list, &target);
181 pin_bomb(struct emp_qelem *list, struct sctstr *target)
198 type = target->sct_type;
200 pr("Target sector is a %s constructed %s\n",
201 effadv((int)target->sct_effic), dcp->d_name);
203 plp = (struct plist *)list->q_forw;
204 nships = shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
205 if (plp->pcp->pl_flags & P_A) {
206 nsubs = shipsatxy(target->sct_x, target->sct_y, M_SUB, 0, 1);
208 pr("Some subs are present in the sector.\n");
210 nplanes = planesatxy(target->sct_x, target->sct_y, 0, 0);
211 nunits = unitsatxy(target->sct_x, target->sct_y, 0, 0);
213 p = getstring("Bomb what? (ship, plane, land unit, efficiency, commodities) ",
226 natp = getnatp(player->cnum);
227 if (target->sct_own) {
228 rel = getrel(natp, target->sct_own);
229 if ((rel != AT_WAR) && (player->cnum != target->sct_own)
230 && (target->sct_own) &&
231 (target->sct_oldown != player->cnum)) {
232 pr("You're not at war with them!\n");
238 pr("no units there\n");
241 land_bomb(list, target);
245 natp = getnatp(player->cnum);
246 if (target->sct_own) {
247 rel = getrel(natp, target->sct_own);
248 if ((rel != AT_WAR) && (player->cnum != target->sct_own)
249 && (target->sct_own) &&
250 (target->sct_oldown != player->cnum)) {
251 pr("You're not at war with them!\n");
257 pr("no planes there\n");
260 plane_bomb(list, target);
264 if (((struct plist *)list->q_forw)->pcp->pl_flags & P_A) {
266 pr("no ships there\n");
270 pr("no ships there\n");
274 ship_bomb(list, target);
278 natp = getnatp(player->cnum);
279 if (target->sct_own) {
280 rel = getrel(natp, target->sct_own);
281 if ((rel != AT_WAR) && (player->cnum != target->sct_own)
282 && (target->sct_own) &&
283 (target->sct_oldown != player->cnum)) {
284 pr("You're not at war with them!\n");
290 for (i = 0; i < nbomb; i++) {
291 if (!target->sct_item[bombcomm[i]])
296 pr("No bombable commodities in %s\n",
297 xyas(target->sct_x, target->sct_y, player->cnum));
300 comm_bomb(list, target);
304 natp = getnatp(player->cnum);
305 if (target->sct_own) {
306 rel = getrel(natp, target->sct_own);
307 if ((rel != AT_WAR) && (player->cnum != target->sct_own)
308 && (target->sct_own) &&
309 (target->sct_oldown != player->cnum)) {
310 pr("You're not at war with them!\n");
315 eff_bomb(list, target);
318 pr("Aborting mission.\n");
321 pr("Bad target type.\n");
327 eff_bomb(struct emp_qelem *list, struct sctstr *target)
330 struct emp_qelem *qp;
335 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
336 plp = (struct plist *)qp;
337 if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
339 if (plp->bombs || nuk_on_plane(&plp->plane) >= 0)
340 dam += pln_damage(&plp->plane, target->sct_x, target->sct_y,
343 if (dam <= 0) /* dam == 0 if only nukes were delivered */
345 getsect(target->sct_x, target->sct_y, §);
347 oldeff = target->sct_effic;
348 target->sct_effic = effdamage(target->sct_effic, dam);
349 target->sct_avail = effdamage(target->sct_avail, dam);
350 target->sct_road = effdamage(target->sct_road, dam);
351 target->sct_rail = effdamage(target->sct_rail, dam);
352 target->sct_defense = effdamage(target->sct_defense, dam);
353 pr("did %d%% damage to efficiency in %s\n",
354 oldeff - target->sct_effic,
355 xyas(target->sct_x, target->sct_y, player->cnum));
357 wu(0, target->sct_own,
358 "%s bombing raid did %d%% damage in %s\n",
359 cname(player->cnum), oldeff - target->sct_effic,
360 xyas(target->sct_x, target->sct_y, target->sct_own));
361 bridge_damaged(target);
363 collateral_damage(target->sct_x, target->sct_y, dam);
367 comm_bomb(struct emp_qelem *list, struct sctstr *target)
374 struct emp_qelem *qp;
379 for (i = 0; i < nbomb; i++) {
380 if (target->sct_item[bombcomm[i]] == 0)
382 if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
384 ip = &ichr[bombcomm[i]];
385 pr("some %s\n", ip->i_name);
388 ip = whatitem(NULL, "commodity to bomb? ");
394 for (i = 0; i < nbomb; i++) {
395 if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
397 if (&ichr[bombcomm[i]] == ip)
401 pr("You can't bomb %s!\n", ip->i_name);
402 for (i = 0; i < nbomb; i++) {
403 if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
405 pr("%s%s", i == 0 ? "Bombable: " : ", ",
406 ichr[bombcomm[i]].i_name);
412 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
413 plp = (struct plist *)qp;
414 if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
416 if (plp->bombs || nuk_on_plane(&plp->plane) >= 0)
417 dam += pln_damage(&plp->plane, target->sct_x, target->sct_y,
420 if (dam <= 0) /* dam == 0 if only nukes were delivered */
422 getsect(target->sct_x, target->sct_y, §);
424 before = target->sct_item[ip->i_uid];
425 target->sct_item[ip->i_uid] = amt = commdamage(before, dam, ip->i_uid);
427 b = 100.0 * (1.0 - (double)amt / (double)before);
430 pr("did %.2f%% damage to %s in %s\n",
431 b, ip->i_name, xyas(target->sct_x, target->sct_y, player->cnum));
432 nreport(player->cnum, N_SCT_BOMB, target->sct_own, 1);
433 if (target->sct_own != 0)
434 wu(0, target->sct_own,
435 "%s precision bombing raid did %.2f%% damage to %s in %s\n",
436 cname(player->cnum), b, ip->i_name,
437 xyas(target->sct_x, target->sct_y, target->sct_own));
439 collateral_damage(target->sct_x, target->sct_y, dam);
443 ship_bomb(struct emp_qelem *list, struct sctstr *target)
450 struct emp_qelem *qp;
454 struct shiplist *head = NULL;
463 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
464 free_shiplist(&head);
465 plp = (struct plist *)qp;
466 if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
468 if (plp->pcp->pl_flags & P_A)
469 nships = asw_shipsatxy(target->sct_x, target->sct_y, 0, 0,
472 nships = shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
474 pr("%s could not find any ships!\n", prplane(&plp->plane));
477 (void)sprintf(prompt, "%s, %d bombs. Target ('~' to skip)? ",
478 prplane(&plp->plane), plp->bombs);
481 if ((q = getstring(prompt, buf)) == 0)
488 if (plp->pcp->pl_flags & P_A)
489 print_shiplist(head);
491 shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
497 if ((!(plp->pcp->pl_flags & P_A) || on_shiplist(n, head)) &&
498 getship(n, &ship) && ship.shp_own &&
499 ship.shp_x == target->sct_x && ship.shp_y == target->sct_y)
502 pr("Ship #%d not spotted\n", n);
506 if ((plp->pcp->pl_flags & P_A) && !on_shiplist(shipno, head))
510 gun = shp_usable_guns(&ship);
512 shell = ship.shp_item[I_SHELL];
514 shell = supply_commod(ship.shp_own,
515 ship.shp_x, ship.shp_y, I_SHELL, 1);
517 mcp = &mchr[(int)ship.shp_type];
518 if (gun > 0 && shell > 0 && !(mcp->m_flags & M_SUB)) {
519 flak = (int)(techfact(ship.shp_tech, gun) * 2.0);
520 ship.shp_item[I_SHELL] = shell;
521 putship(ship.shp_uid, &ship);
522 PR(ship.shp_own, "Flak! Firing %d guns from ship %s\n",
523 flak, prship(&ship));
524 if (pinflak_planedamage(&plp->plane, plp->pcp, ship.shp_own, flak))
529 if (nuk_on_plane(&plp->plane) >= 0)
532 hitchance = pln_hitchance(&plp->plane,
533 shp_hardtarget(&ship), EF_SHIP);
534 pr("%d%% hitchance...", hitchance);
536 if (roll(100) <= hitchance) {
537 /* pinbombing is more accurate than normal bombing */
538 dam = 2 * pln_damage(&plp->plane, target->sct_x, target->sct_y,
542 /* Bombs that miss have to land somewhere! */
543 dam = pln_damage(&plp->plane, target->sct_x, target->sct_y,
545 collateral_damage(target->sct_x, target->sct_y, dam);
548 if (dam <= 0) /* dam == 0 if only nukes were delivered */
550 if (mcp->m_flags & M_SUB)
551 nreport(player->cnum, N_SUB_BOMB, ship.shp_own, 1);
553 nreport(player->cnum, N_SHP_BOMB, ship.shp_own, 1);
555 wu(0, ship.shp_own, "%s bombs did %d damage to %s at %s\n",
556 cname(player->cnum), dam,
558 xyas(target->sct_x, target->sct_y, ship.shp_own));
561 check_retreat_and_do_shipdamage(&ship, dam);
562 if (ship.shp_rflags & RET_BOMBED)
563 if (((ship.shp_rflags & RET_INJURED) == 0) || !dam)
564 retreat_ship(&ship, 'b');
565 putship(ship.shp_uid, &ship);
566 getship(ship.shp_uid, &ship);
568 pr("%s at %s sunk!\n",
570 xyas(target->sct_x, target->sct_y, player->cnum));
572 collateral_damage(target->sct_x, target->sct_y, dam / 2);
575 free_shiplist(&head);
579 plane_bomb(struct emp_qelem *list, struct sctstr *target)
586 struct emp_qelem *qp;
595 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
596 plp = (struct plist *)qp;
597 if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
599 nplanes = planesatxy(target->sct_x, target->sct_y, 0, 0);
601 pr("%s could not find any planes!\n", prplane(&plp->plane));
604 (void)sprintf(prompt, "%s, %d bombs. Target ('~' to skip)? ",
605 prplane(&plp->plane), plp->bombs);
607 while (planeno < 0) {
608 if ((q = getstring(prompt, buf)) == 0)
615 planesatxy(target->sct_x, target->sct_y, 0, 0);
621 if (getplane(n, &plane) &&
622 plane.pln_x == target->sct_x &&
623 plane.pln_y == target->sct_y &&
624 plane.pln_ship < 0 && plane.pln_land < 0 &&
625 !(plane.pln_flags & PLN_LAUNCHED))
628 pr("Plane #%d not spotted\n", n);
633 if (nuk_on_plane(&plp->plane) >= 0)
636 hitchance = pln_hitchance(&plp->plane, 0, EF_PLANE);
637 pr("%d%% hitchance...", hitchance);
639 if (roll(100) <= hitchance) {
640 /* pinbombing is more accurate than normal bombing */
641 dam = 2 * pln_damage(&plp->plane, target->sct_x, target->sct_y,
645 /* Bombs that miss have to land somewhere! */
646 dam = pln_damage(&plp->plane, target->sct_x, target->sct_y,
648 collateral_damage(target->sct_x, target->sct_y, dam);
651 if (dam <= 0) /* dam == 0 if only nukes were delivered */
656 if (dam > plane.pln_effic)
659 plane.pln_effic -= dam;
660 plane.pln_mobil = (dam * plane.pln_mobil / 100.0);
661 if (own == player->cnum) {
662 pr("%s reports %d%% damage\n", prplane(&plane), dam);
666 "%s pinpoint bombing raid did %d%% damage to %s\n",
667 cname(player->cnum), dam, prplane(&plane));
669 nreport(player->cnum, N_DOWN_PLANE, own, 1);
671 wu(0, own, "%s bombs did %d%% damage to %s at %s\n",
672 cname(player->cnum), dam, prplane(&plane),
673 xyas(target->sct_x, target->sct_y, own));
674 putplane(plane.pln_uid, &plane);
675 collateral_damage(target->sct_x, target->sct_y, dam);
680 land_bomb(struct emp_qelem *list, struct sctstr *target)
689 struct emp_qelem *qp;
691 int aaf, flak, hitchance;
696 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
697 plp = (struct plist *)qp;
698 if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
700 nunits = unitsatxy(target->sct_x, target->sct_y, 0, 0);
702 pr("%s could not find any units!\n", prplane(&plp->plane));
705 (void)sprintf(prompt, "%s, %d bombs. Target ('~' to skip)? ",
706 prplane(&plp->plane), plp->bombs);
709 if ((q = getstring(prompt, buf)) == 0)
716 unitsatxy(target->sct_x, target->sct_y, 0, 0);
722 if (getland(n, &land) && land.lnd_own &&
723 land.lnd_ship < 0 && land.lnd_land < 0 &&
724 land.lnd_x == target->sct_x && land.lnd_y == target->sct_y)
727 pr("Unit #%d not spotted\n", n);
732 aaf = lnd_aaf(&land);
734 flak = roundavg(techfact(land.lnd_tech,
735 aaf * 3.0 * land.lnd_effic / 100.0));
737 "Flak! Firing flak guns from unit %s (aa rating %d)\n",
739 if (pinflak_planedamage(&plp->plane, plp->pcp, land.lnd_own, flak))
744 if (nuk_on_plane(&plp->plane) >= 0)
747 hitchance = pln_hitchance(&plp->plane,
748 lnd_hardtarget(&land), EF_LAND);
749 pr("%d%% hitchance...", hitchance);
751 if (roll(100) <= hitchance) {
752 dam = 2 * pln_damage(&plp->plane, target->sct_x, target->sct_y,
756 /* Bombs that miss have to land somewhere! */
757 dam = pln_damage(&plp->plane, target->sct_x, target->sct_y,
759 collateral_damage(target->sct_x, target->sct_y, dam);
762 if (dam <= 0) /* dam == 0 if only nukes were delivered */
767 mpr(own, "%s pinpoint bombing raid did %d damage to %s\n",
768 cname(player->cnum), dam, prland(&land));
769 check_retreat_and_do_landdamage(&land, dam);
771 if (land.lnd_rflags & RET_BOMBED)
772 if (((land.lnd_rflags & RET_INJURED) == 0) || !dam)
773 retreat_land(&land, 'b');
774 nreport(player->cnum, N_UNIT_BOMB, own, 1);
775 putland(land.lnd_uid, &land);
776 collateral_damage(target->sct_x, target->sct_y, dam);
781 strat_bomb(struct emp_qelem *list, struct sctstr *target)
785 struct emp_qelem *qp;
789 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
790 plp = (struct plist *)qp;
791 if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
793 if (plp->bombs || nuk_on_plane(&plp->plane) >= 0)
794 dam += pln_damage(&plp->plane, target->sct_x, target->sct_y,
797 if (dam <= 0) /* dam == 0 if only nukes were delivered */
799 getsect(target->sct_x, target->sct_y, §);
802 wu(0, target->sct_own, "%s bombing raid did %d damage in %s\n",
803 cname(player->cnum), PERCENT_DAMAGE(dam),
804 xyas(target->sct_x, target->sct_y, target->sct_own));
806 sectdamage(target, dam);
808 pr("did %d damage in %s\n", PERCENT_DAMAGE(dam),
809 xyas(target->sct_x, target->sct_y, player->cnum));
814 pinflak_planedamage(struct plnstr *pp, struct plchrstr *pcp, natid from,
823 dam = ac_flak_dam(flak, pln_def(pp), pcp->pl_flags);
825 plane_owner = pp->pln_own;
829 memset(dmess, 0, sizeof(dmess));
833 if (eff < PLANE_MINEFF) {
834 sprintf(dmess, " -- shot down");
836 } else if (chance((100 - eff) / 100.0)) {
837 sprintf(dmess, " -- aborted with %d%% damage", 100 - eff);
840 PR(plane_owner, " Flak! %s %s takes %d%s.\n",
841 cname(pp->pln_own), prplane(pp), dam, dmess);
846 nreport(from, N_DOWN_PLANE, pp->pln_own, 1);
848 putplane(pp->pln_uid, pp);