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