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