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 np->optype = NSC_NOTYPE;
90 if (lft_type == NSC_TYPEID) {
91 if (!nstr_coerce_val(&np->rgt, NSC_TYPEID, str))
92 np->optype = NSC_TYPEID;
93 } else if (rgt_type == NSC_TYPEID) {
94 if (!nstr_coerce_val(&np->lft, NSC_TYPEID, str))
95 np->optype = NSC_TYPEID;
96 } else if (lft_type == NSC_STRING) {
97 if (!nstr_coerce_val(&np->rgt, NSC_STRING, str))
98 np->optype = NSC_STRING;
99 } else if (rgt_type == NSC_STRING) {
100 if (!nstr_coerce_val(&np->lft, NSC_STRING, str))
101 np->optype = NSC_STRING;
102 } else if (lft_type == NSC_DOUBLE) {
103 if (!nstr_coerce_val(&np->rgt, NSC_DOUBLE, str))
104 np->optype = NSC_DOUBLE;
105 } else if (rgt_type == NSC_DOUBLE) {
106 if (!nstr_coerce_val(&np->lft, NSC_DOUBLE, str))
107 np->optype = NSC_DOUBLE;
109 if (!nstr_coerce_val(&np->lft, NSC_LONG, str)
110 && !nstr_coerce_val(&np->rgt, NSC_LONG, str))
111 np->optype = NSC_LONG;
113 if (np->optype == NSC_NOTYPE)
116 /* another condition? */
120 pr("%s -- expected `&'\n", cond);
127 /* could just return I and let caller gripe or enlarge buffer */
128 pr("%s -- too many conditions\n", str);
135 #define EVAL(op, lft, rgt) \
136 ((op) == '<' ? (lft) < (rgt) \
137 : (op) == '=' ? (lft) == (rgt) \
138 : (op) == '>' ? (lft) > (rgt) \
139 : (op) == '#' ? (lft) != (rgt) \
143 * Evaluate compiled conditions in array NP[NCOND].
144 * Return non-zero iff they are all true.
145 * PTR points to a context object of the type that was used to compile
149 nstr_exec(struct nscstr *np, int ncond, void *ptr)
151 int i, op, optype, cmp;
152 struct valstr lft, rgt;
154 for (i = 0; i < ncond; ++i) {
156 optype = np[i].optype;
157 if (np[i].lft.val_cat == NSC_NOCAT || np[i].rgt.val_cat == NSC_NOCAT)
160 nstr_exec_val(&lft, player->cnum, ptr, optype);
162 nstr_exec_val(&rgt, player->cnum, ptr, optype);
166 if (!EVAL(op, lft.val_as.lng, rgt.val_as.lng))
170 if (!EVAL(op, lft.val_as.dbl, rgt.val_as.dbl))
174 cmp = strcmp(lft.val_as.str, rgt.val_as.str);
175 if (!EVAL(op, cmp, 0))
179 CANT_HAPPEN("bad OPTYPE");
188 * Compile a value in STR into VAL.
189 * Return a pointer to the first character after the value on success,
191 * TYPE is the context type, a file type.
192 * If STR names an array, VAL simply refers to the element with index
196 nstr_comp_val(char *str, struct valstr*val, int type)
206 val->val_type = NSC_NOTYPE;
207 val->val_cat = NSC_NOCAT;
208 val->val_as_type = -1;
210 if (isalpha(str[0])) {
212 for (i = 0; isalnum(str[i]) || str[i] == '_'; ++i) {
213 if (i < sizeof(id) - 1)
217 if (i < sizeof(id)) {
220 val->val_as_type = typematch(id, type);
222 cap = ef_cadef(type);
224 j = stmtch(id, cap, offsetof(struct castr, ca_name),
225 sizeof(struct castr));
227 && (!(cap[j].ca_flags & NSC_DEITY) || player->god)) {
228 if (cap[j].ca_type == NSC_TYPEID && val->val_as_type >= 0)
230 * Got two matches of type NSC_TYPEID, need to
231 * choose. Prefer typematch(), because ?des=n
232 * would be interpreted as ?des=newdes
237 val->val_type = cap[j].ca_type;
238 val->val_cat = NSC_OFF;
239 val->val_as.sym.off = cap[j].ca_off;
240 val->val_as.sym.idx = 0;
248 if (val->val_type == NSC_NOTYPE) {
249 if (val->val_as_type >= 0) {
250 val->val_type = NSC_TYPEID;
251 val->val_cat = NSC_VAL;
252 val->val_as.lng = val->val_as_type;
254 pr("%s -- selector access denied\n", id);
255 else if (j == M_NOTUNIQUE)
256 pr("%s -- ambiguous selector name\n", id);
258 pr("%s -- unknown selector name\n", id);
261 return val->val_type == NSC_NOTYPE ? NULL : tail;
264 /* single character type */
267 val->val_as_type = typematch(id, type);
270 l = strtol(str, &tail, 0);
271 d = strtod(str, &tail2);
273 val->val_type = NSC_DOUBLE;
274 val->val_cat = NSC_VAL;
279 val->val_type = NSC_LONG;
280 val->val_cat = NSC_VAL;
284 /* FIXME implement NSC_STRING literals */
286 CANT_HAPPEN(val->val_type != NSC_NOTYPE);
287 if (val->val_as_type >= 0) {
288 val->val_type = NSC_TYPEID;
289 val->val_cat = NSC_VAL;
290 val->val_as.lng = val->val_as_type;
294 pr("%s -- invalid value for condition\n", str);
300 * If VALTYPE is an integer type, return NSC_LONG.
301 * If VALTYPE is a floating-point type, return NSC_DOUBLE.
302 * If VALTYPE is NSC_STRINGY, return NSC_STRING.
303 * If VALTYPE is NSC_NOTYPE, NSC_STRING or NSC_TYPEID, return VALTYPE.
306 nstr_promote(int valtype)
326 valtype = NSC_DOUBLE;
329 valtype = NSC_STRING;
332 CANT_HAPPEN("bad VALTYPE");
333 valtype = NSC_NOTYPE;
339 cond_type_mismatch(char *str)
342 pr("%s -- condition operand type mismatch\n", str);
347 * Coerce VAL to promoted value type TO.
348 * Return 0 on success, -1 on error.
349 * If VAL is evaluated, convert it, else only check.
350 * STR is the condition text to be used for error messages. Suppress
351 * messages if it is a null pointer.
354 nstr_coerce_val(struct valstr *val, nsc_type to, char *str)
356 /* FIXME get rid of promotion? */
357 nsc_type from = nstr_promote(val->val_type);
359 if (from == NSC_NOTYPE)
365 if (val->val_as_type >= 0) {
366 val->val_cat = NSC_VAL;
367 val->val_as.lng = val->val_as_type;
369 return cond_type_mismatch(str);
372 return cond_type_mismatch(str); /* FIXME implement */
374 if (from == NSC_LONG) {
375 if (val->val_cat == NSC_VAL)
376 val->val_as.dbl = val->val_as.lng;
378 return cond_type_mismatch(str);
381 return cond_type_mismatch(str);
383 CANT_HAPPEN("bad TO argument");
388 if (val->val_cat == NSC_VAL) {
389 /* coord literals don't occur, conversion not implemented */
390 CANT_HAPPEN(val->val_type == NSC_XCOORD
391 || val->val_type == NSC_YCOORD);
400 * If VAL is symbolic, evaluate it into a promoted value type.
401 * Use coordinate system of country CNUM.
402 * PTR points to a context object of the type that was used to compile
404 * Unless WANT is NSC_NOTYPE, coerce the value to promoted value type
405 * WANT. VAL must be coercible. That's the case if a previous
406 * nstr_coerce_val(VAL, WANT, STR) succeeded.
409 nstr_exec_val(struct valstr *val, natid cnum, void *ptr, nsc_type want)
412 nsc_type valtype = NSC_LONG;
415 switch (val->val_cat) {
417 CANT_HAPPEN("Bad VAL category");
420 valtype = val->val_type;
424 memb_ptr += val->val_as.sym.off;
425 idx = val->val_as.sym.idx;
426 switch (val->val_type) {
428 val->val_as.lng = ((signed char *)memb_ptr)[idx];
431 val->val_as.lng = ((unsigned char *)memb_ptr)[idx];
434 val->val_as.lng = ((short *)memb_ptr)[idx];
437 val->val_as.lng = ((unsigned short *)memb_ptr)[idx];
440 val->val_as.lng = ((int *)memb_ptr)[idx];
443 val->val_as.lng = ((long *)memb_ptr)[idx];
446 val->val_as.lng = xrel(getnatp(cnum), ((short *)memb_ptr)[idx]);
449 val->val_as.lng = yrel(getnatp(cnum), ((short *)memb_ptr)[idx]);
452 val->val_as.dbl = ((float *)memb_ptr)[idx];
453 valtype = NSC_DOUBLE;
456 val->val_as.dbl = ((double *)memb_ptr)[idx];
457 valtype = NSC_DOUBLE;
461 val->val_as.str = (char *)memb_ptr;
462 valtype = NSC_STRING;
465 val->val_as.str = ((char **)memb_ptr)[idx];
466 valtype = NSC_STRING;
469 val->val_as.lng = ((time_t *)memb_ptr)[idx];
472 val->val_as.lng = ((signed char *)memb_ptr)[idx];
473 valtype = NSC_TYPEID;
476 CANT_HAPPEN("Bad VAL type");
483 else if (want == NSC_DOUBLE) {
484 if (valtype == NSC_LONG) {
486 val->val_as.dbl = val->val_as.lng;
488 } else if (want == NSC_STRING)
489 CANT_HAPPEN("unimplemented WANT"); /* FIXME */
490 if (CANT_HAPPEN(valtype != want && want != NSC_NOTYPE)) {
494 case NSC_LONG: val->val_as.lng = 0; break;
495 case NSC_DOUBLE: val->val_as.dbl = 0.0; break;
496 case NSC_STRING: val->val_as.str = ""; break;
498 CANT_HAPPEN("bad WANT argument");
502 val->val_type = valtype;