]> git.pond.sub.org Git - empserver/blob - src/lib/commands/improve.c
deity.h is redundant, remove 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 "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 vec[I_MAX + 1];
63     int value;
64     int ovalue;
65     int maxup;
66     struct natstr *natp;
67     int lneeded;
68     int hneeded;
69     int mneeded;
70     int dneeded;
71     int wanted;
72
73     if (!(p = getstarg(player->argp[1],
74                        prompt[opt_DEFENSE_INFRA], buf)) || !*p)
75         return RET_SYN;
76     if (!strncmp(p, "ro", 2))
77         type = INT_ROAD;
78     else if (!strncmp(p, "ra", 2))
79         type = INT_RAIL;
80     else if (!strncmp(p, "de", 2) && opt_DEFENSE_INFRA)
81         type = INT_DEF;
82     else
83         return RET_SYN;
84     if (!snxtsct(&nstr, player->argp[2]))
85         return RET_SYN;
86     prdate();
87     nsect = 0;
88     while (nxtsct(&nstr, &sect)) {
89         if (!player->owner)
90             continue;
91         if (type == INT_ROAD)
92             value = sect.sct_road;
93         else if (type == INT_RAIL)
94             value = sect.sct_rail;
95         else if (type == INT_DEF)
96             value = sect.sct_defense;
97         sprintf(inbuf, "Sector %s has a %s of %d%%.  Improve how much? ",
98                 xyas(sect.sct_x, sect.sct_y, player->cnum),
99                 intrchr[type].in_name, value);
100         if (!(p = getstarg(player->argp[3], inbuf, buf)) || !*p)
101             continue;
102         if (!check_sect_ok(&sect))
103             continue;
104         getvec(VT_ITEM, vec, (s_char *)&sect, EF_SECTOR);
105         maxup = 100 - value;
106         wanted = atoi(p);
107         if (wanted < 0)
108             continue;
109         if (wanted < maxup)
110             maxup = wanted;
111         if (!maxup)
112             continue;
113         lneeded = intrchr[type].in_lcms * maxup;
114         if (opt_NO_LCMS)
115             lneeded = 0;
116         if (vec[I_LCM] < lneeded) {
117             lneeded = vec[I_LCM];
118             maxup = lneeded / intrchr[type].in_lcms;
119             if (maxup <= 0) {
120                 pr("Not enough lcms in %s\n",
121                    xyas(sect.sct_x, sect.sct_y, player->cnum));
122                 continue;
123             }
124         }
125         hneeded = intrchr[type].in_hcms * maxup;
126         if (opt_NO_HCMS)
127             hneeded = 0;
128         if (vec[I_HCM] < hneeded) {
129             hneeded = vec[I_HCM];
130             maxup = hneeded / intrchr[type].in_hcms;
131             if (maxup <= 0) {
132                 pr("Not enough hcms in %s\n",
133                    xyas(sect.sct_x, sect.sct_y, player->cnum));
134                 continue;
135             }
136         }
137         mneeded = intrchr[type].in_mcost * maxup;
138         if ((sect.sct_mobil - 1) < mneeded) {
139             mneeded = sect.sct_mobil - 1;
140             if (mneeded < 0)
141                 mneeded = 0;
142             maxup = mneeded / intrchr[type].in_mcost;
143             if (maxup <= 0) {
144                 pr("Not enough mobility in %s\n",
145                    xyas(sect.sct_x, sect.sct_y, player->cnum));
146                 continue;
147             }
148         }
149         dneeded = intrchr[type].in_dcost * maxup;
150         natp = getnatp(player->cnum);
151         if ((natp->nat_money - 1) < dneeded) {
152 /* Nasty - leave 'em with a buck. :) */
153             dneeded = natp->nat_money - 1;
154             if (dneeded < 0)
155                 dneeded = 0;
156             maxup = dneeded / intrchr[type].in_dcost;
157             if (maxup <= 0) {
158                 pr("Not enough money left to improve %s\n",
159                    xyas(sect.sct_x, sect.sct_y, player->cnum));
160                 continue;
161             }
162         }
163         if (maxup <= 0)
164             continue;
165         lneeded = intrchr[type].in_lcms * maxup;
166         hneeded = intrchr[type].in_hcms * maxup;
167         mneeded = intrchr[type].in_mcost * maxup;
168         dneeded = intrchr[type].in_dcost * maxup;
169         player->dolcost += dneeded;
170         if (!opt_NO_LCMS)
171             vec[I_LCM] -= lneeded;
172         if (!opt_NO_HCMS)
173             vec[I_HCM] -= hneeded;
174         sect.sct_mobil -= mneeded;
175         putvec(VT_ITEM, vec, (s_char *)&sect, EF_SECTOR);
176         ovalue = value;
177         value += maxup;
178         if (value > 100)
179             value = 100;
180         pr("Sector %s %s increased from %d%% to %d%%\n",
181            xyas(sect.sct_x, sect.sct_y, player->cnum),
182            intrchr[type].in_name, ovalue, value);
183         if (type == INT_ROAD)
184             sect.sct_road = value;
185         else if (type == INT_RAIL)
186             sect.sct_rail = value;
187         else if (type == INT_DEF)
188             sect.sct_defense = value;
189         putsect(&sect);
190         nsect++;
191     }
192     if (nsect == 0) {
193         if (player->argp[2])
194             pr("%s: No sector(s)\n", player->argp[1]);
195         else
196             pr("%s: No sector(s)\n", "");
197         return RET_FAIL;
198     } else
199         pr("%d sector%s\n", nsect, splur(nsect));
200     return 0;
201 }