]> git.pond.sub.org Git - empserver/blob - src/lib/commands/scut.c
0c0739cce992cccd6985e6c28a0807a6e6e6f3cc
[empserver] / src / lib / commands / scut.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2010, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program 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 2 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, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  scut.c: Scuttle ships, planes or land units
29  *
30  *  Known contributors to this file:
31  *     Markus Armbruster, 2004-2009
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 scut(void)
44 {
45     struct nstr_item ni;
46     union empobj_storage item;
47     int type;
48     struct mchrstr *mp;
49     char *p;
50     char prompt[128];
51     char buf[1024];
52     struct sctstr sect;
53
54     if (!(p = getstarg(player->argp[1], "Ship, land, or plane? ", buf)))
55         return RET_SYN;
56     switch (*p) {
57     case 's':
58         type = EF_SHIP;
59         break;
60     case 'p':
61         type = EF_PLANE;
62         break;
63     case 'l':
64         type = EF_LAND;
65         break;
66     default:
67         pr("Ships, land units, or planes only! (s, l, p)\n");
68         return RET_SYN;
69     }
70     sprintf(prompt, "%s(s)? ", ef_nameof(type));
71     if (!(p = getstarg(player->argp[2], prompt, buf)))
72         return RET_SYN;
73     if (!snxtitem(&ni, type, p, NULL))
74         return RET_SYN;
75     if (p && (isalpha(*p) || (*p == '*') || (*p == '~') || issector(p)
76               || islist(p))) {
77         char y_or_n[80], bbuf[80];
78
79         if (type == EF_SHIP) {
80             if (*p == '*')
81                 sprintf(bbuf, "all ships");
82             else if (*p == '~')
83                 sprintf(bbuf, "all unassigned ships");
84             else if (issector(p))
85                 sprintf(bbuf, "all ships in %s", p);
86             else if (isalpha(*p))
87                 sprintf(bbuf, "fleet %c", *p);
88             else
89                 sprintf(bbuf, "ships %s", p);
90         } else if (type == EF_LAND) {
91             if (*p == '*')
92                 sprintf(bbuf, "all land units");
93             else if (*p == '~')
94                 sprintf(bbuf, "all unassigned land units");
95             else if (issector(p))
96                 sprintf(bbuf, "all units in %s", p);
97             else if (isalpha(*p))
98                 sprintf(bbuf, "army %c", *p);
99             else
100                 sprintf(bbuf, "units %s", p);
101         } else {
102             if (*p == '*')
103                 sprintf(bbuf, "all planes");
104             else if (*p == '~')
105                 sprintf(bbuf, "all unassigned planes");
106             else if (issector(p))
107                 sprintf(bbuf, "all planes in %s", p);
108             else if (isalpha(*p))
109                 sprintf(bbuf, "wing %c", *p);
110             else
111                 sprintf(bbuf, "planes %s", p);
112         }
113         sprintf(y_or_n, "Really scuttle %s? ", bbuf);
114         if (!confirm(y_or_n))
115             return RET_FAIL;
116     }
117     while (nxtitem(&ni, &item)) {
118         if (!player->owner)
119             continue;
120         if (opt_MARKET) {
121             if (ontradingblock(type, &item.ship)) {
122                 pr("You cannot scuttle an item on the trading block!\n");
123                 continue;
124             }
125         }
126
127         if (type == EF_SHIP) {
128             mp = &mchr[(int)item.ship.shp_type];
129             if (opt_TRADESHIPS && (mp->m_flags & M_TRADE)) {
130                 if (!scuttle_tradeship(&item.ship, 1))
131                     continue;
132             }
133         } else if (type == EF_LAND) {
134             if (item.land.lnd_ship >= 0) {
135                 pr("%s is on a ship, and cannot be scuttled!\n",
136                    prland(&item.land));
137                 continue;
138             }
139         }
140         pr("%s scuttled in %s\n",
141            obj_nameof(&item.gen),
142            xyas(item.gen.x, item.gen.y, player->cnum));
143         getsect(item.gen.x, item.gen.y, &sect);
144         if (sect.sct_own == item.gen.own)
145             unit_drop_cargo(&item.gen, sect.sct_own);
146         item.gen.effic = 0;
147         put_empobj(type, item.gen.uid, &item.gen);
148     }
149
150     return RET_OK;
151 }
152
153 int
154 scuttle_tradeship(struct shpstr *sp, int interactive)
155 {
156     float cash = 0;
157     float ally_cash = 0;
158     int dist = -1;
159     struct sctstr sect;
160     struct mchrstr *mp;
161     struct natstr *np;
162     char buf[512];
163     struct natstr *natp;
164
165     mp = &mchr[(int)sp->shp_type];
166     getsect(sp->shp_x, sp->shp_y, &sect);
167     if (sect.sct_own && sect.sct_type == SCT_HARBR && sect.sct_effic >= 2
168         && getrel(getnatp(sect.sct_own), sp->shp_own) >= FRIENDLY) {
169         dist = mapdist(sp->shp_x, sp->shp_y,
170                        sp->shp_orig_x, sp->shp_orig_y);
171         /* Don't disclose distance to to pirates */
172         if (sp->shp_own == sp->shp_orig_own)
173             mpr(sp->shp_own, "%s has gone %d sects\n", prship(sp), dist);
174         if (dist < trade_1_dist)
175             cash = 0;
176         else if (dist < trade_2_dist)
177             cash = 1.0 + trade_1 * dist;
178         else if (dist < trade_3_dist)
179             cash = 1.0 + trade_2 * dist;
180         else
181             cash = 1.0 + trade_3 * dist;
182         cash *= mp->m_cost;
183         cash *= sp->shp_effic / 100.0;
184
185         if (sect.sct_own != sp->shp_own) {
186             ally_cash = cash * trade_ally_cut;
187             cash *= (1.0 + trade_ally_bonus);
188         }
189     }
190
191     if (!cash && (dist < 0 || sp->shp_own == sp->shp_orig_own)) {
192         if (interactive) {
193             pr("You won't get any money if you scuttle in %s!",
194                xyas(sp->shp_x, sp->shp_y, player->cnum));
195             sprintf(buf, "Are you sure you want to scuttle %s? ", prship(sp));
196             return confirm(buf);
197         } else
198             return 0;
199     }
200
201     if (interactive) {
202         player->dolcost -= cash;
203     } else {
204         natp = getnatp(sp->shp_own);
205         natp->nat_money += cash;
206         putnat(natp);
207         wu(0, sp->shp_own, "You just made $%d.\n", (int)cash);
208     }
209
210     if (ally_cash) {
211         np = getnatp(sect.sct_own);
212         np->nat_money += ally_cash;
213         putnat(np);
214         wu(0, sect.sct_own,
215            "Trade with %s nets you $%d at %s\n",
216            cname(sp->shp_own),
217            (int)ally_cash, xyas(sect.sct_x, sect.sct_y, sect.sct_own));
218         if (sp->shp_own != sp->shp_orig_own)
219             nreport(sp->shp_own, N_PIRATE_TRADE, sp->shp_orig_own, 1);
220         else
221             nreport(sp->shp_own, N_TRADE, sect.sct_own, 1);
222     } else if (sp->shp_own != sp->shp_orig_own)
223         nreport(sp->shp_own, N_PIRATE_KEEP, sp->shp_orig_own, 1);
224
225     return 1;
226 }