]> git.pond.sub.org Git - empserver/commitdiff
Factor common code out of add() and new() into nat_reset()
authorMarkus Armbruster <armbru@pond.sub.org>
Wed, 5 Mar 2008 21:21:08 +0000 (22:21 +0100)
committerMarkus Armbruster <armbru@pond.sub.org>
Fri, 14 Mar 2008 19:25:43 +0000 (20:25 +0100)
The common nation wipe code is not quite identical, and it doesn't
wipe the nation thoroughly enough.  The new code does.

Changes to both commands:

* Wipe nat_update, nat_ann, nat_access, and nat_contact.  Bug: should
  set nat_ann to the number of announcements.

Changes to add command:

* Don't wipe for status active and god.  Before, nat_relate and
  nat_flags where wiped then.

Changes to newcap command:

* Wipe nat_hostaddr, nat_hostname, nat_userid, nat_dayno, nat_minused,
  nat_reserve, nat_last_login, nat_last_logout, nat_newstim,
  nat_annotim, nat_relate, nat_rejects, nat_flags.

include/nat.h
src/lib/commands/add.c
src/lib/commands/new.c
src/lib/subs/natsub.c [new file with mode: 0644]

index 828b3dd5d4f8b826fbf89dd8eba765e161fef422..58421b5b7f4e2d2031e4ed887300487fdec9243d 100644 (file)
@@ -174,6 +174,8 @@ extern void putcontact(struct natstr *np, natid them, int contact);
 extern void agecontact(struct natstr *np);
 extern int influx(struct natstr *np);
 
+extern struct natstr *nat_reset(struct natstr *, enum nat_status, coord, coord);
+
 extern int grant_btus(struct natstr *, int );
 
 /* nation flags */
index 20d8180e40a55e881f13baa965c19af6e64d4e9e..819afda1881f62e42ea841d5e42737969fb87c94 100644 (file)
@@ -60,8 +60,6 @@ add(void)
     int stat;
     struct nstr_item ni;
     struct lndstr land;
-    struct realmstr realm;
-    time_t current_time = time(NULL);
 
     for (freecn = 0; NULL != (natp = getnatp(freecn)); freecn++) {
        if (natp->nat_stat == STAT_UNUSED)
@@ -137,7 +135,6 @@ add(void)
            putland(land.lnd_uid, &land);
        }
     }
-    natp->nat_stat = stat;
     strcpy(natp->nat_cnam, cntryname);
     strcpy(natp->nat_pnam, pname);
     if (*p != 'w' && *p != 'c') {
@@ -183,43 +180,10 @@ add(void)
        }
     }
 
-    if (natp->nat_stat == STAT_NEW || natp->nat_stat == STAT_VIS) {
-       *natp->nat_hostaddr = '\0';
-       *natp->nat_hostname = '\0';
-       *natp->nat_userid = '\0';
-       natp->nat_btu = 0;
-       natp->nat_reserve = 0;
-       natp->nat_tgms = 0;
-       natp->nat_ycap = 0;
-       natp->nat_xcap = 0;
-       natp->nat_yorg = 0;
-       natp->nat_xorg = 0;
-       natp->nat_dayno = 0;
-       natp->nat_minused = 0;
-       for (i = 0; i < MAXNOR; i++) {
-           getrealm(i, coun, &realm);
-           realm.r_xl = realm.r_xh = realm.r_yl = realm.r_yh = 0;
-           realm.r_timestamp = current_time;
-           putrealm(&realm);
-       }
-       natp->nat_last_login = natp->nat_last_login = 0;
-       natp->nat_money = 0;
-       natp->nat_level[NAT_TLEV] = start_technology;
-       natp->nat_level[NAT_RLEV] = start_research;
-       natp->nat_level[NAT_ELEV] = start_education;
-       natp->nat_level[NAT_HLEV] = start_happiness;
-       for (i = 0; i < MAXNOC; i++)
-           natp->nat_rejects[i] = 0;
-       natp->nat_newstim = 0;
-       natp->nat_annotim = 0;
-       close(creat(mailbox(buf, coun), S_IRWUG));
-    } else
+    if (stat == STAT_NEW || stat == STAT_VIS)
+       nat_reset(natp, stat, 0, 0);
+    else
        pr("No special initializations done...\n");
-
-    natp->nat_flags =
-       NF_FLASH | NF_BEEP | NF_COASTWATCH | NF_SONAR | NF_TECHLISTS;
-    for (i = 0; i < MAXNOC; i++)
-       natp->nat_relate[i] = NEUTRAL;
     putnat(natp);
     return 0;
 }
index aef15eb3057bfc19de14a2ef7d27f9d0db2b2193..4b6365b89824998afe99c45831c9dafb53f65ae0 100644 (file)
@@ -47,8 +47,6 @@ static int isok(int x, int y);
 static void ok(signed char *map, int x, int y);
 static void init_sanct(struct natstr *, coord, coord);
 
-static struct range defrealm = { -8, -5, 10, 5, 0, 0 };
-
 #define        MAXAVAIL        300
 
 int
