2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2011, 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-2011
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);
59 if (ca->ca_type != NSC_STRINGY && ca->ca_len != 0)
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 must be NSC_EXTRA, because xundump can't
78 * cope with them without setter methods. Exception: if
79 * EFF_MEM is not set, xundump doesn't touch the table.
81 if (CANT_HAPPEN((ef_flags(type) & EFF_MEM)
82 && ca[i].ca_get && !(ca[i].ca_flags & NSC_EXTRA)))
83 ca[i].ca_flags |= NSC_EXTRA;
89 verify_tabref(int type, int row, struct castr *ca, int idx, long val)
91 int tabno = ca->ca_table;
92 struct castr *ca_sym = ef_cadef(tabno);
95 if (ca->ca_flags & NSC_BITS) {
97 if (CANT_HAPPEN(ca_sym != symbol_ca))
99 for (i = 0; i < (int)sizeof(long) * 8; i++) {
100 if (val & (1L << i)) {
101 if (!symbol_by_value(1L << i, ef_ptr(tabno, 0))) {
102 verify_fail(type, row, ca, idx,
103 "bit %d is not in symbol table %s",
104 i, ef_nameof(tabno));
109 } else if (ca_sym == symbol_ca) {
111 if (!symbol_by_value(val, ef_ptr(tabno, 0))) {
112 verify_fail(type, row, ca, idx,
113 "value %ld is not in symbol table %s",
114 val, ef_nameof(tabno));
119 if (val >= ef_nelem(tabno) || val < -1) {
120 verify_fail(type, row, ca, idx,
121 "value %ld indexes table %s out of bounds 0..%d",
122 val, ef_nameof(tabno), ef_nelem(tabno));
125 /* laziness: assumes TABNO is EFF_MEM */
126 if (val >= 0 && !empobj_in_use(tabno, ef_ptr(tabno, val))) {
127 verify_fail(type, row, ca, idx,
128 "value %ld refers to missing element of table %s",
129 val, ef_nameof(tabno));
137 verify_row(int type, int row)
139 struct castr *ca = ef_cadef(type);
140 struct empobj *row_ref;
144 int flags = ef_flags(type);
147 row_ref = ef_ptr(type, row);
149 row_ref = malloc(empfile[type].size);
150 ef_read(type, row, row_ref);
153 if ((flags & EFF_TYPED) && !EF_IS_VIEW(type)) {
154 if (row_ref->ef_type != type || row_ref->uid != row) {
155 verify_fail(type, row, NULL, 0, "header corrupt");
160 if (!empobj_in_use(type, row_ref))
163 for (i = 0; ca[i].ca_name; ++i) {
165 continue; /* virtual */
166 n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
169 if (ca[i].ca_table == EF_BAD)
171 nstr_mksymval(&val, &ca[i], j);
172 nstr_exec_val(&val, 0, row_ref, NSC_NOTYPE);
173 if (CANT_HAPPEN(val.val_type != NSC_LONG)) {
177 if (ca[i].ca_table == type && i == 0) {
179 if (val.val_as.lng != row) {
180 verify_fail(type, row, &ca[i], j,
181 "value is %ld instead of %d",
182 val.val_as.lng, row);
186 if (verify_tabref(type, row, &ca[i], j, val.val_as.lng) < 0)
193 if (!(flags & EFF_MEM))
199 verify_table(int type)
207 for (i = 0; i < ef_nelem(type); i++)
208 retval |= verify_row(type, i);
213 verify_planes(int may_put)
219 /* laziness: assumes plane file is EFF_MEM */
220 for (i = 0; (pp = getplanep(i)); i++) {
222 if (pp->pln_flags & PLN_LAUNCHED
223 && (plchr[pp->pln_type].pl_flags & (P_M | P_O)) != P_O) {
224 pp->pln_flags &= ~PLN_LAUNCHED;
225 /* FIXME missile should be destroyed instead */
228 verify_fail(EF_PLANE, i, NULL, 0, "stuck in the air (fixed)");
230 if (pp->pln_ship >= 0 && pp->pln_land >= 0) {
231 verify_fail(EF_PLANE, i, NULL, 0, "on two carriers");
235 if (pp->pln_ship >= 0 || pp->pln_land >= 0) {
236 pp->pln_ship = pp->pln_land = -1;
239 verify_fail(EF_PLANE, i, NULL, 0,
240 "ghost stuck on carrier (fixed)");
248 verify_lands(int may_put)
254 /* laziness: assumes land file is EFF_MEM */
255 for (i = 0; (lp = getlandp(i)); i++) {
257 if (lp->lnd_ship >= 0 && lp->lnd_land >= 0) {
258 verify_fail(EF_LAND, i, NULL, 0, "on two carriers");
262 if (lp->lnd_ship >= 0 || lp->lnd_land >= 0) {
263 lp->lnd_ship = lp->lnd_land = -1;
266 verify_fail(EF_LAND, i, NULL, 0,
267 "ghost stuck on carrier (fixed)");
275 verify_nukes(int may_put)
281 /* laziness: assumes nuke file is EFF_MEM */
282 for (i = 0; (np = getnukep(i)); i++) {
284 if (np->nuk_plane >= 0) {
288 verify_fail(EF_NUKE, i, NULL, 0,
289 "ghost stuck on carrier (fixed)");
297 verify_products(void)
302 /* product makes either level or item, not both */
303 for (i = 0; pchr[i].p_sname; i++) {
304 if (!pchr[i].p_sname[0])
306 if ((pchr[i].p_type >= 0) == (pchr[i].p_level >= 0)) {
308 "Config %s uid %d field level doesn't match field type\n",
309 ef_nameof(EF_PRODUCT), i);
317 * Verify game configuration is sane.
318 * Return 0 on success, -1 on failure.
321 ef_verify_config(void)
326 for (i = 0; i < EF_MAX; i++) {
327 if (!EF_IS_GAME_STATE(i) && !EF_IS_VIEW(i))
328 retval |= verify_table(i);
332 retval |= verify_products();
337 * Verify game state is sane.
338 * Correct minor problems, but write corrections to backing files only
339 * if MAY_PUT is non-zero.
340 * Return -1 if uncorrected problems remain, else 0.
343 ef_verify_state(int may_put)
348 for (i = 0; i < EF_MAX; i++) {
349 if (EF_IS_GAME_STATE(i) || EF_IS_VIEW(i))
350 retval |= verify_table(i);
354 retval |= verify_planes(may_put);
355 retval |= verify_lands(may_put);
356 retval |= verify_nukes(may_put);