]> git.pond.sub.org Git - empserver/blob - src/lib/commands/improve.c
<string.h> is ISO C, no need to #if it.
[empserver] / src / lib / commands / improve.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, 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 "deity.h"
43 #include "path.h"
44 #include "file.h"
45 #include "optlist.h"
46 #include "commands.h"
47
48 char *prompt[] = {
49     "Improve what ('road' or 'rail')? ",
50     "Improve what ('road', 'rail' or 'defense')? "
51 };
52
53 int
54 improve(void)
55 {
56     struct sctstr sect;
57     int nsect;
58     struct nstr_sect nstr;
59     s_char *p;
60     s_char buf[1024];
61     s_char inbuf[128];
62     int type;
63     int vec[I_MAX + 1];
64     int value;
65     int ovalue;
66     int maxup;
67     struct natstr *natp;
68     int lneeded;
69     int hneeded;
70     int mneeded;
71     int dneeded;
72     int wanted;
73
74     if (!(p = getstarg(player->argp[1],
75                        prompt[opt_DEFENSE_INFRA], buf)) || !*p)
76         return RET_SYN;
77     if (!strncmp(p, "ro", 2))
78         type = INT_ROAD;
79     else if (!strncmp(p, "ra", 2))
80         type = INT_RAIL;
81     else if (!strncmp(p, "de", 2) && opt_DEFENSE_INFRA)
82         type = INT_DEF;
83     else
84         return RET_SYN;
85     if (!snxtsct(&nstr, player->argp[2]))
86         return RET_SYN;
87     prdate();
88     nsect = 0;
89     while (nxtsct(&nstr, &sect)) {
90         if (!player->owner)
91             continue;
92         if (type == INT_ROAD)
93             value = sect.sct_road;
94         else if (type == INT_RAIL)
95             value = sect.sct_rail;
96         else if (type == INT_DEF)
97             value = sect.sct_defense;
98         sprintf(inbuf, "Sector %s has a %s of %d%%.  Improve how much? ",
99                 xyas(sect.sct_x, sect.sct_y, player->cnum),
100                 intrchr[type].in_name, value);
101         if (!(p = getstarg(player->argp[3], inbuf, buf)) || !*p)
102             continue;
103         if (!check_sect_ok(&sect))
104             continue;
105         getvec(VT_ITEM, vec, (s_char *)&sect, EF_SECTOR);
106         maxup = 100 - value;
107         wanted = atoi(p);
108         if (wanted < 0)
109             continue;
110         if (wanted < maxup)
111             maxup = wanted;
112         if (!maxup)
113             continue;
114         lneeded = intrchr[type].in_lcms * maxup;
115         if (opt_NO_LCMS)
116             lneeded = 0;
117         if (vec[I_LCM] < lneeded) {
118             lneeded = vec[I_LCM];
119             maxup = lneeded / intrchr[type].in_lcms;
120             if (maxup <= 0) {
121                 pr("Not enough lcms in %s\n",
122                    xyas(sect.sct_x, sect.sct_y, player->cnum));
123                 continue;
124             }
125         }
126         hneeded = intrchr[type].in_hcms * maxup;
127         if (opt_NO_HCMS)
128             hneeded = 0;
129         if (vec[I_HCM] < hneeded) {
130             hneeded = vec[I_HCM];
131             maxup = hneeded / intrchr[type].in_hcms;
132             if (maxup <= 0) {
133                 pr("Not enough hcms in %s\n",
134                    xyas(sect.sct_x, sect.sct_y, player->cnum));
135                 continue;
136             }
137         }
138         mneeded = intrchr[type].in_mcost * maxup;
139         if ((sect.sct_mobil - 1) < mneeded) {
140             mneeded = sect.sct_mobil - 1;
141             if (mneeded < 0)
142                 mneeded = 0;
143             maxup = mneeded / intrchr[type].in_mcost;
144             if (maxup <= 0) {
145                 pr("Not enough mobility in %s\n",
146                    xyas(sect.sct_x, sect.sct_y, player->cnum));
147                 continue;
148             }
149         }
150         dneeded = intrchr[type].in_dcost * maxup;
151         natp = getnatp(player->cnum);
152         if ((natp->nat_money - 1) < dneeded) {
153 /* Nasty - leave 'em with a buck. :) */
154             dneeded = natp->nat_money - 1;
155             if (dneeded < 0)
156                 dneeded = 0;
157             maxup = dneeded / intrchr[type].in_dcost;
158             if (maxup <= 0) {
159                 pr("Not enough money left to improve %s\n",
160                    xyas(sect.sct_x, sect.sct_y, player->cnum));
161                 continue;
162             }
163         }
164         if (maxup <= 0)
165             continue;
166         lneeded = intrchr[type].in_lcms * maxup;
167         hneeded = intrchr[type].in_hcms * maxup;
168         mneeded = intrchr[type].in_mcost * maxup;
169         dneeded = intrchr[type].in_dcost * maxup;
170         player->dolcost += dneeded;
171         if (!opt_NO_LCMS)
172             vec[I_LCM] -= lneeded;
173         if (!opt_NO_HCMS)
174             vec[I_HCM] -= hneeded;
175         sect.sct_mobil -= mneeded;
176         putvec(VT_ITEM, vec, (s_char *)&sect, EF_SECTOR);
177         ovalue = value;
178         value += maxup;
179         if (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 }