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 dump_table(int, int);
48 static void shp_fixup(void);
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:mtxhv")) != EOF) {
71 printf("Usage: %s [OPTION]... [DUMP-FILE]\n"
72 " -e CONFIG-FILE configuration file\n"
74 " -m use machine-readable format\n"
75 " -t test import, don't update game state\n"
76 " -x export to standard output\n"
77 " -h display this help and exit\n"
78 " -v display version information and exit\n",
79 argv[0], dflt_econfig);
85 private = EFF_PRIVATE;
91 printf("%s\n\n%s", version, legal);
94 fprintf(stderr, "Try -h for help.\n");
100 import = argv[optind++];
103 impf = fopen(import, "r");
105 fprintf(stderr, "Cant open %s for reading (%s)\n",
106 import, strerror(errno));
110 private = EFF_PRIVATE;
112 /* read configuration & initialize */
114 if (emp_config(config_file) < 0)
118 if (read_builtin_tables() < 0)
120 if (read_custom_tables() < 0)
122 if (chdir(gamedir)) {
123 fprintf(stderr, "Can't chdir to %s (%s)\n",
124 gamedir, strerror(errno));
129 for (i = 0; i < EF_MAX; i++) {
130 if (!EF_IS_GAME_STATE(i))
132 if (!ef_open(i, EFF_MEM | private))
136 /* import from IMPORT */
137 memset(dirty, 0, sizeof(dirty));
140 while ((type = xundump(impf, import, &lineno, EF_BAD)) >= 0)
153 /* export to stdout */
155 for (i = 0; i < EF_MAX; i++) {
156 if (!EF_IS_GAME_STATE(i))
158 dump_table(i, human);
162 /* write out imported data */
163 for (i = 0; i < EF_MAX; i++) {
164 if (!EF_IS_GAME_STATE(i))
166 if (!private && dirty[i]) {
176 printf_wrapper(char *fmt, ...)
186 dump_table(int type, int human)
197 xdinit(&xd, 0, human, printf_wrapper);
198 xdhdr(&xd, ef_nameof(type), 0);
200 for (i = 0; (p = ef_ptr(type, i)); i++) {
208 /* FIXME remove need for this */
216 static int fit_plane_on_ship(struct plnstr *, struct shpstr *);
217 static int fit_plane_on_land(struct plnstr *, struct lndstr *);
225 for (i = 0; (sp = ef_ptr(EF_SHIP, i)); i++) {
228 shp_set_tech(sp, sp->shp_tech);
240 for (i = 0; (pp = ef_ptr(EF_PLANE, i)); i++) {
243 pln_set_tech(pp, pp->pln_tech);
244 csp = ef_ptr(EF_SHIP, pp->pln_ship);
245 clp = ef_ptr(EF_LAND, pp->pln_land);
247 fit_plane_on_ship(pp, csp);
249 fit_plane_on_land(pp, clp);
261 for (i = 0; (lp = ef_ptr(EF_LAND, i)); i++) {
264 lnd_set_tech(lp, lp->lnd_tech);
265 csp = ef_ptr(EF_SHIP, lp->lnd_ship);
266 clp = ef_ptr(EF_LAND, lp->lnd_land);
281 for (i = 0; (np = ef_ptr(EF_NUKE, i)); i++) {
284 cpp = ef_ptr(EF_PLANE, np->nuk_plane);
286 cpp->pln_nuketype = np->nuk_type;
290 /* FIXME don't copy this from src/lib/subs/???sub.c */
293 * Set SP's tech to TLEV along with everything else that depends on it.
296 shp_set_tech(struct shpstr *sp, int tlev)
298 struct mchrstr *mcp = mchr + sp->shp_type;
299 int tech_diff = tlev - mcp->m_tech;
301 if (CANT_HAPPEN(tech_diff < 0)) {
307 sp->shp_armor = (short)SHP_DEF(mcp->m_armor, tech_diff);
308 sp->shp_speed = (short)SHP_SPD(mcp->m_speed, tech_diff);
309 sp->shp_visib = (short)SHP_VIS(mcp->m_visib, tech_diff);
310 sp->shp_frnge = (short)SHP_RNG(mcp->m_frnge, tech_diff);
311 sp->shp_glim = (short)SHP_FIR(mcp->m_glim, tech_diff);
315 * Fit a plane of PP's type on ship SP.
316 * Adjust SP's plane counters.
317 * Updating the plane accordingly is the caller's job.
318 * Return whether it fits.
321 fit_plane_on_ship(struct plnstr *pp, struct shpstr *sp)
323 struct plchrstr *pcp = plchr + pp->pln_type;
324 struct mchrstr *mcp = mchr + sp->shp_type;
327 if (pcp->pl_flags & P_K) {
328 /* chopper, try chopper slot first */
329 if (sp->shp_nchoppers < mcp->m_nchoppers)
330 return ++sp->shp_nchoppers;
331 /* else try plane slot */
333 } else if (pcp->pl_flags & P_E) {
334 /* x-light, try x-light slot first */
335 if (sp->shp_nxlight < mcp->m_nxlight)
336 return ++sp->shp_nxlight;
337 /* else try plane slot */
338 wanted = M_MSL | M_FLY;
339 } else if (!(pcp->pl_flags & P_L)) {
340 /* not light, no go */
342 } else if (pcp->pl_flags & P_M) {
343 /* missile, use plane slot */
344 wanted = M_MSL | M_FLY;
346 /* fixed-wing plane, use plane slot */
350 if ((mcp->m_flags & wanted) == 0)
351 return 0; /* ship not capable */
353 if (sp->shp_nplane < mcp->m_nplanes)
354 return ++sp->shp_nplane;
360 * Fit a plane of PP's type on land unit LP.
361 * Adjust LP's plane counters.
362 * Updating the plane accordingly is the caller's job.
363 * Return whether it fits.
366 fit_plane_on_land(struct plnstr *pp, struct lndstr *lp)
368 struct plchrstr *pcp = plchr + pp->pln_type;
369 struct lchrstr *lcp = lchr + lp->lnd_type;
371 if ((pcp->pl_flags & P_E) && lp->lnd_nxlight < lcp->l_nxlight)
372 return ++lp->lnd_nxlight;
378 * Set PP's tech to TLEV along with everything else that depends on it.
381 pln_set_tech(struct plnstr *pp, int tlev)
383 struct plchrstr *pcp = plchr + pp->pln_type;
384 int tech_diff = tlev - pcp->pl_tech;
385 int limited_range = pp->pln_range < pp->pln_range_max;
387 if (CANT_HAPPEN(tech_diff < 0)) {
393 pp->pln_att = PLN_ATTDEF(pcp->pl_att, tech_diff);
394 pp->pln_def = PLN_ATTDEF(pcp->pl_def, tech_diff);
395 pp->pln_acc = PLN_ACC(pcp->pl_acc, tech_diff);
396 pp->pln_range_max = PLN_RAN(pcp->pl_range, tech_diff);
397 pp->pln_load = PLN_LOAD(pcp->pl_load, tech_diff);
399 if (!limited_range || pp->pln_range > pp->pln_range_max)
400 pp->pln_range = pp->pln_range_max;
404 * Set LP's tech to TLEV along with everything else that depends on it.
407 lnd_set_tech(struct lndstr *lp, int tlev)
409 struct lchrstr *lcp = lchr + lp->lnd_type;
410 int tech_diff = tlev - lcp->l_tech;
412 if (CANT_HAPPEN(tech_diff < 0)) {
418 lp->lnd_att = (float)LND_ATTDEF(lcp->l_att, tech_diff);
419 lp->lnd_def = (float)LND_ATTDEF(lcp->l_def, tech_diff);
420 lp->lnd_vul = (int)LND_VUL(lcp->l_vul, tech_diff);
421 lp->lnd_spd = (int)LND_SPD(lcp->l_spd, tech_diff);
422 lp->lnd_vis = (int)LND_VIS(lcp->l_vis, tech_diff);
423 lp->lnd_spy = (int)LND_SPY(lcp->l_spy, tech_diff);
424 lp->lnd_rad = (int)LND_RAD(lcp->l_rad, tech_diff);
425 lp->lnd_frg = (int)LND_FRG(lcp->l_frg, tech_diff);
426 lp->lnd_acc = (int)LND_ACC(lcp->l_acc, tech_diff);
427 lp->lnd_dam = (int)LND_DAM(lcp->l_dam, tech_diff);
428 lp->lnd_ammo = (int)LND_AMM(lcp->l_ammo, tech_diff);
429 lp->lnd_aaf = (int)LND_AAF(lcp->l_aaf, tech_diff);
430 lp->lnd_fuelc = (int)LND_FC(lcp->l_fuelc, tech_diff);
431 lp->lnd_fuelu = (int)LND_FU(lcp->l_fuelu, tech_diff);
432 lp->lnd_maxlight = (int)LND_XPL(lcp->l_nxlight, tech_diff);
433 lp->lnd_maxland = (int)LND_MXL(lcp->l_nland, tech_diff);
436 /* FIXME don't copy this from src/lib/update/nat.c */
439 logx(double d, double base)
443 return log10(d) / log10(base);