]> git.pond.sub.org Git - empserver/blobdiff - src/lib/subs/snxtitem.c
Fix trailing whitespace
[empserver] / src / lib / subs / snxtitem.c
index e88ff7731e67d6993d0b5d5cc4cc49cf007629ab..2c7534d47a420af176a093c4ba51c2186b3eb92f 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
  *  ---
  *
  *  snxtitem.c: Arrange item selection using one of many criteria.
- * 
+ *
  *  Known contributors to this file:
  *     Dave Pare, 1989
+ *     Markus Armbruster, 2008
  */
 
 #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, 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;
-    char natnumber[16];
-    char prompt[128];
+    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 (!prompt) {
+           sprintf(promptbuf, "%s(s)? ", ef_nameof(type));
+           prompt = promptbuf;
+       }
        str = getstring(prompt, buf);
        if (str == 0)
            return 0;
@@ -72,8 +80,11 @@ snxtitem(struct nstr_item *np, int type, char *str)
        return 0;
     }
     if (type == EF_NATION && isalpha(*str)) {
-       sprintf(natnumber, "%d", natarg(str, NULL));
-       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)) {
@@ -115,7 +126,6 @@ snxtitem(struct nstr_item *np, int type, 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,
@@ -133,8 +143,6 @@ 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);
 }
 
@@ -142,25 +150,15 @@ void
 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
@@ -174,8 +172,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
@@ -186,24 +182,20 @@ 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);
 }
 
 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);
 }
 
@@ -224,8 +216,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++)
@@ -233,3 +223,21 @@ 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);
+}