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"
47 * setup the nstr structure for sector selection.
48 * can select on NS_ALL, NS_AREA, NS_DIST, and NS_LIST.
49 * iterate thru the "condarg" string looking
50 * for arguments to compile into the nstr.
51 * Using this function for anything but command arguments is usually
52 * incorrect, because it respects conditionals. Use the snxtitem_FOO()
56 snxtitem(struct nstr_item *np, int type, char *str, char *prompt)
71 sprintf(promptbuf, "%s(s)? ", ef_nameof(type));
74 str = getstring(prompt, buf);
79 /* empty string passed by player */
82 if (type == EF_NATION && isalpha(*str)) {
83 cnum = natarg(str, NULL);
86 sprintf(buf, "%d", cnum);
89 flags = ef_flags(type);
90 switch (sarg_type(str)) {
92 if (!(flags & EFF_XY))
94 if (!sarg_area(str, &range))
96 snxtitem_area(np, type, &range);
99 if (!(flags & EFF_XY))
101 if (!sarg_range(str, &cx, &cy, &dist))
103 snxtitem_dist(np, type, cx, cy, dist);
106 snxtitem_all(np, type);
109 if ((n = sarg_list(str, list, NS_LSIZE)) == 0)
111 if (!snxtitem_list(np, type, list, n))
115 if (!(flags & EFF_XY))
117 if (!sarg_xy(str, &cx, &cy))
119 snxtitem_xy(np, type, cx, cy);
122 if (!(flags & EFF_GROUP))
124 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));
146 xysize_range(&np->range);
150 snxtitem_dist(struct nstr_item *np, int type, int cx, int cy,
153 memset(np, 0, sizeof(*np));
154 xydist_range(cx, cy, dist, &np->range);
165 snxtitem_xy(struct nstr_item *np, int type, coord x, coord y)
167 memset(np, 0, sizeof(*np));
178 snxtitem_all(struct nstr_item *np, int type)
180 memset(np, 0, sizeof(*np));
185 xysize_range(&np->range);
189 snxtitem_group(struct nstr_item *np, int type, char group)
193 memset(np, 0, sizeof(*np));
199 xysize_range(&np->range);
203 snxtitem_rewind(struct nstr_item *np)
210 snxtitem_list(struct nstr_item *np, int type, int *list, int len)
214 memset(np, 0, sizeof(*np));
219 if (len <= 0 || len > NS_LSIZE)
221 for (i = 0; i < len; i++)
222 np->list[i] = list[i];
228 * Initialize NP to iterate over the items of type TYPE in a carrier.
229 * The carrier has file type CARRIER_TYPE and uid CARRIER_UID.
230 * Note: you can take an item gotten with nxtitem() off its carrier
231 * without disturbing the iterator. Whether the iterator will pick up
232 * stuff you load onto the carrier during iteration is unspecified.
235 snxtitem_cargo(struct nstr_item *np, int type,
236 int carrier_type, int carrier_uid)
238 memset(np, 0, sizeof(*np));
242 np->next = unit_cargo_first(carrier_type, carrier_uid, type);