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