@@ -56,14 +54,11 @@ new(void)
 {
     struct sctstr sect;
     struct natstr *natp;
-    struct realmstr newrealm;
-    struct range absrealm;
     natid num;
     coord x, y;
     int i;
     char *p;
     char buf[1024];
-    time_t current_time = time(NULL);
 
     natp = getnatp(player->cnum);
     if (natp->nat_xorg != 0 || natp->nat_yorg != 0) {
@@ -122,33 +117,7 @@ new(void)
     if (player->aborted)
        return RET_FAIL;
     pr("added country %d at %s\n", num, xyas(x, y, player->cnum));
-    natp->nat_btu = max_btus;
-    natp->nat_stat = STAT_SANCT;
-    natp->nat_xcap = x;
-    natp->nat_ycap = y;
-    natp->nat_xorg = x;
-    natp->nat_yorg = y;
-    xyabsrange(natp, &defrealm, &absrealm);
-    for (i = 0; i < MAXNOR; i++) {
-       getrealm(i, num, &newrealm);
-       newrealm.r_xl = absrealm.lx;
-       newrealm.r_xh = absrealm.hx;
-       newrealm.r_yl = absrealm.ly;
-       newrealm.r_yh = absrealm.hy;
-       newrealm.r_timestamp = current_time;
-       putrealm(&newrealm);
-    }
-    if (players_at_00) {
-       natp->nat_xorg = 0;
-       natp->nat_yorg = 0;
-    }
-    natp->nat_money = start_cash;
-    natp->nat_level[NAT_HLEV] = start_happiness;
-    natp->nat_level[NAT_RLEV] = start_research;
-    natp->nat_level[NAT_TLEV] = start_technology;
-    natp->nat_level[NAT_ELEV] = start_education;
-    natp->nat_tgms = 0;
-    close(creat(mailbox(buf, num), S_IRWUG));
+    nat_reset(natp, STAT_SANCT, x, y);
     init_sanct(natp, x, y);
     init_sanct(natp, x + 2, y);
     putnat(natp);
diff --git a/src/lib/subs/natsub.c b/src/lib/subs/natsub.c
new file mode 100644 (file)
index 0000000..3d5ef1c
--- /dev/null
@@ -0,0 +1,115 @@
+/*
+ *  Empire - A multi-player, client/server Internet based war game.
+ *  Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *                           Ken Stevens, Steve McClure
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ *  ---
+ *
+ *  See files README, COPYING and CREDITS in the root of the source
+ *  tree for related information and legal notices.  It is expected
+ *  that future projects/authors will amend these files as needed.
+ *
+ *  ---
+ *
+ *  nat.c: Nation subroutines
+ * 
+ *  Known contributors to this file:
+ *     Markus Armbruster, 2008
+ */
+
+#include <config.h>
+
+#include <string.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include "file.h"
+#include "game.h"
+#include "nat.h"
+#include "optlist.h"
+#include "tel.h"
+#include "xy.h"
+
+/*
+ * Reset NATP for status STAT with origin/capital at X,Y.
+ * Wipes everything but nat_cnum, nat_cnam and nat_pnam.
+ * Also wipes realms and telegrams.
+ */
+struct natstr *
+nat_reset(struct natstr *natp, enum nat_status stat, coord x, coord y)
+{
+    static struct range defrealm = { -8, -5, 10, 5, 0, 0 };
+    struct realmstr newrealm;
+    struct range absrealm;
+    char buf[1024];
+    time_t now = time(NULL);
+    int i;
+
+    natp->nat_stat = stat;
+    *natp->nat_hostaddr = '\0';
+    *natp->nat_hostname = '\0';
+    *natp->nat_userid = '\0';
+
+    natp->nat_xcap = natp->nat_xorg = x;
+    natp->nat_ycap = natp->nat_yorg = y;
+    if (stat == STAT_SANCT)
+       xyabsrange(natp, &defrealm, &absrealm);
+    else
+       memset(&absrealm, 0, sizeof(absrealm));
+    for (i = 0; i < MAXNOR; i++) {
+       ef_blank(EF_REALM, i, &newrealm);
+       newrealm.r_xl = absrealm.lx;
+       newrealm.r_xh = absrealm.hx;
+       newrealm.r_yl = absrealm.ly;
+       newrealm.r_yh = absrealm.hy;
+       newrealm.r_timestamp = now;
+       putrealm(&newrealm);
+    }
+    if (players_at_00) {
+       natp->nat_xorg = 0;
+       natp->nat_yorg = 0;
+    }
+
+    natp->nat_dayno = 0;
+    natp->nat_minused = 0;
+    natp->nat_update = 0;
+
+    natp->nat_tgms = 0;
+    close(creat(mailbox(buf, natp->nat_cnum), S_IRWUG));
+    natp->nat_ann = 0;         /* FIXME number of annos */
+
+    natp->nat_btu = stat == STAT_SANCT ? max_btus : 0;
+    natp->nat_access = 0;
+    game_tick_to_now(&natp->nat_access);
+    natp->nat_reserve = 0;
+    natp->nat_money = stat == STAT_SANCT ? start_cash : 0;
+    natp->nat_last_login = natp->nat_last_login = 0;
+    natp->nat_newstim = 0;
+    natp->nat_annotim = 0;
+    natp->nat_level[NAT_HLEV] = start_happiness;
+    natp->nat_level[NAT_RLEV] = start_research;
+    natp->nat_level[NAT_TLEV] = start_technology;
+    natp->nat_level[NAT_ELEV] = start_education;
+    for (i = 0; i < MAXNOC; i++)
+       natp->nat_relate[i] = NEUTRAL;
+    memset(natp->nat_contact, 0, sizeof(natp->nat_rejects));
+    memset(natp->nat_rejects, 0, sizeof(natp->nat_rejects));
+    natp->nat_flags =
+       NF_FLASH | NF_BEEP | NF_COASTWATCH | NF_SONAR | NF_TECHLISTS;
+
+    return natp;
+}