2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2016, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
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.
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, see <http://www.gnu.org/licenses/>.
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.
27 * ef_verify.c: Verify game configuration
29 * Known contributors to this file:
30 * Ron Koenderink, 2005
31 * Markus Armbruster, 2006-2016
45 static void verify_fail(int, int, struct castr *, int, char *, ...)
46 ATTRIBUTE((format (printf, 5, 6)));
49 verify_fail(int type, int row, struct castr *ca, int idx, char *fmt, ...)
51 int base = empfile[type].base < 0 ? type : empfile[type].base;
54 fprintf(stderr, "%s %s uid %d",
55 EF_IS_GAME_STATE(base) ? "File" : "Config",
56 ef_nameof(type), row);
58 fprintf(stderr, " field %s", ca->ca_name);
60 fprintf(stderr, "(%d)", idx);
62 fprintf(stderr, ": ");
64 vfprintf(stderr, fmt, ap);
72 struct castr *ca = ef_cadef(type);
75 for (i = 0; ca[i].ca_name; i++) {
77 * Virtual selectors can't be used in xundump, since we lack a
78 * setter to go with ca_get(). Exception: if EFF_MEM is not
79 * set, xundump doesn't touch the table.
81 if (CANT_HAPPEN((ef_flags(type) & EFF_MEM)
83 && ca[i].ca_dump <= CA_DUMP_CONST))
84 ca[i].ca_dump = CA_DUMP_ONLY;
90 verify_tabref(int type, int row, struct castr *ca, int idx, long val)
92 int tabno = ca->ca_table;
93 struct castr *ca_sym = ef_cadef(tabno);
96 if (ca->ca_flags & NSC_BITS) {
98 if (CANT_HAPPEN(ca_sym != symbol_ca))
100 for (i = 0; i < (int)sizeof(long) * 8; i++) {
101 if (val & (1UL << i)) {
102 if (!symbol_by_value(1L << i, ef_ptr(tabno, 0))) {
103 verify_fail(type, row, ca, idx,
104 "bit %d is not in symbol table %s",
105 i, ef_nameof(tabno));
110 } else if (ca_sym == symbol_ca) {
112 if (!symbol_by_value(val, ef_ptr(tabno, 0))) {
113 verify_fail(type, row, ca, idx,
114 "value %ld is not in symbol table %s",
115 val, ef_nameof(tabno));
120 if (val >= ef_nelem(tabno) || val < -1) {
121 verify_fail(type, row, ca, idx,
122 "value %ld indexes table %s out of bounds 0..%d",
123 val, ef_nameof(tabno), ef_nelem(tabno));
126 /* laziness: assumes TABNO is EFF_MEM */
127 if (val >= 0 && !empobj_in_use(tabno, ef_ptr(tabno, val))) {
128 verify_fail(type, row, ca, idx,
129 "value %ld refers to missing element of table %s",
130 val, ef_nameof(tabno));
138 verify_row(int type, int row)
140 struct castr *ca = ef_cadef(type);
141 struct empobj *row_ref;
145 int flags = ef_flags(type);
148 row_ref = ef_ptr(type, row);
150 row_ref = malloc(empfile[type].size);
151 ef_read(type, row, row_ref);
154 if ((flags & EFF_TYPED) && !EF_IS_VIEW(type)) {
155 if (row_ref->ef_type != type || row_ref->uid != row) {
156 verify_fail(type, row, NULL, 0, "header corrupt");
161 if (!empobj_in_use(type, row_ref))
164 for (i = 0; ca[i].ca_name; ++i) {
166 continue; /* virtual */
167 n = CA_ARRAY_LEN(&ca[i]);
170 if (ca[i].ca_table == EF_BAD)
172 nstr_mksymval(&val, &ca[i], j);
173 nstr_eval(&val, 0, row_ref, NSC_NOTYPE);
174 if (CANT_HAPPEN(val.val_type != NSC_LONG)) {
178 if (ca[i].ca_table == type && i == 0) {
180 if (val.val_as.lng != row) {
181 verify_fail(type, row, &ca[i], j,
182 "value is %ld instead of %d",
183 val.val_as.lng, row);
187 if (verify_tabref(type, row, &ca[i], j, val.val_as.lng) < 0)
194 if (!(flags & EFF_MEM))
200 verify_table(int type)
208 for (i = 0; i < ef_nelem(type); i++)
209 retval |= verify_row(type, i);
214 verify_sectors(int may_put)
220 /* laziness: assumes sector file is EFF_MEM */
221 for (i = 0; (sp = getsectid(i)); i++) {
222 sctoff2xy(&x, &y, sp->sct_uid);
223 if (sp->sct_x != x || sp->sct_y != y) {
228 verify_fail(EF_SECTOR, i, NULL, 0, "bogus coordinates (fixed)");
235 verify_planes(int may_put)
241 /* laziness: assumes plane file is EFF_MEM */
242 for (i = 0; (pp = getplanep(i)); i++) {
244 if (pp->pln_flags & PLN_LAUNCHED
245 && (plchr[pp->pln_type].pl_flags & (P_M | P_O)) != P_O) {
246 pp->pln_flags &= ~PLN_LAUNCHED;
247 /* FIXME missile should be destroyed instead */
250 verify_fail(EF_PLANE, i, NULL, 0, "stuck in the air (fixed)");
252 if (pp->pln_ship >= 0 && pp->pln_land >= 0) {
253 verify_fail(EF_PLANE, i, NULL, 0, "on two carriers");
257 if (pp->pln_ship >= 0 || pp->pln_land >= 0) {
258 pp->pln_ship = pp->pln_land = -1;
261 verify_fail(EF_PLANE, i, NULL, 0,
262 "ghost stuck on carrier (fixed)");
270 verify_lands(int may_put)
276 /* laziness: assumes land file is EFF_MEM */
277 for (i = 0; (lp = getlandp(i)); i++) {
279 if (lp->lnd_ship >= 0 && lp->lnd_land >= 0) {
280 verify_fail(EF_LAND, i, NULL, 0, "on two carriers");
284 if (lp->lnd_ship >= 0 || lp->lnd_land >= 0) {
285 lp->lnd_ship = lp->lnd_land = -1;
288 verify_fail(EF_LAND, i, NULL, 0,
289 "ghost stuck on carrier (fixed)");
297 verify_nukes(int may_put)
303 /* laziness: assumes nuke file is EFF_MEM */
304 for (i = 0; (np = getnukep(i)); i++) {
306 if (np->nuk_plane >= 0) {
310 verify_fail(EF_NUKE, i, NULL, 0,
311 "ghost stuck on carrier (fixed)");
319 verify_ship_chr(void)
324 for (i = 0; mchr[i].m_name; i++) {
325 if (!mchr[i].m_name[0])
327 if ((mchr[i].m_flags & M_DCH) && !mchr[i].m_glim) {
328 verify_fail(EF_SHIP_CHR, i, NULL, 0,
329 "flag %s requires non-zero glim",
330 symbol_by_value(M_DCH, ship_chr_flags));
333 if (mchr[i].m_nplanes && !(mchr[i].m_flags & (M_MSL | M_FLY))) {
334 verify_fail(EF_SHIP_CHR, i, NULL, 0,
335 "non-zero nplanes needs flag %s or %s",
336 symbol_by_value(M_FLY, ship_chr_flags),
337 symbol_by_value(M_MSL, ship_chr_flags));
345 verify_plane_chr(void)
350 for (i = 0; plchr[i].pl_name; i++) {
351 if (!plchr[i].pl_name[0])
353 if ((plchr[i].pl_flags & (P_M | P_V)) == P_M) {
354 verify_fail(EF_PLANE_CHR, i, NULL, 0,
355 "flag %s requires flag %s",
356 symbol_by_value(P_M, plane_chr_flags),
357 symbol_by_value(P_V, plane_chr_flags));
365 verify_products(void)
370 /* product makes either level or item, not both */
371 for (i = 0; pchr[i].p_sname; i++) {
372 if (!pchr[i].p_sname[0])
374 if ((pchr[i].p_type >= 0) == (pchr[i].p_level >= 0)) {
375 verify_fail(EF_PRODUCT, i, NULL, 0,
376 "level must be none or type -1");
384 * Verify game configuration is sane.
385 * Return 0 on success, -1 on failure.
388 ef_verify_config(void)
393 for (i = 0; i < EF_MAX; i++) {
394 if (!EF_IS_GAME_STATE(i) && !EF_IS_VIEW(i))
395 retval |= verify_table(i);
399 retval |= verify_ship_chr();
400 retval |= verify_plane_chr();
401 retval |= verify_products();
406 * Verify game state is sane.
407 * Correct minor problems, but write corrections to backing files only
408 * if @may_put is non-zero.
409 * Return -1 if uncorrected problems remain, else 0.
412 ef_verify_state(int may_put)
417 for (i = 0; i < EF_MAX; i++) {
418 if (EF_IS_GAME_STATE(i) || EF_IS_VIEW(i))
419 retval |= verify_table(i);
423 retval |= verify_sectors(may_put);
424 retval |= verify_planes(may_put);
425 retval |= verify_lands(may_put);
426 retval |= verify_nukes(may_put);