]> git.pond.sub.org Git - empserver/blobdiff - src/lib/commands/bomb.c
Fix confused and buggy bridge splashing code
[empserver] / src / lib / commands / bomb.c
index b0da3a9505f539644af2b58be219a9885260e1f4..72e4f1f95d34fd3bb133a51899dabc021a7c6db5 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2004, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
@@ -19,9 +19,9 @@
  *
  *  ---
  *
- *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
- *  related information and legal notices. It is expected that any future
- *  projects/authors will amend these files as needed.
+ *  See files README, COPYING and CREDITS in the root of the source
+ *  tree for related information and legal notices.  It is expected
+ *  that future projects/authors will amend these files as needed.
  *
  *  ---
  *
  *     Steve McClure, 1998-2000
  */
 
+#include <config.h>
+
 #include <ctype.h>
-#include <string.h>
-#include "misc.h"
-#include "player.h"
-#include "sect.h"
-#include "ship.h"
-#include "land.h"
+#include "commands.h"
+#include "damage.h"
 #include "item.h"
-#include "plane.h"
-#include "retreat.h"
-#include "xy.h"
-#include "nsc.h"
+#include "land.h"
 #include "news.h"
-#include "file.h"
-#include "nat.h"
-#include "path.h"
 #include "optlist.h"
-#include "damage.h"
-#include "commands.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);
@@ -79,30 +73,29 @@ static i_type bombcomm[] = {
     I_UW,
     I_RAD
 };
