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
51 static void pin_bomb(struct emp_qelem *list, struct sctstr *target);
52 static void strat_bomb(struct emp_qelem *list, struct sctstr *target);
53 static void comm_bomb(struct emp_qelem *list, struct sctstr *target);
54 static void eff_bomb(struct emp_qelem *list, struct sctstr *target);
55 static int pinflak_planedamage(struct plnstr *pp, struct plchrstr *pcp,
56 natid from, int flak);
57 static void plane_bomb(struct emp_qelem *list, struct sctstr *target);
58 static void land_bomb(struct emp_qelem *list, struct sctstr *target);
59 static void ship_bomb(struct emp_qelem *list, struct sctstr *target);
61 static i_type bombcomm[] = {
77 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 (!snxtitem(&ni_bomb, EF_PLANE, player->argp[1]))
104 if (!snxtitem(&ni_esc, EF_PLANE,
105 getstarg(player->argp[2], "escort(s)? ", buf)))
106 pr("No escorts...\n");
107 p = getstarg(player->argp[3], "pinpoint, or strategic? ", buf);
111 if (strchr("ps", mission) == 0)
113 if (!get_assembly_point(player->argp[4], &ap_sect, buf))
117 if (getpath(flightpath, player->argp[5], ax, ay, 0, 0, P_FLYING) == 0
122 (void)pathtoxy(flightpath, &tx, &ty, fcost);
123 pr("target sector is %s\n", xyas(tx, ty, player->cnum));
124 getsect(tx, ty, &target);
126 ap_to_target = strlen(flightpath);
127 if (flightpath[ap_to_target - 1] == 'h')
129 pr("range to target is %d\n", ap_to_target);
131 * select planes within range
134 pln_sel(&ni_bomb, &bomb_list, &ap_sect, ap_to_target,
135 2, wantflags, P_M | P_O);
136 pln_sel(&ni_esc, &esc_list, &ap_sect, ap_to_target,
137 2, wantflags | P_F | P_ESC, P_M | P_O);
139 * now arm and equip the bombers, transports, whatever.
141 mission_flags |= P_X; /* stealth (shhh) */
142 mission_flags |= P_H; /* gets turned off if not all choppers */
143 mission_flags = pln_arm(&bomb_list, 2 * ap_to_target, mission,
144 ip, 0, mission_flags);
145 if (QEMPTY(&bomb_list)) {
146 pr("No planes could be equipped for the mission.\n");
149 mission_flags = pln_arm(&esc_list, 2 * ap_to_target, mission,
150 ip, P_F | P_ESC, mission_flags);
151 ac_encounter(&bomb_list, &esc_list, ax, ay,
152 flightpath, mission_flags, 0);
153 if (QEMPTY(&bomb_list)) {
154 pr("No planes got through fighter defenses\n");
155 } else if (target.sct_type == SCT_SANCT) {
156 pr("You can't bomb that sector!\n");
160 pin_bomb(&bomb_list, &target);
164 natp = getnatp(player->cnum);
165 if (target.sct_own) {
166 rel = getrel(natp, target.sct_own);
167 if ((rel != AT_WAR) && (player->cnum != target.sct_own)
168 && (target.sct_own) &&
169 (target.sct_oldown != player->cnum)) {
170 pr("You're not at war with them!\n");
177 nreport(player->cnum, N_SCT_BOMB, target.sct_own, 1);
178 strat_bomb(&bomb_list, &target);
190 pin_bomb(struct emp_qelem *list, struct sctstr *target)
207 type = target->sct_type;
209 pr("Target sector is a %s constructed %s\n",
210 effadv((int)target->sct_effic), dcp->d_name);
212 plp = (struct plist *)list->q_forw;
213 nships = shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
214 if (plp->pcp->pl_flags & P_A) {
215 nsubs = shipsatxy(target->sct_x, target->sct_y, M_SUB, 0, 1);
217 pr("Some subs are present in the sector.\n");
219 nplanes = planesatxy(target->sct_x, target->sct_y, 0, 0);
220 nunits = unitsatxy(target->sct_x, target->sct_y, 0, 0);
222 p = getstring("Bomb what? (ship, plane, land unit, efficiency, commodities) ",
235 natp = getnatp(player->cnum);
236 if (target->sct_own) {
237 rel = getrel(natp, target->sct_own);
238 if ((rel != AT_WAR) && (player->cnum != target->sct_own)
239 && (target->sct_own) &&
240 (target->sct_oldown != player->cnum)) {
241 pr("You're not at war with them!\n");
247 pr("no units there\n");
250 land_bomb(list, target);
254 natp = getnatp(player->cnum);
255 if (target->sct_own) {
256 rel = getrel(natp, target->sct_own);
257 if ((rel != AT_WAR) && (player->cnum != target->sct_own)
258 && (target->sct_own) &&
259 (target->sct_oldown != player->cnum)) {
260 pr("You're not at war with them!\n");
266 pr("no planes there\n");
269 plane_bomb(list, target);
273 if (((struct plist *)list->q_forw)->pcp->pl_flags & P_A) {
275 pr("no ships there\n");
279 pr("no ships there\n");
283 ship_bomb(list, target);
287 natp = getnatp(player->cnum);
288 if (target->sct_own) {
289 rel = getrel(natp, target->sct_own);
290 if ((rel != AT_WAR) && (player->cnum != target->sct_own)
291 && (target->sct_own) &&
292 (target->sct_oldown != player->cnum)) {
293 pr("You're not at war with them!\n");
299 for (i = 0; i < nbomb; i++) {
300 if (!target->sct_item[bombcomm[i]])
305 pr("No bombable commodities in %s\n",
306 xyas(target->sct_x, target->sct_y, player->cnum));
309 comm_bomb(list, target);
313 natp = getnatp(player->cnum);
314 if (target->sct_own) {
315 rel = getrel(natp, target->sct_own);
316 if ((rel != AT_WAR) && (player->cnum != target->sct_own)
317 && (target->sct_own) &&
318 (target->sct_oldown != player->cnum)) {
319 pr("You're not at war with them!\n");
324 eff_bomb(list, target);
327 pr("Aborting mission.\n");
330 pr("Bad target type.\n");
336 eff_bomb(struct emp_qelem *list, struct sctstr *target)
339 struct emp_qelem *qp;
344 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
345 plp = (struct plist *)qp;
346 if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
348 if (plp->bombs || plp->plane.pln_nuketype != -1)
349 dam += pln_damage(&plp->plane, target->sct_x, target->sct_y,
352 if (dam <= 0) /* dam == 0 if only nukes were delivered */
354 getsect(target->sct_x, target->sct_y, §);
356 oldeff = target->sct_effic;
357 target->sct_effic = effdamage(target->sct_effic, dam);
358 target->sct_avail = effdamage(target->sct_avail, dam);
359 target->sct_road = effdamage(target->sct_road, dam);
360 target->sct_rail = effdamage(target->sct_rail, dam);
361 target->sct_defense = effdamage(target->sct_defense, dam);
362 pr("did %d%% damage to efficiency in %s\n",
363 oldeff - target->sct_effic,
364 xyas(target->sct_x, target->sct_y, player->cnum));
366 wu(0, target->sct_own,
367 "%s bombing raid did %d%% damage in %s\n",
368 cname(player->cnum), oldeff - target->sct_effic,
369 xyas(target->sct_x, target->sct_y, target->sct_own));
370 bridge_damaged(target);
372 collateral_damage(target->sct_x, target->sct_y, dam);
376 comm_bomb(struct emp_qelem *list, struct sctstr *target)
383 struct emp_qelem *qp;
388 for (i = 0; i < nbomb; i++) {
389 if (target->sct_item[bombcomm[i]] == 0)
391 if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
393 ip = &ichr[bombcomm[i]];
394 pr("some %s\n", ip->i_name);
397 ip = whatitem(NULL, "commodity to bomb? ");
403 for (i = 0; i < nbomb; i++) {
404 if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
406 if (&ichr[bombcomm[i]] == ip)
410 pr("You can't bomb %s!\n", ip->i_name);
411 for (i = 0; i < nbomb; i++) {
412 if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
414 pr("%s%s", i == 0 ? "Bombable: " : ", ",
415 ichr[bombcomm[i]].i_name);
421 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
422 plp = (struct plist *)qp;
423 if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
425 if (plp->bombs || plp->plane.pln_nuketype != -1)
426 dam += pln_damage(&plp->plane, target->sct_x, target->sct_y,
429 if (dam <= 0) /* dam == 0 if only nukes were delivered */
431 getsect(target->sct_x, target->sct_y, §);
433 before = target->sct_item[ip->i_uid];
434 target->sct_item[ip->i_uid] = amt = commdamage(before, dam, ip->i_uid);
436 b = 100.0 * (1.0 - (double)amt / (double)before);
439 pr("did %.2f%% damage to %s in %s\n",
440 b, ip->i_name, xyas(target->sct_x, target->sct_y, player->cnum));
441 nreport(player->cnum, N_SCT_BOMB, target->sct_own, 1);
442 if (target->sct_own != 0)
443 wu(0, target->sct_own,
444 "%s precision bombing raid did %.2f%% damage to %s in %s\n",
445 cname(player->cnum), b, ip->i_name,
446 xyas(target->sct_x, target->sct_y, target->sct_own));
448 collateral_damage(target->sct_x, target->sct_y, dam);
452 ship_bomb(struct emp_qelem *list, struct sctstr *target)
459 struct emp_qelem *qp;
464 struct shiplist *head = NULL;
473 for (qp = list->q_forw; qp != list && !player->aborted;
475 free_shiplist(&head);
476 plp = (struct plist *)qp;
477 if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
479 if (plp->pcp->pl_flags & P_A)
480 nships = asw_shipsatxy(target->sct_x, target->sct_y, 0, 0,
483 nships = shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
485 pr("%s could not find any ships!\n", prplane(&plp->plane));
488 (void)sprintf(prompt, "%s, %d bombs. Target ('~' to skip)? ",
489 prplane(&plp->plane), plp->bombs);
492 while (shipno < 0 && !player->aborted && !ignore) {
493 if ((q = getstring(prompt, buf)) == 0 || *q == 0)
500 if (plp->pcp->pl_flags & P_A)
501 print_shiplist(head);
503 shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
513 if ((plp->pcp->pl_flags & P_A) && !on_shiplist(n, head))
515 if (getship(n, &ship) && ship.shp_own &&
516 ship.shp_x == target->sct_x && ship.shp_y == target->sct_y)
521 if ((plp->pcp->pl_flags & P_A) && !on_shiplist(shipno, head))
525 gun = shp_usable_guns(&ship);
527 shell = ship.shp_item[I_SHELL];
529 shell = supply_commod(ship.shp_own,
530 ship.shp_x, ship.shp_y, I_SHELL, 1);
532 mcp = &mchr[(int)ship.shp_type];
533 if (gun > 0 && shell > 0 && !(mcp->m_flags & M_SUB)) {
534 flak = (int)(techfact(ship.shp_tech, gun) * 2.0);
535 ship.shp_item[I_SHELL] = shell;
536 putship(ship.shp_uid, &ship);
537 PR(ship.shp_own, "Flak! Firing %d guns from ship %s\n",
538 flak, prship(&ship));
539 if (pinflak_planedamage(&plp->plane, plp->pcp, ship.shp_own, flak))
544 if (plp->plane.pln_nuketype != -1)
547 hitchance = pln_hitchance(&plp->plane,
548 shp_hardtarget(&ship), EF_SHIP);
549 pr("%d%% hitchance...", hitchance);
551 if (roll(100) <= hitchance) {
552 /* pinbombing is more accurate than normal bombing */
553 dam = 2 * pln_damage(&plp->plane, target->sct_x, target->sct_y,
557 /* Bombs that miss have to land somewhere! */
558 dam = pln_damage(&plp->plane, target->sct_x, target->sct_y,
560 collateral_damage(target->sct_x, target->sct_y, dam);
563 if (dam <= 0) /* dam == 0 if only nukes were delivered */
565 if (mcp->m_flags & M_SUB)
566 nreport(player->cnum, N_SUB_BOMB, ship.shp_own, 1);
568 nreport(player->cnum, N_SHP_BOMB, ship.shp_own, 1);
570 wu(0, ship.shp_own, "%s bombs did %d damage to %s at %s\n",
571 cname(player->cnum), dam,
573 xyas(target->sct_x, target->sct_y, ship.shp_own));
576 check_retreat_and_do_shipdamage(&ship, dam);
577 if (ship.shp_rflags & RET_BOMBED)
578 if (((ship.shp_rflags & RET_INJURED) == 0) || !dam)
579 retreat_ship(&ship, 'b');
580 putship(ship.shp_uid, &ship);
581 getship(ship.shp_uid, &ship);
583 pr("%s at %s sunk!\n",
585 xyas(target->sct_x, target->sct_y, player->cnum));
587 collateral_damage(target->sct_x, target->sct_y, dam / 2);
591 free_shiplist(&head);
595 plane_bomb(struct emp_qelem *list, struct sctstr *target)
602 struct emp_qelem *qp;
612 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
613 plp = (struct plist *)qp;
614 if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
616 nplanes = planesatxy(target->sct_x, target->sct_y, 0, 0);
618 pr("%s could not find any planes!\n", prplane(&plp->plane));
621 (void)sprintf(prompt, "%s, %d bombs. Target ('~' to skip)? ",
622 prplane(&plp->plane), plp->bombs);
625 while (planeno < 0 && !player->aborted && !ignore) {
626 if ((q = getstring(prompt, buf)) == 0 || *q == 0)
633 planesatxy(target->sct_x, target->sct_y, 0, 0);
641 if (getplane(n, &plane) &&
642 plane.pln_x == target->sct_x &&
643 plane.pln_y == target->sct_y &&
644 !(plane.pln_flags & PLN_LAUNCHED))
647 pr("Plane #%d not spotted\n", n);
652 if (plp->plane.pln_nuketype != -1)
655 hitchance = pln_hitchance(&plp->plane, 0, EF_PLANE);
656 pr("%d%% hitchance...", hitchance);
658 if (roll(100) <= hitchance) {
659 /* pinbombing is more accurate than normal bombing */
660 dam = 2 * pln_damage(&plp->plane, target->sct_x, target->sct_y,
664 /* Bombs that miss have to land somewhere! */
665 dam = pln_damage(&plp->plane, target->sct_x, target->sct_y,
667 collateral_damage(target->sct_x, target->sct_y, dam);
670 if (dam <= 0) /* dam == 0 if only nukes were delivered */
675 if (dam > plane.pln_effic)
678 plane.pln_effic -= dam;
679 plane.pln_mobil = (dam * plane.pln_mobil / 100.0);
680 if (own == player->cnum) {
681 pr("%s reports %d%% damage\n", prplane(&plane), dam);
685 "%s pinpoint bombing raid did %d%% damage to %s\n",
686 cname(player->cnum), dam, prplane(&plane));
688 nreport(player->cnum, N_DOWN_PLANE, own, 1);
690 wu(0, own, "%s bombs did %d%% damage to %s at %s\n",
691 cname(player->cnum), dam, prplane(&plane),
692 xyas(target->sct_x, target->sct_y, own));
693 putplane(plane.pln_uid, &plane);
694 collateral_damage(target->sct_x, target->sct_y, dam);
701 land_bomb(struct emp_qelem *list, struct sctstr *target)
710 struct emp_qelem *qp;
712 int ignore, aaf, flak, hitchance;
717 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
718 plp = (struct plist *)qp;
719 if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
721 nunits = unitsatxy(target->sct_x, target->sct_y, 0, 0);
723 pr("%s could not find any units!\n", prplane(&plp->plane));
726 (void)sprintf(prompt, "%s, %d bombs. Target ('~' to skip)? ",
727 prplane(&plp->plane), plp->bombs);
730 while (unitno < 0 && !player->aborted && !ignore) {
731 if ((q = getstring(prompt, buf)) == 0 || *q == 0)
738 unitsatxy(target->sct_x, target->sct_y, 0, 0);
746 if (getland(n, &land) && land.lnd_own &&
747 land.lnd_x == target->sct_x && land.lnd_y == target->sct_y)
750 pr("Unit #%d not spotted\n", n);
755 aaf = lnd_aaf(&land);
756 flak = (int)(techfact(land.lnd_tech, aaf) * 3.0);
759 "Flak! Firing flak guns from unit %s (aa rating %d)\n",
761 if (pinflak_planedamage(&plp->plane, plp->pcp, land.lnd_own, flak))
766 if (plp->plane.pln_nuketype != -1)
769 hitchance = pln_hitchance(&plp->plane,
770 lnd_hardtarget(&land), EF_LAND);
771 pr("%d%% hitchance...", hitchance);
773 if (roll(100) <= hitchance) {
774 dam = 2 * pln_damage(&plp->plane, target->sct_x, target->sct_y,
778 /* Bombs that miss have to land somewhere! */
779 dam = pln_damage(&plp->plane, target->sct_x, target->sct_y,
781 collateral_damage(target->sct_x, target->sct_y, dam);
784 if (dam <= 0) /* dam == 0 if only nukes were delivered */
789 mpr(own, "%s pinpoint bombing raid did %d damage to %s\n",
790 cname(player->cnum), dam, prland(&land));
791 check_retreat_and_do_landdamage(&land, dam);
793 if (land.lnd_rflags & RET_BOMBED)
794 if (((land.lnd_rflags & RET_INJURED) == 0) || !dam)
795 retreat_land(&land, 'b');
796 nreport(player->cnum, N_UNIT_BOMB, own, 1);
797 putland(land.lnd_uid, &land);
798 collateral_damage(target->sct_x, target->sct_y, dam);
805 strat_bomb(struct emp_qelem *list, struct sctstr *target)
809 struct emp_qelem *qp;
813 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
814 plp = (struct plist *)qp;
815 if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
817 if (plp->bombs || plp->plane.pln_nuketype != -1)
818 dam += pln_damage(&plp->plane, target->sct_x, target->sct_y,
821 if (dam <= 0) /* dam == 0 if only nukes were delivered */
823 getsect(target->sct_x, target->sct_y, §);
826 wu(0, target->sct_own, "%s bombing raid did %d damage in %s\n",
827 cname(player->cnum), PERCENT_DAMAGE(dam),
828 xyas(target->sct_x, target->sct_y, target->sct_own));
830 sectdamage(target, dam);
832 pr("did %d damage in %s\n", PERCENT_DAMAGE(dam),
833 xyas(target->sct_x, target->sct_y, player->cnum));
838 pinflak_planedamage(struct plnstr *pp, struct plchrstr *pcp, natid from,
847 dam = ac_flak_dam(flak, pln_def(pp), pcp->pl_flags);
849 plane_owner = pp->pln_own;
853 memset(dmess, 0, sizeof(dmess));
857 if (eff < PLANE_MINEFF) {
858 sprintf(dmess, " -- shot down");
860 } else if (chance((100 - eff) / 100.0)) {
861 sprintf(dmess, " -- aborted with %d%% damage", 100 - eff);
864 PR(plane_owner, " Flak! %s %s takes %d%s.\n",
865 cname(pp->pln_own), prplane(pp), dam, dmess);
870 nreport(from, N_DOWN_PLANE, pp->pln_own, 1);
872 putplane(pp->pln_uid, pp);