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