]> git.pond.sub.org Git - empserver/blob - src/lib/commands/scut.c
d2ead896f038a7d4010f980efadf3361a93acc6e
[empserver] / src / lib / commands / scut.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, 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  *     
32  */
33
34 #include <config.h>
35
36 #include <ctype.h>
37 #include "commands.h"
38 #include "empobj.h"
39 #include "optlist.h"
40
41 static void scuttle_land(struct lndstr *);
42
43 int
44 scut(void)
45 {
46     struct nstr_item ni;
47     union empobj_storage item;
48     int type;
49     struct mchrstr *mp;
50     char *p;
51     char prompt[128];
52     char buf[1024];
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)) == 0)
72         return RET_SYN;
73     if (!snxtitem(&ni, type, p))
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) {
130                 if (mp->m_flags & M_TRADE)
131                     if (!scuttle_tradeship(&item.ship, 1))
132                         continue;
133             }
134             pr("%s", prship(&item.ship));
135             scuttle_ship(&item.ship);
136         } else if (type == EF_LAND) {
137             if (item.land.lnd_ship >= 0) {
138                 pr("%s is on a ship, and cannot be scuttled!\n",
139                    prland(&item.land));
140                 continue;
141             }
142             pr("%s", prland(&item.land));
143             scuttle_land(&item.land);
144         } else {
145             pr("%s", prplane(&item.plane));
146             if (item.plane.pln_ship >= 0) {
147                 struct shpstr ship;
148
149                 getship(item.plane.pln_ship, &ship);
150                 take_plane_off_ship(&item.plane, &ship);
151             }
152             item.plane.pln_effic = 0;
153             putplane(item.plane.pln_uid, &item.plane);
154         }
155         pr(" scuttled in %s\n",
156            xyas(item.ship.shp_x, item.ship.shp_y, player->cnum));
157     }
158
159     return RET_OK;
160 }
161
162 int
163 scuttle_tradeship(struct shpstr *sp, int interactive)
164 {
165     float cash = 0;
166     float ally_cash = 0;
167     int dist;
168     struct sctstr sect;
169     struct mchrstr *mp;
170     struct natstr *np;
171     char buf[512];
172     struct natstr *natp;
173
174     mp = &mchr[(int)sp->shp_type];
175     getsect(sp->shp_x, sp->shp_y, &sect);
176     if (sect.sct_own && sect.sct_type == SCT_HARBR) {
177         dist = mapdist(sp->shp_x, sp->shp_y,
178                        sp->shp_orig_x, sp->shp_orig_y);
179         /* Don't disclose distance to to pirates */
180         if (sp->shp_own == sp->shp_orig_own) {
181             if (interactive)
182                 pr("%s has gone %d sects\n", prship(sp), dist);
183             else
184                 wu(0, sp->shp_own, "%s has gone %d sects\n",
185                    prship(sp), dist);
186         }
187         if (dist < trade_1_dist)
188             cash = 0;
189         else if (dist < trade_2_dist)
190             cash = 1.0 + trade_1 * dist;
191         else if (dist < trade_3_dist)
192             cash = 1.0 + trade_2 * dist;
193         else
194             cash = 1.0 + trade_3 * dist;
195         cash *= mp->m_cost;
196         cash *= sp->shp_effic / 100.0;
197
198         if (sect.sct_own != sp->shp_own) {
199             ally_cash = cash * trade_ally_cut;
200             cash *= (1.0 + trade_ally_bonus);
201         }
202     }
203
204     if (!interactive && cash) {
205         natp = getnatp(sp->shp_own);
206         natp->nat_money += cash;
207         putnat(natp);
208         wu(0, sp->shp_own, "You just made $%d.\n", (int)cash);
209     } else if (!cash && !interactive) {
210         wu(0, sp->shp_own, "Unfortunately, you make $0 on this trade.\n");
211     } else if (cash && interactive) {
212         player->dolcost -= cash;
213     } else if (interactive && sp->shp_own == sp->shp_orig_own) {
214         pr("You won't get any money if you scuttle in %s!",
215            xyas(sp->shp_x, sp->shp_y, player->cnum));
216         sprintf(buf, "Are you sure you want to scuttle %s? ", prship(sp));
217         return confirm(buf);
218     }
219
220     if (ally_cash) {
221         np = getnatp(sect.sct_own);
222         np->nat_money += ally_cash;
223         putnat(np);
224         wu(0, sect.sct_own,
225            "Trade with %s nets you $%d at %s\n",
226            cname(sp->shp_own),
227            (int)ally_cash, xyas(sect.sct_x, sect.sct_y, sect.sct_own));
228         if (sp->shp_own != sp->shp_orig_own)
229             nreport(sp->shp_own, N_PIRATE_TRADE, sp->shp_orig_own, 1);
230         else
231             nreport(sp->shp_own, N_TRADE, sect.sct_own, 1);
232     } else if (sp->shp_own != sp->shp_orig_own)
233         nreport(sp->shp_own, N_PIRATE_KEEP, sp->shp_orig_own, 1);
234
235     return 1;
236 }
237
238 void
239 scuttle_ship(struct shpstr *sp)
240 {
241     struct nstr_item ni;
242     struct sctstr sect;
243     struct plnstr plane;
244     struct lndstr land;
245
246     getsect(sp->shp_x, sp->shp_y, &sect);
247     snxtitem_all(&ni, EF_PLANE);
248     while (nxtitem(&ni, &plane)) {
249         if (plane.pln_own == 0)
250             continue;
251         if (plane.pln_ship == sp->shp_uid) {
252             plane.pln_ship = -1;
253             if (sect.sct_own != sp->shp_own) {
254                 wu(0, plane.pln_own, "Plane %d scuttled in %s\n",
255                    plane.pln_uid,
256                    xyas(plane.pln_x, plane.pln_y, plane.pln_own));
257                 plane.pln_effic = 0;
258             } else {
259                 wu(0, plane.pln_own,
260                    "Plane %d transferred off ship %d to %s\n",
261                    plane.pln_uid, sp->shp_uid,
262                    xyas(plane.pln_x, plane.pln_y, plane.pln_own));
263             }
264             putplane(plane.pln_uid, &plane);
265         }
266     }
267     snxtitem_all(&ni, EF_LAND);
268     while (nxtitem(&ni, &land)) {
269         if (land.lnd_own == 0)
270             continue;
271         if (land.lnd_ship == sp->shp_uid) {
272             land.lnd_ship = -1;
273             if (sect.sct_own == sp->shp_own) {
274                 wu(0, land.lnd_own,
275                    "Land unit %d transferred off ship %d to %s\n",
276                    land.lnd_uid, sp->shp_uid,
277                    xyas(land.lnd_x, land.lnd_y, land.lnd_own));
278                 putland(land.lnd_uid, &land);
279             } else
280                 scuttle_land(&land);
281         }
282     }
283     sp->shp_effic = 0;
284     putship(sp->shp_uid, sp);
285 }
286
287 static void
288 scuttle_land(struct lndstr *lp)
289 {
290     struct nstr_item ni;
291     struct sctstr sect;
292     struct plnstr plane;
293     struct lndstr land;
294
295     getsect(lp->lnd_x, lp->lnd_y, &sect);
296     snxtitem_all(&ni, EF_PLANE);
297     while (nxtitem(&ni, &plane)) {
298         if (plane.pln_own == 0)
299             continue;
300         if (plane.pln_land == lp->lnd_uid) {
301             plane.pln_land = -1;
302             if (sect.sct_own != lp->lnd_own) {
303                 wu(0, plane.pln_own, "Plane %d scuttled in %s\n",
304                    plane.pln_uid,
305                    xyas(plane.pln_x, plane.pln_y, plane.pln_own));
306                 plane.pln_effic = 0;
307             } else {
308                 wu(0, plane.pln_own,
309                    "Plane %d transferred off unit %d to %s\n",
310                    plane.pln_uid, lp->lnd_uid,
311                    xyas(plane.pln_x, plane.pln_y, plane.pln_own));
312             }
313             putplane(plane.pln_uid, &plane);
314         }
315     }
316     snxtitem_all(&ni, EF_LAND);
317     while (nxtitem(&ni, &land)) {
318         if (land.lnd_own == 0)
319             continue;
320         if (land.lnd_land == lp->lnd_uid) {
321             land.lnd_land = -1;
322             if (sect.sct_own == lp->lnd_own) {
323                 wu(0, land.lnd_own,
324                    "Land unit %d transferred off unit %d to %s\n",
325                    land.lnd_uid, lp->lnd_uid,
326                    xyas(land.lnd_x, land.lnd_y, land.lnd_own));
327                 putland(land.lnd_uid, &land);
328             } else
329                 scuttle_land(&land);
330         }
331     }
332     lp->lnd_effic = 0;
333     putland(lp->lnd_uid, lp);
334 }