2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, 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 * xdump.c: Experimental extended dump
30 * Known contributors to this file:
31 * Markus Armbruster, 2004-2006
47 * Dump everything under the sun
49 * Static game data (configuration):
50 * - Item characteristics: ichr[]
51 * - Land unit characteristics: lchr[]
52 * - Nuke characteristics: nchr[]
53 * - Plane characteristics: plchr[]
54 * - Product characteristics: pchr[]
55 * - Sector designation characteristics: dchr[]
56 * - Sector infrastructure characteristics: intrchr[]
57 * - Ship characteristics: mchr[]
59 * - News item characteristics: rpt[]
60 * - News page headings: page_headings[]
61 * - Commands: player_coms[] (TODO)
62 * - Configuration: configkeys[]
65 * - Sectors: EF_SECTOR, sect_ca[] (already have dump)
66 * - Land units: EF_LAND, land_ca[] (already have ldump)
67 * - Lost: EF_LOST, lost_ca[] (already have lost)
68 * - Nukes: EF_NUKE, nuke_ca[] (already have ndump)
69 * - Planes: EF_PLANE, plane_ca[] (already have pdump)
70 * - Ships: EF_SHIP, ship_ca[] (already have sdump)
71 * - News: EF_NEWS, news_ca[]
72 * - Treaties: EF_TREATY, treaty_ca[]
74 * - Nations: EF_NATION, nat_ca[]
75 * - Loans: EF_LOAN, loan_ca[]
76 * - Map: EF_MAP (TODO)
77 * - Bmap: EF_BMAP (TODO)
78 * - Market: EF_COMM, commodity_ca[]
81 /* FIXME document dump format */
82 /* FIXME don't dump stuff that's useless due to options */
85 * Evaluate a attribute of an object into VAL, return VAL.
86 * TYPE is the attribute's type.
87 * PTR points to the context object.
88 * The attribute is stored there at offset OFF + IDX * S, where S is
91 static struct valstr *
92 xdeval(struct valstr *val, nsc_type type, void *ptr, ptrdiff_t off, int idx)
95 val->val_cat = NSC_OFF;
96 val->val_as.sym.off = off;
97 val->val_as.sym.idx = idx;
98 nstr_exec_val(val, player->cnum, ptr, NSC_NOTYPE);
99 return val; /* FIXME nstr_exec_val() should return VAL */
102 /* Dump VAL prefixed with SEP, return " ". */
104 xdprval(struct valstr *val, char *sep)
106 unsigned char *s, *e, *l;
108 switch (val->val_type) {
111 pr("%s%ld", sep, val->val_as.lng);
114 pr("%s%#g", sep, val->val_as.dbl);
117 s = (unsigned char *)val->val_as.str.base;
120 l = s + val->val_as.str.maxsz;
121 /* FIXME maxsz == INT_MAX ! */
123 for (e=s; e<l && *e != '"' && *e != '\\' && isgraph(*e); ++e) ;
124 pr("%.*s", (int)(e-s), s);
136 CANT_HAPPEN("Bad VAL type");
143 * Dump field values of a context object.
144 * CA[] describes fields.
145 * PTR points to context object.
148 xdflds(struct castr ca[], void *ptr)
154 for (i = 0; ca[i].ca_name; ++i) {
155 if (ca[i].ca_flags & NSC_DEITY && !player->god)
157 if (ca[i].ca_flags & NSC_EXTRA)
159 n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
162 xdeval(&val, ca[i].ca_type, ptr, ca[i].ca_off, j);
163 sep = xdprval(&val, sep);
169 * Dump header for dump NAME.
170 * If META, it's for the meta-data dump rather than the data dump.
173 xdhdr(char *name, int meta)
175 pr("XDUMP %s%s %ld\n", meta ? "meta " : "", name, (long)time(NULL));
178 /* Dump footer for a dump that dumped N objects. */
186 * Is object P of type TYPE visible to the player?
187 * TODO: Fold this into interators.
190 xdvisible(int type, void *p)
192 struct genitem *gp = p;
193 struct trtstr *tp = p;
194 struct lonstr *lp = p;
200 return gp->own == player->cnum || player->god;
206 return gp->own != 0 && (gp->own == player->cnum || player->god);
208 if (gp->own == player->cnum || player->god)
212 return ((struct natstr *)p)->nat_stat != STAT_UNUSED;
214 return ((struct nwsstr *)p)->nws_vrb != 0
215 && (!opt_HIDDEN || player->god); /* FIXME */
217 return tp->trt_status != TS_FREE
218 && (tp->trt_cna == player->cnum || tp->trt_cnb == player->cnum
221 if (lp->l_status == LS_FREE)
223 if (lp->l_status == LS_SIGNED)
225 return lp->l_loner == player->cnum || lp->l_lonee == player->cnum
231 tlev = ((struct mchrstr *)p)->m_tech;
234 tlev = ((struct plchrstr *)p)->pl_tech;
237 tlev = ((struct lchrstr *)p)->l_tech;
239 natp = getnatp(player->cnum);
240 return player->god || tlev <= (int)(1.25 * natp->nat_level[NAT_TLEV]);
242 tlev = ((struct nchrstr *)p)->n_tech;
244 natp = getnatp(player->cnum);
245 if (tlev > (int)((int)(1.25 * natp->nat_level[NAT_RLEV])
251 return ((struct rptstr *)p)->r_newspage != 0;
253 return ((struct empfile *)p)->cadef != NULL;
260 * Dump items of type TYPE selected by ARG.
261 * Return RET_OK on success, RET_SYN on error.
264 xditem(int type, char *arg)
266 int check_owner = !player->god && (ef_flags(type) & EFF_OWNER) != 0;
270 char buf[2048]; /* FIXME buffer size? */
276 if (!snxtitem(&ni, type, arg))
279 xdhdr(ef_nameof(type), 0);
282 while (nxtitem(&ni, buf)) {
283 if (!xdvisible(type, buf))
296 * Dump meta-data for items of type TYPE.
302 struct castr *ca = ef_cadef(type);
309 xdhdr(ef_nameof(type), 1);
311 for (i = 0; ca[i].ca_name; i++) {
312 if (ca[i].ca_flags & NSC_DEITY && !player->god)
314 if (ca[i].ca_flags & NSC_EXTRA)
316 xdflds(mdchr_ca, &ca[i]);
327 * Dump configkeys[], return RET_OK.
328 * If META, dump meta-data rather than data.
333 static struct castr vers_ca = {
334 NSC_STRINGY, 0, sizeof(PACKAGE_STRING), 0, "version", EF_BAD
342 xdhdr("version", meta);
346 xdflds(mdchr_ca, &vers_ca);
349 for (kp = configkeys; kp->km_key; ++kp) {
350 if (kp->km_type != NSC_NOTYPE && !(kp->km_flags & KM_INTERNAL)) {
351 ca.ca_type = kp->km_type;
355 ca.ca_name = kp->km_key;
356 ca.ca_table = EF_BAD;
357 xdflds(mdchr_ca, &ca);
366 xdeval(&val, vers_ca.ca_type, version, 0, 0);
367 sep = xdprval(&val, "");
368 for (kp = configkeys; kp->km_key; ++kp) {
369 if (kp->km_type != NSC_NOTYPE && !(kp->km_flags & KM_INTERNAL)) {
370 xdeval(&val, kp->km_type, kp->km_data, 0, 0);
371 sep = xdprval(&val, sep);
381 /* Experimental extended dump command */
390 if (!opt_GUINEA_PIGS) {
391 pr("You are not a guinea pig!\n");
395 p = getstarg(player->argp[1], "What? ", buf);
396 if (p && strcmp(p, "meta") == 0) {
398 p = getstarg(player->argp[2], "What? ", buf);
403 type = isdigit(p[0]) ? atoi(p) : ef_byname(p);
404 if (type >= 0 && type < EF_MAX) {
408 return xditem(type, player->argp[2]);
409 } else if (!strncmp(p, "ver", strlen(p))) {