]> git.pond.sub.org Git - empserver/blob - src/lib/subs/snxtitem.c
These files were split a long time ago, for technical reasons which
[empserver] / src / lib / subs / snxtitem.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, 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 "var.h"
37 #include "xy.h"
38 #include "sect.h"
39 #include "nsc.h"
40 #include "file.h"
41 #include "com.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  */
50 int
51 snxtitem(register struct nstr_item *np, int type, s_char *str)
52 {
53     register s_char *cp;
54     struct range range;
55     int list[NS_LSIZE];
56     int n;
57     coord cx, cy;
58     int dist;
59     int flags;
60     s_char natnumber[16];
61     s_char prompt[128];
62     s_char buf[1024];
63
64     np->type = NS_UNDEF;
65     np->sel = NS_UNDEF;
66     if (str == 0) {
67         sprintf(prompt, "%s(s)? ", ef_nameof(type));
68         str = getstring(prompt, buf);
69         if (str == 0)
70             return 0;
71     }
72     if (*str == 0) {
73         /* str present, but only <cr>: nil string passed by player */
74         return 0;
75     }
76     if (type == EF_NATION && isalpha(*str)) {
77         sprintf(natnumber, "%d", natarg(str, ""));
78         str = natnumber;
79     }
80     flags = ef_flags(type);
81     switch (sarg_type(str)) {
82     case NS_AREA:
83         if (!(flags & EFF_XY))
84             return 0;
85         if (!sarg_area(str, &range))
86             return 0;
87         snxtitem_area(np, type, &range);
88         break;
89     case NS_DIST:
90         if (!(flags & EFF_XY))
91             return 0;
92         if (!sarg_range(str, &cx, &cy, &dist))
93             return 0;
94         snxtitem_dist(np, type, cx, cy, dist);
95         break;
96     case NS_ALL:
97         snxtitem_all(np, type);
98         break;
99     case NS_LIST:
100         if ((n = sarg_list(str, list, NS_LSIZE)) == 0)
101             return 0;
102         if (!snxtitem_list(np, type, list, n))
103             return 0;
104         break;
105     case NS_XY:
106         if (!(flags & EFF_XY))
107             return 0;
108         if (!sarg_xy(str, &cx, &cy))
109             return 0;
110         snxtitem_xy(np, type, cx, cy);
111         break;
112     case NS_GROUP:
113         if (!(flags & EFF_GROUP))
114             return 0;
115         snxtitem_group(np, type, *str);
116         break;
117     default:
118         return 0;
119     }
120     np->flags = flags;
121     if (player->condarg == 0)
122         return 1;
123     cp = player->condarg;
124     while ((cp = nstr_comp(np->cond, &np->ncond, type, cp)) && *cp) ;
125     if (cp == 0)
126         return 0;
127     return 1;
128 }
129
130 void
131 snxtitem_area(register struct nstr_item *np, int type, struct range *range)
132 {
133     memset(np, 0, sizeof(*np));
134     np->cur = -1;
135     np->type = type;
136     np->sel = NS_AREA;
137     np->index = -1;
138     np->range = *range;
139     np->read = ef_read;
140     np->flags = ef_flags(type);
141     xysize_range(&np->range);
142     ef_zapcache(type);
143 }
144
145 void
146 snxtitem_dist(register struct nstr_item *np, int type, int cx, int cy,
147               int dist)
148 {
149     struct range range;
150
151     memset(np, 0, sizeof(*np));
152     xydist_range(cx, cy, dist, &range);
153     np->cur = -1;
154     np->type = type;
155     np->sel = NS_DIST;
156     np->cx = cx;
157     np->cy = cy;
158     np->index = -1;
159     np->range = range;
160     np->dist = dist;
161     np->read = ef_read;
162     np->flags = ef_flags(type);
163 #if 0
164     /* This is no longer proper. */
165     /* It did the wrong thing for small, hitech worlds. */
166     xysize_range(&np->range);
167 #endif
168     ef_zapcache(type);
169 }
170
171 void
172 snxtitem_xy(register struct nstr_item *np, int type, coord x, coord y)
173 {
174     memset(np, 0, sizeof(*np));
175     np->cur = -1;
176     np->type = type;
177     np->sel = NS_XY;
178     np->cx = xnorm(x);
179     np->cy = ynorm(y);
180     np->index = -1;
181     np->dist = 0;
182     np->read = ef_read;
183     np->flags = ef_flags(type);
184     ef_zapcache(type);
185 }
186
187 void
188 snxtitem_all(register 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     ef_zapcache(type);
199 }
200
201 void
202 snxtitem_group(register struct nstr_item *np, int type, s_char group)
203 {
204     if (group == '~')
205         group = ' ';
206     memset(np, 0, sizeof(*np));
207     np->cur = -1;
208     np->sel = NS_GROUP;
209     np->group = group;
210     np->type = type;
211     np->index = -1;
212     np->read = ef_read;
213     np->flags = ef_flags(type);
214     xysize_range(&np->range);
215     ef_zapcache(type);
216 }
217
218 void
219 snxtitem_rewind(struct nstr_item *np)
220 {
221     np->cur = -1;
222     np->index = -1;
223     ef_zapcache(np->type);
224 }
225
226 int
227 snxtitem_list(register 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     ef_zapcache(type);
244     return 1;
245 }