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