]> git.pond.sub.org Git - empserver/blob - src/lib/commands/setsect.c
Replace common pattern by new LIMIT_TO()
[empserver] / src / lib / commands / setsect.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2013, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  setsect.c: Give resources to a country
28  *
29  *  Known contributors to this file:
30  *     David Muir Sharnoff
31  *     Steve McClure, 1998
32  *     Markus Armbruster, 2004-2013
33  */
34
35 #include <config.h>
36
37 #include "commands.h"
38 #include "news.h"
39 #include "optlist.h"
40
41
42 /*
43  * format: setres thing <sect> <#>
44  */
45 int
46 setsector(void)
47 {
48     struct sctstr sect;
49     char *what;
50     int amt, current;
51     char *p;
52     struct nstr_sect nstr;
53     char buf[1024];
54     char char0, char1;
55
56     what = getstarg(player->argp[1],
57                     "Give what (iron, gold, oil, uranium, fertility, owner, eff., mob., work, avail., oldown, mines)? ",
58                     buf);
59     if (!what)
60         return RET_SYN;
61     char0 = what[0];
62     char1 = what[1];
63
64     if (!snxtsct(&nstr, player->argp[2]))
65         return RET_SYN;
66     while (nxtsct(&nstr, &sect) > 0) {
67         p = getstarg(player->argp[3], "What value : ", buf);
68         if (!p || !*p)
69             return RET_SYN;
70         amt = atoi(p);
71         if (!check_sect_ok(&sect))
72             return RET_FAIL;
73         switch (char0) {
74         case 'i':
75             current = sect.sct_min;
76             current += amt;
77             current = LIMIT_TO(current, 0, 100);
78             if (sect.sct_own != 0)
79                 resnoise(&sect, "Iron ore content", sect.sct_min, current);
80             sect.sct_min = (unsigned char)current;
81             break;
82         case 'g':
83             current = sect.sct_gmin;
84             current += amt;
85             current = LIMIT_TO(current, 0, 100);
86             if (sect.sct_own != 0)
87                 resnoise(&sect, "Gold content", sect.sct_gmin, current);
88             sect.sct_gmin = (unsigned char)current;
89             break;
90         case 'o':
91             switch (char1) {
92             case 'i':
93                 current = sect.sct_oil;
94                 current += amt;
95                 current = LIMIT_TO(current, 0, 100);
96                 if (sect.sct_own != 0)
97                     resnoise(&sect, "Oil content", sect.sct_oil, current);
98                 sect.sct_oil = (unsigned char)current;
99                 break;
100             case 'w':
101                 if ((amt < 0) || (amt > MAXNOC - 1))
102                     return RET_SYN;
103                 pr("Owner of %s changed from %s (#%d) to %s (#%d).\n",
104                    xyas(sect.sct_x, sect.sct_y, player->cnum),
105                    cname(sect.sct_own), sect.sct_own, cname(amt), amt);
106                 if (sect.sct_own) {
107                     wu(player->cnum, sect.sct_own,
108                        "Sector %s lost to deity intervention\n",
109                        xyas(sect.sct_x, sect.sct_y, sect.sct_own));
110                 }
111                 if (amt)
112                     wu(player->cnum, amt,
113                        "Sector %s gained from deity intervention\n",
114                        xyas(sect.sct_x, sect.sct_y, amt));
115                 sect.sct_own = (natid)amt;
116                 break;
117             case 'l':
118                 if ((amt < 0) || (amt > MAXNOC - 1))
119                     return RET_SYN;
120                 pr("Old owner of %s changed from %s (#%d) to %s (#%d).\n",
121                    xyas(sect.sct_x, sect.sct_y, player->cnum),
122                    cname(sect.sct_oldown),
123                    sect.sct_oldown, cname(amt), amt);
124                 sect.sct_oldown = (natid)amt;
125                 break;
126             default:
127                 pr("huh?\n");
128                 return RET_SYN;
129             }
130             break;
131         case 'e':
132             current = sect.sct_effic;
133             current += amt;
134             current = LIMIT_TO(current, 0, 100);
135             pr("Efficiency in %s changed to %d.\n",
136                xyas(sect.sct_x, sect.sct_y, player->cnum), current);
137             sect.sct_effic = current;
138             break;
139         case 'm':
140             switch (char1) {
141             case 'i':
142                 current = sect.sct_mines;
143                 current += amt;
144                 current = LIMIT_TO(current, 0, MINES_MAX);
145                 if (sect.sct_own != 0 && sect.sct_own == sect.sct_oldown)
146                     resnoise(&sect, "Mines", sect.sct_mines, current);
147                 sect.sct_mines = current;
148                 break;
149             case 'o':
150                 current = sect.sct_mobil;
151                 current += amt;
152                 current = LIMIT_TO(current, -127, 127);
153                 pr("Mobility in %s changed to %d.\n",
154                    xyas(sect.sct_x, sect.sct_y, player->cnum), current);
155                 sect.sct_mobil = current;
156                 break;
157             default:
158                 pr("huh?\n");
159                 return RET_SYN;
160             }
161             break;
162         case 'a':
163             current = sect.sct_avail;
164             current += amt;
165             current = LIMIT_TO(current, 0, 9999);
166             pr("Available in %s changed to %d.\n",
167                xyas(sect.sct_x, sect.sct_y, player->cnum), current);
168             sect.sct_avail = (short)current;
169             break;
170         case 'w':
171             current = sect.sct_work;
172             current += amt;
173             current = LIMIT_TO(current, 0, 100);
174             pr("Work in %s changed to %d.\n",
175                xyas(sect.sct_x, sect.sct_y, player->cnum), current);
176             sect.sct_work = (unsigned char)current;
177             break;
178         case 'f':
179             current = sect.sct_fertil;
180             current += amt;
181             current = LIMIT_TO(current, 0, 100);
182             if (sect.sct_own != 0)
183                 resnoise(&sect, "Fertility content", sect.sct_fertil, current);
184             sect.sct_fertil = (unsigned char)current;
185             break;
186         case 'u':
187             current = sect.sct_uran;
188             current += amt;
189             current = LIMIT_TO(current, 0, 100);
190             if (sect.sct_own != 0)
191                 resnoise(&sect, "Uranium content", sect.sct_uran, current);
192             sect.sct_uran = (unsigned char)current;
193             break;
194         default:
195             pr("huh?\n");
196             return RET_SYN;
197         }
198         putsect(&sect);
199     }
200     return RET_OK;
201 }
202
203 static void
204 resbenefit(natid who, int good)
205 {
206     if (!opt_GODNEWS)
207         return;
208
209     if (good) {
210         if (who)
211             nreport(player->cnum, N_AIDS, who, 1);
212     } else {
213         if (who)
214             nreport(player->cnum, N_HURTS, who, 1);
215     }
216 }
217
218 void
219 resnoise(struct sctstr *sptr, char *name, int old, int new)
220 {
221     pr("%s of %s changed from %d to %d\n",
222        name, xyas(sptr->sct_x, sptr->sct_y, player->cnum), old, new);
223     if (sptr->sct_own)
224         wu(0, sptr->sct_own,
225            "%s in %s was changed from %d to %d by an act of %s\n",
226            name, xyas(sptr->sct_x, sptr->sct_y, sptr->sct_own),
227            old, new, cname(player->cnum));
228     resbenefit(sptr->sct_own, (old < new));
229 }