]> git.pond.sub.org Git - empserver/blob - src/lib/commands/scra.c
COPYING duplicates information from README. Remove. Move GPL from
[empserver] / src / lib / commands / scra.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, 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 files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  scra.c: Scrap ships, planes or land units
29  * 
30  *  Known contributors to this file:
31  *     Steve McClure, 2000
32  *     
33  */
34
35 #include <config.h>
36
37 #include "misc.h"
38 #include "player.h"
39 #include "xy.h"
40 #include "sect.h"
41 #include "ship.h"
42 #include "plane.h"
43 #include "land.h"
44 #include "nat.h"
45 #include "nsc.h"
46 #include "file.h"
47 #include "commands.h"
48 #include "optlist.h"
49
50 union item_u {
51     struct shpstr ship;
52     struct plnstr plane;
53     struct lndstr land;
54 };
55
56 int
57 scra(void)
58 {
59     struct nstr_item ni;
60     union item_u item;
61     int type;
62     struct sctstr sect;
63     struct mchrstr *mp;
64     struct plchrstr *pp;
65     struct lchrstr *lp;
66     s_char *p;
67     i_type i;
68     struct nstr_item ni2;
69     struct plnstr plane;
70     struct lndstr land;
71     struct sctstr sect2;
72     s_char prompt[128];
73     s_char buf[1024];
74     float eff;
75
76     if (!(p = getstarg(player->argp[1], "Ship, land, or plane? ", buf)))
77         return RET_SYN;
78     switch (*p) {
79     case 's':
80         type = EF_SHIP;
81         break;
82     case 'p':
83         type = EF_PLANE;
84         break;
85     case 'l':
86         type = EF_LAND;
87         break;
88     default:
89         pr("Ships, land units, or planes only! (s, l, p)\n");
90         return RET_SYN;
91     }
92     sprintf(prompt, "%s(s)? ", ef_nameof(type));
93     if ((p = getstarg(player->argp[2], prompt, buf)) == 0)
94         return RET_SYN;
95     if (!snxtitem(&ni, type, p))
96         return RET_SYN;
97     if (p && (isalpha(*p) || (*p == '*') || (*p == '~') || issector(p)
98               || islist(p))) {
99         s_char y_or_n[80], bbuf[80];
100
101         memset(y_or_n, 0, sizeof(y_or_n));
102         if (type == EF_SHIP) {
103             if (*p == '*')
104                 sprintf(bbuf, "all ships");
105             else if (*p == '~')
106                 sprintf(bbuf, "all unassigned ships");
107             else if (issector(p))
108                 sprintf(bbuf, "all ships in %s", p);
109             else if (isalpha(*p))
110                 sprintf(bbuf, "fleet %c", *p);
111             else
112                 sprintf(bbuf, "ships %s", p);
113         } else if (type == EF_LAND) {
114             if (*p == '*')
115                 sprintf(bbuf, "all land units");
116             else if (*p == '~')
117                 sprintf(bbuf, "all unassigned land units");
118             else if (issector(p))
119                 sprintf(bbuf, "all units in %s", p);
120             else if (isalpha(*p))
121                 sprintf(bbuf, "army %c", *p);
122             else
123                 sprintf(bbuf, "units %s", p);
124         } else {
125             if (*p == '*')
126                 sprintf(bbuf, "all planes");
127             else if (*p == '~')
128                 sprintf(bbuf, "all unassigned planes");
129             else if (issector(p))
130                 sprintf(bbuf, "all planes in %s", p);
131             else if (isalpha(*p))
132                 sprintf(bbuf, "wing %c", *p);
133             else
134                 sprintf(bbuf, "planes %s", p);
135         }
136
137         sprintf(y_or_n, "Really scrap %s [n]? ", bbuf);
138         if (!confirm(y_or_n))
139             return RET_FAIL;
140     }
141     while (nxtitem(&ni, &item)) {
142         if (!player->owner)
143             continue;
144
145         if (opt_MARKET) {
146             if (ontradingblock(type, (int *)&item.ship)) {
147                 pr("You cannot scrap an item on the trading block!\n");
148                 continue;
149             }
150         }
151
152         if (type == EF_SHIP) {
153             getsect(item.ship.shp_x, item.ship.shp_y, &sect);
154             if (sect.sct_type != SCT_HARBR)
155                 continue;
156             if (sect.sct_effic < 60 || sect.sct_own != player->cnum)
157                 continue;
158         } else if (type == EF_LAND) {
159             if (item.land.lnd_ship >= 0) {
160                 pr("%s is on a ship, and cannot be scrapped!\n",
161                    prland(&item.land));
162                 continue;
163             }
164             getsect(item.land.lnd_x, item.land.lnd_y, &sect);
165         } else {
166             getsect(item.plane.pln_x, item.plane.pln_y, &sect);
167             if (sect.sct_type != SCT_AIRPT)
168                 continue;
169             if (sect.sct_effic < 60 ||
170                 (sect.sct_own != player->cnum &&
171                  getrel(getnatp(sect.sct_own), player->cnum) < FRIENDLY))
172                 continue;
173         }
174         if (type == EF_SHIP) {
175             eff = ((float)item.ship.shp_effic / 100.0);
176             mp = &mchr[(int)item.ship.shp_type];
177             if (opt_TRADESHIPS) {
178                 if (mp->m_flags & M_TRADE) {
179                     pr("WARNING: You only collect money from trade ships if you \"scuttle\" them!\n");
180                     sprintf(prompt,
181                             "Are you really sure that you want to scrap %s (n)? ",
182                             prship(&item.ship));
183                     if (!confirm(prompt)) {
184                         pr("%s not scrapped\n", prship(&item.ship));
185                         continue;
186                     }
187                 }
188             }
189             pr("%s", prship(&item.ship));
190             for (i = I_NONE + 1; i <= I_MAX; i++) {
191                 sect.sct_item[i] += item.ship.shp_item[i];
192             }
193             sect.sct_item[I_LCM] += mp->m_lcm * 2 / 3 * eff;
194             sect.sct_item[I_HCM] += mp->m_hcm * 2 / 3 * eff;
195             getsect(item.ship.shp_x, item.ship.shp_y, &sect2);
196             snxtitem_all(&ni2, EF_PLANE);
197             while (nxtitem(&ni2, &plane)) {
198                 if (plane.pln_own == 0)
199                     continue;
200                 if (plane.pln_ship == item.ship.shp_uid) {
201                     wu(0, plane.pln_own,
202                        "Plane %d transferred off ship %d to %s\n",
203                        ni2.cur, item.ship.shp_uid,
204                        xyas(plane.pln_x, plane.pln_y, player->cnum));
205                     plane.pln_ship = -1;
206                     if (sect2.sct_own != plane.pln_own) {
207                         wu(0, plane.pln_own,
208                            "%s given to %s\n", prplane(&plane),
209                            cname(sect2.sct_own));
210                         wu(0, sect2.sct_own,
211                            "%s given to you by %s\n", prplane(&plane),
212                            cname(player->cnum));
213                     }
214                     makelost(EF_PLANE, plane.pln_own, plane.pln_uid,
215                              plane.pln_x, plane.pln_y);
216                     plane.pln_own = sect2.sct_own;
217                     makenotlost(EF_PLANE, plane.pln_own, plane.pln_uid,
218                                 plane.pln_x, plane.pln_y);
219                     putplane(plane.pln_uid, &plane);
220                 }
221             }
222             snxtitem_all(&ni2, EF_LAND);
223             while (nxtitem(&ni2, &land)) {
224                 if (land.lnd_own == 0)
225                     continue;
226                 if (land.lnd_ship == item.ship.shp_uid) {
227                     wu(0, land.lnd_own,
228                        "Land unit %d transferred off ship %d to %s\n",
229                        ni2.cur, item.ship.shp_uid,
230                        xyas(land.lnd_x, land.lnd_y, player->cnum));
231                     land.lnd_ship = -1;
232                     if (sect2.sct_own != land.lnd_own) {
233                         wu(0, land.lnd_own,
234                            "%s given to %s\n", prland(&land),
235                            cname(sect2.sct_own));
236                         wu(0, sect2.sct_own,
237                            "%s given to you by %s\n", prland(&land),
238                            cname(player->cnum));
239                     }
240                     makelost(EF_LAND, land.lnd_own, land.lnd_uid,
241                              land.lnd_x, land.lnd_y);
242                     land.lnd_own = sect2.sct_own;
243                     makenotlost(EF_LAND, land.lnd_own, land.lnd_uid,
244                                 land.lnd_x, land.lnd_y);
245                     putland(land.lnd_uid, &land);
246                 }
247             }
248             makelost(EF_SHIP, item.ship.shp_own, item.ship.shp_uid,
249                      item.ship.shp_x, item.ship.shp_y);
250             item.ship.shp_own = 0;
251             putship(item.ship.shp_uid, &item.ship);
252         } else if (type == EF_LAND) {
253             eff = ((float)item.land.lnd_effic / 100.0);
254             lp = &lchr[(int)item.land.lnd_type];
255             pr("%s", prland(&item.land));
256             for (i = I_NONE + 1; i <= I_MAX; i++) {
257                 sect.sct_item[i] += item.land.lnd_item[i];
258             }
259             sect.sct_item[I_LCM] += lp->l_lcm * 2 / 3 * eff;
260             sect.sct_item[I_HCM] += lp->l_hcm * 2 / 3 * eff;
261             getsect(item.land.lnd_x, item.land.lnd_y, &sect2);
262
263             snxtitem_all(&ni2, EF_LAND);
264             while (nxtitem(&ni2, &land)) {
265                 if (land.lnd_own == 0)
266                     continue;
267                 if (land.lnd_land == item.land.lnd_uid) {
268                     wu(0, land.lnd_own,
269                        "Land unit %d transferred off land unit %d to %s\n",
270                        land.lnd_uid, item.land.lnd_uid,
271                        xyas(land.lnd_x, land.lnd_y, player->cnum));
272                     land.lnd_land = -1;
273                     if (sect2.sct_own != land.lnd_own) {
274                         wu(0, land.lnd_own,
275                            "%s given to %s\n", prland(&land),
276                            cname(sect2.sct_own));
277                         wu(0, sect2.sct_own,
278                            "%s given to you by %s\n", prland(&land),
279                            cname(player->cnum));
280                     }
281                     makelost(EF_LAND, land.lnd_own, land.lnd_uid,
282                              land.lnd_x, land.lnd_y);
283                     land.lnd_own = sect2.sct_own;
284                     makenotlost(EF_LAND, land.lnd_own, land.lnd_uid,
285                                 land.lnd_x, land.lnd_y);
286                     putland(land.lnd_uid, &land);
287                 }
288             }
289
290             snxtitem_all(&ni2, EF_PLANE);
291             while (nxtitem(&ni2, &plane)) {
292                 if (plane.pln_own == 0)
293                     continue;
294                 if (plane.pln_land == item.land.lnd_uid) {
295                     wu(0, plane.pln_own,
296                        "Plane %d transferred off land unit %d to %s\n",
297                        ni2.cur, item.land.lnd_uid,
298                        xyas(plane.pln_x, plane.pln_y, player->cnum));
299                     plane.pln_land = -1;
300                     if (sect2.sct_own != plane.pln_own) {
301                         wu(0, plane.pln_own,
302                            "%s given to %s\n", prplane(&plane),
303                            cname(sect2.sct_own));
304                         wu(0, sect2.sct_own,
305                            "%s given to you by %s\n", prplane(&plane),
306                            cname(player->cnum));
307                     }
308                     makelost(EF_PLANE, plane.pln_own, plane.pln_uid,
309                              plane.pln_x, plane.pln_y);
310                     plane.pln_own = sect2.sct_own;
311                     makenotlost(EF_PLANE, plane.pln_own, plane.pln_uid,
312                                 plane.pln_x, plane.pln_y);
313                     putplane(plane.pln_uid, &plane);
314                 }
315             }
316             makelost(EF_LAND, item.land.lnd_own, item.land.lnd_uid,
317                      item.land.lnd_x, item.land.lnd_y);
318             item.land.lnd_own = 0;
319             putland(item.land.lnd_uid, &item.land);
320         } else {
321             eff = ((float)item.land.lnd_effic / 100.0);
322             pp = &plchr[(int)item.plane.pln_type];
323             pr("%s", prplane(&item.plane));
324             sect.sct_item[I_LCM] += pp->pl_lcm * 2 / 3 * eff;
325             sect.sct_item[I_HCM] += pp->pl_hcm * 2 / 3 * eff;
326             sect.sct_item[I_MILIT] += pp->pl_crew;
327             makelost(EF_PLANE, item.plane.pln_own, item.plane.pln_uid,
328                      item.plane.pln_x, item.plane.pln_y);
329             item.plane.pln_own = 0;
330             putplane(item.plane.pln_uid, &item.plane);
331         }
332         pr(" scrapped in %s\n",
333            xyas(sect.sct_x, sect.sct_y, player->cnum));
334         for (i = I_NONE + 1; i <= I_MAX; i++) {
335             if (sect.sct_item[i] > ITEM_MAX)
336                 sect.sct_item[i] = ITEM_MAX;
337         }
338         putsect(&sect);
339     }
340     return RET_OK;
341 }