2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2000, 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[]
56 * - Treaty clause characteristics: tchr[]
58 * - Options: Options[]
59 * - Configuration: configkeys[]
62 * - Sectors: EF_SECTOR, sect_ca[] (already have dump)
63 * - Land units: EF_LAND, land_ca[] (already have ldump)
64 * - Lost: EF_LOST, lost_ca[] (already have lost)
65 * - Nukes: EF_NUKE, nuke_ca[] (already have ndump)
66 * - Planes: EF_PLANE, plane_ca[] (already have pdump)
67 * - Ships: EF_SHIP, ship_ca[] (already have sdump)
68 * - News: EF_NEWS, news_ca[]
69 * - Treaties: EF_TREATY, treaty_ca[]
71 * - Nations: EF_NATION, nat_ca[]
72 * - Loans: EF_LOAN, loan_ca[]
75 * - Market: EF_COMM, commodity_ca[]
78 /* FIXME document dump format */
80 /* Selector descriptors for characteristics tables */
81 /* FIXME belongs into src/lib/global/ */
83 static struct castr dchr_ca[] = {
84 {NSC_STRING, 0, 0, offsetof(struct dchrstr, d_name), "name"},
85 {NSC_INT, 0, 0, offsetof(struct dchrstr, d_mnem), "mnem"},
86 {NSC_INT, 0, 0, offsetof(struct dchrstr, d_prd), "prd"},
87 {NSC_INT, 0, 0, offsetof(struct dchrstr, d_mcst), "mcst"},
88 {NSC_INT, 0, 0, offsetof(struct dchrstr, d_flg), "flg"},
89 {NSC_INT, 0, 0, offsetof(struct dchrstr, d_pkg), "pkg"},
90 {NSC_FLOAT, 0, 0, offsetof(struct dchrstr, d_ostr), "ostr"},
91 {NSC_FLOAT, 0, 0, offsetof(struct dchrstr, d_dstr), "dstr"},
92 {NSC_INT, 0, 0, offsetof(struct dchrstr, d_value), "value"},
93 {NSC_INT, 0, 0, offsetof(struct dchrstr, d_cost), "cost"},
94 {NSC_INT, 0, 0, offsetof(struct dchrstr, d_build), "build"},
95 {NSC_INT, 0, 0, offsetof(struct dchrstr, d_lcms), "lcms"},
96 {NSC_INT, 0, 0, offsetof(struct dchrstr, d_hcms), "hcms"},
97 {NSC_NOTYPE, 0, 0, 0, NULL}
100 static struct castr ichr_ca[] = {
101 {NSC_STRING, 0, 0, offsetof(struct ichrstr, i_name), "name"},
102 {NSC_INT, 0, 0, offsetof(struct ichrstr, i_mnem), "mnem"},
103 {NSC_INT, 0, 0, offsetof(struct ichrstr, i_vtype), "vtype"},
104 {NSC_INT, 0, 0, offsetof(struct ichrstr, i_value), "value"},
105 {NSC_INT, 0, 0, offsetof(struct ichrstr, i_sell), "sell"},
106 {NSC_INT, 0, 0, offsetof(struct ichrstr, i_lbs), "lbs"},
107 {NSC_INT, 0, NUMPKG, offsetof(struct ichrstr, i_pkg), "pkg"},
108 {NSC_NOTYPE, 0, 0, 0, NULL}
111 static struct castr intrchr_ca[] = {
112 {NSC_STRING, 0, 0, offsetof(struct sctintrins, in_name), "name"},
113 {NSC_UCHAR, 0, 0, offsetof(struct sctintrins, in_lcms), "lcms"},
114 {NSC_UCHAR, 0, 0, offsetof(struct sctintrins, in_hcms), "hcms"},
115 {NSC_UCHAR, 0, 0, offsetof(struct sctintrins, in_dcost), "dcost"},
116 {NSC_UCHAR, 0, 0, offsetof(struct sctintrins, in_mcost), "mcost"},
117 {NSC_NOTYPE, 0, 0, 0, NULL}
120 static struct castr rpt_ca[] = {
121 {NSC_STRING, 0, NUM_RPTS, offsetof(struct rptstr, r_newstory), "newstory"},
122 {NSC_INT, 0, 0, offsetof(struct rptstr, r_good_will), "good_will"},
123 {NSC_INT, 0, 0, offsetof(struct rptstr, r_newspage), "newspage"},
124 {NSC_NOTYPE, 0, 0, 0, NULL}
127 static struct castr tchr_ca[] = {
128 {NSC_STRING, 0, 0, offsetof(struct tchrstr, t_name), "name"},
129 {NSC_INT, 0, 0, offsetof(struct tchrstr, t_cond), "cond"},
130 {NSC_NOTYPE, 0, 0, 0, NULL}
133 static struct castr mchr_ca[] = {
134 {NSC_STRING, 0, 0, offsetof(struct mchrstr, m_name), "name"},
135 {NSC_USHORT, 0, I_MAX+1, offsetof(struct mchrstr, m_item), "item"},
136 {NSC_INT, 0, 0, offsetof(struct mchrstr, m_lcm), "lcm"},
137 {NSC_INT, 0, 0, offsetof(struct mchrstr, m_hcm), "hcm"},
138 {NSC_INT, 0, 0, offsetof(struct mchrstr, m_armor), "armor"},
139 {NSC_INT, 0, 0, offsetof(struct mchrstr, m_speed), "speed"},
140 {NSC_INT, 0, 0, offsetof(struct mchrstr, m_visib), "visib"},
141 {NSC_INT, 0, 0, offsetof(struct mchrstr, m_vrnge), "vrnge"},
142 {NSC_INT, 0, 0, offsetof(struct mchrstr, m_glim), "glim"},
143 {NSC_UCHAR, 0, 0, offsetof(struct mchrstr, m_nxlight), "nxlight"},
144 {NSC_UCHAR, 0, 0, offsetof(struct mchrstr, m_nchoppers), "nchoppers"},
145 {NSC_UCHAR, 0, 0, offsetof(struct mchrstr, m_fuelc), "fuelc"},
146 {NSC_UCHAR, 0, 0, offsetof(struct mchrstr, m_fuelu), "fuelu"},
147 {NSC_INT, 0, 0, offsetof(struct mchrstr, m_tech), "tech"},
148 {NSC_INT, 0, 0, offsetof(struct mchrstr, m_cost), "cost"},
149 {NSC_INT, 0, 0, offsetof(struct mchrstr, m_flags), "flags"},
150 {NSC_UCHAR, 0, 0, offsetof(struct mchrstr, m_nplanes), "nplanes"},
151 {NSC_UCHAR, 0, 0, offsetof(struct mchrstr, m_nland), "nland"},
152 {NSC_NOTYPE, 0, 0, 0, NULL}
155 static struct castr pchr_ca[] = {
156 {NSC_STRING, 0, 0, offsetof(struct pchrstr, p_name), "name"},
157 {NSC_STRING, 0, 0, offsetof(struct pchrstr, p_sname), "sname"},
158 {NSC_UCHAR, 0, MAXPRCON, offsetof(struct pchrstr, p_ctype), "ctype"},
159 {NSC_USHORT, 0, MAXPRCON, offsetof(struct pchrstr, p_camt), "camt"},
160 {NSC_INT, 0, 0, offsetof(struct pchrstr, p_type), "type"},
161 {NSC_INT, 0, 0, offsetof(struct pchrstr, p_level), "level"},
162 {NSC_INT, 0, 0, offsetof(struct pchrstr, p_cost), "cost"},
163 {NSC_INT, 0, 0, offsetof(struct pchrstr, p_nrndx), "nrndx"},
164 {NSC_INT, 0, 0, offsetof(struct pchrstr, p_nrdep), "nrdep"},
165 {NSC_INT, 0, 0, offsetof(struct pchrstr, p_nlndx), "nlndx"},
166 {NSC_INT, 0, 0, offsetof(struct pchrstr, p_nlmin), "nlmin"},
167 {NSC_INT, 0, 0, offsetof(struct pchrstr, p_nllag), "nllag"},
168 {NSC_INT, 0, 0, offsetof(struct pchrstr, p_effic), "effic"},
169 {NSC_NOTYPE, 0, 0, 0, NULL}
172 static struct castr plchr_ca[] = {
173 {NSC_STRING, 0, 0, offsetof(struct plchrstr, pl_name), "name"},
174 {NSC_INT, 0, 0, offsetof(struct plchrstr, pl_lcm), "lcm"},
175 {NSC_INT, 0, 0, offsetof(struct plchrstr, pl_hcm), "hcm"},
176 {NSC_INT, 0, 0, offsetof(struct plchrstr, pl_cost), "cost"},
177 {NSC_INT, 0, 0, offsetof(struct plchrstr, pl_tech), "tech"},
178 {NSC_INT, 0, 0, offsetof(struct plchrstr, pl_acc), "acc"},
179 {NSC_INT, 0, 0, offsetof(struct plchrstr, pl_load), "load"},
180 {NSC_INT, 0, 0, offsetof(struct plchrstr, pl_att), "att"},
181 {NSC_INT, 0, 0, offsetof(struct plchrstr, pl_def), "def"},
182 {NSC_INT, 0, 0, offsetof(struct plchrstr, pl_range), "range"},
183 {NSC_INT, 0, 0, offsetof(struct plchrstr, pl_crew), "crew"},
184 {NSC_INT, 0, 0, offsetof(struct plchrstr, pl_fuel), "fuel"},
185 {NSC_INT, 0, 0, offsetof(struct plchrstr, pl_stealth), "stealth"},
186 {NSC_INT, 0, 0, offsetof(struct plchrstr, pl_flags), "flags"},
187 {NSC_NOTYPE, 0, 0, 0, NULL}
190 static struct castr lchr_ca[] = {
191 {NSC_STRING, 0, 0, offsetof(struct lchrstr, l_name), "name"},
192 {NSC_USHORT, 0, I_MAX+1, offsetof(struct mchrstr, m_item), "item"},
193 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_lcm), "lcm"},
194 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_hcm), "hcm"},
195 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_gun), "gun"},
196 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_shell), "shell"},
197 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_tech), "tech"},
198 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_cost), "cost"},
199 {NSC_FLOAT, 0, 0, offsetof(struct lchrstr, l_att), "att"},
200 {NSC_FLOAT, 0, 0, offsetof(struct lchrstr, l_def), "def"},
201 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_vul), "vul"},
202 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_spd), "spd"},
203 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_vis), "vis"},
204 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_spy), "spy"},
205 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_rad), "rad"},
206 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_frg), "frg"},
207 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_acc), "acc"},
208 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_dam), "dam"},
209 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_ammo), "ammo"},
210 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_aaf), "aaf"},
211 {NSC_UCHAR, 0, 0, offsetof(struct lchrstr, l_fuelc), "fuelc"},
212 {NSC_UCHAR, 0, 0, offsetof(struct lchrstr, l_fuelu), "fuelu"},
213 {NSC_UCHAR, 0, 0, offsetof(struct lchrstr, l_nxlight), "nxlight"},
214 {NSC_UCHAR, 0, 0, offsetof(struct lchrstr, l_mxland), "mxland"},
215 {NSC_INT, 0, 0, offsetof(struct lchrstr, l_flags), "flags"},
216 {NSC_NOTYPE, 0, 0, 0, NULL}
219 static struct castr nchr_ca[] = {
220 {NSC_STRING, 0, 0, offsetof(struct nchrstr, n_name), "name"},
221 {NSC_INT, 0, 0, offsetof(struct nchrstr, n_lcm), "lcm"},
222 {NSC_INT, 0, 0, offsetof(struct nchrstr, n_hcm), "hcm"},
223 {NSC_INT, 0, 0, offsetof(struct nchrstr, n_oil), "oil"},
224 {NSC_INT, 0, 0, offsetof(struct nchrstr, n_rad), "rad"},
225 {NSC_INT, 0, 0, offsetof(struct nchrstr, n_blast), "blast"},
226 {NSC_INT, 0, 0, offsetof(struct nchrstr, n_dam), "dam"},
227 {NSC_INT, 0, 0, offsetof(struct nchrstr, n_cost), "cost"},
228 {NSC_INT, 0, 0, offsetof(struct nchrstr, n_tech), "tech"},
229 {NSC_INT, 0, 0, offsetof(struct nchrstr, n_weight), "weight"},
230 {NSC_INT, 0, 0, offsetof(struct nchrstr, n_flags), "flags"},
231 {NSC_NOTYPE, 0, 0, 0, NULL}
234 /* Characteristics table meta data */
236 char *name; /* name for lookup */
237 struct castr *ca; /* selector descriptors */
238 void *chr; /* characteristics table */
239 size_t size; /* size of characteristics table element */
242 /* Table of characteristics tables */
243 static struct camap chr_camap[] = {
244 {"sect chr", dchr_ca, dchr, sizeof(dchr[0])},
245 {"ship chr", mchr_ca, mchr, sizeof(mchr[0])},
246 {"plane chr", plchr_ca, plchr, sizeof(plchr[0])},
247 {"land chr", lchr_ca, lchr, sizeof(lchr[0])},
248 {"nuke chr", nchr_ca, nchr, sizeof(nchr[0])},
250 /* FIXME rpt[] lacks sentinel, xdchr() doesn't terminate */
251 {"news chr", rpt_ca, rpt, sizeof(rpt[0])},
253 {"treaty chr", tchr_ca, tchr, sizeof(tchr[0])},
254 {"item", ichr_ca, ichr, sizeof(ichr[0])},
255 {"infrastructure", intrchr_ca, intrchr, sizeof(intrchr[0])},
256 {"product", pchr_ca, pchr, sizeof(pchr[0])},
257 {NULL, NULL, NULL, 0}
261 * Search chr_camap[] for element named NAME, return its index.
262 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
266 chridx_by_name(char *name)
268 return stmtch(name, chr_camap, offsetof(struct camap, name),
269 sizeof(chr_camap[0]));
273 * Evaluate a attribute of an object into VAL.
274 * TYPE is the attribute's type.
275 * PTR points to the context object.
276 * The attribute is stored there at offset OFF + IDX * S, where S is
279 static struct valstr *
280 xdeval(struct valstr *val, nsc_type type, void *ptr, ptrdiff_t off, int idx)
282 val->val_type = type;
283 val->val_cat = NSC_OFF;
284 val->val_as_type = -1;
285 val->val_as.sym.off = off;
286 val->val_as.sym.idx = idx;
287 nstr_exec_val(val, player->cnum, ptr, NSC_NOTYPE);
288 return val; /* FIXME nstr_exec_val() should return VAL */
291 /* Dump VAL prefixed with SEP, return " ". */
293 xdprval(struct valstr *val, char *sep)
297 switch (val->val_type) {
300 pr("%s%ld", sep, val->val_as.lng);
303 pr("%s%g", sep, val->val_as.dbl);
310 for (e = s; *e != '"' && *e != '\\' && isprint(*e); ++e) ;
311 pr("%.*s", (int)(e-s), s);
321 CANT_HAPPEN("Bad VAL type");
328 * Dump field values of a context object.
329 * CA[] describes fields.
330 * PTR points to context object.
333 xdflds(struct castr ca[], void *ptr)
339 for (i = 0; ca[i].ca_name; ++i) {
340 if (ca[i].ca_flags & NSC_DEITY && !player->god)
342 if (ca[i].ca_flags & NSC_EXTRA)
344 n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
347 xdeval(&val, ca[i].ca_type, ptr, ca[i].ca_off, j);
348 sep = xdprval(&val, sep);
353 /* Dump field names; CA[] describes fields. */
355 xdfldnam(struct castr ca[])
360 for (i = 0; ca[i].ca_name; ++i) {
361 if (ca[i].ca_flags & NSC_DEITY && !player->god)
363 if (ca[i].ca_flags & NSC_EXTRA)
365 pr("%s%s", sep, ca[i].ca_name);
366 if (ca[i].ca_len && ca[i].ca_type != NSC_STRINGY)
367 pr(" %d", ca[i].ca_len);
372 /* Dump first line of header for dump NAME. */
376 pr("XDUMP %s %ld\n", name, (long)time(NULL));
379 /* Dump header for dump NAME with fields described by CA[]. */
381 xdhdr(char *name, struct castr ca[])
388 /* Dump footer for a dump that dumped N objects. */
392 pr("dumped %d\n", n);
396 * Dump items of type TYPE selected by ARG.
397 * Return RET_OK on success, RET_SYN on error.
400 xditem(int type, char *arg)
405 s_char buf[2048]; /* FIXME buffer size? */
411 if (!snxtitem(&ni, type, arg))
414 xdhdr(ef_nameof(type), ca);
417 while (nxtitem(&ni, buf)) {
431 * Dump characteristics described by chr_camap[IDX].
432 * Return RET_OK on success, RET_SYN if IDX < 0.
444 cm = &chr_camap[chridx];
446 xdhdr(cm->name, cm->ca);
449 for (p = cm->chr; ; p += cm->size) {
450 val.val_type = cm->ca[0].ca_type;
451 val.val_cat = NSC_OFF;
452 val.val_as_type = -1;
453 val.val_as.sym.off = cm->ca[0].ca_off;
454 val.val_as.sym.idx = 0;
455 nstr_exec_val(&val, player->cnum, p, NSC_STRING);
456 if (!val.val_as.str || !*val.val_as.str)
468 /* Dump Options[], return RET_OK. */
478 for (i = 0; Options[i].opt_key; ++i) {
479 pr("%s%s", sep, Options[i].opt_key);
485 for (i = 0; Options[i].opt_key; ++i) {
486 pr("%s%d", sep, *Options[i].opt_valuep);
504 for (kp = configkeys; kp->km_key; ++kp) {
505 if (kp->km_type != NSC_NOTYPE && !(kp->km_flags & KM_INTERNAL)) {
506 pr("%s%s", sep, kp->km_key);
513 for (kp = configkeys; kp->km_key; ++kp) {
514 if (kp->km_type != NSC_NOTYPE && !(kp->km_flags & KM_INTERNAL)) {
515 xdeval(&val, kp->km_type, kp->km_data, 0, 0);
516 sep = xdprval(&val, sep);
524 /* Experimental extended dump command */
532 p = getstarg(player->argp[1], "What? ", buf);
538 return xditem(type, player->argp[2]);
539 } else if (!strncmp(p, "chr", strlen(p)) && player->argp[2]) {
540 return xdchr(chridx_by_name(player->argp[2]));
541 } else if (!strncmp(p, "opt", strlen(p))) {
543 } else if (!strncmp(p, "ver", strlen(p))) {