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