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