]> 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-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  */
33
34 #include <config.h>
35
36 #include <ctype.h>
37 #include "misc.h"
38 #include "player.h"
39 #include "xy.h"
40 #include "nsc.h"
41 #include "file.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  * Using this function for anything but command arguments is usually
50  * incorrect, because it respects conditionals.  Use the snxtitem_FOO()
51  * instead.
52  */
53 int
54 snxtitem(struct nstr_item *np, int type, char *str)
55 {
56     struct range range;
57     int list[NS_LSIZE];
58     int n;
59     coord cx, cy;
60     int dist;
61     int flags;
62     char natnumber[16];
63     char prompt[128];
64     char buf[1024];
65
66     np->type = EF_BAD;
67     np->sel = NS_UNDEF;
68     if (str == 0) {
69         sprintf(prompt, "%s(s)? ", ef_nameof(type));
70         str = getstring(prompt, buf);
71         if (str == 0)
72             return 0;
73     }
74     if (*str == 0) {
75         /* empty string passed by player */
76         return 0;
77     }
78     if (type == EF_NATION && isalpha(*str)) {
79         sprintf(natnumber, "%d", natarg(str, NULL));
80         str = natnumber;
81     }
82     flags = ef_flags(type);
83     switch (sarg_type(str)) {
84     case NS_AREA:
85         if (!(flags & EFF_XY))
86             return 0;
87         if (!sarg_area(str, &range))
88             return 0;
89         snxtitem_area(np, type, &range);
90         break;
91     case NS_DIST:
92         if (!(flags & EFF_XY))
93             return 0;
94         if (!sarg_range(str, &cx, &cy, &dist))
95             return 0;
96         snxtitem_dist(np, type, cx, cy, dist);
97         break;
98     case NS_ALL:
99         snxtitem_all(np, type);
100         break;
101     case NS_LIST:
102         if ((n = sarg_list(str, list, NS_LSIZE)) == 0)
103             return 0;
104         if (!snxtitem_list(np, type, list, n))
105             return 0;
106         break;
107     case NS_XY:
108         if (!(flags & EFF_XY))
109             return 0;
110         if (!sarg_xy(str, &cx, &cy))
111             return 0;
112         snxtitem_xy(np, type, cx, cy);
113         break;
114     case NS_GROUP:
115         if (!(flags & EFF_GROUP))
116             return 0;
117         snxtitem_group(np, type, *str);
118         break;
119     default:
120         return 0;
121     }
122     np->flags = flags;
123     if (player->condarg == 0)
124         return 1;
125     n = nstr_comp(np->cond, sizeof(np->cond) / sizeof(*np->cond), type,
126                   player->condarg);
127     np->ncond = n >= 0 ? n : 0;
128     return n >= 0;
129 }
130
131 void
132 snxtitem_area(struct nstr_item *np, int type, struct range *range)
133 {
134     memset(np, 0, sizeof(*np));
135     np->cur = -1;
136     np->type = type;
137     np->sel = NS_AREA;
138     np->index = -1;
139     np->range = *range;
140     np->read = ef_read;
141     np->flags = ef_flags(type);
142     xysize_range(&np->range);
143 }
144
145 void
146 snxtitem_dist(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 }
169
170 void
171 snxtitem_xy(struct nstr_item *np, int type, coord x, coord y)
172 {
173     memset(np, 0, sizeof(*np));
174     np->cur = -1;
175     np->type = type;
176     np->sel = NS_XY;
177     np->cx = xnorm(x);
178     np->cy = ynorm(y);
179     np->index = -1;
180     np->dist = 0;
181     np->read = ef_read;
182     np->flags = ef_flags(type);
183 }
184
185 void
186 snxtitem_all(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 }
197
198 void
199 snxtitem_group(struct nstr_item *np, int type, char group)
200 {
201     if (group == '~')
202         group = 0;
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 }
213
214 void
215 snxtitem_rewind(struct nstr_item *np)
216 {
217     np->cur = -1;
218     np->index = -1;
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     return 1;
239 }