]> git.pond.sub.org Git - empserver/blobdiff - src/lib/subs/snxtitem.c
Update copyright notice
[empserver] / src / lib / subs / snxtitem.c
index 1e799fa33b3e69de52aef728a19daa9edfe508c6..858e45cb4b56299792e4ef4f38b42aaef976f3fe 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2008, 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/>.
  *
  *  ---
  *
  *  ---
  *
  *  snxtitem.c: Arrange item selection using one of many criteria.
- * 
+ *
  *  Known contributors to this file:
  *     Dave Pare, 1989
- *     Markus Armbruster, 2008
+ *     Markus Armbruster, 2009-2011
  */
 
 #include <config.h>
@@ -39,8 +38,8 @@
 #include "player.h"
 #include "xy.h"
 #include "nsc.h"
-#include "file.h"
 #include "prototypes.h"
+#include "unit.h"
 
 /*
  * setup the nstr structure for sector selection.
@@ -65,17 +64,17 @@ snxtitem(struct nstr_item *np, int type, char *str, char *prompt)
 
     np->type = EF_BAD;
     np->sel = NS_UNDEF;
-    if (str == 0) {
+    if (!str) {
        if (!prompt) {
            sprintf(promptbuf, "%s(s)? ", ef_nameof(type));
            prompt = promptbuf;
        }
        str = getstring(prompt, buf);
-       if (str == 0)
+       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)) {
@@ -125,12 +124,7 @@ snxtitem(struct nstr_item *np, int type, char *str, char *prompt)
     default:
        return 0;
     }
-    if (player->condarg == 0)
-       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
@@ -146,8 +140,7 @@ snxtitem_area(struct nstr_item *np, int type, struct range *range)
 }
 
 void
-snxtitem_dist(struct nstr_item *np, int type, int cx, int cy,
-             int dist)
+snxtitem_dist(struct nstr_item *np, int type, int cx, int cy, int dist)
 {
     memset(np, 0, sizeof(*np));
     xydist_range(cx, cy, dist, &np->range);
@@ -222,3 +215,36 @@ snxtitem_list(struct nstr_item *np, int type, int *list, int len)
     np->size = len;
     return 1;
 }
+
+/*
+ * 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.
+ */
+void
+snxtitem_cargo(struct nstr_item *np, int type,
+              int carrier_type, int carrier_uid)
+{
+    memset(np, 0, sizeof(*np));
+    np->cur = -1;
+    np->type = 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, sizeof(np->cond) / sizeof(*np->cond),
+                 np->type, player->condarg);
+    if (n < 0)
+       return 0;
+    np->ncond = n;
+    return 1;
+}