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