]> git.pond.sub.org Git - empserver/blobdiff - src/lib/subs/snxtitem.c
Update copyright notice
[empserver] / src / lib / subs / snxtitem.c
index c5bea17e666e59921b3f7fa50de01a9a41c36b6e..88452951276815329d4c4dd9ecd339096f05136d 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-2014, 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>
@@ -41,6 +40,7 @@
 #include "nsc.h"
 #include "file.h"
 #include "prototypes.h"
+#include "unit.h"
 
 /*
  * setup the nstr structure for sector selection.
@@ -65,17 +65,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,13 +125,7 @@ snxtitem(struct nstr_item *np, int type, char *str, char *prompt)
     default:
        return 0;
     }
-    np->flags = flags;
-    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
@@ -143,34 +137,21 @@ snxtitem_area(struct nstr_item *np, int type, struct range *range)
     np->sel = NS_AREA;
     np->index = -1;
     np->range = *range;
-    np->read = ef_read;
-    np->flags = ef_flags(type);
     xysize_range(&np->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)
 {
-    struct range range;
-
     memset(np, 0, sizeof(*np));
-    xydist_range(cx, cy, dist, &range);
+    xydist_range(cx, cy, dist, &np->range);
     np->cur = -1;
     np->type = type;
     np->sel = NS_DIST;
     np->cx = cx;
     np->cy = cy;
     np->index = -1;
-    np->range = range;
     np->dist = dist;
-    np->read = ef_read;
-    np->flags = ef_flags(type);
-#if 0
-    /* This is no longer proper. */
-    /* It did the wrong thing for small, hitech worlds. */
-    xysize_range(&np->range);
-#endif
 }
 
 void
@@ -184,8 +165,6 @@ snxtitem_xy(struct nstr_item *np, int type, coord x, coord y)
     np->cy = ynorm(y);
     np->index = -1;
     np->dist = 0;
-    np->read = ef_read;
-    np->flags = ef_flags(type);
 }
 
 void
@@ -196,8 +175,6 @@ snxtitem_all(struct nstr_item *np, int type)
     np->sel = NS_ALL;
     np->type = type;
     np->index = -1;
-    np->read = ef_read;
-    np->flags = ef_flags(type);
     xysize_range(&np->range);
 }
 
@@ -212,8 +189,6 @@ snxtitem_group(struct nstr_item *np, int type, char group)
     np->group = group;
     np->type = type;
     np->index = -1;
-    np->read = ef_read;
-    np->flags = ef_flags(type);
     xysize_range(&np->range);
 }
 
@@ -234,8 +209,6 @@ snxtitem_list(struct nstr_item *np, int type, int *list, int len)
     np->type = type;
     np->sel = NS_LIST;
     np->index = -1;
-    np->read = ef_read;
-    np->flags = ef_flags(type);
     if (len <= 0 || len > NS_LSIZE)
        return 0;
     for (i = 0; i < len; i++)
@@ -243,3 +216,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;
+}