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