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