]> git.pond.sub.org Git - empserver/blobdiff - src/lib/commands/laun.c
Fix launch not to wipe out target sector updates
[empserver] / src / lib / commands / laun.c
index efdd73f9d35d8f70c2ef7b6df373ad3d93ef4896..c4553398c902cf48eccd82f463cf9929317e163e 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
  *  ---
  *
  *  laun.c: Launch missiles from land or sea
- * 
+ *
  *  Known contributors to this file:
  *     Dave Pare, 1986
  *     Ken Stevens, 1995
  *     Steve McClure, 1998-2000
- *     Markus Armbruster, 2005-2008
+ *     Markus Armbruster, 2005-2009
  */
 
 #include <config.h>
 #include "damage.h"
 #include "mission.h"
 #include "news.h"
+#include "nuke.h"
 #include "optlist.h"
 #include "path.h"
 #include "plane.h"
 #include "ship.h"
 
 static int launch_as(struct plnstr *pp);
-static int launch_missile(struct plnstr *pp, int sublaunch);
-static int launch_sat(struct plnstr *pp, int sublaunch);
+static int launch_missile(struct plnstr *pp);
+static int launch_sat(struct plnstr *pp);
+static int msl_equip(struct plnstr *, char);
 
 /*
  * laun <PLANES>
@@ -57,8 +59,6 @@ laun(void)
 {
     struct nstr_item nstr;
     struct plnstr plane;
-    struct shpstr ship;
-    int sublaunch;
     struct plchrstr *pcp;
     int retval, gone;
 
@@ -93,29 +93,23 @@ laun(void)
            }
        }
 
-       if (plane.pln_effic < 40) { 
+       if (plane.pln_effic < 40) {
            pr("%s is damaged (%d%%)\n", prplane(&plane), plane.pln_effic);
            continue;
        }
        if (!pln_airbase_ok(&plane, 1, 1))
            continue;
-       sublaunch = 0;
-       if (plane.pln_ship >= 0) {
-           getship(plane.pln_ship, &ship);
-           if (mchr[(int)ship.shp_type].m_flags & M_SUB)
-               sublaunch = 1;
-       }
        pr("%s at %s; range %d, eff %d%%\n", prplane(&plane),
           xyas(plane.pln_x, plane.pln_y, player->cnum),
           plane.pln_range, plane.pln_effic);
        if (!(pcp->pl_flags & P_O)) {
-           retval = launch_missile(&plane, sublaunch);
+           retval = launch_missile(&plane);
            gone = 1;
        } else if ((pcp->pl_flags & (P_M | P_O)) == (P_M | P_O)) {
            retval = launch_as(&plane);
            gone = 1;
        } else {                /* satellites */
-           retval = launch_sat(&plane, sublaunch);
+           retval = launch_sat(&plane);
            gone = !(plane.pln_flags & PLN_LAUNCHED);
        }
        if (retval != RET_OK)
