]> git.pond.sub.org Git - empserver/blob - src/lib/commands/trad.c
Move declarations for chance.c to new chance.h
[empserver] / src / lib / commands / trad.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2013, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  trad.c: Buy units/ships/planes/nukes from other nations.
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1986
31  *     Pat Loney, 1992
32  *     Steve McClure, 1996-2000
33  */
34
35 #include <config.h>
36
37 #include <ctype.h>
38 #include "chance.h"
39 #include "commands.h"
40 #include "commodity.h"
41 #include "empobj.h"
42 #include "land.h"
43 #include "loan.h"
44 #include "news.h"
45 #include "nuke.h"
46 #include "optlist.h"
47 #include "plane.h"
48 #include "ship.h"
49 #include "trade.h"
50 #include "unit.h"
51
52 /*
53  * format: trade
54  */
55 int
56 trad(void)
57 {
58     struct sctstr sect;
59     struct natstr *natp;
60     struct comstr comt;
61     int lotno;
62     float price;
63     coord sx, sy;
64     int n;
65     char *p;
66     struct nstr_item ni;
67     struct trdstr trade;
68     struct trdstr tmpt;
69     union empobj_storage tg;
70     double canspend;
71     time_t now;
72     int bid;
73     double tleft;
74     double tally;
75     int i;
76     char buf[1024];
77
78     if (!opt_MARKET) {
79         pr("The market is disabled.\n");
80         return RET_FAIL;
81     }
82     /* First, we execute all trades, so that we can only buy what is available. */
83     check_market();
84     check_trade();
85
86     pr("\n     Empire Trade Report\n  ");
87     prdate();
88     n = 0;
89     pr(" lot high bid  by time left owner  description\n");
90     pr(" --- --------  -- --------- -----  -------------------------\n");
91
92     snxtitem_all(&ni, EF_TRADE);
93     while (nxtitem(&ni, &trade)) {
94         if (trade.trd_owner == 0)
95             continue;
96         if (!trade_getitem(&trade, &tg)) {
97             continue;
98         };
99         pr(" %3d ", ni.cur);
100         (void)time(&now);
101         tleft =
102             TRADE_DELAY / 3600.0 - (now - trade.trd_markettime) / 3600.0;
103         if (tleft < 0.0)
104             tleft = 0.0;
105         pr("$%7ld  %2d %5.2f hrs ",
106            trade.trd_price, trade.trd_maxbidder, tleft);
107         trade_desc(&tg.gen);    /* XXX */
108         pr("\n");
109         if (trade.trd_owner == player->cnum && !player->god)
110             pr(" (your own lot)\n");
111         n++;
112     }
113     if (n == 0) {
114         pr("Nothing to buy at the moment...\n");
115         return RET_OK;
116     }
117     p = getstring("Which lot to buy: ", buf);
118     if (!p || !*p)
119         return RET_OK;
120     if (isdigit(*p) == 0)
121         return RET_OK;
122     lotno = atoi(p);
123     if (lotno < 0 || lotno >= ni.cur) {
124         pr("Bad lot number\n");
125         return RET_OK;
126     }
127     if (!gettrade(lotno, &trade)) {
128         pr("No such lot number\n");
129         return RET_OK;
130     }
131     if (trade.trd_unitid < 0) {
132         pr("Invalid lot number.\n");
133         return RET_OK;
134     }
135     if (!trade_getitem(&trade, &tg)) {
136         pr("Can't find trade #%d!\n", trade.trd_unitid);
137         trade.trd_owner = 0;
138         trade.trd_unitid = -1;
139         if (!puttrade(lotno, &trade)) {
140             logerror("trad: can't write trade");
141             pr("Couldn't save after getitem failed; get help!\n");
142             return RET_FAIL;
143         }
144         return RET_OK;
145     }
146     switch (trade.trd_type) {
147     case EF_NUKE:
148     case EF_PLANE:
149     case EF_SHIP:
150     case EF_LAND:
151         break;
152     default:
153         pr("Bad unit type on lot number %d\n", lotno);
154         return RET_FAIL;
155     }
156     if (trade.trd_owner == player->cnum) {
157         pr("You can't buy from yourself!\n");
158         return RET_OK;
159     }
160     price = trade.trd_price;
161     natp = getnatp(player->cnum);
162     if (natp->nat_money < price) {
163         pr("You don't have %.2f to spend!\n", price);
164         return RET_OK;
165     }
166     tally = 0.0;
167     for (i = 0; gettrade(i, &tmpt); i++) {
168         if (tmpt.trd_maxbidder == player->cnum &&
169             tmpt.trd_unitid >= 0 && tmpt.trd_owner != player->cnum) {
170             tally += tmpt.trd_price * tradetax;
171         }
172     }
173     for (i = 0; getcomm(i, &comt); i++) {
174         if (comt.com_maxbidder == player->cnum &&
175             comt.com_owner != 0 && comt.com_owner != player->cnum) {
176             tally += (comt.com_price * comt.com_amount) * buytax;
177         }
178     }
179     canspend = natp->nat_money - tally;
180
181     /* Find the destination sector for the trade */
182     if (((trade.trd_type == EF_PLANE) && !pln_is_in_orbit(&tg.plane))
183         || (trade.trd_type == EF_NUKE)) {
184         while (1) {
185             p = getstring("Destination sector: ", buf);
186             if (!trade_check_ok(&trade, &tg.gen))
187                 return RET_FAIL;
188             if (!p) {
189                 return RET_FAIL;
190             }
191             if (!sarg_xy(p, &sx, &sy) || !getsect(sx, sy, &sect)) {
192                 pr("Bad sector designation; try again!\n");
193                 continue;
194             }
195             if (!player->owner) {
196                 pr("You don't own that sector; try again!\n");
197                 continue;
198             }
199             if (!(plchr[tg.plane.pln_type].pl_flags & P_V)) {
200                 if (!player->god && (sect.sct_type != SCT_AIRPT)) {
201                     pr("Destination sector is not an airfield!\n");
202                     continue;
203                 }
204                 if (!player->god && (sect.sct_effic < 60)) {
205                     pr("That airport still under construction!\n");
206                     continue;
207                 }
208             }
209             break;
210         }
211     } else if (trade.trd_type == EF_LAND) {
212         while (1) {
213             p = getstring("Destination sector: ", buf);
214             if (!trade_check_ok(&trade, &tg.gen))
215                 return RET_FAIL;
216             if (!p) {
217                 return RET_FAIL;
218             }
219             if (!sarg_xy(p, &sx, &sy) || !getsect(sx, sy, &sect)) {
220                 pr("Bad sector designation; try again!\n");
221                 continue;
222             }
223             if (!player->owner) {
224                 pr("You don't own that sector; try again!\n");
225                 continue;
226             }
227             if (!player->god && (sect.sct_type != SCT_HEADQ)) {
228                 pr("Destination sector is not a headquarters!\n");
229                 continue;
230             }
231             if (!player->god && (sect.sct_effic < 60)) {
232                 pr("That headquarters still under construction!\n");
233                 continue;
234             }
235             break;
236         }
237     } else {
238         /* This trade doesn't teleport; make destination invalid */
239         sx = 1;
240         sy = 0;
241     }
242
243     p = getstring("How much do you bid: ", buf);
244     if (!p || !*p)
245         return RET_OK;
246     if (!trade_check_ok(&trade, &tg.gen))
247         return RET_FAIL;
248     bid = atoi(p);
249     if (bid < price)
250         bid = price;
251     if (bid > canspend) {
252         pr("You don't have %.2f to spend!\n", price);
253         return RET_OK;
254     }
255     if (bid > trade.trd_price) {
256         /* Add five minutes to the time if less than 5 minutes left. */
257         time(&now);
258         if (((TRADE_DELAY - (now - trade.trd_markettime)) < 300) &&
259             trade.trd_maxbidder != player->cnum)
260             trade.trd_markettime += 300;
261         trade.trd_price = bid;
262         trade.trd_maxbidder = player->cnum;
263         trade.trd_x = sx;
264         trade.trd_y = sy;
265         pr("Your bid on lot #%d is being considered.\n", lotno);
266         if (!puttrade(lotno, &trade))
267             pr("Problems with the trade file.  Get help\n");
268     } else
269         pr("Your bid wasn't high enough (you need to bid more than someone else.)\n");
270
271     check_trade();
272
273     return RET_OK;
274 }
275
276 int
277 check_trade(void)
278 {
279     int n;
280     struct natstr *natp;
281     struct trdstr trade;
282     union empobj_storage tg;
283     time_t now;
284     double tleft;
285     float price;
286     int saveid;
287     natid seller;
288
289     for (n = 0; gettrade(n, &trade); n++) {
290         if (trade.trd_unitid < 0)
291             continue;
292         if (!trade_getitem(&trade, &tg))
293             continue;
294         if (tg.gen.own == 0) {
295             trade.trd_owner = 0;
296             trade.trd_unitid = -1;
297             puttrade(n, &trade);
298             continue;
299         }
300         if (tg.gen.own != trade.trd_owner) {
301             logerror("Something weird, tg.gen.own != trade.trd_owner!\n");
302             trade.trd_owner = 0;
303             trade.trd_unitid = -1;
304             puttrade(n, &trade);
305             continue;
306         }
307
308         if (trade.trd_owner == trade.trd_maxbidder)
309             continue;
310
311         (void)time(&now);
312         tleft =
313             TRADE_DELAY / 3600.0 - (now - trade.trd_markettime) / 3600.0;
314         if (tleft < 0.0)
315             tleft = 0.0;
316         if (tleft > 0.0)
317             continue;
318
319         saveid = trade.trd_unitid;
320         seller = trade.trd_owner;
321         trade.trd_owner = 0;
322         trade.trd_unitid = -1;
323         if (!puttrade(n, &trade)) {
324             logerror("Couldn't save trade after purchase; get help!\n");
325             continue;
326         }
327
328         price = trade.trd_price;
329         natp = getnatp(trade.trd_maxbidder);
330         if (natp->nat_money < price) {
331             nreport(trade.trd_maxbidder, N_WELCH_DEAL, seller, 1);
332             wu(0, seller,
333                "%s tried to buy a %s #%d from you for $%.2f\n",
334                cname(trade.trd_maxbidder), trade_nameof(&trade, &tg.gen),
335                saveid, price * tradetax);
336             wu(0, seller, "   but couldn't afford it.\n");
337             wu(0, seller,
338                "   Your item was taken off the market.\n");
339             wu(0, trade.trd_maxbidder,
340                "You tried to buy %s #%d from %s for $%.2f\n",
341                trade_nameof(&trade, &tg.gen), saveid, cname(seller),
342                price);
343             wu(0, trade.trd_maxbidder, "but couldn't afford it.\n");
344             continue;
345         }
346
347 /* If we get this far, the sale will go through. */
348
349         natp->nat_money -= price;
350         putnat(natp);
351
352         natp = getnatp(seller);
353         natp->nat_money += roundavg(price * tradetax);
354         putnat(natp);
355
356         switch (trade.trd_type) {
357         case EF_NUKE:
358             tg.nuke.nuk_x = trade.trd_x;
359             tg.nuke.nuk_y = trade.trd_y;
360             tg.nuke.nuk_plane = -1;
361             break;
362         case EF_PLANE:
363             if (!pln_is_in_orbit(&tg.plane)) {
364                 tg.plane.pln_x = trade.trd_x;
365                 tg.plane.pln_y = trade.trd_y;
366             }
367             if (opt_MOB_ACCESS) {
368                 tg.plane.pln_mobil = -(etu_per_update / sect_mob_neg_factor);
369                 game_tick_to_now(&tg.plane.pln_access);
370             } else {
371                 tg.plane.pln_mobil = 0;
372             }
373             tg.plane.pln_harden = 0;
374             tg.plane.pln_ship = -1;
375             tg.plane.pln_land = -1;
376             break;
377         case EF_SHIP:
378             break;
379         case EF_LAND:
380             tg.land.lnd_x = trade.trd_x;
381             tg.land.lnd_y = trade.trd_y;
382             if (opt_MOB_ACCESS) {
383                 tg.land.lnd_mobil = -(etu_per_update / sect_mob_neg_factor);
384                 game_tick_to_now(&tg.land.lnd_access);
385             } else {
386                 tg.land.lnd_mobil = 0;
387             }
388             tg.land.lnd_harden = 0;
389             unit_drop_cargo(&tg.gen, 0);
390             tg.land.lnd_ship = -1;
391             tg.land.lnd_land = -1;
392             break;
393         default:
394             logerror("Bad trade type %d in trade\n", trade.trd_type);
395             break;
396         }
397         unit_give_away(&tg.gen, trade.trd_maxbidder, 0);
398         put_empobj(trade.trd_type, saveid, &tg.gen);
399
400         nreport(seller, N_MAKE_SALE, trade.trd_maxbidder, 1);
401         wu(0, seller, "%s bought %s #%d from you for $%.2f\n",
402            cname(trade.trd_maxbidder), trade_nameof(&trade, &tg.gen),
403            saveid, price * tradetax);
404         wu(0, trade.trd_maxbidder,
405            "The bidding is over & you bought %s #%d from %s for $%.2f\n",
406            trade_nameof(&trade, &tg.gen), saveid, cname(seller),
407            price);
408     }
409     return RET_OK;
410 }
411
412 int
413 ontradingblock(int type, void *ptr)
414 {
415     struct trdstr trade;
416     union empobj_storage tg;
417     int n;
418
419     for (n = 0; gettrade(n, &trade); n++) {
420         if (trade.trd_unitid < 0)
421             continue;
422         if (!trade_getitem(&trade, &tg))
423             continue;
424         if (trade.trd_type != type)
425             continue;
426         if (tg.gen.uid == ((struct empobj *)ptr)->uid)
427             return 1;
428     }
429     return 0;
430 }
431
432 void
433 trdswitchown(int type, void *ptr, int newown)
434 {
435     struct trdstr trade;
436     union empobj_storage tg;
437     int n;
438
439     for (n = 0; gettrade(n, &trade); n++) {
440         if (trade.trd_unitid < 0)
441             continue;
442         if (!trade_getitem(&trade, &tg))
443             continue;
444         if (trade.trd_type != type)
445             continue;
446         if (tg.gen.uid != ((struct empobj *)ptr)->uid)
447             continue;
448         if (trade.trd_owner == trade.trd_maxbidder)
449             trade.trd_maxbidder = newown;
450         trade.trd_owner = newown;
451         if (newown == 0)
452             trade.trd_unitid = -1;
453         puttrade(n, &trade);
454         return;
455     }
456 }