]> git.pond.sub.org Git - empserver/blob - src/lib/commands/scut.c
46a582c533b9459077e2ab12c0547aaa420e3596
[empserver] / src / lib / commands / scut.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  *  scut.c: Scuttle ships, planes or land units
28  *
29  *  Known contributors to this file:
30  *     Markus Armbruster, 2004-2011
31  */
32
33 #include <config.h>
34
35 #include <ctype.h>
36 #include "commands.h"
37 #include "news.h"
38 #include "optlist.h"
39 #include "unit.h"
40
41 int
42 scut(void)
43 {
44     struct nstr_item ni;
45     union empobj_storage item;
46     int type;
47     struct mchrstr *mp;
48     char *p;
49     char prompt[128];
50     char buf[1024];
51     struct sctstr sect;
52
53     if (!(p = getstarg(player->argp[1], "Ship, land, or plane? ", buf)))
54         return RET_SYN;
55     switch (*p) {
56     case 's':
57         type = EF_SHIP;
58         break;
59     case 'p':
60         type = EF_PLANE;
61         break;
62     case 'l':
63         type = EF_LAND;
64         break;
65     default:
66         pr("Ships, land units, or planes only! (s, l, p)\n");
67         return RET_SYN;
68     }
69     sprintf(prompt, "%s(s)? ", ef_nameof(type));
70     if (!(p = getstarg(player->argp[2], prompt, buf)))
71         return RET_SYN;
72     if (!snxtitem(&ni, type, p, NULL))
73         return RET_SYN;
74     if (p && (isalpha(*p) || (*p == '*') || (*p == '~') || issector(p)
75               || islist(p))) {
76         char y_or_n[80], bbuf[80];
77
78         if (type == EF_SHIP) {
79             if (*p == '*')
80                 sprintf(bbuf, "all ships");
81             else if (*p == '~')
82                 sprintf(bbuf, "all unassigned ships");
83             else if (issector(p))
84                 sprintf(bbuf, "all ships in %s", p);
85             else if (isalpha(*p))
86                 sprintf(bbuf, "fleet %c", *p);
87             else
88                 sprintf(bbuf, "ships %s", p);
89         } else if (type == EF_LAND) {
90             if (*p == '*')
91                 sprintf(bbuf, "all land units");
92             else if (*p == '~')
93                 sprintf(bbuf, "all unassigned land units");
94             else if (issector(p))
95                 sprintf(bbuf, "all units in %s", p);
96             else if (isalpha(*p))
97                 sprintf(bbuf, "army %c", *p);
98             else
99                 sprintf(bbuf, "units %s", p);
100         } else {
101             if (*p == '*')
102                 sprintf(bbuf, "all planes");
103             else if (*p == '~')
104                 sprintf(bbuf, "all unassigned planes");
105             else if (issector(p))
106                 sprintf(bbuf, "all planes in %s", p);
107             else if (isalpha(*p))
108                 sprintf(bbuf, "wing %c", *p);
109             else
110                 sprintf(bbuf, "planes %s", p);
111         }
112         sprintf(y_or_n, "Really scuttle %s? ", bbuf);
113         if (!confirm(y_or_n))
114             return RET_FAIL;
115     }
116     while (nxtitem(&ni, &item)) {
117         if (!player->owner)
118             continue;
119         if (opt_MARKET) {
120             if (ontradingblock(type, &item.ship)) {
121                 pr("You cannot scuttle an item on the trading block!\n");
122                 continue;
123             }
124         }
125
126         if (type == EF_SHIP) {
127             mp = &mchr[(int)item.ship.shp_type];
128             if (mp->m_flags & M_TRADE) {
129                 if (!scuttle_tradeship(&item.ship, 1))
130                     continue;
131             }
132         } else if (type == EF_LAND) {
133             if (item.land.lnd_ship >= 0) {
134                 pr("%s is on a ship, and cannot be scuttled!\n",
135                    prland(&item.land));
136                 continue;
137             }
138         }
139         pr("%s scuttled in %s\n",
140            unit_nameof(&item.gen),
141            xyas(item.gen.x, item.gen.y, player->cnum));
142         getsect(item.gen.x, item.gen.y, &sect);
143         if (sect.sct_own == item.gen.own)
144             unit_drop_cargo(&item.gen, sect.sct_own);
145         item.gen.effic = 0;
146         put_empobj(type, item.gen.uid, &item.gen);
147     }
148
149     return RET_OK;
150 }
151
152 int
153 scuttle_tradeship(struct shpstr *sp, int interactive)
154 {
155     float cash = 0;
156     float ally_cash = 0;
157     int dist = -1;
158     struct sctstr sect;
159     struct mchrstr *mp;
160     struct natstr *np;
161     char buf[512];
162     struct natstr *natp;
163
164     mp = &mchr[(int)sp->shp_type];
165     getsect(sp->shp_x, sp->shp_y, &sect);
166     if (sect.sct_own && sect.sct_type == SCT_HARBR && sect.sct_effic >= 2
167         && relations_with(sect.sct_own, sp->shp_own) >= FRIENDLY) {
168         dist = mapdist(sp->shp_x, sp->shp_y,
169                        sp->shp_orig_x, sp->shp_orig_y);
170         /* Don't disclose distance to to pirates */
171         if (sp->shp_own == sp->shp_orig_own)
172             mpr(sp->shp_own, "%s has gone %d sects\n", prship(sp), dist);
173         if (dist < trade_1_dist)
174             cash = 0;
175         else if (dist < trade_2_dist)
176             cash = 1.0 + trade_1 * dist;
177         else if (dist < trade_3_dist)
178             cash = 1.0 + trade_2 * dist;
179         else
180             cash = 1.0 + trade_3 * dist;
181         cash *= mp->m_cost;
182         cash *= sp->shp_effic / 100.0;
183
184         if (sect.sct_own != sp->shp_own) {
185             ally_cash = cash * trade_ally_cut;
186             cash *= (1.0 + trade_ally_bonus);
187         }
188     }
189
190     if (!cash && (dist < 0 || sp->shp_own == sp->shp_orig_own)) {
191         if (interactive) {
192             pr("You won't get any money if you scuttle in %s!",
193                xyas(sp->shp_x, sp->shp_y, player->cnum));
194             sprintf(buf, "Are you sure you want to scuttle %s? ", prship(sp));
195             return confirm(buf);
196         } else
197             return 0;
198     }
199
200     if (interactive) {
201         player->dolcost -= cash;
202     } else {
203         natp = getnatp(sp->shp_own);
204         natp->nat_money += cash;
205         putnat(natp);
206         wu(0, sp->shp_own, "You just made $%d.\n", (int)cash);
207     }
208
209     if (ally_cash) {
210         np = getnatp(sect.sct_own);
211         np->nat_money += ally_cash;
212         putnat(np);
213         wu(0, sect.sct_own,
214            "Trade with %s nets you $%d at %s\n",
215            cname(sp->shp_own),
216            (int)ally_cash, xyas(sect.sct_x, sect.sct_y, sect.sct_own));
217         if (sp->shp_own != sp->shp_orig_own)
218             nreport(sp->shp_own, N_PIRATE_TRADE, sp->shp_orig_own, 1);
219         else
220             nreport(sp->shp_own, N_TRADE, sect.sct_own, 1);
221     } else if (sp->shp_own != sp->shp_orig_own)
222         nreport(sp->shp_own, N_PIRATE_KEEP, sp->shp_orig_own, 1);
223
224     return 1;
225 }