]> 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-2004, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  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 <string.h>
35 #include "misc.h"
36 #include "player.h"
37 #include "var.h"
38 #include "xy.h"
39 #include "sect.h"
40 #include "nsc.h"
41 #include "nat.h"
42 #include "path.h"
43 #include "file.h"
44 #include "optlist.h"
45 #include "commands.h"
46
47 char *prompt[] = {
48     "Improve what ('road' or 'rail')? ",
49     "Improve what ('road', 'rail' or 'defense')? "
50 };
51
52 int
53 improve(void)
54 {
55     struct sctstr sect;
56     int nsect;
57     struct nstr_sect nstr;
58     s_char *p;
59     s_char buf[1024];
60     s_char inbuf[128];
61     int type;
62     int value;
63     int ovalue;
64     int maxup;
65     struct natstr *natp;
66     int lneeded;
67     int hneeded;
68     int mneeded;
69     int dneeded;
70     int wanted;
71
72     if (!(p = getstarg(player->argp[1],
73                        prompt[opt_DEFENSE_INFRA], buf)) || !*p)
74         return RET_SYN;
75     if (!strncmp(p, "ro", 2))
76         type = INT_ROAD;
77     else if (!strncmp(p, "ra", 2))
78         type = INT_RAIL;
79     else if (!strncmp(p, "de", 2)) {
80         if (!opt_DEFENSE_INFRA) {
81             pr("Defense infrastructure is disabled.\n");
82             return RET_FAIL;
83         }
84         type = INT_DEF;
85     } else
86         return RET_SYN;
87     if (!snxtsct(&nstr, player->argp[2]))
88         return RET_SYN;
89     prdate();
90     nsect = 0;
91     while (nxtsct(&nstr, &sect)) {
92         if (!player->owner)
93             continue;
94         if (type == INT_ROAD)
95             value = sect.sct_road;
96         else if (type == INT_RAIL)
97             value = sect.sct_rail;
98         else /* type == INT_DEF */
99             value = sect.sct_defense;
100         sprintf(inbuf, "Sector %s has a %s of %d%%.  Improve how much? ",
101                 xyas(sect.sct_x, sect.sct_y, player->cnum),
102                 intrchr[type].in_name, value);
103         if (!(p = getstarg(player->argp[3], inbuf, buf)) || !*p)
104             continue;
105         if (!check_sect_ok(&sect))
106             continue;
107         maxup = 100 - value;
108         wanted = atoi(p);
109         if (wanted < 0)
110             continue;
111         if (wanted < maxup)
112             maxup = wanted;
113         if (!maxup)
114             continue;
115         lneeded = intrchr[type].in_lcms * maxup;
116         if (opt_NO_LCMS)
117             lneeded = 0;
118         if (sect.sct_item[I_LCM] < lneeded) {
119             lneeded = sect.sct_item[I_LCM];
120             maxup = lneeded / intrchr[type].in_lcms;
121             if (maxup <= 0) {
122                 pr("Not enough lcms in %s\n",
123                    xyas(sect.sct_x, sect.sct_y, player->cnum));
124                 continue;
125             }
126         }
127         hneeded = intrchr[type].in_hcms * maxup;
128         if (opt_NO_HCMS)
129             hneeded = 0;
130         if (sect.sct_item[I_HCM] < hneeded) {
131             hneeded = sect.sct_item[I_HCM];
132             maxup = hneeded / intrchr[type].in_hcms;
133             if (maxup <= 0) {
134                 pr("Not enough hcms in %s\n",
135                    xyas(sect.sct_x, sect.sct_y, player->cnum));
136                 continue;
137             }
138         }
139         mneeded = intrchr[type].in_mcost * maxup;
140         if ((sect.sct_mobil - 1) < mneeded) {
141             mneeded = sect.sct_mobil - 1;
142             if (mneeded < 0)
143                 mneeded = 0;
144             maxup = mneeded / intrchr[type].in_mcost;
145             if (maxup <= 0) {
146                 pr("Not enough mobility in %s\n",
147                    xyas(sect.sct_x, sect.sct_y, player->cnum));
148                 continue;
149             }
150         }
151         dneeded = intrchr[type].in_dcost * maxup;
152         natp = getnatp(player->cnum);
153         if ((natp->nat_money - 1) < dneeded) {
154 /* Nasty - leave 'em with a buck. :) */
155             dneeded = natp->nat_money - 1;
156             if (dneeded < 0)
157                 dneeded = 0;
158             maxup = dneeded / intrchr[type].in_dcost;
159             if (maxup <= 0) {
160                 pr("Not enough money left to improve %s\n",
161                    xyas(sect.sct_x, sect.sct_y, player->cnum));
162                 continue;
163             }
164         }
165         if (maxup <= 0)
166             continue;
167         lneeded = intrchr[type].in_lcms * maxup;
168         hneeded = intrchr[type].in_hcms * maxup;
169         mneeded = intrchr[type].in_mcost * maxup;
170         dneeded = intrchr[type].in_dcost * maxup;
171         player->dolcost += dneeded;
172         if (!opt_NO_LCMS)
173             sect.sct_item[I_LCM] -= lneeded;
174         if (!opt_NO_HCMS)
175             sect.sct_item[I_HCM] -= hneeded;
176         sect.sct_mobil -= mneeded;
177         ovalue = value;
178         value += maxup;
179         if (CANT_HAPPEN(value > 100))
180             value = 100;
181         pr("Sector %s %s increased from %d%% to %d%%\n",
182            xyas(sect.sct_x, sect.sct_y, player->cnum),
183            intrchr[type].in_name, ovalue, value);
184         if (type == INT_ROAD)
185             sect.sct_road = value;
186         else if (type == INT_RAIL)
187             sect.sct_rail = value;
188         else if (type == INT_DEF)
189             sect.sct_defense = value;
190         putsect(&sect);
191         nsect++;
192     }
193     if (nsect == 0) {
194         if (player->argp[2])
195             pr("%s: No sector(s)\n", player->argp[1]);
196         else
197             pr("%s: No sector(s)\n", "");
198         return RET_FAIL;
199     } else
200         pr("%d sector%s\n", nsect, splur(nsect));
201     return 0;
202 }