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