]> git.pond.sub.org Git - empserver/blob - src/util/empdump.c
New utility program empdump
[empserver] / src / util / empdump.c
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  *  empdump.c: Export/import Empire game state
29  * 
30  *  Known contributors to this file:
31  *     Markus Armbruster, 2008
32  */
33
34 #include <config.h>
35
36 #include <errno.h>
37 #include <stdarg.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include "file.h"
42 #include "optlist.h"
43 #include "prototypes.h"
44 #include "version.h"
45 #include "xdump.h"
46
47 static void dump_table(int, int);
48 static void pln_fixup(void);
49 static void lnd_fixup(void);
50 static void nuk_fixup(void);
51
52 int
53 main(int argc, char *argv[])
54 {
55     char *config_file = NULL;
56     char *import = NULL;
57     int export = 0;
58     int private = 0;
59     int human = 1;
60     int opt, i, lineno, type;
61     FILE *impf = NULL;
62     int dirty[EF_MAX];
63
64     while ((opt = getopt(argc, argv, "e:mtxhv")) != EOF) {
65         switch (opt) {
66         case 'e':
67             config_file = optarg;
68             break;
69         case 'h':
70             printf("Usage: %s [OPTION]... [DUMP-FILE]\n"
71                    "  -e CONFIG-FILE  configuration file\n"
72                    "                  (default %s)\n"
73                    "  -m              use machine-readable format\n"
74                    "  -t              test import, don't update game state\n"
75                    "  -x              export to standard output\n"
76                    "  -h              display this help and exit\n"
77                    "  -v              display version information and exit\n",
78                    argv[0], dflt_econfig);
79             exit(0);
80         case 'm':
81             human = 0;
82             break;
83         case 't':
84             private = EFF_PRIVATE;
85             break;
86         case 'x':
87             export = 1;
88             break;
89         case 'v':
90             printf("%s\n\n%s", version, legal);
91             exit(0);
92         default:
93             fprintf(stderr, "Try -h for help.\n");
94             exit(1);
95         }
96     }
97
98     if (argv[optind])
99         import = argv[optind++];
100
101     if (import) {
102         impf = fopen(import, "r");
103         if (!impf) {
104             fprintf(stderr, "Cant open %s for reading (%s)\n",
105                     import, strerror(errno));
106             exit(1);
107         }
108     } else
109         private = EFF_PRIVATE;
110
111     /* read configuration & initialize */
112     empfile_init();
113     if (emp_config(config_file) < 0)
114         exit(1);
115     empfile_fixup();
116     nsc_init();
117     if (read_builtin_tables() < 0)
118         exit(1);
119     if (read_custom_tables() < 0)
120         exit(1);
121     if (chdir(gamedir)) {
122         fprintf(stderr, "Can't chdir to %s (%s)\n",
123                 gamedir, strerror(errno));
124         exit(1);
125     }
126     global_init();
127
128     for (i = 0; i < EF_MAX; i++) {
129         if (!EF_IS_GAME_STATE(i))
130             continue;
131         if (!ef_open(i, EFF_MEM | private))
132             exit(1);
133     }
134
135     /* import from IMPORT */
136     memset(dirty, 0, sizeof(dirty));
137     if (import) {
138         lineno = 1;
139         while ((type = xundump(impf, import, &lineno, EF_BAD)) >= 0)
140             dirty[type] = 1;
141         if (type == EF_BAD)
142             exit(1);
143         pln_fixup();
144         lnd_fixup();
145         nuk_fixup();
146     }
147
148     if (ef_verify() < 0)
149         exit(1);
150
151     /* export to stdout */
152     if (export) {
153         for (i = 0; i < EF_MAX; i++) {
154             if (!EF_IS_GAME_STATE(i))
155                 continue;
156             dump_table(i, human);
157         }
158     }
159
160     /* write out imported data */
161     for (i = 0; i < EF_MAX; i++) {
162         if (!EF_IS_GAME_STATE(i))
163             continue;
164         if (!private && dirty[i]) {
165             if (!ef_close(i))
166                 exit(1);
167         }
168     }
169
170     return 0;
171 }
172
173 static void
174 printf_wrapper(char *fmt, ...)
175 {
176     va_list ap;
177
178     va_start(ap, fmt);
179     vprintf(fmt, ap);
180     va_end(ap);
181 }
182
183 static void
184 dump_table(int type, int human)
185 {
186     struct xdstr xd;
187     struct castr *ca;
188     int i;
189     void *p;
190
191     ca = ef_cadef(type);
192     if (!ca)
193         return;
194
195     xdinit(&xd, 0, human, printf_wrapper);
196     xdhdr(&xd, ef_nameof(type), 0);
197     xdcolhdr(&xd, ca);
198     for (i = 0; (p = ef_ptr(type, i)); i++) {
199         xdflds(&xd, ca, p);
200         printf("\n");
201     }
202     xdftr(&xd, i);
203 }
204
205
206 /* TODO remove need for this */
207
208 #include <math.h>
209 #include "ship.h"
210 #include "plane.h"
211 #include "land.h"
212 #include "nuke.h"
213
214 static int fit_plane_on_ship(struct plnstr *, struct shpstr *);
215 static int fit_plane_on_land(struct plnstr *, struct lndstr *);
216
217 static void
218 pln_fixup(void)
219 {
220     int i;
221     struct plnstr *pp;
222     struct shpstr *csp;
223     struct lndstr *clp;
224
225     for (i = 0; (pp = ef_ptr(EF_PLANE, i)); i++) {
226         if (!pp->pln_own)
227             continue;
228         csp = ef_ptr(EF_SHIP, pp->pln_ship);
229         clp = ef_ptr(EF_LAND, pp->pln_land);
230         if (csp)
231             fit_plane_on_ship(pp, csp);
232         else if (clp)
233             fit_plane_on_land(pp, clp);
234     }
235 }
236
237 static void
238 lnd_fixup(void)
239 {
240     int i;
241     struct lndstr *lp;
242     struct shpstr *csp;
243     struct lndstr *clp;
244
245     for (i = 0; (lp = ef_ptr(EF_LAND, i)); i++) {
246         if (!lp->lnd_own)
247             continue;
248         csp = ef_ptr(EF_SHIP, lp->lnd_ship);
249         clp = ef_ptr(EF_LAND, lp->lnd_land);
250         if (csp)
251             csp->shp_nland++;
252         else if (clp)
253             clp->lnd_nland++;
254     }
255 }
256
257 static void
258 nuk_fixup(void)
259 {
260     int i;
261     struct nukstr *np;
262     struct plnstr *cpp;
263
264     for (i = 0; (np = ef_ptr(EF_NUKE, i)); i++) {
265         if (!np->nuk_own)
266             continue;
267         cpp = ef_ptr(EF_PLANE, np->nuk_plane);
268         if (cpp)
269             cpp->pln_nuketype = np->nuk_type;
270     }
271 }
272
273 /* Temporarily copied from src/lib/subs/???sub.c */
274
275 /*
276  * Fit a plane of PP's type on ship SP.
277  * Adjust SP's plane counters.
278  * Updating the plane accordingly is the caller's job.
279  * Return whether it fits.
280  */
281 static int
282 fit_plane_on_ship(struct plnstr *pp, struct shpstr *sp)
283 {
284     struct plchrstr *pcp = plchr + pp->pln_type;
285     struct mchrstr *mcp = mchr + sp->shp_type;
286     int wanted;
287
288     if (pcp->pl_flags & P_K) {
289         /* chopper, try chopper slot first */
290         if (sp->shp_nchoppers < mcp->m_nchoppers)
291             return ++sp->shp_nchoppers;
292         /* else try plane slot */
293         wanted = M_FLY;
294     } else if (pcp->pl_flags & P_E) {
295         /* x-light, try x-light slot first */
296         if (sp->shp_nxlight < mcp->m_nxlight)
297             return ++sp->shp_nxlight;
298         /* else try plane slot */
299         wanted = M_MSL | M_FLY;
300     } else if (!(pcp->pl_flags & P_L)) {
301         /* not light, no go */
302         wanted = 0;
303     } else if (pcp->pl_flags & P_M) {
304         /* missile, use plane slot */
305         wanted = M_MSL | M_FLY;
306     } else {
307         /* fixed-wing plane, use plane slot */
308         wanted = M_FLY;
309     }
310
311     if ((mcp->m_flags & wanted) == 0)
312         return 0;               /* ship not capable */
313
314     if (sp->shp_nplane < mcp->m_nplanes)
315         return ++sp->shp_nplane;
316
317     return 0;
318 }
319
320 /*
321  * Fit a plane of PP's type on land unit LP.
322  * Adjust LP's plane counters.
323  * Updating the plane accordingly is the caller's job.
324  * Return whether it fits.
325  */
326 static int
327 fit_plane_on_land(struct plnstr *pp, struct lndstr *lp)
328 {
329     struct plchrstr *pcp = plchr + pp->pln_type;
330     struct lchrstr *lcp = lchr + lp->lnd_type;
331
332     if ((pcp->pl_flags & P_E) && lp->lnd_nxlight < lcp->l_nxlight)
333         return ++lp->lnd_nxlight;
334
335     return 0;
336 }