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
43 #include "prototypes.h"
47 static void exit_bad_arg(char *, ...) ATTRIBUTE((noreturn));
48 static void dump_table(int, int);
49 static void pln_fixup(void);
50 static void lnd_fixup(void);
51 static void nuk_fixup(void);
54 main(int argc, char *argv[])
56 char *config_file = NULL;
61 int opt, i, lineno, type;
65 while ((opt = getopt(argc, argv, "e:i:mnxhv")) != EOF) {
77 private = EFF_PRIVATE;
83 printf("Usage: %s [OPTION]...\n"
84 " -e CONFIG-FILE configuration file\n"
86 " -i DUMP-FILE import from DUMP-FILE\n"
87 " -m use machine-readable format\n"
88 " -n dry run, don't update game state\n"
89 " -x export to standard output\n"
90 " -h display this help and exit\n"
91 " -v display version information and exit\n",
92 argv[0], dflt_econfig);
95 printf("%s\n\n%s", version, legal);
103 exit_bad_arg("%s: extra operand %s\n", argv[0], argv[optind]);
105 if (!import && !export)
106 exit_bad_arg("%s: nothing to do!\n", argv[0]);
109 impf = fopen(import, "r");
111 fprintf(stderr, "%s: Can't open %s for reading (%s)\n",
112 argv[0], import, strerror(errno));
116 private = EFF_PRIVATE;
118 /* read configuration & initialize */
120 if (emp_config(config_file) < 0)
124 if (read_builtin_tables() < 0)
126 if (read_custom_tables() < 0)
128 if (chdir(gamedir)) {
129 fprintf(stderr, "%s: Can't chdir to %s (%s)\n",
130 argv[0], gamedir, strerror(errno));
135 for (i = 0; i < EF_MAX; i++) {
136 if (!EF_IS_GAME_STATE(i))
138 if (!ef_open(i, EFF_MEM | private))
142 /* import from IMPORT */
143 memset(dirty, 0, sizeof(dirty));
146 while ((type = xundump(impf, import, &lineno, EF_BAD)) >= 0)
158 /* export to stdout */
160 for (i = 0; i < EF_MAX; i++) {
161 if (!EF_IS_GAME_STATE(i))
163 dump_table(i, human);
167 /* write out imported data */
168 for (i = 0; i < EF_MAX; i++) {
169 if (!EF_IS_GAME_STATE(i))
171 if (!private && dirty[i]) {
181 exit_bad_arg(char *complaint, ...)
186 va_start(ap, complaint);
187 vfprintf(stderr, complaint, ap);
190 fprintf(stderr, "Try -h for help.\n");
195 printf_wrapper(char *fmt, ...)
205 dump_table(int type, int human)
216 xdinit(&xd, 0, human, printf_wrapper);
217 xdhdr(&xd, ef_nameof(type), 0);
219 for (i = 0; (p = ef_ptr(type, i)); i++) {
227 /* TODO remove need for this */
235 static int fit_plane_on_ship(struct plnstr *, struct shpstr *);
236 static int fit_plane_on_land(struct plnstr *, struct lndstr *);
246 for (i = 0; (pp = ef_ptr(EF_PLANE, i)); i++) {
249 csp = ef_ptr(EF_SHIP, pp->pln_ship);
250 clp = ef_ptr(EF_LAND, pp->pln_land);
252 fit_plane_on_ship(pp, csp);
254 fit_plane_on_land(pp, clp);
266 for (i = 0; (lp = ef_ptr(EF_LAND, i)); i++) {
269 csp = ef_ptr(EF_SHIP, lp->lnd_ship);
270 clp = ef_ptr(EF_LAND, lp->lnd_land);
285 for (i = 0; (np = ef_ptr(EF_NUKE, i)); i++) {
288 cpp = ef_ptr(EF_PLANE, np->nuk_plane);
290 cpp->pln_nuketype = np->nuk_type;
294 /* Temporarily copied from src/lib/subs/???sub.c */
297 * Fit a plane of PP's type on ship SP.
298 * Adjust SP's plane counters.
299 * Updating the plane accordingly is the caller's job.
300 * Return whether it fits.
303 fit_plane_on_ship(struct plnstr *pp, struct shpstr *sp)
305 struct plchrstr *pcp = plchr + pp->pln_type;
306 struct mchrstr *mcp = mchr + sp->shp_type;
309 if (pcp->pl_flags & P_K) {
310 /* chopper, try chopper slot first */
311 if (sp->shp_nchoppers < mcp->m_nchoppers)
312 return ++sp->shp_nchoppers;
313 /* else try plane slot */
315 } else if (pcp->pl_flags & P_E) {
316 /* x-light, try x-light slot first */
317 if (sp->shp_nxlight < mcp->m_nxlight)
318 return ++sp->shp_nxlight;
319 /* else try plane slot */
320 wanted = M_MSL | M_FLY;
321 } else if (!(pcp->pl_flags & P_L)) {
322 /* not light, no go */
324 } else if (pcp->pl_flags & P_M) {
325 /* missile, use plane slot */
326 wanted = M_MSL | M_FLY;
328 /* fixed-wing plane, use plane slot */
332 if ((mcp->m_flags & wanted) == 0)
333 return 0; /* ship not capable */
335 if (sp->shp_nplane < mcp->m_nplanes)
336 return ++sp->shp_nplane;
342 * Fit a plane of PP's type on land unit LP.
343 * Adjust LP's plane counters.
344 * Updating the plane accordingly is the caller's job.
345 * Return whether it fits.
348 fit_plane_on_land(struct plnstr *pp, struct lndstr *lp)
350 struct plchrstr *pcp = plchr + pp->pln_type;
351 struct lchrstr *lcp = lchr + lp->lnd_type;
353 if ((pcp->pl_flags & P_E) && lp->lnd_nxlight < lcp->l_nxlight)
354 return ++lp->lnd_nxlight;