2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2005, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * projects/authors will amend these files as needed.
28 * xdump.c: Experimental extended dump
30 * Known contributors to this file:
31 * Markus Armbruster, 2004
44 * Dump everything under the sun
46 * Static game data (configuration):
47 * - Item characteristics: ichr[]
48 * - Land unit characteristics: lchr[]
49 * - Nuke characteristics: nchr[]
50 * - Plane characteristics: plchr[]
51 * - Product characteristics: pchr[]
52 * - Sector designation characteristics: dchr[]
53 * - Sector infrastructure characteristics: intrchr[]
54 * - Ship characteristics: mchr[]
56 * - News item characteristics: rpt[] (TODO)
57 * - News page headings: page_headings[] (TODO)
58 * - Treaty clause characteristics: tchr[]
59 * - Commands: coms[] (TODO)
60 * - Options: Options[]
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 */
83 /* Selector descriptors for characteristics tables */
84 /* FIXME belongs into src/lib/global/ */
86 static struct castr dchr_ca[] = {
87 {NSC_STRING, 0, 0, offsetof(struct dchrstr, d_name), "name"},
88 {NSC_INT, 0, 0, offsetof(struct dchrstr, d_mnem), "mnem"},
89 {NSC_INT, 0, 0, offsetof(struct dchrstr, d_prd), "prd"},
90 {NSC_INT, 0, 0, offsetof(struct dchrstr, d_mcst), "mcst"},
91 {NSC_INT, 0, 0, offsetof(struct dchrstr, d_flg), "flg"},
92 {NSC_SITYPE(i_packing), 0, 0, offsetof(struct dchrstr, d_pkg), "pkg"},
93 {NSC_FLOAT, 0, 0, offsetof(struct dchrstr, d_ostr), "ostr"},
94 {NSC_FLOAT, 0, 0, offsetof(struct dchrstr, d_dstr), "dstr"},
95 {NSC_INT, 0, 0, offsetof(struct dchrstr, d_value), "value"},
96 {NSC_INT, 0, 0, offsetof(struct dchrstr, d_cost), "cost"},
97 {NSC_INT, 0, 0, offsetof(struct dchrstr, d_build), "build"},
98 {NSC_INT, 0, 0, offsetof(struct dchrstr, d_lcms), "lcms"},
99 {NSC_INT, 0, 0, offsetof(struct dchrstr, d_hcms), "hcms"},
100 {NSC_INT, 0, 0, offsetof(struct dchrstr, d_maxpop), "maxpop"},
101 {NSC_NOTYPE, 0, 0, 0, NULL}
104 static struct castr ichr_ca[] = {
105 {NSC_STRING, 0, 0, offsetof(struct ichrstr, i_name), "name"},
106 {NSC_INT, 0, 0, offsetof(struct ichrstr, i_mnem), "mnem"},
107 {NSC_SITYPE(i_type), 0, 0, offsetof(struct ichrstr, i_vtype), "vtype"},
108 {NSC_INT, 0, 0, offsetof(struct ichrstr, i_value), "value"},
109 {NSC_INT, 0, 0, offsetof(struct ichrstr, i_sell), "sell"},
110 {NSC_INT, 0, 0, offsetof(struct ichrstr, i_lbs), "lbs"},
111 {NSC_INT, 0, NUMPKG, offsetof(struct ichrstr, i_pkg), "pkg"},
112 {NSC_NOTYPE, 0, 0, 0, NULL}
115 static struct castr intrchr_ca[] = {
116 {NSC_STRING, 0, 0, offsetof(struct sctintrins, in_name), "name"},
117 {NSC_UCHAR, 0, 0, offsetof(struct sctintrins, in_lcms), "lcms"},
118 {NSC_UCHAR, 0, 0, offsetof(struct sctintrins, in_hcms), "hcms"},
119 {NSC_UCHAR, 0, 0, offsetof(struct sctintrins, in_dcost), "dcost"},
120 {NSC_UCHAR, 0, 0, offsetof(struct sctintrins, in_mcost), "mcost"},
121 {NSC_NOTYPE, 0, 0, 0, NULL}
124 static struct castr rpt_ca[] = {
125 {NSC_STRING, 0, NUM_RPTS, offsetof(struct rptstr, r_newstory), "newstory"},
126 {NSC_INT, 0, 0, offsetof(struct rptstr, r_good_will), "good_will"},
127 {NSC_INT, 0, 0, offsetof(struct rptstr, r_newspage), "newspage"},
128 {NSC_NOTYPE, 0, 0, 0, NULL}
131 static struct castr tchr_ca[] = {
132 {NSC_STRING, 0, 0, offsetof(struct tchrstr, t_name), "name"},
133 {NSC_INT, 0, 0, offsetof(struct tchrstr, t_cond), "cond"},
134 {NSC_NOTYPE, 0, 0, 0, NULL}
137 static struct castr mchr_ca[] = {
138 {NSC_STRING, 0, 0, offsetof(struct mchrstr, m_name), "name"},
139 {NSC_USHORT, 0, I_MAX+1, offsetof(struct mchrstr, m_item), "item"},
140 {NSC_INT, 0, 0, offsetof(struct mchrstr, m_lcm), "lcm"},
141 {NSC_INT, 0, 0, offsetof(struct mchrstr, m_hcm), "hcm"},
142 {NSC_INT, 0, 0, offsetof(struct mchrstr, m_armor), "armor"},
143 {NSC_INT, 0, 0, offsetof(struct mchrstr, m_speed), "speed"},
144 {NSC_INT, 0, 0, offsetof(struct mchrstr, m_visib), "visib"},
145 {NSC_INT, 0, 0, offsetof(struct mchrstr, m_vrnge), "vrnge"},
146 {NSC_INT, 0, 0, offsetof(struct mchrstr, m_frnge), "frnge"},
147 {NSC_INT, 0, 0, offsetof(struct mchrstr, m_glim), "glim"},
148 {NSC_UCHAR, 0, 0, offsetof(struct mchrstr, m_nxlight), "nxlight"},
149 {NSC_UCHAR, 0, 0, offsetof(struct mchrstr, m_nchoppers), "nchoppers"},
150 {NSC_UCHAR, 0, 0, offsetof(struct mchrstr, m_fuelc), "fuelc"},
151 {NSC_UCHAR, 0, 0, offsetof(struct mchrstr, m_fuelu), "fuelu"},
152 {NSC_INT, 0, 0, offsetof(struct mchrstr, m_tech), "tech"},
153 {NSC_INT, 0, 0, offsetof(struct mchrstr, m_cost), "cost"},
154 {NSC_LONG, 0, 0, offsetof(struct mchrstr, m_flags), "flags"},
155 {NSC_UCHAR, 0, 0, offsetof(struct mchrstr, m_nplanes), "nplanes"},
156 {NSC_UCHAR, 0, 0, offsetof(struct mchrstr, m_nland), "nland"},
157 {NSC_NOTYPE, 0, 0, 0, NULL}
160 static struct castr pchr_ca[] = {
161 {NSC_STRING, 0, 0, offsetof(struct pchrstr, p_name), "name"},
162 {NSC_STRING, 0, 0, offsetof(struct pchrstr, p_sname), "sname"},
163 {NSC_SITYPE(i_type), 0, MAXPRCON, offsetof(struct pchrstr, p_ctype), "ctype"},
164 {NSC_USHORT, 0, MAXPRCON, offsetof(struct pchrstr, p_camt), "camt"},
165 {NSC_SITYPE(i_type), 0, 0, offsetof(struct pchrstr, p_type), "type"},
166 {NSC_INT, 0, 0, offsetof(struct pchrstr, p_level), "level"},
167 {NSC_INT, 0, 0, offsetof(struct pchrstr, p_cost), "cost"},
168 {NSC_INT, 0, 0, offsetof(struct pchrstr, p_nrndx), "nrndx"},
169 {NSC_INT, 0, 0, offsetof(struct pchrstr, p_nrdep), "nrdep"},
170 {NSC_INT, 0, 0, offsetof(struct pchrstr, p_nlndx), "nlndx"},
171 {NSC_INT, 0, 0, offsetof(struct pchrstr, p_nlmin), "nlmin"},
172 {NSC_INT, 0, 0, offsetof(struct pchrstr, p_nllag), "nllag"},
173 {NSC_INT, 0, 0, offsetof(struct pchrstr, p_effic), "effic"},
174 {NSC_NOTYPE, 0, 0, 0, NULL}
177 static struct castr plchr_ca[] = {
178 {NSC_STRING, 0, 0, offsetof(struct plchrstr, pl_name), "name"},
179 {NSC_INT, 0, 0, offsetof(struct plchrstr, pl_lcm), "lcm"},
180 {NSC_INT, 0, 0, offsetof(struct plchrstr, pl_hcm), "hcm"},
181 {NSC_INT, 0, 0, offsetof(struct plchrstr, pl_cost), "cost"},
182 {NSC_INT, 0, 0, offsetof(struct plchrstr, pl_tech), "tech"},
183 {NSC_INT, 0, 0, offsetof(struct plchrstr, pl_acc), "acc"},
184 {NSC_INT, 0, 0, offsetof(struct plchrstr, pl_load), "load"},
185 {NSC_INT, 0, 0, offsetof(struct plchrstr, pl_att), "att"},
186 {NSC_INT, 0, 0, offsetof(struct plchrstr, pl_def), "def"},
187 {NSC_INT, 0, 0, offsetof(struct plchrstr, pl_range), "range"},
188 {NSC_INT, 0, 0, offsetof(struct plchrstr, pl_crew), "crew"},
189 {NSC_INT, 0, 0, offsetof(struct plchrstr, pl_fuel), "fuel"},
190 {NSC_INT, 0, 0, offsetof(struct plchrstr, pl_stealth), "stealth"},
191 {NSC_INT, 0, 0, offsetof(struct plchrstr, pl_flags), "flags"},
192 {NSC_NOTYPE, 0, 0, 0, NULL}
195 static struct castr lchr_ca[] = {
196 {NSC_STRING, 0, 0, offsetof(struct lchrstr, l_name), "name"},
197 {NSC_USHORT, 0, I_MAX+1, offsetof(struct mchrstr, m_item), "item"},
198 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_lcm), "lcm"},
199 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_hcm), "hcm"},
200 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_gun), "gun"},
201 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_shell), "shell"},
202 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_tech), "tech"},
203 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_cost), "cost"},
204 {NSC_FLOAT, 0, 0, offsetof(struct lchrstr, l_att), "att"},
205 {NSC_FLOAT, 0, 0, offsetof(struct lchrstr, l_def), "def"},
206 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_vul), "vul"},
207 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_spd), "spd"},
208 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_vis), "vis"},
209 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_spy), "spy"},
210 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_rad), "rad"},
211 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_frg), "frg"},
212 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_acc), "acc"},
213 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_dam), "dam"},
214 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_ammo), "ammo"},
215 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_aaf), "aaf"},
216 {NSC_UCHAR, 0, 0, offsetof(struct lchrstr, l_fuelc), "fuelc"},
217 {NSC_UCHAR, 0, 0, offsetof(struct lchrstr, l_fuelu), "fuelu"},
218 {NSC_UCHAR, 0, 0, offsetof(struct lchrstr, l_nxlight), "nxlight"},
219 {NSC_UCHAR, 0, 0, offsetof(struct lchrstr, l_mxland), "mxland"},
220 {NSC_LONG, 0, 0, offsetof(struct lchrstr, l_flags), "flags"},
221 {NSC_NOTYPE, 0, 0, 0, NULL}
224 static struct castr nchr_ca[] = {
225 {NSC_STRING, 0, 0, offsetof(struct nchrstr, n_name), "name"},
226 {NSC_INT, 0, 0, offsetof(struct nchrstr, n_lcm), "lcm"},
227 {NSC_INT, 0, 0, offsetof(struct nchrstr, n_hcm), "hcm"},
228 {NSC_INT, 0, 0, offsetof(struct nchrstr, n_oil), "oil"},
229 {NSC_INT, 0, 0, offsetof(struct nchrstr, n_rad), "rad"},
230 {NSC_INT, 0, 0, offsetof(struct nchrstr, n_blast), "blast"},
231 {NSC_INT, 0, 0, offsetof(struct nchrstr, n_dam), "dam"},
232 {NSC_INT, 0, 0, offsetof(struct nchrstr, n_cost), "cost"},
233 {NSC_INT, 0, 0, offsetof(struct nchrstr, n_tech), "tech"},
234 {NSC_INT, 0, 0, offsetof(struct nchrstr, n_weight), "weight"},
235 {NSC_INT, 0, 0, offsetof(struct nchrstr, n_flags), "flags"},
236 {NSC_NOTYPE, 0, 0, 0, NULL}
239 /* Characteristics table meta data */
241 char *name; /* name for lookup */
242 struct castr *ca; /* selector descriptors */
243 void *chr; /* characteristics table */
244 size_t size; /* size of characteristics table element */
247 /* Table of characteristics tables */
248 static struct camap chr_camap[] = {
249 {"sect chr", dchr_ca, dchr, sizeof(dchr[0])},
250 {"ship chr", mchr_ca, mchr, sizeof(mchr[0])},
251 {"plane chr", plchr_ca, plchr, sizeof(plchr[0])},
252 {"land chr", lchr_ca, lchr, sizeof(lchr[0])},
253 {"nuke chr", nchr_ca, nchr, sizeof(nchr[0])},
255 /* FIXME rpt[] lacks sentinel, xdchr() doesn't terminate */
256 {"news chr", rpt_ca, rpt, sizeof(rpt[0])},
258 {"treaty chr", tchr_ca, tchr, sizeof(tchr[0])},
259 {"item", ichr_ca, ichr, sizeof(ichr[0])},
260 {"infrastructure", intrchr_ca, intrchr, sizeof(intrchr[0])},
261 {"product", pchr_ca, pchr, sizeof(pchr[0])},
262 {NULL, NULL, NULL, 0}
266 * Search chr_camap[] for element named NAME, return its index.
267 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
271 chridx_by_name(char *name)
273 return stmtch(name, chr_camap, offsetof(struct camap, name),
274 sizeof(chr_camap[0]));
278 * Evaluate a attribute of an object into VAL, return VAL.
279 * TYPE is the attribute's type.
280 * PTR points to the context object.
281 * The attribute is stored there at offset OFF + IDX * S, where S is
284 static struct valstr *
285 xdeval(struct valstr *val, nsc_type type, void *ptr, ptrdiff_t off, int idx)
287 val->val_type = type;
288 val->val_cat = NSC_OFF;
289 val->val_as.sym.off = off;
290 val->val_as.sym.idx = idx;
291 nstr_exec_val(val, player->cnum, ptr, NSC_NOTYPE);
292 return val; /* FIXME nstr_exec_val() should return VAL */
295 /* Dump VAL prefixed with SEP, return " ". */
297 xdprval(struct valstr *val, char *sep)
299 unsigned char *s, *e, *l;
301 switch (val->val_type) {
304 pr("%s%ld", sep, val->val_as.lng);
307 pr("%s%#g", sep, val->val_as.dbl);
310 s = (unsigned char *)val->val_as.str.base;
313 l = s + val->val_as.str.maxsz;
315 for (e=s; e<l && *e != '"' && *e != '\\' && isgraph(*e); ++e) ;
316 pr("%.*s", (int)(e-s), s);
328 CANT_HAPPEN("Bad VAL type");
335 * Dump field values of a context object.
336 * CA[] describes fields.
337 * PTR points to context object.
340 xdflds(struct castr ca[], void *ptr)
346 for (i = 0; ca[i].ca_name; ++i) {
347 if (ca[i].ca_flags & NSC_DEITY && !player->god)
349 if (ca[i].ca_flags & NSC_EXTRA)
351 n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
354 xdeval(&val, ca[i].ca_type, ptr, ca[i].ca_off, j);
355 sep = xdprval(&val, sep);
360 /* Dump field names; CA[] describes fields. */
362 xdfldnam(struct castr ca[])
367 for (i = 0; ca[i].ca_name; ++i) {
368 if (ca[i].ca_flags & NSC_DEITY && !player->god)
370 if (ca[i].ca_flags & NSC_EXTRA)
372 pr("%s%s", sep, ca[i].ca_name);
373 if (ca[i].ca_len && ca[i].ca_type != NSC_STRINGY)
374 pr(" %d", ca[i].ca_len);
379 /* Dump first line of header for dump NAME. */
383 pr("XDUMP %s %ld\n", name, (long)time(NULL));
386 /* Dump header for dump NAME with fields described by CA[]. */
388 xdhdr(char *name, struct castr ca[])
395 /* Dump footer for a dump that dumped N objects. */
399 pr("dumped %d\n", n);
403 * Dump items of type TYPE selected by ARG.
404 * Return RET_OK on success, RET_SYN on error.
407 xditem(int type, char *arg)
412 char buf[2048]; /* FIXME buffer size? */
418 if (!snxtitem(&ni, type, arg))
421 xdhdr(ef_nameof(type), ca);
424 while (nxtitem(&ni, buf)) {
438 * Dump characteristics described by chr_camap[IDX].
439 * Return RET_OK on success, RET_SYN if IDX < 0.
451 cm = &chr_camap[chridx];
453 xdhdr(cm->name, cm->ca);
456 for (p = cm->chr; ; p += cm->size) {
457 val.val_type = cm->ca[0].ca_type;
458 val.val_cat = NSC_OFF;
459 val.val_as.sym.off = cm->ca[0].ca_off;
460 val.val_as.sym.idx = 0;
461 nstr_exec_val(&val, player->cnum, p, NSC_STRING);
462 if (!val.val_as.str.base || !*val.val_as.str.base)
474 /* Dump Options[], return RET_OK. */
484 for (i = 0; Options[i].opt_key; ++i) {
485 pr("%s%s", sep, Options[i].opt_key);
491 for (i = 0; Options[i].opt_key; ++i) {
492 pr("%s%d", sep, *Options[i].opt_valuep);
510 for (kp = configkeys; kp->km_key; ++kp) {
511 if (kp->km_type != NSC_NOTYPE && !(kp->km_flags & KM_INTERNAL)) {
512 pr("%s%s", sep, kp->km_key);
519 for (kp = configkeys; kp->km_key; ++kp) {
520 if (kp->km_type != NSC_NOTYPE && !(kp->km_flags & KM_INTERNAL)) {
521 xdeval(&val, kp->km_type, kp->km_data, 0, 0);
522 sep = xdprval(&val, sep);
530 /* Experimental extended dump command */
538 if (!opt_GUINEA_PIGS) {
539 pr("You are not a guinea pig!\n");
543 p = getstarg(player->argp[1], "What? ", buf);
549 return xditem(type, player->argp[2]);
550 } else if (!strncmp(p, "chr", strlen(p)) && player->argp[2]) {
551 return xdchr(chridx_by_name(player->argp[2]));
552 } else if (!strncmp(p, "opt", strlen(p))) {
554 } else if (!strncmp(p, "ver", strlen(p))) {