]> git.pond.sub.org Git - empserver/commitdiff
edit: Report flags change properly
authorMarkus Armbruster <armbru@pond.sub.org>
Sun, 3 Feb 2013 14:10:46 +0000 (15:10 +0100)
committerMarkus Armbruster <armbru@pond.sub.org>
Thu, 6 Jun 2013 17:55:02 +0000 (19:55 +0200)
Print a message, send bulletin to owner.  Affects ship key 'W', land
unit key 'W', and plane key 'f'.  The message is necessary to give the
deity a chance to catch unexpected changes, e.g. a player modifying
retreat conditions right before the deity edits them.  Watching out
for such changes is especially important with non-interactive edit.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
include/actofgod.h
include/types.h
src/lib/commands/edit.c
src/lib/subs/actofgod.c
tests/actofgod/journal.log

index 9f991c61a23b82fb40f6188f1277464445ad4e17..69ba33db5fa3b00d077e6c052175e0e91d9c03d7 100644 (file)
@@ -35,6 +35,7 @@
 
 #include "item.h"
 #include "nat.h"
+#include "types.h"
 
 extern void report_god_takes(char *, char *, natid);
 extern void report_god_gives(char *, char *, natid);
@@ -48,6 +49,8 @@ extern void divine_unit_change(struct empobj *, char *, int, int, char *, ...)
     ATTRIBUTE((format (printf, 5, 6)));
 #define divine_unit_change_quiet(unit, name, change, ...) \
     divine_unit_change((unit), (name), -(change), 0, __VA_ARGS__)
+extern void divine_flag_change(struct empobj *, char *, int, int,
+                              struct symbol *);
 extern void report_divine_gift(natid, struct ichrstr *, int, char *);
 
 #endif
index 44f8753d5e1fc39262582d3ccb2b74caf37cf528..3253c14ba2f47998282b9749c8a02652b6aa9f04 100644 (file)
@@ -27,7 +27,7 @@
  *  types.h: Empire types
  *
  *  Known contributors to this file:
- *     Markus Armbruster, 2006-2011
+ *     Markus Armbruster, 2006-2013
  */
 
 #ifndef TYPES_H
@@ -56,6 +56,7 @@ struct range;
 struct sctstr;
 struct shiplist;
 struct shpstr;
+struct symbol;
 struct trdstr;
 struct trtstr;
 struct comstr;
index 5cda12de8fbd63c6fa258750a018a3452fe30b35..847b9c759d4a339a13cce4755e95409b6c1b78f9 100644 (file)
@@ -844,6 +844,8 @@ edit_ship(struct shpstr *ship, char *key, char *p)
        strncpy(ship->shp_rpath, p, sizeof(ship->shp_rpath) - 1);
        break;
     case 'W':
+       divine_flag_change((struct empobj *)ship, "Retreat conditions",
+                          ship->shp_rflags, arg, retreat_flags);
        ship->shp_rflags = arg;
        break;
     case 'c':
@@ -938,6 +940,8 @@ edit_land(struct lndstr *land, char *key, char *p)
        strncpy(land->lnd_rpath, p, sizeof(land->lnd_rpath) - 1);
        break;
     case 'W':
+       divine_flag_change((struct empobj *)land, "Retreat condition",
+                          land->lnd_rflags, arg, retreat_flags);
        land->lnd_rflags = arg;
        break;
     case 'c':
@@ -1025,6 +1029,8 @@ edit_plane(struct plnstr *plane, char *key, char *p)
        plane->pln_land = arg;
        break;
     case 'f':
+       divine_flag_change((struct empobj *)plane, "Flags",
+                          plane->pln_flags, arg, plane_flags);
        plane->pln_flags = arg;
        break;
     default:
index 7146577169b48f361bbb1a6376daca187806b8d2..06054ee0a36fc5468f54b255245ce6bfaaeaa21b 100644 (file)
@@ -175,6 +175,61 @@ 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.
index a49766c7ddefa17175401f0bb1772a0935820fa4..8732311c4d73ceaa1a797645ba7ed9204998e7b7 100644 (file)
     Play#0 output Play#0 6 0 640
     Play#0 input edit s 2 W 0 W 1
     Play#0 command edit
