]> git.pond.sub.org Git - empserver/blobdiff - src/lib/subs/actofgod.c
actofgod: New divine_nat_change(), similar to divine_sct_change()
[empserver] / src / lib / subs / actofgod.c
index 215e944a6b80b10e5795023a966e73ec60867047..e564eaf093b00a166d352f795c980e25216743fb 100644 (file)
@@ -40,6 +40,7 @@
 #include "player.h"
 #include "prototypes.h"
 #include "sect.h"
+#include "unit.h"
 
 static void
 nreport_divine_aid(natid whom, int goodness)
@@ -108,6 +109,155 @@ divine_sct_change(struct sctstr *sp, char *name,
     }
 }
 
+/*
+ * Report deity meddling with NP.
+ * Just like divine_sct_change(), only for nations.
+ */
+void
+divine_nat_change(struct natstr *np, char *name,
+                  int change, int goodness, char *fmt, ...)
+{
+    va_list ap;
+    char buf[4096];
+
+    va_start(ap, fmt);
+    vsnprintf(buf, sizeof(buf), fmt, ap);
+    va_end(ap);
+
+    if (!change) {
+       pr("%s of %s unchanged\n", name, prnat(np));
+       return;
+    }
+
+    pr("%s of %s changed %s\n", name, prnat(np), buf);
+    if (change > 0 && np->nat_cnum != player->cnum) {
+       wu(0, np->nat_cnum, "%s changed %s by an act of %s!\n",
+          name, buf, cname(player->cnum));
+       nreport_divine_aid(np->nat_cnum, goodness);
+    }
+}
+
+/*
+ * Report deity meddling with UNIT.
+ * Just like divine_sct_change(), only for ships, planes, land units,
+ * nukes.
+ */
+void
+divine_unit_change(struct empobj *unit, char *name,
+                  int change, int goodness, char *fmt, ...)
+{
+    va_list ap;
+    char buf[4096];
+
+    va_start(ap, fmt);
+    vsnprintf(buf, sizeof(buf), fmt, ap);
+    va_end(ap);
+
+    if (!change) {
+       pr("%s of %s unchanged\n", name, unit_nameof(unit));
+       return;
+    }
+
+    pr("%s of %s changed %s\n", name, unit_nameof(unit), buf);
+    if (change > 0 && unit->own && unit->own != player->cnum) {
+       wu(0, unit->own, "%s of %s changed %s by an act of %s\n",
+          name, unit_nameof(unit), buf, cname(player->cnum));
+       nreport_divine_aid(unit->own, goodness);
+    }
+}
+
+static void
+divine_load_unload(struct empobj *unit, int type, int uid, char *act)
+{
+    if (uid < 0)
+       return;
+
+    pr("%s %s %s #%d\n",
+       unit_nameof(unit), act, ef_nameof(type), uid);
+    if (unit->own && unit->own != player->cnum)
+       wu(0, unit->own,
+          "%s %s %s #%d by an act of %s!\n",
+          unit_nameof(unit), act, ef_nameof(type), uid,
+          cname(player->cnum));
+    /* carrier owner could differ; can't be bothered to report to him */
+}
+
+void
+divine_load(struct empobj *unit, int type, int uid)
+{
+    union empobj_storage carrier;
+
+    divine_load_unload(unit, type, uid, "loaded onto");
+    if (get_empobj(type, uid, &carrier)
+       && (unit->x != carrier.gen.x || unit->y != carrier.gen.y)) {
+       pr("%s teleported from %s to %s!",
+          unit_nameof(unit), xyas(unit->x, unit->y, player->cnum),
+          xyas(carrier.gen.x, carrier.gen.y, player->cnum));
+       unit_teleport(unit, carrier.gen.x, carrier.gen.y);
+    }
+}
+
+void
+divine_unload(struct empobj *unit, int type, int uid)
+{
+    divine_load_unload(unit, type, uid, "unloaded from");
+}
+
+static int
+fmtflags (char *buf, size_t sz, int flags, struct symbol symtab[], int all)
+{
+    char *sep = "";
+    int n, i;
+    char *p;
+
+    if (sz)
+       buf[0] = 0;
+    n = 0;
+    for (i = 0; i < 32; i++) {
+       if (!(flags & bit(i)))
+           continue;
+       p = symbol_by_value(bit(i), symtab);
+       if (p)
+           n += snprintf(buf + n, sz - n, "%s%s", sep, p);
+       else if (all)
+           n += snprintf(buf + n, sz - n, "%s#%d", sep, i);
+       if (CANT_HAPPEN((size_t)n >= sz)) {
+           buf = NULL;
+           sz = n;
+       }
+       sep = ", ";
+    }
+    return n;
+}
+
+void
+divine_flag_change(struct empobj *unit, char *name,
+                  int old, int new, struct symbol sym[])
+{
+    char set[1024], clr[1024];
+
+    if (new == old) {
+       pr("%s of %s unchanged\n", name, unit_nameof(unit));
+       return;
+    }
+
+    fmtflags(set, sizeof(set), new & ~old, sym, 1);
+    fmtflags(clr, sizeof(clr), old & ~new, sym, 1);
+    pr("%s of %s changed: %s%s%s%s%s\n",
+       name, unit_nameof(unit),
+       set, set[0] ? " set" : "",
+       set[0] && clr[0] ? ", and " : "",
+       clr, clr[0] ? " cleared" : "");
+
+    if (fmtflags(set, sizeof(set), new & ~old, sym, 0)
+       + fmtflags(clr, sizeof(clr), old & ~new, sym, 0))
+       wu(0, unit->own, "%s of %s changed by an act of %s: %s%s%s%s%s\n",
+          name, unit_nameof(unit), cname(player->cnum),
+          set, set[0] ? " set" : "",
+          set[0] && clr[0] ? " and " : "",
+          clr, clr[0] ? " cleared" : "");
+}
+
 /*
  * Report deity giving/taking commodities to/from WHOM.
  * Give AMT of IP in PLACE.