]> git.pond.sub.org Git - empserver/commitdiff
Use the new Empire clock for generating BTUs:
authorMarkus Armbruster <armbru@pond.sub.org>
Sat, 14 Jul 2007 16:30:12 +0000 (16:30 +0000)
committerMarkus Armbruster <armbru@pond.sub.org>
Sat, 14 Jul 2007 16:30:12 +0000 (16:30 +0000)
(natstr): New member nat_access.
(cou_ca): New selector access.
(grant_btus, accrued_btus): New.
(prod_nat, init_nat): Use grant_btus().  BTUs are now made at the
update in addition to login, because that lets us get away with a
simple ETU stamp (nat_access).
(nat_cap): Replaced by grant_btus(), remove.

include/nat.h
src/lib/common/btu.c [new file with mode: 0644]
src/lib/global/nsc.c
src/lib/player/init_nats.c
src/lib/update/nat.c

index 370e422fe0a41f301aa765e9113d30d1ca280492..4adf6add33165b2bda3659d881eedea631b790be 100644 (file)
@@ -95,6 +95,7 @@ struct natstr {
     unsigned short nat_ann;    /* # of annos pending */
     unsigned short nat_minused;        /* number of minutes used today */
     short nat_btu;             /* bureaucratic time units */
+    short nat_access;          /* The tick when BTUs were last updated */
     long nat_reserve;          /* military reserves */
     long nat_money;            /* moola */
     time_t nat_last_login;     /* time of last login, 0 menas never */
@@ -178,6 +179,8 @@ extern void putcontact(struct natstr *np, natid them, int contact);
 extern void agecontact(struct natstr *np);
 extern int influx(struct natstr *np);
 
+extern int grant_btus(struct natstr *, int );
+
 /* nation flags */
 #define NF_INFORM      bit(0)  /* Inform me of telegrams right away */
 #define NF_FLASH       bit(1)  /* Allow other players to flash me (sicko :) */
diff --git a/src/lib/common/btu.c b/src/lib/common/btu.c
new file mode 100644 (file)
index 0000000..ebf9368
--- /dev/null
@@ -0,0 +1,97 @@
+/*
+ *  Empire - A multi-player, client/server Internet based war game.
+ *  Copyright (C) 1986-2007, 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.
+ *
+ *  ---
+ *
+ *  btu.c: Dealing with BTUs
+ * 
+ *  Known contributors to this file:
+ *     Markus Armbruster, 2007
+ */
+
+#include <config.h>
+
+#include "file.h"
+#include "nat.h"
+#include "optlist.h"
+#include "prototypes.h"
+#include "sect.h"
+
+/*
+ * Return BTUs produced by CAP in ETU ETUs.
+ */
+static int
+accrued_btus(struct sctstr *cap, int etu)
+{
+    double eff, civ;
+
+    switch (cap->sct_type) {
+    case SCT_CAPIT:
+    case SCT_SANCT:
+       eff = cap->sct_effic;
+       break;
+    case SCT_MOUNT:
+       eff = 0;
+       break;
+    default:
+       return 0;
+    }
+
+    eff *= cap->sct_work / 100.0;
+    if (eff < 0.5)
+       eff = 0.5;
+
+    civ = cap->sct_item[I_CIVIL];
+    if (civ > 999)
+       civ = 999;
+
+    return roundavg(etu * civ * eff * btu_build_rate);
+}
+
+/*
+ * Grant nation NP the BTUs produced by its capital in ETU ETUs.
+ * Return whether it has a capital.
+ */
+int
+grant_btus(struct natstr *np, int etu)
+{
+    int has_cap, delta;
+    struct sctstr sect;
+
+    getsect(np->nat_xcap, np->nat_ycap, &sect);
+    has_cap = np->nat_stat >= STAT_ACTIVE && !influx(np);
+
+    if (has_cap) {
+       delta = accrued_btus(&sect, etu);
+       if (delta + np->nat_btu > max_btus)
+           np->nat_btu = max_btus;
+       else
+           np->nat_btu += delta;
+    }
+    if (np->nat_stat == STAT_VIS || np->nat_stat == STAT_GOD)
+       np->nat_btu = max_btus;
+
+    return has_cap;
+}
index 77ff582866f6c93ca52484a40e3c60afe8ebc21e..71d252d69b77471732234e92a1a45aa425fa6a45 100644 (file)
@@ -505,6 +505,7 @@ struct castr cou_ca[] = {
     {NSC_USHORT, 0, 0, fldoff(natstr, nat_ann), "ann", EF_BAD},
     {NSC_USHORT, 0, 0, fldoff(natstr, nat_minused), "minused", EF_BAD},
     {NSC_SHORT, 0, 0, fldoff(natstr, nat_btu), "btu", EF_BAD},
+    {NSC_SHORT, 0, 0, fldoff(natstr, nat_access), "access", EF_BAD},
     {NSC_LONG, 0, 0, fldoff(natstr, nat_reserve), "milreserve", EF_BAD},
     {NSC_LONG, 0, 0, fldoff(natstr, nat_money), "money", EF_BAD},
     {NSC_TIME, 0, 0, fldoff(natstr, nat_last_login), "login", EF_BAD},
index dd681a2c72227d8c5403041459b72640320ac990..6fd2c97887aeacb235ed41bd97e721a4baf00487 100644 (file)
  *  Known contributors to this file:
  *     Dave Pare, 1994
  *     Steve McClure, 2000
+ *     Markus Armbruster, 2007
  */
 
 #include <config.h>
 
 #include "file.h"
+#include "game.h"
 #include "misc.h"
 #include "nat.h"
 #include "optlist.h"
@@ -42,8 +44,6 @@
 #include "prototypes.h"
 #include "sect.h"
 
-static int nat_cap(int);
-
 int
 init_nats(void)
 {
@@ -69,51 +69,8 @@ init_nats(void)
        player->nstat |= MONEY;
        player->broke = 0;
     }
-    if (nat_cap(np->nat_btu) < 0)
-       return -1;
-    return 0;
-}
-
-static int
-nat_cap(int btu)
-{
-    struct sctstr sect;
-    struct natstr *np;
-    double d, eff;
-    double civ;
-    int delta;
-
-    np = getnatp(player->cnum);
-    if (!getsect(np->nat_xcap, np->nat_ycap, &sect)) {
-       CANT_HAPPEN("read cap");
-       return -1;
-    }
-    if (np->nat_stat >= STAT_ACTIVE) {
-       if (influx(np))
-           player->nstat &= ~CAP;
-       else
-           player->nstat |= CAP;
-    }
-    delta = 0;
-    if ((player->nstat & CAP) || player->god) {
-       d = (double)(player->curup - np->nat_last_login) / s_p_etu;
-       if (d > 336.0)
-           d = 336.0;
-       civ = sect.sct_item[I_CIVIL];
-       if (civ > 999)
-           civ = 999;
-       eff = sect.sct_effic * sect.sct_work / 100.0;
-       if (eff < 0.5 || sect.sct_type == SCT_MOUNT)
-           eff = 0.5;
-       delta = roundavg(d * civ * eff * btu_build_rate);
-
-       if (delta + btu > max_btus)
-           np->nat_btu = max_btus;
-       else
-           np->nat_btu += delta;
-    }
-    if (np->nat_stat == STAT_VIS)
-       np->nat_btu = max_btus;
+    if (grant_btus(np, game_tick_to_now(&np->nat_access)))
+       player->nstat |= CAP;
     putnat(np);
     return 0;
 }
index 5ff9a93e89454ffacb7c12b55529b58005a50f9a..22a3cf19c26fd8c18b37ad4edf2df39b0d8c169e 100644 (file)
@@ -133,6 +133,8 @@ prod_nat(int etu)
     struct natstr *cnp;
 
     for (n = 0; NULL != (np = getnatp(n)); n++) {
+       grant_btus(np, etu_per_update - np->nat_access);
+       np->nat_access = 0;
        if (np->nat_stat < STAT_ACTIVE)
            continue;
        /*