-static int nbomb = sizeof(bombcomm) / sizeof(int);
+static int nbomb = sizeof(bombcomm) / sizeof(*bombcomm);
 
 int
 bomb(void)
 {
-    s_char *p;
+    char *p;
     int mission_flags;
     coord tx, ty;
     coord ax, ay;
     int ap_to_target;
     struct ichrstr *ip;
-    s_char flightpath[MAX_PATH_LEN];
+    char flightpath[MAX_PATH_LEN];
     struct nstr_item ni_bomb;
     struct nstr_item ni_esc;
-    coord x, y;
     struct sctstr target;
     struct emp_qelem bomb_list;
     struct emp_qelem esc_list;
     int wantflags;
     struct sctstr ap_sect;
-    s_char mission;
+    char mission;
     int rel;
     struct natstr *natp;
-    s_char buf[1024];
+    char buf[1024];
 
     wantflags = 0;
     if (!snxtitem(&ni_bomb, EF_PLANE, player->argp[1]))
@@ -110,25 +103,17 @@ bomb(void)
     if (!snxtitem(&ni_esc, EF_PLANE,
                  getstarg(player->argp[2], "escort(s)? ", buf)))
        pr("No escorts...\n");
-    if ((p =
-        getstarg(player->argp[3], "pinpoint, or strategic? ", buf)) == 0)
+    p = getstarg(player->argp[3], "pinpoint, or strategic? ", buf);
+    if (!p)
        return RET_SYN;
     mission = *p;
     if (strchr("ps", mission) == 0)
        return RET_SYN;
-    if ((p = getstarg(player->argp[4], "assembly point? ", buf)) == 0
-       || *p == 0)
+    if (!get_assembly_point(player->argp[4], &ap_sect, buf))
        return RET_SYN;
-    if (!sarg_xy(p, &x, &y) || !getsect(x, y, &ap_sect))
-       return RET_SYN;
-    if (ap_sect.sct_own && ap_sect.sct_own != player->cnum &&
-       getrel(getnatp(ap_sect.sct_own), player->cnum) != ALLIED) {
-       pr("Assembly point not owned by you or an ally!\n");
-       return RET_SYN;
-    }
-    ax = x;
-    ay = y;
-    if (getpath(flightpath, player->argp[5], ax, ay, 0, 0, 0, P_FLYING) == 0
+    ax = ap_sect.sct_x;
+    ay = ap_sect.sct_y;
+    if (getpath(flightpath, player->argp[5], ax, ay, 0, 0, P_FLYING) == 0
        || *flightpath == 0)
        return RET_SYN;
     tx = ax;
@@ -138,7 +123,7 @@ bomb(void)
     getsect(tx, ty, &target);
     ip = 0;
     ap_to_target = strlen(flightpath);
-    if (*(flightpath + strlen(flightpath) - 1) == 'h')
+    if (flightpath[ap_to_target - 1] == 'h')
        ap_to_target--;
     pr("range to target is %d\n", ap_to_target);
     /*
@@ -209,15 +194,13 @@ pin_bomb(struct emp_qelem *list, struct sctstr *target)
     int nships;
     int type;
     int bad;
-    s_char *p;
+    char *p;
     struct plist *plp;
-    struct emp_qelem *qp;
-    int bestacc;
     int nsubs;
     int nunits;
     struct natstr *natp;
     int rel;
-    s_char buf[1024];
+    char buf[1024];
     int i;
 
     bad = 0;
@@ -228,11 +211,6 @@ pin_bomb(struct emp_qelem *list, struct sctstr *target)
     nsubs = 0;
     plp = (struct plist *)list->q_forw;
     if (plp->pcp->pl_flags & P_A) {
-       bestacc = 0;
-       for (qp = list->q_forw; qp != list; qp = qp->q_forw)
-           plp = (struct plist *)qp;
-       if (plp->plane.pln_acc < bestacc)
-           bestacc = plp->plane.pln_acc;
        nships = num_shipsatxy(target->sct_x, target->sct_y, 0, 0);
        nsubs = nships - shipsatxy(target->sct_x, target->sct_y, 0, M_SUB);
        if (nsubs > 0)
@@ -370,9 +348,8 @@ eff_bomb(struct emp_qelem *list, struct sctstr *target)
        if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
            continue;
        if (plp->bombs || plp->plane.pln_nuketype != -1)
-           dam +=
-               pln_damage(&plp->plane, target->sct_x, target->sct_y, 'p',
-                          &nukedam, 1);
+           dam += pln_damage(&plp->plane, target->sct_x, target->sct_y,
+                             'p', &nukedam, 1);
     }
     if (dam <= 0)              /* dam == 0 if only nukes were delivered */
        return;
@@ -384,8 +361,6 @@ eff_bomb(struct emp_qelem *list, struct sctstr *target)
     target->sct_road = effdamage(target->sct_road, dam);
     target->sct_rail = effdamage(target->sct_rail, dam);
     target->sct_defense = effdamage(target->sct_defense, dam);
-    if (!opt_DEFENSE_INFRA)
-       target->sct_defense = target->sct_effic;
     pr("did %d%% damage to efficiency in %s\n",
        oldeff - target->sct_effic,
        xyas(target->sct_x, target->sct_y, player->cnum));
@@ -394,14 +369,7 @@ eff_bomb(struct emp_qelem *list, struct sctstr *target)
           "%s bombing raid did %d%% damage in %s\n",
           cname(player->cnum), oldeff - target->sct_effic,
           xyas(target->sct_x, target->sct_y, target->sct_own));
-    if (target->sct_effic < SCT_MINEFF) {
-       if (target->sct_type == SCT_BSPAN)
-           knockdown(target, list);
-       else if (target->sct_type == SCT_BTOWER) {
-           knockdown(target, list);
-           bridgefall(target, list);
-       }
-    }
+    bridge_damaged(target, list);
     putsect(&sect);
     collateral_damage(target->sct_x, target->sct_y, dam, list);
 }
@@ -410,7 +378,7 @@ static void
 comm_bomb(struct emp_qelem *list, struct sctstr *target)
 {
     struct plist *plp;
-    float b;
+    double b;
     int i;
     int amt, before;
     struct ichrstr *ip;
@@ -428,7 +396,7 @@ comm_bomb(struct emp_qelem *list, struct sctstr *target)
        pr("some %s\n", ip->i_name);
     }
     for (;;) {
-       ip = whatitem((s_char *)0, "commodity to bomb? ");
+       ip = whatitem(NULL, "commodity to bomb? ");
        if (player->aborted)
            return;
        if (!ip)
@@ -445,8 +413,8 @@ comm_bomb(struct emp_qelem *list, struct sctstr *target)
            for (i = 0; i < nbomb; i++) {
                if (opt_SUPER_BARS && bombcomm[i] == I_BAR)
                    continue;
-               pr(i == 0 ? "Bombable: " : ", ");
-               pr(ichr[bombcomm[i]].i_name);
+               pr("%s%s", i == 0 ? "Bombable: " : ", ",
+                  ichr[bombcomm[i]].i_name);
            }
            pr("\n");
        } else
