2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2004, 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
43 * Dump everything under the sun
45 * Static game data (configuration):
46 * - Item characteristics: ichr[]
47 * - Land unit characteristics: lchr[]
48 * - Nuke characteristics: nchr[]
49 * - Plane characteristics: plchr[]
50 * - Product characteristics: pchr[]
51 * - Sector designation characteristics: dchr[]
52 * - Sector infrastructure characteristics: intrchr[]
53 * - Ship characteristics: mchr[]
55 * - News item characteristics: rpt[] (TODO)
56 * - News page headings: page_headings[] (TODO)
57 * - Treaty clause characteristics: tchr[]
58 * - Commands: coms[] (TODO)
59 * - Options: Options[]
60 * - Configuration: configkeys[]
63 * - Sectors: EF_SECTOR, sect_ca[] (already have dump)
64 * - Land units: EF_LAND, land_ca[] (already have ldump)
65 * - Lost: EF_LOST, lost_ca[] (already have lost)
66 * - Nukes: EF_NUKE, nuke_ca[] (already have ndump)
67 * - Planes: EF_PLANE, plane_ca[] (already have pdump)
68 * - Ships: EF_SHIP, ship_ca[] (already have sdump)
69 * - News: EF_NEWS, news_ca[]
70 * - Treaties: EF_TREATY, treaty_ca[]
72 * - Nations: EF_NATION, nat_ca[]
73 * - Loans: EF_LOAN, loan_ca[]
74 * - Map: EF_MAP (TODO)
75 * - Bmap: EF_BMAP (TODO)
76 * - Market: EF_COMM, commodity_ca[]
79 /* FIXME document dump format */
80 /* FIXME don't dump stuff that's useless due to options */
82 /* Selector descriptors for characteristics tables */
83 /* FIXME belongs into src/lib/global/ */
85 static struct castr dchr_ca[] = {
86 {NSC_STRING, 0, 0, offsetof(struct dchrstr, d_name), "name"},
87 {NSC_INT, 0, 0, offsetof(struct dchrstr, d_mnem), "mnem"},
88 {NSC_INT, 0, 0, offsetof(struct dchrstr, d_prd), "prd"},
89 {NSC_INT, 0, 0, offsetof(struct dchrstr, d_mcst), "mcst"},
90 {NSC_INT, 0, 0, offsetof(struct dchrstr, d_flg), "flg"},
91 {NSC_SITYPE(i_packing), 0, 0, offsetof(struct dchrstr, d_pkg), "pkg"},
92 {NSC_FLOAT, 0, 0, offsetof(struct dchrstr, d_ostr), "ostr"},
93 {NSC_FLOAT, 0, 0, offsetof(struct dchrstr, d_dstr), "dstr"},
94 {NSC_INT, 0, 0, offsetof(struct dchrstr, d_value), "value"},
95 {NSC_INT, 0, 0, offsetof(struct dchrstr, d_cost), "cost"},
96 {NSC_INT, 0, 0, offsetof(struct dchrstr, d_build), "build"},
97 {NSC_INT, 0, 0, offsetof(struct dchrstr, d_lcms), "lcms"},
98 {NSC_INT, 0, 0, offsetof(struct dchrstr, d_hcms), "hcms"},
99 {NSC_NOTYPE, 0, 0, 0, NULL}
102 static struct castr ichr_ca[] = {
103 {NSC_STRING, 0, 0, offsetof(struct ichrstr, i_name), "name"},
104 {NSC_INT, 0, 0, offsetof(struct ichrstr, i_mnem), "mnem"},
105 {NSC_SITYPE(i_type), 0, 0, offsetof(struct ichrstr, i_vtype), "vtype"},
106 {NSC_INT, 0, 0, offsetof(struct ichrstr, i_value), "value"},
107 {NSC_INT, 0, 0, offsetof(struct ichrstr, i_sell), "sell"},
108 {NSC_INT, 0, 0, offsetof(struct ichrstr, i_lbs), "lbs"},
109 {NSC_INT, 0, NUMPKG, offsetof(struct ichrstr, i_pkg), "pkg"},
110 {NSC_NOTYPE, 0, 0, 0, NULL}
113 static struct castr intrchr_ca[] = {
114 {NSC_STRING, 0, 0, offsetof(struct sctintrins, in_name), "name"},
115 {NSC_UCHAR, 0, 0, offsetof(struct sctintrins, in_lcms), "lcms"},
116 {NSC_UCHAR, 0, 0, offsetof(struct sctintrins, in_hcms), "hcms"},
117 {NSC_UCHAR, 0, 0, offsetof(struct sctintrins, in_dcost), "dcost"},
118 {NSC_UCHAR, 0, 0, offsetof(struct sctintrins, in_mcost), "mcost"},
119 {NSC_NOTYPE, 0, 0, 0, NULL}
122 static struct castr rpt_ca[] = {
123 {NSC_STRING, 0, NUM_RPTS, offsetof(struct rptstr, r_newstory), "newstory"},
124 {NSC_INT, 0, 0, offsetof(struct rptstr, r_good_will), "good_will"},
125 {NSC_INT, 0, 0, offsetof(struct rptstr, r_newspage), "newspage"},
126 {NSC_NOTYPE, 0, 0, 0, NULL}
129 static struct castr tchr_ca[] = {
130 {NSC_STRING, 0, 0, offsetof(struct tchrstr, t_name), "name"},
131 {NSC_INT, 0, 0, offsetof(struct tchrstr, t_cond), "cond"},
132 {NSC_NOTYPE, 0, 0, 0, NULL}
135 static struct castr mchr_ca[] = {
136 {NSC_STRING, 0, 0, offsetof(struct mchrstr, m_name), "name"},
137 {NSC_USHORT, 0, I_MAX+1, offsetof(struct mchrstr, m_item), "item"},
138 {NSC_INT, 0, 0, offsetof(struct mchrstr, m_lcm), "lcm"},
139 {NSC_INT, 0, 0, offsetof(struct mchrstr, m_hcm), "hcm"},
140 {NSC_INT, 0, 0, offsetof(struct mchrstr, m_armor), "armor"},
141 {NSC_INT, 0, 0, offsetof(struct mchrstr, m_speed), "speed"},
142 {NSC_INT, 0, 0, offsetof(struct mchrstr, m_visib), "visib"},
143 {NSC_INT, 0, 0, offsetof(struct mchrstr, m_vrnge), "vrnge"},
144 {NSC_INT, 0, 0, offsetof(struct mchrstr, m_frnge), "frnge"},
145 {NSC_INT, 0, 0, offsetof(struct mchrstr, m_glim), "glim"},
146 {NSC_UCHAR, 0, 0, offsetof(struct mchrstr, m_nxlight), "nxlight"},
147 {NSC_UCHAR, 0, 0, offsetof(struct mchrstr, m_nchoppers), "nchoppers"},
148 {NSC_UCHAR, 0, 0, offsetof(struct mchrstr, m_fuelc), "fuelc"},
149 {NSC_UCHAR, 0, 0, offsetof(struct mchrstr, m_fuelu), "fuelu"},
150 {NSC_INT, 0, 0, offsetof(struct mchrstr, m_tech), "tech"},
151 {NSC_INT, 0, 0, offsetof(struct mchrstr, m_cost), "cost"},
152 {NSC_LONG, 0, 0, offsetof(struct mchrstr, m_flags), "flags"},
153 {NSC_UCHAR, 0, 0, offsetof(struct mchrstr, m_nplanes), "nplanes"},
154 {NSC_UCHAR, 0, 0, offsetof(struct mchrstr, m_nland), "nland"},
155 {NSC_NOTYPE, 0, 0, 0, NULL}
158 static struct castr pchr_ca[] = {
159 {NSC_STRING, 0, 0, offsetof(struct pchrstr, p_name), "name"},
160 {NSC_STRING, 0, 0, offsetof(struct pchrstr, p_sname), "sname"},
161 {NSC_SITYPE(i_type), 0, MAXPRCON, offsetof(struct pchrstr, p_ctype), "ctype"},
162 {NSC_USHORT, 0, MAXPRCON, offsetof(struct pchrstr, p_camt), "camt"},
163 {NSC_SITYPE(i_type), 0, 0, offsetof(struct pchrstr, p_type), "type"},
164 {NSC_INT, 0, 0, offsetof(struct pchrstr, p_level), "level"},
165 {NSC_INT, 0, 0, offsetof(struct pchrstr, p_cost), "cost"},
166 {NSC_INT, 0, 0, offsetof(struct pchrstr, p_nrndx), "nrndx"},
167 {NSC_INT, 0, 0, offsetof(struct pchrstr, p_nrdep), "nrdep"},
168 {NSC_INT, 0, 0, offsetof(struct pchrstr, p_nlndx), "nlndx"},
169 {NSC_INT, 0, 0, offsetof(struct pchrstr, p_nlmin), "nlmin"},
170 {NSC_INT, 0, 0, offsetof(struct pchrstr, p_nllag), "nllag"},
171 {NSC_INT, 0, 0, offsetof(struct pchrstr, p_effic), "effic"},
172 {NSC_NOTYPE, 0, 0, 0, NULL}
175 static struct castr plchr_ca[] = {
176 {NSC_STRING, 0, 0, offsetof(struct plchrstr, pl_name), "name"},
177 {NSC_INT, 0, 0, offsetof(struct plchrstr, pl_lcm), "lcm"},
178 {NSC_INT, 0, 0, offsetof(struct plchrstr, pl_hcm), "hcm"},
179 {NSC_INT, 0, 0, offsetof(struct plchrstr, pl_cost), "cost"},
180 {NSC_INT, 0, 0, offsetof(struct plchrstr, pl_tech), "tech"},
181 {NSC_INT, 0, 0, offsetof(struct plchrstr, pl_acc), "acc"},
182 {NSC_INT, 0, 0, offsetof(struct plchrstr, pl_load), "load"},
183 {NSC_INT, 0, 0, offsetof(struct plchrstr, pl_att), "att"},
184 {NSC_INT, 0, 0, offsetof(struct plchrstr, pl_def), "def"},
185 {NSC_INT, 0, 0, offsetof(struct plchrstr, pl_range), "range"},
186 {NSC_INT, 0, 0, offsetof(struct plchrstr, pl_crew), "crew"},
187 {NSC_INT, 0, 0, offsetof(struct plchrstr, pl_fuel), "fuel"},
188 {NSC_INT, 0, 0, offsetof(struct plchrstr, pl_stealth), "stealth"},
189 {NSC_INT, 0, 0, offsetof(struct plchrstr, pl_flags), "flags"},
190 {NSC_NOTYPE, 0, 0, 0, NULL}
193 static struct castr lchr_ca[] = {
194 {NSC_STRING, 0, 0, offsetof(struct lchrstr, l_name), "name"},
195 {NSC_USHORT, 0, I_MAX+1, offsetof(struct mchrstr, m_item), "item"},
196 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_lcm), "lcm"},
197 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_hcm), "hcm"},
198 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_gun), "gun"},
199 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_shell), "shell"},
200 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_tech), "tech"},
201 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_cost), "cost"},
202 {NSC_FLOAT, 0, 0, offsetof(struct lchrstr, l_att), "att"},
203 {NSC_FLOAT, 0, 0, offsetof(struct lchrstr, l_def), "def"},
204 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_vul), "vul"},
205 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_spd), "spd"},
206 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_vis), "vis"},
207 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_spy), "spy"},
208 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_rad), "rad"},
209 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_frg), "frg"},
210 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_acc), "acc"},
211 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_dam), "dam"},
212 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_ammo), "ammo"},
213 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_aaf), "aaf"},
214 {NSC_UCHAR, 0, 0, offsetof(struct lchrstr, l_fuelc), "fuelc"},
215 {NSC_UCHAR, 0, 0, offsetof(struct lchrstr, l_fuelu), "fuelu"},
216 {NSC_UCHAR, 0, 0, offsetof(struct lchrstr, l_nxlight), "nxlight"},
217 {NSC_UCHAR, 0, 0, offsetof(struct lchrstr, l_mxland), "mxland"},
218 {NSC_LONG, 0, 0, offsetof(struct lchrstr, l_flags), "flags"},
219 {NSC_NOTYPE, 0, 0, 0, NULL}
222 static struct castr nchr_ca[] = {
223 {NSC_STRING, 0, 0, offsetof(struct nchrstr, n_name), "name"},
224 {NSC_INT, 0, 0, offsetof(struct nchrstr, n_lcm), "lcm"},
225 {NSC_INT, 0, 0, offsetof(struct nchrstr, n_hcm), "hcm"},
226 {NSC_INT, 0, 0, offsetof(struct nchrstr, n_oil), "oil"},
227 {NSC_INT, 0, 0, offsetof(struct nchrstr, n_rad), "rad"},
228 {NSC_INT, 0, 0, offsetof(struct nchrstr, n_blast), "blast"},
229 {NSC_INT, 0, 0, offsetof(struct nchrstr, n_dam), "dam"},
230 {NSC_INT, 0, 0, offsetof(struct nchrstr, n_cost), "cost"},
231 {NSC_INT, 0, 0, offsetof(struct nchrstr, n_tech), "tech"},
232 {NSC_INT, 0, 0, offsetof(struct nchrstr, n_weight), "weight"},
233 {NSC_INT, 0, 0, offsetof(struct nchrstr, n_flags), "flags"},
234 {NSC_NOTYPE, 0, 0, 0, NULL}
237 /* Characteristics table meta data */
239 char *name; /* name for lookup */
240 struct castr *ca; /* selector descriptors */
241 void *chr; /* characteristics table */
242 size_t size; /* size of characteristics table element */
245 /* Table of characteristics tables */
246 static struct camap chr_camap[] = {
247 {"sect chr", dchr_ca, dchr, sizeof(dchr[0])},
248 {"ship chr", mchr_ca, mchr, sizeof(mchr[0])},
249 {"plane chr", plchr_ca, plchr, sizeof(plchr[0])},
250 {"land chr", lchr_ca, lchr, sizeof(lchr[0])},
251 {"nuke chr", nchr_ca, nchr, sizeof(nchr[0])},
253 /* FIXME rpt[] lacks sentinel, xdchr() doesn't terminate */
254 {"news chr", rpt_ca, rpt, sizeof(rpt[0])},
256 {"treaty chr", tchr_ca, tchr, sizeof(tchr[0])},
257 {"item", ichr_ca, ichr, sizeof(ichr[0])},
258 {"infrastructure", intrchr_ca, intrchr, sizeof(intrchr[0])},
259 {"product", pchr_ca, pchr, sizeof(pchr[0])},
260 {NULL, NULL, NULL, 0}
264 * Search chr_camap[] for element named NAME, return its index.
265 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
269 chridx_by_name(char *name)
271 return stmtch(name, chr_camap, offsetof(struct camap, name),
272 sizeof(chr_camap[0]));
276 * Evaluate a attribute of an object into VAL, return VAL.
277 * TYPE is the attribute's type.
278 * PTR points to the context object.
279 * The attribute is stored there at offset OFF + IDX * S, where S is
282 static struct valstr *
283 xdeval(struct valstr *val, nsc_type type, void *ptr, ptrdiff_t off, int idx)
285 val->val_type = type;
286 val->val_cat = NSC_OFF;
287 val->val_as_type = -1;
288 val->val_as.sym.off = off;
289 val->val_as.sym.idx = idx;
290 nstr_exec_val(val, player->cnum, ptr, NSC_NOTYPE);
291 return val; /* FIXME nstr_exec_val() should return VAL */
294 /* Dump VAL prefixed with SEP, return " ". */
296 xdprval(struct valstr *val, char *sep)
298 unsigned char *s, *e, *l;
300 switch (val->val_type) {
303 pr("%s%ld", sep, val->val_as.lng);
306 pr("%s%#g", sep, val->val_as.dbl);
309 s = (unsigned char *)val->val_as.str.base;
312 l = s + val->val_as.str.maxsz;
314 for (e=s; e<l && *e != '"' && *e != '\\' && isgraph(*e); ++e) ;
315 pr("%.*s", (int)(e-s), s);
327 CANT_HAPPEN("Bad VAL type");
334 * Dump field values of a context object.
335 * CA[] describes fields.
336 * PTR points to context object.
339 xdflds(struct castr ca[], void *ptr)
345 for (i = 0; ca[i].ca_name; ++i) {
346 if (ca[i].ca_flags & NSC_DEITY && !player->god)
348 if (ca[i].ca_flags & NSC_EXTRA)
350 n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
353 xdeval(&val, ca[i].ca_type, ptr, ca[i].ca_off, j);
354 sep = xdprval(&val, sep);
359 /* Dump field names; CA[] describes fields. */
361 xdfldnam(struct castr ca[])
366 for (i = 0; ca[i].ca_name; ++i) {
367 if (ca[i].ca_flags & NSC_DEITY && !player->god)
369 if (ca[i].ca_flags & NSC_EXTRA)
371 pr("%s%s", sep, ca[i].ca_name);
372 if (ca[i].ca_len && ca[i].ca_type != NSC_STRINGY)
373 pr(" %d", ca[i].ca_len);
378 /* Dump first line of header for dump NAME. */
382 pr("XDUMP %s %ld\n", name, (long)time(NULL));
385 /* Dump header for dump NAME with fields described by CA[]. */
387 xdhdr(char *name, struct castr ca[])
394 /* Dump footer for a dump that dumped N objects. */
398 pr("dumped %d\n", n);
402 * Dump items of type TYPE selected by ARG.
403 * Return RET_OK on success, RET_SYN on error.
406 xditem(int type, char *arg)
411 char buf[2048]; /* FIXME buffer size? */
417 if (!snxtitem(&ni, type, arg))
420 xdhdr(ef_nameof(type), ca);
423 while (nxtitem(&ni, buf)) {
437 * Dump characteristics described by chr_camap[IDX].
438 * Return RET_OK on success, RET_SYN if IDX < 0.
450 cm = &chr_camap[chridx];
452 xdhdr(cm->name, cm->ca);
455 for (p = cm->chr; ; p += cm->size) {
456 val.val_type = cm->ca[0].ca_type;
457 val.val_cat = NSC_OFF;
458 val.val_as_type = -1;
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))) {