]> 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-2018, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  snxtitem.c: Arrange item selection using one of many criteria.
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1989
31  *     Markus Armbruster, 2009-2011
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 "prototypes.h"
42 #include "unit.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, char *prompt)
55 {
56     struct range range;
57     int list[NS_LSIZE];
58     int cnum, n;
59     coord cx, cy;
60     int dist;
61     int flags;
62     char promptbuf[128];
63     char buf[1024];
64
65     np->type = EF_BAD;
66     np->sel = NS_UNDEF;
67     if (!str) {
68         if (!prompt) {
69             sprintf(promptbuf, "%s(s)? ", ef_nameof(type));
70             prompt = promptbuf;
71         }
72         str = getstring(prompt, buf);
73         if (!str)
74             return 0;
75     } else
76         make_stale_if_command_arg(str);
77     if (*str == 0) {
78         return 0;
79     }
80     if (type == EF_NATION && isalpha(*str)) {
81         cnum = natarg(str, NULL);
82         if (cnum < 0)
83             return 0;
84         sprintf(buf, "%d", cnum);
85         str = buf;
86     }
87     flags = ef_flags(type);
88     switch (sarg_type(str)) {
89     case NS_AREA:
90         if (!(flags & EFF_XY))
91             return 0;
92         if (!sarg_area(str, &range))
93             return 0;
94         snxtitem_area(np, type, &range);
95         break;
96     case NS_DIST:
97         if (!(flags & EFF_XY))
98             return 0;
99         if (!sarg_range(str, &cx, &cy, &dist))
100             return 0;
101         snxtitem_dist(np, type, cx, cy, dist);
102         break;
103     case NS_ALL:
104         snxtitem_all(np, type);
105         break;
106     case NS_LIST:
107         if ((n = sarg_list(str, list, NS_LSIZE)) == 0)
108             return 0;
109         if (!snxtitem_list(np, type, list, n))
110             return 0;
111         break;
112     case NS_XY:
113         if (!(flags & EFF_XY))
114             return 0;
115         if (!sarg_xy(str, &cx, &cy))
116             return 0;
117         snxtitem_xy(np, type, cx, cy);
118         break;
119     case NS_GROUP:
120         if (!(flags & EFF_GROUP))
121             return 0;
122         snxtitem_group(np, type, *str);
123         break;
124     default:
125         return 0;
126     }
127     return snxtitem_use_condarg(np);
128 }
129
130 void
131 snxtitem_area(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     xysize_range(&np->range);
140 }
141
142 void
143 snxtitem_dist(struct nstr_item *np, int type, int cx, int cy, int dist)
144 {
145     memset(np, 0, sizeof(*np));
146     xydist_range(cx, cy, dist, &np->range);
147     np->cur = -1;
148     np->type = type;
149     np->sel = NS_DIST;
150     np->cx = cx;
151     np->cy = cy;
152     np->index = -1;
153     np->dist = dist;
154 }
155
156 void
157 snxtitem_xy(struct nstr_item *np, int type, coord x, coord y)
158 {
159     memset(np, 0, sizeof(*np));
160     np->cur = -1;
161     np->type = type;
162     np->sel = NS_XY;
163     np->cx = xnorm(x);
164     np->cy = ynorm(y);
165     np->index = -1;
166     np->dist = 0;
167 }
168
169 void
170 snxtitem_all(struct nstr_item *np, int type)
171 {
172     memset(np, 0, sizeof(*np));
173     np->cur = -1;
174     np->sel = NS_ALL;
175     np->type = type;
176     np->index = -1;
177     xysize_range(&np->range);
178 }
179
180 void
181 snxtitem_group(struct nstr_item *np, int type, char group)
182 {
183     if (group == '~')
184         group = 0;
185     memset(np, 0, sizeof(*np));
186     np->cur = -1;
187     np->sel = NS_GROUP;
188     np->group = group;
189     np->type = type;
190     np->index = -1;
191     xysize_range(&np->range);
192 }
193
194 void
195 snxtitem_rewind(struct nstr_item *np)
196 {
197     np->cur = -1;
198     np->index = -1;
199 }
200
201 int
202 snxtitem_list(struct nstr_item *np, int type, int *list, int len)
203 {
204     int i;
205
206     memset(np, 0, sizeof(*np));
207     np->cur = -1;
208     np->type = type;
209     np->sel = NS_LIST;
210     np->index = -1;
211     if (len <= 0 || len > NS_LSIZE)
212         return 0;
213     for (i = 0; i < len; i++)
214         np->list[i] = list[i];
215     np->size = len;
216     return 1;
217 }
218
219 /*
220  * Initialize @np to iterate over the items of type @type in a carrier.
221  * The carrier has file type @carrier_type and UID @carrier_uid.
222  * Note: you can take an item gotten with nxtitem() off its carrier
223  * without disturbing the iterator.  Whether the iterator will pick up
224  * stuff you load onto the carrier during iteration is unspecified.
225  */
226 void
227 snxtitem_cargo(struct nstr_item *np, int type,
228                int carrier_type, int carrier_uid)
229 {
230     memset(np, 0, sizeof(*np));
231     np->cur = -1;
232     np->type = type;
233     np->sel = NS_CARGO;
234     np->next = unit_cargo_first(carrier_type, carrier_uid, type);
235 }
236
237 int
238 snxtitem_use_condarg(struct nstr_item *np)
239 {
240     int n;
241
242     if (!player->condarg)
243         return 1;
244     n = nstr_comp(np->cond, sizeof(np->cond) / sizeof(*np->cond),
245                   np->type, player->condarg);
246     if (n < 0)
247         return 0;
248     np->ncond = n;
249     return 1;
250 }