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