2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2016, 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-2015
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;
57 val->val_as.sym.hidden = ca->ca_flags & NSC_HIDDEN;
63 * If @val has category NSC_OFF, read the value from the context object
64 * @ptr, and translate it for country @cnum (coordinate system and
65 * contact status). No translation when @cnum is NATID_BAD.
66 * @ptr points to a context object of the type that was used to compile
68 * Unless @want is NSC_NOTYPE, coerce the value to promoted value type
69 * @want. @val must be coercible.
70 * The result's type is promoted on success, NSC_NOTYPE on error.
71 * In either case, the category is NSC_VAL.
75 nstr_eval(struct valstr *val, natid cnum, void *ptr, enum nsc_type want)
78 enum nsc_type valtype;
83 if (CANT_HAPPEN(want != NSC_NOTYPE && !NSC_IS_PROMOTED(want)))
84 want = nstr_promote(want);
86 switch (val->val_cat) {
88 valtype = val->val_type;
89 if (CANT_HAPPEN(!NSC_IS_PROMOTED(valtype)))
90 valtype = nstr_promote(valtype);
93 if (val->val_as.sym.get) {
96 ptr = val->val_as.sym.get(val, natp, ptr);
97 } while (ptr && val->val_as.sym.get);
99 valtype = val->val_type;
100 val->val_cat = NSC_VAL;
107 memb_ptr += val->val_as.sym.off;
108 idx = val->val_as.sym.idx;
109 hidden = val->val_as.sym.hidden;
110 val->val_cat = NSC_VAL;
111 switch (val->val_type) {
113 val->val_as.lng = ((signed char *)memb_ptr)[idx];
116 val->val_as.lng = ((unsigned char *)memb_ptr)[idx];
119 val->val_as.lng = ((short *)memb_ptr)[idx];
122 val->val_as.lng = ((unsigned short *)memb_ptr)[idx];
125 val->val_as.lng = ((int *)memb_ptr)[idx];
128 val->val_as.lng = ((long *)memb_ptr)[idx];
131 c = ((short *)memb_ptr)[idx];
132 if (cnum == NATID_BAD) {
133 /* FIXME use variant of xrel() that takes orig instead of nation */
134 if (c >= WORLD_X / 2)
137 c = xrel(getnatp(cnum), c);
141 c = ((short *)memb_ptr)[idx];
142 if (cnum == NATID_BAD) {
143 /* FIXME use variant of yrel() that takes orig instead of nation */
144 if (c >= WORLD_Y / 2)
147 c = yrel(getnatp(cnum), c);
151 val->val_as.dbl = ((float *)memb_ptr)[idx];
152 valtype = NSC_DOUBLE;
155 val->val_as.dbl = ((double *)memb_ptr)[idx];
156 valtype = NSC_DOUBLE;
160 val->val_as.str.maxsz = val->val_as.sym.len;
161 val->val_as.str.base = (char *)memb_ptr;
162 valtype = NSC_STRING;
165 val->val_as.str.base = ((char **)memb_ptr)[idx];
166 val->val_as.str.maxsz = INT_MAX;
167 /* really SIZE_MAX, but that's C99 */
168 valtype = NSC_STRING;
171 val->val_as.lng = ((time_t *)memb_ptr)[idx];
175 valtype = NSC_NOTYPE;
179 if (CANT_HAPPEN(hidden && valtype != NSC_LONG))
180 break; /* not implemented */
181 if (CANT_HAPPEN(((struct natstr *)ptr)->ef_type != EF_NATION))
182 break; /* only defined for nation selectors */
183 if (!opt_HIDDEN || cnum == NATID_BAD)
185 natp = getnatp(cnum);
186 if (natp->nat_stat != STAT_GOD
187 && !(in_contact(natp, idx) && in_contact(ptr, idx)))
188 val->val_as.lng = -1;
193 valtype = NSC_NOTYPE;
199 else if (want == NSC_DOUBLE) {
200 if (valtype == NSC_LONG) {
202 val->val_as.dbl = val->val_as.lng;
206 if (CANT_HAPPEN(valtype != want && want != NSC_NOTYPE))
207 valtype = NSC_NOTYPE;
209 val->val_type = valtype;
215 * If @valtype is an integer type, return NSC_LONG.
216 * If @valtype is a floating-point type, return NSC_DOUBLE.
217 * If @valtype is a string type, return NSC_STRING.
220 nstr_promote(int valtype)
238 valtype = NSC_DOUBLE;
241 valtype = NSC_STRING;
245 valtype = NSC_NOTYPE;
251 symbol_by_value(int key, struct symbol *table)
255 for (i = 0; table[i].name; i++)
256 if (key == table[i].value)
257 return table[i].name;
263 symbol_set_fmt(char *buf, size_t sz, int flags, struct symbol symtab[],
273 for (i = 0; i < 32; i++) {
274 if (!(flags & bit(i)))
276 p = symbol_by_value(bit(i), symtab);
278 n += snprintf(buf + n, sz - n, "%s%s", pfx, p);
280 n += snprintf(buf + n, sz - n, "%s#%d", pfx, i);
286 CANT_HAPPEN((size_t)n >= sz && buf);