]> git.pond.sub.org Git - empserver/blobdiff - src/lib/subs/detonate.c
Simplify detonate() for nukes falling on water
[empserver] / src / lib / subs / detonate.c
index 3265410d1643adb7d0f7480fdfcafbb34426dc84..d695917f86c2c30de8a187e0686f6cdf341ddbcd 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2010, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
  *
  *  ---
  *
- *  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.
  *
  *  ---
  *
  *  detonate.c: Detonate a nuclear device in a sector.
- * 
+ *
  *  Known contributors to this file:
  *     Steve McClure, 1998-2000
+ *     Markus Armbruster, 2004-2009
  */
 
-#include "misc.h"
-#include "player.h"
-#include "xy.h"
-#include "nat.h"
+#include <config.h>
+
 #include "file.h"
-#include "sect.h"
-#include "nuke.h"
-#include "ship.h"
 #include "land.h"
+#include "lost.h"
+#include "misc.h"
+#include "nat.h"
 #include "news.h"
-#include "plane.h"
 #include "nsc.h"
-#include "var.h"
+#include "nuke.h"
 #include "optlist.h"
+#include "plane.h"
+#include "player.h"
 #include "prototypes.h"
+#include "sect.h"
+#include "ship.h"
+#include "xy.h"
 
 static void kaboom(int x, int y, int rad, natid cn);
 
 int
