]> git.pond.sub.org Git - empserver/blob - src/lib/commands/upgr.c
Supply prototypes where possible. This uncovered type errors with
[empserver] / src / lib / commands / upgr.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  *  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 <math.h>
36
37 #include "misc.h"
38 #include "player.h"
39 #include "xy.h"
40 #include "var.h"
41 #include "ship.h"
42 #include "land.h"
43 #include "plane.h"
44 #include "sect.h"
45 #include "nat.h"
46 #include "nsc.h"
47 #include "file.h"
48 #include "commands.h"
49
50 static int lupgr(void);
51 static int pupgr(void);
52 static int supgr(void);
53
54 int
55 upgr(void)
56 {
57     s_char *p;
58     s_char buf[1024];
59
60     if (!(p = getstarg(player->argp[1], "Ship, land, or plane? ", buf)))
61         return RET_SYN;
62     switch (*p) {
63     case 's':
64     case 'S':
65         return supgr();
66     case 'p':
67     case 'P':
68         return pupgr();
69     case 'l':
70     case 'L':
71         return lupgr();
72     default:
73         pr("Ships, land units or planes only!\n");
74         return RET_SYN;
75     }
76     return RET_OK;
77 }
78
79 static int
80 lupgr(void)
81 {
82     struct sctstr sect;
83     struct natstr *natp;
84     struct nstr_item ni;
85     struct lndstr land;
86     struct lchrstr *lp;
87     int n;
88     int tlev;
89     int w_p_eff;
90     int points;
91     int rel;
92     int techdiff;
93     long cash;
94
95     if (!snxtitem(&ni, EF_LAND, player->argp[2]))
96         return RET_SYN;
97     ni.flags &= ~(EFF_OWNER);
98     natp = getnatp(player->cnum);
99     cash = natp->nat_money;
100     tlev = (int)natp->nat_level[NAT_TLEV];
101     n = 0;
102     while (nxtitem(&ni, (s_char *)&land)) {
103         if (land.lnd_own == 0)
104             continue;
105         getsect(land.lnd_x, land.lnd_y, &sect);
106         if (sect.sct_own != player->cnum)
107             continue;
108         if (sect.sct_type != SCT_HEADQ || sect.sct_effic < 60)
109             continue;
110         rel = getrel(getnatp(land.lnd_own), sect.sct_own);
111         if ((rel < FRIENDLY) && (sect.sct_own != land.lnd_own)) {
112             pr("You are not on friendly terms with the owner of unit %d!\n", land.lnd_uid);
113             continue;
114         }
115         n++;
116         lp = &lchr[(int)land.lnd_type];
117         w_p_eff = ((lp->l_lcm / 2) + lp->l_hcm);
118         points = sect.sct_avail * 100 / w_p_eff;
119         if (points < 20) {
120             pr("Not enough available work in %s to upgrade a %s\n",
121                xyas(sect.sct_x, sect.sct_y, player->cnum), lp->l_name);
122             pr(" (%d available work required)\n",
123                1 + (w_p_eff * 20) / 100);
124             continue;
125         }
126         if (land.lnd_effic < 60) {
127             pr("%s is too damaged to upgrade!\n", prland(&land));
128             continue;
129         }
130         if (land.lnd_tech >= tlev) {
131             pr("%s tech: %d, yours is only %d\n", prland(&land),
132                land.lnd_tech, tlev);
133             continue;
134         }
135         if (lp->l_cost * .15 + player->dolcost > cash) {
136             pr("You don't have enough money to upgrade %s!\n",
137                prland(&land));
138             continue;
139         }
140
141         sect.sct_avail = (sect.sct_avail * 100 - w_p_eff * 20) / 100;
142         land.lnd_effic -= 35;
143
144         land.lnd_tech = tlev;
145         techdiff = (int)(tlev - lp->l_tech);
146
147         land.lnd_harden = 0;
148         land.lnd_mission = 0;
149
150         land.lnd_att = (float)LND_ATTDEF(lp->l_att, techdiff);
151         land.lnd_def = (float)LND_ATTDEF(lp->l_def, techdiff);
152         land.lnd_vul = (int)LND_VUL(lp->l_vul, techdiff);
153         land.lnd_spd = (int)LND_SPD(lp->l_spd, techdiff);
154         land.lnd_vis = (int)LND_VIS(lp->l_vis, techdiff);
155         land.lnd_spy = (int)LND_SPY(lp->l_spy, techdiff);
156         land.lnd_rad = (int)LND_RAD(lp->l_rad, techdiff);
157         land.lnd_frg = (int)LND_FRG(lp->l_frg, techdiff);
158         land.lnd_acc = (int)LND_ACC(lp->l_acc, techdiff);
159         land.lnd_dam = (int)LND_DAM(lp->l_dam, techdiff);
160         land.lnd_ammo = (int)LND_AMM(lp->l_ammo, lp->l_dam, techdiff);
161         land.lnd_aaf = (int)LND_AAF(lp->l_aaf, techdiff);
162         land.lnd_fuelc = (int)LND_FC(lp->l_fuelc, techdiff);
163         land.lnd_fuelu = (int)LND_FU(lp->l_fuelu, techdiff);
164         land.lnd_maxlight = (int)LND_XPL(lp->l_nxlight, techdiff);
165         land.lnd_maxland = (int)LND_MXL(lp->l_mxland, techdiff);
166
167         time(&land.lnd_access);
168
169         putland(land.lnd_uid, &land);
170         putsect(&sect);
171         player->dolcost += (double)lp->l_cost * .15;
172         pr("%s upgraded to tech %d, at a cost of %d\n", prland(&land),
173            land.lnd_tech, (int)(lp->l_cost * .15));
174         if (land.lnd_own != player->cnum)
175             wu(0, land.lnd_own,
176                "%s upgraded by %s to tech %d, at a cost of %d\n",
177                prland(&land), cname(player->cnum), land.lnd_tech,
178                (int)(lp->l_cost * .15));
179     }
180     if (n == 0) {
181         pr("No land units\n");
182         return RET_SYN;
183     }
184     return RET_OK;
185 }
186
187 static int
188 supgr(void)
189 {
190     struct sctstr sect;
191     struct natstr *natp;
192     struct nstr_item ni;
193     struct shpstr ship;
194     struct mchrstr *mp;
195     int n;
196     int tlev;
197     int w_p_eff;
198     int points;
199     int rel;
200     int techdiff;
201     long cash;
202
203     if (!snxtitem(&ni, EF_SHIP, player->argp[2]))
204         return RET_SYN;
205     ni.flags &= ~(EFF_OWNER);
206     natp = getnatp(player->cnum);
207     cash = natp->nat_money;
208     tlev = (int)natp->nat_level[NAT_TLEV];
209     n = 0;
210     while (nxtitem(&ni, (s_char *)&ship)) {
211         if (ship.shp_own == 0)
212             continue;
213         getsect(ship.shp_x, ship.shp_y, &sect);
214         if (sect.sct_own != player->cnum)
215             continue;
216         if (sect.sct_type != SCT_HARBR || sect.sct_effic < 60)
217             continue;
218         rel = getrel(getnatp(ship.shp_own), sect.sct_own);
219         if ((rel < FRIENDLY) && (sect.sct_own != ship.shp_own)) {
220             pr("You are not on friendly terms with the owner of ship %d!\n", ship.shp_uid);
221             continue;
222         }
223         n++;
224         mp = &mchr[(int)ship.shp_type];
225         w_p_eff = ((mp->m_lcm / 2) + mp->m_hcm);
226         points = sect.sct_avail * 100 / w_p_eff;
227         if (points < 20) {
228             pr("Not enough available work in %s to upgrade a %s\n",
229                xyas(sect.sct_x, sect.sct_y, player->cnum), mp->m_name);
230             pr(" (%d available work required)\n",
231                1 + (w_p_eff * 20) / 100);
232             continue;
233         }
234         if (ship.shp_effic < 60) {
235             pr("%s is too damaged to upgrade!\n", prship(&ship));
236             continue;
237         }
238         if (ship.shp_tech >= tlev) {
239             pr("%s tech: %d, yours is only %d\n", prship(&ship),
240                ship.shp_tech, tlev);
241             continue;
242         }
243         if (mp->m_cost * .15 + player->dolcost > cash) {
244             pr("You don't have enough money to upgrade %s!\n",
245                prship(&ship));
246             continue;
247         }
248
249         sect.sct_avail = (sect.sct_avail * 100 - w_p_eff * 20) / 100;
250         ship.shp_effic -= 35;
251         ship.shp_tech = tlev;
252
253         techdiff = (int)(tlev - mp->m_tech);
254         ship.shp_armor = (short)SHP_DEF(mp->m_armor, techdiff);
255         ship.shp_speed = (short)SHP_SPD(mp->m_speed, techdiff);
256         ship.shp_visib = (short)SHP_VIS(mp->m_visib, techdiff);
257         ship.shp_frnge = (short)SHP_RNG(mp->m_frnge, techdiff);
258         ship.shp_glim = (short)SHP_FIR(mp->m_glim, techdiff);
259
260         ship.shp_mission = 0;
261         time(&ship.shp_access);
262
263         putship(ship.shp_uid, &ship);
264         putsect(&sect);
265         player->dolcost += (double)mp->m_cost * .15;
266         pr("%s upgraded to tech %d, at a cost of %d\n", prship(&ship),
267            ship.shp_tech, (int)(mp->m_cost * .15));
268         if (ship.shp_own != player->cnum)
269             wu(0, ship.shp_own,
270                "%s upgraded by %s to tech %d, at a cost of %d\n",
271                prship(&ship), cname(player->cnum), ship.shp_tech,
272                (int)(mp->m_cost * .15));
273     }
274     if (n == 0) {
275         pr("No ships\n");
276         return RET_SYN;
277     }
278     return RET_OK;
279 }
280
281 static int
282 pupgr(void)
283 {
284     struct sctstr sect;
285     struct natstr *natp;
286     struct nstr_item ni;
287     struct plnstr plane;
288     struct plchrstr *pp;
289     int n;
290     int tlev;
291     int w_p_eff;
292     int points;
293     int rel;
294     int techdiff;
295     long cash;
296
297     if (!snxtitem(&ni, EF_PLANE, player->argp[2]))
298         return RET_SYN;
299     ni.flags &= ~(EFF_OWNER);
300     natp = getnatp(player->cnum);
301     cash = natp->nat_money;
302     tlev = (int)natp->nat_level[NAT_TLEV];
303     n = 0;
304     while (nxtitem(&ni, (s_char *)&plane)) {
305         if (plane.pln_own == 0)
306             continue;
307         getsect(plane.pln_x, plane.pln_y, &sect);
308         if (sect.sct_own != player->cnum)
309             continue;
310         if (sect.sct_type != SCT_AIRPT || sect.sct_effic < 60)
311             continue;
312         rel = getrel(getnatp(plane.pln_own), sect.sct_own);
313         if ((rel < FRIENDLY) && (sect.sct_own != plane.pln_own)) {
314             pr("You are not on friendly terms with the owner of plane %d!\n", plane.pln_uid);
315             continue;
316         }
317         n++;
318         pp = &plchr[(int)plane.pln_type];
319         w_p_eff = ((pp->pl_lcm / 2) + pp->pl_hcm);
320         points = sect.sct_avail * 100 / w_p_eff;
321         if (points < 20) {
322             pr("Not enough available work in %s to upgrade a %s\n",
323                xyas(sect.sct_x, sect.sct_y, player->cnum), pp->pl_name);
324             pr(" (%d available work required)\n",
325                1 + (w_p_eff * 20) / 100);
326             continue;
327         }
328         if (plane.pln_effic < 60) {
329             pr("%s is too damaged to upgrade!\n", prplane(&plane));
330             continue;
331         }
332         if (plane.pln_tech >= tlev) {
333             pr("%s tech: %d, yours is only %d\n", prplane(&plane),
334                plane.pln_tech, tlev);
335             continue;
336         }
337         if (pp->pl_cost * .15 + player->dolcost > cash) {
338             pr("You don't have enough money to upgrade %s!\n",
339                prplane(&plane));
340             continue;
341         }
342         if (plane.pln_flags & PLN_LAUNCHED) {
343             pr("Plane %s is in orbit!\n", prplane(&plane));
344             continue;
345         }
346
347         sect.sct_avail = (sect.sct_avail * 100 - w_p_eff * 20) / 100;
348         plane.pln_effic -= 35;
349
350         plane.pln_tech = tlev;
351         techdiff = (int)(tlev - pp->pl_tech);
352
353         plane.pln_mission = 0;
354         plane.pln_opx = 0;
355         plane.pln_opy = 0;
356         plane.pln_radius = 0;
357         plane.pln_harden = 0;
358         plane.pln_mission = 0;
359
360         plane.pln_att = PLN_ATTDEF(pp->pl_att, techdiff);
361         plane.pln_def = PLN_ATTDEF(pp->pl_def, techdiff);
362         plane.pln_acc = PLN_ACC(pp->pl_acc, techdiff);
363         plane.pln_range = PLN_RAN(pp->pl_range, techdiff);
364         plane.pln_range_max = plane.pln_range;
365         plane.pln_load = PLN_LOAD(pp->pl_load, techdiff);
366
367         time(&plane.pln_access);
368
369         putplane(plane.pln_uid, &plane);
370         putsect(&sect);
371         player->dolcost += (double)pp->pl_cost * .15;
372         pr("%s upgraded to tech %d, at a cost of %d\n", prplane(&plane),
373            plane.pln_tech, (int)(pp->pl_cost * .15));
374         if (plane.pln_own != player->cnum)
375             wu(0, plane.pln_own,
376                "%s upgraded by %s to tech %d, at a cost of %d\n",
377                prplane(&plane), cname(player->cnum), plane.pln_tech,
378                (int)(pp->pl_cost * .15));
379     }
380     if (n == 0) {
381         pr("No planes.\n");
382         return RET_SYN;
383     }
384     return RET_OK;
385 }