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