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