]> git.pond.sub.org Git - empserver/blob - src/lib/commands/upgr.c
Remove a bunch of redundant casts.
[empserver] / src / lib / commands / upgr.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2005, 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  *  upgr.c: Upgrade tech of ships/planes/units
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1986
32  *     Steve McClure, 1996-2000
33  */
34
35 #include "misc.h"
36 #include "player.h"
37 #include "xy.h"
38 #include "ship.h"
39 #include "land.h"
40 #include "plane.h"
41 #include "sect.h"
42 #include "nat.h"
43 #include "nsc.h"
44 #include "file.h"
45 #include "commands.h"
46
47 enum {
48   UPGR_COST = 15,               /* how much avail and money to charge */
49   UPGR_EFF = 35                 /* efficiency reduction */
50 };
51
52 static int lupgr(void);
53 static int pupgr(void);
54 static int supgr(void);
55
56 int
57 upgr(void)
58 {
59     s_char *p;
60     s_char buf[1024];
61
62     if (!(p = getstarg(player->argp[1], "Ship, land, or plane? ", buf)))
63         return RET_SYN;
64     switch (*p) {
65     case 's':
66     case 'S':
67         return supgr();
68     case 'p':
69     case 'P':
70         return pupgr();
71     case 'l':
72     case 'L':
73         return lupgr();
74     default:
75         break;
76     }
77     pr("Ships, land units or planes only!\n");
78     return RET_SYN;
79 }
80
81 static int
82 lupgr(void)
83 {
84     struct sctstr sect;
85     struct natstr *natp;
86     struct nstr_item ni;
87     struct lndstr land;
88     struct lchrstr *lp;
89     int n;
90     int tlev;
91     int avail, cost;
92     int rel;
93     long cash;
94
95     if (!snxtitem(&ni, EF_LAND, player->argp[2]))
96         return RET_SYN;
97     natp = getnatp(player->cnum);
98     cash = natp->nat_money;
99     tlev = (int)natp->nat_level[NAT_TLEV];
100     n = 0;
101     while (nxtitem(&ni, &land)) {
102         if (land.lnd_own == 0)
103             continue;
104         getsect(land.lnd_x, land.lnd_y, &sect);
105         if (sect.sct_own != player->cnum)
106             continue;
107         if (sect.sct_type != SCT_HEADQ || sect.sct_effic < 60)
108             continue;
109         rel = getrel(getnatp(land.lnd_own), sect.sct_own);
110         if ((rel < FRIENDLY) && (sect.sct_own != land.lnd_own)) {
111             pr("You are not on friendly terms with the owner of unit %d!\n", land.lnd_uid);
112             continue;
113         }
114         n++;
115         lp = &lchr[(int)land.lnd_type];
116         avail = (LND_BLD_WORK(lp->l_lcm, lp->l_hcm) * UPGR_COST + 99) / 100;
117         if (sect.sct_avail < avail) {
118             pr("Not enough available work in %s to upgrade a %s\n",
119                xyas(sect.sct_x, sect.sct_y, player->cnum), lp->l_name);
120             pr(" (%d available work required)\n", avail);
121             continue;
122         }
123         if (land.lnd_effic < 60) {
124             pr("%s is too damaged to upgrade!\n", prland(&land));
125             continue;
126         }
127         if (land.lnd_tech >= tlev) {
128             pr("%s tech: %d, yours is only %d\n", prland(&land),
129                land.lnd_tech, tlev);
130             continue;
131         }
132         cost = lp->l_cost * UPGR_COST / 100;
133         if (cost + player->dolcost > cash) {
134             pr("You don't have enough money to upgrade %s!\n",
135                prland(&land));
136             continue;
137         }
138
139         sect.sct_avail -= avail;
140         land.lnd_effic -= UPGR_EFF;
141         lnd_set_tech(&land, tlev);
142         land.lnd_harden = 0;
143         land.lnd_mission = 0;
144         time(&land.lnd_access);
145
146         putland(land.lnd_uid, &land);
147         putsect(&sect);
148         player->dolcost += cost;
149         pr("%s upgraded to tech %d, at a cost of %d\n", prland(&land),
150            land.lnd_tech, cost);
151         if (land.lnd_own != player->cnum)
152             wu(0, land.lnd_own,
153                "%s upgraded by %s to tech %d, at a cost of %d\n",
154                prland(&land), cname(player->cnum), land.lnd_tech,
155                cost);
156     }
157     if (n == 0) {
158         pr("No land units\n");
159         return RET_SYN;
160     }
161     return RET_OK;
162 }
163
164 static int
165 supgr(void)
166 {
167     struct sctstr sect;
168     struct natstr *natp;
169     struct nstr_item ni;
170     struct shpstr ship;
171     struct mchrstr *mp;
172     int n;
173     int tlev;
174     int avail, cost;
175     int rel;
176     long cash;
177
178     if (!snxtitem(&ni, EF_SHIP, player->argp[2]))
179         return RET_SYN;
180     natp = getnatp(player->cnum);
181     cash = natp->nat_money;
182     tlev = (int)natp->nat_level[NAT_TLEV];
183     n = 0;
184     while (nxtitem(&ni, &ship)) {
185         if (ship.shp_own == 0)
186             continue;
187         getsect(ship.shp_x, ship.shp_y, &sect);
188         if (sect.sct_own != player->cnum)
189             continue;
190         if (sect.sct_type != SCT_HARBR || sect.sct_effic < 60)
191             continue;
192         rel = getrel(getnatp(ship.shp_own), sect.sct_own);
193         if ((rel < FRIENDLY) && (sect.sct_own != ship.shp_own)) {
194             pr("You are not on friendly terms with the owner of ship %d!\n", ship.shp_uid);
195             continue;
196         }
197         n++;
198         mp = &mchr[(int)ship.shp_type];
199         avail = (SHP_BLD_WORK(mp->m_lcm, mp->m_hcm) * UPGR_COST + 99) / 100;
200         if (sect.sct_avail < avail) {
201             pr("Not enough available work in %s to upgrade a %s\n",
202                xyas(sect.sct_x, sect.sct_y, player->cnum), mp->m_name);
203             pr(" (%d available work required)\n", avail);
204             continue;
205         }
206         if (ship.shp_effic < 60) {
207             pr("%s is too damaged to upgrade!\n", prship(&ship));
208             continue;
209         }
210         if (ship.shp_tech >= tlev) {
211             pr("%s tech: %d, yours is only %d\n", prship(&ship),
212                ship.shp_tech, tlev);
213             continue;
214         }
215         cost = mp->m_cost * UPGR_COST / 100;
216         if (cost + player->dolcost > cash) {
217             pr("You don't have enough money to upgrade %s!\n",
218                prship(&ship));
219             continue;
220         }
221
222         sect.sct_avail -= avail;
223         ship.shp_effic -= UPGR_EFF;
224         shp_set_tech(&ship, tlev);
225         ship.shp_mission = 0;
226         time(&ship.shp_access);
227
228         putship(ship.shp_uid, &ship);
229         putsect(&sect);
230         player->dolcost += cost;
231         pr("%s upgraded to tech %d, at a cost of %d\n", prship(&ship),
232            ship.shp_tech, cost);
233         if (ship.shp_own != player->cnum)
234             wu(0, ship.shp_own,
235                "%s upgraded by %s to tech %d, at a cost of %d\n",
236                prship(&ship), cname(player->cnum), ship.shp_tech,
237                cost);
238     }
239     if (n == 0) {
240         pr("No ships\n");
241         return RET_SYN;
242     }
243     return RET_OK;
244 }
245
246 static int
247 pupgr(void)
248 {
249     struct sctstr sect;
250     struct natstr *natp;
251     struct nstr_item ni;
252     struct plnstr plane;
253     struct plchrstr *pp;
254     int n;
255     int tlev;
256     int avail, cost;
257     int rel;
258     long cash;
259
260     if (!snxtitem(&ni, EF_PLANE, player->argp[2]))
261         return RET_SYN;
262     natp = getnatp(player->cnum);
263     cash = natp->nat_money;
264     tlev = (int)natp->nat_level[NAT_TLEV];
265     n = 0;
266     while (nxtitem(&ni, &plane)) {
267         if (plane.pln_own == 0)
268             continue;
269         getsect(plane.pln_x, plane.pln_y, &sect);
270         if (sect.sct_own != player->cnum)
271             continue;
272         if (sect.sct_type != SCT_AIRPT || sect.sct_effic < 60)
273             continue;
274         rel = getrel(getnatp(plane.pln_own), sect.sct_own);
275         if ((rel < FRIENDLY) && (sect.sct_own != plane.pln_own)) {
276             pr("You are not on friendly terms with the owner of plane %d!\n", plane.pln_uid);
277             continue;
278         }
279         n++;
280         pp = &plchr[(int)plane.pln_type];
281         avail = (PLN_BLD_WORK(pp->pl_lcm, pp->pl_hcm) * UPGR_COST + 99) / 100;
282         if (sect.sct_avail < avail) {
283             pr("Not enough available work in %s to upgrade a %s\n",
284                xyas(sect.sct_x, sect.sct_y, player->cnum), pp->pl_name);
285             pr(" (%d available work required)\n", avail);
286             continue;
287         }
288         if (plane.pln_effic < 60) {
289             pr("%s is too damaged to upgrade!\n", prplane(&plane));
290             continue;
291         }
292         if (plane.pln_tech >= tlev) {
293             pr("%s tech: %d, yours is only %d\n", prplane(&plane),
294                plane.pln_tech, tlev);
295             continue;
296         }
297         cost = pp->pl_cost * UPGR_COST / 100;
298         if (cost + player->dolcost > cash) {
299             pr("You don't have enough money to upgrade %s!\n",
300                prplane(&plane));
301             continue;
302         }
303         if (plane.pln_flags & PLN_LAUNCHED) {
304             pr("Plane %s is in orbit!\n", prplane(&plane));
305             continue;
306         }
307
308         sect.sct_avail -= avail;
309         plane.pln_effic -= UPGR_EFF;
310         pln_set_tech(&plane, tlev);
311         plane.pln_harden = 0;
312         plane.pln_mission = 0;
313         time(&plane.pln_access);
314
315         putplane(plane.pln_uid, &plane);
316         putsect(&sect);
317         player->dolcost += cost;
318         pr("%s upgraded to tech %d, at a cost of %d\n", prplane(&plane),
319            plane.pln_tech, cost);
320         if (plane.pln_own != player->cnum)
321             wu(0, plane.pln_own,
322                "%s upgraded by %s to tech %d, at a cost of %d\n",
323                prplane(&plane), cname(player->cnum), plane.pln_tech,
324                cost);
325     }
326     if (n == 0) {
327         pr("No planes.\n");
328         return RET_SYN;
329     }
330     return RET_OK;
331 }