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)
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 CANT_HAPPEN("unimplemented OPTYPE"); /* FIXME */
177 CANT_HAPPEN("bad OPTYPE");
186 * Compile a value in STR into VAL.
187 * Return a pointer to the first character after the value on success,
189 * TYPE is the context type, a file type.
192 nstr_comp_val(char *str, struct valstr*val, int type)
202 val->val_type = NSC_NOTYPE;
203 val->val_cat = NSC_NOCAT;
204 val->val_as_type = -1;
206 if (isalpha(str[0])) {
208 for (i = 0; isalnum(str[i]) || str[i] == '_'; ++i) {
209 if (i < sizeof(id) - 1)
213 if (i < sizeof(id)) {
216 val->val_as_type = typematch(id, type);
218 cap = ef_cadef(type);
220 j = stmtch(id, cap, offsetof(struct castr, ca_name),
221 sizeof(struct castr));
223 && (!(cap[j].ca_flags & NSC_DEITY) || player->god)) {
224 val->val_type = cap[j].ca_type;
225 val->val_cat = NSC_OFF;
226 val->val_as.off = cap[j].ca_off;
233 if (val->val_type == NSC_NOTYPE) {
234 if (val->val_as_type >= 0) {
235 val->val_type = NSC_TYPEID;
236 val->val_cat = NSC_VAL;
237 val->val_as.lng = val->val_as_type;
239 pr("%s -- selector access denied\n", id);
240 else if (j == M_NOTUNIQUE)
241 pr("%s -- ambiguous selector name\n", id);
243 pr("%s -- unknown selector name\n", id);
246 return val->val_type == NSC_NOTYPE ? NULL : tail;
249 /* single character type */
252 val->val_as_type = typematch(id, type);
255 l = strtol(str, &tail, 0);
256 d = strtod(str, &tail2);
258 val->val_type = NSC_DOUBLE;
259 val->val_cat = NSC_VAL;
264 val->val_type = NSC_LONG;
265 val->val_cat = NSC_VAL;
269 /* FIXME implement NSC_STRING literals */
271 CANT_HAPPEN(val->val_type != NSC_NOTYPE);
272 if (val->val_as_type >= 0) {
273 val->val_type = NSC_TYPEID;
274 val->val_cat = NSC_VAL;
275 val->val_as.lng = val->val_as_type;
279 pr("%s -- invalid value for condition\n", str);
285 * If VALTYPE is an integer type, return NSC_LONG.
286 * If VALTYPE is a floating-point type, return NSC_DOUBLE.
287 * If VALTYPE is NSC_NOTYPE, NSC_STRING or NSC_TYPEID, return VALTYPE.
290 nstr_promote(int valtype)
310 valtype = NSC_DOUBLE;
313 CANT_HAPPEN("bad VALTYPE");
314 valtype = NSC_NOTYPE;
320 cond_type_mismatch(char *str)
323 pr("%s -- condition operand type mismatch\n", str);
328 * Coerce VAL to promoted value type TO.
329 * Return 0 on success, -1 on error.
330 * If VAL is evaluated, convert it, else only check.
331 * STR is the condition text to be used for error messages. Suppress
332 * messages if it is a null pointer.
335 nstr_coerce_val(struct valstr *val, nsc_type to, char *str)
337 /* FIXME get rid of promotion? */
338 nsc_type from = nstr_promote(val->val_type);
340 if (from == NSC_NOTYPE)
346 if (val->val_as_type >= 0) {
347 val->val_cat = NSC_VAL;
348 val->val_as.lng = val->val_as_type;
350 return cond_type_mismatch(str);
353 return cond_type_mismatch(str); /* FIXME implement */
355 if (from == NSC_LONG) {
356 if (val->val_cat == NSC_VAL)
357 val->val_as.dbl = val->val_as.lng;
359 return cond_type_mismatch(str);
362 return cond_type_mismatch(str);
364 CANT_HAPPEN("bad TO argument");
369 if (val->val_cat == NSC_VAL) {
370 /* coord literals don't occur, conversion not implemented */
371 CANT_HAPPEN(val->val_type == NSC_XCOORD
372 || val->val_type == NSC_YCOORD);
381 * Use coordinate system of country CNUM.
382 * PTR points to a context object of the type that was used to compile
384 * If WANT is not zero, coerce the value to promoted value type WANT.
385 * VAL must be coercible. That's the case if a previous
386 * nstr_coerce_val(VAL, WANT, STR) succeeded.
389 nstr_exec_val(struct valstr *val, natid cnum, void *ptr, nsc_type want)
392 nsc_type valtype = NSC_LONG;
394 switch (val->val_cat) {
396 CANT_HAPPEN("Bad VAL category");
399 valtype = val->val_type;
403 memb_ptr += val->val_as.off;
404 switch (val->val_type) {
406 val->val_as.lng = *(signed char *)memb_ptr;
409 val->val_as.lng = *(unsigned char *)memb_ptr;
412 val->val_as.lng = *(short *)memb_ptr;
415 val->val_as.lng = *(unsigned short *)memb_ptr;
418 val->val_as.lng = *(int *)memb_ptr;
421 val->val_as.lng = *(long *)memb_ptr;
424 val->val_as.lng = xrel(getnatp(cnum), *(short *)memb_ptr);
427 val->val_as.lng = yrel(getnatp(cnum), *(short *)memb_ptr);
430 val->val_as.dbl = *(float *)memb_ptr;
431 valtype = NSC_DOUBLE;
434 val->val_as.dbl = *(double *)memb_ptr;
435 valtype = NSC_DOUBLE;
438 val->val_as.str = *(char **)memb_ptr;
439 valtype = NSC_STRING;
442 val->val_as.lng = *(time_t *)memb_ptr;
445 val->val_as.lng = *(signed char *)memb_ptr;
446 valtype = NSC_TYPEID;
449 CANT_HAPPEN("Bad VAL type");
457 else if (want == NSC_DOUBLE) {
458 if (valtype == NSC_LONG) {
460 val->val_as.dbl = val->val_as.lng;
462 } else if (want == NSC_STRING)
463 CANT_HAPPEN("unimplemented WANT"); /* FIXME */
464 if (CANT_HAPPEN(valtype != want)) {
468 case NSC_LONG: val->val_as.lng = 0; break;
469 case NSC_DOUBLE: val->val_as.dbl = 0.0; break;
470 case NSC_STRING: val->val_as.str = ""; break;
472 CANT_HAPPEN("bad WANT argument");
477 val->val_type = valtype;