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