]> git.pond.sub.org Git - empserver/blob - src/lib/common/filetable.c
Clean up how a view's base table is defined
[empserver] / src / lib / common / filetable.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2011, 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  *  filetable.c: Empire game data file descriptions.
28  *
29  *  Known contributors to this file:
30  *     Markus Armbruster, 2005-2008
31  */
32
33 #include <config.h>
34
35 #include <stddef.h>
36 #include "commodity.h"
37 #include "file.h"
38 #include "game.h"
39 #include "land.h"
40 #include "loan.h"
41 #include "lost.h"
42 #include "nat.h"
43 #include "news.h"
44 #include "nsc.h"
45 #include "nuke.h"
46 #include "optlist.h"
47 #include "plane.h"
48 #include "power.h"
49 #include "product.h"
50 #include "sect.h"
51 #include "ship.h"
52 #include "server.h"
53 #include "trade.h"
54 #include "treaty.h"
55 #include "version.h"
56 #include "xy.h"
57
58 /* Number of elements in ARRAY.  */
59 #define SZ(array) (sizeof(array) / sizeof((array)[0]))
60
61 /* Initializers for members flags... */
62 /* Unmapped cache */
63 #define UNMAPPED_CACHE(type, flags) \
64     sizeof(type), (flags), NULL, 0, 0, 0, 0, -1, NULL, NULL, NULL, NULL
65 /*
66  * Mapped cache, array with known size.
67  * Members cids, fids are not set.
68  */
69 #define ARRAY_CACHE(array, flags) \
70     sizeof(*(array)), (flags), (char *)(array), \
71     SZ((array)), 0, 0, 0, -1, NULL, NULL, NULL, NULL
72 /*
73  * Mapped cache, array with unknown size.
74  * Members csize, cids, fids are not set.
75  */
76 #define PTR_CACHE(ptr, flags) \
77     sizeof(*(ptr)), (flags), (char *)(ptr), \
78     0, 0, 0, 0, -1, NULL, NULL, NULL, NULL
79 /*
80  * Array-backed table.
81  * The array's last element is the sentinel.
82  */
83 #define ARRAY_TABLE(array, flags)                               \
84     sizeof(*(array)), (flags), (char *)(array),                 \
85     SZ((array)), 0, SZ((array)) - 1, SZ((array)) - 1, -1,       \
86     NULL, NULL, NULL, NULL
87
88 /* Common configuration table flags */
89 #define EFF_CFG (EFF_PRIVATE | EFF_MEM | EFF_STATIC | EFF_SENTINEL)
90
91 struct empfile empfile[] = {
92     /*
93      * How this initializer works:
94      *
95      * Members uid, name, file, cadef, size, and the EFF_IMMUTABLE
96      * bits of flags get their final value.
97      * If flags & EFF_STATIC, the cache is mapped here, and members
98      * cache, csize get their final value.
99      * Members baseid, cids, fids and the EFF_MEM|EFF_PRIVATE bits of
100      * flags are initialized according the initial cache contents.
101      * Member fd is initialized to -1.
102      * Members init, postread, prewrite get initialized to NULL, but
103      * that can be changed by users.
104      *
105      * Whatever of the above can't be done here must be done in
106      * empfile_init() or empfile_fixup().  Except cadef may be set in
107      * nsc_init() instead.
108      */
109
110     /*
111      * Keep in mind that player command arguments are matched against
112      * values of member name: no whitespace there, please.
113      */
114
115     /*
116      * Dynamic game data
117      *
118      * All caches unmapped.  EF_MAP and EF_BMAP get a bogus size here.
119      * Fixed up by empfile_fixup().
120      */
121     {EF_SECTOR, "sect", "sector", sect_ca, EF_BAD,
122      UNMAPPED_CACHE(struct sctstr, EFF_TYPED | EFF_XY | EFF_OWNER)},
123     {EF_SHIP, "ship", "ship", ship_ca, EF_BAD,
124      UNMAPPED_CACHE(struct shpstr,
125                     EFF_TYPED | EFF_XY | EFF_OWNER | EFF_GROUP)},
126     {EF_PLANE, "plane", "plane", plane_ca, EF_BAD,
127      UNMAPPED_CACHE(struct plnstr,
128                     EFF_TYPED | EFF_XY | EFF_OWNER | EFF_GROUP)},
129     {EF_LAND, "land", "land", land_ca, EF_BAD,
130      UNMAPPED_CACHE(struct lndstr,
131                     EFF_TYPED | EFF_XY | EFF_OWNER | EFF_GROUP)},
132     {EF_NUKE, "nuke", "nuke", nuke_ca, EF_BAD,
133      UNMAPPED_CACHE(struct nukstr, EFF_TYPED | EFF_XY | EFF_OWNER)},
134     {EF_NEWS, "news", "news", news_ca, EF_BAD,
135      UNMAPPED_CACHE(struct nwsstr, 0)},
136     {EF_TREATY, "treaty", "treaty", treaty_ca, EF_BAD,
137      UNMAPPED_CACHE(struct trtstr, EFF_TYPED)},
138     {EF_TRADE, "trade", "trade", trade_ca, EF_BAD,
139      UNMAPPED_CACHE(struct trdstr, EFF_TYPED | EFF_OWNER)},
140     {EF_POWER, "pow", "power", NULL, EF_BAD,
141      UNMAPPED_CACHE(struct powstr, 0)},
142     {EF_NATION, "nat", "nation", nat_ca, EF_BAD,
143      UNMAPPED_CACHE(struct natstr, EFF_TYPED | EFF_OWNER)},
144     {EF_LOAN, "loan", "loan", loan_ca, EF_BAD,
145      UNMAPPED_CACHE(struct lonstr, EFF_TYPED)},
146     {EF_MAP, "map", "map", NULL, EF_BAD,
147      0, 0, NULL, 0, 0, 0, 0, -1, NULL, NULL, NULL, NULL},
148     {EF_BMAP, "bmap", "bmap", NULL, EF_BAD,
149      0, 0, NULL, 0, 0, 0, 0, -1, NULL, NULL, NULL, NULL},
150     {EF_COMM, "commodity", "commodity", commodity_ca, EF_BAD,
151      UNMAPPED_CACHE(struct comstr, EFF_TYPED | EFF_OWNER)},
152     {EF_LOST, "lost", "lostitems", lost_ca, EF_BAD,
153      UNMAPPED_CACHE(struct loststr, EFF_TYPED | EFF_OWNER)},
154     {EF_REALM, "realm", "realms", realm_ca, EF_BAD,
155      UNMAPPED_CACHE(struct realmstr, EFF_TYPED | EFF_OWNER)},
156     {EF_GAME, "game", "game", game_ca, EF_BAD,
157      UNMAPPED_CACHE(struct gamestr, EFF_TYPED)},
158
159     /*
160      * Static game data (configuration)
161      */
162     /*
163      * Characteristics tables.  Characteristics with a null file
164      * member are compiled in.  The others are empty; use
165      * read_builtin_tables() to fill them.
166      */
167     {EF_ITEM, "item", "item.config", ichr_ca, EF_BAD,
168      ARRAY_CACHE(ichr, EFF_CFG)},
169     {EF_PRODUCT, "product", "product.config", pchr_ca, EF_BAD,
170      ARRAY_CACHE(pchr, EFF_CFG)},
171     {EF_SECTOR_CHR, "sect-chr", "sect.config", dchr_ca, EF_BAD,
172      ARRAY_CACHE(dchr, EFF_CFG)},
173     {EF_SHIP_CHR, "ship-chr", "ship.config", mchr_ca, EF_BAD,
174      ARRAY_CACHE(mchr, EFF_CFG)},
175     {EF_PLANE_CHR, "plane-chr", "plane.config", plchr_ca, EF_BAD,
176      ARRAY_CACHE(plchr, EFF_CFG)},
177     {EF_LAND_CHR, "land-chr", "land.config", lchr_ca, EF_BAD,
178      ARRAY_CACHE(lchr, EFF_CFG)},
179     {EF_NUKE_CHR, "nuke-chr", "nuke.config", nchr_ca, EF_BAD,
180      ARRAY_CACHE(nchr, EFF_CFG)},
181     {EF_NEWS_CHR, "news-chr", NULL, rpt_ca, EF_BAD,
182      ARRAY_TABLE(rpt, EFF_CFG)},
183     {EF_INFRASTRUCTURE, "infrastructure", "infra.config", intrchr_ca, EF_BAD,
184      ARRAY_CACHE(intrchr, EFF_CFG)},
185     /*
186      * Update schedule table.  Use read_schedule() to fill.
187      */
188     {EF_UPDATES, "updates", NULL, update_ca, EF_BAD,
189      ARRAY_CACHE(update_time, EFF_CFG)},
190     /*
191      * Special tables.  EF_META gets bogus size, cids and fids here.
192      * Fixed up by empfile_init().  EF_VERSION's cadef is set by
193      * nsc_init().
194      */
195     {EF_TABLE, "table", NULL, empfile_ca, EF_BAD,
196      ARRAY_TABLE(empfile, EFF_CFG)},
197     {EF_VERSION, "version", NULL, NULL, EF_BAD,
198      sizeof(PACKAGE_STRING), EFF_STATIC, version, 1, 0, 1, 1, -1,
199      NULL, NULL, NULL, NULL},
200     {EF_META, "meta", NULL, mdchr_ca, EF_BAD,
201      PTR_CACHE(mdchr_ca, EFF_CFG)},
202
203     /*
204      * Symbol tables
205      *
206      * These get bogus csize, cids and fids here.  Fixed up by
207      * empfile_init().
208      */
209 #define SYMTAB(type, name, tab) {(type), (name), NULL, symbol_ca, EF_BAD, \
210                                  PTR_CACHE((tab), EFF_CFG)}
211     SYMTAB(EF_AGREEMENT_STATUS, "agreement-status", agreement_statuses),
212     SYMTAB(EF_LAND_CHR_FLAGS, "land-chr-flags", land_chr_flags),
213     SYMTAB(EF_LEVEL, "level", level),
214     SYMTAB(EF_META_FLAGS, "meta-flags", meta_flags),
215     SYMTAB(EF_META_TYPE, "meta-type", meta_type),
216     SYMTAB(EF_MISSIONS, "missions", missions),
217     SYMTAB(EF_NATION_FLAGS, "nation-flags", nation_flags),
218     SYMTAB(EF_NATION_REJECTS, "nation-rejects", nation_rejects),
219     SYMTAB(EF_NATION_RELATIONS, "nation-relationships", nation_relations),
220     SYMTAB(EF_NATION_STATUS, "nation-status", nation_status),
221     SYMTAB(EF_NUKE_CHR_FLAGS, "nuke-chr-flags", nuke_chr_flags),
222     SYMTAB(EF_PACKING, "packing", packing),
223     SYMTAB(EF_PAGE_HEADINGS, "page-headings", page_headings),
224     SYMTAB(EF_PLAGUE_STAGES, "plague-stages", plague_stages),
225     SYMTAB(EF_PLANE_CHR_FLAGS, "plane-chr-flags", plane_chr_flags),
226     SYMTAB(EF_PLANE_FLAGS, "plane-flags", plane_flags),
227     SYMTAB(EF_RESOURCES, "resources", resources),
228     SYMTAB(EF_RETREAT_FLAGS, "retreat-flags", retreat_flags),
229     SYMTAB(EF_SECTOR_NAVIGATION, "sector-navigation", sector_navigation),
230     SYMTAB(EF_SHIP_CHR_FLAGS, "ship-chr-flags", ship_chr_flags),
231     SYMTAB(EF_TREATY_FLAGS, "treaty-flags", treaty_flags),
232
233     /* Views */
234     {EF_COUNTRY, "country", NULL, cou_ca, EF_NATION,
235      UNMAPPED_CACHE(struct natstr, EFF_TYPED | EFF_OWNER)},
236
237     /* Sentinel */
238     {EF_BAD, NULL, NULL, NULL, EF_BAD,
239      0, 0, NULL, 0, 0, 0, 0, -1, NULL, NULL, NULL, NULL},
240 };
241
242 static void
243 ef_fix_size(struct empfile *ep, int n)
244 {
245     ep->cids = ep->fids = n;
246     ep->csize = n + 1;
247 }
248
249 void
250 empfile_init(void)
251 {
252     struct castr *ca;
253     struct empfile *ep;
254     struct symbol *lup;
255     int i;
256
257     ca = (struct castr *)empfile[EF_META].cache;
258     for (i = 0; ca[i].ca_name; i++) ;
259     ef_fix_size(&empfile[EF_META], i);
260
261     for (ep = empfile; ep->uid >= 0; ep++) {
262         if (ep->cadef == symbol_ca) {
263             lup = (struct symbol *)ep->cache;
264             for (i = 0; lup[i].name; i++) ;
265             ef_fix_size(ep, i);
266         }
267     }
268 }
269
270 void
271 empfile_fixup(void)
272 {
273     empfile[EF_MAP].size = empfile[EF_BMAP].size = WORLD_SZ();
274 }