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