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