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
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.
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.
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
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.
28 * empdump.c: Export/import Empire game state
30 * Known contributors to this file:
31 * Markus Armbruster, 2008
44 #include "prototypes.h"
48 static void exit_bad_arg(char *, ...) ATTRIBUTE((noreturn));
49 static void dump_table(int, int);
50 static void pln_fixup(void);
51 static void lnd_fixup(void);
52 static void nuk_fixup(void);
55 main(int argc, char *argv[])
57 char *config_file = NULL;
62 int opt, i, lineno, type;
66 while ((opt = getopt(argc, argv, "e:i:mnxhv")) != EOF) {
78 private = EFF_PRIVATE;
84 printf("Usage: %s [OPTION]...\n"
85 " -e CONFIG-FILE configuration file\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);
96 printf("%s\n\n%s", version, legal);
104 exit_bad_arg("%s: extra operand %s\n", argv[0], argv[optind]);
106 if (!import && !export)
107 exit_bad_arg("%s: nothing to do!\n", argv[0]);
110 impf = fopen(import, "r");
112 fprintf(stderr, "%s: Can't open %s for reading (%s)\n",
113 argv[0], import, strerror(errno));
117 private = EFF_PRIVATE;
119 /* read configuration & initialize */
121 if (emp_config(config_file) < 0)
125 if (read_builtin_tables() < 0)
127 if (read_custom_tables() < 0)
129 if (chdir(gamedir)) {
130 fprintf(stderr, "%s: Can't chdir to %s (%s)\n",
131 argv[0], gamedir, strerror(errno));
136 for (i = 0; i < EF_MAX; i++) {
137 if (!EF_IS_GAME_STATE(i))
139 if (!ef_open(i, EFF_MEM | private, -1))
143 /* import from IMPORT */
144 memset(dirty, 0, sizeof(dirty));
147 while ((type = xundump(impf, import, &lineno, EF_BAD)) >= 0)
159 /* export to stdout */
161 for (i = 0; i < EF_MAX; i++) {
162 if (!EF_IS_GAME_STATE(i))
164 dump_table(i, human);
166 if (fclose(stdout) != 0) {
167 fprintf(stderr, "%s: error writing export (%s)\n",
168 argv[0], strerror(errno));
173 /* write out imported data */
174 for (i = 0; i < EF_MAX; i++) {
175 if (!EF_IS_GAME_STATE(i))
177 if (!private && dirty[i]) {
187 exit_bad_arg(char *complaint, ...)
192 va_start(ap, complaint);
193 vfprintf(stderr, complaint, ap);
196 fprintf(stderr, "Try -h for help.\n");
201 printf_wrapper(char *fmt, ...)
211 dump_table(int type, int human)
222 xdinit(&xd, NATID_BAD, human, printf_wrapper);
223 xdhdr(&xd, ef_nameof(type), 0);
225 for (i = 0; (p = ef_ptr(type, i)); i++) {
233 /* TODO remove need for this */
241 static int fit_plane_on_ship(struct plnstr *, struct shpstr *);
242 static int fit_plane_on_land(struct plnstr *, struct lndstr *);
252 for (i = 0; (pp = ef_ptr(EF_PLANE, i)); i++) {
255 csp = ef_ptr(EF_SHIP, pp->pln_ship);
256 clp = ef_ptr(EF_LAND, pp->pln_land);
258 fit_plane_on_ship(pp, csp);
260 fit_plane_on_land(pp, clp);
272 for (i = 0; (lp = ef_ptr(EF_LAND, i)); i++) {
275 csp = ef_ptr(EF_SHIP, lp->lnd_ship);
276 clp = ef_ptr(EF_LAND, lp->lnd_land);
291 for (i = 0; (np = ef_ptr(EF_NUKE, i)); i++) {
294 cpp = ef_ptr(EF_PLANE, np->nuk_plane);
296 cpp->pln_nuketype = np->nuk_type;
300 /* Temporarily copied from src/lib/subs/???sub.c */
303 * Fit a plane of PP's type on ship SP.
304 * Adjust SP's plane counters.
305 * Updating the plane accordingly is the caller's job.
306 * Return whether it fits.
309 fit_plane_on_ship(struct plnstr *pp, struct shpstr *sp)
311 struct plchrstr *pcp = plchr + pp->pln_type;
312 struct mchrstr *mcp = mchr + sp->shp_type;
315 if (pcp->pl_flags & P_K) {
316 /* chopper, try chopper slot first */
317 if (sp->shp_nchoppers < mcp->m_nchoppers)
318 return ++sp->shp_nchoppers;
319 /* else try plane slot */
321 } else if (pcp->pl_flags & P_E) {
322 /* x-light, try x-light slot first */
323 if (sp->shp_nxlight < mcp->m_nxlight)
324 return ++sp->shp_nxlight;
325 /* else try plane slot */
326 wanted = M_MSL | M_FLY;
327 } else if (!(pcp->pl_flags & P_L)) {
328 /* not light, no go */
330 } else if (pcp->pl_flags & P_M) {
331 /* missile, use plane slot */
332 wanted = M_MSL | M_FLY;
334 /* fixed-wing plane, use plane slot */
338 if ((mcp->m_flags & wanted) == 0)
339 return 0; /* ship not capable */
341 if (sp->shp_nplane < mcp->m_nplanes)
342 return ++sp->shp_nplane;
348 * Fit a plane of PP's type on land unit LP.
349 * Adjust LP's plane counters.
350 * Updating the plane accordingly is the caller's job.
351 * Return whether it fits.
354 fit_plane_on_land(struct plnstr *pp, struct lndstr *lp)
356 struct plchrstr *pcp = plchr + pp->pln_type;
357 struct lchrstr *lcp = lchr + lp->lnd_type;
359 if ((pcp->pl_flags & P_E) && lp->lnd_nxlight < lcp->l_nxlight)
360 return ++lp->lnd_nxlight;