-detonate(struct plnstr *pp, int x, int y)
+detonate(struct nukstr *np, coord x, coord y, int airburst)
 {
-    int nuketype = pp->pln_nuketype;
-    natid bombown = pp->pln_own;
-    int airburst = (pp->pln_flags & PLN_AIRBURST);
+    int nuketype = np->nuk_type;
+    natid bombown = np->nuk_own;
     struct nchrstr *ncp;
     struct plnstr plane;
     struct sctstr sect;
     struct shpstr ship;
     struct lndstr land;
     struct nukstr nuke;
-    s_char *bp;
-    s_char buf[128];
-    s_char buf2[128];
+    char *bp;
+    char buf[128];
+    char buf2[128];
     natid own;
     int type;
     int damage;
@@ -71,51 +73,44 @@ detonate(struct plnstr *pp, int x, int y)
     int rad;
     struct nstr_sect ns;
     struct nstr_item ni;
-    int issea = 0;
-    int retval;
 
-    pp->pln_nuketype = -1;
+    mpr(bombown, "Releasing RV's for %s detonation...\n",
+       airburst ? "airburst" : "groundburst");
+
     getsect(x, y, &sect);
-    if (sect.sct_type == SCT_WATER)
-       issea = 1;
     ncp = &nchr[nuketype];
     kaboom(x, y, ncp->n_blast, bombown);
     rad = ncp->n_blast;
     if (!airburst)
        rad = rad * 2 / 3;
+    if (sect.sct_type == SCT_WATER)
+       rad = 0;     /* Nukes falling on water affect only 1 sector */
+    np->nuk_effic = 0;
+    putnuke(np->nuk_uid, np);
+
     snxtsct_dist(&ns, x, y, rad);
     while (nxtsct(&ns, &sect)) {
-       /* Nukes falling on water affect only 1 sector */
-       if ((sect.sct_x != x) && issea)
-           continue;
-       if ((sect.sct_y != y) && issea)
-           continue;
        own = sect.sct_own;
        type = sect.sct_type;
        if ((damage = nukedamage(ncp, ns.curdist, airburst)) <= 0)
            continue;
        if (type == SCT_SANCT) {
            mpr(bombown, "bounced off %s\n", xyas(ns.x, ns.y, bombown));
-           if (own != 0)
-               mpr(own, "%s nuclear device bounced off %s\n",
-                   cname(bombown), xyas(ns.x, ns.y, bombown));
+           mpr(own, "%s nuclear device bounced off %s\n",
+               cname(bombown), xyas(ns.x, ns.y, bombown));
            nreport(bombown, N_NUKE, own, 1);
            continue;
        }
-       if (opt_FALLOUT)
-           fallout = sect.sct_fallout;
-       sect_damage(&sect, damage, 0);
-       if (sect.sct_x == x && sect.sct_y == y)
-           retval = damage;
+       sect_damage(&sect, damage);
        if (opt_FALLOUT) {
-           if (opt_NEUTRON && (ncp->n_flags & N_NEUT))
+           fallout = sect.sct_fallout;
+           if (ncp->n_flags & N_NEUT)
                fallout += damage * 30;
            else
                fallout += damage * 3;
-           sect.sct_fallout = fallout;
+           sect.sct_fallout = MIN(fallout, FALLOUT_MAX);
        }
        if (damage > 100) {
-           makelost(EF_SECTOR, sect.sct_own, 0, sect.sct_x, sect.sct_y);
            sect.sct_oldown = 0;
            sect.sct_own = 0;
            if (type == SCT_WATER || type == SCT_BSPAN ||
@@ -134,8 +129,6 @@ detonate(struct plnstr *pp, int x, int y)
            sprintf(buf, "did %d%%%% damage in %%s\n", damage);
            bp = buf;
        }
-       if ((type == SCT_CAPIT || type == SCT_MOUNT) && damage >= 100)
-           caploss(&sect, own, "\n%s lost its capital!\n\n");
        (void)putsect(&sect);
        if (type != SCT_WATER)
            nreport(bombown, N_NUKE, own, 1);
@@ -145,16 +138,12 @@ detonate(struct plnstr *pp, int x, int y)
            mpr(own, "%s nuclear device %s\n", cname(bombown), buf2);
        }
     }
+
     snxtitem_dist(&ni, EF_PLANE, x, y, rad);
-    while (nxtitem(&ni, (caddr_t)&plane)) {
-       /* Nukes falling on water affect only 1 sector */
-       if ((plane.pln_x != x) && issea)
-           continue;
-       if ((plane.pln_y != y) && issea)
-           continue;
+    while (nxtitem(&ni, &plane)) {
        if ((own = plane.pln_own) == 0)
            continue;
-       if ((plane.pln_flags & PLN_LAUNCHED) && (airburst != 2))
+       if (plane.pln_flags & PLN_LAUNCHED)
            continue;
        damage = nukedamage(ncp, ni.curdist, airburst) - plane.pln_harden;
        if (damage <= 0)
@@ -188,21 +177,15 @@ detonate(struct plnstr *pp, int x, int y)
                prplane(&plane),
                xyas(plane.pln_x, plane.pln_y, own), damage);
        } else {
-           if (own != 0)
-               mpr(own,
-                   "%s nuclear device did %d%% damage to %s at %s\n",
-                   cname(bombown), damage,
-                   prplane(&plane), xyas(plane.pln_x, plane.pln_y, own));
+           mpr(own, "%s nuclear device did %d%% damage to %s at %s\n",
+               cname(bombown), damage,
+               prplane(&plane), xyas(plane.pln_x, plane.pln_y, own));
        }
        putplane(ni.cur, &plane);
     }
+
     snxtitem_dist(&ni, EF_LAND, x, y, rad);
-    while (nxtitem(&ni, (caddr_t)&land)) {
-       /* Nukes falling on water affect only 1 sector */
-       if ((land.lnd_x != x) && issea)
-           continue;
-       if ((land.lnd_y != y) && issea)
-           continue;
+    while (nxtitem(&ni, &land)) {
        if ((own = land.lnd_own) == 0)
            continue;
        if ((damage = nukedamage(ncp, ni.curdist, airburst)) <= 0)
@@ -236,20 +219,15 @@ detonate(struct plnstr *pp, int x, int y)
            mpr(bombown, "%s at %s reports %d%% damage\n",
                prland(&land), xyas(land.lnd_x, land.lnd_y, own), damage);
        } else {
-           if (own != 0)
-               mpr(own, "%s nuclear device did %d%% damage to %s at %s\n",
-                   cname(bombown), damage,
-                   prland(&land), xyas(land.lnd_x, land.lnd_y, own));
+           mpr(own, "%s nuclear device did %d%% damage to %s at %s\n",
+               cname(bombown), damage,
+               prland(&land), xyas(land.lnd_x, land.lnd_y, own));
        }
        putland(land.lnd_uid, &land);
     }
