]> git.pond.sub.org Git - empserver/blob - src/lib/subs/snxtitem.c
Do not include var.h where no longer needed. Clean up register keywords in these...
[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 "sect.h"
38 #include "nsc.h"
39 #include "file.h"
40 #include "com.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         /* str present, but only <cr>: nil string passed by player */
72         return 0;
73     }
74     if (type == EF_NATION && isalpha(*str)) {
75         sprintf(natnumber, "%d", natarg(str, ""));
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     ef_zapcache(type);
140 }
141
142 void
143 snxtitem_dist(struct nstr_item *np, int type, int cx, int cy,
144               int dist)
145 {
146     struct range range;
147
148     memset(np, 0, sizeof(*np));
149     xydist_range(cx, cy, dist, &range);
150     np->cur = -1;
151     np->type = type;
152     np->sel = NS_DIST;
153     np->cx = cx;
154     np->cy = cy;
155     np->index = -1;
156     np->range = range;
157     np->dist = dist;
158     np->read = ef_read;
159     np->flags = ef_flags(type);
160 #if 0
161     /* This is no longer proper. */
162     /* It did the wrong thing for small, hitech worlds. */
163     xysize_range(&np->range);
164 #endif
165     ef_zapcache(type);
166 }
167
168 void
169 snxtitem_xy(struct nstr_item *np, int type, coord x, coord y)
170 {
171     memset(np, 0, sizeof(*np));
172     np->cur = -1;
173     np->type = type;
174     np->sel = NS_XY;
175     np->cx = xnorm(x);
176     np->cy = ynorm(y);
177     np->index = -1;
178     np->dist = 0;
179     np->read = ef_read;
180     np->flags = ef_flags(type);
181     ef_zapcache(type);
182 }
183
184 void
185 snxtitem_all(struct nstr_item *np, int type)
186 {
187     memset(np, 0, sizeof(*np));
188     np->cur = -1;
189     np->sel = NS_ALL;
190     np->type = type;
191     np->index = -1;
192     np->read = ef_read;
193     np->flags = ef_flags(type);
194     xysize_range(&np->range);
195     ef_zapcache(type);
196 }
197
198 void
199 snxtitem_group(struct nstr_item *np, int type, s_char group)
200 {
201     if (group == '~')
202         group = ' ';
203     memset(np, 0, sizeof(*np));
204     np->cur = -1;
205     np->sel = NS_GROUP;
206     np->group = group;
207     np->type = type;
208     np->index = -1;
209     np->read = ef_read;
210     np->flags = ef_flags(type);
211     xysize_range(&np->range);
212     ef_zapcache(type);
213 }
214
215 void
216 snxtitem_rewind(struct nstr_item *np)
217 {
218     np->cur = -1;
219     np->index = -1;
220     ef_zapcache(np->type);
221 }
222
223 int
224 snxtitem_list(struct nstr_item *np, int type, int *list, int len)
225 {
226     int i;
227
228     memset(np, 0, sizeof(*np));
229     np->cur = -1;
230     np->type = type;
231     np->sel = NS_LIST;
232     np->index = -1;
233     np->read = ef_read;
234     np->flags = ef_flags(type);
235     if (len <= 0 || len > NS_LSIZE)
236         return 0;
237     for (i = 0; i < len; i++)
238         np->list[i] = list[i];
239     np->size = len;
240     ef_zapcache(type);
241     return 1;
242 }