2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2008, 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 files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * nstreval.c: evaluate compiled values
30 * Known contributors to this file:
33 * Markus Armbruster, 2004-2006
48 * If VAL is symbolic, evaluate it into a promoted value type.
49 * Use country CNUM's coordinate system and access control.
50 * PTR points to a context object of the type that was used to compile
52 * Unless WANT is NSC_NOTYPE, coerce the value to promoted value type
53 * WANT. VAL must be coercible. That's the case if a previous
54 * nstr_coerce_val(VAL, WANT, STR) succeeded.
57 nstr_exec_val(struct valstr *val, natid cnum, void *ptr, nsc_type want)
64 if (CANT_HAPPEN(want != NSC_NOTYPE && !NSC_IS_PROMOTED(want)))
65 want = nstr_promote(want);
67 switch (val->val_cat) {
69 valtype = val->val_type;
74 memb_ptr += val->val_as.sym.off;
75 idx = val->val_as.sym.idx;
76 switch (val->val_type) {
78 val->val_as.lng = ((signed char *)memb_ptr)[idx];
81 val->val_as.lng = ((unsigned char *)memb_ptr)[idx];
84 val->val_as.lng = ((short *)memb_ptr)[idx];
87 val->val_as.lng = ((unsigned short *)memb_ptr)[idx];
90 val->val_as.lng = ((int *)memb_ptr)[idx];
93 val->val_as.lng = ((long *)memb_ptr)[idx];
96 val->val_as.lng = xrel(getnatp(cnum), ((short *)memb_ptr)[idx]);
99 val->val_as.lng = yrel(getnatp(cnum), ((short *)memb_ptr)[idx]);
102 val->val_as.lng = -1;
103 if (CANT_HAPPEN(((struct natstr *)ptr)->ef_type != EF_NATION))
105 natp = getnatp(cnum);
107 || natp->nat_stat == STAT_GOD
108 || (getcontact(natp, idx) && getcontact(ptr, idx)))
109 val->val_as.lng = ((unsigned char *)memb_ptr)[idx];
112 val->val_as.dbl = ((float *)memb_ptr)[idx];
113 valtype = NSC_DOUBLE;
116 val->val_as.dbl = ((double *)memb_ptr)[idx];
117 valtype = NSC_DOUBLE;
121 val->val_as.str.maxsz = val->val_as.sym.len;
122 val->val_as.str.base = (char *)memb_ptr;
123 valtype = NSC_STRING;
126 val->val_as.str.base = ((char **)memb_ptr)[idx];
127 val->val_as.str.maxsz = INT_MAX;
128 valtype = NSC_STRING;
131 val->val_as.lng = ((time_t *)memb_ptr)[idx];
137 val->val_cat = NSC_VAL;
141 valtype = NSC_NOTYPE;
146 else if (want == NSC_DOUBLE) {
147 if (valtype == NSC_LONG) {
149 val->val_as.dbl = val->val_as.lng;
151 } else if (want == NSC_STRING)
152 CANT_REACH(); /* FIXME implement */
154 if (CANT_HAPPEN(valtype != want && want != NSC_NOTYPE)) {
155 /* make up an error value */
157 memset(&val->val_as, 0, sizeof(val->val_as));
160 val->val_type = valtype;
166 * If VALTYPE is an integer type, return NSC_LONG.
167 * If VALTYPE is a floating-point type, return NSC_DOUBLE.
168 * If VALTYPE is a string type, return NSC_STRING.
171 nstr_promote(int valtype)
190 valtype = NSC_DOUBLE;
193 valtype = NSC_STRING;
197 valtype = NSC_NOTYPE;
203 symbol_by_value(int key, struct symbol *table)
207 for (i = 0; table[i].name; i++)
208 if (key == table[i].value)
209 return table[i].name;