]> git.pond.sub.org Git - empserver/blob - src/lib/commands/xdump.c
Update copyright notice
[empserver] / src / lib / commands / xdump.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2012, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 of the License, or
9  *  (at your option) any later version.
10  *
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.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  xdump.c: Extended dump
28  *
29  *  Known contributors to this file:
30  *     Markus Armbruster, 2004-2011
31  */
32
33 #include <config.h>
34
35 #include <ctype.h>
36 #include "commands.h"
37 #include "empobj.h"
38 #include "optlist.h"
39 #include "xdump.h"
40
41 /*
42  * Is object P of type TYPE visible to the player?
43  * TODO: Fold this into interators.
44  */
45 static int
46 xdvisible(int type, void *p)
47 {
48     struct empobj *gp = p;
49     struct trtstr *tp = p;
50     struct lonstr *lp = p;
51     struct natstr *natp;
52     int tlev;
53
54     if (!empobj_in_use(type, p))
55         return 0;
56
57     switch (type) {
58     case EF_SECTOR:
59     case EF_SHIP:
60     case EF_PLANE:
61     case EF_LAND:
62     case EF_NUKE:
63     case EF_LOST:
64     case EF_REALM:
65         return gp->own == player->cnum || player->god;
66     case EF_COUNTRY:
67         return gp->own == player->cnum;
68     case EF_NEWS:
69         return !opt_HIDDEN || player->god; /* FIXME */
70     case EF_TREATY:
71         return tp->trt_cna == player->cnum
72             || tp->trt_cnb == player->cnum
73             || player->god;
74     case EF_LOAN:
75         if (lp->l_status == LS_SIGNED)
76             return 1;
77         return lp->l_loner == player->cnum || lp->l_lonee == player->cnum
78             || player->god;
79     case EF_SHIP_CHR:
80         tlev = ((struct mchrstr *)p)->m_tech;
81         goto tech;
82     case EF_PLANE_CHR:
83         tlev = ((struct plchrstr *)p)->pl_tech;
84         goto tech;
85     case EF_LAND_CHR:
86         tlev = ((struct lchrstr *)p)->l_tech;
87     tech:
88         natp = getnatp(player->cnum);
89         return player->god || tlev <= (int)(1.25 * natp->nat_level[NAT_TLEV]);
90     case EF_NUKE_CHR:
91         tlev = ((struct nchrstr *)p)->n_tech;
92         if (drnuke_const > MIN_DRNUKE_CONST) {
93             natp = getnatp(player->cnum);
94             if (tlev > (int)((int)(1.25 * natp->nat_level[NAT_RLEV])
95                              / drnuke_const))
96                 return player->god;
97         }
98         goto tech;
99     case EF_TABLE:
100         return ((struct empfile *)p)->cadef != NULL;
101     default:
102         return 1;
103     }
104 }
105
106 /*
107  * Dump meta-data for items of type TYPE to XD.
108  * Return RET_SYN when TYPE doesn't have meta-data, else RET_OK.
109  */
110 static int
111 xdmeta(struct xdstr *xd, int type)
112 {
113     struct castr *ca = ef_cadef(type);
114     int i;
115     int n = 0;
116
117     if (!ca)
118         return RET_SYN;
119
120     xdhdr(xd, ef_nameof(type), 1);
121     xdcolhdr(xd, ca);
122
123     for (i = 0; ca[i].ca_name; i++) {
124         if (ca[i].ca_flags & NSC_DEITY && !xd->divine)
125             continue;
126         if (ca[i].ca_flags & NSC_EXTRA)
127             continue;
128         xdflds(xd, mdchr_ca, &ca[i]);
129         xd->pr("\n");
130         n++;
131     }
132
133     xdftr(xd, n);
134
135     return RET_OK;
136 }
137
138 /*
139  * Dump items of type TYPE selected by ARG to XD.
140  * Return RET_OK on success, RET_SYN on error.
141  */
142 static int
143 xditem(struct xdstr *xd, int type, char *arg)
144 {
145     struct castr *ca;
146     struct nstr_item ni;
147     int n;
148     char buf[2048];             /* FIXME buffer size? */
149
150     ca = ef_cadef(type);
151     if (!ca)
152         return RET_SYN;
153
154     if (!snxtitem(&ni, type, arg, NULL))
155         return RET_SYN;
156
157     xdhdr(xd, ef_nameof(type), 0);
158
159     n = 0;
160     while (nxtitem(&ni, buf)) {
161         if (!xdvisible(type, buf))
162             continue;
163         ++n;
164         xdflds(xd, ca, buf);
165         xd->pr("\n");
166     }
167
168     xdftr(xd, n);
169
170     return RET_OK;
171 }
172
173 /* Extended dump command */
174 int
175 xdump(void)
176 {
177     char *p;
178     char buf[1024];
179     struct xdstr xd;
180     struct natstr *natp;
181     int type;
182     int meta = 0;
183
184     p = getstarg(player->argp[1], "Table name, or meta? ", buf);
185     if (p && strcmp(p, "meta") == 0) {
186         meta = 1;
187         p = getstarg(player->argp[2], "Table name? ", buf);
188     }
189     if (!p || !*p)
190         return RET_SYN;
191
192     xdinit(&xd, player->cnum, 0, pr);
193     natp = getnatp(player->cnum);
194     type = isdigit(p[0]) ? atoi(p) : ef_byname(p);
195     if (type < 0 || type >= EF_MAX)
196         return RET_SYN;
197     if (meta)
198         return xdmeta(&xd, type);
199     if ((EF_IS_GAME_STATE(type) || EF_IS_VIEW(type))
200         && !(natp->nat_stat == STAT_ACTIVE || player->god)) {
201         pr("Access to table %s denied\n", ef_nameof(type));
202         return RET_FAIL;
203     }
204     return xditem(&xd, type, player->argp[2]);
205 }