]> git.pond.sub.org Git - empserver/blobdiff - src/lib/subs/snxtitem.c
Update copyright notice
[empserver] / src / lib / subs / snxtitem.c
index d3ba6bcc74a4a5320f042d86c0aeb8054b36f099..88452951276815329d4c4dd9ecd339096f05136d 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-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,
  *  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.
  *
  *  ---
  *
  *  snxtitem.c: Arrange item selection using one of many criteria.
- * 
+ *
  *  Known contributors to this file:
  *     Dave Pare, 1989
+ *     Markus Armbruster, 2009-2011
  */
 
+#include <config.h>
+
+#include <ctype.h>
 #include "misc.h"
 #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.
  * can select on NS_ALL, NS_AREA, NS_DIST, and NS_LIST.
  * iterate thru the "condarg" string looking
  * for arguments to compile into the nstr.
+ * Using this function for anything but command arguments is usually
+ * incorrect, because it respects conditionals.  Use the snxtitem_FOO()
+ * instead.
  */
 int
-snxtitem(struct nstr_item *np, int type, s_char *str)
+snxtitem(struct nstr_item *np, int type, char *str, char *prompt)
 {
     struct range range;
     int list[NS_LSIZE];
-    int n;
+    int cnum, n;
     coord cx, cy;
     int dist;
     int flags;
-    s_char natnumber[16];
-    s_char prompt[128];
-    s_char buf[1024];
+    char promptbuf[128];
+    char buf[1024];
 
     np->type = EF_BAD;
     np->sel = NS_UNDEF;
-    if (str == 0) {
-       sprintf(prompt, "%s(s)? ", ef_nameof(type));
+    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)) {
-       sprintf(natnumber, "%d", natarg(str, ""));
-       str = natnumber;
+       cnum = natarg(str, NULL);
+       if (cnum < 0)
+           return 0;
+       sprintf(buf, "%d", cnum);
+       str = buf;
     }
     flags = ef_flags(type);
     switch (sarg_type(str)) {
@@ -113,13 +125,7 @@ snxtitem(struct nstr_item *np, int type, s_char *str)
     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
@@ -131,36 +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);
-    ef_zapcache(type);
 }
 
 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
-    ef_zapcache(type);
 }
 
 void
@@ -174,9 +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);
-    ef_zapcache(type);
 }
 
 void
@@ -187,27 +175,21 @@ 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);
-    ef_zapcache(type);
 }
 
 void
-snxtitem_group(struct nstr_item *np, int type, s_char group)
+snxtitem_group(struct nstr_item *np, int type, char group)
 {
     if (group == '~')
-       group = ' ';
+       group = 0;
     memset(np, 0, sizeof(*np));
     np->cur = -1;
     np->sel = NS_GROUP;
     np->group = group;
     np->type = type;
     np->index = -1;
-    np->read = ef_read;
-    np->flags = ef_flags(type);
     xysize_range(&np->range);
-    ef_zapcache(type);
 }
 
 void
@@ -215,7 +197,6 @@ snxtitem_rewind(struct nstr_item *np)
 {
     np->cur = -1;
     np->index = -1;
-    ef_zapcache(np->type);
 }
 
 int
@@ -228,13 +209,43 @@ 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++)
        np->list[i] = list[i];
     np->size = len;
-    ef_zapcache(type);
+    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;
 }