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