]> git.pond.sub.org Git - empserver/blob - include/nsc.h
Update copyright notice
[empserver] / include / nsc.h
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2010, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
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.
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, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
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.
25  *
26  *  ---
27  *
28  *  nsc.h: Definitions for Empire conditionals
29  *
30  *  Known contributors to this file:
31  *     Dave Pare, 1989
32  *     Markus Armbruster, 2004-2008
33  */
34
35 #ifndef NSC_H
36 #define NSC_H
37
38 #include <stddef.h>
39 #include "misc.h"
40 #include "xy.h"
41
42 #define NS_LSIZE        128
43 #define NS_NCOND        16
44
45 /* Value type */
46 enum nsc_type {
47     NSC_NOTYPE,
48     /* promoted types */
49     NSC_LONG,                   /* long */
50     NSC_DOUBLE,                 /* double */
51     NSC_STRING,                 /* character string */
52     /* unpromoted types */
53     NSC_CHAR,                   /* signed char */
54     NSC_UCHAR,                  /* unsigned char */
55     NSC_SHORT,                  /* short */
56     NSC_USHORT,                 /* unsigned short */
57     NSC_INT,                    /* int */
58     NSC_XCOORD,                 /* coord that needs x conversion */
59     NSC_YCOORD,                 /* coord that needs y conversion */
60     NSC_HIDDEN,                 /* unsigned char in struct natstr that
61                                    may need hiding */
62     NSC_TIME,                   /* time_t */
63     NSC_FLOAT,                  /* float */
64     NSC_STRINGY,                /* char[] */
65     /* aliases, must match typedefs */
66     NSC_NATID = NSC_UCHAR       /* nation id */
67 };
68
69 /* Is TYPE a promoted value type?  */
70 #define NSC_IS_PROMOTED(type) (NSC_LONG <= (type) && (type) <= NSC_STRING)
71
72 /* Return nsc_type for a signed integer with the same size as TYPE.  */
73 #define NSC_SITYPE(type)                                \
74     (sizeof(type) == 1 ? NSC_CHAR                       \
75      : sizeof(type) == sizeof(short) ? NSC_SHORT        \
76      : sizeof(type) == sizeof(int) ? NSC_INT            \
77      : sizeof(type) == sizeof(long) ? NSC_LONG          \
78      : 1/0)
79
80 /* Value category */
81 enum nsc_cat {
82     NSC_NOCAT,
83     NSC_VAL,                    /* evaluated value */
84     NSC_OFF,                    /* symbolic value: at offset in object */
85     NSC_ID                      /* unresolved identifier (internal use) */
86 };
87
88 /*
89  * Value, possibly symbolic
90  *
91  * If type is NSC_NOTYPE, it's an error value.
92  * If category is NSC_VAL, the value is in val_as, and the type is a
93  * promoted type.
94  * If category is NSC_OFF, the value is in a context object, and
95  * val_as.sym specifies how to get it, as follows.
96  * If sym.get is null, and type is NSC_STRINGY, the value is an array
97  * of sym.len characters starting at sym.offs in the context object.
98  * sym.idx must be zero.
99  * Else if sym.get is null, and sym.len is zero, the value is in the
100  * context object at offset sym.off.  sym.idx must be zero.
101  * Else if sym.get is null, sym.len is non-zero, and the value has
102  * index sym.idx in an array of sym.len elements at offset sym.off in
103  * in the context object.  I.e. the value is at sym.off + sym.idx *
104  * SZ, where SZ is the size of the value.
105  * If sym.get is not null, you obtain the value by calling get() like
106  * VAL->get(VAL, NP, CTXO), where NP points to the country to use for
107  * coordinate translation and access control (null for none), and CTXO
108  * is the context object.  get() either returns a null pointer and
109  * sets VAL->val_as to the value, as appropriate for the type.  Or it
110  * returns another context object and sets VAL->val_as.sym for it.
111  */
112 struct valstr {
113     enum nsc_type val_type;     /* type of value */
114     enum nsc_cat val_cat;       /* category of value */
115     union {
116         struct {                /* cat NSC_OFF */
117             ptrdiff_t off;
118             int len;
119             int idx;
120             void *(*get)(struct valstr *, struct natstr *, void *);
121         } sym;
122         double dbl;             /* cat NSC_VAL, type NSC_DOUBLE */
123         struct {                /* cat NSC_VAL, type NSC_STRING, cat NSC_ID */
124             char *base;
125             size_t maxsz;
126         } str;
127         long lng;               /* cat NSC_VAL, type NSC_LONG */
128     } val_as;
129 };
130
131 /* Compiled condition */
132 struct nscstr {
133     char operator;              /* '<', '=', '>', '#' */
134     enum nsc_type optype;       /* operator type */
135     struct valstr lft;          /* left operand */
136     struct valstr rgt;          /* right operand */
137 };
138
139 /* Selection type */
140 enum ns_seltype {
141     NS_UNDEF,                   /* error value */
142     NS_LIST,                    /* list of IDs */
143     NS_DIST,                    /* circular area */
144     NS_AREA,                    /* rectangular area */
145     NS_ALL,                     /* everything */
146     NS_XY,                      /* one sector area */
147     NS_GROUP,                   /* group, i.e. fleet, wing, army */
148     NS_CARGO                    /* loaded on the same carrier */
149 };
150
151 /* Sector iterator */
152 struct nstr_sect {
153     coord x, y;                 /* current x-y */
154     coord dx, dy;               /* accumlated x,y travel */
155     int id;                     /* return value of sctoff */
156     enum ns_seltype type;       /* selection type: NS_AREA or NS_DIST */
157     int curdist;                /* NS_DIST: current range */
158     struct range range;         /* area of coverage */
159     int dist;                   /* NS_DIST: range */
160     coord cx, cy;               /* NS_DIST: center x-y */
161     int ncond;                  /* # of selection conditions */
162     struct nscstr cond[NS_NCOND]; /* selection conditions */
163 };
164
165 /* Item iterator */
166 struct nstr_item {
167     int cur;                    /* current item */
168     enum ns_seltype sel;        /* selection type, any but NS_UNDEF */
169     int type;                   /* item type being selected */
170     int curdist;                /* if NS_DIST, current item's dist */
171     struct range range;         /* NS_AREA/NS_DIST: range selector */
172     int dist;                   /* NS_DIST: distance selector */
173     coord cx, cy;               /* NS_DIST: center x-y, NS_XY: xy */
174     char group;                 /* NS_GROUP: fleet/wing match */
175     int next;                   /* NS_CARGO: next item */
176     int size;                   /* NS_LIST: size of list */
177     int index;                  /* NS_LIST: index */
178     int list[NS_LSIZE];         /* NS_LIST: item list */
179     int ncond;                  /* # of selection conditions */
180     struct nscstr cond[NS_NCOND]; /* selection conditions */
181 };
182
183 /*
184  * Symbol binding: associate NAME with VALUE.
185  */
186 struct symbol {
187     int value;
188     char *name;
189 };
190
191 /* Selector flags */
192 enum {
193     NSC_DEITY = bit(0),         /* access restricted to deity */
194     NSC_EXTRA = bit(1),         /* computable from other selectors */
195     NSC_CONST = bit(2),         /* field cannot be changed */
196     NSC_BITS = bit(3)           /* value consists of flag bits */
197 };
198
199 /*
200  * Selector descriptor
201  *
202  * A selector describes an attribute of some context object.
203  * A selector with ca_type NSC_NOTYPE is invalid.
204  * If ca_get is null, the selector describes a datum of type ca_type
205  * at offset ca_offs in the context object.
206  * A datum of type NSC_STRINGY is an array of ca_len characters.
207  * A datum of any other type is either a scalar of that type (if
208  * ca_len is zero), or an array of ca_len elements of that type.
209  * If ca_get is not null, the selector is virtual.  Values can be
210  * obtained by calling ca_get(VAL, NP, CTXO), where VAL has been
211  * initialized my from the selector and an index by nstr_mksymval(),
212  * NP points to the country to use for coordinate translation and
213  * access control (null for none), and CTXO is the context object.
214  * See struct valstr for details.
215  * Because virtual selectors don't have a setter method, xundump must
216  * be made to ignore them, e.g. by setting NSC_EXTRA.
217  * If flag NSC_DEITY is set, only to deities can use this selector.
218  * If flag NSC_EXTRA is set, xdump and xundump ignore this selector.
219  * If flag NSC_CONST is set, the datum can't be changed from its
220  * initial value (xundump obeys that).
221  * If ca_table is not EF_BAD, the datum refers to that Empire table;
222  * ca_type must be an integer type.  If flag NSC_BITS is set, the
223  * datum consists of flag bits, and the referred table must be a
224  * symbol table defining those bits.
225  */
226 struct castr {
227     char *ca_name;
228     ptrdiff_t ca_off;
229     enum nsc_type ca_type;
230     unsigned short ca_len;
231     void *(*ca_get)(struct valstr *, struct natstr *, void *);
232     int ca_table;
233     int ca_flags;
234 };
235
236 /* variables using the above */
237
238 extern struct castr ichr_ca[];
239 extern struct castr pchr_ca[];
240 extern struct castr sect_ca[];
241 extern struct castr dchr_ca[];
242 extern struct castr ship_ca[];
243 extern struct castr mchr_ca[];
244 extern struct castr plane_ca[];
245 extern struct castr plchr_ca[];
246 extern struct castr land_ca[];
247 extern struct castr lchr_ca[];
248 extern struct castr nuke_ca[];
249 extern struct castr nchr_ca[];
250 extern struct castr treaty_ca[];
251 extern struct castr loan_ca[];
252 extern struct castr news_ca[];
253 extern struct castr lost_ca[];
254 extern struct castr commodity_ca[];
255 extern struct castr trade_ca[];
256 extern struct castr nat_ca[];
257 extern struct castr cou_ca[];
258 extern struct castr realm_ca[];
259 extern struct castr game_ca[];
260 extern struct castr intrchr_ca[];
261 extern struct castr rpt_ca[];
262 extern struct castr update_ca[];
263 extern struct castr empfile_ca[];
264 extern struct castr symbol_ca[];
265 extern struct symbol ship_chr_flags[];
266 extern struct symbol plane_chr_flags[];
267 extern struct symbol land_chr_flags[];
268 extern struct symbol nuke_chr_flags[];
269 extern struct symbol treaty_flags[];
270 extern struct castr mdchr_ca[];
271 extern struct symbol meta_type[];
272 extern struct symbol meta_flags[];
273 extern struct symbol missions[];
274 extern struct symbol plane_flags[];
275 extern struct symbol retreat_flags[];
276 extern struct symbol nation_status[];
277 extern struct symbol nation_flags[];
278 extern struct symbol nation_rejects[];
279 extern struct symbol nation_relations[];
280 extern struct symbol level[];
281 extern struct symbol agreement_statuses[];
282 extern struct symbol plague_stages[];
283 extern struct symbol packing[];
284 extern struct symbol resources[];
285 extern struct symbol sector_navigation[];
286
287 /* src/lib/subs/nstr.c */
288 extern int nstr_comp(struct nscstr *np, int len, int type, char *str);
289 extern char *nstr_comp_val(char *, struct valstr*, int);
290 extern int nstr_exec(struct nscstr *, int, void *);
291 /* src/lib/common/nstreval.c */
292 extern struct valstr *nstr_mksymval(struct valstr *, struct castr *, int);
293 extern struct valstr *nstr_exec_val(struct valstr *, natid, void *, enum nsc_type);
294 extern int nstr_promote(int);
295 extern char *symbol_by_value(int, struct symbol *);
296 /* src/lib/global/nsc.c */
297 extern void nsc_init(void);
298
299 #endif