]> git.pond.sub.org Git - empserver/blobdiff - src/lib/commands/strv.c
Update copyright notice
[empserver] / src / lib / commands / strv.c
index d473fdc8f17a98776eecc78e129d79c807f982aa..f39eedb53f1864871129e6fc7228dccee0de2975 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
- *                           Ken Stevens, Steve McClure
+ *  Copyright (C) 1986-2021, 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,
  *  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/>.
  *
  *  ---
  *
- *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
- *  related information and legal notices. It is expected that any future
- *  projects/authors will amend these files as needed.
+ *  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.
  *
  *  ---
  *
  *  star.c: Do a starvation report
- * 
+ *
  *  Known contributors to this file:
- *     
+ *
  */
 
-#include "misc.h"
-#include "player.h"
-#include "var.h"
-#include "xy.h"
-#include "sect.h"
-#include "nsc.h"
-#include "nat.h"
-#include "deity.h"
-#include "path.h"
-#include "file.h"
+#include <config.h>
+
 #include <math.h>
+#include "commands.h"
 #include "item.h"
-#include "ship.h"
-#include "optlist.h"
 #include "land.h"
-#include "commands.h"
+#include "optlist.h"
+#include "ship.h"
+#include "update.h"
 
-extern int etu_per_update;
-static void starv_sects(s_char *range);
-static void starv_ships(s_char *range);
-static void starv_units(s_char *range);
+static void starv_sects(char *range);
+static void starv_ships(char *range);
+static void starv_units(char *range);
 static void sect_hdr(void);
 static void ship_hdr(void);
 static void unit_hdr(void);
@@ -59,33 +50,30 @@ static void unit_hdr(void);
 int
 starve(void)
 {
-    struct nstr_sect nstr;
     int do_sects = 0;
     int do_ships = 0;
     int do_units = 0;
-    s_char range[1024];
+    char *range;
 
     if (opt_NOFOOD) {          /* no food - no work! */
        pr("No food is required in this game\n");
        return RET_OK;
     }
 
-    strcpy(range, "*");
+    range = "*";
     if (!player->argp[1]) {
        do_sects = do_ships = do_units = 1;
     } else if (*(player->argp[1]) == 's') {
        do_ships = 1;
        if (player->argp[2])
-           strcpy(range, player->argp[2]);
+           range = player->argp[2];
     } else if (*(player->argp[1]) == 'l') {
        do_units = 1;
        if (player->argp[2])
-           strcpy(range, player->argp[2]);
+           range = player->argp[2];
     } else {
-       if (!snxtsct(&nstr, player->argp[1]))
-           return RET_SYN;
        do_sects = 1;
-       strcpy(range, player->argp[1]);
+       range = player->argp[1];
     }
     player->simulation = 1;
     prdate();
@@ -100,12 +88,19 @@ starve(void)
 }
 
 static void
