]> git.pond.sub.org Git - empserver/blob - src/lib/commands/improve.c
COPYING duplicates information from README. Remove. Move GPL from
[empserver] / src / lib / commands / improve.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, 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 <string.h>
37 #include "misc.h"
38 #include "player.h"
39 #include "xy.h"
40 #include "sect.h"
41 #include "nsc.h"
42 #include "nat.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 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)) {
81         if (!opt_DEFENSE_INFRA) {
82             pr("Defense infrastructure is disabled.\n");
83             return RET_FAIL;
84         }
85         type = INT_DEF;
86     } else
87         return RET_SYN;
88     if (!snxtsct(&nstr, player->argp[2]))
89         return RET_SYN;
90     prdate();
91     nsect = 0;
92     while (nxtsct(&nstr, &sect)) {
93         if (!player->owner)
94             continue;
95         if (type == INT_ROAD)
96             value = sect.sct_road;
97         else if (type == INT_RAIL)
98             value = sect.sct_rail;
99         else /* type == INT_DEF */
100             value = sect.sct_defense;
101         sprintf(inbuf, "Sector %s has a %s of %d%%.  Improve how much? ",
102                 xyas(sect.sct_x, sect.sct_y, player->cnum),
103                 intrchr[type].in_name, value);
104         if (!(p = getstarg(player->argp[3], inbuf, buf)) || !*p)
105             continue;
106         if (!check_sect_ok(&sect))
107             continue;
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 (sect.sct_item[I_LCM] < lneeded) {
118             lneeded = sect.sct_item[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 (sect.sct_item[I_HCM] < hneeded) {
128             hneeded = sect.sct_item[I_HCM];
129             maxup = hneeded / intrchr[type].in_hcms;
130             if (maxup <= 0) {
131                 pr("Not enough hcms in %s\n",
132                    xyas(sect.sct_x, sect.sct_y, player->cnum));
133                 continue;
134             }
135         }
136         mneeded = intrchr[type].in_mcost * maxup;
137         if ((sect.sct_mobil - 1) < mneeded) {
138             mneeded = sect.sct_mobil - 1;
139             if (mneeded < 0)
140                 mneeded = 0;
141             maxup = mneeded / intrchr[type].in_mcost;
142             if (maxup <= 0) {
143                 pr("Not enough mobility in %s\n",
144                    xyas(sect.sct_x, sect.sct_y, player->cnum));
145                 continue;
146             }
147         }
148         dneeded = intrchr[type].in_dcost * maxup;
149         natp = getnatp(player->cnum);
150         if ((natp->nat_money - 1) < dneeded) {
151 /* Nasty - leave 'em with a buck. :) */
152             dneeded = natp->nat_money - 1;
153             if (dneeded < 0)
154                 dneeded = 0;
155             maxup = dneeded / intrchr[type].in_dcost;
156             if (maxup <= 0) {
157                 pr("Not enough money left to improve %s\n",
158                    xyas(sect.sct_x, sect.sct_y, player->cnum));
159                 continue;
160             }
161         }
162         if (maxup <= 0)
163             continue;
164         lneeded = intrchr[type].in_lcms * maxup;
165         hneeded = intrchr[type].in_hcms * maxup;
166         mneeded = intrchr[type].in_mcost * maxup;
167         dneeded = intrchr[type].in_dcost * maxup;
168         player->dolcost += dneeded;
169         sect.sct_item[I_LCM] -= lneeded;
170         sect.sct_item[I_HCM] -= hneeded;
171         sect.sct_mobil -= mneeded;
172         ovalue = value;
173         value += maxup;
174         if (CANT_HAPPEN(value > 100))
175             value = 100;
176         pr("Sector %s %s increased from %d%% to %d%%\n",
177            xyas(sect.sct_x, sect.sct_y, player->cnum),
178            intrchr[type].in_name, ovalue, value);
179         if (type == INT_ROAD)
180             sect.sct_road = value;
181         else if (type == INT_RAIL)
182             sect.sct_rail = value;
183         else if (type == INT_DEF)
184             sect.sct_defense = value;
185         putsect(&sect);
186         nsect++;
187     }
188     if (nsect == 0) {
189         if (player->argp[2])
190             pr("%s: No sector(s)\n", player->argp[1]);
191         else
192             pr("%s: No sector(s)\n", "");
193         return RET_FAIL;
194     } else
195         pr("%d sector%s\n", nsect, splur(nsect));
196     return 0;
197 }