]> git.pond.sub.org Git - empserver/blobdiff - src/lib/common/ef_verify.c
ef_verify: Reject invalid plane flag combinations
[empserver] / src / lib / common / ef_verify.c
index 2345efdb731520deb29f27ae4c0ab975a9f51ec8..71d437c25ba80d498689a347f70b2347f246660d 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2014, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2021, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                Ken Stevens, Steve McClure, Markus Armbruster
  *
  *  Empire is free software: you can redistribute it and/or modify
@@ -28,7 +28,7 @@
  *
  *  Known contributors to this file:
  *     Ron Koenderink, 2005
- *     Markus Armbruster, 2006-2014
+ *     Markus Armbruster, 2006-2021
  */
 
 #include <config.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include "empobj.h"
-#include "file.h"
 #include "misc.h"
 #include "nsc.h"
 #include "product.h"
+#include "xdump.h"
 
 static void verify_fail(int, int, struct castr *, int, char *, ...)
     ATTRIBUTE((format (printf, 5, 6)));
@@ -72,15 +72,15 @@ verify_ca(int type)
     struct castr *ca = ef_cadef(type);
     int i;
 
-    for (i = 0; ca[i].ca_name; i++) {
+    for (i = 0; ca && ca[i].ca_name; i++) {
        /*
-        * Virtual selectors must be NSC_EXTRA, because xundump can't
-        * cope with them without setter methods.  Exception: if
-        * EFF_MEM is not set, xundump doesn't touch the table.
+        * Virtual selectors can't be used in xundump, since we lack a
+        * setter to go with ca_get().
         */
-       if (CANT_HAPPEN((ef_flags(type) & EFF_MEM)
-                       && ca[i].ca_get && !(ca[i].ca_flags & NSC_EXTRA)))
-           ca[i].ca_flags |= NSC_EXTRA;
+       if (CANT_HAPPEN(xundumpable(type)
+                       && ca[i].ca_get
+                       && ca[i].ca_dump <= CA_DUMP_CONST))
+           ca[i].ca_dump = CA_DUMP_ONLY;
     }
     return 0;
 }
