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