]> git.pond.sub.org Git - empserver/blob - src/lib/commands/improve.c
Import of Empire 4.2.12
[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 #ifdef Rel4
35 #include <string.h>
36 #endif /* Rel4 */
37 #include "misc.h"
38 #include "player.h"
39 #include "var.h"
40 #include "xy.h"
41 #include "sect.h"
42 #include "nsc.h"
43 #include "nat.h"
44 #include "deity.h"
45 #include "path.h"
46 #include "file.h"
47 #include "optlist.h"
48 #include "commands.h"
49
50 char *prompt[] = {
51       "Improve what ('road' or 'rail')? ",
52       "Improve what ('road', 'rail' or 'defense')? " };
53
54 int
55 improve(void)
56 {
57     struct      sctstr sect;
58     int nsect;
59     struct      nstr_sect nstr;
60     s_char *p;
61     s_char buf[1024];
62     s_char inbuf[128];
63     int type;
64     int vec[I_MAX+1];
65     int value;
66     int ovalue;
67     int maxup;
68     struct natstr *natp;
69     int lneeded;
70     int hneeded;
71     int mneeded;
72     int dneeded;
73     int wanted;
74     
75     if (!(p = getstarg(player->argp[1],
76                         prompt[opt_DEFENSE_INFRA], buf)) ||
77         !*p)
78         return RET_SYN;
79     if (!strncmp(p, "ro", 2))
80         type = INT_ROAD;
81     else if (!strncmp(p, "ra", 2))
82         type = INT_RAIL;
83     else if (!strncmp(p, "de", 2) && opt_DEFENSE_INFRA)
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 if (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         getvec(VT_ITEM, vec, (s_char *)&sect, EF_SECTOR);
108         maxup = 100 - value;
109         wanted = atoi(p);
110         if (wanted < 0)
111             continue;
112         if (wanted < maxup)
113             maxup = wanted;
114         if (!maxup)
115             continue;
116         lneeded = intrchr[type].in_lcms * maxup;
117         if (opt_NO_LCMS) lneeded = 0;
118         if (vec[I_LCM] < lneeded) {
119             lneeded = vec[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) 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) vec[I_LCM] -= lneeded;
172         if (!opt_NO_HCMS) vec[I_HCM] -= hneeded;
173         sect.sct_mobil -= mneeded;
174         putvec(VT_ITEM, vec, (s_char *)&sect, EF_SECTOR);
175         ovalue = value;
176         value += maxup;
177         if (value > 100)
178             value = 100;
179         pr("Sector %s %s increased from %d%% to %d%%\n",
180            xyas(sect.sct_x, sect.sct_y, player->cnum),
181            intrchr[type].in_name, ovalue, value);
182         if (type == INT_ROAD)
183             sect.sct_road = value;
184         else if (type == INT_RAIL)
185             sect.sct_rail = value;
186         else if (type == INT_DEF)
187             sect.sct_defense = value;
188         putsect(&sect);
189         nsect++;
190     }
191     if (nsect == 0) {
192         if (player->argp[2])
193             pr("%s: No sector(s)\n", player->argp[1]);
194         else
195             pr("%s: No sector(s)\n", "");
196         return RET_FAIL;
197     }else
198         pr("%d sector%s\n", nsect, splur(nsect));
199     return 0;
200 }
201
202