]> git.pond.sub.org Git - empserver/blob - src/lib/subs/sarg.c
Update copyright notice.
[empserver] / src / lib / subs / sarg.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  *  sarg.c: Parse selection arguments
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1989
32  */
33
34 #include <ctype.h>
35 #include <string.h>
36 #include "misc.h"
37 #include "player.h"
38 #include "xy.h"
39 #include "nsc.h"
40 #include "sect.h"
41 #include "nat.h"
42 #include "file.h"
43 #include "prototypes.h"
44 #include "optlist.h"
45
46 /*
47  * returns one of
48  *
49  *  #1, lx:ly,hx:hy --> NS_AREA
50  *  @x,y:dist  --> NS_DIST
51  *  %d or %d/%d/%d --> NS_LIST
52  *  * --> NS_ALL
53  *
54  * or 0 for none of the above.
55  */
56 ns_seltype
57 sarg_type(char *str)
58 {
59     int c;
60
61     c = *str;
62     if (c == '@')
63         return NS_DIST;
64     if (c == '*')
65         return NS_ALL;
66     if (c == '#' || strchr(str, ',') != 0)
67         return NS_AREA;
68     if (isdigit(c))
69         return NS_LIST;
70     if (c == '~' || isupper(c) || islower(c))
71         return NS_GROUP;
72     return NS_UNDEF;
73 }
74
75 int
76 sarg_xy(char *str, coord *xp, coord *yp)
77 {
78     coord x, y;
79     struct natstr *np;
80
81     x = strtox(str, &str);
82     if (x < 0 || *str++ != ',')
83         return 0;
84     y = strtoy(str, &str);
85     if (y < 0 || (*str != 0 && !isspace(*str)))
86       return 0;
87     if ((x ^ y) & 1)
88         return 0;
89     np = getnatp(player->cnum);
90     *xp = xabs(np, x);
91     *yp = yabs(np, y);
92     return 1;
93 }
94
95 /* returns absolute coords */
96 static int
97 sarg_getrange(char *str, struct range *rp)
98 {
99     long rlm;
100     struct natstr *np;
101     char *end;
102
103     if (*str == '#') {
104         /*
105          * realm #X where (X > 0 && X < MAXNOR)
106          * Assumes realms are in abs coordinates
107          */
108         if (*++str) {
109             rlm = strtol(str, &end, 10);
110             if (end == str || (*end != 0 && !isspace(*end))
111                 || rlm < 0 || MAXNOR <= rlm)
112                 return 0;
113         } else 
114             rlm = 0;
115         np = getnatp(player->cnum);
116         rp->lx = np->nat_b[rlm].b_xl;
117         rp->hx = np->nat_b[rlm].b_xh;
118         rp->ly = np->nat_b[rlm].b_yl;
119         rp->hy = np->nat_b[rlm].b_yh;
120     } else {
121         /*
122          * full map specification
123          * LX:LY,HX:HY where
124          * ly, hy are optional.
125          */
126         rp->lx = rp->hx = strtox(str, &str);
127         if (rp->lx < 0)
128             return 0;
129         if (*str == ':') {
130             rp->hx = strtox(str + 1, &str);
131             if (rp->hx < 0)
132                 return 0;
133         }
134         if (*str++ != ',')
135             return 0;
136         rp->ly = rp->hy = strtoy(str, &str);
137         if (rp->ly < 0)
138             return 0;
139         if (*str == ':') {
140             rp->hy = strtoy(str + 1, &str);
141             if (rp->hy < 0)
142                 return 0;
143         }
144         if (*str != 0 && !isspace(*str))
145             return 0;
146         np = getnatp(player->cnum);
147         rp->lx = xabs(np, rp->lx);
148         rp->hx = xabs(np, rp->hx);
149         rp->ly = yabs(np, rp->ly);
150         rp->hy = yabs(np, rp->hy);
151     }
152     xysize_range(rp);
153     return 1;
154 }
155
156 /*
157  * translate #1 or lx:ly,hx:hy into
158  * a result range struct
159  */
160 int
161 sarg_area(char *str, struct range *rp)
162 {
163     if (!sarg_getrange(str, rp))
164         return 0;
165     rp->hx += 1;
166     if (rp->hx >= WORLD_X)
167         rp->hx = 0;
168     rp->hy += 1;
169     if (rp->hy >= WORLD_Y)
170         rp->hy = 0;
171     xysize_range(rp);
172     return 1;
173 }
174
175 /*
176  * translate @x,y:int into
177  * result params
178  */
179 int
180 sarg_range(char *str, coord *xp, coord *yp, int *dist)
181 {
182     coord x, y;
183     long d;
184     char *end;
185     struct natstr *np;
186
187     if (*str++ != '@')
188         return 0;
189     x = strtox(str, &str);
190     if (x < 0 || *str++ != ',')
191         return 0;
192     y = strtoy(str, &str);
193     if (y < 0 || *str++ != ':')
194         return 0;
195     d = strtol(str, &end, 10);
196     if (end == str || (*end != 0 && !isspace(*end)) || d < 0)
197         return 0;
198     *dist = d;
199     np = getnatp(player->cnum);
200     *xp = xabs(np, x);
201     *yp = yabs(np, y);
202     return 1;
203 }
204
205 /*
206  * list of idents; id/id/id/id/id
207  */
208 int
209 sarg_list(char *str, int *list, int max)
210 {
211     int i, j;
212     long n;
213     char *end;
214
215     i = 0;
216     do {
217         n = strtol(str, &end, 10);
218         if (end == str || n < 0) {
219             pr("Illegal character '%c'\n", *str);
220             return 0;
221         }
222         for (j = 0; j < i; j++) {
223             if (list[j] == n)
224                 break;
225         }
226         if (j == i) {
227             if (i >= max) {
228                 pr("List too long (limit is %d)\n", max);
229                 return 0;
230             }
231             list[i++] = n;
232         }
233         str = end;
234     } while (*str++ == '/');
235
236     if (str[-1] != 0 && !isspace(str[-1])) {
237         pr("Expecting '/', got '%c'\n", str[-1]);
238         return 0;
239     }
240     return i;
241 }