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
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 * nstr.c: compile and execute the item selections on sectors
30 * Known contributors to this file:
33 * Markus Armbruster, 2004
41 static int nstr_promote(int valtype);
44 * Compile conditions into array NP[LEN].
45 * Return number of conditions, or -1 on error.
46 * It is an error if there are more than LEN conditions.
47 * TYPE is the context type, a file type.
48 * STR is the condition string, in Empire syntax, without the leading
52 nstr_comp(struct nscstr *np, int len, int type, char *str)
58 int lft_type, rgt_type;
61 for (i = 0; ; ++i, ++np) {
66 tail = nstr_comp_val(cond, &np->lft, type);
71 if (*tail != '<' && *tail != '=' && *tail != '>' && *tail != '#') {
73 pr("%s -- expected condition operator\n", cond);
75 pr("%s -- missing condition operator\n", cond);
82 tail = nstr_comp_val(tail, &np->rgt, type);
86 /* find operator type, coerce operands */
87 lft_type = nstr_promote(np->lft.val_type);
88 rgt_type = nstr_promote(np->rgt.val_type);
89 if (lft_type == NSC_TYPEID) {
90 if (!nstr_coerce_val(&np->rgt, NSC_TYPEID, str))
91 np->optype = NSC_TYPEID;
92 } else if (rgt_type == NSC_TYPEID) {
93 if (!nstr_coerce_val(&np->lft, NSC_TYPEID, str))
94 np->optype = NSC_TYPEID;
95 } else if (lft_type == NSC_STRING) {
96 if (!nstr_coerce_val(&np->rgt, NSC_STRING, str))
97 np->optype = NSC_STRING;
98 } else if (rgt_type == NSC_STRING) {
99 if (!nstr_coerce_val(&np->lft, NSC_STRING, str))
100 np->optype = NSC_STRING;
101 } else if (lft_type == NSC_DOUBLE) {
102 if (!nstr_coerce_val(&np->rgt, NSC_DOUBLE, str))
103 np->optype = NSC_DOUBLE;
104 } else if (rgt_type == NSC_DOUBLE) {
105 if (!nstr_coerce_val(&np->lft, NSC_DOUBLE, str))
106 np->optype = NSC_DOUBLE;
108 if (!nstr_coerce_val(&np->lft, NSC_LONG, str)
109 && !nstr_coerce_val(&np->rgt, NSC_LONG, str))
110 np->optype = NSC_LONG;
113 /* another condition? */
117 pr("%s -- expected `&'\n", cond);
124 /* could just return I and let caller gripe or enlarge buffer */
125 pr("%s -- too many conditions\n", str);
132 #define EVAL(op, lft, rgt) \
133 ((op) == '<' ? (lft) < (rgt) \
134 : (op) == '=' ? (lft) == (rgt) \
135 : (op) == '>' ? (lft) > (rgt) \
136 : (op) == '#' ? (lft) != (rgt) \
140 * Evaluate compiled conditions in array NP[NCOND].
141 * Return non-zero iff they are all true.
142 * PTR points to a context object of the type that was used to compile
146 nstr_exec(struct nscstr *np, int ncond, void *ptr)
149 struct valstr lft, rgt;
151 for (i = 0; i < ncond; ++i) {
153 optype = np[i].optype;
154 if (np[i].lft.val_cat == NSC_NOCAT || np[i].rgt.val_cat == NSC_NOCAT)
157 nstr_exec_val(&lft, player->cnum, ptr, optype);
159 nstr_exec_val(&rgt, player->cnum, ptr, optype);
163 if (!EVAL(op, lft.val_as.lng, rgt.val_as.lng))
167 if (!EVAL(op, lft.val_as.dbl, rgt.val_as.dbl))
171 return 0; /* FIXME */
173 CANT_HAPPEN("bad OPTYPE");
182 * Compile a value in STR into VAL.
183 * Return a pointer to the first character after the value on success,
185 * TYPE is the context type, a file type.
188 nstr_comp_val(char *str, struct valstr*val, int type)
198 val->val_type = NSC_NOTYPE;
199 val->val_cat = NSC_NOCAT;
200 val->val_as_type = -1;
202 if (isalpha(str[0])) {
204 for (i = 0; isalnum(str[i]) || str[i] == '_'; ++i) {
205 if (i < sizeof(id) - 1)
209 if (i < sizeof(id)) {
212 val->val_as_type = typematch(id, type);
214 cap = ef_cadef(type);
216 j = stmtch(id, cap, offsetof(struct castr, ca_name),
217 sizeof(struct castr));
219 && (!(cap[j].ca_flags & NSC_DEITY) || player->god)) {
220 val->val_type = cap[j].ca_type;
221 val->val_cat = NSC_OFF;
222 val->val_as.off = cap[j].ca_off;
229 if (val->val_type == NSC_NOTYPE) {
230 if (val->val_as_type >= 0) {
231 val->val_type = NSC_TYPEID;
232 val->val_cat = NSC_VAL;
233 val->val_as.lng = val->val_as_type;
235 pr("%s -- selector access denied\n", id);
236 else if (j == M_NOTUNIQUE)
237 pr("%s -- ambiguous selector name\n", id);
239 pr("%s -- unknown selector name\n", id);
242 return val->val_type == NSC_NOTYPE ? NULL : tail;
246 l = strtol(str, &tail, 0);
247 d = strtod(str, &tail2);
249 val->val_type = NSC_DOUBLE;
250 val->val_cat = NSC_VAL;
255 val->val_type = NSC_LONG;
256 val->val_cat = NSC_VAL;
260 /* FIXME NSC_STRING */
262 /* single character type */
265 val->val_as_type = typematch(id, type);
266 if (val->val_as_type >= 0) {
267 val->val_type = NSC_TYPEID;
268 val->val_cat = NSC_VAL;
269 val->val_as.lng = val->val_as_type;
273 pr("%s -- invalid value for condition\n", str);
279 * If VALTYPE is an integer type, return NSC_LONG.
280 * If VALTYPE is a floating-point type, return NSC_DOUBLE.
281 * If VALTYPE is NSC_NOTYPE, NSC_STRING or NSC_TYPEID, return VALTYPE.
284 nstr_promote(int valtype)
304 valtype = NSC_DOUBLE;
307 CANT_HAPPEN("bad VALTYPE");
308 valtype = NSC_NOTYPE;
314 cond_type_mismatch(char *str)
317 pr("%s -- condition type mismatch\n", str);
322 * Coerce VAL to promoted value type TO.
323 * Return 0 on success, -1 on error.
324 * If VAL is evaluated, convert it, else only check.
325 * STR is the condition text to be used for error messages. Suppress
326 * messages if it is a null pointer.
329 nstr_coerce_val(struct valstr *val, nsc_type to, char *str)
331 /* FIXME get rid of promotion? */
332 nsc_type from = nstr_promote(val->val_type);
334 if (from == NSC_NOTYPE)
340 if (val->val_as_type >= 0) {
341 val->val_cat = NSC_VAL;
342 val->val_as.lng = val->val_as_type;
344 return cond_type_mismatch(str);
347 return cond_type_mismatch(str); /* FIXME */
349 if (from == NSC_LONG) {
350 if (val->val_cat == NSC_VAL)
351 val->val_as.dbl = val->val_as.lng;
353 return cond_type_mismatch(str);
356 return cond_type_mismatch(str);
358 CANT_HAPPEN("bad TO argument");
363 if (val->val_cat == NSC_VAL) {
364 /* coord literals don't occur, conversion not implemented */
365 CANT_HAPPEN(val->val_type == NSC_XCOORD
366 || val->val_type == NSC_YCOORD);
375 * Use coordinate system of country CNUM.
376 * PTR points to a context object of the type that was used to compile
378 * If WANT is not zero, coerce the value to promoted value type WANT.
379 * VAL must be coercible. That's the case if a previous
380 * nstr_coerce_val(VAL, WANT, STR) succeeded.
383 nstr_exec_val(struct valstr *val, natid cnum, void *ptr, nsc_type want)
386 nsc_type valtype = NSC_LONG;
388 switch (val->val_cat) {
390 CANT_HAPPEN("Bad VAL category");
393 valtype = val->val_type;
397 memb_ptr += val->val_as.off;
398 switch (val->val_type) {
400 val->val_as.lng = *(signed char *)memb_ptr;
403 val->val_as.lng = *(unsigned char *)memb_ptr;
406 val->val_as.lng = *(short *)memb_ptr;
409 val->val_as.lng = *(unsigned short *)memb_ptr;
412 val->val_as.lng = *(int *)memb_ptr;
415 val->val_as.lng = *(long *)memb_ptr;
418 val->val_as.lng = xrel(getnatp(cnum), *(short *)memb_ptr);
421 val->val_as.lng = yrel(getnatp(cnum), *(short *)memb_ptr);
424 val->val_as.dbl = *(float *)memb_ptr;
425 valtype = NSC_DOUBLE;
428 val->val_as.dbl = *(double *)memb_ptr;
429 valtype = NSC_DOUBLE;
432 val->val_as.str = *(char **)memb_ptr;
433 valtype = NSC_STRING;
436 val->val_as.lng = *(time_t *)memb_ptr;
439 val->val_as.lng = *(signed char *)memb_ptr;
440 valtype = NSC_TYPEID;
443 CANT_HAPPEN("Bad VAL type");
451 else if (want == NSC_DOUBLE) {
452 if (valtype == NSC_LONG) {
454 val->val_as.dbl = val->val_as.lng;
456 } else if (want == NSC_STRING)
458 if (CANT_HAPPEN(valtype != want)) {
462 case NSC_LONG: val->val_as.lng = 0; break;
463 case NSC_DOUBLE: val->val_as.dbl = 0.0; break;
464 case NSC_STRING: val->val_as.str = ""; break;
466 CANT_HAPPEN("bad WANT argument");
471 val->val_type = valtype;