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