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