]> git.pond.sub.org Git - empserver/blobdiff - src/lib/subs/snxtitem.c
New macro ARRAY_SIZE()
[empserver] / src / lib / subs / snxtitem.c
index 030fc6814fbfe3acd72b0fca5f65d229172b841d..6cdd02b8275f1335b0e07ea065ba6d38969b9582 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2010, Dave Pare, Jeff Bailey, Thomas Ruschak,
- *                           Ken Stevens, Steve McClure
+ *  Copyright (C) 1986-2020, 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,
@@ -14,8 +14,7 @@
  *  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/>.
  *
  *  ---
  *
@@ -29,7 +28,7 @@
  *
  *  Known contributors to this file:
  *     Dave Pare, 1989
- *     Markus Armbruster, 2009
+ *     Markus Armbruster, 2009-2020
  */
 
 #include <config.h>
@@ -39,7 +38,6 @@
 #include "player.h"
 #include "xy.h"
 #include "nsc.h"
-#include "file.h"
 #include "prototypes.h"
 #include "unit.h"
 
@@ -74,9 +72,9 @@ snxtitem(struct nstr_item *np, int type, char *str, char *prompt)
        str = getstring(prompt, buf);
        if (!str)
            return 0;
-    }
+    } else
+       make_stale_if_command_arg(str);
     if (*str == 0) {
-       /* empty string passed by player */
        return 0;
     }
     if (type == EF_NATION && isalpha(*str)) {
@@ -126,12 +124,7 @@ snxtitem(struct nstr_item *np, int type, char *str, char *prompt)
     default:
        return 0;
     }
-    if (!player->condarg)
-       return 1;
-    n = nstr_comp(np->cond, sizeof(np->cond) / sizeof(*np->cond), type,
-                 player->condarg);
-    np->ncond = n >= 0 ? n : 0;
-    return n >= 0;
+    return snxtitem_use_condarg(np);
 }
 
 void
@@ -224,8 +217,8 @@ snxtitem_list(struct nstr_item *np, int type, int *list, int len)
 }
 
 /*
- * Initialize NP to iterate over the items of type TYPE in a carrier.
- * The carrier has file type CARRIER_TYPE and uid CARRIER_UID.
+ * Initialize @np to iterate over the items of type @type in a carrier.
+ * The carrier has file type @carrier_type and UID @carrier_uid.
  * Note: you can take an item gotten with nxtitem() off its carrier
  * without disturbing the iterator.  Whether the iterator will pick up
  * stuff you load onto the carrier during iteration is unspecified.
@@ -240,3 +233,18 @@ snxtitem_cargo(struct nstr_item *np, int type,
     np->sel = NS_CARGO;
     np->next = unit_cargo_first(carrier_type, carrier_uid, type);
 }
+
+int
+snxtitem_use_condarg(struct nstr_item *np)
+{
+    int n;
+
+    if (!player->condarg)
+       return 1;
+    n = nstr_comp(np->cond, ARRAY_SIZE(np->cond),
+                 np->type, player->condarg);
+    if (n < 0)
+       return 0;
+    np->ncond = n;
+    return 1;
+}