]> git.pond.sub.org Git - empserver/blob - src/lib/subs/snxtitem.c
(acce, rea, rela, snxtitem): Pass NULL as prompt when the prompt won't
[empserver] / src / lib / subs / snxtitem.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  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 "misc.h"
37 #include "player.h"
38 #include "xy.h"
39 #include "nsc.h"
40 #include "file.h"
41 #include "prototypes.h"
42
43 /*
44  * setup the nstr structure for sector selection.
45  * can select on NS_ALL, NS_AREA, NS_DIST, and NS_LIST.
46  * iterate thru the "condarg" string looking
47  * for arguments to compile into the nstr.
48  */
49 int
50 snxtitem(struct nstr_item *np, int type, s_char *str)
51 {
52     struct range range;
53     int list[NS_LSIZE];
54     int n;
55     coord cx, cy;
56     int dist;
57     int flags;
58     s_char natnumber[16];
59     s_char prompt[128];
60     s_char buf[1024];
61
62     np->type = EF_BAD;
63     np->sel = NS_UNDEF;
64     if (str == 0) {
65         sprintf(prompt, "%s(s)? ", ef_nameof(type));
66         str = getstring(prompt, buf);
67         if (str == 0)
68             return 0;
69     }
70     if (*str == 0) {
71         /* empty string passed by player */
72         return 0;
73     }
74     if (type == EF_NATION && isalpha(*str)) {
75         sprintf(natnumber, "%d", natarg(str, NULL));
76         str = natnumber;
77     }
78     flags = ef_flags(type);
79     switch (sarg_type(str)) {
80     case NS_AREA:
81         if (!(flags & EFF_XY))
82             return 0;
83         if (!sarg_area(str, &range))
84             return 0;
85         snxtitem_area(np, type, &range);
86         break;
87     case NS_DIST:
88         if (!(flags & EFF_XY))
89             return 0;
90         if (!sarg_range(str, &cx, &cy, &dist))
91             return 0;
92         snxtitem_dist(np, type, cx, cy, dist);
93         break;
94     case NS_ALL:
95         snxtitem_all(np, type);
96         break;
97     case NS_LIST:
98         if ((n = sarg_list(str, list, NS_LSIZE)) == 0)
99             return 0;
100         if (!snxtitem_list(np, type, list, n))
101             return 0;
102         break;
103     case NS_XY:
104         if (!(flags & EFF_XY))
105             return 0;
106         if (!sarg_xy(str, &cx, &cy))
107             return 0;
108         snxtitem_xy(np, type, cx, cy);
109         break;
110     case NS_GROUP:
111         if (!(flags & EFF_GROUP))
112             return 0;
113         snxtitem_group(np, type, *str);
114         break;
115     default:
116         return 0;
117     }
118     np->flags = flags;
119     if (player->condarg == 0)
120         return 1;
121     n = nstr_comp(np->cond, sizeof(np->cond) / sizeof(*np->cond), type,
122                   player->condarg);
123     np->ncond = n >= 0 ? n : 0;
124     return n >= 0;
125 }
126
127 void
128 snxtitem_area(struct nstr_item *np, int type, struct range *range)
129 {
130     memset(np, 0, sizeof(*np));
131     np->cur = -1;
132     np->type = type;
133     np->sel = NS_AREA;
134     np->index = -1;
135     np->range = *range;
136     np->read = ef_read;
137     np->flags = ef_flags(type);
138     xysize_range(&np->range);
139 }
140
141 void
142 snxtitem_dist(struct nstr_item *np, int type, int cx, int cy,
143               int dist)
144 {
145     struct range range;
146
147     memset(np, 0, sizeof(*np));
148     xydist_range(cx, cy, dist, &range);
149     np->cur = -1;
150     np->type = type;
151     np->sel = NS_DIST;
152     np->cx = cx;
153     np->cy = cy;
154     np->index = -1;
155     np->range = range;
156     np->dist = dist;
157     np->read = ef_read;
158     np->flags = ef_flags(type);
159 #if 0
160     /* This is no longer proper. */
161     /* It did the wrong thing for small, hitech worlds. */
162     xysize_range(&np->range);
163 #endif
164 }
165
166 void
167 snxtitem_xy(struct nstr_item *np, int type, coord x, coord y)
168 {
169     memset(np, 0, sizeof(*np));
170     np->cur = -1;
171     np->type = type;
172     np->sel = NS_XY;
173     np->cx = xnorm(x);
174     np->cy = ynorm(y);
175     np->index = -1;
176     np->dist = 0;
177     np->read = ef_read;
178     np->flags = ef_flags(type);
179 }
180
181 void
182 snxtitem_all(struct nstr_item *np, int type)
183 {
184     memset(np, 0, sizeof(*np));
185     np->cur = -1;
186     np->sel = NS_ALL;
187     np->type = type;
188     np->index = -1;
189     np->read = ef_read;
190     np->flags = ef_flags(type);
191     xysize_range(&np->range);
192 }
193
194 void
195 snxtitem_group(struct nstr_item *np, int type, s_char group)
196 {
197     if (group == '~')
198         group = ' ';
199     memset(np, 0, sizeof(*np));
200     np->cur = -1;
201     np->sel = NS_GROUP;
202     np->group = group;
203     np->type = type;
204     np->index = -1;
205     np->read = ef_read;
206     np->flags = ef_flags(type);
207     xysize_range(&np->range);
208 }
209
210 void
211 snxtitem_rewind(struct nstr_item *np)
212 {
213     np->cur = -1;
214     np->index = -1;
215 }
216
217 int
218 snxtitem_list(struct nstr_item *np, int type, int *list, int len)
219 {
220     int i;
221
222     memset(np, 0, sizeof(*np));
223     np->cur = -1;
224     np->type = type;
225     np->sel = NS_LIST;
226     np->index = -1;
227     np->read = ef_read;
228     np->flags = ef_flags(type);
229     if (len <= 0 || len > NS_LSIZE)
230         return 0;
231     for (i = 0; i < len; i++)
232         np->list[i] = list[i];
233     np->size = len;
234     return 1;
235 }