]> git.pond.sub.org Git - empserver/blob - src/lib/commands/setsect.c
05397967186f9ad07b52728a3c1f1f54594eda48
[empserver] / src / lib / commands / setsect.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2011, 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  */
33
34 #include <config.h>
35
36 #include "commands.h"
37 #include "lost.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         check_sect_ok(&sect);
72         switch (char0) {
73         case 'i':
74             current = sect.sct_min;
75             current += amt;
76             if (current < 0)
77                 current = 0;
78             if (current > 100)
79                 current = 100;
80             if (sect.sct_own != 0)
81                 resnoise(&sect, 1, "Iron ore content",
82                          (int)sect.sct_min, current);
83             sect.sct_min = (unsigned char)current;
84             break;
85         case 'g':
86             current = sect.sct_gmin;
87             current += amt;
88             if (current < 0)
89                 current = 0;
90             if (current > 100)
91                 current = 100;
92             if (sect.sct_own != 0)
93                 resnoise(&sect, 1, "Gold content",
94                          (int)sect.sct_gmin, current);
95             sect.sct_gmin = (unsigned char)current;
96             break;
97         case 'o':
98             switch (char1) {
99             case 'i':
100                 current = sect.sct_oil;
101                 current += amt;
102                 if (current < 0)
103                     current = 0;
104                 if (current > 100)
105                     current = 100;
106                 if (sect.sct_own != 0)
107                     resnoise(&sect, 1, "Oil content",
108                              (int)sect.sct_oil, current);
109                 sect.sct_oil = (unsigned char)current;
110                 break;
111             case 'w':
112                 if ((amt < 0) || (amt > MAXNOC - 1))
113                     return RET_SYN;
114                 pr("Owner of %s changed from %s (#%d) to %s (#%d).\n",
115                    xyas(sect.sct_x, sect.sct_y, player->cnum),
116                    cname(sect.sct_own), sect.sct_own, cname(amt), amt);
117                 if (sect.sct_own) {
118                     wu(player->cnum, sect.sct_own,
119                        "Sector %s lost to deity intervention\n",
120                        xyas(sect.sct_x, sect.sct_y, sect.sct_own));
121                 }
122                 if (amt)
123                     wu(player->cnum, amt,
124                        "Sector %s gained from deity intervention\n",
125                        xyas(sect.sct_x, sect.sct_y, amt));
126                 sect.sct_own = (natid)amt;
127                 break;
128             case 'l':
129                 if ((amt < 0) || (amt > MAXNOC - 1))
130                     return RET_SYN;
131                 pr("Old owner of %s changed from %s (#%d) to %s (#%d).\n",
132                    xyas(sect.sct_x, sect.sct_y, player->cnum),
133                    cname(sect.sct_oldown),
134                    sect.sct_oldown, cname(amt), amt);
135                 sect.sct_oldown = (natid)amt;
136                 break;
137             default:
138                 pr("huh?\n");
139                 return RET_SYN;
140             }
141             break;
142         case 'e':
143             current = sect.sct_effic;
144             current += amt;
145             if (current < 0)
146                 current = 0;
147             if (current > 100)
148                 current = 100;
149             pr("Efficiency in %s changed to %d.\n",
150                xyas(sect.sct_x, sect.sct_y, player->cnum), current);
151             sect.sct_effic = current;
152             break;
153         case 'm':
154             switch (char1) {
155             case 'i':
156                 current = sect.sct_mines;
157                 current += amt;
158                 if (current < 0)
159                     current = 0;
160                 if (current > MINES_MAX)
161                     current = MINES_MAX;
162                 if (sect.sct_own != 0)
163                     resnoise(&sect, 1, "Mines", sect.sct_mines, current);
164                 sect.sct_mines = current;
165                 break;
166             case 'o':
167                 current = sect.sct_mobil;
168                 current += amt;
169                 if (current < -127)
170                     current = -127;
171                 if (current > 127)
172                     current = 127;
173                 pr("Mobility in %s changed to %d.\n",
174                    xyas(sect.sct_x, sect.sct_y, player->cnum), current);
175                 sect.sct_mobil = current;
176                 break;
177             default:
178                 pr("huh?\n");
179                 return RET_SYN;
180             }
181             break;
182         case 'a':
183             current = sect.sct_avail;
184             current += amt;
185             if (current < 0)
186                 current = 0;
187             if (current > 9999)
188                 current = 9999;
189             pr("Available in %s changed to %d.\n",
190                xyas(sect.sct_x, sect.sct_y, player->cnum), current);
191             sect.sct_avail = (short)current;
192             break;
193         case 'w':
194             current = sect.sct_work;
195             current += amt;
196             if (current < 0)
197                 current = 0;
198             if (current > 100)
199                 current = 100;
200             pr("Work in %s changed to %d.\n",
201                xyas(sect.sct_x, sect.sct_y, player->cnum), current);
202             sect.sct_work = (unsigned char)current;
203             break;
204         case 'f':
205             current = sect.sct_fertil;
206             current += amt;
207             if (current < 0)
208                 current = 0;
209             if (current > 100)
210                 current = 100;
211             if (sect.sct_own != 0)
212                 resnoise(&sect, 1, "Fertility content",
213                          (int)sect.sct_fertil, current);
214             sect.sct_fertil = (unsigned char)current;
215             break;
216         case 'u':
217             current = sect.sct_uran;
218             current += amt;
219             if (current < 0)
220                 current = 0;
221             if (current > 100)
222                 current = 100;
223             if (sect.sct_own != 0)
224                 resnoise(&sect, 1, "Uranium content",
225                          (int)sect.sct_uran, current);
226             sect.sct_uran = (unsigned char)current;
227             break;
228         default:
229             pr("huh?\n");
230             return RET_SYN;
231         }
232         putsect(&sect);
233     }
234     return RET_OK;
235 }
236
237 static void
238 resbenefit(natid who, int good)
239 {
240     if (!opt_GODNEWS)
241         return;
242
243     if (good) {
244         if (who)
245             nreport(player->cnum, N_AIDS, who, 1);
246     } else {
247         if (who)
248             nreport(player->cnum, N_HURTS, who, 1);
249     }
250 }
251
252 void
253 resnoise(struct sctstr *sptr, int public_amt, char *name, int old,
254          int new)
255 {
256     char p[100];
257
258     pr("%s of %s changed from %d to %d\n",
259        name, xyas(sptr->sct_x, sptr->sct_y, player->cnum), old, new);
260     if (public_amt)
261         (void)sprintf(p, "changed from %d to %d", old, new);
262     else
263         (void)sprintf(p, "%s", old < new ? "increased" : "decreased");
264     if (sptr->sct_own)
265         wu(0, sptr->sct_own,
266            "%s in %s was %s by an act of %s\n",
267            name, xyas(sptr->sct_x, sptr->sct_y, sptr->sct_own),
268            p, cname(player->cnum));
269     resbenefit(sptr->sct_own, (old < new));
270 }