]> 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-2004, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  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 "misc.h"
35 #include "player.h"
36 #include "sect.h"
37 #include "news.h"
38 #include "var.h"
39 #include "xy.h"
40 #include "ship.h"
41 #include "land.h"
42 #include "plane.h"
43 #include "nat.h"
44 #include "nsc.h"
45 #include "file.h"
46 #include "commands.h"
47 #include "optlist.h"
48
49 static void scuttle_land(struct lndstr *);
50
51 union item_u {
52     struct shpstr ship;
53     struct plnstr plane;
54     struct lndstr land;
55 };
56
57 int
58 scuttle_tradeship(struct shpstr *sp, int interactive)
59 {
60     float cash = 0;
61     float ally_cash = 0;
62     int dist;
63     struct sctstr sect;
64     struct mchrstr *mp;
65     struct natstr *np;
66     s_char buf[512];
67     struct natstr *natp;
68
69     mp = &mchr[(int)sp->shp_type];
70     getsect(sp->shp_x, sp->shp_y, &sect);
71     if (sect.sct_own && sect.sct_type == SCT_HARBR) {
72         dist = mapdist(sp->shp_x, sp->shp_y,
73                        sp->shp_orig_x, sp->shp_orig_y);
74         /* Don't disclose distance to to pirates */
75         if (sp->shp_own == sp->shp_orig_own) {
76             if (interactive)
77                 pr("%s has gone %d sects\n", prship(sp), dist);
78             else
79                 wu(0, sp->shp_own, "%s has gone %d sects\n",
80                    prship(sp), dist);
81         }
82         if (dist < trade_1_dist)
83             cash = 0;
84         else if (dist < trade_2_dist)
85             cash = (1.0 + trade_1 * ((float)dist));
86         else if (dist < trade_3_dist)
87             cash = (1.0 + trade_2 * ((float)dist));
88         else
89             cash = (1.0 + trade_3 * ((float)dist));
90         cash *= mp->m_cost;
91         cash *= (((float)sp->shp_effic) / 100.0);
92
93         if (sect.sct_own != sp->shp_own) {
94             cash *= (1.0 + trade_ally_bonus);
95             ally_cash = cash * trade_ally_cut;
96         }
97     }
98
99     if (!interactive && cash) {
100         natp = getnatp(sp->shp_own);
101         natp->nat_money += cash;
102         putnat(natp);
103         wu(0, sp->shp_own, "You just made $%d.\n", (int)cash);
104     } else if (!cash && !interactive) {
105         wu(0, sp->shp_own, "Unfortunately, you make $0 on this trade.\n");
106     } else if (cash && interactive) {
107         player->dolcost -= cash;
108     } else if (interactive && sp->shp_own == sp->shp_orig_own) {
109         pr("You won't get any money if you scuttle in %s!",
110            xyas(sp->shp_x, sp->shp_y, player->cnum));
111         sprintf(buf, "Are you sure you want to scuttle %s? ", prship(sp));
112         return confirm(buf);
113     }
114
115     if (ally_cash) {
116         np = getnatp(sect.sct_own);
117         np->nat_money += ally_cash;
118         putnat(np);
119         wu(0, sect.sct_own,
120            "Trade with %s nets you $%d at %s\n",
121            cname(sp->shp_own),
122            (int)ally_cash, xyas(sect.sct_x, sect.sct_y, sect.sct_own));
123         if (sp->shp_own != sp->shp_orig_own)
124             nreport(sp->shp_own, N_PIRATE_TRADE, sp->shp_orig_own, 1);
125         else
126             nreport(sp->shp_own, N_TRADE, sect.sct_own, 1);
127     } else if (sp->shp_own != sp->shp_orig_own)
128         nreport(sp->shp_own, N_PIRATE_KEEP, sp->shp_orig_own, 1);
129
130     return 1;
131 }
132
133 int
134 scut(void)
135 {
136     struct nstr_item ni;
137     union item_u item;
138     int type;
139     struct mchrstr *mp;
140     struct plchrstr *pp;
141     struct lchrstr *lp;
142     s_char *p;
143     s_char prompt[128];
144     s_char buf[1024];
145
146     if (!(p = getstarg(player->argp[1], "Ship, land, or plane? ", buf)))
147         return RET_SYN;
148     switch (*p) {
149     case 's':
150         type = EF_SHIP;
151         break;
152     case 'p':
153         type = EF_PLANE;
154         break;
155     case 'l':
156         type = EF_LAND;
157         break;
158     default:
159         pr("Ships, land units, or planes only! (s, l, p)\n");
160         return RET_SYN;
161     }
162     sprintf(prompt, "%s(s)? ", ef_nameof(type));
163     if ((p = getstarg(player->argp[2], prompt, buf)) == 0)
164         return RET_SYN;
165     if (!snxtitem(&ni, type, p))
166         return RET_SYN;
167     if (p && (isalpha(*p) || (*p == '*') || (*p == '~') || issector(p)
168               || islist(p))) {
169         s_char y_or_n[80], bbuf[80];
170
171         if (type == EF_SHIP) {
172             if (*p == '*')
173                 sprintf(bbuf, "all ships");
174             else if (*p == '~')
175                 sprintf(bbuf, "all unassigned ships");
176             else if (issector(p))
177                 sprintf(bbuf, "all ships in %s", p);
178             else if (isalpha(*p))
179                 sprintf(bbuf, "fleet %c", *p);
180             else
181                 sprintf(bbuf, "ships %s", p);
182         } else if (type == EF_LAND) {
183             if (*p == '*')
184                 sprintf(bbuf, "all land units");
185             else if (*p == '~')
186                 sprintf(bbuf, "all unassigned land units");
187             else if (issector(p))
188                 sprintf(bbuf, "all units in %s", p);
189             else if (isalpha(*p))
190                 sprintf(bbuf, "army %c", *p);
191             else
192                 sprintf(bbuf, "units %s", p);
193         } else {
194             if (*p == '*')
195                 sprintf(bbuf, "all planes");
196             else if (*p == '~')
197                 sprintf(bbuf, "all unassigned planes");
198             else if (issector(p))
199                 sprintf(bbuf, "all planes in %s", p);
200             else if (isalpha(*p))
201                 sprintf(bbuf, "wing %c", *p);
202             else
203                 sprintf(bbuf, "planes %s", p);
204         }
205         sprintf(y_or_n, "Really scuttle %s? ", bbuf);
206         if (!confirm(y_or_n))
207             return RET_FAIL;
208     }
209     while (nxtitem(&ni, (s_char *)&item)) {
210         if (!player->owner)
211             continue;
212         if (opt_MARKET) {
213             if (ontradingblock(type, (int *)&item.ship)) {
214                 pr("You cannot scuttle an item on the trading block!\n");
215                 continue;
216             }
217         }
218
219         if (type == EF_SHIP) {
220             mp = &mchr[(int)item.ship.shp_type];
221             if (opt_TRADESHIPS) {
222                 if (mp->m_flags & M_TRADE)
223                     if (!scuttle_tradeship(&item.ship, 1))
224                         continue;
225             }
226             pr("%s", prship(&item.ship));
227             scuttle_ship(&item.ship);
228         } else if (type == EF_LAND) {
229             if (item.land.lnd_ship >= 0) {
230                 pr("%s is on a ship, and cannot be scuttled!\n",
231                    prland(&item.land));
232                 continue;
233             }
234             lp = &lchr[(int)item.land.lnd_type];
235             pr("%s", prland(&item.land));
236             scuttle_land(&item.land);
237         } else {
238             pp = &plchr[(int)item.plane.pln_type];
239             pr("%s", prplane(&item.plane));
240             if (item.plane.pln_ship >= 0) {
241                 struct shpstr ship;
242
243                 getship(item.plane.pln_ship, &ship);
244                 take_plane_off_ship(&item.plane, &ship);
245             }
246             makelost(EF_PLANE, item.plane.pln_own, item.plane.pln_uid,
247                      item.plane.pln_x, item.plane.pln_y);
248             item.plane.pln_own = 0;
249             putplane(item.plane.pln_uid, (s_char *)&item.plane);
250         }
251         pr(" scuttled in %s\n",
252            xyas(item.ship.shp_x, item.ship.shp_y, player->cnum));
253     }
254
255     return RET_OK;
256 }
257
258 void
259 scuttle_ship(struct shpstr *sp)
260 {
261     struct nstr_item ni;
262     struct sctstr sect;
263     struct plnstr plane;
264     struct lndstr land;
265
266     getsect(sp->shp_x, sp->shp_y, &sect);
267     snxtitem_all(&ni, EF_PLANE);
268     while (nxtitem(&ni, (s_char *)&plane)) {
269         if (plane.pln_own == 0)
270             continue;
271         if (plane.pln_ship == sp->shp_uid) {
272             plane.pln_ship = -1;
273             if (sect.sct_own != sp->shp_own) {
274                 wu(0, plane.pln_own, "Plane %d scuttled in %s\n",
275                    plane.pln_uid,
276                    xyas(plane.pln_x, plane.pln_y, plane.pln_own));
277                 makelost(EF_PLANE, plane.pln_own, plane.pln_uid,
278                          plane.pln_x, plane.pln_y);
279                 plane.pln_own = 0;
280             } else {
281                 wu(0, plane.pln_own,
282                    "Plane %d transferred off ship %d to %s\n",
283                    plane.pln_uid, sp->shp_uid,
284                    xyas(plane.pln_x, plane.pln_y, plane.pln_own));
285             }
286             putplane(plane.pln_uid, (s_char *)&plane);
287         }
288     }
289     snxtitem_all(&ni, EF_LAND);
290     while (nxtitem(&ni, (s_char *)&land)) {
291         if (land.lnd_own == 0)
292             continue;
293         if (land.lnd_ship == sp->shp_uid) {
294             land.lnd_ship = -1;
295             if (sect.sct_own == sp->shp_own) {
296                 wu(0, land.lnd_own,
297                    "Land unit %d transferred off ship %d to %s\n",
298                    land.lnd_uid, sp->shp_uid,
299                    xyas(land.lnd_x, land.lnd_y, land.lnd_own));
300                 putland(land.lnd_uid, (s_char *)&land);
301             } else
302                 scuttle_land(&land);
303         }
304     }
305     makelost(EF_SHIP, sp->shp_own, sp->shp_uid, sp->shp_x, sp->shp_y);
306     sp->shp_own = 0;
307     putship(sp->shp_uid, sp);
308 }
309
310 static void
311 scuttle_land(struct lndstr *lp)
312 {
313     struct nstr_item ni;
314     struct sctstr sect;
315     struct plnstr plane;
316     struct lndstr land;
317
318     getsect(lp->lnd_x, lp->lnd_y, &sect);
319     snxtitem_all(&ni, EF_PLANE);
320     while (nxtitem(&ni, (s_char *)&plane)) {
321         if (plane.pln_own == 0)
322             continue;
323         if (plane.pln_land == lp->lnd_uid) {
324             plane.pln_land = -1;
325             if (sect.sct_own != lp->lnd_own) {
326                 wu(0, plane.pln_own, "Plane %d scuttled in %s\n",
327                    plane.pln_uid,
328                    xyas(plane.pln_x, plane.pln_y, plane.pln_own));
329                 makelost(EF_PLANE, plane.pln_own, plane.pln_uid,
330                          plane.pln_x, plane.pln_y);
331                 plane.pln_own = 0;
332             } else {
333                 wu(0, plane.pln_own,
334                    "Plane %d transferred off unit %d to %s\n",
335                    plane.pln_uid, lp->lnd_uid,
336                    xyas(plane.pln_x, plane.pln_y, plane.pln_own));
337             }
338             putplane(plane.pln_uid, (s_char *)&plane);
339         }
340     }
341     snxtitem_all(&ni, EF_LAND);
342     while (nxtitem(&ni, (s_char *)&land)) {
343         if (land.lnd_own == 0)
344             continue;
345         if (land.lnd_land == lp->lnd_uid) {
346             land.lnd_land = -1;
347             if (sect.sct_own == lp->lnd_own) {
348                 wu(0, land.lnd_own,
349                    "Land unit %d transferred off unit %d to %s\n",
350                    land.lnd_uid, lp->lnd_uid,
351                    xyas(land.lnd_x, land.lnd_y, land.lnd_own));
352                 putland(land.lnd_uid, (s_char *)&land);
353             } else
354                 scuttle_land(&land);
355         }
356     }
357     makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
358     lp->lnd_own = 0;
359     putland(lp->lnd_uid, lp);
360 }