]> git.pond.sub.org Git - empserver/blob - include/nsc.h
COPYING duplicates information from README. Remove. Move GPL from
[empserver] / include / nsc.h
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, 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 "xy.h"
40
41 #define NS_LSIZE        128
42 #define NS_NCOND        16
43
44 /* Value type */
45 typedef enum {
46     NSC_NOTYPE,
47     /* promoted types */
48     NSC_LONG,                   /* long */
49     NSC_DOUBLE,                 /* double */
50     NSC_STRING,                 /* char *, zero-terminated string */
51     NSC_TYPEID,                 /* signed char, index into chr table */
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_TIME,                   /* time_t */
61     NSC_FLOAT,                  /* float */
62     NSC_STRINGY,                /* char[], zero-terminated string */
63                                 /* FIXME zero may be missing */
64     /* aliases, must match typedefs */
65     NSC_NATID = NSC_UCHAR       /* nation id */
66 } nsc_type;
67 typedef char packed_nsc_type;
68
69 /* Return nsc_type for a signed integer with the same size as TYPE.  */
70 #define NSC_SITYPE(type)                                \
71     (sizeof(type) == 1 ? NSC_CHAR                       \
72      : sizeof(type) == sizeof(short) ? NSC_SHORT        \
73      : sizeof(type) == sizeof(int) ? NSC_INT            \
74      : sizeof(type) == sizeof(long) ? NSC_LONG          \
75      : 1/0)
76
77 /* Value category */
78 typedef enum {
79     NSC_NOCAT,
80     NSC_VAL,                    /* evaluated value */
81     NSC_OFF,                    /* symbolic value: at offset in object */
82     NSC_ID                      /* unresolved identifier (internal use) */
83 } nsc_cat;
84 typedef char packed_nsc_cat;
85
86 enum {
87     NSC_DEITY = bit(0),         /* access restricted to deity */
88     NSC_EXTRA = bit(1),         /* computable from other selectors */
89     NSC_CONST = bit(2),         /* field cannot be changed */
90     NSC_BITS = bit(3)           /* value consists of flag bits */
91 };
92 typedef unsigned char nsc_flags;
93
94 /*
95  * Value, possibly symbolic.
96  * If type is NSC_NOTYPE, it's an error value.
97  * If category is NSC_OFF, the value is in a context object at offset
98  * val_as.sym.off + val_as.sym.idx * S, where S is the size of the
99  * value.
100  * If category is NSC_VAL, the value is in val_as, and the type is a
101  * promoted type.
102  */
103 struct valstr {
104     packed_nsc_type val_type;   /* type of value */
105     packed_nsc_cat val_cat;     /* category of value */
106     union {
107         struct {                /* cat NSC_OFF */
108             ptrdiff_t off;
109             int len;
110             int idx;
111         } sym;
112         double dbl;             /* cat NSC_VAL, type NSC_DOUBLE */
113         struct {                /* cat NSC_VAL, type NSC_STRING, cat NSC_ID */
114             char *base;
115             size_t maxsz;
116         } str;
117         long lng;               /* cat NSC_VAL, type NSC_LONG, NSC_TYPEID */
118     } val_as;
119 };
120
121 /* Compiled condition */
122 struct nscstr {
123     char operator;              /* '<', '=', '>', '#' */
124     packed_nsc_type optype;     /* operator type */
125     struct valstr lft;          /* left operand */
126     struct valstr rgt;          /* right operand */
127 };
128
129 /* Selection type */
130 typedef enum {
131     NS_UNDEF,                   /* error value */
132     NS_LIST,                    /* list of IDs */
133     NS_DIST,                    /* circular area */
134     NS_AREA,                    /* rectangular area */
135     NS_ALL,                     /* everything */
136     NS_XY,                      /* one sector area */
137     NS_GROUP                    /* group, i.e. fleet, wing, army */
138 } ns_seltype;
139
140 /* Sector iterator */
141 struct nstr_sect {
142     coord x, y;                 /* current x-y */
143     coord dx, dy;               /* accumlated x,y travel */
144     int id;                     /* return value of sctoff */
145     ns_seltype type;            /* type of query */
146     int curdist;                /* dist query: current range */
147     struct range range;         /* area of coverage */
148     int dist;                   /* dist query: range */
149     coord cx, cy;               /* dist query: center x-y */
150     int (*read)(int type, int id, void *ptr);   /* read function */
151     int ncond;                  /* # of selection conditions */
152     struct nscstr cond[NS_NCOND];       /* selection conditions */
153 };
154
155 /* Item iterator */
156 struct nstr_item {
157     int cur;                    /* current item */
158     ns_seltype sel;             /* selection type */
159     int type;                   /* item type being selected */
160     int curdist;                /* if NS_DIST, current item's dist */
161     struct range range;         /* NS_AREA/NS_DIST: range selector */
162     int dist;                   /* NS_DIST: distance selector */
163     coord cx, cy;               /* NS_DIST: center x-y, NS_XY: xy */
164     int group;                  /* NS_GROUP: fleet/wing match */
165     int size;                   /* NS_LIST: size of list */
166     int index;                  /* NS_LIST: index */
167     int list[NS_LSIZE];         /* NS_LIST: item list */
168     int (*read)(int type, int id, void *ptr);   /* read function */
169     int flags;                  /* ef_flags(TYPE) */
170     int ncond;                  /* # of selection conditions */
171     struct nscstr cond[NS_NCOND]; /* selection conditions */
172 };
173
174 /*
175  * Symbol binding: associate NAME with VALUE.
176  */
177 struct symbol {
178     int value;
179     char *name;
180 };
181
182 /*
183  * Selector descriptor.
184  */
185 struct castr {
186     packed_nsc_type ca_type;    /* type of value */
187     nsc_flags ca_flags;
188     unsigned short ca_len;      /* non-zero: is an array; #array elements */
189     ptrdiff_t ca_off;           /* offset of value in the context object */
190     char *ca_name;
191     int ca_table;               /* referred table ID, or EF_BAD */
192 };
193
194 /* variables using the above */
195
196 extern struct castr ichr_ca[];
197 extern struct castr pchr_ca[];
198 extern struct castr sect_ca[];
199 extern struct castr dchr_ca[];
200 extern struct castr ship_ca[];
201 extern struct castr mchr_ca[];
202 extern struct castr plane_ca[];
203 extern struct castr plchr_ca[];
204 extern struct castr land_ca[];
205 extern struct castr lchr_ca[];
206 extern struct castr nuke_ca[];
207 extern struct castr nchr_ca[];
208 extern struct castr treaty_ca[];
209 extern struct castr loan_ca[];
210 extern struct castr news_ca[];
211 extern struct castr lost_ca[];
212 extern struct castr commodity_ca[];
213 extern struct castr trade_ca[];
214 extern struct castr nat_ca[];
215 extern struct castr cou_ca[];
216 extern struct castr realm_ca[];
217 extern struct castr intrchr_ca[];
218 extern struct castr rpt_ca[];
219 extern struct castr empfile_ca[];
220 extern struct castr symbol_ca[];
221 extern struct symbol ship_chr_flags[];
222 extern struct symbol plane_chr_flags[];
223 extern struct symbol land_chr_flags[];
224 extern struct symbol nuke_chr_flags[];
225 extern struct symbol treaty_flags[];
226 extern struct castr mdchr_ca[];
227 extern struct symbol meta_type[];
228 extern struct symbol meta_flags[];
229 extern struct symbol missions[];
230 extern struct symbol plane_flags[];
231 extern struct symbol retreat_flags[];
232 extern struct symbol nation_status[];
233 extern struct symbol nation_flags[];
234 extern struct symbol nation_relations[];
235 extern struct symbol level[];
236 extern struct symbol agreement_statuses[];
237 extern struct symbol plague_stages[];
238 extern struct symbol packing[];
239 extern struct symbol resources[];
240 extern struct symbol sector_navigation[];
241
242 /* src/lib/subs/nstr.c */
243 extern int nstr_comp(struct nscstr *np, int len, int type, char *str);
244 extern char *nstr_comp_val(char *, struct valstr*, int);
245 extern int nstr_coerce_val(struct valstr *, nsc_type, char *);
246 extern int nstr_exec(struct nscstr *, int, void *);
247 extern void nstr_exec_val(struct valstr *, natid, void *, nsc_type);
248 extern char *symbol_by_value(int, struct symbol *);
249
250 #endif