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 * xdump.c: Extended dump
30 * Known contributors to this file:
31 * Markus Armbruster, 2004-2008
35 * Dump everything under the sun
37 * Static game data (configuration):
38 * - Item characteristics: ichr[]
39 * - Land unit characteristics: lchr[]
40 * - Nuke characteristics: nchr[]
41 * - Plane characteristics: plchr[]
42 * - Product characteristics: pchr[]
43 * - Sector designation characteristics: dchr[]
44 * - Sector infrastructure characteristics: intrchr[]
45 * - Ship characteristics: mchr[]
46 * - News item characteristics: rpt[]
47 * - News page headings: page_headings[]
48 * - Commands: player_coms[] (TODO)
49 * - Update schedule: update_time[] (not really static)
50 * - Configuration: configkeys[]
53 * - Sectors: EF_SECTOR (superseding dump)
54 * - Land units: EF_LAND (superseding ldump)
55 * - Lost: EF_LOST (superseding lost)
56 * - Nukes: EF_NUKE (superseding ndump)
57 * - Planes: EF_PLANE (superseding pdump)
58 * - Ships: EF_SHIP (superseding sdump)
60 * - Treaties: EF_TREATY
61 * - Power: EF_POWER (TODO)
62 * - Nations: EF_NATION
64 * - Map: EF_MAP (TODO)
65 * - Bmap: EF_BMAP (TODO)
70 * See doc/xdump for motivation, syntax, semantics, and rationale.
71 * Make sure to keep it up-to-date!
74 /* TODO don't dump stuff that's useless due to options */
88 /* xdump descriptor */
90 natid cnum; /* dump for this country */
91 int divine; /* is this a deity dump? */
92 void (*pr)(char *fmt, ...); /* callback for printing dump */
96 * Initialize XD to dump for country CNUM.
97 * Dump is to be delivered through callback PR.
100 static struct xdstr *
101 xdinit(struct xdstr *xd, natid cnum, void (*pr)(char *fmt, ...))
104 xd->divine = getnatp(cnum)->nat_stat == STAT_GOD;
110 * Evaluate a attribute of an object into VAL, return VAL.
111 * CA describes the attribute.
112 * XD is the xdump descriptor.
113 * PTR points to the context object.
114 * IDX is the index within the attribute.
116 static struct valstr *
117 xdeval(struct valstr *val, struct xdstr *xd,
118 struct castr *ca, void *ptr, int idx)
120 nstr_mksymval(val, ca, idx);
121 return nstr_exec_val(val, xd->cnum, ptr, NSC_NOTYPE);
125 * Dump VAL prefixed with SEP, return " ".
126 * VAL must be evaluated.
129 xdprval(struct xdstr *xd, struct valstr *val, char *sep)
131 unsigned char *s, *e, *l;
133 if (CANT_HAPPEN(val->val_cat != NSC_VAL)) {
134 xd->pr("%snil", sep);
138 switch (val->val_type) {
140 xd->pr("%s%ld", sep, val->val_as.lng);
143 xd->pr("%s%#g", sep, val->val_as.dbl);
146 s = (unsigned char *)val->val_as.str.base;
149 l = s + val->val_as.str.maxsz;
150 /* FIXME maxsz == INT_MAX ! */
153 e < l && *e != '"' && *e != '\\' && isgraph(*e);
156 xd->pr("%.*s", (int)(e-s), s);
158 xd->pr("\\%03o", *e++);
165 xd->pr("%snil", sep);
169 xd->pr("%snil", sep);
175 * Dump field values of a context object to XD.
176 * CA[] describes fields.
177 * PTR points to context object.
180 xdflds(struct xdstr *xd, struct castr ca[], void *ptr)
186 for (i = 0; ca[i].ca_name; ++i) {
187 if (ca[i].ca_flags & NSC_DEITY && !xd->divine)
189 if (ca[i].ca_flags & NSC_EXTRA)
191 n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
194 xdeval(&val, xd, &ca[i], ptr, j);
195 sep = xdprval(xd, &val, sep);
201 * Dump header for dump NAME.
202 * If META, it's for the meta-data dump rather than the data dump.
205 xdhdr(struct xdstr *xd, char *name, int meta)
207 xd->pr("XDUMP %s%s %ld\n", meta ? "meta " : "", name, (long)time(NULL));
210 /* Dump footer for a dump that dumped N objects. */
212 xdftr(struct xdstr *xd, int n)
218 * Is object P of type TYPE visible to the player?
219 * TODO: Fold this into interators.
222 xdvisible(int type, void *p)
224 struct empobj *gp = p;
225 struct trtstr *tp = p;
226 struct lonstr *lp = p;
233 return gp->own == player->cnum || player->god;
239 return gp->own != 0 && (gp->own == player->cnum || player->god);
241 return ((struct natstr *)p)->nat_stat != STAT_UNUSED;
243 return gp->own == player->cnum;
245 return ((struct nwsstr *)p)->nws_vrb != 0
246 && (!opt_HIDDEN || player->god); /* FIXME */
248 return tp->trt_status != TS_FREE
249 && (tp->trt_cna == player->cnum || tp->trt_cnb == player->cnum
252 if (lp->l_status == LS_FREE)
254 if (lp->l_status == LS_SIGNED)
256 return lp->l_loner == player->cnum || lp->l_lonee == player->cnum
262 tlev = ((struct mchrstr *)p)->m_tech;
265 tlev = ((struct plchrstr *)p)->pl_tech;
268 tlev = ((struct lchrstr *)p)->l_tech;
270 natp = getnatp(player->cnum);
271 return player->god || tlev <= (int)(1.25 * natp->nat_level[NAT_TLEV]);
273 tlev = ((struct nchrstr *)p)->n_tech;
274 if (drnuke_const > MIN_DRNUKE_CONST) {
275 natp = getnatp(player->cnum);
276 if (tlev > (int)((int)(1.25 * natp->nat_level[NAT_RLEV])
282 return ((struct rptstr *)p)->r_newspage != 0;
284 return ((struct empfile *)p)->cadef != NULL;
291 * Dump items of type TYPE selected by ARG to XD.
292 * Return RET_OK on success, RET_SYN on error.
295 xditem(struct xdstr *xd, int type, char *arg)
300 char buf[2048]; /* FIXME buffer size? */
306 if (!snxtitem(&ni, type, arg))
309 xdhdr(xd, ef_nameof(type), 0);
312 while (nxtitem(&ni, buf)) {
313 if (!xdvisible(type, buf))
326 * Dump meta-data for items of type TYPE to XD.
330 xdmeta(struct xdstr *xd, int type)
332 struct castr *ca = ef_cadef(type);
339 xdhdr(xd, ef_nameof(type), 1);
341 for (i = 0; ca[i].ca_name; i++) {
342 if (ca[i].ca_flags & NSC_DEITY && !xd->divine)
344 if (ca[i].ca_flags & NSC_EXTRA)
346 xdflds(xd, mdchr_ca, &ca[i]);
356 /* Extended dump command */
367 p = getstarg(player->argp[1], "Table name, or meta? ", buf);
368 if (p && strcmp(p, "meta") == 0) {
370 p = getstarg(player->argp[2], "Table name? ", buf);
375 xdinit(&xd, player->cnum, pr);
376 natp = getnatp(player->cnum);
377 type = isdigit(p[0]) ? atoi(p) : ef_byname(p);
378 if (type < 0 || type >= EF_MAX)
381 return xdmeta(&xd, type);
382 if ((EF_IS_GAME_STATE(type) || EF_IS_VIEW(type))
383 && !(natp->nat_stat == STAT_ACTIVE || player->god)) {
384 pr("Access to table %s denied\n", ef_nameof(type));
387 if (type == EF_VERSION && !player->argp[2])
388 return xditem(&xd, type, "*"); /* backward compatibility */
389 return xditem(&xd, type, player->argp[2]);