@@ -457,18 +425,17 @@ comm_bomb(struct emp_qelem *list, struct sctstr *target)
        if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
            continue;
        if (plp->bombs || plp->plane.pln_nuketype != -1)
-           dam +=
-               pln_damage(&plp->plane, target->sct_x, target->sct_y, 'p',
-                          &nukedam, 1);
+           dam += pln_damage(&plp->plane, target->sct_x, target->sct_y,
+                             'p', &nukedam, 1);
     }
     if (dam <= 0)              /* dam == 0 if only nukes were delivered */
        return;
     getsect(target->sct_x, target->sct_y, &sect);
     target = &sect;
-    before = amt = target->sct_item[ip->i_vtype];
-    target->sct_item[ip->i_vtype] = amt = commdamage(amt, dam, ip->i_vtype);
+    before = target->sct_item[ip->i_uid];
+    target->sct_item[ip->i_uid] = amt = commdamage(before, dam, ip->i_uid);
     if (before > 0.0)
-       b = 100.0 * (1.0 - ((float)amt / (float)before));
+       b = 100.0 * (1.0 - (double)amt / (double)before);
     else
        b = 0.0;
     pr("did %.2f%% damage to %s in %s\n",
@@ -489,7 +456,7 @@ ship_bomb(struct emp_qelem *list, struct sctstr *target)
     struct plist *plp;
     struct mchrstr *mcp;
     int dam;
-    s_char *q;
+    char *q;
     int n;
     struct emp_qelem *qp;
     int shipno;
@@ -497,9 +464,8 @@ ship_bomb(struct emp_qelem *list, struct sctstr *target)
     struct shpstr ship;
     int nships = 0;
     struct shiplist *head = NULL;
-    s_char buf[1024];
-    s_char prompt[128];
-    s_char msg[128];
+    char buf[1024];
+    char prompt[128];
     int hitchance;
     int nukedam;
     int flak;
@@ -558,21 +524,20 @@ ship_bomb(struct emp_qelem *list, struct sctstr *target)
            continue;
 
        shell = gun = 0;
-       gun = min(ship.shp_item[I_GUN], ship.shp_glim);
+       gun = MIN(ship.shp_item[I_GUN], ship.shp_glim);
        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);
+               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)) {
-           flak = (int)(techfact(ship.shp_tech, (double)gun) * 2.0);
+           flak = (int)(techfact(ship.shp_tech, gun) * 2.0);
            ship.shp_item[I_SHELL] = shell;
            putship(ship.shp_uid, &ship);
-           sprintf(msg, "Flak! Firing %d guns from ship %s\n",
-                   flak, prship(&ship));
-           PR(ship.shp_own, msg);
+           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))
                continue;
        }
@@ -581,21 +546,19 @@ ship_bomb(struct emp_qelem *list, struct sctstr *target)
        if (plp->plane.pln_nuketype != -1)
            hitchance = 100;
        else {
-           hitchance =
-               pln_hitchance(&plp->plane, shp_hardtarget(&ship), EF_SHIP);
+           hitchance = pln_hitchance(&plp->plane,
+                                     shp_hardtarget(&ship), EF_SHIP);
            pr("%d%% hitchance...", hitchance);
        }
        if (roll(100) <= hitchance) {
            /* pinbombing is more accurate than normal bombing */
-           dam =
-               2 * pln_damage(&plp->plane, target->sct_x, target->sct_y,
-                              'p', &nukedam, 1);
+           dam = 2 * pln_damage(&plp->plane, target->sct_x, target->sct_y,
+                                'p', &nukedam, 1);
        } else {
            pr("splash\n");
            /* Bombs that miss have to land somewhere! */
-           dam =
-               pln_damage(&plp->plane, target->sct_x, target->sct_y, 'p',
-                          &nukedam, 0);
+           dam = pln_damage(&plp->plane, target->sct_x, target->sct_y,
+                            'p', &nukedam, 0);
            collateral_damage(target->sct_x, target->sct_y, dam, list);
            dam = 0;
        }
