]> git.pond.sub.org Git - empserver/blobdiff - src/lib/common/nsc.c
New macro ARRAY_SIZE()
[empserver] / src / lib / common / nsc.c
index f5fdee469ce6e5213ac11b6643fd3cac3b0b643b..d66493e64518f82ff000a0f2a3241341b9072a3e 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2016, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2020, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                Ken Stevens, Steve McClure, Markus Armbruster
  *
  *  Empire is free software: you can redistribute it and/or modify
@@ -27,7 +27,7 @@
  *  nsc.c: Empire selection global structures
  *
  *  Known contributors to this file:
- *     Markus Armbruster, 2004-2016
+ *     Markus Armbruster, 2004-2020
  */
 
 /*
 
 #include <config.h>
 
+#include <limits.h>
 #include <stdlib.h>
 #include "empobj.h"
-#include "file.h"
 #include "optlist.h"
 #include "news.h"
 #include "nsc.h"
 #include "product.h"
 #include "unit.h"
+#include "version.h"
 
 static void *nsc_ver(struct valstr *, struct natstr *, void *);
 static void *nsc_ver_maxnoc(struct valstr *, struct natstr *, void *);
+static void *nsc_ver_version(struct valstr *, struct natstr *, void *);
 static void *nsc_sct_terr(struct valstr *, struct natstr *, void *);
 static void *nsc_sct_track(struct valstr *, struct natstr *, void *);
 static void *nsc_cargo_nplane(struct valstr *, struct natstr *, void *);
@@ -68,6 +70,8 @@ static void *nsc_lnd_dam(struct valstr *, struct natstr *, void *);
 static void *nsc_lnd_aaf(struct valstr *, struct natstr *, void *);
 static void *nsc_lchr(struct valstr *, struct natstr *, void *);
 static void *nsc_nws_timestamp(struct valstr *, struct natstr *, void *);
+static void *nsc_meta_type(struct valstr *, struct natstr *, void *);
+static void *nsc_meta_len(struct valstr *, struct natstr *, void *);
 
 /* Ugly hacks to avoid illegibly long lines */
 #define fldoff(fld) offsetof(CURSTR, fld)
