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