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
46 * Dump everything under the sun
48 * Static game data (configuration):
49 * - Item characteristics: ichr[]
50 * - Land unit characteristics: lchr[]
51 * - Nuke characteristics: nchr[]
52 * - Plane characteristics: plchr[]
53 * - Product characteristics: pchr[]
54 * - Sector designation characteristics: dchr[]
55 * - Sector infrastructure characteristics: intrchr[]
56 * - Ship characteristics: mchr[]
58 * - News item characteristics: rpt[]
59 * - News page headings: page_headings[] (TODO)
60 * - Commands: player_coms[] (TODO)
61 * - Configuration: configkeys[]
64 * - Sectors: EF_SECTOR, sect_ca[] (already have dump)
65 * - Land units: EF_LAND, land_ca[] (already have ldump)
66 * - Lost: EF_LOST, lost_ca[] (already have lost)
67 * - Nukes: EF_NUKE, nuke_ca[] (already have ndump)
68 * - Planes: EF_PLANE, plane_ca[] (already have pdump)
69 * - Ships: EF_SHIP, ship_ca[] (already have sdump)
70 * - News: EF_NEWS, news_ca[]
71 * - Treaties: EF_TREATY, treaty_ca[]
73 * - Nations: EF_NATION, nat_ca[]
74 * - Loans: EF_LOAN, loan_ca[]
75 * - Map: EF_MAP (TODO)
76 * - Bmap: EF_BMAP (TODO)
77 * - Market: EF_COMM, commodity_ca[]
80 /* FIXME document dump format */
81 /* FIXME don't dump stuff that's useless due to options */
84 * Evaluate a attribute of an object into VAL, return VAL.
85 * TYPE is the attribute's type.
86 * PTR points to the context object.
87 * The attribute is stored there at offset OFF + IDX * S, where S is
90 static struct valstr *
91 xdeval(struct valstr *val, nsc_type type, void *ptr, ptrdiff_t off, int idx)
94 val->val_cat = NSC_OFF;
95 val->val_as.sym.off = off;
96 val->val_as.sym.idx = idx;
97 nstr_exec_val(val, player->cnum, ptr, NSC_NOTYPE);
98 return val; /* FIXME nstr_exec_val() should return VAL */
101 /* Dump VAL prefixed with SEP, return " ". */
103 xdprval(struct valstr *val, char *sep)
105 unsigned char *s, *e, *l;
107 switch (val->val_type) {
110 pr("%s%ld", sep, val->val_as.lng);
113 pr("%s%#g", sep, val->val_as.dbl);
116 s = (unsigned char *)val->val_as.str.base;
119 l = s + val->val_as.str.maxsz;
120 /* FIXME maxsz == INT_MAX ! */
122 for (e=s; e<l && *e != '"' && *e != '\\' && isgraph(*e); ++e) ;
123 pr("%.*s", (int)(e-s), s);
135 CANT_HAPPEN("Bad VAL type");
142 * Dump field values of a context object.
143 * CA[] describes fields.
144 * PTR points to context object.
147 xdflds(struct castr ca[], void *ptr)
153 for (i = 0; ca[i].ca_name; ++i) {
154 if (ca[i].ca_flags & NSC_DEITY && !player->god)
156 if (ca[i].ca_flags & NSC_EXTRA)
158 n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
161 xdeval(&val, ca[i].ca_type, ptr, ca[i].ca_off, j);
162 sep = xdprval(&val, sep);
168 * Dump header for dump NAME.
169 * If META, it's for the meta-data dump rather than the data dump.
172 xdhdr(char *name, int meta)
174 pr("XDUMP %s%s %ld\n", meta ? "meta " : "", name, (long)time(NULL));
177 /* Dump footer for a dump that dumped N objects. */
185 * Is object P of type TYPE visible to the player?
186 * TODO: Fold this into interators.
189 xdvisible(int type, void *p)
191 struct genitem *gp = p;
192 struct trtstr *tp = p;
193 struct lonstr *lp = p;
199 return gp->own == player->cnum || player->god;
205 return gp->own != 0 && (gp->own == player->cnum || player->god);
207 if (gp->own == player->cnum || player->god)
211 return ((struct natstr *)p)->nat_stat != STAT_UNUSED;
213 return ((struct nwsstr *)p)->nws_vrb != 0
214 && (!opt_HIDDEN || player->god); /* FIXME */
216 return tp->trt_status != TS_FREE
217 && (tp->trt_cna == player->cnum || tp->trt_cnb == player->cnum
220 if (lp->l_status == LS_FREE)
222 if (lp->l_status == LS_SIGNED)
224 return lp->l_loner == player->cnum || lp->l_lonee == player->cnum
230 tlev = ((struct mchrstr *)p)->m_tech;
233 tlev = ((struct plchrstr *)p)->pl_tech;
236 tlev = ((struct lchrstr *)p)->l_tech;
238 natp = getnatp(player->cnum);
239 return player->god || tlev <= (int)(1.25 * natp->nat_level[NAT_TLEV]);
241 tlev = ((struct nchrstr *)p)->n_tech;
243 natp = getnatp(player->cnum);
244 if (tlev > (int)((int)(1.25 * natp->nat_level[NAT_RLEV])
250 return ((struct rptstr *)p)->r_newspage != 0;
252 return ((struct empfile *)p)->cadef != NULL;
259 * Dump items of type TYPE selected by ARG.
260 * Return RET_OK on success, RET_SYN on error.
263 xditem(int type, char *arg)
265 int check_owner = !player->god && (ef_flags(type) & EFF_OWNER) != 0;
269 char buf[2048]; /* FIXME buffer size? */
275 if (!snxtitem(&ni, type, arg))
278 xdhdr(ef_nameof(type), 0);
281 while (nxtitem(&ni, buf)) {
282 if (!xdvisible(type, buf))
295 * Dump meta-data for items of type TYPE.
301 struct castr *ca = ef_cadef(type);
308 xdhdr(ef_nameof(type), 1);
310 for (i = 0; ca[i].ca_name; i++) {
311 if (ca[i].ca_flags & NSC_DEITY && !player->god)
313 if (ca[i].ca_flags & NSC_EXTRA)
315 xdflds(mdchr_ca, &ca[i]);
326 * Dump configkeys[], return RET_OK.
327 * If META, dump meta-data rather than data.
338 xdhdr("version", meta);
342 for (kp = configkeys; kp->km_key; ++kp) {
343 if (kp->km_type != NSC_NOTYPE && !(kp->km_flags & KM_INTERNAL)) {
344 ca.ca_type = kp->km_type;
348 ca.ca_name = kp->km_key;
349 ca.ca_table = EF_BAD;
350 xdflds(mdchr_ca, &ca);
360 for (kp = configkeys; kp->km_key; ++kp) {
361 if (kp->km_type != NSC_NOTYPE && !(kp->km_flags & KM_INTERNAL)) {
362 xdeval(&val, kp->km_type, kp->km_data, 0, 0);
363 sep = xdprval(&val, sep);
373 /* Experimental extended dump command */
382 if (!opt_GUINEA_PIGS) {
383 pr("You are not a guinea pig!\n");
387 p = getstarg(player->argp[1], "What? ", buf);
388 if (p && strcmp(p, "meta") == 0) {
390 p = getstarg(player->argp[2], "What? ", buf);
395 type = isdigit(p[0]) ? atoi(p) : ef_byname(p);
396 if (type >= 0 && type < EF_MAX) {
400 return xditem(type, player->argp[2]);
401 } else if (!strncmp(p, "ver", strlen(p))) {