]> 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-2010, 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  *     Markus Armbruster, 2009
33  */
34
35 #include <config.h>
36
37 #include <ctype.h>
38 #include "misc.h"
39 #include "player.h"
40 #include "xy.h"
41 #include "nsc.h"
42 #include "file.h"
43 #include "prototypes.h"
44 #include "unit.h"
45
46 /*
47  * setup the nstr structure for sector selection.
48  * can select on NS_ALL, NS_AREA, NS_DIST, and NS_LIST.
49  * iterate thru the "condarg" string looking
50  * for arguments to compile into the nstr.
51  * Using this function for anything but command arguments is usually
52  * incorrect, because it respects conditionals.  Use the snxtitem_FOO()
53  * instead.
54  */
55 int
56 snxtitem(struct nstr_item *np, int type, char *str, char *prompt)
57 {
58     struct range range;
59     int list[NS_LSIZE];
60     int cnum, n;
61     coord cx, cy;
62     int dist;
63     int flags;
64     char promptbuf[128];
65     char buf[1024];
66
67     np->type = EF_BAD;
68     np->sel = NS_UNDEF;
69     if (!str) {
70         if (!prompt) {
71             sprintf(promptbuf, "%s(s)? ", ef_nameof(type));
72             prompt = promptbuf;
73         }
74         str = getstring(prompt, buf);
75         if (!str)
76             return 0;
77     }
78     if (*str == 0) {
79         /* empty string passed by player */
80         return 0;
81     }
82     if (type == EF_NATION && isalpha(*str)) {
83         cnum = natarg(str, NULL);
84         if (cnum < 0)
85             return 0;
86         sprintf(buf, "%d", cnum);
87         str = buf;
88     }
89     flags = ef_flags(type);
90     switch (sarg_type(str)) {
91     case NS_AREA:
92         if (!(flags & EFF_XY))
93             return 0;
94         if (!sarg_area(str, &range))
95             return 0;
96         snxtitem_area(np, type, &range);
97         break;
98     case NS_DIST:
99         if (!(flags & EFF_XY))
100             return 0;
101         if (!sarg_range(str, &cx, &cy, &dist))
102             return 0;
103         snxtitem_dist(np, type, cx, cy, dist);
104         break;
105     case NS_ALL:
106         snxtitem_all(np, type);
107         break;
108     case NS_LIST:
109         if ((n = sarg_list(str, list, NS_LSIZE)) == 0)
110             return 0;
111         if (!snxtitem_list(np, type, list, n))
112             return 0;
113         break;
114     case NS_XY:
115         if (!(flags & EFF_XY))
116             return 0;
117         if (!sarg_xy(str, &cx, &cy))
118             return 0;
119         snxtitem_xy(np, type, cx, cy);
120         break;
121     case NS_GROUP:
122         if (!(flags & EFF_GROUP))
123             return 0;
124         snxtitem_group(np, type, *str);
125         break;
126     default:
127         return 0;
128     }
129     if (!player->condarg)
130         return 1;
131     n = nstr_comp(np->cond, sizeof(np->cond) / sizeof(*np->cond), type,
132                   player->condarg);
133     np->ncond = n >= 0 ? n : 0;
134     return n >= 0;
135 }
136
137 void
138 snxtitem_area(struct nstr_item *np, int type, struct range *range)
139 {
140     memset(np, 0, sizeof(*np));
141     np->cur = -1;
142     np->type = type;
143     np->sel = NS_AREA;
144     np->index = -1;
145     np->range = *range;
146     xysize_range(&np->range);
147 }
148
149 void
150 snxtitem_dist(struct nstr_item *np, int type, int cx, int cy,
151               int dist)
152 {
153     memset(np, 0, sizeof(*np));
154     xydist_range(cx, cy, dist, &np->range);
155     np->cur = -1;
156     np->type = type;
157     np->sel = NS_DIST;
158     np->cx = cx;
159     np->cy = cy;
160     np->index = -1;
161     np->dist = dist;
162 }
163
164 void
165 snxtitem_xy(struct nstr_item *np, int type, coord x, coord y)
166 {
167     memset(np, 0, sizeof(*np));
168     np->cur = -1;
169     np->type = type;
170     np->sel = NS_XY;
171     np->cx = xnorm(x);
172     np->cy = ynorm(y);
173     np->index = -1;
174     np->dist = 0;
175 }
176
177 void
178 snxtitem_all(struct nstr_item *np, int type)
179 {
180     memset(np, 0, sizeof(*np));
181     np->cur = -1;
182     np->sel = NS_ALL;
183     np->type = type;
184     np->index = -1;
185     xysize_range(&np->range);
186 }
187
188 void
189 snxtitem_group(struct nstr_item *np, int type, char group)
190 {
191     if (group == '~')
192         group = 0;
193     memset(np, 0, sizeof(*np));
194     np->cur = -1;
195     np->sel = NS_GROUP;
196     np->group = group;
197     np->type = type;
198     np->index = -1;
199     xysize_range(&np->range);
200 }
201
202 void
203 snxtitem_rewind(struct nstr_item *np)
204 {
205     np->cur = -1;
206     np->index = -1;
207 }
208
209 int
210 snxtitem_list(struct nstr_item *np, int type, int *list, int len)
211 {
212     int i;
213
214     memset(np, 0, sizeof(*np));
215     np->cur = -1;
216     np->type = type;
217     np->sel = NS_LIST;
218     np->index = -1;
219     if (len <= 0 || len > NS_LSIZE)
220         return 0;
221     for (i = 0; i < len; i++)
222         np->list[i] = list[i];
223     np->size = len;
224     return 1;
225 }
226
227 /*
228  * Initialize NP to iterate over the items of type TYPE in a carrier.
229  * The carrier has file type CARRIER_TYPE and uid CARRIER_UID.
230  * Note: you can take an item gotten with nxtitem() off its carrier
231  * without disturbing the iterator.  Whether the iterator will pick up
232  * stuff you load onto the carrier during iteration is unspecified.
233  */
234 void
235 snxtitem_cargo(struct nstr_item *np, int type,
236                int carrier_type, int carrier_uid)
237 {
238     memset(np, 0, sizeof(*np));
239     np->cur = -1;
240     np->type = type;
241     np->sel = NS_CARGO;
242     np->next = unit_cargo_first(carrier_type, carrier_uid, type);
243 }