]> git.pond.sub.org Git - empserver/blob - src/lib/common/filetable.c
23d2272fbbdd83dcdad1229a30db449bfafb0dbf
[empserver] / src / lib / common / filetable.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2016, 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-2016
31  */
32
33 #include <config.h>
34
35 #include <assert.h>
36 #include <stddef.h>
37 #include <string.h>
38 #include "commodity.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 "xy.h"
56
57 static void sct_oninit(void *);
58 static void pln_oninit(void *);
59 static void lnd_oninit(void *);
60 static void nuk_oninit(void *);
61 static void nat_oninit(void *);
62 static void contact_oninit(void *);
63 static void realm_oninit(void *);
64 static void game_oninit(void *);
65 static void pchr_oninit(void *);
66 static void mchr_oninit(void *);
67 static void plchr_oninit(void *);
68 static void lchr_oninit(void *);
69 static void nchr_oninit(void *);
70
71 static char dummy_cache;
72
73 /* Number of elements in ARRAY. */
74 #define SZ(array) (sizeof(array) / sizeof((array)[0]))
75
76 /* Initializers for members flags... */
77 /* Unmapped cache */
78 #define UNMAPPED_CACHE(type, nent, flags)       \
79     sizeof(type), (nent), (flags), NULL,        \
80     0, 0, 0, 0, -1
81 /*
82  * Mapped cache, array with known size.
83  * Members cids, fids are zero, i.e. cache is empty.
84  */
85 #define ARRAY_CACHE(array, flags) \
86     sizeof(*(array)), -1, (flags), (char *)(array),     \
87     SZ((array)), 0, 0, 0, -1
88 /*
89  * Mapped cache, array with unknown size.
90  * Member csize gets a bogus value, needs to be fixed up.
91  * Members cids, fids are zero, i.e. cache is empty.
92  */
93 #define PTR_CACHE(ptr, flags) \
94     sizeof(*(ptr)), -1, (flags), (char *)(ptr), \
95     0, 0, 0, 0, -1
96 /*
97  * Array-backed table of fixed size.
98  */
99 #define ARRAY_TABLE(array, nent, flags)                 \
100     sizeof(*(array)), (nent), (flags), (char *)(array), \
101     SZ((array)), 0, (nent), (nent), -1
102
103 /* Common configuration table flags */
104 #define EFF_CFG (EFF_PRIVATE | EFF_MEM | EFF_STATIC | EFF_SENTINEL)
105
106 struct empfile empfile[] = {
107     /*
108      * How this initializer works:
109      *
110      * Members uid, name, file, cadef, size, and the EFF_IMMUTABLE
111      * bits of flags get their final value.
112      * If flags & EFF_STATIC, the cache is mapped here, and members
113      * cache, csize get their final value.
114      * Members baseid, cids, fids and the EFF_MEM|EFF_PRIVATE bits of
115      * flags are initialized according the initial cache contents.
116      * Member fd is initialized to -1.
117      * Members init, postread, prewrite get initialized to NULL, but
118      * that can be changed by users.
119      *
120      * Whatever of the above can't be done here must be done in
121      * empfile_init() or empfile_fixup().  Except cadef may be set in
122      * nsc_init() instead.
123      */
124
125     /*
126      * Keep in mind that player command arguments are matched against
127      * values of member name: no whitespace there, please.
128      */
129
130     /*
131      * Dynamic game data
132      *
133      * All caches unmapped.  EF_SECTOR gets bogus nent here.  EF_MAP
134      * and EF_BMAP get a bogus size here.  Fixed up by
135      * empfile_fixup().
136      */
137     {EF_SECTOR, "sect", "sector", "sector", sect_ca, EF_BAD,
138      UNMAPPED_CACHE(struct sctstr, -1, EFF_TYPED | EFF_XY | EFF_OWNER),
139      sct_oninit, NULL, NULL, NULL},
140     {EF_SHIP, "ship", NULL, "ship", ship_ca, EF_BAD,
141      UNMAPPED_CACHE(struct shpstr, -1,
142                     EFF_TYPED | EFF_XY | EFF_OWNER | EFF_GROUP),
143      NULL, NULL, NULL, NULL},
144     {EF_PLANE, "plane", NULL, "plane", plane_ca, EF_BAD,
145      UNMAPPED_CACHE(struct plnstr, -1,
146                     EFF_TYPED | EFF_XY | EFF_OWNER | EFF_GROUP),
147      pln_oninit, NULL, NULL, NULL},
148     {EF_LAND, "land", "land unit", "land", land_ca, EF_BAD,
149      UNMAPPED_CACHE(struct lndstr, -1,
150                     EFF_TYPED | EFF_XY | EFF_OWNER | EFF_GROUP),
151      lnd_oninit, NULL, NULL, NULL},
152     {EF_NUKE, "nuke", NULL, "nuke", nuke_ca, EF_BAD,
153      UNMAPPED_CACHE(struct nukstr, -1,
154                     EFF_TYPED | EFF_XY | EFF_OWNER | EFF_GROUP),
155      nuk_oninit, NULL, NULL, NULL},
156     {EF_NEWS, "news", NULL, "news", news_ca, EF_BAD,
157      UNMAPPED_CACHE(struct nwsstr, -1, 0),
158      NULL, NULL, NULL, NULL},
159     {EF_TRADE, "trade", "trade lot", "trade", trade_ca, EF_BAD,
160      UNMAPPED_CACHE(struct trdstr, -1, EFF_TYPED | EFF_OWNER),
161      NULL, NULL, NULL, NULL},
162     {EF_POWER, "pow", NULL, "power", NULL, EF_BAD,
163      UNMAPPED_CACHE(struct powstr, -1, 0),
164      NULL, NULL, NULL, NULL},
165     {EF_NATION, "nat", "nation", "nation", nat_ca, EF_BAD,
166      UNMAPPED_CACHE(struct natstr, MAXNOC, EFF_TYPED | EFF_OWNER),
167      nat_oninit, NULL, NULL, NULL},
168     {EF_RELAT, "relat", NULL, "relat", relat_ca, EF_BAD,
169      UNMAPPED_CACHE(struct relatstr, MAXNOC, EFF_TYPED),
170      NULL, NULL, NULL, NULL},
171     {EF_CONTACT, "contact", NULL, "contact", contact_ca, EF_BAD,
172      UNMAPPED_CACHE(struct contactstr, MAXNOC, EFF_TYPED),
173      contact_oninit, NULL, NULL, NULL},
174     {EF_REJECT, "reject", NULL, "reject", reject_ca, EF_BAD,
175      UNMAPPED_CACHE(struct rejectstr, MAXNOC, EFF_TYPED),
176      NULL, NULL, NULL, NULL},
177     {EF_LOAN, "loan", NULL, "loan", loan_ca, EF_BAD,
178      UNMAPPED_CACHE(struct lonstr, -1, EFF_TYPED),
179      NULL, NULL, NULL, NULL},
180     {EF_MAP, "map", NULL, "map", NULL, EF_BAD,
181      0, MAXNOC, 0, NULL, 0, 0, 0, 0, -1, NULL, NULL, NULL, NULL},
182     {EF_BMAP, "bmap", NULL, "bmap", NULL, EF_BAD,
183      0, MAXNOC, 0, NULL, 0, 0, 0, 0, -1, NULL, NULL, NULL, NULL},
184     {EF_COMM, "commodity", NULL, "commodity", commodity_ca, EF_BAD,
185      UNMAPPED_CACHE(struct comstr, -1, EFF_TYPED | EFF_OWNER),
186      NULL, NULL, NULL, NULL},
187     {EF_LOST, "lost", "lost item", "lostitems", lost_ca, EF_BAD,
188      UNMAPPED_CACHE(struct loststr, -1, EFF_TYPED | EFF_OWNER),
189      NULL, NULL, NULL, NULL},
190     {EF_REALM, "realm", NULL, "realms", realm_ca, EF_BAD,
191      UNMAPPED_CACHE(struct realmstr, MAXNOC * MAXNOR,
192                     EFF_TYPED | EFF_OWNER),
193      realm_oninit, NULL, NULL, NULL},
194     {EF_GAME, "game", NULL, "game", game_ca, EF_BAD,
195      UNMAPPED_CACHE(struct gamestr, 1, EFF_TYPED),
196      game_oninit, NULL, NULL, NULL},
197
198     /*
199      * Static game data (configuration)
200      */
201     /*
202      * Characteristics tables.  Characteristics with a null file
203      * member are compiled in.  The others are empty; use
204      * read_builtin_tables() to fill them.
205      */
206     {EF_ITEM, "item", NULL, "item.config", ichr_ca, EF_BAD,
207      ARRAY_CACHE(ichr, EFF_CFG), NULL, NULL, NULL, NULL},
208     {EF_PRODUCT, "product", NULL, "product.config", pchr_ca, EF_BAD,
209      ARRAY_CACHE(pchr, EFF_CFG), pchr_oninit, NULL, NULL, NULL},
210     {EF_SECTOR_CHR, "sect-chr", NULL, "sect.config", dchr_ca, EF_BAD,
211      ARRAY_CACHE(dchr, EFF_CFG), NULL, NULL, NULL, NULL},
212     {EF_SHIP_CHR, "ship-chr", NULL, "ship.config", mchr_ca, EF_BAD,
213      ARRAY_CACHE(mchr, EFF_CFG), mchr_oninit, NULL, NULL, NULL},
214     {EF_PLANE_CHR, "plane-chr", NULL, "plane.config", plchr_ca, EF_BAD,
215      ARRAY_CACHE(plchr, EFF_CFG), plchr_oninit, NULL, NULL, NULL},
216     {EF_LAND_CHR, "land-chr", NULL, "land.config", lchr_ca, EF_BAD,
217      ARRAY_CACHE(lchr, EFF_CFG), lchr_oninit, NULL, NULL, NULL},
218     {EF_NUKE_CHR, "nuke-chr", NULL, "nuke.config", nchr_ca, EF_BAD,
219      ARRAY_CACHE(nchr, EFF_CFG), nchr_oninit, NULL, NULL, NULL},
220     {EF_NEWS_CHR, "news-chr", NULL, NULL, rpt_ca, EF_BAD,
221      ARRAY_TABLE(rpt, N_MAX_VERB + 1, EFF_CFG), NULL, NULL, NULL, NULL},
222     {EF_INFRASTRUCTURE, "infrastructure", NULL, "infra.config",
223      intrchr_ca, EF_BAD,
224      ARRAY_CACHE(intrchr, EFF_CFG), NULL, NULL, NULL, NULL},
225     /*
226      * Update schedule table.  Use read_schedule() to fill.
227      */
228     {EF_UPDATES, "updates", NULL, NULL, update_ca, EF_BAD,
229      ARRAY_CACHE(update_time, EFF_CFG),
230      NULL, NULL, NULL, NULL},
231     /*
232      * Special tables.  EF_META gets bogus size, cids and fids here.
233      * Fixed up by empfile_init().  EF_VERSION's cadef is set by
234      * nsc_init().
235      */
236     {EF_TABLE, "table", NULL, NULL, empfile_ca, EF_BAD,
237      ARRAY_TABLE(empfile, EF_MAX, EFF_CFG),
238      NULL, NULL, NULL, NULL},
239     {EF_VERSION, "version", NULL, NULL, NULL, EF_BAD,
240      0, -1, EFF_MEM | EFF_STATIC, &dummy_cache, 1, 0, 1, 1, -1,
241      NULL, NULL, NULL, NULL},
242     {EF_META, "meta", NULL, NULL, mdchr_ca, EF_BAD,
243      PTR_CACHE(mdchr_ca, EFF_CFG),
244      NULL, NULL, NULL, NULL},
245
246     /*
247      * Symbol tables
248      *
249      * These get bogus csize, cids and fids here.  Fixed up by
250      * empfile_init().
251      */
252 #define SYMTAB(type, name, tab)                         \
253     {(type), (name), NULL, NULL, symbol_ca, EF_BAD,     \
254      PTR_CACHE((tab), EFF_CFG), NULL, NULL, NULL, NULL}
255
256     SYMTAB(EF_AGREEMENT_STATUS, "agreement-status", agreement_statuses),
257     SYMTAB(EF_LAND_CHR_FLAGS, "land-chr-flags", land_chr_flags),
258     SYMTAB(EF_LEVEL, "level", level),
259     SYMTAB(EF_META_FLAGS, "meta-flags", meta_flags),
260     SYMTAB(EF_META_TYPE, "meta-type", meta_type),
261     SYMTAB(EF_MISSIONS, "missions", missions),
262     SYMTAB(EF_NATION_FLAGS, "nation-flags", nation_flags),
263     SYMTAB(EF_NATION_REJECTS, "nation-rejects", nation_rejects),
264     SYMTAB(EF_NATION_RELATIONS, "nation-relationships", nation_relations),
265     SYMTAB(EF_NATION_STATUS, "nation-status", nation_status),
266     SYMTAB(EF_NUKE_CHR_FLAGS, "nuke-chr-flags", nuke_chr_flags),
267     SYMTAB(EF_PACKING, "packing", packing),
268     SYMTAB(EF_PAGE_HEADINGS, "page-headings", page_headings),
269     SYMTAB(EF_PLAGUE_STAGES, "plague-stages", plague_stages),
270     SYMTAB(EF_PLANE_CHR_FLAGS, "plane-chr-flags", plane_chr_flags),
271     SYMTAB(EF_PLANE_FLAGS, "plane-flags", plane_flags),
272     SYMTAB(EF_RESOURCES, "resources", resources),
273     SYMTAB(EF_RETREAT_FLAGS, "retreat-flags", retreat_flags),
274     SYMTAB(EF_SECTOR_NAVIGATION, "sector-navigation", sector_navigation),
275     SYMTAB(EF_SECTOR_CHR_FLAGS, "sect-chr-flags", sect_chr_flags),
276     SYMTAB(EF_SHIP_CHR_FLAGS, "ship-chr-flags", ship_chr_flags),
277
278     /* Views */
279     {EF_COUNTRY, "country", NULL, NULL, cou_ca, EF_NATION,
280      UNMAPPED_CACHE(struct natstr, MAXNOC, EFF_TYPED | EFF_OWNER),
281      NULL, NULL, NULL, NULL},
282
283     /* Sentinel */
284     {EF_BAD, NULL, NULL, NULL, NULL, EF_BAD,
285      0, -1, 0, NULL, 0, 0, 0, 0, -1, NULL, NULL, NULL, NULL},
286 };
287
288 static void
289 sct_oninit(void *ptr)
290 {
291     struct sctstr *sp = (struct sctstr *)ptr;
292
293     sctoff2xy(&sp->sct_x, &sp->sct_y, sp->sct_uid);
294     sp->sct_dist_x = sp->sct_x;
295     sp->sct_dist_y = sp->sct_y;
296     sp->sct_newtype = sp->sct_type = SCT_WATER;
297     sp->sct_work = 100;
298     sp->sct_coastal = 1;
299 }
300
301 static void
302 pln_oninit(void *ptr)
303 {
304     struct plnstr *pp = ptr;
305
306     pp->pln_ship = pp->pln_land = -1;
307 }
308
309 static void
310 lnd_oninit(void *ptr)
311 {
312     struct lndstr *lp = ptr;
313
314     lp->lnd_ship = lp->lnd_land = -1;
315 }
316
317 static void
318 nuk_oninit(void *ptr)
319 {
320     struct nukstr *np = ptr;
321
322     np->nuk_plane = -1;
323 }
324
325 static void
326 nat_oninit(void *ptr)
327 {
328     struct natstr *np = ptr;
329
330     np->nat_cnum = np->nat_uid;
331 }
332
333 static void
334 contact_oninit(void *ptr)
335 {
336     struct contactstr *cp = ptr;
337
338     if (opt_HIDDEN)
339         cp->con_contact[cp->con_uid] = 1;
340     else
341         memset(cp->con_contact, 1, sizeof(cp->con_contact));
342 }
343
344 static void
345 realm_oninit(void *ptr)
346 {
347     struct realmstr *realm = ptr;
348
349     realm->r_cnum = realm->r_uid / MAXNOR;
350     realm->r_realm = realm->r_uid % MAXNOR;
351 }
352
353 static void
354 game_oninit(void *ptr)
355 {
356     ((struct gamestr *)ptr)->game_turn = 1;
357 }
358
359 static void
360 pchr_oninit(void *ptr)
361 {
362     ((struct pchrstr *)ptr)->p_sname = "";
363 }
364
365 static void
366 mchr_oninit(void *ptr)
367 {
368     ((struct mchrstr *)ptr)->m_name = "";
369 }
370
371 static void
372 plchr_oninit(void *ptr)
373 {
374     ((struct plchrstr *)ptr)->pl_name = "";
375 }
376
377 static void
378 lchr_oninit(void *ptr)
379 {
380     ((struct lchrstr *)ptr)->l_name = "";
381 }
382
383 static void
384 nchr_oninit(void *ptr)
385 {
386     ((struct nchrstr *)ptr)->n_name = "";
387 }
388
389 static void
390 ef_fix_size(struct empfile *ep, int n)
391 {
392     ep->nent = ep->cids = ep->fids = n;
393     ep->csize = n + 1;
394 }
395
396 void
397 empfile_init(void)
398 {
399     struct castr *ca;
400     struct empfile *ep;
401     struct symbol *lup;
402     int i;
403
404     ca = (struct castr *)empfile[EF_META].cache;
405     for (i = 0; ca[i].ca_name; i++) ;
406     ef_fix_size(&empfile[EF_META], i);
407
408     for (ep = empfile; ep->uid >= 0; ep++) {
409         if (!ep->pretty_name)
410             ep->pretty_name = ep->name;
411         if (ep->cadef == symbol_ca) {
412             lup = (struct symbol *)ep->cache;
413             for (i = 0; lup[i].name; i++) ;
414             ef_fix_size(ep, i);
415         }
416     }
417 }
418
419 void
420 empfile_fixup(void)
421 {
422     struct empfile *ep;
423
424     empfile[EF_SECTOR].nent = WORLD_SZ();
425     empfile[EF_MAP].size = empfile[EF_BMAP].size = WORLD_SZ();
426
427     for (ep = empfile; ep->uid >= 0; ep++)
428         assert(!ep->cadef || ep->size <= EF_WITH_CADEF_MAX_ENTRY_SIZE);
429 }