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