@@ -634,7 +597,7 @@ static void
 plane_bomb(struct emp_qelem *list, struct sctstr *target)
 {
     int dam;
-    s_char *q;
+    char *q;
     int n;
     natid own;
     struct plnstr plane;
@@ -642,8 +605,8 @@ plane_bomb(struct emp_qelem *list, struct sctstr *target)
     int planeno;
     int ignore;
     struct plist *plp;
-    s_char prompt[128];
-    s_char buf[1024];
+    char prompt[128];
+    char buf[1024];
     int hitchance;
     int nukedam;
     int nplanes;
@@ -697,15 +660,13 @@ plane_bomb(struct emp_qelem *list, struct sctstr *target)
        }
        if (roll(100) <= hitchance) {
            /* pinbombing is more accurate than normal bombing */
-           dam =
-               2 * pln_damage(&plp->plane, target->sct_x, target->sct_y,
-                              'p', &nukedam, 1);
+           dam = 2 * pln_damage(&plp->plane, target->sct_x, target->sct_y,
+                                'p', &nukedam, 1);
        } else {
            pr("thud\n");
            /* Bombs that miss have to land somewhere! */
-           dam =
-               pln_damage(&plp->plane, target->sct_x, target->sct_y, 'p',
-                          &nukedam, 0);
+           dam = pln_damage(&plp->plane, target->sct_x, target->sct_y,
+                            'p', &nukedam, 0);
            collateral_damage(target->sct_x, target->sct_y, dam, list);
            dam = 0;
        }
@@ -714,12 +675,9 @@ plane_bomb(struct emp_qelem *list, struct sctstr *target)
        if (dam > 100)
            dam = 100;
        own = plane.pln_own;
-       if (dam > plane.pln_effic) {
+       if (dam > plane.pln_effic)
            plane.pln_effic = 0;
-           makelost(EF_PLANE, plane.pln_own, plane.pln_uid, plane.pln_x,
-                    plane.pln_y);
-           plane.pln_own = 0;
-       } else
+       else
            plane.pln_effic -= dam;
        plane.pln_mobil = (dam * plane.pln_mobil / 100.0);
        if (own == player->cnum) {
@@ -736,7 +694,7 @@ plane_bomb(struct emp_qelem *list, struct sctstr *target)
               cname(player->cnum), dam, prplane(&plane),
               xyas(target->sct_x, target->sct_y, own));
        putplane(plane.pln_uid, &plane);
-       collateral_damage(plane.pln_x, plane.pln_y, dam, list);
+       collateral_damage(target->sct_x, target->sct_y, dam, list);
       next:
        ;
     }
