]> git.pond.sub.org Git - empserver/blob - src/lib/commands/upgr.c
Update copyright notice.
[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     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         avail = (LND_BLD_WORK(lp->l_lcm, lp->l_hcm) * UPGR_COST + 99) / 100;
118         if (sect.sct_avail < avail) {
119             pr("Not enough available work in %s to upgrade a %s\n",
120                xyas(sect.sct_x, sect.sct_y, player->cnum), lp->l_name);
121             pr(" (%d available work required)\n", avail);
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         cost = lp->l_cost * UPGR_COST / 100;
134         if (cost + player->dolcost > cash) {
135             pr("You don't have enough money to upgrade %s!\n",
136                prland(&land));
137             continue;
138         }
139
140         sect.sct_avail -= avail;
141         land.lnd_effic -= UPGR_EFF;
142         lnd_set_tech(&land, tlev);
143         land.lnd_harden = 0;
144         land.lnd_mission = 0;
145         time(&land.lnd_access);
146
147         putland(land.lnd_uid, &land);
148         putsect(&sect);
149         player->dolcost += cost;
150         pr("%s upgraded to tech %d, at a cost of %d\n", prland(&land),
151            land.lnd_tech, cost);
152         if (land.lnd_own != player->cnum)
153             wu(0, land.lnd_own,
154                "%s upgraded by %s to tech %d, at a cost of %d\n",
155                prland(&land), cname(player->cnum), land.lnd_tech,
156                cost);
157     }
158     if (n == 0) {
159         pr("No land units\n");
160         return RET_SYN;
161     }
162     return RET_OK;
163 }
164
165 static int
166 supgr(void)
167 {
168     struct sctstr sect;
169     struct natstr *natp;
170     struct nstr_item ni;
171     struct shpstr ship;
172     struct mchrstr *mp;
173     int n;
174     int tlev;
175     int avail, cost;
176     int rel;
177     long cash;
178
179     if (!snxtitem(&ni, EF_SHIP, player->argp[2]))
180         return RET_SYN;
181     ni.flags &= ~(EFF_OWNER);
182     natp = getnatp(player->cnum);
183     cash = natp->nat_money;
184     tlev = (int)natp->nat_level[NAT_TLEV];
185     n = 0;
186     while (nxtitem(&ni, (s_char *)&ship)) {
187         if (ship.shp_own == 0)
188             continue;
189         getsect(ship.shp_x, ship.shp_y, &sect);
190         if (sect.sct_own != player->cnum)
191             continue;
192         if (sect.sct_type != SCT_HARBR || sect.sct_effic < 60)
193             continue;
194         rel = getrel(getnatp(ship.shp_own), sect.sct_own);
195         if ((rel < FRIENDLY) && (sect.sct_own != ship.shp_own)) {
196             pr("You are not on friendly terms with the owner of ship %d!\n", ship.shp_uid);
197             continue;
198         }
199         n++;
200         mp = &mchr[(int)ship.shp_type];
201         avail = (SHP_BLD_WORK(mp->m_lcm, mp->m_hcm) * UPGR_COST + 99) / 100;
202         if (sect.sct_avail < avail) {
203             pr("Not enough available work in %s to upgrade a %s\n",
204                xyas(sect.sct_x, sect.sct_y, player->cnum), mp->m_name);
205             pr(" (%d available work required)\n", avail);
206             continue;
207         }
208         if (ship.shp_effic < 60) {
209             pr("%s is too damaged to upgrade!\n", prship(&ship));
210             continue;
211         }
212         if (ship.shp_tech >= tlev) {
213             pr("%s tech: %d, yours is only %d\n", prship(&ship),
214                ship.shp_tech, tlev);
215             continue;
216         }
217         cost = mp->m_cost * UPGR_COST / 100;
218         if (cost + player->dolcost > cash) {
219             pr("You don't have enough money to upgrade %s!\n",
220                prship(&ship));
221             continue;
222         }
223
224         sect.sct_avail -= avail;
225         ship.shp_effic -= UPGR_EFF;
226         shp_set_tech(&ship, tlev);
227         ship.shp_mission = 0;
228         time(&ship.shp_access);
229
230         putship(ship.shp_uid, &ship);
231         putsect(&sect);
232         player->dolcost += cost;
233         pr("%s upgraded to tech %d, at a cost of %d\n", prship(&ship),
234            ship.shp_tech, cost);
235         if (ship.shp_own != player->cnum)
236             wu(0, ship.shp_own,
237                "%s upgraded by %s to tech %d, at a cost of %d\n",
238                prship(&ship), cname(player->cnum), ship.shp_tech,
239                cost);
240     }
241     if (n == 0) {
242         pr("No ships\n");
243         return RET_SYN;
244     }
245     return RET_OK;
246 }
247
248 static int
249 pupgr(void)
250 {
251     struct sctstr sect;
252     struct natstr *natp;
253     struct nstr_item ni;
254     struct plnstr plane;
255     struct plchrstr *pp;
256     int n;
257     int tlev;
258     int avail, cost;
259     int rel;
260     long cash;
261
262     if (!snxtitem(&ni, EF_PLANE, player->argp[2]))
263         return RET_SYN;
264     ni.flags &= ~(EFF_OWNER);
265     natp = getnatp(player->cnum);
266     cash = natp->nat_money;
267     tlev = (int)natp->nat_level[NAT_TLEV];
268     n = 0;
269     while (nxtitem(&ni, (s_char *)&plane)) {
270         if (plane.pln_own == 0)
271             continue;
272         getsect(plane.pln_x, plane.pln_y, &sect);
273         if (sect.sct_own != player->cnum)
274             continue;
275         if (sect.sct_type != SCT_AIRPT || sect.sct_effic < 60)
276             continue;
277         rel = getrel(getnatp(plane.pln_own), sect.sct_own);
278         if ((rel < FRIENDLY) && (sect.sct_own != plane.pln_own)) {
279             pr("You are not on friendly terms with the owner of plane %d!\n", plane.pln_uid);
280             continue;
281         }
282         n++;
283         pp = &plchr[(int)plane.pln_type];
284         avail = (PLN_BLD_WORK(pp->pl_lcm, pp->pl_hcm) * UPGR_COST + 99) / 100;
285         if (sect.sct_avail < avail) {
286             pr("Not enough available work in %s to upgrade a %s\n",
287                xyas(sect.sct_x, sect.sct_y, player->cnum), pp->pl_name);
288             pr(" (%d available work required)\n", avail);
289             continue;
290         }
291         if (plane.pln_effic < 60) {
292             pr("%s is too damaged to upgrade!\n", prplane(&plane));
293             continue;
294         }
295         if (plane.pln_tech >= tlev) {
296             pr("%s tech: %d, yours is only %d\n", prplane(&plane),
297                plane.pln_tech, tlev);
298             continue;
299         }
300         cost = pp->pl_cost * UPGR_COST / 100;
301         if (cost + player->dolcost > cash) {
302             pr("You don't have enough money to upgrade %s!\n",
303                prplane(&plane));
304             continue;
305         }
306         if (plane.pln_flags & PLN_LAUNCHED) {
307             pr("Plane %s is in orbit!\n", prplane(&plane));
308             continue;
309         }
310
311         sect.sct_avail -= avail;
312         plane.pln_effic -= UPGR_EFF;
313         pln_set_tech(&plane, tlev);
314         plane.pln_harden = 0;
315         plane.pln_mission = 0;
316         time(&plane.pln_access);
317
318         putplane(plane.pln_uid, &plane);
319         putsect(&sect);
320         player->dolcost += cost;
321         pr("%s upgraded to tech %d, at a cost of %d\n", prplane(&plane),
322            plane.pln_tech, cost);
323         if (plane.pln_own != player->cnum)
324             wu(0, plane.pln_own,
325                "%s upgraded by %s to tech %d, at a cost of %d\n",
326                prplane(&plane), cname(player->cnum), plane.pln_tech,
327                cost);
328     }
329     if (n == 0) {
330         pr("No planes.\n");
331         return RET_SYN;
332     }
333     return RET_OK;
334 }