+    Play#0 output Play#0 1 Retreat conditions of cs   cargo ship (#2) unchanged
+    Play#0 output Play#0 1 Retreat conditions of cs   cargo ship (#2) changed: group set
     Play#0 output Play#0 6 0 640
     Play#0 input edit s 3 W 513 W 1030 W 2
     Play#0 command edit
+    Play#0 output Play#0 1 Retreat conditions of cs   cargo ship (#3) changed: group, #9 set
+    Play#0 output Play#0 1 Retreat conditions of cs   cargo ship (#3) changed: injured, torped, #10 set, and group, #9 cleared
+    Play#0 output Play#0 1 Retreat conditions of cs   cargo ship (#3) changed: torped, #10 cleared
     Play#0 output Play#0 6 0 640
     Play#0 input edit u 2 R ""
     Play#0 command edit
     Play#0 output Play#0 6 0 640
     Play#0 input edit u 2 W 0 W 1
     Play#0 command edit
+    Play#0 output Play#0 1 Retreat condition of sup  supply #2 unchanged
+    Play#0 output Play#0 1 Retreat condition of sup  supply #2 changed: group set
     Play#0 output Play#0 6 0 640
     Play#0 input edit u 3 W 513 W 1030 W 2
     Play#0 command edit
+    Play#0 output Play#0 1 Retreat condition of sup  supply #3 changed: group, #9 set
+    Play#0 output Play#0 1 Retreat condition of sup  supply #3 changed: injured, torped, #10 set, and group, #9 cleared
+    Play#0 output Play#0 1 Retreat condition of sup  supply #3 changed: torped, #10 cleared
     Play#0 output Play#0 6 0 640
     Play#0 input edit p 2 f 4
     Play#0 command edit
+    Play#0 output Play#0 1 Flags of f1   Sopwith Camel #2 changed: airburst set
     Play#0 output Play#0 6 0 640
     Play#0 input edit p 2 s -1 s 9999
     Play#0 command edit
     Play#0 output Play#0 1 cs   cargo ship (#2) given to you by an act of POGO!
     Play#0 output Play#0 1 f1   Sopwith Camel #2 given to you by an act of POGO!
     Play#0 output Play#0 1 sup  supply #2 given to you by an act of POGO!
+    Play#0 output Play#0 1 Retreat conditions of cs   cargo ship (#2) changed by an act of POGO: group set
+    Play#0 output Play#0 1 Retreat condition of sup  supply #2 changed by an act of POGO: group set
+    Play#0 output Play#0 1 Flags of f1   Sopwith Camel #2 changed by an act of POGO: airburst set
     Play#0 output Play#0 6 0 640
     Play#0 input read 3
     Play#0 command read
     Play#0 output Play#0 1 POGO gave you 50 military in cs   cargo ship (#5)
     Play#0 output Play#0 1 POGO gave you 25 military in sup  supply #4
     Play#0 output Play#0 1 POGO gave you 25 military in sup  supply #5
+    Play#0 output Play#0 1 Retreat conditions of cs   cargo ship (#3) changed by an act of POGO: group set
+    Play#0 output Play#0 1 Retreat conditions of cs   cargo ship (#3) changed by an act of POGO: injured, torped set and group cleared
+    Play#0 output Play#0 1 Retreat conditions of cs   cargo ship (#3) changed by an act of POGO: torped cleared
+    Play#0 output Play#0 1 Retreat condition of sup  supply #3 changed by an act of POGO: group set
+    Play#0 output Play#0 1 Retreat condition of sup  supply #3 changed by an act of POGO: injured, torped set and group cleared
+    Play#0 output Play#0 1 Retreat condition of sup  supply #3 changed by an act of POGO: torped cleared
     Play#0 output Play#0 1 f1   Sopwith Camel #3 loaded onto ship #3 by an act of POGO!
     Play#0 output Play#0 1 f1   Sopwith Camel #4 loaded onto ship #4 by an act of POGO!
     Play#0 output Play#0 1 f1   Sopwith Camel #4 unloaded from ship #4 by an act of POGO!