2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * snxtitem.c: Arrange item selection using one of many criteria.
30 * Known contributors to this file:
32 * Markus Armbruster, 2008
43 #include "prototypes.h"
46 * setup the nstr structure for sector selection.
47 * can select on NS_ALL, NS_AREA, NS_DIST, and NS_LIST.
48 * iterate thru the "condarg" string looking
49 * for arguments to compile into the nstr.
50 * Using this function for anything but command arguments is usually
51 * incorrect, because it respects conditionals. Use the snxtitem_FOO()
55 snxtitem(struct nstr_item *np, int type, char *str, char *prompt)
70 sprintf(promptbuf, "%s(s)? ", ef_nameof(type));
73 str = getstring(prompt, buf);
78 /* empty string passed by player */
81 if (type == EF_NATION && isalpha(*str)) {
82 cnum = natarg(str, NULL);
85 sprintf(buf, "%d", cnum);
88 flags = ef_flags(type);
89 switch (sarg_type(str)) {
91 if (!(flags & EFF_XY))
93 if (!sarg_area(str, &range))
95 snxtitem_area(np, type, &range);
98 if (!(flags & EFF_XY))
100 if (!sarg_range(str, &cx, &cy, &dist))
102 snxtitem_dist(np, type, cx, cy, dist);
105 snxtitem_all(np, type);
108 if ((n = sarg_list(str, list, NS_LSIZE)) == 0)
110 if (!snxtitem_list(np, type, list, n))
114 if (!(flags & EFF_XY))
116 if (!sarg_xy(str, &cx, &cy))
118 snxtitem_xy(np, type, cx, cy);
121 if (!(flags & EFF_GROUP))
123 snxtitem_group(np, type, *str);
129 if (player->condarg == 0)
131 n = nstr_comp(np->cond, sizeof(np->cond) / sizeof(*np->cond), type,
133 np->ncond = n >= 0 ? n : 0;
138 snxtitem_area(struct nstr_item *np, int type, struct range *range)
140 memset(np, 0, sizeof(*np));
147 np->flags = ef_flags(type);
148 xysize_range(&np->range);
152 snxtitem_dist(struct nstr_item *np, int type, int cx, int cy,
157 memset(np, 0, sizeof(*np));
158 xydist_range(cx, cy, dist, &range);
168 np->flags = ef_flags(type);
170 /* This is no longer proper. */
171 /* It did the wrong thing for small, hitech worlds. */
172 xysize_range(&np->range);
177 snxtitem_xy(struct nstr_item *np, int type, coord x, coord y)
179 memset(np, 0, sizeof(*np));
188 np->flags = ef_flags(type);
192 snxtitem_all(struct nstr_item *np, int type)
194 memset(np, 0, sizeof(*np));
200 np->flags = ef_flags(type);
201 xysize_range(&np->range);
205 snxtitem_group(struct nstr_item *np, int type, char group)
209 memset(np, 0, sizeof(*np));
216 np->flags = ef_flags(type);
217 xysize_range(&np->range);
221 snxtitem_rewind(struct nstr_item *np)
228 snxtitem_list(struct nstr_item *np, int type, int *list, int len)
232 memset(np, 0, sizeof(*np));
238 np->flags = ef_flags(type);
239 if (len <= 0 || len > NS_LSIZE)
241 for (i = 0; i < len; i++)
242 np->list[i] = list[i];