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 *, ...)
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);
56 main(int argc, char *argv[])
58 char *config_file = NULL;
63 int opt, i, lineno, type;
67 while ((opt = getopt(argc, argv, "e:i:mnxhv")) != EOF) {
79 private = EFF_PRIVATE;
85 printf("Usage: %s [OPTION]...\n"
86 " -e CONFIG-FILE configuration file\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);
97 printf("%s\n\n%s", version, legal);
105 exit_bad_arg("%s: extra operand %s\n", argv[0], argv[optind]);
107 if (!import && !export)
108 exit_bad_arg("%s: nothing to do!\n", argv[0]);
111 impf = fopen(import, "r");
113 fprintf(stderr, "%s: Can't open %s for reading (%s)\n",
114 argv[0], import, strerror(errno));
118 private = EFF_PRIVATE;
120 /* read configuration & initialize */
122 if (emp_config(config_file) < 0)
126 if (read_builtin_tables() < 0)
128 if (read_custom_tables() < 0)
130 if (chdir(gamedir)) {
131 fprintf(stderr, "%s: Can't chdir to %s (%s)\n",
132 argv[0], gamedir, strerror(errno));
137 for (i = 0; i < EF_MAX; i++) {
138 if (!EF_IS_GAME_STATE(i))
140 if (!ef_open(i, EFF_MEM | private, -1))
144 /* import from IMPORT */
145 memset(dirty, 0, sizeof(dirty));
148 while ((type = xundump(impf, import, &lineno, EF_BAD)) >= 0)
160 /* export to stdout */
162 for (i = 0; i < EF_MAX; i++) {
163 if (!EF_IS_GAME_STATE(i))
165 dump_table(i, human);
167 if (fclose(stdout) != 0) {
168 fprintf(stderr, "%s: error writing export (%s)\n",
169 argv[0], strerror(errno));
174 /* write out imported data */
175 for (i = 0; i < EF_MAX; i++) {
176 if (!EF_IS_GAME_STATE(i))
178 if (!private && dirty[i]) {
188 exit_bad_arg(char *complaint, ...)
193 va_start(ap, complaint);
194 vfprintf(stderr, complaint, ap);
197 fprintf(stderr, "Try -h for help.\n");
202 printf_wrapper(char *fmt, ...)
212 dump_table(int type, int human)
223 xdinit(&xd, NATID_BAD, human, printf_wrapper);
224 xdhdr(&xd, ef_nameof(type), 0);
226 for (i = 0; (p = ef_ptr(type, i)); i++) {
234 /* TODO remove need for this */
242 static int fit_plane_on_ship(struct plnstr *, struct shpstr *);
243 static int fit_plane_on_land(struct plnstr *, struct lndstr *);
253 for (i = 0; (pp = ef_ptr(EF_PLANE, i)); i++) {
256 csp = ef_ptr(EF_SHIP, pp->pln_ship);
257 clp = ef_ptr(EF_LAND, pp->pln_land);
259 fit_plane_on_ship(pp, csp);
261 fit_plane_on_land(pp, clp);
273 for (i = 0; (lp = ef_ptr(EF_LAND, i)); i++) {
276 csp = ef_ptr(EF_SHIP, lp->lnd_ship);
277 clp = ef_ptr(EF_LAND, lp->lnd_land);
292 for (i = 0; (np = ef_ptr(EF_NUKE, i)); i++) {
295 cpp = ef_ptr(EF_PLANE, np->nuk_plane);
297 cpp->pln_nuketype = np->nuk_type;
301 /* Temporarily copied from src/lib/subs/???sub.c */
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.
310 fit_plane_on_ship(struct plnstr *pp, struct shpstr *sp)
312 struct plchrstr *pcp = plchr + pp->pln_type;
313 struct mchrstr *mcp = mchr + sp->shp_type;
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 */
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 */
331 } else if (pcp->pl_flags & P_M) {
332 /* missile, use plane slot */
333 wanted = M_MSL | M_FLY;
335 /* fixed-wing plane, use plane slot */
339 if ((mcp->m_flags & wanted) == 0)
340 return 0; /* ship not capable */
342 if (sp->shp_nplane < mcp->m_nplanes)
343 return ++sp->shp_nplane;
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.
355 fit_plane_on_land(struct plnstr *pp, struct lndstr *lp)
357 struct plchrstr *pcp = plchr + pp->pln_type;
358 struct lchrstr *lcp = lchr + lp->lnd_type;
360 if ((pcp->pl_flags & P_E) && lp->lnd_nxlight < lcp->l_nxlight)
361 return ++lp->lnd_nxlight;