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