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