]> git.pond.sub.org Git - empserver/blob - src/lib/common/filetable.c
Move file initialization from files.c to empfile oninit()
[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 static void sct_oninit(void *);
59 static void nat_oninit(void *);
60 static void realm_oninit(void *);
61 static void game_oninit(void *);
62
63 /* Number of elements in ARRAY.  */
64 #define SZ(array) (sizeof(array) / sizeof((array)[0]))
65
66 /* Initializers for members flags... */
67 /* Unmapped cache */
68 #define UNMAPPED_CACHE(type, nent, flags)       \
69     sizeof(type), (nent), (flags), NULL,        \
70     0, 0, 0, 0, -1
71 /*
72  * Mapped cache, array with known size.
73  * Members cids, fids are zero, i.e. cache is empty.
74  */
75 #define ARRAY_CACHE(array, flags) \
76     sizeof(*(array)), -1, (flags), (char *)(array),     \
77     SZ((array)), 0, 0, 0, -1
78 /*
79  * Mapped cache, array with unknown size.
80  * Member csize gets a bogus value, needs to be fixed up.
81  * Members cids, fids are zero, i.e. cache is empty.
82  */
83 #define PTR_CACHE(ptr, flags) \
84     sizeof(*(ptr)), -1, (flags), (char *)(ptr), \
85     0, 0, 0, 0, -1
86 /*
87  * Array-backed table of fixed size.
88  */
89 #define ARRAY_TABLE(array, nent, flags)                 \
90     sizeof(*(array)), (nent), (flags), (char *)(array), \
91     SZ((array)), 0, (nent), (nent), -1
92
93 /* Common configuration table flags */
94 #define EFF_CFG (EFF_PRIVATE | EFF_MEM | EFF_STATIC | EFF_SENTINEL)
95
96 struct empfile empfile[] = {
97     /*
98      * How this initializer works:
99      *
100      * Members uid, name, file, cadef, size, and the EFF_IMMUTABLE
101      * bits of flags get their final value.
102      * If flags & EFF_STATIC, the cache is mapped here, and members
103      * cache, csize get their final value.
104      * Members baseid, cids, fids and the EFF_MEM|EFF_PRIVATE bits of
105      * flags are initialized according the initial cache contents.
106      * Member fd is initialized to -1.
107      * Members init, postread, prewrite get initialized to NULL, but
108      * that can be changed by users.
109      *
110      * Whatever of the above can't be done here must be done in
111      * empfile_init() or empfile_fixup().  Except cadef may be set in
112      * nsc_init() instead.
113      */
114
115     /*
116      * Keep in mind that player command arguments are matched against
117      * values of member name: no whitespace there, please.
118      */
119
120     /*
121      * Dynamic game data
122      *
123      * All caches unmapped.  EF_SECTOR gets bogus nent here.  EF_MAP
124      * and EF_BMAP get a bogus size here.  Fixed up by
125      * empfile_fixup().
126      */
127     {EF_SECTOR, "sect", "sector", sect_ca, EF_BAD,
128      UNMAPPED_CACHE(struct sctstr, -1, EFF_TYPED | EFF_XY | EFF_OWNER),
129      sct_oninit, NULL, NULL, NULL},
130     {EF_SHIP, "ship", "ship", ship_ca, EF_BAD,
131      UNMAPPED_CACHE(struct shpstr, -1,
132                     EFF_TYPED | EFF_XY | EFF_OWNER | EFF_GROUP),
133      NULL, NULL, NULL, NULL},
134     {EF_PLANE, "plane", "plane", plane_ca, EF_BAD,
135      UNMAPPED_CACHE(struct plnstr, -1,
136                     EFF_TYPED | EFF_XY | EFF_OWNER | EFF_GROUP),
137      NULL, NULL, NULL, NULL},
138     {EF_LAND, "land", "land", land_ca, EF_BAD,
139      UNMAPPED_CACHE(struct lndstr, -1,
140                     EFF_TYPED | EFF_XY | EFF_OWNER | EFF_GROUP),
141      NULL, NULL, NULL, NULL},
142     {EF_NUKE, "nuke", "nuke", nuke_ca, EF_BAD,
143      UNMAPPED_CACHE(struct nukstr, -1, EFF_TYPED | EFF_XY | EFF_OWNER),
144      NULL, NULL, NULL, NULL},
145     {EF_NEWS, "news", "news", news_ca, EF_BAD,
146      UNMAPPED_CACHE(struct nwsstr, -1, 0),
147      NULL, NULL, NULL, NULL},
148     {EF_TREATY, "treaty", "treaty", treaty_ca, EF_BAD,
149      UNMAPPED_CACHE(struct trtstr, -1, EFF_TYPED),
150      NULL, NULL, NULL, NULL},
151     {EF_TRADE, "trade", "trade", trade_ca, EF_BAD,
152      UNMAPPED_CACHE(struct trdstr, -1, EFF_TYPED | EFF_OWNER),
153      NULL, NULL, NULL, NULL},
154     {EF_POWER, "pow", "power", NULL, EF_BAD,
155      UNMAPPED_CACHE(struct powstr, -1, 0),
156      NULL, NULL, NULL, NULL},
157     {EF_NATION, "nat", "nation", nat_ca, EF_BAD,
158      UNMAPPED_CACHE(struct natstr, MAXNOC, EFF_TYPED | EFF_OWNER),
159      nat_oninit, NULL, NULL, NULL},
160     {EF_LOAN, "loan", "loan", loan_ca, EF_BAD,
161      UNMAPPED_CACHE(struct lonstr, -1, EFF_TYPED),
162      NULL, NULL, NULL, NULL},
163     {EF_MAP, "map", "map", NULL, EF_BAD,
164      0, MAXNOC, 0, NULL, 0, 0, 0, 0, -1, NULL, NULL, NULL, NULL},
165     {EF_BMAP, "bmap", "bmap", NULL, EF_BAD,
166      0, MAXNOC, 0, NULL, 0, 0, 0, 0, -1, NULL, NULL, NULL, NULL},
167     {EF_COMM, "commodity", "commodity", commodity_ca, EF_BAD,
168      UNMAPPED_CACHE(struct comstr, -1, EFF_TYPED | EFF_OWNER),
169      NULL, NULL, NULL, NULL},
170     {EF_LOST, "lost", "lostitems", lost_ca, EF_BAD,
171      UNMAPPED_CACHE(struct loststr, -1, EFF_TYPED | EFF_OWNER),
172      NULL, NULL, NULL, NULL},
173     {EF_REALM, "realm", "realms", realm_ca, EF_BAD,
174      UNMAPPED_CACHE(struct realmstr, MAXNOC * MAXNOR,
175                     EFF_TYPED | EFF_OWNER),
176      realm_oninit, NULL, NULL, NULL},
177     {EF_GAME, "game", "game", game_ca, EF_BAD,
178      UNMAPPED_CACHE(struct gamestr, 1, EFF_TYPED),
179      game_oninit, NULL, NULL, NULL},
180
181     /*
182      * Static game data (configuration)
183      */
184     /*
185      * Characteristics tables.  Characteristics with a null file
186      * member are compiled in.  The others are empty; use
187      * read_builtin_tables() to fill them.
188      */
189     {EF_ITEM, "item", "item.config", ichr_ca, EF_BAD,
190      ARRAY_CACHE(ichr, EFF_CFG), NULL, NULL, NULL, NULL},
191     {EF_PRODUCT, "product", "product.config", pchr_ca, EF_BAD,
192      ARRAY_CACHE(pchr, EFF_CFG), NULL, NULL, NULL, NULL},
193     {EF_SECTOR_CHR, "sect-chr", "sect.config", dchr_ca, EF_BAD,
194      ARRAY_CACHE(dchr, EFF_CFG), NULL, NULL, NULL, NULL},
195     {EF_SHIP_CHR, "ship-chr", "ship.config", mchr_ca, EF_BAD,
196      ARRAY_CACHE(mchr, EFF_CFG), NULL, NULL, NULL, NULL},
197     {EF_PLANE_CHR, "plane-chr", "plane.config", plchr_ca, EF_BAD,
198      ARRAY_CACHE(plchr, EFF_CFG), NULL, NULL, NULL, NULL},
199     {EF_LAND_CHR, "land-chr", "land.config", lchr_ca, EF_BAD,
200      ARRAY_CACHE(lchr, EFF_CFG), NULL, NULL, NULL, NULL},
201     {EF_NUKE_CHR, "nuke-chr", "nuke.config", nchr_ca, EF_BAD,
202      ARRAY_CACHE(nchr, EFF_CFG), NULL, NULL, NULL, NULL},
203     {EF_NEWS_CHR, "news-chr", NULL, rpt_ca, EF_BAD,
204      ARRAY_TABLE(rpt, N_MAX_VERB + 1, EFF_CFG), NULL, NULL, NULL, NULL},
205     {EF_INFRASTRUCTURE, "infrastructure", "infra.config", intrchr_ca, EF_BAD,
206      ARRAY_CACHE(intrchr, EFF_CFG), NULL, NULL, NULL, NULL},
207     /*
208      * Update schedule table.  Use read_schedule() to fill.
209      */
210     {EF_UPDATES, "updates", NULL, update_ca, EF_BAD,
211      ARRAY_CACHE(update_time, EFF_CFG),
212      NULL, NULL, NULL, NULL},
213     /*
214      * Special tables.  EF_META gets bogus size, cids and fids here.
215      * Fixed up by empfile_init().  EF_VERSION's cadef is set by
216      * nsc_init().
217      */
218     {EF_TABLE, "table", NULL, empfile_ca, EF_BAD,
219      ARRAY_TABLE(empfile, EF_MAX, EFF_CFG),
220      NULL, NULL, NULL, NULL},
221     {EF_VERSION, "version", NULL, NULL, EF_BAD,
222      sizeof(PACKAGE_STRING), -1, EFF_STATIC, version, 1, 0, 1, 1, -1,
223      NULL, NULL, NULL, NULL},
224     {EF_META, "meta", NULL, mdchr_ca, EF_BAD,
225      PTR_CACHE(mdchr_ca, EFF_CFG),
226      NULL, NULL, NULL, NULL},
227
228     /*
229      * Symbol tables
230      *
231      * These get bogus csize, cids and fids here.  Fixed up by
232      * empfile_init().
233      */
234 #define SYMTAB(type, name, tab) {(type), (name), NULL, symbol_ca, EF_BAD, \
235                                  PTR_CACHE((tab), EFF_CFG),             \
236                                  NULL, NULL, NULL, NULL}
237     SYMTAB(EF_AGREEMENT_STATUS, "agreement-status", agreement_statuses),
238     SYMTAB(EF_LAND_CHR_FLAGS, "land-chr-flags", land_chr_flags),
239     SYMTAB(EF_LEVEL, "level", level),
240     SYMTAB(EF_META_FLAGS, "meta-flags", meta_flags),
241     SYMTAB(EF_META_TYPE, "meta-type", meta_type),
242     SYMTAB(EF_MISSIONS, "missions", missions),
243     SYMTAB(EF_NATION_FLAGS, "nation-flags", nation_flags),
244     SYMTAB(EF_NATION_REJECTS, "nation-rejects", nation_rejects),
245     SYMTAB(EF_NATION_RELATIONS, "nation-relationships", nation_relations),
246     SYMTAB(EF_NATION_STATUS, "nation-status", nation_status),
247     SYMTAB(EF_NUKE_CHR_FLAGS, "nuke-chr-flags", nuke_chr_flags),
248     SYMTAB(EF_PACKING, "packing", packing),
249     SYMTAB(EF_PAGE_HEADINGS, "page-headings", page_headings),
250     SYMTAB(EF_PLAGUE_STAGES, "plague-stages", plague_stages),
251     SYMTAB(EF_PLANE_CHR_FLAGS, "plane-chr-flags", plane_chr_flags),
252     SYMTAB(EF_PLANE_FLAGS, "plane-flags", plane_flags),
253     SYMTAB(EF_RESOURCES, "resources", resources),
254     SYMTAB(EF_RETREAT_FLAGS, "retreat-flags", retreat_flags),
255     SYMTAB(EF_SECTOR_NAVIGATION, "sector-navigation", sector_navigation),
256     SYMTAB(EF_SHIP_CHR_FLAGS, "ship-chr-flags", ship_chr_flags),
257     SYMTAB(EF_TREATY_FLAGS, "treaty-flags", treaty_flags),
258
259     /* Views */
260     {EF_COUNTRY, "country", NULL, cou_ca, EF_NATION,
261      UNMAPPED_CACHE(struct natstr, MAXNOC, EFF_TYPED | EFF_OWNER),
262      NULL, NULL, NULL, NULL},
263
264     /* Sentinel */
265     {EF_BAD, NULL, NULL, NULL, EF_BAD,
266      0, -1, 0, NULL, 0, 0, 0, 0, -1, NULL, NULL, NULL, NULL},
267 };
268
269 static void
270 sct_oninit(void *ptr)
271 {
272     struct sctstr *sp = (struct sctstr *)ptr;
273
274     sp->sct_y = sp->sct_uid * 2 / WORLD_X;
275     sp->sct_x = sp->sct_uid * 2 % WORLD_X + sp->sct_y % 2;
276     sp->sct_dist_x = sp->sct_x;
277     sp->sct_dist_y = sp->sct_y;
278     sp->sct_newtype = sp->sct_type = SCT_WATER;
279     sp->sct_coastal = 1;
280 }
281
282 static void
283 nat_oninit(void *ptr)
284 {
285     struct natstr *np = ptr;
286
287     np->nat_cnum = np->nat_uid;
288 }
289
290 static void
291 realm_oninit(void *ptr)
292 {
293     struct realmstr *realm = ptr;
294
295     realm->r_cnum = realm->r_uid / MAXNOR;
296     realm->r_realm = realm->r_uid % MAXNOR;
297 }
298
299 static void
300 game_oninit(void *ptr)
301 {
302     ((struct gamestr *)ptr)->game_turn = 1;
303 }
304
305 static void
306 ef_fix_size(struct empfile *ep, int n)
307 {
308     ep->nent = ep->cids = ep->fids = n;
309     ep->csize = n + 1;
310 }
311
312 void
313 empfile_init(void)
314 {
315     struct castr *ca;
316     struct empfile *ep;
317     struct symbol *lup;
318     int i;
319
320     ca = (struct castr *)empfile[EF_META].cache;
321     for (i = 0; ca[i].ca_name; i++) ;
322     ef_fix_size(&empfile[EF_META], i);
323
324     for (ep = empfile; ep->uid >= 0; ep++) {
325         if (ep->cadef == symbol_ca) {
326             lup = (struct symbol *)ep->cache;
327             for (i = 0; lup[i].name; i++) ;
328             ef_fix_size(ep, i);
329         }
330     }
331 }
332
333 void
334 empfile_fixup(void)
335 {
336     empfile[EF_SECTOR].nent = WORLD_SZ();
337     empfile[EF_MAP].size = empfile[EF_BMAP].size = WORLD_SZ();
338 }