]> git.pond.sub.org Git - empserver/blob - src/lib/subs/snxtitem.c
Clean up how snxtitem() parses country names
[empserver] / src / lib / subs / snxtitem.c
1 /*
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
5  *
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.
10  *
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.
15  *
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
19  *
20  *  ---
21  *
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.
25  *
26  *  ---
27  *
28  *  snxtitem.c: Arrange item selection using one of many criteria.
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1989
32  */
33
34 #include <config.h>
35
36 #include <ctype.h>
37 #include "misc.h"
38 #include "player.h"
39 #include "xy.h"
40 #include "nsc.h"
41 #include "file.h"
42 #include "prototypes.h"
43
44 /*
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()
51  * instead.
52  */
53 int
54 snxtitem(struct nstr_item *np, int type, char *str)
55 {
56     struct range range;
57     int list[NS_LSIZE];
58     int cnum, n;
59     coord cx, cy;
60     int dist;
61     int flags;
62     char prompt[128];
63     char buf[1024];
64
65     np->type = EF_BAD;
66     np->sel = NS_UNDEF;
67     if (str == 0) {
68         sprintf(prompt, "%s(s)? ", ef_nameof(type));
69         str = getstring(prompt, buf);
70         if (str == 0)
71             return 0;
72     }
73     if (*str == 0) {
74         /* empty string passed by player */
75         return 0;
76     }
77     if (type == EF_NATION && isalpha(*str)) {
78         cnum = natarg(str, NULL);
79         if (cnum < 0)
80             return 0;
81         sprintf(buf, "%d", cnum);
82         str = buf;
83     }
84     flags = ef_flags(type);
85     switch (sarg_type(str)) {
86     case NS_AREA:
87         if (!(flags & EFF_XY))
88             return 0;
89         if (!sarg_area(str, &range))
90             return 0;
91         snxtitem_area(np, type, &range);
92         break;
93     case NS_DIST:
94         if (!(flags & EFF_XY))
95             return 0;
96         if (!sarg_range(str, &cx, &cy, &dist))
97             return 0;
98         snxtitem_dist(np, type, cx, cy, dist);
99         break;
100     case NS_ALL:
101         snxtitem_all(np, type);
102         break;
103     case NS_LIST:
104         if ((n = sarg_list(str, list, NS_LSIZE)) == 0)
105             return 0;
106         if (!snxtitem_list(np, type, list, n))
107             return 0;
108         break;
109     case NS_XY:
110         if (!(flags & EFF_XY))
111             return 0;
112         if (!sarg_xy(str, &cx, &cy))
113             return 0;
114         snxtitem_xy(np, type, cx, cy);
115         break;
116     case NS_GROUP:
117         if (!(flags & EFF_GROUP))
118             return 0;
119         snxtitem_group(np, type, *str);
120         break;
121     default:
122         return 0;
123     }
124     np->flags = flags;
125     if (player->condarg == 0)
126         return 1;
127     n = nstr_comp(np->cond, sizeof(np->cond) / sizeof(*np->cond), type,
128                   player->condarg);
129     np->ncond = n >= 0 ? n : 0;
130     return n >= 0;
131 }
132
133 void
134 snxtitem_area(struct nstr_item *np, int type, struct range *range)
135 {
136     memset(np, 0, sizeof(*np));
137     np->cur = -1;
138     np->type = type;
139     np->sel = NS_AREA;
140     np->index = -1;
141     np->range = *range;
142     np->read = ef_read;
143     np->flags = ef_flags(type);
144     xysize_range(&np->range);
145 }
146
147 void
148 snxtitem_dist(struct nstr_item *np, int type, int cx, int cy,
149               int dist)
150 {
151     struct range range;
152
153     memset(np, 0, sizeof(*np));
154     xydist_range(cx, cy, dist, &range);
155     np->cur = -1;
156     np->type = type;
157     np->sel = NS_DIST;
158     np->cx = cx;
159     np->cy = cy;
160     np->index = -1;
161     np->range = range;
162     np->dist = dist;
163     np->read = ef_read;
164     np->flags = ef_flags(type);
165 #if 0
166     /* This is no longer proper. */
167     /* It did the wrong thing for small, hitech worlds. */
168     xysize_range(&np->range);
169 #endif
170 }
171
172 void
173 snxtitem_xy(struct nstr_item *np, int type, coord x, coord y)
174 {
175     memset(np, 0, sizeof(*np));
176     np->cur = -1;
177     np->type = type;
178     np->sel = NS_XY;
179     np->cx = xnorm(x);
180     np->cy = ynorm(y);
181     np->index = -1;
182     np->dist = 0;
183     np->read = ef_read;
184     np->flags = ef_flags(type);
185 }
186
187 void
188 snxtitem_all(struct nstr_item *np, int type)
189 {
190     memset(np, 0, sizeof(*np));
191     np->cur = -1;
192     np->sel = NS_ALL;
193     np->type = type;
194     np->index = -1;
195     np->read = ef_read;
196     np->flags = ef_flags(type);
197     xysize_range(&np->range);
198 }
199
200 void
201 snxtitem_group(struct nstr_item *np, int type, char group)
202 {
203     if (group == '~')
204         group = 0;
205     memset(np, 0, sizeof(*np));
206     np->cur = -1;
207     np->sel = NS_GROUP;
208     np->group = group;
209     np->type = type;
210     np->index = -1;
211     np->read = ef_read;
212     np->flags = ef_flags(type);
213     xysize_range(&np->range);
214 }
215
216 void
217 snxtitem_rewind(struct nstr_item *np)
218 {
219     np->cur = -1;
220     np->index = -1;
221 }
222
223 int
224 snxtitem_list(struct nstr_item *np, int type, int *list, int len)
225 {
226     int i;
227
228     memset(np, 0, sizeof(*np));
229     np->cur = -1;
230     np->type = type;
231     np->sel = NS_LIST;
232     np->index = -1;
233     np->read = ef_read;
234     np->flags = ef_flags(type);
235     if (len <= 0 || len > NS_LSIZE)
236         return 0;
237     for (i = 0; i < len; i++)
238         np->list[i] = list[i];
239     np->size = len;
240     return 1;
241 }