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
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.
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.
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
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.
28 * sarg.c: Parse selection arguments
30 * Known contributors to this file:
44 #include "prototypes.h"
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
56 * or 0 for none of the above.
68 if (c == '#' || strchr(str, ',') != 0)
72 if (c == '~' || isalpha(c))
78 sarg_xy(char *str, coord *xp, coord *yp)
83 x = strtox(str, &str);
84 if (x < 0 || *str++ != ',')
86 y = strtoy(str, &str);
87 if (y < 0 || (*str != 0 && !isspace(*str)))
91 np = getnatp(player->cnum);
97 /* returns absolute coords */
99 sarg_getrange(char *str, struct range *rp)
107 * realm #X where (X > 0 && X < MAXNOR)
108 * Assumes realms are in abs coordinates
111 rlm = strtol(str, &end, 10);
112 if (end == str || (*end != 0 && !isspace(*end))
113 || rlm < 0 || MAXNOR <= rlm)
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;
124 * full map specification
126 * ly, hy are optional.
128 rp->lx = rp->hx = strtox(str, &str);
132 rp->hx = strtox(str + 1, &str);
138 rp->ly = rp->hy = strtoy(str, &str);
142 rp->hy = strtoy(str + 1, &str);
146 if (*str != 0 && !isspace(*str))
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);
159 * translate #1 or lx:ly,hx:hy into
160 * a result range struct
163 sarg_area(char *str, struct range *rp)
165 if (!sarg_getrange(str, rp))
168 if (rp->hx >= WORLD_X)
171 if (rp->hy >= WORLD_Y)
178 * translate @x,y:int into
182 sarg_range(char *str, coord *xp, coord *yp, int *dist)
191 x = strtox(str, &str);
192 if (x < 0 || *str++ != ',')
194 y = strtoy(str, &str);
195 if (y < 0 || *str++ != ':')
197 d = strtol(str, &end, 10);
198 if (end == str || (*end != 0 && !isspace(*end)) || d < 0)
201 np = getnatp(player->cnum);
208 * list of idents; id/id/id/id/id
211 sarg_list(char *str, int *list, int max)
219 n = strtol(str, &end, 10);
220 if (end == str || n < 0) {
221 pr("Illegal character '%c'\n", *str);
224 for (j = 0; j < i; j++) {
230 pr("List too long (limit is %d)\n", max);
236 } while (*str++ == '/');
238 if (str[-1] != 0 && !isspace(str[-1])) {
239 pr("Expecting '/', got '%c'\n", str[-1]);