]> git.pond.sub.org Git - empserver/blob - src/lib/common/ef_verify.c
ef_verify: Fix error message in verify_land_chr()
[empserver] / src / lib / common / ef_verify.c
1 /*
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
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  *  ef_verify.c: Verify game configuration
28  *
29  *  Known contributors to this file:
30  *     Ron Koenderink, 2005
31  *     Markus Armbruster, 2006-2016
32  */
33
34 #include <config.h>
35
36 #include <stdarg.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include "empobj.h"
40 #include "misc.h"
41 #include "nsc.h"
42 #include "product.h"
43 #include "xdump.h"
44
45 static void verify_fail(int, int, struct castr *, int, char *, ...)
46     ATTRIBUTE((format (printf, 5, 6)));
47
48 static void
49 verify_fail(int type, int row, struct castr *ca, int idx, char *fmt, ...)
50 {
51     int base = empfile[type].base < 0 ? type : empfile[type].base;
52     va_list ap;
53
54     fprintf(stderr, "%s %s uid %d",
55             EF_IS_GAME_STATE(base) ? "File" : "Config",
56             ef_nameof(type), row);
57     if (ca) {
58         fprintf(stderr, " field %s", ca->ca_name);
59         if (CA_IS_ARRAY(ca))
60             fprintf(stderr, "(%d)", idx);
61     }
62     fprintf(stderr, ": ");
63     va_start(ap, fmt);
64     vfprintf(stderr, fmt, ap);
65     va_end(ap);
66     putc('\n', stderr);
67 }
68
69 static int
70 verify_ca(int type)
71 {
72     struct castr *ca = ef_cadef(type);
73     int i;
74
75     for (i = 0; ca && ca[i].ca_name; i++) {
76         /*
77          * Virtual selectors can't be used in xundump, since we lack a
78          * setter to go with ca_get().
79          */
80         if (CANT_HAPPEN(xundumpable(type)
81                         && ca[i].ca_get
82                         && ca[i].ca_dump <= CA_DUMP_CONST))
83             ca[i].ca_dump = CA_DUMP_ONLY;
84     }
85     return 0;
86 }
87
88 static int
89 verify_tabref(int type, int row, struct castr *ca, int idx, long val)
90 {
91     int tabno = ca->ca_table;
92     struct castr *ca_sym = ef_cadef(tabno);
93     int i;
94
95     if (ca->ca_flags & NSC_BITS) {
96         /* symbol set */
97         if (CANT_HAPPEN(ca_sym != symbol_ca))
98             return -1;
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));
105                     return -1;
106                 }
107             }
108         }
109     } else if (ca_sym == symbol_ca) {
110         /* symbol */
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));
115             return -1;
116         }
117     } else {
118         /* table index */
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));
123             return -1;
124         }
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));
130             return -1;
131         }
132     }
133     return 0;
134 }
135
136 static int
137 verify_row(int type, int row)
138 {
139     struct castr *ca = ef_cadef(type);
140     struct empobj *row_ref;
141     int i, j, n;
142     struct valstr val;
143     int ret_val = 0;
144     int flags = ef_flags(type);
145
146     if (flags & EFF_MEM)
147         row_ref = ef_ptr(type, row);
148     else {
149         row_ref = malloc(empfile[type].size);
150         ef_read(type, row, row_ref);
151     }
152
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");
156             ret_val = -1;
157         }
158     }
159
160     if (!empobj_in_use(type, row_ref))
161         goto out;
162
163     for (i = 0; ca && ca[i].ca_name; ++i) {
164         if (ca[i].ca_get)
165             continue;           /* virtual */
166         n = CA_ARRAY_LEN(&ca[i]);
167         j = 0;
168         do {
169             if (ca[i].ca_table == EF_BAD)
170                 continue;
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)) {
174                 ret_val = -1;
175                 continue;
176             }
177             if (ca[i].ca_table == type && i == 0) {
178                 /* UID */
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);
183                     ret_val = -1;
184                 }
185             } else {
186                 if (verify_tabref(type, row, &ca[i], j, val.val_as.lng) < 0)
187                     ret_val = -1;
188             }
189         } while (++j < n);
190     }
191
192 out:
193     if (!(flags & EFF_MEM))
194         free(row_ref);
195     return ret_val;
196 }
197
198 static int
199 verify_table(int type)
200 {
201     int retval = 0;
202     int i;
203
204     verify_ca(type);
205     for (i = 0; i < ef_nelem(type); i++)
206         retval |= verify_row(type, i);
207     return retval;
208 }
209
210 static int
211 verify_sectors(int may_put)
212 {
213     int i;
214     struct sctstr *sp;
215     coord x, y;
216
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) {
221             sp->sct_x = x;
222             sp->sct_y = y;
223             if (may_put)
224                 putsect(sp);
225             verify_fail(EF_SECTOR, i, NULL, 0, "bogus coordinates (fixed)");
226         }
227     }
228     return 0;
229 }
230
231 static int
232 verify_planes(int may_put)
233 {
234     int retval = 0;
235     int i;
236     struct plnstr *pp;
237
238     /* laziness: assumes plane file is EFF_MEM */
239     for (i = 0; (pp = getplanep(i)); i++) {
240         if (pp->pln_own) {
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 */
245                 if (may_put)
246                     putplane(i, pp);
247                 verify_fail(EF_PLANE, i, NULL, 0, "stuck in the air (fixed)");
248             }
249             if (pp->pln_ship >= 0 && pp->pln_land >= 0) {
250                 verify_fail(EF_PLANE, i, NULL, 0, "on two carriers");
251                 retval = -1;
252             }
253         } else {
254             if (pp->pln_ship >= 0 || pp->pln_land >= 0) {
255                 pp->pln_ship = pp->pln_land = -1;
256                 if (may_put)
257                     putplane(i, pp);
258                 verify_fail(EF_PLANE, i, NULL, 0,
259                             "ghost stuck on carrier (fixed)");
260             }
261         }
262     }
263     return retval;
264 }
265
266 static int
267 verify_lands(int may_put)
268 {
269     int retval = 0;
270     int i;
271     struct lndstr *lp;
272
273     /* laziness: assumes land file is EFF_MEM */
274     for (i = 0; (lp = getlandp(i)); i++) {
275         if (lp->lnd_own) {
276             if (lp->lnd_ship >= 0 && lp->lnd_land >= 0) {
277                 verify_fail(EF_LAND, i, NULL, 0, "on two carriers");
278                 retval = -1;
279             }
280         } else {
281             if (lp->lnd_ship >= 0 || lp->lnd_land >= 0) {
282                 lp->lnd_ship = lp->lnd_land = -1;
283                 if (may_put)
284                     putland(i, lp);
285                 verify_fail(EF_LAND, i, NULL, 0,
286                             "ghost stuck on carrier (fixed)");
287             }
288         }
289     }
290     return retval;
291 }
292
293 static int
294 verify_nukes(int may_put)
295 {
296     int retval = 0;
297     int i;
298     struct nukstr *np;
299
300     /* laziness: assumes nuke file is EFF_MEM */
301     for (i = 0; (np = getnukep(i)); i++) {
302         if (!np->nuk_own) {
303             if (np->nuk_plane >= 0) {
304                 np->nuk_plane = -1;
305                 if (may_put)
306                     putnuke(i, np);
307                 verify_fail(EF_NUKE, i, NULL, 0,
308                             "ghost stuck on carrier (fixed)");
309             }
310         }
311     }
312     return retval;
313 }
314
315 static int
316 verify_ship_chr(void)
317 {
318     int retval = 0;
319     int i;
320
321     for (i = 0; mchr[i].m_name; i++) {
322         if (!mchr[i].m_name[0])
323             continue;
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));
328             retval = -1;
329         }
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));
335             retval = -1;
336         }
337     }
338     return retval;
339 }
340
341 static int
342 verify_plane_chr(void)
343 {
344     int retval = 0;
345     int i;
346
347     for (i = 0; plchr[i].pl_name; i++) {
348         if (!plchr[i].pl_name[0])
349             continue;
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));
355             retval = -1;
356         }
357     }
358     return retval;
359 }
360
361 static int
362 verify_land_chr(void)
363 {
364     int retval = 0;
365     int i;
366
367     for (i = 0; lchr[i].l_name; i++) {
368         if (!lchr[i].l_name[0])
369             continue;
370         if ((lchr[i].l_flags & L_SPY) && lchr[i].l_item[I_MILIT]) {
371             verify_fail(EF_LAND_CHR, i, NULL, 0,
372                         "flag %s requires zero milit",
373                         symbol_by_value(L_SPY, land_chr_flags));
374             retval = -1;
375         }
376     }
377     return retval;
378 }
379
380 static int
381 verify_products(void)
382 {
383     int retval = 0;
384     int i;
385
386     /* product makes either level or item, not both */
387     for (i = 0; pchr[i].p_sname; i++) {
388         if (!pchr[i].p_sname[0])
389             continue;
390         if ((pchr[i].p_type >= 0) == (pchr[i].p_level >= 0)) {
391             verify_fail(EF_PRODUCT, i, NULL, 0,
392                         "level must be none or type -1");
393             retval = -1;
394         }
395     }
396     return retval;
397 }
398
399 /*
400  * Verify game configuration is sane.
401  * Return 0 on success, -1 on failure.
402  */
403 int
404 ef_verify_config(void)
405 {
406     int retval = 0;
407     int i;
408
409     for (i = 0; i < EF_MAX; i++) {
410         if (!EF_IS_GAME_STATE(i) && !EF_IS_VIEW(i))
411             retval |= verify_table(i);
412     }
413
414     /* Special checks */
415     retval |= verify_ship_chr();
416     retval |= verify_plane_chr();
417     retval |= verify_land_chr();
418     retval |= verify_products();
419     return retval;
420 }
421
422 /*
423  * Verify game state is sane.
424  * Correct minor problems, but write corrections to backing files only
425  * if @may_put is non-zero.
426  * Return -1 if uncorrected problems remain, else 0.
427  */
428 int
429 ef_verify_state(int may_put)
430 {
431     int retval = 0;
432     int i;
433
434     for (i = 0; i < EF_MAX; i++) {
435         if (EF_IS_GAME_STATE(i) || EF_IS_VIEW(i))
436             retval |= verify_table(i);
437     }
438
439     /* Special checks */
440     retval |= verify_sectors(may_put);
441     retval |= verify_planes(may_put);
442     retval |= verify_lands(may_put);
443     retval |= verify_nukes(may_put);
444     return retval;
445 }