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