]> git.pond.sub.org Git - empserver/blobdiff - src/lib/commands/bomb.c
Really fix bomb not to wipe out plane updates
[empserver] / src / lib / commands / bomb.c
index 7440793ca4db4748b10ef55069625b8e47a5dacf..f959e06d77a19fd0c3ee5698649ba2993e384324 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2009, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
  *  ---
  *
  *  bomb.c: Fly bombing missions
- * 
+ *
  *  Known contributors to this file:
  *     Dave Pare, 1986
  *     Ken Stevens, 1995
  *     Steve McClure, 1998-2000
- *     Markus Armbruster, 2004-2008
+ *     Markus Armbruster, 2004-2009
  */
 
 #include <config.h>
 #include "item.h"
 #include "land.h"
 #include "news.h"
+#include "nuke.h"
 #include "optlist.h"
 #include "path.h"
 #include "plane.h"
 #include "retreat.h"
 #include "ship.h"
 
-static void pin_bomb(struct emp_qelem *list, struct sctstr *target);
-static void strat_bomb(struct emp_qelem *list, struct sctstr *target);
-static void comm_bomb(struct emp_qelem *list, struct sctstr *target);
-static void eff_bomb(struct emp_qelem *list, struct sctstr *target);
-static int pinflak_planedamage(struct plnstr *pp, struct plchrstr *pcp,
-                              natid from, int flak);
-static void plane_bomb(struct emp_qelem *list, struct sctstr *target);
-static void land_bomb(struct emp_qelem *list, struct sctstr *target);
-static void ship_bomb(struct emp_qelem *list, struct sctstr *target);
+static void pin_bomb(struct emp_qelem *, struct sctstr *);
+static void eff_bomb(struct emp_qelem *, struct sctstr *);
+static void comm_bomb(struct emp_qelem *, struct sctstr *);
+static void ship_bomb(struct emp_qelem *, struct sctstr *);
+static void plane_bomb(struct emp_qelem *, struct sctstr *);
+static void land_bomb(struct emp_qelem *, struct sctstr *);
+static void strat_bomb(struct emp_qelem *, struct sctstr *);
+static int changed_plane_aborts(struct plist *);
+static int pinflak_planedamage(struct plnstr *, struct plchrstr *,
+                              natid, int);
 
 static i_type bombcomm[] = {
     I_CIVIL,
@@ -80,7 +82,6 @@ int
 bomb(void)
 {
     char *p;
-    int mission_flags;
     coord tx, ty;
     coord ax, ay;
     int ap_to_target;
@@ -94,6 +95,8 @@ bomb(void)
     int wantflags;
     struct sctstr ap_sect;
     char mission;
+    struct plist *plp;
+    struct emp_qelem *qp, *next;
     int rel;
     struct natstr *natp;
     char buf[1024];
@@ -127,7 +130,6 @@ bomb(void)
     /*
      * select planes within range
      */
-    mission_flags = 0;
     pln_sel(&ni_bomb, &bomb_list, &ap_sect, ap_to_target,
            2, wantflags, P_M | P_O);
     pln_sel(&ni_esc, &esc_list, &ap_sect, ap_to_target,
@@ -135,18 +137,13 @@ bomb(void)
     /*
      * now arm and equip the bombers, transports, whatever.
      */
-    mission_flags |= P_X;      /* stealth (shhh) */
-    mission_flags |= P_H;      /* gets turned off if not all choppers */
-    mission_flags = pln_arm(&bomb_list, 2 * ap_to_target, mission,
-                           ip, 0, mission_flags);
+    pln_arm(&bomb_list, 2 * ap_to_target, mission, ip, 0);
     if (QEMPTY(&bomb_list)) {
        pr("No planes could be equipped for the mission.\n");
        return RET_FAIL;
     }
-    mission_flags = pln_arm(&esc_list, 2 * ap_to_target, mission,
-                           ip, P_F | P_ESC, mission_flags);
-    ac_encounter(&bomb_list, &esc_list, ax, ay,
-                flightpath, mission_flags, 0);
+    pln_arm(&esc_list, 2 * ap_to_target, mission, ip, P_F | P_ESC);
+    ac_encounter(&bomb_list, &esc_list, ax, ay, flightpath, 0);
     if (QEMPTY(&bomb_list)) {
        pr("No planes got through fighter defenses\n");
     } else if (target.sct_type == SCT_SANCT) {
@@ -155,6 +152,11 @@ bomb(void)
        switch (mission) {
        case 'p':
            pin_bomb(&bomb_list, &target);
+           for (qp = bomb_list.q_forw; qp != &bomb_list; qp = next) {
+               next = qp->q_forw;
+               plp = (struct plist *)qp;
+               changed_plane_aborts(plp);
+           }
            break;
        case 's':
            if (opt_SLOW_WAR) {
@@ -192,7 +194,6 @@ pin_bomb(struct emp_qelem *list, struct sctstr *target)
     int type;
     int bad;
     char *p;
-    struct plist *plp;
     int nsubs;
     int nunits;
     struct natstr *natp;
@@ -206,9 +207,8 @@ pin_bomb(struct emp_qelem *list, struct sctstr *target)
     pr("Target sector is a %s constructed %s\n",
        effadv((int)target->sct_effic), dcp->d_name);
     nsubs = 0;
-    plp = (struct plist *)list->q_forw;
     nships = shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
-    if (plp->pcp->pl_flags & P_A) {
+    if (pln_caps(list) & P_A) {
        nsubs = shipsatxy(target->sct_x, target->sct_y, M_SUB, 0, 1);
        if (nsubs > 0)
            pr("Some subs are present in the sector.\n");
@@ -267,7 +267,7 @@ pin_bomb(struct emp_qelem *list, struct sctstr *target)
        break;
     case 's':
        if (nships == 0) {
-           if (((struct plist *)list->q_forw)->pcp->pl_flags & P_A) {
+           if (pln_caps(list) & P_A) {
                if (nsubs == 0) {
                    pr("no ships there\n");
                    goto retry;
@@ -333,16 +333,19 @@ static void
 eff_bomb(struct emp_qelem *list, struct sctstr *target)
 {
     struct plist *plp;
-    struct emp_qelem *qp;
+    struct emp_qelem *qp, *next;
     struct sctstr sect;
     int oldeff, dam = 0;
     int nukedam;
 
-    for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
+    for (qp = list->q_forw; qp != list; qp = next) {
+       next = qp->q_forw;
        plp = (struct plist *)qp;
+       if (changed_plane_aborts(plp))
+           continue;
        if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
            continue;
-       if (plp->bombs || plp->plane.pln_nuketype != -1)
+       if (plp->bombs || nuk_on_plane(&plp->plane) >= 0)
            dam += pln_damage(&plp->plane, target->sct_x, target->sct_y,
                              'p', &nukedam, 1);
     }
@@ -377,7 +380,7 @@ comm_bomb(struct emp_qelem *list, struct sctstr *target)
     int i;
     int amt, before;
     struct ichrstr *ip;
-    struct emp_qelem *qp;
+    struct emp_qelem *qp, *next;
     struct sctstr sect;
     int dam = 0;
     int nukedam;
@@ -415,11 +418,14 @@ comm_bomb(struct emp_qelem *list, struct sctstr *target)
        } else
            break;
     }
-    for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
+    for (qp = list->q_forw; qp != list; qp = next) {
+       next = qp->q_forw;
        plp = (struct plist *)qp;
+       if (changed_plane_aborts(plp))
+           continue;
        if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
            continue;
-       if (plp->bombs || plp->plane.pln_nuketype != -1)
+       if (plp->bombs || nuk_on_plane(&plp->plane) >= 0)
            dam += pln_damage(&plp->plane, target->sct_x, target->sct_y,
                              'p', &nukedam, 1);
     }
@@ -453,9 +459,8 @@ ship_bomb(struct emp_qelem *list, struct sctstr *target)
     int dam;
     char *q;
     int n;
-    struct emp_qelem *qp;
+    struct emp_qelem *qp, *next;
     int shipno;
-    int ignore;
     struct shpstr ship;
     int nships = 0;
     struct shiplist *head = NULL;
@@ -465,12 +470,13 @@ ship_bomb(struct emp_qelem *list, struct sctstr *target)
     int nukedam;
     int flak;
     int gun;
-    int shell;
 
-    for (qp = list->q_forw; qp != list && !player->aborted;
-        qp = qp->q_forw) {
+    for (qp = list->q_forw; qp != list; qp = next) {
+       next = qp->q_forw;
        free_shiplist(&head);
        plp = (struct plist *)qp;
+       if (changed_plane_aborts(plp))
+           continue;
        if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
            continue;
        if (plp->pcp->pl_flags & P_A)
@@ -484,15 +490,14 @@ ship_bomb(struct emp_qelem *list, struct sctstr *target)
        }
        (void)sprintf(prompt, "%s, %d bombs.  Target ('~' to skip)? ",
                      prplane(&plp->plane), plp->bombs);
-       ignore = 0;
        shipno = -1;
-       while (shipno < 0 && !player->aborted && !ignore) {
-           if ((q = getstring(prompt, buf)) == 0 || *q == 0)
-               continue;
-           if (*q == '~') {
-               ignore = 1;
+       while (shipno < 0) {
+           if ((q = getstring(prompt, buf)) == 0)
+               goto out;
+           if (*q == 0)
                continue;
-           }
+           if (*q == '~')
+               break;
            if (*q == '?') {
                if (plp->pcp->pl_flags & P_A)
                    print_shiplist(head);
@@ -500,37 +505,27 @@ ship_bomb(struct emp_qelem *list, struct sctstr *target)
                    shipsatxy(target->sct_x, target->sct_y, 0, M_SUB, 0);
                continue;
            }
-           if (*q == 'd')
-               goto next;
-           if (!isdigit(*q))
-               continue;
            n = atoi(q);
            if (n < 0)
                continue;
-           if ((plp->pcp->pl_flags & P_A) && !on_shiplist(n, head))
-               continue;
-           if (getship(n, &ship) && ship.shp_own &&
+           if ((!(plp->pcp->pl_flags & P_A) || on_shiplist(n, head)) &&
+               getship(n, &ship) && ship.shp_own &&
                ship.shp_x == target->sct_x && ship.shp_y == target->sct_y)
                shipno = n;
+           else
+               pr("Ship #%d not spotted\n", n);
        }
        if (shipno < 0)
            continue;
        if ((plp->pcp->pl_flags & P_A) && !on_shiplist(shipno, head))
            continue;
+       if (changed_plane_aborts(plp))
+           continue;
 
-       shell = 0;
        gun = shp_usable_guns(&ship);
-       if (gun > 0) {
-           shell = ship.shp_item[I_SHELL];
-           if (shell <= 0)
-               shell = supply_commod(ship.shp_own,
-                                     ship.shp_x, ship.shp_y, I_SHELL, 1);
-       }
        mcp = &mchr[(int)ship.shp_type];
-       if (gun > 0 && shell > 0 && !(mcp->m_flags & M_SUB)) {
+       if (gun > 0 && !(mcp->m_flags & M_SUB)) {
            flak = (int)(techfact(ship.shp_tech, gun) * 2.0);
-           ship.shp_item[I_SHELL] = shell;
-           putship(ship.shp_uid, &ship);
            PR(ship.shp_own, "Flak! Firing %d guns from ship %s\n",
               flak, prship(&ship));
            if (pinflak_planedamage(&plp->plane, plp->pcp, ship.shp_own, flak))
@@ -538,7 +533,7 @@ ship_bomb(struct emp_qelem *list, struct sctstr *target)
        }
 
        dam = 0;
-       if (plp->plane.pln_nuketype != -1)
+       if (nuk_on_plane(&plp->plane) >= 0)
            hitchance = 100;
        else {
            hitchance = pln_hitchance(&plp->plane,
@@ -582,9 +577,8 @@ ship_bomb(struct emp_qelem *list, struct sctstr *target)
               xyas(target->sct_x, target->sct_y, player->cnum));
        }
        collateral_damage(target->sct_x, target->sct_y, dam / 2);
-      next:
-       ;
     }
+out:
     free_shiplist(&head);
 }
 
@@ -596,9 +590,8 @@ plane_bomb(struct emp_qelem *list, struct sctstr *target)
     int n;
     natid own;
     struct plnstr plane;
-    struct emp_qelem *qp;
+    struct emp_qelem *qp, *next;
     int planeno;
-    int ignore;
     struct plist *plp;
     char prompt[128];
     char buf[1024];
@@ -606,8 +599,11 @@ plane_bomb(struct emp_qelem *list, struct sctstr *target)
     int nukedam;
     int nplanes;
 
-    for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
+    for (qp = list->q_forw; qp != list; qp = next) {
+       next = qp->q_forw;
        plp = (struct plist *)qp;
+       if (changed_plane_aborts(plp))
+           continue;
        if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
            continue;
        nplanes = planesatxy(target->sct_x, target->sct_y, 0, 0);
@@ -618,20 +614,17 @@ plane_bomb(struct emp_qelem *list, struct sctstr *target)
        (void)sprintf(prompt, "%s, %d bombs.  Target ('~' to skip)? ",
                      prplane(&plp->plane), plp->bombs);
        planeno = -1;
-       ignore = 0;
-       while (planeno < 0 && !player->aborted && !ignore) {
-           if ((q = getstring(prompt, buf)) == 0 || *q == 0)
+       while (planeno < 0) {
+           if ((q = getstring(prompt, buf)) == 0)
+               return;
+           if (*q == 0)
                continue;
-           if (*q == '~') {
-               ignore = 1;
-               continue;
-           }
+           if (*q == '~')
+               break;
            if (*q == '?') {
                planesatxy(target->sct_x, target->sct_y, 0, 0);
                continue;
            }
-           if (*q == 'd')
-               goto next;
            n = atoi(q);
            if (n < 0)
                continue;
@@ -646,8 +639,10 @@ plane_bomb(struct emp_qelem *list, struct sctstr *target)
        }
        if (planeno < 0)
            continue;
+       if (changed_plane_aborts(plp))
+           continue;
        dam = 0;
-       if (plp->plane.pln_nuketype != -1)
+       if (nuk_on_plane(&plp->plane) >= 0)
            hitchance = 100;
        else {
            hitchance = pln_hitchance(&plp->plane, 0, EF_PLANE);
@@ -690,8 +685,6 @@ plane_bomb(struct emp_qelem *list, struct sctstr *target)
               xyas(target->sct_x, target->sct_y, own));
        putplane(plane.pln_uid, &plane);
        collateral_damage(target->sct_x, target->sct_y, dam);
-      next:
-       ;
     }
 }
 
@@ -705,15 +698,18 @@ land_bomb(struct emp_qelem *list, struct sctstr *target)
     char prompt[128];
     char buf[1024];
     struct lndstr land;
-    struct emp_qelem *qp;
+    struct emp_qelem *qp, *next;
     int unitno;
-    int ignore, aaf, flak, hitchance;
+    int aaf, flak, hitchance;
     struct plist *plp;
     int nukedam;
     int nunits;
 
-    for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
+    for (qp = list->q_forw; qp != list; qp = next) {
+       next = qp->q_forw;
        plp = (struct plist *)qp;
+       if (changed_plane_aborts(plp))
+           continue;
        if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
            continue;
        nunits = unitsatxy(target->sct_x, target->sct_y, 0, 0);
@@ -724,20 +720,17 @@ land_bomb(struct emp_qelem *list, struct sctstr *target)
        (void)sprintf(prompt, "%s, %d bombs.  Target ('~' to skip)? ",
                      prplane(&plp->plane), plp->bombs);
        unitno = -1;
-       ignore = 0;
-       while (unitno < 0 && !player->aborted && !ignore) {
-           if ((q = getstring(prompt, buf)) == 0 || *q == 0)
+       while (unitno < 0) {
+           if ((q = getstring(prompt, buf)) == 0)
+               return;
+           if (*q == 0)
                continue;
-           if (*q == '~') {
-               ignore = 1;
-               continue;
-           }
+           if (*q == '~')
+               break;
            if (*q == '?') {
                unitsatxy(target->sct_x, target->sct_y, 0, 0);
                continue;
            }
-           if (*q == 'd')
-               goto next;
            n = atoi(q);
            if (n < 0)
                continue;
@@ -750,6 +743,8 @@ land_bomb(struct emp_qelem *list, struct sctstr *target)
        }
        if (unitno < 0)
            continue;
+       if (changed_plane_aborts(plp))
+           continue;
 
        aaf = lnd_aaf(&land);
        if (aaf) {
@@ -763,7 +758,7 @@ land_bomb(struct emp_qelem *list, struct sctstr *target)
        }
 
        dam = 0;
-       if (plp->plane.pln_nuketype != -1)
+       if (nuk_on_plane(&plp->plane) >= 0)
            hitchance = 100;
        else {
            hitchance = pln_hitchance(&plp->plane,
@@ -796,8 +791,6 @@ land_bomb(struct emp_qelem *list, struct sctstr *target)
        nreport(player->cnum, N_UNIT_BOMB, own, 1);
        putland(land.lnd_uid, &land);
        collateral_damage(target->sct_x, target->sct_y, dam);
-      next:
-       ;
     }
 }
 
@@ -814,7 +807,7 @@ strat_bomb(struct emp_qelem *list, struct sctstr *target)
        plp = (struct plist *)qp;
        if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
            continue;
-       if (plp->bombs || plp->plane.pln_nuketype != -1)
+       if (plp->bombs || nuk_on_plane(&plp->plane) >= 0)
            dam += pln_damage(&plp->plane, target->sct_x, target->sct_y,
                              's', &nukedam, 1);
     }
@@ -834,6 +827,16 @@ strat_bomb(struct emp_qelem *list, struct sctstr *target)
     putsect(&sect);
 }
 
+static int
+changed_plane_aborts(struct plist *plp)
+{
+    if (check_plane_ok(&plp->plane))
+       return 0;
+    getplane(plp->plane.pln_uid, &plp->plane);
+    pln_put1(plp);
+    return 1;
+}
+
 static int
 pinflak_planedamage(struct plnstr *pp, struct plchrstr *pcp, natid from,
                    int flak)