]> git.pond.sub.org Git - empserver/blob - src/lib/commands/scut.c
Update copyright notice
[empserver] / src / lib / commands / scut.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2016, 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-2014
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 static int scuttle_tradeship(struct shpstr *);
42
43 int
44 scut(void)
45 {
46     struct nstr_item ni;
47     union empobj_storage item;
48     int type, n;
49     struct mchrstr *mp;
50     char *p;
51     char prompt[128];
52     char buf[1024];
53     struct sctstr sect;
54
55     if (!(p = getstarg(player->argp[1], "Ship, land, or plane? ", buf)))
56         return RET_SYN;
57     switch (*p) {
58     case 's':
59         type = EF_SHIP;
60         break;
61     case 'p':
62         type = EF_PLANE;
63         break;
64     case 'l':
65         type = EF_LAND;
66         break;
67     default:
68         pr("Ships, land units, or planes only! (s, l, p)\n");
69         return RET_SYN;
70     }
71
72     if (!snxtitem(&ni, type, player->argp[2], NULL))
73         return RET_SYN;
74     n = 0;
75     while (nxtitem(&ni, &item)) {
76         if (!player->owner)
77             continue;
78         n++;
79     }
80     snprintf(prompt, sizeof(prompt), "Really scuttle %d %s%s [n]? ",
81                             n, ef_nameof(type), splur(n));
82     if (!confirm(prompt))
83         return RET_FAIL;
84
85     snxtitem_rewind(&ni);
86     while (nxtitem(&ni, &item)) {
87         if (!player->owner)
88             continue;
89         if (opt_MARKET) {
90             if (ontradingblock(type, &item.ship)) {
91                 pr("You cannot scuttle an item on the trading block!\n");
92                 continue;
93             }
94         }
95
96         if (type == EF_SHIP) {
97             mp = &mchr[(int)item.ship.shp_type];
98             if (mp->m_flags & M_TRADE) {
99                 if (!scuttle_tradeship(&item.ship))
100                     continue;
101             }
102         } else if (type == EF_LAND) {
103             if (item.land.lnd_ship >= 0) {
104                 pr("%s is on a ship, and cannot be scuttled!\n",
105                    prland(&item.land));
106                 continue;
107             }
108         }
109         pr("%s scuttled in %s\n",
110            unit_nameof(&item.gen),
111            xyas(item.gen.x, item.gen.y, player->cnum));
112         getsect(item.gen.x, item.gen.y, &sect);
113         if (sect.sct_own == item.gen.own)
114             unit_drop_cargo(&item.gen, sect.sct_own);
115         item.gen.effic = 0;
116         put_empobj(type, item.gen.uid, &item.gen);
117     }
118
119     return RET_OK;
120 }
121
122 static int
123 scuttle_tradeship(struct shpstr *sp)
124 {
125     float cash = 0;
126     float ally_cash = 0;
127     int dist = -1;
128     struct sctstr sect;
129     struct mchrstr *mp;
130     struct natstr *np;
131     char buf[512];
132
133     mp = &mchr[(int)sp->shp_type];
134     getsect(sp->shp_x, sp->shp_y, &sect);
135     if (sect.sct_own && sect.sct_type == SCT_HARBR && sect.sct_effic >= 2
136         && relations_with(sect.sct_own, sp->shp_own) >= FRIENDLY) {
137         dist = mapdist(sp->shp_x, sp->shp_y,
138                        sp->shp_orig_x, sp->shp_orig_y);
139         /* Don't disclose distance to to pirates */
140         if (player->cnum == sp->shp_orig_own)
141             pr("%s has gone %d sects\n", prship(sp), dist);
142         if (dist < trade_1_dist)
143             cash = 0;
144         else if (dist < trade_2_dist)
145             cash = 1.0 + trade_1 * dist;
146         else if (dist < trade_3_dist)
147             cash = 1.0 + trade_2 * dist;
148         else
149             cash = 1.0 + trade_3 * dist;
150         cash *= mp->m_cost;
151         cash *= sp->shp_effic / 100.0;
152
153         if (sect.sct_own != sp->shp_own) {
154             ally_cash = cash * trade_ally_cut;
155             cash *= (1.0 + trade_ally_bonus);
156         }
157     }
158
159     if (!cash && (dist < 0 || sp->shp_own == sp->shp_orig_own)) {
160         pr("You won't get any money if you scuttle in %s!",
161            xyas(sp->shp_x, sp->shp_y, player->cnum));
162         sprintf(buf, "Are you sure you want to scuttle %s? ", prship(sp));
163         return confirm(buf);
164     }
165
166     player->dolcost -= cash;
167
168     if (ally_cash) {
169         np = getnatp(sect.sct_own);
170         np->nat_money += ally_cash;
171         putnat(np);
172         wu(0, sect.sct_own,
173            "Trade with %s nets you $%d at %s\n",
174            cname(sp->shp_own),
175            (int)ally_cash, xyas(sect.sct_x, sect.sct_y, sect.sct_own));
176         if (sp->shp_own != sp->shp_orig_own)
177             nreport(sp->shp_own, N_PIRATE_TRADE, sp->shp_orig_own, 1);
178         else
179             nreport(sp->shp_own, N_TRADE, sect.sct_own, 1);
180     } else if (sp->shp_own != sp->shp_orig_own)
181         nreport(sp->shp_own, N_PIRATE_KEEP, sp->shp_orig_own, 1);
182
183     return 1;
184 }