]> git.pond.sub.org Git - empserver/blob - src/lib/commands/scra.c
(scra): Work directly on item arrays instead of copies made by
[empserver] / src / lib / commands / scra.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  *  scra.c: Scrap ships, planes or land units
29  * 
30  *  Known contributors to this file:
31  *     Steve McClure, 2000
32  *     
33  */
34
35 #include "misc.h"
36 #include "player.h"
37 #include "var.h"
38 #include "xy.h"
39 #include "sect.h"
40 #include "ship.h"
41 #include "plane.h"
42 #include "land.h"
43 #include "nat.h"
44 #include "nsc.h"
45 #include "file.h"
46 #include "commands.h"
47 #include "optlist.h"
48
49 union item_u {
50     struct shpstr ship;
51     struct plnstr plane;
52     struct lndstr land;
53 };
54
55 int
56 scra(void)
57 {
58     struct nstr_item ni;
59     union item_u item;
60     int type;
61     struct sctstr sect;
62     struct mchrstr *mp;
63     struct plchrstr *pp;
64     struct lchrstr *lp;
65     s_char *p;
66     int i;
67     struct nstr_item ni2;
68     struct plnstr plane;
69     struct lndstr land;
70     struct sctstr sect2;
71     s_char prompt[128];
72     s_char buf[1024];
73     float eff;
74
75     if (!(p = getstarg(player->argp[1], "Ship, land, or plane? ", buf)))
76         return RET_SYN;
77     switch (*p) {
78     case 's':
79         type = EF_SHIP;
80         break;
81     case 'p':
82         type = EF_PLANE;
83         break;
84     case 'l':
85         type = EF_LAND;
86         break;
87     default:
88         pr("Ships, land units, or planes only! (s, l, p)\n");
89         return RET_SYN;
90     }
91     sprintf(prompt, "%s(s)? ", ef_nameof(type));
92     if ((p = getstarg(player->argp[2], prompt, buf)) == 0)
93         return RET_SYN;
94     if (!snxtitem(&ni, type, p))
95         return RET_SYN;
96     if (p && (isalpha(*p) || (*p == '*') || (*p == '~') || issector(p)
97               || islist(p))) {
98         s_char y_or_n[80], bbuf[80];
99
100         memset(y_or_n, 0, sizeof(y_or_n));
101         if (type == EF_SHIP) {
102             if (*p == '*')
103                 sprintf(bbuf, "all ships");
104             else if (*p == '~')
105                 sprintf(bbuf, "all unassigned ships");
106             else if (issector(p))
107                 sprintf(bbuf, "all ships in %s", p);
108             else if (isalpha(*p))
109                 sprintf(bbuf, "fleet %c", *p);
110             else
111                 sprintf(bbuf, "ships %s", p);
112         } else if (type == EF_LAND) {
113             if (*p == '*')
114                 sprintf(bbuf, "all land units");
115             else if (*p == '~')
116                 sprintf(bbuf, "all unassigned land units");
117             else if (issector(p))
118                 sprintf(bbuf, "all units in %s", p);
119             else if (isalpha(*p))
120                 sprintf(bbuf, "army %c", *p);
121             else
122                 sprintf(bbuf, "units %s", p);
123         } else {
124             if (*p == '*')
125                 sprintf(bbuf, "all planes");
126             else if (*p == '~')
127                 sprintf(bbuf, "all unassigned planes");
128             else if (issector(p))
129                 sprintf(bbuf, "all planes in %s", p);
130             else if (isalpha(*p))
131                 sprintf(bbuf, "wing %c", *p);
132             else
133                 sprintf(bbuf, "planes %s", p);
134         }
135
136         sprintf(y_or_n, "Really scrap %s [n]? ", bbuf);
137         if (!confirm(y_or_n))
138             return RET_FAIL;
139     }
140     while (nxtitem(&ni, (s_char *)&item)) {
141         if (!player->owner)
142             continue;
143
144         if (opt_MARKET) {
145             if (ontradingblock(type, (int *)&item.ship)) {
146                 pr("You cannot scrap an item on the trading block!\n");
147                 continue;
148             }
149         }
150
151         if (type == EF_SHIP) {
152             getsect(item.ship.shp_x, item.ship.shp_y, &sect);
153             if (sect.sct_type != SCT_HARBR)
154                 continue;
155             if (sect.sct_effic < 60 || sect.sct_own != player->cnum)
156                 continue;
157         } else if (type == EF_LAND) {
158             if (item.land.lnd_ship >= 0) {
159                 pr("%s is on a ship, and cannot be scrapped!\n",
160                    prland(&item.land));
161                 continue;
162             }
163             getsect(item.land.lnd_x, item.land.lnd_y, &sect);
164         } else {
165             getsect(item.plane.pln_x, item.plane.pln_y, &sect);
166             if (sect.sct_type != SCT_AIRPT)
167                 continue;
168             if (sect.sct_effic < 60 ||
169                 (sect.sct_own != player->cnum &&
170                  getrel(getnatp(sect.sct_own), player->cnum) < FRIENDLY))
171                 continue;
172         }
173         if (type == EF_SHIP) {
174             eff = ((float)item.ship.shp_effic / 100.0);
175             mp = &mchr[(int)item.ship.shp_type];
176             if (opt_TRADESHIPS) {
177                 if (mp->m_flags & M_TRADE) {
178                     pr("WARNING: You only collect money from trade ships if you \"scuttle\" them!\n");
179                     sprintf(prompt,
180                             "Are you really sure that you want to scrap %s (n)? ",
181                             prship(&item.ship));
182                     if (!confirm(prompt)) {
183                         pr("%s not scrapped\n", prship(&item.ship));
184                         continue;
185                     }
186                 }
187             }
188             pr("%s", prship(&item.ship));
189             /* FIXME obey ITEM_MAX */
190             for (i = 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, (s_char *)&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, (s_char *)&plane);
220                 }
221             }
222             snxtitem_all(&ni2, EF_LAND);
223             while (nxtitem(&ni2, (s_char *)&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, (s_char *)&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, (s_char *)&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             /* FIXME obey ITEM_MAX */
257             for (i = 1; i <= I_MAX; i++) {
258                 sect.sct_item[i] += item.land.lnd_item[i];
259             }
260             sect.sct_item[I_LCM] += lp->l_lcm * 2 / 3 * eff;
261             sect.sct_item[I_HCM] += lp->l_hcm * 2 / 3 * eff;
262             getsect(item.land.lnd_x, item.land.lnd_y, &sect2);
263
264             snxtitem_all(&ni2, EF_LAND);
265             while (nxtitem(&ni2, (s_char *)&land)) {
266                 if (land.lnd_own == 0)
267                     continue;
268                 if (land.lnd_land == item.land.lnd_uid) {
269                     wu(0, land.lnd_own,
270                        "Land unit %d transferred off land unit %d to %s\n",
271                        land.lnd_uid, item.land.lnd_uid,
272                        xyas(land.lnd_x, land.lnd_y, player->cnum));
273                     land.lnd_land = -1;
274                     if (sect2.sct_own != land.lnd_own) {
275                         wu(0, land.lnd_own,
276                            "%s given to %s\n", prland(&land),
277                            cname(sect2.sct_own));
278                         wu(0, sect2.sct_own,
279                            "%s given to you by %s\n", prland(&land),
280                            cname(player->cnum));
281                     }
282                     makelost(EF_LAND, land.lnd_own, land.lnd_uid,
283                              land.lnd_x, land.lnd_y);
284                     land.lnd_own = sect2.sct_own;
285                     makenotlost(EF_LAND, land.lnd_own, land.lnd_uid,
286                                 land.lnd_x, land.lnd_y);
287                     putland(land.lnd_uid, (s_char *)&land);
288                 }
289             }
290
291             snxtitem_all(&ni2, EF_PLANE);
292             while (nxtitem(&ni2, (s_char *)&plane)) {
293                 if (plane.pln_own == 0)
294                     continue;
295                 if (plane.pln_land == item.land.lnd_uid) {
296                     wu(0, plane.pln_own,
297                        "Plane %d transferred off land unit %d to %s\n",
298                        ni2.cur, item.land.lnd_uid,
299                        xyas(plane.pln_x, plane.pln_y, player->cnum));
300                     plane.pln_land = -1;
301                     if (sect2.sct_own != plane.pln_own) {
302                         wu(0, plane.pln_own,
303                            "%s given to %s\n", prplane(&plane),
304                            cname(sect2.sct_own));
305                         wu(0, sect2.sct_own,
306                            "%s given to you by %s\n", prplane(&plane),
307                            cname(player->cnum));
308                     }
309                     makelost(EF_PLANE, plane.pln_own, plane.pln_uid,
310                              plane.pln_x, plane.pln_y);
311                     plane.pln_own = sect2.sct_own;
312                     makenotlost(EF_PLANE, plane.pln_own, plane.pln_uid,
313                                 plane.pln_x, plane.pln_y);
314                     putplane(plane.pln_uid, (s_char *)&plane);
315                 }
316             }
317             makelost(EF_LAND, item.land.lnd_own, item.land.lnd_uid,
318                      item.land.lnd_x, item.land.lnd_y);
319             item.land.lnd_own = 0;
320             putland(item.land.lnd_uid, (s_char *)&item.land);
321         } else {
322             eff = ((float)item.land.lnd_effic / 100.0);
323             pp = &plchr[(int)item.plane.pln_type];
324             pr("%s", prplane(&item.plane));
325             /* FIXME obey ITEM_MAX */
326             sect.sct_item[I_LCM] += pp->pl_lcm * 2 / 3 * eff;
327             sect.sct_item[I_HCM] += pp->pl_hcm * 2 / 3 * eff;
328             sect.sct_item[I_MILIT] += pp->pl_crew;
329             makelost(EF_PLANE, item.plane.pln_own, item.plane.pln_uid,
330                      item.plane.pln_x, item.plane.pln_y);
331             item.plane.pln_own = 0;
332             putplane(item.plane.pln_uid, (s_char *)&item.plane);
333         }
334         pr(" scrapped in %s\n",
335            xyas(sect.sct_x, sect.sct_y, player->cnum));
336         putsect(&sect);
337     }
338     return RET_OK;
339 }