@@ -122,7 +126,6 @@ struct castr ichr_ca[] = {
     {"mnem", fldoff(i_mnem), NSC_STRINGY, 1, NULL, EF_BAD, 0,
      CA_DUMP_CONST},
     {"power", fldoff(i_power), NSC_INT, 0, NULL, EF_BAD, 0, CA_DUMP},
-    {"value", fldoff(i_value), NSC_INT, 0, NULL, EF_BAD, 0, CA_DUMP},
     {"sell", fldoff(i_sell), NSC_INT, 0, NULL, EF_BAD, 0, CA_DUMP},
     {"lbs", fldoff(i_lbs), NSC_INT, 0, NULL, EF_BAD, 0, CA_DUMP},
     {"pkg", fldoff(i_pkg), NSC_INT, NUMPKG, NULL, EF_BAD, 0, CA_DUMP},
@@ -239,8 +242,8 @@ struct castr dchr_ca[] = {
      EF_PACKING, 0, CA_DUMP},
     {"ostr", fldoff(d_ostr), NSC_FLOAT, 0, NULL, EF_BAD, 0, CA_DUMP},
     {"dstr", fldoff(d_dstr), NSC_FLOAT, 0, NULL, EF_BAD, 0, CA_DUMP},
-    {"value", fldoff(d_value), NSC_INT, 0, NULL, EF_BAD, 0, CA_DUMP},
     NSC_MVEC(fldoff(d_mat), CA_DUMP_ONLY, CA_DUMP_ONLY, CA_DUMP_ONLY),
+    {"bwork", fldoff(d_bwork), NSC_INT, 0, NULL, EF_BAD, 0, CA_DUMP},
     {"cost", fldoff(d_cost), NSC_INT, 0, NULL, EF_BAD, 0, CA_DUMP},
     {"maint", fldoff(d_maint), NSC_INT, 0, NULL, EF_BAD, 0, CA_DUMP},
     {"maxpop", fldoff(d_maxpop), NSC_INT, 0, NULL, EF_BAD, 0, CA_DUMP},
@@ -496,7 +499,7 @@ struct castr news_ca[] = {
     {"action", fldoff(nws_vrb), NSC_UCHAR, 0, NULL, EF_NEWS_CHR, 0,
      CA_DUMP},
     {"victim", fldoff(nws_vno), NSC_NATID, 0, NULL, EF_NATION, 0, CA_DUMP},
-    {"times", fldoff(nws_ntm), NSC_CHAR, 0, NULL, EF_BAD, 0, CA_DUMP},
+    {"times", fldoff(nws_ntm), NSC_USHORT, 0, NULL, EF_BAD, 0, CA_DUMP},
     {"duration", fldoff(nws_duration), NSC_SHORT, 0, NULL, EF_BAD, 0,
      CA_DUMP},
     {"time", fldoff(nws_when), NSC_TIME, 0, NULL, EF_BAD, 0, CA_DUMP},
@@ -599,7 +602,6 @@ struct castr cou_ca[] = {
      EF_BAD, NSC_DEITY, CA_DUMP_NONE},
     {"ip", fldoff(nat_hostaddr), NSC_STRINGY, 46, NULL, EF_BAD, 0,
      CA_DUMP},
-    {"hostname", 0, NSC_STRINGY, 0, NULL, EF_BAD, 0, CA_DUMP}, /* deprecated */
     {"userid", fldoff(nat_userid), NSC_STRINGY, 32, NULL, EF_BAD, 0,
      CA_DUMP},
     {"xcap", fldoff(nat_xcap), NSC_XCOORD, 0, NULL, EF_BAD, 0, CA_DUMP},
@@ -634,20 +636,47 @@ struct castr cou_ca[] = {
      EF_BAD, 0, CA_DUMP},
     {"happiness", fldoff(nat_level[NAT_HLEV]), NSC_FLOAT, 0, NULL,
      EF_BAD, 0, CA_DUMP},
-    {"relations", fldoff(nat_relate), NSC_UCHAR, MAXNOC, NULL,
-     EF_NATION_RELATIONS, NSC_HIDDEN, CA_DUMP_NONE},
-    /* mortals know there's contact (relations show), but not how strong */
-    {"contacts", fldoff(nat_contact), NSC_UCHAR, MAXNOC, NULL,
-     EF_BAD, NSC_DEITY, CA_DUMP_NONE},
-    {"rejects", fldoff(nat_rejects), NSC_UCHAR, MAXNOC, NULL,
-     EF_NATION_REJECTS, NSC_BITS, CA_DUMP_NONE},
     {NULL, 0, NSC_NOTYPE, 0, NULL, EF_BAD, 0, CA_DUMP}
 #undef CURSTR
 };
 
-struct castr nat_ca[sizeof(cou_ca) / sizeof(*cou_ca)];
+struct castr nat_ca[ARRAY_SIZE(cou_ca)];
 /* initialized by nsc_init() */
 
+struct castr relat_ca[] = {
+#define CURSTR struct relatstr
+    {"uid", fldoff(rel_uid), NSC_INT, 0, NULL, EF_RELAT, 0, CA_DUMP},
+    {"timestamp", fldoff(rel_timestamp), NSC_TIME, 0, NULL,
+     EF_BAD, 0, CA_DUMP_NONE},
+    {"relations", fldoff(rel_relate), NSC_UCHAR, MAXNOC, NULL,
+     EF_NATION_RELATIONS, NSC_HIDDEN, CA_DUMP},
+    {NULL, 0, NSC_NOTYPE, 0, NULL, EF_BAD, 0, CA_DUMP}
+#undef CURSTR
+};
+
+struct castr contact_ca[] = {
+#define CURSTR struct contactstr
+    {"uid", fldoff(con_uid), NSC_INT, 0, NULL, EF_CONTACT, 0, CA_DUMP},
+    {"timestamp", fldoff(con_timestamp), NSC_TIME, 0, NULL,
+     EF_BAD, 0, CA_DUMP_NONE},
+    /* mortals know there's contact (relations show), but not how strong */
+    {"contacts", fldoff(con_contact), NSC_UCHAR, MAXNOC, NULL,
+     EF_BAD, NSC_DEITY, CA_DUMP},
+    {NULL, 0, NSC_NOTYPE, 0, NULL, EF_BAD, 0, CA_DUMP}
+#undef CURSTR
+};
+
+struct castr reject_ca[] = {
+#define CURSTR struct rejectstr
+    {"uid", fldoff(rej_uid), NSC_INT, 0, NULL, EF_REJECT, 0, CA_DUMP},
+    {"timestamp", fldoff(rej_timestamp), NSC_TIME, 0, NULL,
+     EF_BAD, 0, CA_DUMP_NONE},
+    {"rejects", fldoff(rej_rejects), NSC_UCHAR, MAXNOC, NULL,
+     EF_NATION_REJECTS, NSC_BITS, CA_DUMP},
+    {NULL, 0, NSC_NOTYPE, 0, NULL, EF_BAD, 0, CA_DUMP}
+#undef CURSTR
+};
+
 struct castr realm_ca[] = {
 #define CURSTR struct realmstr
     /* uid is encoded in cnum, realm */
@@ -686,10 +715,9 @@ struct castr intrchr_ca[] = {
     /* no need for uid as long as it's not referenced from other tables */
     {"name", fldoff(in_name), NSC_STRING, 0, NULL, EF_BAD, 0,
      CA_DUMP_CONST},
-    {"lcms", fldoff(in_lcms), NSC_UCHAR, 0, NULL, EF_BAD, 0, CA_DUMP},
-    {"hcms", fldoff(in_hcms), NSC_UCHAR, 0, NULL, EF_BAD, 0, CA_DUMP},
-    {"dcost", fldoff(in_dcost), NSC_UCHAR, 0, NULL, EF_BAD, 0, CA_DUMP},
-    {"mcost", fldoff(in_mcost), NSC_UCHAR, 0, NULL, EF_BAD, 0, CA_DUMP},
+    NSC_MVEC(fldoff(in_mat), CA_DUMP_ONLY, CA_DUMP_ONLY, CA_DUMP_ONLY),
+    {"bmobil", fldoff(in_bmobil), NSC_SHORT, 0, NULL, EF_BAD, 0, CA_DUMP},
+    {"cost", fldoff(in_cost), NSC_INT, 0, NULL, EF_BAD, 0, CA_DUMP},
     {"enable", fldoff(in_enable), NSC_UCHAR, 0, NULL, EF_BAD, 0, CA_DUMP},
     {NULL, 0, NSC_NOTYPE, 0, NULL, EF_BAD, 0, CA_DUMP}
 #undef CURSTR
@@ -724,7 +752,7 @@ struct castr empfile_ca[] = {
 struct castr symbol_ca[] = {
 #define CURSTR struct symbol
     /*
-     * value is is const because it has to match what is compiled into
+     * value is const because it has to match what is compiled into
      * the server.  name is const because clients key on it.
      */
     {"value", fldoff(value), NSC_INT, 0, NULL, EF_BAD, 0, CA_DUMP_CONST},
@@ -739,11 +767,12 @@ struct castr mdchr_ca[] = {
     /* name must come first, clients may rely on it */
     {"name", fldoff(ca_name), NSC_STRING, 0, NULL, EF_BAD, 0,
      CA_DUMP_CONST},
-    {"type", fldoff(ca_type), NSC_SITYPE(enum nsc_type), 0, NULL,
+    {"type", fldoff(ca_type), NSC_LONG, 0, nsc_meta_type,
      EF_META_TYPE, 0, CA_DUMP_CONST},
     {"flags", fldoff(ca_flags), NSC_INT, 0, NULL,
      EF_META_FLAGS, NSC_BITS, CA_DUMP_CONST},
-    {"len", fldoff(ca_len), NSC_USHORT, 0, NULL, EF_BAD, 0, CA_DUMP_CONST},
+    {"len", fldoff(ca_len), NSC_LONG, 0, nsc_meta_len,
+     EF_BAD, 0, CA_DUMP_CONST},
     {"table", fldoff(ca_table), NSC_INT, 0, NULL, EF_TABLE, 0,
      CA_DUMP_CONST},
     {NULL, 0, NSC_NOTYPE, 0, NULL, EF_BAD, 0, CA_DUMP}
@@ -754,8 +783,7 @@ void
 nsc_init(void)
 {
     static struct castr version_ca0 = {
-       "version", 0, NSC_STRINGY, sizeof(PACKAGE_STRING), NULL, EF_BAD, 0,
-           CA_DUMP
+       "version", 0, NSC_STRING, 0, nsc_ver_version, EF_BAD, 0, CA_DUMP
     };
     static struct castr version_ca1 = {
        "maxnoc", 0, NSC_LONG, 0, nsc_ver_maxnoc, EF_BAD, 0, CA_DUMP
@@ -819,6 +847,14 @@ nsc_ver_maxnoc(struct valstr *val, struct natstr *np, void *ptr)
     return NULL;
 }
 
+static void *
+nsc_ver_version(struct valstr *val, struct natstr *np, void *ptr)
+{
+    val->val_as.str.base = version;
+    val->val_as.str.maxsz = INT_MAX; /* really SIZE_MAX, but that's C99 */
+    return NULL;
+}
+
 static void *
 nsc_sct_terr(struct valstr *val, struct natstr *np, void *ptr)
 {
@@ -979,3 +1015,19 @@ nsc_nws_timestamp(struct valstr *val, struct natstr *natp, void *ptr)
     val->val_as.lng = np->nws_when + np->nws_duration;
     return NULL;
 }
+
+static void *
+nsc_meta_type(struct valstr *val, struct natstr *natp, void *ptr)
+{
+    val->val_as.lng = nstr_promote(((struct castr *)ptr)->ca_type);
+    return NULL;
+}
+
+static void *
+nsc_meta_len(struct valstr *val, struct natstr *natp, void *ptr)
+{
+    struct castr *ca = ptr;
+
+    val->val_as.lng = ca->ca_type == NSC_STRINGY ? 0 : ca->ca_len;
+    return NULL;
+}