]> git.pond.sub.org Git - empserver/blob - src/lib/common/nstreval.c
nsc: Rename nstr_exec_val() to nstr_eval() and tighten contract
[empserver] / src / lib / common / nstreval.c
1 /*
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
5  *
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.
10  *
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.
15  *
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/>.
18  *
19  *  ---
20  *
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.
24  *
25  *  ---
26  *
27  *  nstreval.c: evaluate compiled values
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1989
31  *     Steve McClure, 1997
32  *     Markus Armbruster, 2004-2014
33  */
34
35 #include <config.h>
36
37 #include <limits.h>
38 #include <string.h>
39 #include "file.h"
40 #include "nat.h"
41 #include "nsc.h"
42 #include "optlist.h"
43
44 /*
45  * Initialize VAL to symbolic value for selector CA with index IDX.
46  * Return VAL.
47  */
48 struct valstr *
49 nstr_mksymval(struct valstr *val, struct castr *ca, int idx)
50 {
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     return val;
58 }
59
60 /*
61  * Evaluate VAL.
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
66  * the value.
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.
71  * Return VAL.
72  */
73 struct valstr *
74 nstr_eval(struct valstr *val, natid cnum, void *ptr, enum nsc_type want)
75 {
76     char *memb_ptr;
77     enum nsc_type valtype;
78     int idx;
79     coord c;
80     struct natstr *natp;
81
82     if (CANT_HAPPEN(want != NSC_NOTYPE && !NSC_IS_PROMOTED(want)))
83         want = nstr_promote(want);
84
85     switch (val->val_cat) {
86     case NSC_VAL:
87         valtype = val->val_type;
88         if (CANT_HAPPEN(!NSC_IS_PROMOTED(valtype)))
89             valtype = nstr_promote(valtype);
90         break;
91     case NSC_OFF:
92         if (val->val_as.sym.get) {
93             natp = getnatp(cnum);
94             do {
95                 ptr = val->val_as.sym.get(val, natp, ptr);
96             } while (ptr && val->val_as.sym.get);
97             if (!ptr) {
98                 valtype = val->val_type;
99                 val->val_cat = NSC_VAL;
100                 break;
101             }
102         }
103
104         valtype = NSC_LONG;
105         memb_ptr = ptr;
106         memb_ptr += val->val_as.sym.off;
107         idx = val->val_as.sym.idx;
108         switch (val->val_type) {
109         case NSC_CHAR:
110             val->val_as.lng = ((signed char *)memb_ptr)[idx];
111             break;
112         case NSC_UCHAR:
113             val->val_as.lng = ((unsigned char *)memb_ptr)[idx];
114             break;
115         case NSC_SHORT:
116             val->val_as.lng = ((short *)memb_ptr)[idx];
117             break;
118         case NSC_USHORT:
119             val->val_as.lng = ((unsigned short *)memb_ptr)[idx];
120             break;
121         case NSC_INT:
122             val->val_as.lng = ((int *)memb_ptr)[idx];
123             break;
124         case NSC_LONG:
125             val->val_as.lng = ((long *)memb_ptr)[idx];
126             break;
127         case NSC_XCOORD:
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)
132                     c -= WORLD_X;
133             } else
134                 c = xrel(getnatp(cnum), c);
135             val->val_as.lng = c;
136             break;
137         case NSC_YCOORD:
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)
142                     c -= WORLD_Y;
143             } else
144                 c = yrel(getnatp(cnum), c);
145             val->val_as.lng = c;
146             break;
147         case NSC_HIDDEN:
148             val->val_as.lng = -1;
149             if (CANT_HAPPEN(((struct natstr *)ptr)->ef_type != EF_NATION))
150                 break;
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)))
155                     break;
156             }
157             val->val_as.lng = ((unsigned char *)memb_ptr)[idx];
158             break;
159         case NSC_FLOAT:
160             val->val_as.dbl = ((float *)memb_ptr)[idx];
161             valtype = NSC_DOUBLE;
162             break;
163         case NSC_DOUBLE:
164             val->val_as.dbl = ((double *)memb_ptr)[idx];
165             valtype = NSC_DOUBLE;
166             break;
167         case NSC_STRINGY:
168             CANT_HAPPEN(idx);
169             val->val_as.str.maxsz = val->val_as.sym.len;
170             val->val_as.str.base = (char *)memb_ptr;
171             valtype = NSC_STRING;
172             break;
173         case 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;
178             break;
179         case NSC_TIME:
180             val->val_as.lng = ((time_t *)memb_ptr)[idx];
181             break;
182         default:
183             CANT_REACH();
184             valtype = NSC_NOTYPE;
185         }
186         val->val_cat = NSC_VAL;
187         break;
188     default:
189         CANT_REACH();
190         valtype = NSC_NOTYPE;
191     }
192
193     /* coerce */
194     if (valtype == want)
195         ;
196     else if (want == NSC_DOUBLE) {
197         if (valtype == NSC_LONG) {
198             valtype = want;
199             val->val_as.dbl = val->val_as.lng;
200         }
201     }
202
203     if (CANT_HAPPEN(valtype != want && want != NSC_NOTYPE))
204         valtype = NSC_NOTYPE;
205
206     val->val_type = valtype;
207     return val;
208 }
209
210 /*
211  * Promote 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.
215  */
216 int
217 nstr_promote(int valtype)
218 {
219     switch (valtype) {
220     case NSC_LONG:
221     case NSC_DOUBLE:
222     case NSC_STRING:
223         break;
224     case NSC_CHAR:
225     case NSC_UCHAR:
226     case NSC_SHORT:
227     case NSC_USHORT:
228     case NSC_INT:
229     case NSC_XCOORD:
230     case NSC_YCOORD:
231     case NSC_HIDDEN:
232     case NSC_TIME:
233         valtype = NSC_LONG;
234         break;
235     case NSC_FLOAT:
236         valtype = NSC_DOUBLE;
237         break;
238     case NSC_STRINGY:
239         valtype = NSC_STRING;
240         break;
241     default:
242         CANT_REACH();
243         valtype = NSC_NOTYPE;
244     }
245     return valtype;
246 }
247
248 char *
249 symbol_by_value(int key, struct symbol *table)
250 {
251     int i;
252
253     for (i = 0; table[i].name; i++)
254         if (key == table[i].value)
255             return table[i].name;
256
257     return NULL;
258 }