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