]> git.pond.sub.org Git - empserver/blob - src/lib/commands/improve.c
72f0ba8095b9cb73994e5e19f80e6999a0ba3a25
[empserver] / src / lib / commands / improve.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2021, 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  *  improve.c: Improve the infrastructure of a sector
28  *
29  *  Known contributors to this file:
30  *     Steve McClure, 1996-2000
31  *     Markus Armbruster, 2004-2016
32  */
33
34 #include <config.h>
35
36 #include "chance.h"
37 #include "commands.h"
38
39 int
40 improve(void)
41 {
42     struct sctintrins *incp;
43     struct sctstr sect;
44     int nsect;
45     struct nstr_sect nstr;
46     char *p;
47     char buf[1024];
48     char prompt[128];
49     int type;
50     int value;
51     int ovalue;
52     int maxup, i, lim;
53     struct natstr *natp;
54     int wanted;
55
56     p = getstarg(player->argp[1],
57                  "Improve what ('road', 'rail' or 'defense')? ",
58                  buf);
59     if (!p || !*p)
60         return RET_SYN;
61     if (!strncmp(p, "ro", 2))
62         type = INT_ROAD;
63     else if (!strncmp(p, "ra", 2))
64         type = INT_RAIL;
65     else if (!strncmp(p, "de", 2)) {
66         type = INT_DEF;
67     } else
68         return RET_SYN;
69
70     incp = &intrchr[type];
71     if (!incp->in_enable) {
72         pr("%s improvement is disabled.\n", incp->in_name);
73         return RET_FAIL;
74     }
75
76     if (!snxtsct(&nstr, player->argp[2]))
77         return RET_SYN;
78     prdate();
79     nsect = 0;
80     while (nxtsct(&nstr, &sect)) {
81         if (!player->owner)
82             continue;
83         if (type == INT_ROAD)
84             value = sect.sct_road;
85         else if (type == INT_RAIL)
86             value = sect.sct_rail;
87         else /* type == INT_DEF */
88             value = sect.sct_defense;
89         sprintf(prompt, "Sector %s has a %s of %d%%.  Improve how much? ",
90                 xyas(sect.sct_x, sect.sct_y, player->cnum),
91                 incp->in_name, value);
92         p = getstarg(player->argp[3], prompt, buf);
93         if (!p || !*p)
94             continue;
95         if (!check_sect_ok(&sect))
96             continue;
97         maxup = 100 - value;
98         wanted = atoi(p);
99         if (wanted < 0)
100             continue;
101         if (wanted < maxup)
102             maxup = wanted;
103         if (!maxup)
104             continue;
105
106         lim = (sect. sct_mobil - 1) * 100 / incp->in_bmobil;
107         if (lim <= 0) {
108             pr("Not enough mobility in %s\n",
109                xyas(sect.sct_x, sect.sct_y, player->cnum));
110             continue;
111         }
112         if (maxup > lim)
113             maxup = lim;
114
115         for (i = I_NONE + 1; i <= I_MAX; i++) {
116             if (!incp->in_mat[i])
117                 continue;
118             lim = sect.sct_item[i] * 100 / incp->in_mat[i];
119             if (lim <= 0) {
120                 pr("Not enough %s in %s\n",
121                    ichr[i].i_name,
122                    xyas(sect.sct_x, sect.sct_y, player->cnum));
123             }
124             if (maxup > lim)
125                 maxup = lim;
126         }
127         if (maxup <= 0)
128             continue;
129
130         natp = getnatp(player->cnum);
131         lim = (natp->nat_money - player->dolcost) * 100 / incp->in_cost;
132         if (lim <= 0) {
133             pr("Not enough money left to improve %s by %d%%\n",
134                xyas(sect.sct_x, sect.sct_y, player->cnum), maxup);
135             break;
136         }
137         if (maxup > lim)
138             maxup = lim;
139
140         for (i = I_NONE + 1; i <= I_MAX; i++)
141             sect.sct_item[i] -= roundavg(maxup * incp->in_mat[i] / 100.0);
142         sect.sct_mobil -= roundavg(maxup * incp->in_bmobil / 100.0);
143         player->dolcost += maxup * incp->in_cost / 100.0;
144         ovalue = value;
145         value += maxup;
146         if (CANT_HAPPEN(value > 100))
147             value = 100;
148         pr("Sector %s %s increased from %d%% to %d%%\n",
149            xyas(sect.sct_x, sect.sct_y, player->cnum),
150            incp->in_name, ovalue, value);
151         if (type == INT_ROAD)
152             sect.sct_road = value;
153         else if (type == INT_RAIL)
154             sect.sct_rail = value;
155         else if (type == INT_DEF)
156             sect.sct_defense = value;
157         putsect(&sect);
158         nsect++;
159     }
160     if (nsect == 0) {
161         if (player->argp[2])
162             pr("%s: No sector(s)\n", player->argp[1]);
163         else
164             pr("%s: No sector(s)\n", "");
165         return RET_FAIL;
166     } else
167         pr("%d sector%s\n", nsect, splur(nsect));
168     return 0;
169 }