]> 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-2010, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program 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 2 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, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  improve.c: Improve the infrastructure of a sector
29  *
30  *  Known contributors to this file:
31  *     Steve McClure, 1996-2000
32  */
33
34 #include <config.h>
35
36 #include "commands.h"
37 #include "optlist.h"
38 #include "path.h"
39
40 int
41 improve(void)
42 {
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;
53     struct natstr *natp;
54     int lneeded;
55     int hneeded;
56     int mneeded;
57     int dneeded;
58     int wanted;
59
60     p = getstarg(player->argp[1],
61                  "Improve what ('road', 'rail' or 'defense')? ",
62                  buf);
63     if (!p || !*p)
64         return RET_SYN;
65     if (!strncmp(p, "ro", 2))
66         type = INT_ROAD;
67     else if (!strncmp(p, "ra", 2))
68         type = INT_RAIL;
69     else if (!strncmp(p, "de", 2)) {
70         type = INT_DEF;
71     } else
72         return RET_SYN;
73
74     if (!intrchr[type].in_enable) {
75         pr("%s improvement is disabled.\n", intrchr[type].in_name);
76         return RET_FAIL;
77     }
78
79     if (!snxtsct(&nstr, player->argp[2]))
80         return RET_SYN;
81     prdate();
82     nsect = 0;
83     while (nxtsct(&nstr, &sect)) {
84         if (!player->owner)
85             continue;
86         if (type == INT_ROAD)
87             value = sect.sct_road;
88         else if (type == INT_RAIL)
89             value = sect.sct_rail;
90         else /* type == INT_DEF */
91             value = sect.sct_defense;
92         sprintf(prompt, "Sector %s has a %s of %d%%.  Improve how much? ",
93                 xyas(sect.sct_x, sect.sct_y, player->cnum),
94                 intrchr[type].in_name, value);
95         p = getstarg(player->argp[3], prompt, buf);
96         if (!p || !*p)
97             continue;
98         if (!check_sect_ok(&sect))
99             continue;
100         maxup = 100 - value;
101         wanted = atoi(p);
102         if (wanted < 0)
103             continue;
104         if (wanted < maxup)
105             maxup = wanted;
106         if (!maxup)
107             continue;
108         lneeded = intrchr[type].in_lcms * maxup;
109         if (sect.sct_item[I_LCM] < lneeded) {
110             lneeded = sect.sct_item[I_LCM];
111             maxup = lneeded / intrchr[type].in_lcms;
112             if (maxup <= 0) {
113                 pr("Not enough lcms in %s\n",
114                    xyas(sect.sct_x, sect.sct_y, player->cnum));
115                 continue;
116             }
117         }
118         hneeded = intrchr[type].in_hcms * maxup;
119         if (sect.sct_item[I_HCM] < hneeded) {
120             hneeded = sect.sct_item[I_HCM];
121             maxup = hneeded / intrchr[type].in_hcms;
122             if (maxup <= 0) {
123                 pr("Not enough hcms in %s\n",
124                    xyas(sect.sct_x, sect.sct_y, player->cnum));
125                 continue;
126             }
127         }
128         mneeded = intrchr[type].in_mcost * maxup;
129         if ((sect.sct_mobil - 1) < mneeded) {
130             mneeded = sect.sct_mobil - 1;
131             if (mneeded < 0)
132                 mneeded = 0;
133             maxup = mneeded / intrchr[type].in_mcost;
134             if (maxup <= 0) {
135                 pr("Not enough mobility in %s\n",
136                    xyas(sect.sct_x, sect.sct_y, player->cnum));
137                 continue;
138             }
139         }
140         dneeded = intrchr[type].in_dcost * maxup;
141         natp = getnatp(player->cnum);
142         if (player->dolcost + dneeded > natp->nat_money) {
143             pr("Not enough money left to improve %s by %d%%\n",
144                xyas(sect.sct_x, sect.sct_y, player->cnum), maxup);
145             break;
146         }
147         lneeded = intrchr[type].in_lcms * maxup;
148         hneeded = intrchr[type].in_hcms * maxup;
149         mneeded = intrchr[type].in_mcost * maxup;
150         dneeded = intrchr[type].in_dcost * maxup;
151         player->dolcost += dneeded;
152         sect.sct_item[I_LCM] -= lneeded;
153         sect.sct_item[I_HCM] -= hneeded;
154         sect.sct_mobil -= mneeded;
155         ovalue = value;
156         value += maxup;
157         if (CANT_HAPPEN(value > 100))
158             value = 100;
159         pr("Sector %s %s increased from %d%% to %d%%\n",
160            xyas(sect.sct_x, sect.sct_y, player->cnum),
161            intrchr[type].in_name, ovalue, value);
162         if (type == INT_ROAD)
163             sect.sct_road = value;
164         else if (type == INT_RAIL)
165             sect.sct_rail = value;
166         else if (type == INT_DEF)
167             sect.sct_defense = value;
168         putsect(&sect);
169         nsect++;
170     }
171     if (nsect == 0) {
172         if (player->argp[2])
173             pr("%s: No sector(s)\n", player->argp[1]);
174         else
175             pr("%s: No sector(s)\n", "");
176         return RET_FAIL;
177     } else
178         pr("%d sector%s\n", nsect, splur(nsect));
179     return 0;
180 }