-starv_sects(s_char *range)
+starv_people(short *vec, int victims)
+{
+    pr(" will starve %d people. %.0f more food needed\n", victims,
+       ceil(food_needed(vec, etu_per_update) - vec[I_FOOD]));
+}
+
+static void
+starv_sects(char *range)
 {
     struct nstr_sect nstr;
     struct sctstr sect;
     int nsect = 0;
-    int vec[I_MAX + 1], s, needed;
+    int s, save;
 
     if (!snxtsct(&nstr, range))
        return;
@@ -115,27 +110,33 @@ starv_sects(s_char *range)
        if (sect.sct_type == SCT_SANCT)
            continue;
 
-       getvec(VT_ITEM, vec, (s_char *)&sect, EF_SECTOR);
-/* This next 2 lines were added to overcompensate for the needy */
-       if (vec[I_FOOD])
-           vec[I_FOOD]--;
-       s = feed_people(vec, etu_per_update, &needed);
-
+       /*
+        * Check for starvation.  Suppress complaints about tiny
+        * population without food by adding 1f just for the check.
+        * That's okay because growfood() will grow at least that much
+        * anyway.
+        */
+       save = sect.sct_item[I_FOOD];
+       if (sect.sct_item[I_FOOD] == 0)
+           sect.sct_item[I_FOOD] = 1;
+       s = famine_victims(sect.sct_item, etu_per_update);
+       sect.sct_item[I_FOOD] = save;
        if (s == 0)
            continue;
+
        if (nsect++ == 0)
            sect_hdr();
        if (player->god)
            pr("%3d ", sect.sct_own);
-       prxy("%4d,%-4d", nstr.x, nstr.y, player->cnum);
+       prxy("%4d,%-4d", nstr.x, nstr.y);
        pr(" %c", dchr[sect.sct_type].d_mnem);
-       pr(" %c", (sect.sct_own != sect.sct_oldown ? '*' : ' '));
+       pr(" %c", sect.sct_own != sect.sct_oldown ? '*' : ' ');
        if (sect.sct_newtype != sect.sct_type)
            pr("%c", dchr[sect.sct_newtype].d_mnem);
        else
            pr(" ");
        pr("%4d%%", sect.sct_effic);
-       pr(" will starve %d people. %d more food needed\n", s, needed);
+       starv_people(sect.sct_item, s);
     }
     if (nsect == 0) {
        if (player->argp[1])
@@ -153,7 +154,7 @@ sect_hdr(void)
 {
     if (player->god)
        pr("    ");
-    pr("Starvation               \n");
+    pr("Starvation\n");
     if (player->god)
        pr("own ");
     pr("  sect         eff ");
@@ -161,23 +162,21 @@ sect_hdr(void)
 }
 
 static void
-starv_ships(s_char *range)
+starv_ships(char *range)
 {
     struct nstr_item ni;
     struct shpstr ship;
     int nship = 0;
-    int vec[I_MAX + 1], s, needed;
+    int s;
 
-    if (!snxtitem(&ni, EF_SHIP, range))
+    if (!snxtitem(&ni, EF_SHIP, range, NULL))
        return;
 
-    while (nxtitem(&ni, (s_char *)&ship)) {
+    while (nxtitem(&ni, &ship)) {
        if (!player->owner || !ship.shp_own)
            continue;
 
-       getvec(VT_ITEM, vec, (s_char *)&ship, EF_SHIP);
-       s = feed_ship(&ship, vec, etu_per_update, &needed, 0);
-
+       s = famine_victims(ship.shp_item, etu_per_update);
        if (s == 0)
            continue;
        if (nship++ == 0)
@@ -186,7 +185,7 @@ starv_ships(s_char *range)
            pr("%3d ", ship.shp_own);
        pr("%5d ", ship.shp_uid);
        pr("%-16.16s ", mchr[(int)ship.shp_type].m_name);
-       pr(" will starve %d people. %d more food needed\n", s, needed);
+       starv_people(ship.shp_item, s);
     }
     if (nship == 0) {
        if (range)
@@ -204,30 +203,28 @@ ship_hdr(void)
 {
     if (player->god)
        pr("    ");
-    pr("Starvation               \n");
+    pr("Starvation\n");
     if (player->god)
        pr("own ");
-    pr(" shp#     ship type       \n");
+    pr(" shp#     ship type\n");
 }
 
 static void
-starv_units(s_char *range)
+starv_units(char *range)
 {
     struct nstr_item ni;
     struct lndstr land;
     int nunit = 0;
-    int vec[I_MAX + 1], s, needed;
+    int s;
 
-    if (!snxtitem(&ni, EF_LAND, range))
+    if (!snxtitem(&ni, EF_LAND, range, NULL))
        return;
 
-    while (nxtitem(&ni, (s_char *)&land)) {
+    while (nxtitem(&ni, &land)) {
        if (!player->owner || !land.lnd_own)
            continue;
 
-       getvec(VT_ITEM, vec, (s_char *)&land, EF_LAND);
-       s = feed_land(&land, vec, etu_per_update, &needed, 0);
-
+       s = famine_victims(land.lnd_item, etu_per_update);
        if (s == 0)
            continue;
        if (nunit++ == 0)
@@ -236,7 +233,7 @@ starv_units(s_char *range)
            pr("%3d ", land.lnd_own);
        pr("%5d ", land.lnd_uid);
        pr("%-16.16s ", lchr[(int)land.lnd_type].l_name);
-       pr(" will starve %d mil. %d more food needed\n", s, needed);
+       starv_people(land.lnd_item, s);
     }
     if (nunit == 0) {
        if (range)
@@ -254,8 +251,8 @@ unit_hdr(void)
 {
     if (player->god)
        pr("    ");
-    pr("Starvation               \n");
+    pr("Starvation\n");
     if (player->god)
        pr("own ");
-    pr(" lnd#     unit type       \n");
+    pr(" lnd#     unit type\n");
 }