]> git.pond.sub.org Git - empserver/blob - src/lib/commands/scra.c
c15fb6236559da3baf9c4b0f1d14d01b72055853
[empserver] / src / lib / commands / scra.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2013, 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-2012
32  */
33
34 #include <config.h>
35
36 #include <ctype.h>
37 #include "commands.h"
38 #include "optlist.h"
39 #include "plague.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, n;
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
75     if (!snxtitem(&ni, type, player->argp[2], NULL))
76         return RET_SYN;
77     n = 0;
78     while (nxtitem(&ni, &item)) {
79         if (!player->owner)
80             continue;
81         n++;
82     }
83     snprintf(prompt, sizeof(prompt), "Really scrap %d %s%s [n]? ",
84                             n, ef_nameof(type), splur(n));
85     if (!confirm(prompt))
86         return RET_FAIL;
87
88     snxtitem_rewind(&ni);
89     while (nxtitem(&ni, &item)) {
90         if (!player->owner)
91             continue;
92
93         if (opt_MARKET) {
94             if (ontradingblock(type, &item.ship)) {
95                 pr("You cannot scrap an item on the trading block!\n");
96                 continue;
97             }
98         }
99
100         getsect(item.gen.x, item.gen.y, &sect);
101         if (type == EF_SHIP) {
102             if (!player->owner
103                 && relations_with(sect.sct_own, player->cnum) < FRIENDLY) {
104                 pr("%s is not in a friendly harbor!\n",
105                    prship(&item.ship));
106                 continue;
107             }
108             if (sect.sct_type != SCT_HARBR || sect.sct_effic < 60) {
109                 pr("%s is not in a 60%% efficient harbor!\n",
110                    prship(&item.ship));
111                 continue;
112             }
113             if (mchr[item.ship.shp_type].m_flags & M_TRADE) {
114                 pr("WARNING: You only collect money from trade ships if you \"scuttle\" them!\n");
115                 sprintf(prompt,
116                         "Are you really sure that you want to scrap %s (n)? ",
117                         prship(&item.ship));
118                 if (!confirm(prompt)) {
119                     pr("%s not scrapped\n", prship(&item.ship));
120                     continue;
121                 }
122             }
123         } else {
124             if (!player->owner
125                 && relations_with(sect.sct_own, player->cnum) != ALLIED) {
126                 pr("%s is not in an allied sector!\n",
127                    unit_nameof(&item.gen));
128                 continue;
129             }
130             if (type == EF_PLANE
131                 && (sect.sct_type != SCT_AIRPT || sect.sct_effic < 60)) {
132                 pr("%s is not in a 60%% efficient airfield!\n",
133                    prplane(&item.plane));
134                 continue;
135             }
136         }
137
138         pr("%s scrapped in %s\n",
139            unit_nameof(&item.gen),
140            xyas(item.gen.x, item.gen.y, player->cnum));
141         unit_drop_cargo(&item.gen, sect.sct_own);
142         if (type == EF_SHIP) {
143             eff = item.ship.shp_effic / 100.0;
144             mp = &mchr[(int)item.ship.shp_type];
145             for (i = I_NONE + 1; i <= I_MAX; i++) {
146                 if (load_comm_ok(&sect, item.ship.shp_own, i,
147                                  -item.ship.shp_item[i]))
148                     sect.sct_item[i] += item.ship.shp_item[i];
149             }
150             sect.sct_item[I_LCM] += mp->m_lcm * 2 / 3 * eff;
151             sect.sct_item[I_HCM] += mp->m_hcm * 2 / 3 * eff;
152             if (item.ship.shp_pstage == PLG_INFECT
153                 && sect.sct_pstage == PLG_HEALTHY)
154                 sect.sct_pstage = PLG_EXPOSED;
155         } else if (type == EF_LAND) {
156             eff = item.land.lnd_effic / 100.0;
157             lp = &lchr[(int)item.land.lnd_type];
158             for (i = I_NONE + 1; i <= I_MAX; i++) {
159                 if (load_comm_ok(&sect, item.land.lnd_own, i,
160                                  -item.land.lnd_item[i]))
161                     sect.sct_item[i] += item.land.lnd_item[i];
162             }
163             sect.sct_item[I_LCM] += lp->l_lcm * 2 / 3 * eff;
164             sect.sct_item[I_HCM] += lp->l_hcm * 2 / 3 * eff;
165             if (item.land.lnd_pstage == PLG_INFECT
166                 && sect.sct_pstage == PLG_HEALTHY)
167                 sect.sct_pstage = PLG_EXPOSED;
168         } else {
169             eff = item.land.lnd_effic / 100.0;
170             pp = &plchr[(int)item.plane.pln_type];
171             sect.sct_item[I_LCM] += pp->pl_lcm * 2 / 3 * eff;
172             sect.sct_item[I_HCM] += pp->pl_hcm * 2 / 3 * eff;
173             sect.sct_item[I_MILIT] += pp->pl_crew;
174         }
175         item.gen.effic = 0;
176         put_empobj(type, item.gen.uid, &item.gen);
177         for (i = I_NONE + 1; i <= I_MAX; i++) {
178             if (sect.sct_item[i] > ITEM_MAX)
179                 sect.sct_item[i] = ITEM_MAX;
180         }
181         putsect(&sect);
182     }
183     return RET_OK;
184 }