]> git.pond.sub.org Git - empserver/blobdiff - src/lib/commands/powe.c
Update copyright notice
[empserver] / src / lib / commands / powe.c
index 94c15bbabf9c6e8824bd152cccfa7b9e4f89e7dc..dcb0da8ae279da81c4baf0919cc71277a03077d4 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
- *                           Ken Stevens, Steve McClure
+ *  Copyright (C) 1986-2013, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *                Ken Stevens, Steve McClure, Markus Armbruster
  *
- *  This program is free software; you can redistribute it and/or modify
+ *  Empire 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
+ *  the Free Software Foundation, either version 3 of the License, or
  *  (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
@@ -14,8 +14,7 @@
  *  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
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  *  ---
  *
  *  ---
  *
  *  powe.c: Do a power report
- * 
+ *
  *  Known contributors to this file:
  *     Dave Pare
  *     Ken Stevens, 1995
  *     Steve McClure, 1998-2000
- *     Markus Armbruster, 2006
+ *     Markus Armbruster, 2004-2009
+ *     Ron Koenderink, 2005-2008
  */
 
 #include <config.h>
-
+#include <unistd.h>
 #include <math.h>
-#ifdef _WIN32
-#include <io.h>
-#endif
 
 #include "commands.h"
 #include "item.h"
@@ -51,7 +48,7 @@
 
 static void prpower(char *, struct powstr *, int);
 static void out5(double, int, int);
-static void gen_power(void);
+static void gen_power(struct powstr *, int);
 static int powcmp(const void *, const void *);
 static void addtopow(short *, struct powstr *);
 
@@ -59,40 +56,56 @@ int
 powe(void)
 {
     struct natstr *natp;
+    int i;
     time_t pow_time;
     struct nstr_item ni;
-    struct powstr pow;
-    int num;
+    int save = 1;
+    int num = MAXNOC;
     int power_generated = 0;
     struct natstr nat;
+    struct powstr powbuf[MAXNOC];
     int targets[MAXNOC];
     int use_targets = 0;
     int no_numbers = 0;
 
     memset(targets, 0, sizeof(targets));
-    natp = getnatp(player->cnum);
-    num = MAXNOC;
 
+    i = 1;
     if (player->argp[1]) {
-       if (player->argp[1][0] == 'n') {
+       switch (player->argp[1][0]) {
+       case 'u':
+           if (player->god)
+               save = 0;
+           /* fall through */
+       case 'n':
+           i++;
+           natp = getnatp(player->cnum);
            if (natp->nat_btu < 1)
                pr("\n  Insufficient BTUs, using the last report.\n\n");
+           else if (opt_AUTO_POWER && save)
+               pr("\n  power new is disabled, using the last report.\n\n");
            else {
-               gen_power();
+               gen_power(powbuf, save);
+               pow_time = time(NULL);
                power_generated = 1;
            }
-           if (player->argp[2])
-               num = atoi(player->argp[2]);
-       } else if (player->argp[1][0] == 'c') {
-           snxtitem(&ni, EF_NATION, player->argp[2]);
+       }
+    }
+
+    if (player->argp[i]) {
+       if (player->argp[i][0] == 'c') {
+           if (!snxtitem(&ni, EF_NATION, player->argp[i + 1], NULL))
+               return RET_SYN;
            while (nxtitem(&ni, &nat)) {
-               if (nat.nat_stat != STAT_ACTIVE)
+               if (nat.nat_stat == STAT_UNUSED)
+                   continue;
+               if (!player->god && nat.nat_stat != STAT_ACTIVE)
                    continue;
                targets[nat.nat_cnum] = 1;
            }
            use_targets = 1;
        } else
-           num = atoi(player->argp[1]);
+           num = atoi(player->argp[i]);
     }
 
     if (num < 0) {
@@ -103,39 +116,42 @@ powe(void)
     }
 
     if (!power_generated) {
+       pow_time = ef_mtime(EF_POWER);
        snxtitem_all(&ni, EF_POWER);
-       if (!nxtitem(&ni, &pow)) {
-           pr("Power for this game has not been built yet.  Type 'power new' to build it.\n");
+       if (!nxtitem(&ni, &powbuf[0])) {
+           pr("Power for this game has not been built yet.%s\n",
+              opt_AUTO_POWER ? "" : "  Type 'power new' to build it.");
            return RET_FAIL;
        }
+       for (i = 1; i < MAXNOC; i++) {
+           if (!nxtitem(&ni, &powbuf[i])) {
+               CANT_REACH();
+               memset(&powbuf[i], 0, sizeof(powbuf[i]));
+           }
+       }
     }
 
     pr("     - = [   Empire Power Report   ] = -\n");
-    pow_time = ef_mtime(EF_POWER);
     pr("      as of %s\n         sects  eff civ", ctime(&pow_time));
     pr("  mil  shell gun pet  iron dust oil  pln ship unit money\n");
-    snxtitem_all(&ni, EF_POWER);
-    while ((nxtitem(&ni, &pow)) && num > 0) {
-       if (pow.p_nation == 0)
-           continue;
+    for (i = 1; i < MAXNOC && num > 0; i++) {
        if (opt_HIDDEN) {
-           if (!player->god && pow.p_nation != player->cnum)
+           if (!player->god && powbuf[i].p_nation != player->cnum)
                continue;
        }
-       if (use_targets && !targets[pow.p_nation])
+       if (use_targets && !targets[powbuf[i].p_nation])
            continue;
-       if (!use_targets && pow.p_power <= 0.0)
+       if (!use_targets && powbuf[i].p_power <= 0.0)
            continue;
-       prpower(cname(pow.p_nation), &pow,
-               pow.p_nation != player->cnum && !player->god);
+       prpower(cname(powbuf[i].p_nation), &powbuf[i],
+               powbuf[i].p_nation != player->cnum && !player->god);
        if (player->god && !no_numbers)
-           pr("%9.2f\n", pow.p_power);
+           pr("%9.2f\n", powbuf[i].p_power);
        num--;
     }
     if (!opt_HIDDEN || player->god) {
        pr("          ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----\n");
-       getpower(0, &pow);
-       prpower("worldwide", &pow, !player->god);
+       prpower("worldwide", &powbuf[0], !player->god);
        pr("\n");
     }
     return RET_OK;
@@ -187,8 +203,16 @@ out5(double value, int round_val, int round_flag)
        pr("%4.0fG", value / 1e9);
 }
 
+void
+update_power(void)
+{
+    struct powstr powbuf[MAXNOC];
+
+    gen_power(powbuf, 1);
+}
+
 static void
-gen_power(void)
+gen_power(struct powstr *powbuf, int save)
 {
     float *f_ptr;
     float *f_pt2;
@@ -198,14 +222,13 @@ gen_power(void)
     struct plnstr plane;
     struct shpstr ship;
     struct lndstr land;
-    struct powstr powbuf[MAXNOC];
     struct nstr_item ni;
     struct nstr_sect ns;
     struct natstr *natp;
     float f;
 
     player->btused += 10;
-    memset(powbuf, 0, sizeof(powbuf));
+    memset(powbuf, 0, MAXNOC * sizeof(*powbuf));
     snxtsct_all(&ns);
     while (nxtsct(&ns, &sect)) {
        if (sect.sct_own == 0)
@@ -269,7 +292,7 @@ gen_power(void)
            pow->p_power += pow->p_sects
                * (pow->p_effic / pow->p_sects / 100.0)
                * 10.0;
-       pow->p_power *= MAX(1.0, natp->nat_level[NAT_TLEV] / 500.0);
+       pow->p_power *= MAX(1.0, natp->nat_level[NAT_TLEV]) / 500.0;
        /* ack.  add this vec to the "world power" element */
        f_pt2 = &powbuf[0].p_sects;
        f_ptr = &pow->p_sects;
@@ -290,13 +313,15 @@ gen_power(void)
        }
     }
     qsort(&powbuf[1], MAXNOC - 1, sizeof(*powbuf), powcmp);
+    if (!save)
+       return;
     for (i = 0; i < MAXNOC; i++)
        putpower(i, &powbuf[i]);
 #ifdef _WIN32
     /*
      * At least some versions of Windows fail to update mtime on
-     * write().  Bad, because `power' displays that time.  Attempt to
-     * force an update.
+     * write(), they delay it until the write actually hits the disk.
+     * Bad, because `power' displays that time.  Force it.
      */
     _commit(empfile[EF_POWER].fd);
 #endif