]> git.pond.sub.org Git - empserver/blobdiff - src/lib/commands/setsect.c
edit &c: Suppress news and bulletins on no-op acts of god
[empserver] / src / lib / commands / setsect.c
index fe0b0ddfbe29ade569b919eceec24c63ba76dd31..686913f256c29f5618a644d30af55918df387436 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2004, 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,
  *  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.
  *
  *  ---
  *
  *  setsect.c: Give resources to a country
- * 
+ *
  *  Known contributors to this file:
  *     David Muir Sharnoff
  *     Steve McClure, 1998
+ *     Markus Armbruster, 2004-2013
  */
 
-#include <stdio.h>
-#include <ctype.h>
-#include "misc.h"
-#include "player.h"
-#include "xy.h"
-#include "sect.h"
-#include "nat.h"
-#include "news.h"
-#include "nsc.h"
-#include "item.h"
-#include "file.h"
+#include <config.h>
+
 #include "commands.h"
+#include "news.h"
 #include "optlist.h"
 
 
@@ -54,326 +46,179 @@ int
 setsector(void)
 {
     struct sctstr sect;
-    s_char *what;
+    char *what;
     int amt, current;
-    s_char *p;
+    char *p;
     struct nstr_sect nstr;
-    s_char buf[1024];
-    s_char char0, char1;
+    char buf[1024];
+    char char0, char1;
 
-    if ((what = getstarg(player->argp[1],
-                        "Give What (iron, gold, oil, uranium, fertility, owner, eff., mob., work, avail., oldown, mines)? ",
-                        buf)) == 0)
+    what = getstarg(player->argp[1],
+                   "Give what (iron, gold, oil, uranium, fertility, owner, eff., mob., work, avail., oldown, mines)? ",
+                   buf);
+    if (!what)
        return RET_SYN;
     char0 = what[0];
     char1 = what[1];
 
-    switch (char0) {
-    case 'i':
-       if (!snxtsct(&nstr, player->argp[2]))
+    if (!snxtsct(&nstr, player->argp[2]))
+       return RET_SYN;
+    while (nxtsct(&nstr, &sect) > 0) {
+       p = getstarg(player->argp[3], "What value : ", buf);
+       if (!p || !*p)
            return RET_SYN;
-       while (nxtsct(&nstr, &sect) > 0) {
-           if (!(p = getstarg(player->argp[3], "What value : ", buf)) ||
-               (*p == '\0'))
-               return RET_SYN;
-           amt = atoi(p);
+       amt = atoi(p);
+       if (!check_sect_ok(&sect))
+           return RET_FAIL;
+       switch (char0) {
+       case 'i':
            current = sect.sct_min;
            current += amt;
-           if (current < 0)
-               current = 0;
-           if (current > 100)
-               current = 100;
+           current = LIMIT_TO(current, 0, 100);
            if (sect.sct_own != 0)
-               resnoise(&sect, 1, "Iron ore content",
-                        (int)sect.sct_min, current);
-           sect.sct_min = (u_char)current;
-           putsect(&sect);
-       }
-       break;
-    case 'g':
-       if (!snxtsct(&nstr, player->argp[2]))
-           return RET_SYN;
-       while (nxtsct(&nstr, &sect) > 0) {
-           if (!(p = getstarg(player->argp[3], "What value : ", buf)) ||
-               (*p == '\0'))
-               return RET_SYN;
-           amt = atoi(p);
+               resnoise(&sect, "Iron ore content", sect.sct_min, current);
+           sect.sct_min = (unsigned char)current;
+           break;
+       case 'g':
            current = sect.sct_gmin;
            current += amt;
-           if (current < 0)
-               current = 0;
-           if (current > 100)
-               current = 100;
+           current = LIMIT_TO(current, 0, 100);
            if (sect.sct_own != 0)
-               resnoise(&sect, 1, "Gold content",
-                        (int)sect.sct_gmin, current);
-           sect.sct_gmin = (u_char)current;
-           putsect(&sect);
-       }
-       break;
-    case 'o':
-       switch (char1) {
-       case 'i':
-           if (!snxtsct(&nstr, player->argp[2]))
-               return RET_SYN;
-           while (nxtsct(&nstr, &sect) > 0) {
-               if (!(p = getstarg(player->argp[3], "What value : ", buf))
-                   || (*p == '\0'))
-                   return RET_SYN;
-               amt = atoi(p);
+               resnoise(&sect, "Gold content", sect.sct_gmin, current);
+           sect.sct_gmin = (unsigned char)current;
+           break;
+       case 'o':
+           switch (char1) {
+           case 'i':
                current = sect.sct_oil;
                current += amt;
-               if (current < 0)
-                   current = 0;
-               if (current > 100)
-                   current = 100;
+               current = LIMIT_TO(current, 0, 100);
                if (sect.sct_own != 0)
-                   resnoise(&sect, 1, "Oil content",
-                            (int)sect.sct_oil, current);
-               sect.sct_oil = (u_char)current;
-               putsect(&sect);
-           }
-           break;
-       case 'w':
-           if (!snxtsct(&nstr, player->argp[2]))
-               return RET_SYN;
-           while (nxtsct(&nstr, &sect) > 0) {
-               if (!(p = getstarg(player->argp[3], "What value : ", buf))
-                   || (*p == '\0'))
-                   return RET_SYN;
-               amt = atoi(p);
+                   resnoise(&sect, "Oil content", sect.sct_oil, current);
+               sect.sct_oil = (unsigned char)current;
+               break;
+           case 'w':
                if ((amt < 0) || (amt > MAXNOC - 1))
                    return RET_SYN;
-               pr("Owner of %s changed from %s (#%d) to %s (#%d).\n",
+               pr("Owner of %s changed from %s to %s.\n",
                   xyas(sect.sct_x, sect.sct_y, player->cnum),
-                  cname(sect.sct_own), sect.sct_own, cname(amt), amt);
+                  prnatid(sect.sct_own), prnatid(amt));
+               if (amt == sect.sct_own)
+                   break;
                if (sect.sct_own) {
-                   wu(player->cnum, sect.sct_own,
-                      "Sector %s lost to deity intervention\n",
-                      xyas(sect.sct_x, sect.sct_y, sect.sct_own));
+                   wu(0, sect.sct_own,
+                      "Sector %s taken from you by an act of %s!\n",
+                      xyas(sect.sct_x, sect.sct_y, sect.sct_own),
+                      cname(player->cnum));
                }
                if (amt)
-                   wu(player->cnum, amt,
-                      "Sector %s gained from deity intervention\n",
-                      xyas(sect.sct_x, sect.sct_y, amt));
-               makelost(EF_SECTOR, sect.sct_own, 0, sect.sct_x,
-                        sect.sct_y);
-               makenotlost(EF_SECTOR, amt, 0, sect.sct_x, sect.sct_y);
-
+                   wu(0, amt,
+                      "Sector %s given to you by an act of %s!\n",
+                      xyas(sect.sct_x, sect.sct_y, amt),
+                      cname(player->cnum));
                sect.sct_own = (natid)amt;
-               putsect(&sect);
-           }
-           break;
-       case 'l':
-           if (!snxtsct(&nstr, player->argp[2]))
-               return RET_SYN;
-           while (nxtsct(&nstr, &sect) > 0) {
-               if (!(p = getstarg(player->argp[3], "What value : ", buf))
-                   || (*p == '\0'))
-                   return RET_SYN;
-               amt = atoi(p);
+               break;
+           case 'l':
                if ((amt < 0) || (amt > MAXNOC - 1))
                    return RET_SYN;
-               pr("Old owner of %s changed from %s (#%d) to %s (#%d).\n",
+               pr("Old owner of %s changed from %s to %s.\n",
                   xyas(sect.sct_x, sect.sct_y, player->cnum),
-                  cname(sect.sct_oldown),
-                  sect.sct_oldown, cname(amt), amt);
+                  prnatid(sect.sct_oldown), prnatid(amt));
                sect.sct_oldown = (natid)amt;
-               putsect(&sect);
+               break;
+           default:
+               pr("huh?\n");
+               return RET_SYN;
            }
            break;
-       default:
-           pr("huh?\n");
-           return RET_SYN;
-       }
-       break;
-    case 'e':
-       if (!snxtsct(&nstr, player->argp[2]))
-           return RET_SYN;
-       while (nxtsct(&nstr, &sect) > 0) {
-           if (!(p = getstarg(player->argp[3], "What value : ", buf)) ||
-               (*p == '\0'))
-               return RET_SYN;
-           amt = atoi(p);
+       case 'e':
            current = sect.sct_effic;
            current += amt;
-           if (current < 0)
-               current = 0;
-           if (current > 100)
-               current = 100;
+           current = LIMIT_TO(current, 0, 100);
            pr("Efficiency in %s changed to %d.\n",
               xyas(sect.sct_x, sect.sct_y, player->cnum), current);
-           sect.sct_effic = (u_char)current;
-           putsect(&sect);
-       }
-       break;
-    case 'm':
-       switch (char1) {
-       case 'i':
-           if (!snxtsct(&nstr, player->argp[2]))
-               return RET_SYN;
-           while (nxtsct(&nstr, &sect) > 0) {
-               if (!(p = getstarg(player->argp[3], "What value : ", buf))
-                   || (*p == '\0'))
-                   return RET_SYN;
-               amt = atoi(p);
+           sect.sct_effic = current;
+           break;
+       case 'm':
+           switch (char1) {
+           case 'i':
                current = sect.sct_mines;
                current += amt;
-               if (current < 0)
-                   current = 0;
-               if (current > MINES_MAX)
-                   current = MINES_MAX;
-               if (sect.sct_own != 0)
-                   resnoise(&sect, 1, "Mines", sect.sct_mines, current);
+               current = LIMIT_TO(current, 0, MINES_MAX);
+               if (sect.sct_own != 0 && sect.sct_own == sect.sct_oldown)
+                   resnoise(&sect, "Mines", sect.sct_mines, current);
                sect.sct_mines = current;
-               putsect(&sect);
-           }
-           break;
-       case 'o':
-           if (!snxtsct(&nstr, player->argp[2]))
-               return RET_SYN;
-           while (nxtsct(&nstr, &sect) > 0) {
-               if (!(p = getstarg(player->argp[3], "What value : ", buf))
-                   || (*p == '\0'))
-                   return RET_SYN;
-               amt = atoi(p);
+               break;
+           case 'o':
                current = sect.sct_mobil;
                current += amt;
-               if (current < -127)
-                   current = -127;
-               if (current > 127)
-                   current = 127;
+               current = LIMIT_TO(current, -127, 127);
                pr("Mobility in %s changed to %d.\n",
                   xyas(sect.sct_x, sect.sct_y, player->cnum), current);
-               sect.sct_mobil = (short)current;
-               putsect(&sect);
+               sect.sct_mobil = current;
+               break;
+           default:
+               pr("huh?\n");
+               return RET_SYN;
            }
            break;
-       default:
-           pr("huh?\n");
-           return RET_SYN;
-       }
-       break;
-    case 'a':
-       if (!snxtsct(&nstr, player->argp[2]))
-           return RET_SYN;
-       while (nxtsct(&nstr, &sect) > 0) {
-           if (!(p = getstarg(player->argp[3], "What value : ", buf)) ||
-               (*p == '\0'))
-               return RET_SYN;
-           amt = atoi(p);
+       case 'a':
            current = sect.sct_avail;
            current += amt;
-           if (current < 0)
-               current = 0;
-           if (current > 9999)
-               current = 9999;
+           current = LIMIT_TO(current, 0, 9999);
            pr("Available in %s changed to %d.\n",
               xyas(sect.sct_x, sect.sct_y, player->cnum), current);
            sect.sct_avail = (short)current;
-           putsect(&sect);
-       }
-       break;
-    case 'w':
-       if (!snxtsct(&nstr, player->argp[2]))
-           return RET_SYN;
-       while (nxtsct(&nstr, &sect) > 0) {
-           if (!(p = getstarg(player->argp[3], "What value : ", buf)) ||
-               (*p == '\0'))
-               return RET_SYN;
-           amt = atoi(p);
+           break;
+       case 'w':
            current = sect.sct_work;
            current += amt;
-           if (current < 0)
-               current = 0;
-           if (current > 100)
-               current = 100;
+           current = LIMIT_TO(current, 0, 100);
            pr("Work in %s changed to %d.\n",
               xyas(sect.sct_x, sect.sct_y, player->cnum), current);
-           sect.sct_work = (u_char)current;
-           putsect(&sect);
-       }
-       break;
-    case 'f':
-       if (!snxtsct(&nstr, player->argp[2]))
-           return RET_SYN;
-       while (nxtsct(&nstr, &sect) > 0) {
-           if (!(p = getstarg(player->argp[3], "What value : ", buf)) ||
-               (*p == '\0'))
-               return RET_SYN;
-           amt = atoi(p);
+           sect.sct_work = (unsigned char)current;
+           break;
+       case 'f':
            current = sect.sct_fertil;
            current += amt;
-           if (current < 0)
-               current = 0;
-           if (current > 120)
-               current = 120;
+           current = LIMIT_TO(current, 0, 100);
            if (sect.sct_own != 0)
-               resnoise(&sect, 1, "Fertility content",
-                        (int)sect.sct_fertil, current);
-           sect.sct_fertil = (u_char)current;
-           putsect(&sect);
-       }
-       break;
-    case 'u':
-       if (!snxtsct(&nstr, player->argp[2]))
-           return RET_SYN;
-       while (nxtsct(&nstr, &sect) > 0) {
-           if (!(p = getstarg(player->argp[3], "What value : ", buf)) ||
-               (*p == '\0'))
-               return RET_SYN;
-           amt = atoi(p);
+               resnoise(&sect, "Fertility content", sect.sct_fertil, current);
+           sect.sct_fertil = (unsigned char)current;
+           break;
+       case 'u':
            current = sect.sct_uran;
            current += amt;
-           if (current < 0)
-               current = 0;
-           if (current > 100)
-               current = 100;
+           current = LIMIT_TO(current, 0, 100);
            if (sect.sct_own != 0)
-               resnoise(&sect, 1, "Uranium content",
-                        (int)sect.sct_uran, current);
-           sect.sct_uran = (u_char)current;
-           putsect(&sect);
+               resnoise(&sect, "Uranium content", sect.sct_uran, current);
+           sect.sct_uran = (unsigned char)current;
+           break;
+       default:
+           pr("huh?\n");
+           return RET_SYN;
        }
-       break;
-    default:
-       pr("huh?\n");
-       return RET_SYN;
+       putsect(&sect);
     }
     return RET_OK;
 }
 
 static void
-resbenefit(natid who, int good)
+resbenefit(natid who, int goodness)
 {
-    if (!opt_GODNEWS)
-       return;
-
-    if (good) {
-       if (who)
-           nreport(player->cnum, N_AIDS, who, 1);
-    } else {
-       if (who)
-           nreport(player->cnum, N_HURTS, who, 1);
-    }
+    if (opt_GODNEWS && who && goodness)
+       nreport(player->cnum, goodness > 0 ? N_AIDS : N_HURTS, who, 1);
 }
 
 void
-resnoise(struct sctstr *sptr, int public_amt, s_char *name, int old,
-        int new)
+resnoise(struct sctstr *sptr, char *name, int old, int new)
 {
-    s_char p[100];
-
-    pr("%s of %s changed from %d to %d%%\n",
+    pr("%s of %s changed from %d to %d\n",
        name, xyas(sptr->sct_x, sptr->sct_y, player->cnum), old, new);
-    if (public_amt)
-       (void)sprintf(p, "changed from %d to %d", old, new);
-    else
-       (void)sprintf(p, "%s", old < new ? "increased" : "decreased");
-    if (sptr->sct_own)
+    if (sptr->sct_own && new != old)
        wu(0, sptr->sct_own,
-          "%s in %s was %s by an act of %s\n",
+          "%s in %s was changed from %d to %d by an act of %s\n",
           name, xyas(sptr->sct_x, sptr->sct_y, sptr->sct_own),
-          p, cname(player->cnum));
-    resbenefit(sptr->sct_own, (old < new));
+          old, new, cname(player->cnum));
+    resbenefit(sptr->sct_own, new - old);
 }