]> git.pond.sub.org Git - empserver/blob - include/nsc.h
(NS_UNDEF, NS_LIST, NS_DIST, NS_AREA, NS_ALL, NS_XY, NS_GROUP): Turn
[empserver] / include / nsc.h
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  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_type;
63 typedef char packed_nsc_type;
64
65 /* Value category */
66 typedef enum {
67     NSC_NOCAT,
68     NSC_VAL,                    /* evaluated value */
69     NSC_OFF                     /* symbolic value: at offset in object */
70 } nsc_cat;
71 typedef char packed_nsc_cat;
72
73 enum {
74     NSC_DEITY = 1               /* access restricted to deity */
75 };
76 typedef unsigned char nsc_flags;
77
78 /*
79  * Value, possibly symbolic.
80  * If type is NSC_NOTYPE, it's an error value.
81  * If category is NSC_OFF, the value is at offset val_as.off in the
82  * context object.
83  * If category is NSC_VAL, the value is in val_as, and the type is a
84  * promoted type.
85  * Some values can also be interpreted as an object type.  The values
86  * consumer chooses how to interpret it, depending on context.
87  */
88 struct valstr {
89     packed_nsc_type val_type;   /* type of value */
90     packed_nsc_cat val_cat;     /* category of value */
91     signed char val_as_type;    /* value interpreted as object type */
92     union {
93         ptrdiff_t off;          /* cat NSC_OFF */
94         double dbl;             /* cat NSC_VAL, types NSC_DOUBLE */
95         char *str;              /* cat NSC_VAL, type NSC_STRING */
96         long lng;               /* cat NSC_VAL, type NSC_LONG */
97     } val_as;
98 };
99
100 /* Compiled condition */
101 struct nscstr {
102     char operator;              /* '<', '=', '>', '#' */
103     packed_nsc_type optype;     /* operator type */
104     struct valstr lft;          /* left operand */
105     struct valstr rgt;          /* right operand */
106 };
107
108 /* Selection type */
109 typedef enum {
110     NS_UNDEF,                   /* error value */
111     NS_LIST,                    /* list of IDs */
112     NS_DIST,                    /* circular area */
113     NS_AREA,                    /* rectangular area */
114     NS_ALL,                     /* everything */
115     NS_XY,                      /* one sector area */
116     NS_GROUP                    /* group, i.e. fleet, wing, army */
117 } ns_seltype;
118
119 /* Sector iterator */
120 struct nstr_sect {
121     coord x, y;                 /* current x-y */
122     coord dx, dy;               /* accumlated x,y travel */
123     int id;                     /* return value of sctoff */
124     ns_seltype type;            /* type of query */
125     int curdist;                /* dist query: current range */
126     struct range range;         /* area of coverage */
127     int dist;                   /* dist query: range */
128     coord cx, cy;               /* dist query: center x-y */
129     int (*read)(int type, int id, caddr_t ptr); /* read function */
130     int ncond;                  /* # of selection conditions */
131     struct nscstr cond[NS_NCOND];       /* selection conditions */
132 };
133
134 /* Item iterator */
135 struct nstr_item {
136     int cur;                    /* current item */
137     ns_seltype sel;             /* selection type */
138     int type;                   /* item type being selected */
139     int curdist;                /* if NS_DIST, current item's dist */
140     struct range range;         /* NS_AREA/NS_DIST: range selector */
141     int dist;                   /* NS_DIST: distance selector */
142     coord cx, cy;               /* NS_DIST: center x-y, NS_XY: xy */
143     int group;                  /* NS_GROUP: fleet/wing match */
144     int size;                   /* NS_LIST: size of list */
145     int index;                  /* NS_LIST: index */
146     int list[NS_LSIZE];         /* NS_LIST: item list */
147     int (*read)(int type, int id, caddr_t ptr); /* read function */
148     int flags;                  /* ef_flags(TYPE) */
149     int ncond;                  /* # of selection conditions */
150     struct nscstr cond[NS_NCOND]; /* selection conditions */
151 };
152
153 /*
154  * Selector descriptor.
155  * Value is at offset CA_OFF in the context object.
156  */
157 struct castr {
158     packed_nsc_type ca_type;    /* type of value */
159     nsc_flags ca_flags;
160     unsigned short ca_len;      /* non-zero: is an array; #array elements */
161     ptrdiff_t ca_off;
162     char *ca_name;
163 };
164
165 /* variables using the above */
166
167 extern struct castr sect_ca[];
168 extern struct castr ship_ca[];
169 extern struct castr plane_ca[];
170 extern struct castr land_ca[];
171 extern struct castr nuke_ca[];
172 extern struct castr news_ca[];
173 extern struct castr nat_ca[];
174 extern struct castr treaty_ca[];
175 extern struct castr trade_ca[];
176 extern struct castr loan_ca[];
177 extern struct castr genitem_ca[];
178 extern struct castr lost_ca[];
179 extern struct castr commodity_ca[];
180
181 /* src/lib/subs/nstr.c */
182 extern int nstr_comp(struct nscstr *np, int len, int type, char *str);
183 extern char *nstr_comp_val(char *, struct valstr*, int);
184 extern int nstr_coerce_val(struct valstr *, nsc_type, char *);
185 extern int nstr_exec(struct nscstr *, int, void *);
186 extern void nstr_exec_val(struct valstr *, natid, void *, nsc_type);
187
188 #endif /* _NSC_H_ */