]> git.pond.sub.org Git - empserver/blob - src/lib/common/ef_verify.c
client: Unbreak standalone build
[empserver] / src / lib / common / ef_verify.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2021, 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-2021
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, flags, accepted_flags;
346     char buf[1024];
347
348     for (i = 0; plchr[i].pl_name; i++) {
349         if (!plchr[i].pl_name[0])
350             continue;
351         flags = plchr[i].pl_flags;
352         accepted_flags = P_V | P_K | P_L;
353         if (flags & P_M) {
354             /* missile */
355             accepted_flags |= P_M | P_E;
356             if (flags & P_N)
357                 accepted_flags |=  P_N;
358             else if (flags & P_O)
359                 accepted_flags |=  P_O;
360             else if (flags & P_F)
361                 accepted_flags |=  P_F;
362             else
363                 accepted_flags |=  P_T | P_MAR;
364             if (!(flags & P_V)) {
365                 verify_fail(EF_PLANE_CHR, i, NULL, 0,
366                             "flag %s requires flag %s",
367                             symbol_by_value(P_M, plane_chr_flags),
368                             symbol_by_value(P_V, plane_chr_flags));
369                 retval = -1;
370             }
371         } else if (flags & P_O) {
372             /* satellite */
373             accepted_flags |= P_O | P_S | P_I;
374         } else {
375             /* plane */
376             accepted_flags |= P_B | P_T | P_F | P_C | P_S | P_I | P_A | P_P
377                 | P_ESC | P_MINE | P_SWEEP;
378             if ((flags & (P_P | P_C)) == P_P) {
379                 verify_fail(EF_PLANE_CHR, i, NULL, 0,
380                             "flag %s requires flag %s",
381                             symbol_by_value(P_P, plane_chr_flags),
382                             symbol_by_value(P_C, plane_chr_flags));
383                 retval = -1;
384             }
385         }
386         if (flags & ~accepted_flags) {
387             symbol_set_fmt(buf, sizeof(buf), flags & ~accepted_flags,
388                            plane_chr_flags, ", ", 1);
389             verify_fail(EF_PLANE_CHR, i, NULL, 0,
390                         "invalid flag combination, can't have %s", buf);
391             retval = -1;
392         }
393     }
394     return retval;
395 }
396
397 static int
398 verify_land_chr(void)
399 {
400     int retval = 0;
401     int i;
402
403     for (i = 0; lchr[i].l_name; i++) {
404         if (!lchr[i].l_name[0])
405             continue;
406         if ((lchr[i].l_flags & L_SPY) && lchr[i].l_item[I_MILIT]) {
407             verify_fail(EF_LAND_CHR, i, NULL, 0,
408                         "flag %s requires zero milit",
409                         symbol_by_value(L_SPY, land_chr_flags));
410             retval = -1;
411         }
412     }
413     return retval;
414 }
415
416 static int
417 verify_products(void)
418 {
419     int retval = 0;
420     int i;
421
422     /* product makes either level or item, not both */
423     for (i = 0; pchr[i].p_sname; i++) {
424         if (!pchr[i].p_sname[0])
425             continue;
426         if ((pchr[i].p_type >= 0) == (pchr[i].p_level >= 0)) {
427             verify_fail(EF_PRODUCT, i, NULL, 0,
428                         "level must be none or type -1");
429             retval = -1;
430         }
431     }
432     return retval;
433 }
434
435 /*
436  * Verify game configuration is sane.
437  * Return 0 on success, -1 on failure.
438  */
439 int
440 ef_verify_config(void)
441 {
442     int retval = 0;
443     int i;
444
445     for (i = 0; i < EF_MAX; i++) {
446         if (!EF_IS_GAME_STATE(i) && !EF_IS_VIEW(i))
447             retval |= verify_table(i);
448     }
449
450     /* Special checks */
451     retval |= verify_ship_chr();
452     retval |= verify_plane_chr();
453     retval |= verify_land_chr();
454     retval |= verify_products();
455     return retval;
456 }
457
458 /*
459  * Verify game state is sane.
460  * Correct minor problems, but write corrections to backing files only
461  * if @may_put is non-zero.
462  * Return -1 if uncorrected problems remain, else 0.
463  */
464 int
465 ef_verify_state(int may_put)
466 {
467     int retval = 0;
468     int i;
469
470     for (i = 0; i < EF_MAX; i++) {
471         if (EF_IS_GAME_STATE(i) || EF_IS_VIEW(i))
472             retval |= verify_table(i);
473     }
474
475     /* Special checks */
476     retval |= verify_sectors(may_put);
477     retval |= verify_planes(may_put);
478     retval |= verify_lands(may_put);
479     retval |= verify_nukes(may_put);
480     return retval;
481 }