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