]> git.pond.sub.org Git - empserver/blobdiff - src/lib/commands/arm.c
arm: Rework "cannot carry nukes" test for robustness
[empserver] / src / lib / commands / arm.c
index 32b486890265b6c1cb663b64947f4fed1bc9c19b..74ea9485eb69487f22b8a614e740f349efbed02c 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2005, Dave Pare, Jeff Bailey, Thomas Ruschak,
- *                           Ken Stevens, Steve McClure
+ *  Copyright (C) 1986-2021, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *                Ken Stevens, Steve McClure, Markus Armbruster
  *
- *  This program is free software; you can redistribute it and/or modify
+ *  Empire is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
+ *  the Free Software Foundation, either version 3 of the License, or
  *  (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  *  ---
  *
- *  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.
  *
  *  ---
  *
  *  arm.c: Arm planes (missiles) with nuclear devices
- * 
+ *
  *  Known contributors to this file:
  *     Dave Pare, 1986
  *     Ken Stevens, 1995
  *     Steve McClure, 2000
+ *     Markus Armbruster, 2006-2021
  */
 
-#include <string.h>
-#include "misc.h"
-#include "player.h"
-#include "nuke.h"
-#include "plane.h"
-#include "xy.h"
-#include "nsc.h"
-#include "file.h"
+#include <config.h>
+
+#include <stdio.h>
 #include "commands.h"
+#include "nuke.h"
 #include "optlist.h"
+#include "plane.h"
 
 int
 arm(void)
@@ -50,108 +47,133 @@ arm(void)
     struct nchrstr *ncp;
     struct plchrstr *plc;
     struct plnstr pl;
-    struct plnstr start;       /* Used for sanity checking */
     struct nukstr nuke;
-    s_char *p;
-    int i;
-    int pno;
-    int nuketype;
-    int nukenum;
-    int len;
+    char *p;
+    int nukno;
     struct nstr_item ni;
-    s_char buf[1024];
-    int disarm = **player->argp == 'd';
-    s_char *prompt = disarm ? "Disarm plane: " : "Arm plane: ";
+    char buf[1024];
+    char prompt[128];
 
-    if (!(p = getstarg(player->argp[1], prompt, buf)) || !*p)
+    if (!snxtitem(&ni, EF_PLANE, player->argp[1], NULL))
        return RET_SYN;
-    pno = atoi(p);
-    if (pno < 0 || !getplane(pno, &pl) || pl.pln_own != player->cnum)
-       return RET_FAIL;
-    memcpy(&start, &pl, sizeof(struct plnstr));
-    plc = &plchr[(int)pl.pln_type];
-    if ((plc->pl_flags & (P_O | P_M)) == (P_O | P_M)) {
-       pr("A %s cannot carry nuclear devices!\n", plc->pl_name);
-       return RET_FAIL;
-    }
-    if (opt_MARKET) {
-       if (ontradingblock(EF_PLANE, (int *)&pl)) {
-           pr("You cannot arm/disarm an item on the trading block!\n");
-           return RET_FAIL;
-       }
-    }
-    if (pl.pln_nuketype == -1) {
-       if (disarm) {
-           pr("%s is not carrying any nuclear devices\n", prplane(&pl));
-           return RET_FAIL;
-       }
-       if ((p = getstarg(player->argp[2], "Device type: ", buf)) == 0)
-           return RET_SYN;
-       if (!check_plane_ok(&start))
+    while (nxtitem(&ni, &pl)) {
+       if (!player->owner
+           && relations_with(pl.pln_own, player->cnum) != ALLIED)
+           continue;
+       plc = &plchr[(int)pl.pln_type];
+       if (!(plc->pl_flags & (P_B | P_T | P_C))
+           || (plc->pl_flags & P_MAR)) {
+           pr("A %s cannot carry nuclear devices!\n", plc->pl_name);
            return RET_FAIL;
-       len = strlen(p);
-       for (i = 0, ncp = nchr; i < N_MAXNUKE; i++, ncp++) {
-           if (strncmp(ncp->n_name, p, len) == 0)
-               break;
        }
-       if (i >= N_MAXNUKE) {
-           pr("No such nuke type!\n");
-           return RET_SYN;
+       if (CANT_HAPPEN(pl.pln_flags & PLN_LAUNCHED))
+           continue;
+       if (opt_MARKET) {
+           if (ontradingblock(EF_PLANE, &pl)) {
+               pr("You cannot arm %s while it is on the trading block!\n",
+                  prplane(&pl));
+               return RET_FAIL;
+           }
        }
-       nuketype = i;
-       nukenum = -1;
-       snxtitem_all(&ni, EF_NUKE);
-       while (nxtitem(&ni, (s_char *)&nuke)) {
-           if (nuke.nuk_own != player->cnum)
-               continue;
-           if (nuke.nuk_x != pl.pln_x || nuke.nuk_y != pl.pln_y)
-               continue;
-           nukenum = ni.cur;
-           break;
+       if (!getnuke(nuk_on_plane(&pl), &nuke)) {
+           sprintf(prompt, "Nuclear device for %s: ", prplane(&pl));
+           p = getstarg(player->argp[2], prompt, buf);
+           if (!p || !*p)
+               return RET_SYN;
+           if (!check_plane_ok(&pl))
+               return RET_FAIL;
+           nukno = atoi(p);
+           if (!getnuke(nukno, &nuke) || !player->owner)
+               return RET_FAIL;
        }
-       if (nukenum < 0) {
-           pr("You don't own any nukes in that sector.\n");
+       ncp = &nchr[nuke.nuk_type];
+       if (pln_load(&pl) < ncp->n_weight) {
+           pr("A %s cannot carry %s devices!\n",
+              plc->pl_name, ncp->n_name);
            return RET_FAIL;
        }
-       if (nuke.nuk_types[nuketype] == 0) {
-           pr("No nukes of that type in that sector.\n");
+       p = getstarg(player->argp[3], "Airburst [n]? ", buf);
+       if (!p)
            return RET_FAIL;
-       }
-       if (pl.pln_load < ncp->n_weight) {
-           pr("A %s cannot carry %s devices!\n", plc->pl_name,
-              ncp->n_name);
+
+       if (!check_plane_ok(&pl) || !check_nuke_ok(&nuke))
            return RET_FAIL;
-       }
-       p = getstarg(player->argp[3], "Airburst [n]? ", buf);
 
-       if (!check_plane_ok(&start))
+       if (nuke.nuk_plane >= 0 && nuke.nuk_plane != pl.pln_uid) {
+           pr("%s is already armed on plane #%d!\n",
+              prnuke(&nuke), nuke.nuk_plane);
+           return RET_FAIL;
+       }
+       if (nuke.nuk_x != pl.pln_x || nuke.nuk_y != pl.pln_y) {
+           pr("%s isn't in the same sector as %s!\n",
+              prnuke(&nuke), prplane(&pl));
            return RET_FAIL;
+       }
 
-       if (p && (*p == 'y' || *p == 'Y'))
+       if (*p == 'y' || *p == 'Y')
            pl.pln_flags |= PLN_AIRBURST;
        else
            pl.pln_flags &= ~PLN_AIRBURST;
+       pl.pln_mission = 0;
 
-       pl.pln_nuketype = nuketype;
-       nuk_delete(&nuke, nuketype, 1);
-    } else if (!disarm) {
-       pr("%s already carrying a warhead.\n", prplane(&pl));
-    }
-    if (disarm) {
-       pr("%s warhead removed from %s and added to %s\n",
-          nchr[(int)pl.pln_nuketype].n_name,
-          prplane(&pl), xyas(pl.pln_x, pl.pln_y, player->cnum));
-       nuk_add(pl.pln_x, pl.pln_y, pl.pln_nuketype, 1);
-       pl.pln_nuketype = -1;
-       pl.pln_flags &= ~PLN_AIRBURST;
-    } else {
-       pr("%s armed with a %s warhead.\n", prplane(&pl),
-          nchr[(int)pl.pln_nuketype].n_name);
+       snprintf(buf, sizeof(buf), "armed on your %s in %s",
+                prplane(&pl), xyas(pl.pln_x, pl.pln_y, pl.pln_own));
+       gift(pl.pln_own, player->cnum, &nuke, buf);
+       nuke.nuk_plane = pl.pln_uid;
+       putplane(pl.pln_uid, &pl);
+       putnuke(nuke.nuk_uid, &nuke);
+       pr("%s armed with %s.\n", prplane(&pl), prnuke(&nuke));
        pr("Warhead on %s is programmed to %s\n",
           prplane(&pl),
           pl.pln_flags & PLN_AIRBURST ? "airburst" : "groundburst");
     }
 
-    putplane(pl.pln_uid, &pl);
+    return RET_OK;
+}
+
+int
+disarm(void)
+{
+    struct plnstr pl;
+    struct nukstr nuke;
+    struct nstr_item ni;
+    struct sctstr sect;
+    char buf[128];
+
+    if (!snxtitem(&ni, EF_PLANE, player->argp[1], NULL))
+       return RET_SYN;
+    while (nxtitem(&ni, &pl)) {
+       if (!player->owner)
+           continue;
+       if (!getnuke(nuk_on_plane(&pl), &nuke))
+           continue;
+       if (CANT_HAPPEN(pl.pln_flags & PLN_LAUNCHED))
+           continue;
+       if (opt_MARKET) {
+           if (ontradingblock(EF_PLANE, &pl)) {
+               pr("You cannot disarm %s while it is on the trading block!\n",
+                  prplane(&pl));
+               return RET_FAIL;
+           }
+       }
+       getsect(nuke.nuk_x, nuke.nuk_y, &sect);
+       if (!player->owner
+           && relations_with(sect.sct_own, player->cnum) != ALLIED) {
+           pr("Disarming %s in sector %s requires an alliance!\n",
+              prplane(&pl), xyas(sect.sct_x, sect.sct_y, player->cnum));
+           continue;
+       }
+       snprintf(buf, sizeof(buf), "unloaded in your %s at %s",
+                dchr[sect.sct_type].d_name,
+                xyas(sect.sct_x, sect.sct_y, sect.sct_own));
+       gift(sect.sct_own, player->cnum, &nuke, buf);
+       nuke.nuk_plane = -1;
+       pl.pln_flags &= ~PLN_AIRBURST;
+       putplane(pl.pln_uid, &pl);
+       putnuke(nuke.nuk_uid, &nuke);
+       pr("%s removed from %s and added to %s\n",
+          prnuke(&nuke), prplane(&pl),
+          xyas(pl.pln_x, pl.pln_y, player->cnum));
+    }
     return RET_OK;
 }