2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2014, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire 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 3 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, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * nstreval.c: evaluate compiled values
29 * Known contributors to this file:
32 * Markus Armbruster, 2004-2014
45 * Initialize VAL to symbolic value for selector CA with index IDX.
49 nstr_mksymval(struct valstr *val, struct castr *ca, int idx)
51 val->val_type = ca->ca_type;
52 val->val_cat = NSC_OFF;
53 val->val_as.sym.off = ca->ca_off;
54 val->val_as.sym.len = ca->ca_len;
55 val->val_as.sym.idx = idx;
56 val->val_as.sym.get = ca->ca_get;
62 * If VAL has category NSC_OFF, read the value from the context object
63 * PTR, and translate it for country CNUM (coordinate system and
64 * contact status). No translation when CNUM is NATID_BAD.
65 * PTR points to a context object of the type that was used to compile
67 * Unless WANT is NSC_NOTYPE, coerce the value to promoted value type
68 * WANT. VAL must be coercible.
69 * The result's type is promoted on success, NSC_NOTYPE on error.
70 * In either case, the category is NSC_VAL.
74 nstr_eval(struct valstr *val, natid cnum, void *ptr, enum nsc_type want)
77 enum nsc_type valtype;
82 if (CANT_HAPPEN(want != NSC_NOTYPE && !NSC_IS_PROMOTED(want)))
83 want = nstr_promote(want);
85 switch (val->val_cat) {
87 valtype = val->val_type;
88 if (CANT_HAPPEN(!NSC_IS_PROMOTED(valtype)))
89 valtype = nstr_promote(valtype);
92 if (val->val_as.sym.get) {
95 ptr = val->val_as.sym.get(val, natp, ptr);
96 } while (ptr && val->val_as.sym.get);
98 valtype = val->val_type;
99 val->val_cat = NSC_VAL;
106 memb_ptr += val->val_as.sym.off;
107 idx = val->val_as.sym.idx;
108 switch (val->val_type) {
110 val->val_as.lng = ((signed char *)memb_ptr)[idx];
113 val->val_as.lng = ((unsigned char *)memb_ptr)[idx];
116 val->val_as.lng = ((short *)memb_ptr)[idx];
119 val->val_as.lng = ((unsigned short *)memb_ptr)[idx];
122 val->val_as.lng = ((int *)memb_ptr)[idx];
125 val->val_as.lng = ((long *)memb_ptr)[idx];
128 c = ((short *)memb_ptr)[idx];
129 if (cnum == NATID_BAD) {
130 /* FIXME use variant of xrel() that takes orig instead of nation */
131 if (c >= WORLD_X / 2)
134 c = xrel(getnatp(cnum), c);
138 c = ((short *)memb_ptr)[idx];
139 if (cnum == NATID_BAD) {
140 /* FIXME use variant of yrel() that takes orig instead of nation */
141 if (c >= WORLD_Y / 2)
144 c = yrel(getnatp(cnum), c);
148 val->val_as.lng = -1;
149 if (CANT_HAPPEN(((struct natstr *)ptr)->ef_type != EF_NATION))
151 if (opt_HIDDEN && cnum != NATID_BAD) {
152 natp = getnatp(cnum);
153 if (natp->nat_stat != STAT_GOD
154 && !(getcontact(natp, idx) && getcontact(ptr, idx)))
157 val->val_as.lng = ((unsigned char *)memb_ptr)[idx];
160 val->val_as.dbl = ((float *)memb_ptr)[idx];
161 valtype = NSC_DOUBLE;
164 val->val_as.dbl = ((double *)memb_ptr)[idx];
165 valtype = NSC_DOUBLE;
169 val->val_as.str.maxsz = val->val_as.sym.len;
170 val->val_as.str.base = (char *)memb_ptr;
171 valtype = NSC_STRING;
174 val->val_as.str.base = ((char **)memb_ptr)[idx];
175 val->val_as.str.maxsz = INT_MAX;
176 /* really SIZE_MAX, but that's C99 */
177 valtype = NSC_STRING;
180 val->val_as.lng = ((time_t *)memb_ptr)[idx];
184 valtype = NSC_NOTYPE;
186 val->val_cat = NSC_VAL;
190 valtype = NSC_NOTYPE;
196 else if (want == NSC_DOUBLE) {
197 if (valtype == NSC_LONG) {
199 val->val_as.dbl = val->val_as.lng;
203 if (CANT_HAPPEN(valtype != want && want != NSC_NOTYPE))
204 valtype = NSC_NOTYPE;
206 val->val_type = valtype;
212 * If VALTYPE is an integer type, return NSC_LONG.
213 * If VALTYPE is a floating-point type, return NSC_DOUBLE.
214 * If VALTYPE is a string type, return NSC_STRING.
217 nstr_promote(int valtype)
236 valtype = NSC_DOUBLE;
239 valtype = NSC_STRING;
243 valtype = NSC_NOTYPE;
249 symbol_by_value(int key, struct symbol *table)
253 for (i = 0; table[i].name; i++)
254 if (key == table[i].value)
255 return table[i].name;