]> git.pond.sub.org Git - empserver/blob - src/lib/commands/scra.c
e133477f2936c2146cfd0c546288498b306fb0b5
[empserver] / src / lib / commands / scra.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2017, 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-2016
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     short *mvec;
58     int amt;
59
60     if (!(p = getstarg(player->argp[1], "Ship, land, or plane? ", buf)))
61         return RET_SYN;
62     switch (*p) {
63     case 's':
64         type = EF_SHIP;
65         break;
66     case 'p':
67         type = EF_PLANE;
68         break;
69     case 'l':
70         type = EF_LAND;
71         break;
72     default:
73         pr("Ships, land units, or planes only! (s, l, p)\n");
74         return RET_SYN;
75     }
76
77     if (!snxtitem(&ni, type, player->argp[2], NULL))
78         return RET_SYN;
79     n = 0;
80     while (nxtitem(&ni, &item)) {
81         if (!player->owner)
82             continue;
83         n++;
84     }
85     snprintf(prompt, sizeof(prompt), "Really scrap %d %s%s [n]? ",
86                             n, ef_nameof(type), splur(n));
87     if (!confirm(prompt))
88         return RET_FAIL;
89
90     snxtitem_rewind(&ni);
91     while (nxtitem(&ni, &item)) {
92         if (!player->owner)
93             continue;
94
95         if (opt_MARKET) {
96             if (ontradingblock(type, &item.ship)) {
97                 pr("You cannot scrap an item on the trading block!\n");
98                 continue;
99             }
100         }
101
102         getsect(item.gen.x, item.gen.y, &sect);
103         if (type == EF_SHIP) {
104             if (!player->owner
105                 && relations_with(sect.sct_own, player->cnum) < FRIENDLY) {
106                 pr("%s is not in a friendly harbor!\n",
107                    prship(&item.ship));
108                 continue;
109             }
110             if (sect.sct_type != SCT_HARBR || sect.sct_effic < 60) {
111                 pr("%s is not in a 60%% efficient harbor!\n",
112                    prship(&item.ship));
113                 continue;
114             }
115             if (mchr[item.ship.shp_type].m_flags & M_TRADE) {
116                 pr("WARNING: You only collect money from trade ships if you \"scuttle\" them!\n");
117                 sprintf(prompt,
118                         "Are you really sure that you want to scrap %s (n)? ",
119                         prship(&item.ship));
120                 if (!confirm(prompt)) {
121                     pr("%s not scrapped\n", prship(&item.ship));
122                     continue;
123                 }
124             }
125         } else {
126             if (!player->owner
127                 && relations_with(sect.sct_own, player->cnum) != ALLIED) {
128                 pr("%s is not in an allied sector!\n",
129                    unit_nameof(&item.gen));
130                 continue;
131             }
132             if (type == EF_PLANE
133                 && (sect.sct_type != SCT_AIRPT || sect.sct_effic < 60)) {
134                 pr("%s is not in a 60%% efficient airfield!\n",
135                    prplane(&item.plane));
136                 continue;
137             }
138         }
139
140         pr("%s scrapped in %s\n",
141            unit_nameof(&item.gen),
142            xyas(item.gen.x, item.gen.y, player->cnum));
143         unit_drop_cargo(&item.gen, sect.sct_own);
144         if (type == EF_SHIP) {
145             eff = item.ship.shp_effic / 100.0;
146             mp = &mchr[(int)item.ship.shp_type];
147             for (i = I_NONE + 1; i <= I_MAX; i++) {
148                 if (load_comm_ok(&sect, item.ship.shp_own, i,
149                                  -item.ship.shp_item[i]))
150                     sect.sct_item[i] += item.ship.shp_item[i];
151             }
152             mvec = mp->m_mat;
153             if (item.ship.shp_pstage == PLG_INFECT
154                 && sect.sct_pstage == PLG_HEALTHY)
155                 sect.sct_pstage = PLG_EXPOSED;
156         } else if (type == EF_LAND) {
157             eff = item.land.lnd_effic / 100.0;
158             lp = &lchr[(int)item.land.lnd_type];
159             for (i = I_NONE + 1; i <= I_MAX; i++) {
160                 if (load_comm_ok(&sect, item.land.lnd_own, i,
161                                  -item.land.lnd_item[i]))
162                     sect.sct_item[i] += item.land.lnd_item[i];
163             }
164             mvec = lp->l_mat;
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             mvec = pp->pl_mat;
172         }
173         item.gen.effic = 0;
174         put_empobj(type, item.gen.uid, &item.gen);
175         for (i = I_NONE + 1; i <= I_MAX; i++) {
176             if (i == I_CIVIL || i == I_MILIT || i == I_UW)
177                 amt = sect.sct_item[i] + mvec[i] * eff;
178             else
179                 amt = sect.sct_item[i] + mvec[i] * 2 / 3 * eff;
180             if (amt > ITEM_MAX)
181                 amt = ITEM_MAX;
182             sect.sct_item[i] = amt;
183         }
184         putsect(&sect);
185     }
186     return RET_OK;
187 }