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