+
     snxtitem_dist(&ni, EF_SHIP, x, y, rad);
-    while (nxtitem(&ni, (caddr_t)&ship)) {
-       /* Nukes falling on water affect only 1 sector */
-       if ((ship.shp_x != x) && issea)
-           continue;
-       if ((ship.shp_y != y) && issea)
-           continue;
+    while (nxtitem(&ni, &ship)) {
        if ((own = ship.shp_own) == 0)
            continue;
        if ((damage = nukedamage(ncp, ni.curdist, airburst)) <= 0)
@@ -277,40 +255,33 @@ detonate(struct plnstr *pp, int x, int y)
            mpr(bombown, "%s at %s reports %d%% damage\n",
                prship(&ship), xyas(ship.shp_x, ship.shp_y, own), damage);
        } else {
-           if (own != 0)
-               mpr(own, "%s nuclear device did %d%% damage to %s at %s\n",
-                   cname(bombown), damage, prship(&ship),
-                   xyas(ship.shp_x, ship.shp_y, own));
+           mpr(own, "%s nuclear device did %d%% damage to %s at %s\n",
+               cname(bombown), damage, prship(&ship),
+               xyas(ship.shp_x, ship.shp_y, own));
        }
        putship(ship.shp_uid, &ship);
     }
+
     snxtitem_dist(&ni, EF_NUKE, x, y, rad);
-    while (nxtitem(&ni, (caddr_t)&nuke)) {
-       /* Nukes falling on water affect only 1 sector */
-       if ((nuke.nuk_x != x) && issea)
-           continue;
-       if ((nuke.nuk_y != y) && issea)
-           continue;
+    while (nxtitem(&ni, &nuke)) {
        if ((own = nuke.nuk_own) == 0)
            continue;
        if ((damage = nukedamage(ncp, ni.curdist, airburst)) <= 0)
            continue;
        if (roll(100) >= damage)
            continue;
-       makelost(EF_NUKE, nuke.nuk_own, nuke.nuk_uid, nuke.nuk_x,
-                nuke.nuk_y);
-       nuke.nuk_own = 0;
+       nuke.nuk_effic = 0;
        if (own == bombown) {
-           mpr(bombown, "nuclear stockpile #%d at %s destroyed\n",
-               ni.cur, xyas(nuke.nuk_x, nuke.nuk_y, own));
+           mpr(bombown, "%s at %s destroyed\n",
+               prnuke(&nuke), xyas(nuke.nuk_x, nuke.nuk_y, own));
        } else {
-           if (own != 0)
-               mpr(own, "nuclear stockpile #%d at %s destroyed\n",
-                   ni.cur, xyas(nuke.nuk_x, nuke.nuk_y, own));
+           mpr(own, "%s at %s destroyed\n",
+               prnuke(&nuke), xyas(nuke.nuk_x, nuke.nuk_y, own));
        }
        putnuke(ni.cur, &nuke);
     }
-    return retval;
+
+    return nukedamage(ncp, 0, airburst);
 }
 
 
@@ -320,8 +291,8 @@ detonate(struct plnstr *pp, int x, int y)
 static void
 kaboom(int x, int y, int rad, natid cn)
 {
-    mpr(cn, "\n\nK A B ");
-    while (rad-- > 0)
+    mpr(cn, "\n\nK A B O O ");
+    while (rad-- > 1)
        mpr(cn, "O O ");
     mpr(cn, "M ! in %s\n\n", xyas(x, y, cn));
 }