]> git.pond.sub.org Git - empserver/blob - include/file.h
New ef_blank()
[empserver] / include / file.h
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program 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 2 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, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  file.h: Describes Empire tables (`files' for historical reasons)
29  * 
30  *  Known contributors to this file:
31  *     Markus Armbruster, 2005
32  */
33
34 #ifndef FILE_H
35 #define FILE_H
36
37 #include <time.h>
38
39 struct empfile {
40     /* Members with immutable values */
41     int uid;                    /* Table ID */
42     char *name;                 /* Empire name (e.g., "treaty") */
43     char *file;                 /* if backed by file, file name relative to
44                                    data directory */
45     struct castr *cadef;        /* table column selectors (column meta-data) */
46     int size;                   /* size of a table entry */
47     int flags;                  /* only EFF_IMMUTABLE immutable, see below
48                                    for use of remaining bits */
49
50     /* Members whose values are fixed when the cache is mapped */
51     char *cache;                /* pointer to cache */
52     int csize;                  /* cache size, in entries */
53     /* flags bit EFF_MEM also fixed then */
54
55     /* Members whose values may vary throughout operation */
56     int baseid;                 /* id of first entry in cache */
57     int cids;                   /* # entries in cache */
58     int fids;                   /* # entries in table */
59     int fd;                     /* file descriptor, -1 if not open */
60     /* flags bits EFF_PRIVATE, EFF_CUSTOM also vary */
61
62     /* User callbacks */
63     int (*postread)(int, void *); /* called after read, unless null */
64     int (*prewrite)(int, void *); /* called before write, unless null */
65 };
66
67 struct emptypedstr {
68     short ef_type;
69     short uid;
70 };
71
72 /*
73  * Flag bits for struct empfile member flags
74  * Immutable flags are properties of the table and thus cannot change.
75  * The remaining flags record how the table is being used.
76  */
77 /* Immutable flags, fixed at compile-time */
78 /* Dereferencing entry address cast to struct emptypedstr * is safe */
79 #define EFF_TYPED       bit(0)
80 /*
81  * EFF_XY / EFF_OWNER / EFF_GROUP assert that coordinates / owner /
82  * group of such a table's entries can be safely obtained by
83  * dereferencing the entry's address cast to struct empobj *.
84  */
85 #define EFF_XY          bit(1)
86 #define EFF_OWNER       bit(2)
87 #define EFF_GROUP       bit(3)
88 /* Table is allocated statically */
89 #define EFF_STATIC      bit(4)
90 /* All the immutable flags */
91 #define EFF_IMMUTABLE \
92     (EFF_TYPED | EFF_XY | EFF_OWNER | EFF_GROUP | EFF_STATIC)
93 /* Flags set when table contents is mapped */
94 /* Table is entirely in memory */
95 #define EFF_MEM         bit(8)
96 /* Table is privately mapped: changes don't affect the underlying file */
97 #define EFF_PRIVATE     bit(9)
98 /* Table is customized (configuration tables only) */
99 #define EFF_CUSTOM      bit(10)
100 /* Transient flags, only occur in argument of ef_open() */
101 /* Create table file, clobbering any existing file */
102 #define EFF_CREATE      bit(11)
103
104 /*
105  * Empire `file types'
106  * These are really table IDs.  Some tables are backed by files, some
107  * are compiled into the server, some initialized from configuration
108  * files.
109  */
110 enum {
111     /* Error value */
112     EF_BAD = -1,
113     /* Dynamic game data tables */
114     EF_SECTOR,
115     EF_SHIP,
116     EF_PLANE,
117     EF_LAND,
118     EF_NUKE,
119     EF_NEWS,
120     EF_TREATY,
121     EF_TRADE,
122     EF_POWER,
123     EF_NATION,
124     EF_LOAN,
125     EF_MAP,
126     EF_BMAP,
127     EF_COMM,
128     EF_LOST,
129     EF_REALM,
130     EF_GAME,
131     EF_DYNMAX = EF_GAME,
132     /* Static game data (configuration) */
133     /* Order is relevant; see read_builtin_tables() */
134     EF_ITEM,
135     EF_PRODUCT,
136     EF_SECTOR_CHR,
137     EF_SHIP_CHR,
138     EF_PLANE_CHR,
139     EF_LAND_CHR,
140     EF_NUKE_CHR,
141     EF_NEWS_CHR,
142     EF_INFRASTRUCTURE,
143     EF_UPDATES,                 /* not actually static */
144     EF_TABLE,
145     EF_META,                    /* not really configuration */
146     /* Symbol tables */
147     EF_AGREEMENT_STATUS,
148     EF_LAND_CHR_FLAGS,
149     EF_LEVEL,
150     EF_META_FLAGS,
151     EF_META_TYPE,
152     EF_MISSIONS,
153     EF_NATION_FLAGS,
154     EF_NATION_REJECTS,
155     EF_NATION_RELATIONS,
156     EF_NATION_STATUS,
157     EF_NUKE_CHR_FLAGS,
158     EF_PACKING,
159     EF_PAGE_HEADINGS,
160     EF_PLAGUE_STAGES,
161     EF_PLANE_CHR_FLAGS,
162     EF_PLANE_FLAGS,
163     EF_RESOURCES,
164     EF_RETREAT_FLAGS,
165     EF_SECTOR_NAVIGATION,
166     EF_SHIP_CHR_FLAGS,
167     EF_TREATY_FLAGS,
168     /* Views */
169     EF_COUNTRY,
170     /* Number of types: */
171     EF_MAX
172 };
173
174 #define EF_IS_GAME_STATE(type) (EF_SECTOR <= (type) && (type) <= EF_DYNMAX)
175 #define EF_IS_VIEW(type) (EF_COUNTRY <= (type) && (type) < EF_MAX)
176
177 extern struct castr *ef_cadef(int);
178 extern int ef_read(int, int, void *);
179 extern void *ef_ptr(int, int);
180 extern char *ef_nameof(int);
181 extern time_t ef_mtime(int);
182 extern int ef_open(int, int);
183 extern int ef_check(int);
184 extern int ef_close(int);
185 extern int ef_flush(int);
186 extern void ef_blank(int, int, void *);
187 extern int ef_write(int, int, void *);
188 extern int ef_extend(int, int);
189 extern int ef_ensure_space(int, int, int);
190 extern int ef_nelem(int);
191 extern int ef_flags(int);
192 extern int ef_byname(char *);
193 extern int ef_byname_from(char *, int *);
194 extern int ef_verify(void);
195 extern int ef_elt_byname(int, char *);
196
197 extern struct empfile empfile[EF_MAX + 1];
198 extern void empfile_init(void);
199 extern void empfile_fixup(void);
200
201 #endif