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