]> git.pond.sub.org Git - empserver/blob - src/lib/commands/improve.c
Update copyright notice
[empserver] / src / lib / commands / improve.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2014, 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
37 int
38 improve(void)
39 {
40     struct sctstr sect;
41     int nsect;
42     struct nstr_sect nstr;
43     char *p;
44     char buf[1024];
45     char prompt[128];
46     int type;
47     int value;
48     int ovalue;
49     int maxup;
50     struct natstr *natp;
51     int lneeded;
52     int hneeded;
53     int mneeded;
54     int dneeded;
55     int wanted;
56
57     p = getstarg(player->argp[1],
58                  "Improve what ('road', 'rail' or 'defense')? ",
59                  buf);
60     if (!p || !*p)
61         return RET_SYN;
62     if (!strncmp(p, "ro", 2))
63         type = INT_ROAD;
64     else if (!strncmp(p, "ra", 2))
65         type = INT_RAIL;
66     else if (!strncmp(p, "de", 2)) {
67         type = INT_DEF;
68     } else
69         return RET_SYN;
70
71     if (!intrchr[type].in_enable) {
72         pr("%s improvement is disabled.\n", intrchr[type].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                 intrchr[type].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         lneeded = intrchr[type].in_lcms * maxup;
106         if (sect.sct_item[I_LCM] < lneeded) {
107             lneeded = sect.sct_item[I_LCM];
108             maxup = lneeded / intrchr[type].in_lcms;
109             if (maxup <= 0) {
110                 pr("Not enough lcms in %s\n",
111                    xyas(sect.sct_x, sect.sct_y, player->cnum));
112                 continue;
113             }
114         }
115         hneeded = intrchr[type].in_hcms * maxup;
116         if (sect.sct_item[I_HCM] < hneeded) {
117             hneeded = sect.sct_item[I_HCM];
118             maxup = hneeded / intrchr[type].in_hcms;
119             if (maxup <= 0) {
120                 pr("Not enough hcms in %s\n",
121                    xyas(sect.sct_x, sect.sct_y, player->cnum));
122                 continue;
123             }
124         }
125         mneeded = intrchr[type].in_mcost * maxup;
126         if ((sect.sct_mobil - 1) < mneeded) {
127             mneeded = sect.sct_mobil - 1;
128             if (mneeded < 0)
129                 mneeded = 0;
130             maxup = mneeded / intrchr[type].in_mcost;
131             if (maxup <= 0) {
132                 pr("Not enough mobility in %s\n",
133                    xyas(sect.sct_x, sect.sct_y, player->cnum));
134                 continue;
135             }
136         }
137         dneeded = intrchr[type].in_dcost * maxup;
138         natp = getnatp(player->cnum);
139         if (player->dolcost + dneeded > natp->nat_money) {
140             pr("Not enough money left to improve %s by %d%%\n",
141                xyas(sect.sct_x, sect.sct_y, player->cnum), maxup);
142             break;
143         }
144         lneeded = intrchr[type].in_lcms * maxup;
145         hneeded = intrchr[type].in_hcms * maxup;
146         mneeded = intrchr[type].in_mcost * maxup;
147         dneeded = intrchr[type].in_dcost * maxup;
148         player->dolcost += dneeded;
149         sect.sct_item[I_LCM] -= lneeded;
150         sect.sct_item[I_HCM] -= hneeded;
151         sect.sct_mobil -= mneeded;
152         ovalue = value;
153         value += maxup;
154         if (CANT_HAPPEN(value > 100))
155             value = 100;
156         pr("Sector %s %s increased from %d%% to %d%%\n",
157            xyas(sect.sct_x, sect.sct_y, player->cnum),
158            intrchr[type].in_name, ovalue, value);
159         if (type == INT_ROAD)
160             sect.sct_road = value;
161         else if (type == INT_RAIL)
162             sect.sct_rail = value;
163         else if (type == INT_DEF)
164             sect.sct_defense = value;
165         putsect(&sect);
166         nsect++;
167     }
168     if (nsect == 0) {
169         if (player->argp[2])
170             pr("%s: No sector(s)\n", player->argp[1]);
171         else
172             pr("%s: No sector(s)\n", "");
173         return RET_FAIL;
174     } else
175         pr("%d sector%s\n", nsect, splur(nsect));
176     return 0;
177 }