@@ -746,12 +704,11 @@ static void
 land_bomb(struct emp_qelem *list, struct sctstr *target)
 {
     int dam;
-    s_char *q;
+    char *q;
     int n;
     natid own;
-    s_char prompt[128];
-    s_char buf[1024];
-    s_char msg[128];
+    char prompt[128];
+    char buf[1024];
     struct lndstr land;
     struct emp_qelem *qp;
     int unitno;
@@ -798,12 +755,11 @@ land_bomb(struct emp_qelem *list, struct sctstr *target)
        if (unitno < 0)
            continue;
 
-       flak = (int)(techfact(land.lnd_tech, (double)land.lnd_aaf) * 3.0);
+       flak = (int)(techfact(land.lnd_tech, land.lnd_aaf) * 3.0);
        if (flak) {
-           sprintf(msg,
-                   "Flak! Firing flak guns from unit %s (aa rating %d)\n",
-                   prland(&land), land.lnd_aaf);
-           PR(land.lnd_own, msg);
+           PR(land.lnd_own,
+              "Flak! Firing flak guns from unit %s (aa rating %d)\n",
+              prland(&land), land.lnd_aaf);
            if (pinflak_planedamage(&plp->plane, plp->pcp, land.lnd_own, flak))
                continue;
        }
@@ -812,20 +768,18 @@ land_bomb(struct emp_qelem *list, struct sctstr *target)
        if (plp->plane.pln_nuketype != -1)
            hitchance = 100;
        else {
-           hitchance =
-               pln_hitchance(&plp->plane, lnd_hardtarget(&land), EF_LAND);
+           hitchance = pln_hitchance(&plp->plane,
+                                     lnd_hardtarget(&land), EF_LAND);
            pr("%d%% hitchance...", hitchance);
        }
        if (roll(100) <= hitchance) {
-           dam =
-               2 * pln_damage(&plp->plane, target->sct_x, target->sct_y,
-                              'p', &nukedam, 1);
+           dam = 2 * pln_damage(&plp->plane, target->sct_x, target->sct_y,
+                                'p', &nukedam, 1);
        } else {
            pr("thud\n");
            /* Bombs that miss have to land somewhere! */
-           dam =
-               pln_damage(&plp->plane, target->sct_x, target->sct_y, 'p',
-                          &nukedam, 0);
+           dam = pln_damage(&plp->plane, target->sct_x, target->sct_y,
+                            'p', &nukedam, 0);
            collateral_damage(target->sct_x, target->sct_y, dam, list);
            dam = 0;
        }
@@ -835,8 +789,7 @@ land_bomb(struct emp_qelem *list, struct sctstr *target)
            dam = 100;
        own = land.lnd_own;
        if (own != 0)
-           mpr(own,
-               "%s pinpoint bombing raid did %d damage to %s\n",
+           mpr(own, "%s pinpoint bombing raid did %d damage to %s\n",
                cname(player->cnum), dam, prland(&land));
        check_retreat_and_do_landdamage(&land, dam);
 
@@ -845,7 +798,7 @@ land_bomb(struct emp_qelem *list, struct sctstr *target)
                retreat_land(&land, 'b');
        nreport(player->cnum, N_UNIT_BOMB, own, 1);
        putland(land.lnd_uid, &land);
-       collateral_damage(land.lnd_x, land.lnd_y, dam, list);
+       collateral_damage(target->sct_x, target->sct_y, dam, list);
       next:
        ;
     }
@@ -865,9 +818,8 @@ strat_bomb(struct emp_qelem *list, struct sctstr *target)
        if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
            continue;
        if (plp->bombs || plp->plane.pln_nuketype != -1)
-           dam +=
-               pln_damage(&plp->plane, target->sct_x, target->sct_y, 's',
-                          &nukedam, 1);
+           dam += pln_damage(&plp->plane, target->sct_x, target->sct_y,
+                             's', &nukedam, 1);
     }
     if (dam <= 0)              /* dam == 0 if only nukes were delivered */
        return;
@@ -890,20 +842,14 @@ pinflak_planedamage(struct plnstr *pp, struct plchrstr *pcp, natid from,
                    int flak)
 {
     int disp;
-    s_char dmess[255];
+    char dmess[255];
     int eff;
-    s_char mesg[128];
     struct shpstr ship;
     struct lndstr land;
     natid plane_owner;
     int dam;
 
-    flak -= (pp->pln_def + 1);
-    if (pcp->pl_flags & P_X)
-       flak -= 2;
-    if (pcp->pl_flags & P_H)
-       flak -= 1;
-    dam = ac_flak_dam(flak);
+    dam = ac_flak_dam(flak, pp->pln_def, pcp->pl_flags);
     disp = 0;
     plane_owner = pp->pln_own;
     eff = pp->pln_effic;
@@ -917,12 +863,11 @@ pinflak_planedamage(struct plnstr *pp, struct plchrstr *pcp, natid from,
        sprintf(dmess, " -- shot down");
        disp = 1;
     } else if (chance((100 - eff) / 100.0)) {
-       sprintf(dmess, " -- aborted with %d%%%% damage", 100 - eff);
+       sprintf(dmess, " -- aborted with %d%% damage", 100 - eff);
        disp = 2;
     }
-    sprintf(mesg, "    Flak! %s %s takes %d%s.\n",
-           cname(pp->pln_own), prplane(pp), dam, dmess);
-    PR(plane_owner, mesg);
+    PR(plane_owner, "    Flak! %s %s takes %d%s.\n",
+       cname(pp->pln_own), prplane(pp), dam, dmess);
 
     pp->pln_effic = eff;
     if (disp == 1) {
@@ -936,11 +881,8 @@ pinflak_planedamage(struct plnstr *pp, struct plchrstr *pcp, natid from,
            getland(pp->pln_land, &land);
            take_plane_off_land(pp, &land);
        }
-       makelost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x, pp->pln_y);
-       pp->pln_own = 0;
-       putplane(pp->pln_uid, pp);
-    } else
-       putplane(pp->pln_uid, pp);
+    }
+    putplane(pp->pln_uid, pp);
 
     if (disp > 0)
        return 1;