]> git.pond.sub.org Git - empserver/blob - src/lib/subs/sarg.c
Import of Empire 4.2.12
[empserver] / src / lib / subs / sarg.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, 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 "misc.h"
36 #include "player.h"
37 #include "xy.h"
38 #include "nsc.h"
39 #include "sect.h"
40 #include "nat.h"
41 #include "deity.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 int
57 sarg_type(s_char *ptr)
58 {
59         int     c;
60
61         c = *ptr;
62         if (c == '@')
63                 return NS_DIST;
64         if (c == '*')
65                 return NS_ALL;
66         if (c == '#' || index(ptr, ',') != 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 0;
73 }
74
75 int
76 sarg_xy(s_char *ptr, coord *xp, coord *yp)
77 {
78         if (sarg_type(ptr) != NS_AREA)
79                 return 0;
80         *xp = atoip(&ptr);
81         if (*ptr++ != ',')
82                 return 0;
83         if (!isdigit(*ptr) && *ptr != '-')
84                 return 0;
85         *yp = atoi(ptr);
86         inputxy(xp, yp, player->cnum);
87         if ((*xp ^ *yp) & 01)
88                 return 0;
89         return 1;
90 }
91
92 /* returns absolute coords */
93 int
94 sarg_getrange(s_char *buf, register struct range *rp)
95 {
96         register int rlm;
97         register int c;
98         struct  natstr *np;
99         s_char  *bp;
100
101         bp = buf;
102         c = *bp;
103         if (c == '#') {
104                 /*
105                  * realm #X where (X > 0 && X < MAXNOR)
106                  * Assumes realms are in abs coordinates
107                  */
108                 bp++;
109                 rlm = atoi(bp);
110                 if (rlm < 0 || rlm >= MAXNOR) 
111                         return 0;
112                 np = getnatp(player->cnum);
113                 rp->lx = np->nat_b[rlm].b_xl;
114                 rp->hx = np->nat_b[rlm].b_xh;
115                 rp->ly = np->nat_b[rlm].b_yl;
116                 rp->hy = np->nat_b[rlm].b_yh;
117         } else {
118                 /*
119                  * full map specification
120                  * LX:LY,HX:HY where
121                  * ly, hy are optional.
122                  */
123                 if (!isdigit(c) && c != '-')
124                         return 0;
125                 rp->lx = rp->hx = atoip(&bp);
126                 if (*bp == ':') {
127                         bp++;
128                         rp->hx = atoip(&bp);
129                 }
130                 if (*bp++ != ',')
131                         return 0;
132                 if (!isdigit(c) && c != '-')
133                         return 0;
134                 rp->ly = rp->hy = atoip(&bp);
135                 if (*bp == ':') {
136                         bp++;
137                         rp->hy = atoip(&bp);
138                 }
139                 inputxy(&rp->lx, &rp->ly, player->cnum);
140                 inputxy(&rp->hx, &rp->hy, player->cnum);
141         }
142         xysize_range(rp);
143         return 1;
144 }
145
146 /*
147  * translate #1 or lx:ly,hx:hy into
148  * a result range struct
149  */
150 int
151 sarg_area(s_char *buf, register struct range *rp)
152 {
153         if (!sarg_getrange(buf, rp))
154                 return 0;
155         rp->hx += 1;
156         if (rp->hx >= WORLD_X)
157                 rp->hx = 0;
158         rp->hy += 1;
159         if (rp->hy >= WORLD_Y)
160                 rp->hy = 0;
161         xysize_range(rp);
162         return 1;
163 }
164
165 /*
166  * translate @x,y:int into
167  * result params
168  */
169 int
170 sarg_range(s_char *buf, coord *xp, coord *yp, int *dist)
171 {
172         s_char  *bp;
173
174         bp = buf;
175         if (bp == 0 || *bp == 0)
176                 return 0;
177         if (*bp++ != '@')
178                 return 0;
179         *xp = atoip(&bp);
180         if (*bp++ != ',')
181                 return 0;
182         *yp = atoip(&bp);
183         if (*bp++ != ':')
184                 return 0;
185         inputxy(xp, yp, player->cnum);
186         *dist = atoi(bp);
187         return 1;
188 }
189
190 /*
191  * list of idents; id/id/id/id/id
192  */
193 int
194 sarg_list(s_char *str, register int *list, int max)
195 {
196         register int i;
197         register int j;
198         register int n;
199         s_char  *arg;
200
201         arg = str;
202         for (i=0; i<max; i++) {
203                 if (!isdigit(*arg)) {
204                         pr("Illegal character '%c'\n", *arg);
205                         return 0;
206                 }
207                 n = atoip(&arg);
208                 for (j=0; j<i; j++) {
209                         if (list[j] == n)
210                                 break;
211                 }
212                 if (j != i) {
213                         /* duplicate; ignore */
214                         i--;
215                 } else
216                         list[i] = n;
217                 if (*arg == 0)
218                         break;
219                 if (*arg != '/') {
220                         pr("Expecting '/', got '%c'\n", *arg);
221                         return 0;
222                 }
223                 arg++;
224                 if (*arg == 0)
225                         break;
226         }
227         return i + 1;
228 }