2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2017, 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 && 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().
80 if (CANT_HAPPEN(xundumpable(type)
82 && ca[i].ca_dump <= CA_DUMP_CONST))
83 ca[i].ca_dump = CA_DUMP_ONLY;
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 & (1UL << 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 && ca[i].ca_name; ++i) {
165 continue; /* virtual */
166 n = CA_ARRAY_LEN(&ca[i]);
169 if (ca[i].ca_table == EF_BAD)
171 nstr_mksymval(&val, &ca[i], j);
172 nstr_eval(&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)
205 for (i = 0; i < ef_nelem(type); i++)
206 retval |= verify_row(type, i);
211 verify_sectors(int may_put)
217 /* laziness: assumes sector file is EFF_MEM */
218 for (i = 0; (sp = getsectid(i)); i++) {
219 sctoff2xy(&x, &y, sp->sct_uid);
220 if (sp->sct_x != x || sp->sct_y != y) {
225 verify_fail(EF_SECTOR, i, NULL, 0, "bogus coordinates (fixed)");
232 verify_planes(int may_put)
238 /* laziness: assumes plane file is EFF_MEM */
239 for (i = 0; (pp = getplanep(i)); i++) {
241 if (pp->pln_flags & PLN_LAUNCHED
242 && (plchr[pp->pln_type].pl_flags & (P_M | P_O)) != P_O) {
243 pp->pln_flags &= ~PLN_LAUNCHED;
244 /* FIXME missile should be destroyed instead */
247 verify_fail(EF_PLANE, i, NULL, 0, "stuck in the air (fixed)");
249 if (pp->pln_ship >= 0 && pp->pln_land >= 0) {
250 verify_fail(EF_PLANE, i, NULL, 0, "on two carriers");
254 if (pp->pln_ship >= 0 || pp->pln_land >= 0) {
255 pp->pln_ship = pp->pln_land = -1;
258 verify_fail(EF_PLANE, i, NULL, 0,
259 "ghost stuck on carrier (fixed)");
267 verify_lands(int may_put)
273 /* laziness: assumes land file is EFF_MEM */
274 for (i = 0; (lp = getlandp(i)); i++) {
276 if (lp->lnd_ship >= 0 && lp->lnd_land >= 0) {
277 verify_fail(EF_LAND, i, NULL, 0, "on two carriers");
281 if (lp->lnd_ship >= 0 || lp->lnd_land >= 0) {
282 lp->lnd_ship = lp->lnd_land = -1;
285 verify_fail(EF_LAND, i, NULL, 0,
286 "ghost stuck on carrier (fixed)");
294 verify_nukes(int may_put)
300 /* laziness: assumes nuke file is EFF_MEM */
301 for (i = 0; (np = getnukep(i)); i++) {
303 if (np->nuk_plane >= 0) {
307 verify_fail(EF_NUKE, i, NULL, 0,
308 "ghost stuck on carrier (fixed)");
316 verify_ship_chr(void)
321 for (i = 0; mchr[i].m_name; i++) {
322 if (!mchr[i].m_name[0])
324 if ((mchr[i].m_flags & M_DCH) && !mchr[i].m_glim) {
325 verify_fail(EF_SHIP_CHR, i, NULL, 0,
326 "flag %s requires non-zero glim",
327 symbol_by_value(M_DCH, ship_chr_flags));
330 if (mchr[i].m_nplanes && !(mchr[i].m_flags & (M_MSL | M_FLY))) {
331 verify_fail(EF_SHIP_CHR, i, NULL, 0,
332 "non-zero nplanes needs flag %s or %s",
333 symbol_by_value(M_FLY, ship_chr_flags),
334 symbol_by_value(M_MSL, ship_chr_flags));
342 verify_plane_chr(void)
347 for (i = 0; plchr[i].pl_name; i++) {
348 if (!plchr[i].pl_name[0])
350 if ((plchr[i].pl_flags & (P_M | P_V)) == P_M) {
351 verify_fail(EF_PLANE_CHR, i, NULL, 0,
352 "flag %s requires flag %s",
353 symbol_by_value(P_M, plane_chr_flags),
354 symbol_by_value(P_V, plane_chr_flags));
362 verify_land_chr(void)
367 for (i = 0; lchr[i].l_name; i++) {
368 if (!lchr[i].l_name[0])
370 if ((lchr[i].l_flags & L_SPY) && lchr[i].l_item[I_MILIT]) {
371 verify_fail(EF_PLANE_CHR, i, NULL, 0,
372 "flag %s requires zero milit",
373 symbol_by_value(L_SPY, land_chr_flags));
380 verify_products(void)
385 /* product makes either level or item, not both */
386 for (i = 0; pchr[i].p_sname; i++) {
387 if (!pchr[i].p_sname[0])
389 if ((pchr[i].p_type >= 0) == (pchr[i].p_level >= 0)) {
390 verify_fail(EF_PRODUCT, i, NULL, 0,
391 "level must be none or type -1");
399 * Verify game configuration is sane.
400 * Return 0 on success, -1 on failure.
403 ef_verify_config(void)
408 for (i = 0; i < EF_MAX; i++) {
409 if (!EF_IS_GAME_STATE(i) && !EF_IS_VIEW(i))
410 retval |= verify_table(i);
414 retval |= verify_ship_chr();
415 retval |= verify_plane_chr();
416 retval |= verify_land_chr();
417 retval |= verify_products();
422 * Verify game state is sane.
423 * Correct minor problems, but write corrections to backing files only
424 * if @may_put is non-zero.
425 * Return -1 if uncorrected problems remain, else 0.
428 ef_verify_state(int may_put)
433 for (i = 0; i < EF_MAX; i++) {
434 if (EF_IS_GAME_STATE(i) || EF_IS_VIEW(i))
435 retval |= verify_table(i);
439 retval |= verify_sectors(may_put);
440 retval |= verify_planes(may_put);
441 retval |= verify_lands(may_put);
442 retval |= verify_nukes(may_put);