2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2021, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire 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 3 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, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * snxtitem.c: Arrange item selection using one of many criteria.
29 * Known contributors to this file:
31 * Markus Armbruster, 2009-2020
41 #include "prototypes.h"
45 * setup the nstr structure for sector selection.
46 * can select on NS_ALL, NS_AREA, NS_DIST, and NS_LIST.
47 * iterate thru the "condarg" string looking
48 * for arguments to compile into the nstr.
49 * Using this function for anything but command arguments is usually
50 * incorrect, because it respects conditionals. Use the snxtitem_FOO()
54 snxtitem(struct nstr_item *np, int type, char *str, char *prompt)
69 sprintf(promptbuf, "%s(s)? ", ef_nameof(type));
72 str = getstring(prompt, buf);
76 make_stale_if_command_arg(str);
80 if (type == EF_NATION && isalpha(*str)) {
81 cnum = natarg(str, NULL);
84 sprintf(buf, "%d", cnum);
87 flags = ef_flags(type);
88 switch (sarg_type(str)) {
90 if (!(flags & EFF_XY))
92 if (!sarg_area(str, &range))
94 snxtitem_area(np, type, &range);
97 if (!(flags & EFF_XY))
99 if (!sarg_range(str, &cx, &cy, &dist))
101 snxtitem_dist(np, type, cx, cy, dist);
104 snxtitem_all(np, type);
107 if ((n = sarg_list(str, list, NS_LSIZE)) == 0)
109 if (!snxtitem_list(np, type, list, n))
113 if (!(flags & EFF_XY))
115 if (!sarg_xy(str, &cx, &cy))
117 snxtitem_xy(np, type, cx, cy);
120 if (!(flags & EFF_GROUP))
122 snxtitem_group(np, type, *str);
127 return snxtitem_use_condarg(np);
131 snxtitem_area(struct nstr_item *np, int type, struct range *range)
133 memset(np, 0, sizeof(*np));
139 xysize_range(&np->range);
143 snxtitem_dist(struct nstr_item *np, int type, int cx, int cy, int dist)
145 memset(np, 0, sizeof(*np));
146 xydist_range(cx, cy, dist, &np->range);
157 snxtitem_xy(struct nstr_item *np, int type, coord x, coord y)
159 memset(np, 0, sizeof(*np));
170 snxtitem_all(struct nstr_item *np, int type)
172 memset(np, 0, sizeof(*np));
177 xysize_range(&np->range);
181 snxtitem_group(struct nstr_item *np, int type, char group)
185 memset(np, 0, sizeof(*np));
191 xysize_range(&np->range);
195 snxtitem_rewind(struct nstr_item *np)
202 snxtitem_list(struct nstr_item *np, int type, int *list, int len)
206 memset(np, 0, sizeof(*np));
211 if (len <= 0 || len > NS_LSIZE)
213 for (i = 0; i < len; i++)
214 np->list[i] = list[i];
220 * Initialize @np to iterate over the items of type @type in a carrier.
221 * The carrier has file type @carrier_type and UID @carrier_uid.
222 * Note: you can take an item gotten with nxtitem() off its carrier
223 * without disturbing the iterator. Whether the iterator will pick up
224 * stuff you load onto the carrier during iteration is unspecified.
227 snxtitem_cargo(struct nstr_item *np, int type,
228 int carrier_type, int carrier_uid)
230 memset(np, 0, sizeof(*np));
234 np->next = unit_cargo_first(carrier_type, carrier_uid, type);
238 snxtitem_use_condarg(struct nstr_item *np)
242 if (!player->condarg)
244 n = nstr_comp(np->cond, ARRAY_SIZE(np->cond),
245 np->type, player->condarg);