@@ -136,82 +130,61 @@ laun(void)
 static int
 launch_as(struct plnstr *pp)
 {
-    coord sx, sy;
     char *cp, buf[1024];
     struct plnstr plane;
-    struct nstr_item ni;
-    int goodtarget;
-    int dam, nukedam;
-    natid oldown;
 
-    cp = getstarg(player->argp[2], "Target sector? ", buf);
+    cp = getstarg(player->argp[2], "Target satellite? ", buf);
     if (!check_plane_ok(pp))
        return RET_FAIL;
     if (!cp || !*cp)
        return RET_SYN;
-    if (!sarg_xy(cp, &sx, &sy)) {
-       pr("Bad sector designation!\n");
-       return RET_SYN;
-    }
-    if (mapdist(pp->pln_x, pp->pln_y, sx, sy) > pp->pln_range) {
-       pr("Range too great!\n");
+    if (!getplane(atoi(cp), &plane) || !plane.pln_own
+       || !pln_is_in_orbit(&plane)) {
+       pr("No such satellite exists!\n");
        return RET_FAIL;
+       /* Can be abused to find satellite ids.  Tolerable.  */
     }
-    goodtarget = 0;
-    snxtitem_xy(&ni, EF_PLANE, sx, sy);
-    while (!goodtarget && nxtitem(&ni, &plane)) {
-       if (!plane.pln_own)
-           continue;
-       if (!pln_is_in_orbit(&plane))
-           continue;
-       goodtarget = 1;
 
-    }
-    if (!goodtarget) {
-       pr("No satellites there!\n");
+    if (mapdist(pp->pln_x, pp->pln_y, plane.pln_x, plane.pln_y)
+       > pp->pln_range) {
+       pr("Range too great!\n");
        return RET_FAIL;
     }
-    if (msl_equip(pp, 'p') < 0) {
-       pr("%s not enough petrol or shells!\n", prplane(pp));
+    if (msl_equip(pp, 'i') < 0)
        return RET_FAIL;
-    }
-    if (msl_hit(pp, pln_def(&plane), EF_PLANE, N_SAT_KILL, N_SAT_KILL,
-               prplane(&plane), sx, sy, plane.pln_own)) {
-       dam = pln_damage(pp, sx, sy, 'p', &nukedam, 1);
-       if (CANT_HAPPEN(nukedam))
-           return RET_OK;
-       oldown = plane.pln_own;
-       planedamage(&plane, dam);
-       pr("Hit satellite for %d%% damage!\n", dam);
-       mpr(oldown, "%s anti-sat did %d%% damage to %s over %s\n",
-           cname(player->cnum), dam, prplane(&plane),
+    if (msl_launch(pp, EF_PLANE, prplane(&plane),
+                  plane.pln_x, plane.pln_y, plane.pln_own, NULL) < 0)
+       return RET_OK;
+    if (msl_hit(pp, pln_def(&plane), EF_PLANE, 0, 0, 0, plane.pln_own)) {
+       pr("Satellite shot down\n");
+       mpr(plane.pln_own, "%s anti-sat destroyed %s over %s\n",
+           cname(player->cnum), prplane(&plane),
            xyas(plane.pln_x, plane.pln_y, plane.pln_own));
+       nreport(pp->pln_own, N_SAT_KILL, plane.pln_own, 1);
+       plane.pln_effic = 0;
        putplane(plane.pln_uid, &plane);
-       if (!plane.pln_own)
-           mpr(oldown, "Satellite shot down\n");
     }
     return RET_OK;
 }
 
 /*
  * Launch missile PP.
- * If SUBLAUNCH, it's sub-launched.
  * Return RET_OK if launched (even when missile explodes),
  * else RET_SYN or RET_FAIL.
  */
 static int
-launch_missile(struct plnstr *pp, int sublaunch)
+launch_missile(struct plnstr *pp)
 {
     struct plchrstr *pcp = plchr + pp->pln_type;
     coord sx, sy;
-    int n, dam;
+    int n, dam, sublaunch;
     char *cp;
     struct mchrstr *mcp;
     struct shpstr target_ship;
     struct sctstr sect;
-    int nukedam;
     int rel;
     struct natstr *natp;
+    struct nukstr nuke;
     char buf[1024];
 
     if (pcp->pl_flags & P_MAR)
@@ -222,7 +195,7 @@ launch_missile(struct plnstr *pp, int sublaunch)
        return RET_SYN;
     if (!check_plane_ok(pp))
        return RET_FAIL;
-    if (opt_PINPOINTMISSILE && sarg_type(cp) == NS_LIST) {
+    if (sarg_type(cp) == NS_LIST) {
        if (!(pcp->pl_flags & P_MAR)) {
            pr("Missile not designed to attack ships!\n");
            return RET_FAIL;
@@ -240,26 +213,23 @@ launch_missile(struct plnstr *pp, int sublaunch)
            pr("Bad ship number!\n");
            return RET_FAIL;
        }
-    } /* not PINPOINTMISSILE for ships */
-    else if (!sarg_xy(cp, &sx, &sy)) {
+    } else if (!sarg_xy(cp, &sx, &sy)) {
        pr("Not a sector!\n");
        return RET_FAIL;
-    } else if (opt_PINPOINTMISSILE) {
+    } else {
        if (pcp->pl_flags & P_MAR) {
            pr("Missile designed to attack ships!\n");
            return RET_FAIL;
        }
     }
-    /* end PINPOINTMISSILE */
+
     if (mapdist(pp->pln_x, pp->pln_y, sx, sy) > pp->pln_range) {
        pr("Range too great; try again!\n");
        return RET_FAIL;
     }
-    if (msl_equip(pp, 'p') < 0) {
-       pr("%s not enough shells!\n", prplane(pp));
-       return RET_FAIL;
-    }
-    if (opt_PINPOINTMISSILE == 0 || !(pcp->pl_flags & P_MAR)) {
+    if (!(pcp->pl_flags & P_MAR)) {
+       if (msl_equip(pp, 's') < 0)
+           return RET_FAIL;
        getsect(sx, sy, &sect);
        if (opt_SLOW_WAR) {
            natp = getnatp(player->cnum);
@@ -273,21 +243,17 @@ launch_missile(struct plnstr *pp, int sublaunch)
                return RET_OK;
            }
        }
-       if (!msl_hit(pp, SECT_HARDTARGET, EF_SECTOR, N_SCT_MISS,
-                    N_SCT_SMISS, "sector", sx, sy, sect.sct_own)) {
-#if 0
-           /*
-            * FIXME want collateral damage on miss (which can't
-            * happen for nuclear war heads), but we get here too when
-            * launch fails or missile is intercepted
-            */
-           dam = pln_damage(pp, sect.sct_x, sect.sct_y, 's', &nukedam, 0);
-           collateral_damage(sect.sct_x, sect.sct_y, dam, 0);
-#endif
+       if (msl_launch(pp, EF_SECTOR, "sector", sx, sy, sect.sct_own,
+                      &sublaunch) < 0)
            return RET_OK;
-       }
-       dam = pln_damage(pp, sect.sct_x, sect.sct_y, 's', &nukedam, 1);
-       if (!nukedam) {
+       getsect(sx, sy, &sect);
+       if (!msl_hit(pp, SECT_HARDTARGET, EF_SECTOR,
+                    N_SCT_MISS, N_SCT_SMISS, sublaunch, sect.sct_own))
+           CANT_REACH();
+       if (getnuke(nuk_on_plane(pp), &nuke))
+           detonate(&nuke, sx, sy, pp->pln_flags & PLN_AIRBURST);
+       else {
+           dam = pln_damage(pp, 's', 1);
            pr("did %d damage in %s\n", PERCENT_DAMAGE(dam),
               xyas(sx, sy, player->cnum));
            if (sect.sct_own != 0) {
@@ -304,30 +270,29 @@ launch_missile(struct plnstr *pp, int sublaunch)
            sectdamage(&sect, dam);
            putsect(&sect);
        }
-    } /* end PINPOINTMISSILE conditional */
-    else if (opt_PINPOINTMISSILE) {    /* else */
+    } else {
+       if (msl_equip(pp, 'p') < 0)
+           return RET_FAIL;
+       if (msl_launch(pp, EF_SHIP, prship(&target_ship),
+                      target_ship.shp_x, target_ship.shp_y,
+                      target_ship.shp_own, &sublaunch) < 0)
+           return RET_OK;
+       getship(n, &target_ship);
        if (!msl_hit(pp, shp_hardtarget(&target_ship), EF_SHIP,
-                    N_SHP_MISS, N_SHP_SMISS, prship(&target_ship),
-                    target_ship.shp_x, target_ship.shp_y,
+                    N_SHP_MISS, N_SHP_SMISS, sublaunch,
                     target_ship.shp_own)) {
            pr("splash\n");
-#if 0 /* FIXME see above */
-           dam = pln_damage(pp,target_ship.shp_x,target_ship.shp_y,'p',&nukedam, 0);
-           collateral_damage(target_ship.shp_x, target_ship.shp_y, dam, 0);
-#endif
+           dam = pln_damage(pp, 'p', 0);
+           collateral_damage(target_ship.shp_x, target_ship.shp_y, dam);
            return RET_OK;
        }
-       dam = pln_damage(pp, target_ship.shp_x, target_ship.shp_y, 'p',
-                        &nukedam, 1);
-       if (!nukedam) {
-           check_retreat_and_do_shipdamage(&target_ship, dam);
-           putship(target_ship.shp_uid, &target_ship);
-       }
+       dam = pln_damage(pp, 'p', 1);
+       check_retreat_and_do_shipdamage(&target_ship, dam);
+       putship(target_ship.shp_uid, &target_ship);
        getship(target_ship.shp_uid, &target_ship);
        if (!target_ship.shp_own)
            pr("%s sunk!\n", prship(&target_ship));
     }
-    /* end PINPOINTMISSILE */
     return RET_OK;
 }
 
@@ -337,9 +302,8 @@ launch_missile(struct plnstr *pp, int sublaunch)
  * else RET_SYN or RET_FAIL.
  */
 static int
-launch_sat(struct plnstr *pp, int sublaunch)
+launch_sat(struct plnstr *pp)
 {
-    struct plchrstr *pcp = plchr + pp->pln_type;
     coord sx, sy;
     int i;
     int dist;
@@ -363,14 +327,12 @@ launch_sat(struct plnstr *pp, int sublaunch)
        return RET_FAIL;
     }
     p = getstring("Geostationary orbit? ", buf);
-    if (p == 0)
+    if (!p)
        return RET_SYN;
     if (!check_plane_ok(pp))
        return RET_FAIL;
-    if (msl_equip(pp, 'r') < 0) {
-       pr("%s not enough petrol!\n", prplane(pp));
+    if (msl_equip(pp, 'r') < 0)
        return RET_FAIL;
-    }
     pp->pln_theta = 0;
     pp->pln_flags |= PLN_SYNCHRONOUS;
     if (*p == 0 || *p == 'n')
@@ -384,22 +346,32 @@ launch_sat(struct plnstr *pp, int sublaunch)
     i = pp->pln_tech + pp->pln_effic;
     if (chance(1.0 - (i / (i + 50.0)))) {
        dir = (random() % 6) + 1;
-       sx += diroff[dir][0];
-       sy += diroff[dir][1];
+       sx = xnorm(sx + diroff[dir][0]);
+       sy = ynorm(sy + diroff[dir][1]);
        pr("Your trajectory was a little off.\n");
     }
     nreport(player->cnum, N_LAUNCH, 0, 1);
-    pr("%s positioned over %s", prplane(pp), xyas(sx, sy, player->cnum));
-    if (msl_intercept(sx, sy, pp->pln_own, pcp->pl_def, sublaunch, P_O, 0)) {
+    if (msl_asat_intercept(pp, sx, sy))
        return RET_OK;
-    }
     pp->pln_x = sx;
     pp->pln_y = sy;
     CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED);
     pp->pln_flags |= PLN_LAUNCHED;
     pp->pln_mobil = pp->pln_mobil > dist ? pp->pln_mobil - dist : 0;
     putplane(pp->pln_uid, pp);
-    pr(", will be ready for use in %d time units\n",
+    pr("%s positioned over %s, will be ready for use in %d time units\n",
+       prplane(pp), xyas(sx, sy, player->cnum),
        plane_mob_max - pp->pln_mobil);
     return RET_OK;
 }
+
+static int
+msl_equip(struct plnstr *pp, char mission)
+{
+    struct plist pl;
+
+    memset(&pl, 0, sizeof(struct plist));
+    pl.pcp = plchr + pp->pln_type;
+    pl.plane = *pp;
+    return pln_equip(&pl, NULL, mission);
+}