]> 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-2010, 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  *     Markus Armbruster, 2004-2009
33  */
34
35 #include <config.h>
36
37 #include <ctype.h>
38 #include "commands.h"
39 #include "empobj.h"
40 #include "optlist.h"
41 #include "unit.h"
42
43 int
44 scra(void)
45 {
46     struct nstr_item ni;
47     union empobj_storage item;
48     int type;
49     struct sctstr sect;
50     struct mchrstr *mp;
51     struct plchrstr *pp;
52     struct lchrstr *lp;
53     char *p;
54     i_type i;
55     char prompt[128];
56     char buf[1024];
57     float eff;
58
59     if (!(p = getstarg(player->argp[1], "Ship, land, or plane? ", buf)))
60         return RET_SYN;
61     switch (*p) {
62     case 's':
63         type = EF_SHIP;
64         break;
65     case 'p':
66         type = EF_PLANE;
67         break;
68     case 'l':
69         type = EF_LAND;
70         break;
71     default:
72         pr("Ships, land units, or planes only! (s, l, p)\n");
73         return RET_SYN;
74     }
75     sprintf(prompt, "%s(s)? ", ef_nameof(type));
76     if (!(p = getstarg(player->argp[2], prompt, buf)))
77         return RET_SYN;
78     if (!snxtitem(&ni, type, p, NULL))
79         return RET_SYN;
80     if (p && (isalpha(*p) || (*p == '*') || (*p == '~') || issector(p)
81               || islist(p))) {
82         char y_or_n[80], bbuf[80];
83
84         memset(y_or_n, 0, sizeof(y_or_n));
85         if (type == EF_SHIP) {
86             if (*p == '*')
87                 sprintf(bbuf, "all ships");
88             else if (*p == '~')
89                 sprintf(bbuf, "all unassigned ships");
90             else if (issector(p))
91                 sprintf(bbuf, "all ships in %s", p);
92             else if (isalpha(*p))
93                 sprintf(bbuf, "fleet %c", *p);
94             else
95                 sprintf(bbuf, "ships %s", p);
96         } else if (type == EF_LAND) {
97             if (*p == '*')
98                 sprintf(bbuf, "all land units");
99             else if (*p == '~')
100                 sprintf(bbuf, "all unassigned land units");
101             else if (issector(p))
102                 sprintf(bbuf, "all units in %s", p);
103             else if (isalpha(*p))
104                 sprintf(bbuf, "army %c", *p);
105             else
106                 sprintf(bbuf, "units %s", p);
107         } else {
108             if (*p == '*')
109                 sprintf(bbuf, "all planes");
110             else if (*p == '~')
111                 sprintf(bbuf, "all unassigned planes");
112             else if (issector(p))
113                 sprintf(bbuf, "all planes in %s", p);
114             else if (isalpha(*p))
115                 sprintf(bbuf, "wing %c", *p);
116             else
117                 sprintf(bbuf, "planes %s", p);
118         }
119
120         sprintf(y_or_n, "Really scrap %s [n]? ", bbuf);
121         if (!confirm(y_or_n))
122             return RET_FAIL;
123     }
124     while (nxtitem(&ni, &item)) {
125         if (!player->owner)
126             continue;
127
128         if (opt_MARKET) {
129             if (ontradingblock(type, &item.ship)) {
130                 pr("You cannot scrap an item on the trading block!\n");
131                 continue;
132             }
133         }
134
135         getsect(item.gen.x, item.gen.y, &sect);
136         if (type == EF_SHIP) {
137             if (!player->owner
138                 && getrel(getnatp(sect.sct_own), player->cnum) < FRIENDLY) {
139                 pr("%s is not in a friendly harbor!\n",
140                    prship(&item.ship));
141                 continue;
142             }
143             if (sect.sct_type != SCT_HARBR || sect.sct_effic < 60) {
144                 pr("%s is not in a 60%% efficient harbor!\n",
145                    prship(&item.ship));
146                 continue;
147             }
148             if (opt_TRADESHIPS
149                 && (mchr[item.ship.shp_type].m_flags & M_TRADE)) {
150                 pr("WARNING: You only collect money from trade ships if you \"scuttle\" them!\n");
151                 sprintf(prompt,
152                         "Are you really sure that you want to scrap %s (n)? ",
153                         prship(&item.ship));
154                 if (!confirm(prompt)) {
155                     pr("%s not scrapped\n", prship(&item.ship));
156                     continue;
157                 }
158             }
159         } else {
160             if (!player->owner
161                 && getrel(getnatp(sect.sct_own), player->cnum) != ALLIED) {
162                 pr("%s is not in an allied sector!\n",
163                    obj_nameof(&item.gen));
164                 continue;
165             }
166             if (type == EF_PLANE
167                 && (sect.sct_type != SCT_AIRPT || sect.sct_effic < 60)) {
168                 pr("%s is not in a 60%% efficient airfield!\n",
169                    prplane(&item.plane));
170                 continue;
171             }
172         }
173
174         pr("%s scrapped in %s\n",
175            obj_nameof(&item.gen),
176            xyas(item.gen.x, item.gen.y, player->cnum));
177         unit_drop_cargo(&item.gen, sect.sct_own);
178         if (type == EF_SHIP) {
179             eff = item.ship.shp_effic / 100.0;
180             mp = &mchr[(int)item.ship.shp_type];
181             for (i = I_NONE + 1; i <= I_MAX; i++) {
182                 sect.sct_item[i] += item.ship.shp_item[i];
183             }
184             sect.sct_item[I_LCM] += mp->m_lcm * 2 / 3 * eff;
185             sect.sct_item[I_HCM] += mp->m_hcm * 2 / 3 * eff;
186         } else if (type == EF_LAND) {
187             eff = item.land.lnd_effic / 100.0;
188             lp = &lchr[(int)item.land.lnd_type];
189             for (i = I_NONE + 1; i <= I_MAX; i++) {
190                 sect.sct_item[i] += item.land.lnd_item[i];
191             }
192             sect.sct_item[I_LCM] += lp->l_lcm * 2 / 3 * eff;
193             sect.sct_item[I_HCM] += lp->l_hcm * 2 / 3 * eff;
194         } else {
195             eff = item.land.lnd_effic / 100.0;
196             pp = &plchr[(int)item.plane.pln_type];
197             sect.sct_item[I_LCM] += pp->pl_lcm * 2 / 3 * eff;
198             sect.sct_item[I_HCM] += pp->pl_hcm * 2 / 3 * eff;
199             sect.sct_item[I_MILIT] += pp->pl_crew;
200         }
201         item.gen.effic = 0;
202         put_empobj(type, item.gen.uid, &item.gen);
203         for (i = I_NONE + 1; i <= I_MAX; i++) {
204             if (sect.sct_item[i] > ITEM_MAX)
205                 sect.sct_item[i] = ITEM_MAX;
206         }
207         putsect(&sect);
208     }
209     return RET_OK;
210 }