@@ -97,7 +97,7 @@ verify_tabref(int type, int row, struct castr *ca, int idx, long val)
        if (CANT_HAPPEN(ca_sym != symbol_ca))
            return -1;
        for (i = 0; i < (int)sizeof(long) * 8; i++) {
-           if (val & (1L << i)) {
+           if (val & (1UL << i)) {
                if (!symbol_by_value(1L << i, ef_ptr(tabno, 0))) {
                    verify_fail(type, row, ca, idx,
                                "bit %d is not in symbol table %s",
@@ -160,7 +160,7 @@ verify_row(int type, int row)
     if (!empobj_in_use(type, row_ref))
        goto out;
 
-    for (i = 0; ca[i].ca_name; ++i) {
+    for (i = 0; ca && ca[i].ca_name; ++i) {
        if (ca[i].ca_get)
            continue;           /* virtual */
        n = CA_ARRAY_LEN(&ca[i]);
@@ -175,7 +175,7 @@ verify_row(int type, int row)
                continue;
            }
            if (ca[i].ca_table == type && i == 0) {
-               /* uid */
+               /* UID */
                if (val.val_as.lng != row) {
                    verify_fail(type, row, &ca[i], j,
                                "value is %ld instead of %d",
@@ -201,8 +201,6 @@ verify_table(int type)
     int retval = 0;
     int i;
 
-    if (!ef_cadef(type))
-       return 0;
     verify_ca(type);
     for (i = 0; i < ef_nelem(type); i++)
        retval |= verify_row(type, i);
@@ -314,6 +312,107 @@ verify_nukes(int may_put)
     return retval;
 }
 
+static int
+verify_ship_chr(void)
+{
+    int retval = 0;
+    int i;
+
+    for (i = 0; mchr[i].m_name; i++) {
+       if (!mchr[i].m_name[0])
+           continue;
+       if ((mchr[i].m_flags & M_DCH) && !mchr[i].m_glim) {
+           verify_fail(EF_SHIP_CHR, i, NULL, 0,
+                       "flag %s requires non-zero glim",
+                       symbol_by_value(M_DCH, ship_chr_flags));
+           retval = -1;
+       }
+       if (mchr[i].m_nplanes && !(mchr[i].m_flags & (M_MSL | M_FLY))) {
+           verify_fail(EF_SHIP_CHR, i, NULL, 0,
+                       "non-zero nplanes needs flag %s or %s",
+                       symbol_by_value(M_FLY, ship_chr_flags),
+                       symbol_by_value(M_MSL, ship_chr_flags));
+           retval = -1;
+       }
+    }
+    return retval;
+}
+
+static int
+verify_plane_chr(void)
+{
+    int retval = 0;
+    int i, flags, accepted_flags;
+    char buf[1024];
+
+    for (i = 0; plchr[i].pl_name; i++) {
+       if (!plchr[i].pl_name[0])
+           continue;
+       flags = plchr[i].pl_flags;
+       accepted_flags = P_V | P_K | P_L;
+       if (flags & P_M) {
+           /* missile */
+           accepted_flags |= P_M | P_E;
+           if (flags & P_N)
+               accepted_flags |=  P_N;
+           else if (flags & P_O)
+               accepted_flags |=  P_O;
+           else if (flags & P_F)
+               accepted_flags |=  P_F;
+           else
+               accepted_flags |=  P_T | P_MAR;
+           if (!(flags & P_V)) {
+               verify_fail(EF_PLANE_CHR, i, NULL, 0,
+                           "flag %s requires flag %s",
+                           symbol_by_value(P_M, plane_chr_flags),
+                           symbol_by_value(P_V, plane_chr_flags));
+               retval = -1;
+           }
+       } else if (flags & P_O) {
+           /* satellite */
+           accepted_flags |= P_O | P_S | P_I;
+       } else {
+           /* plane */
+           accepted_flags |= P_B | P_T | P_F | P_C | P_S | P_I | P_A | P_P
+               | P_ESC | P_MINE | P_SWEEP;
+           if ((flags & (P_P | P_C)) == P_P) {
+               verify_fail(EF_PLANE_CHR, i, NULL, 0,
+                           "flag %s requires flag %s",
+                           symbol_by_value(P_P, plane_chr_flags),
+                           symbol_by_value(P_C, plane_chr_flags));
+               retval = -1;
+           }
+       }
+       if (flags & ~accepted_flags) {
+           symbol_set_fmt(buf, sizeof(buf), flags & ~accepted_flags,
+                          plane_chr_flags, ", ", 1);
+           verify_fail(EF_PLANE_CHR, i, NULL, 0,
+                       "invalid flag combination, can't have %s", buf);
+           retval = -1;
+       }
+    }
+    return retval;
+}
+
+static int
+verify_land_chr(void)
+{
+    int retval = 0;
+    int i;
+
+    for (i = 0; lchr[i].l_name; i++) {
+       if (!lchr[i].l_name[0])
+           continue;
+       if ((lchr[i].l_flags & L_SPY) && lchr[i].l_item[I_MILIT]) {
+           verify_fail(EF_LAND_CHR, i, NULL, 0,
+                       "flag %s requires zero milit",
+                       symbol_by_value(L_SPY, land_chr_flags));
+           retval = -1;
+       }
+    }
+    return retval;
+}
+
 static int
 verify_products(void)
 {
@@ -325,9 +424,8 @@ verify_products(void)
        if (!pchr[i].p_sname[0])
            continue;
        if ((pchr[i].p_type >= 0) == (pchr[i].p_level >= 0)) {
-           fprintf(stderr,
-               "Config %s uid %d field level doesn't match field type\n",
-               ef_nameof(EF_PRODUCT), i);
+           verify_fail(EF_PRODUCT, i, NULL, 0,
+                       "level must be none or type -1");
            retval = -1;
        }
     }
@@ -350,6 +448,9 @@ ef_verify_config(void)
     }
 
     /* Special checks */
+    retval |= verify_ship_chr();
+    retval |= verify_plane_chr();
+    retval |= verify_land_chr();
     retval |= verify_products();
     return retval;
 }
@@ -357,7 +458,7 @@ ef_verify_config(void)
 /*
  * Verify game state is sane.
  * Correct minor problems, but write corrections to backing files only
- * if MAY_PUT is non-zero.
+ * if @may_put is non-zero.
  * Return -1 if uncorrected problems remain, else 0.
  */
 int