]> git.pond.sub.org Git - empserver/blob - src/lib/commands/trad.c
Do not include var.h where no longer needed. Clean up register keywords in these...
[empserver] / src / lib / commands / trad.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  *  trad.c: Buy units/ships/planes/nukes from other nations.
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1986
32  *     Pat Loney, 1992
33  *     Steve McClure, 1996-2000
34  */
35
36 #include <ctype.h>
37 #include "misc.h"
38 #include "sect.h"
39 #include "nat.h"
40 #include "news.h"
41 #include "item.h"
42 #include "ship.h"
43 #include "nuke.h"
44 #include "land.h"
45 #include "plane.h"
46 #include "trade.h"
47 #include "xy.h"
48 #include "nsc.h"
49 #include "file.h"
50 #include "player.h"
51 #include "commodity.h"
52 #include "loan.h"
53 #include "commands.h"
54 #include "optlist.h"
55
56 /*
57  * format: trade
58  */
59 int
60 trad(void)
61 {
62     struct sctstr sect;
63     struct natstr *natp;
64     struct comstr comt;
65     int lotno;
66     float price;
67     coord sx, sy;
68     int n;
69     char *p;
70     struct nstr_item ni;
71     struct trdstr trade;
72     struct trdstr tmpt;
73     union trdgenstr tg;
74     int plflags;
75     double canspend;
76     time_t now;
77     int bid;
78     double tleft;
79     double tally;
80     int q;
81     s_char buf[1024];
82
83
84     if (!opt_MARKET) {
85         pr("The market is disabled.\n");
86         return RET_FAIL;
87     }
88     /* First, we execute all trades, so that we can only buy what is available. */
89     check_market();
90     check_trade();
91
92     pr("\n     Empire Trade Report\n  ");
93     prdate();
94     n = 0;
95     pr(" lot high bid  by time left owner  description\n");
96     pr(" --- --------  -- --------- -----  -------------------------\n");
97
98     snxtitem_all(&ni, EF_TRADE);
99     while (nxtitem(&ni, (char *)&trade)) {
100         if (trade.trd_owner == 0)
101             continue;
102         if (!trade_getitem(&trade, &tg)) {
103             continue;
104         };
105         /* fix up database if things get weird */
106         /*if (trade.trd_owner != tg.gen.trg_own) {
107            trade.trd_unitid = -1;
108            (void) puttrade(ni.cur, &trade);
109            continue;
110            } */
111         pr(" %3d ", ni.cur);
112         (void)time(&now);
113         tleft =
114             TRADE_DELAY / 3600.0 - (now - trade.trd_markettime) / 3600.0;
115         if (tleft < 0.0)
116             tleft = 0.0;
117         pr("$%7ld  %2d %5.2f hrs ", trade.trd_price,
118            trade.trd_maxbidder, tleft);
119         (void)trade_desc(&trade, &tg);  /* XXX */
120         pr("\n");
121         if (trade.trd_owner == player->cnum && !player->god)
122             pr(" (your own lot)\n");
123         n++;
124     }
125     if (n == 0) {
126         pr("Nothing to buy at the moment...\n");
127         return RET_OK;
128     }
129     if ((p = getstring("Which lot to buy: ", buf)) == 0 || *p == 0)
130         return RET_OK;
131     if (isdigit(*p) == 0)
132         return RET_OK;
133     lotno = atoi(p);
134     if (lotno < 0 || lotno >= ni.cur) {
135         pr("Bad lot number\n");
136         return RET_OK;
137     }
138     if (!gettrade(lotno, &trade)) {
139         pr("No such lot number\n");
140         return RET_OK;
141     }
142     if (trade.trd_unitid < 0) {
143         pr("Invalid lot number.\n");
144         return RET_OK;
145     }
146     if (!trade_getitem(&trade, &tg)) {
147         pr("Can't find trade #%d!\n", trade.trd_unitid);
148         trade.trd_unitid = -1;
149         if (!puttrade(lotno, &trade)) {
150             pr("Couldn't save after getitem failed; get help!\n");
151             return RET_SYS;
152         }
153         return RET_OK;
154     }
155     switch (trade.trd_type) {
156     case EF_NUKE:
157         /*
158            if (!getsect(tg.gen.trg_x, tg.gen.trg_y, &sect)) {
159            return RET_FAIL;
160            }
161            trade.trd_owner = sect.sct_own;
162            break;
163          */
164     case EF_PLANE:
165     case EF_SHIP:
166     case EF_LAND:
167         break;
168     default:
169         pr("Bad unit type on lot number %d\n", lotno);
170         return RET_FAIL;
171     }
172     if (trade.trd_owner == player->cnum) {
173         pr("You can't buy from yourself!\n");
174         return RET_OK;
175     }
176     price = trade.trd_price;
177     natp = getnatp(player->cnum);
178     if (natp->nat_money < price) {
179         pr("You don't have %.2f to spend!\n", price);
180         return RET_OK;
181     }
182     tally = 0.0;
183     for (q = 0; gettrade(q, &tmpt); q++) {
184         if (tmpt.trd_maxbidder == player->cnum &&
185             tmpt.trd_unitid >= 0 && tmpt.trd_owner != player->cnum) {
186             tally += tmpt.trd_price * tradetax;
187         }
188     }
189     for (q = 0; getcomm(q, &comt); q++) {
190         if (comt.com_maxbidder == player->cnum &&
191             comt.com_owner != 0 && comt.com_owner != player->cnum) {
192             tally += (comt.com_price * comt.com_amount) * buytax;
193         }
194     }
195     canspend = natp->nat_money - tally;
196     /*
197      * Find the destination sector for the plane before the trade
198      * is actually made. Must be owned (except for satellites) and
199      * must be a 60% airfield (except for VTOL planes).
200      */
201     if (((trade.trd_type == EF_PLANE) || (trade.trd_type == EF_NUKE))
202         && ((trade.trd_type == EF_NUKE) ||
203             !(tg.pln.pln_flags & PLN_LAUNCHED))) {
204         plflags = plchr[(int)tg.pln.pln_type].pl_flags;
205         while (1) {
206             p = getstring("Destination sector: ", buf);
207             if (!trade_check_ok(&trade, &tg))
208                 return RET_FAIL;
209             if (p == 0) {
210                 return RET_FAIL;
211             }
212             if (!sarg_xy(p, &sx, &sy) || !getsect(sx, sy, &sect)) {
213                 pr("Bad sector designation; try again!\n");
214                 continue;
215             }
216             if (!player->owner && !(plflags & P_O)) {
217                 pr("You don't own that sector; try again!\n");
218                 continue;
219             }
220             if (!(plflags & (P_V | P_O))) {
221                 if (!player->god && (sect.sct_type != SCT_AIRPT)) {
222                     pr("Destination sector is not an airfield!\n");
223                     continue;
224                 }
225                 if (!player->god && (sect.sct_effic < 60)) {
226                     pr("That airport still under construction!\n");
227                     continue;
228                 }
229             }
230             break;
231         }
232     }
233     if (trade.trd_type == EF_LAND) {
234         while (1) {
235             p = getstring("Destination sector: ", buf);
236             if (!trade_check_ok(&trade, &tg))
237                 return RET_FAIL;
238             if (p == 0) {
239                 return RET_FAIL;
240             }
241             if (!sarg_xy(p, &sx, &sy) || !getsect(sx, sy, &sect)) {
242                 pr("Bad sector designation; try again!\n");
243                 continue;
244             }
245             if (!player->owner) {
246                 pr("You don't own that sector; try again!\n");
247                 continue;
248             }
249             if (!player->god && (sect.sct_type != SCT_HEADQ)) {
250                 pr("Destination sector is not a headquarters!\n");
251                 continue;
252             }
253             if (!player->god && (sect.sct_effic < 60)) {
254                 pr("That headquarters still under construction!\n");
255                 continue;
256             }
257             break;
258         }
259     }
260
261     pr("WARNING!  This market issues credit.  If you make more\n");
262     pr("  bids than your treasury can cover at the time of sale,\n");
263     pr("  you can potentially go into financial ruin, and see no\n");
264     pr("  gains.  You have been warned.\n\n");
265
266     if ((p = getstring("How much do you bid: ", buf)) == 0 || *p == 0)
267         return RET_OK;
268     if (!trade_check_ok(&trade, &tg))
269         return RET_FAIL;
270     bid = atoi(p);
271     if (bid < price)
272         bid = price;
273     if (bid > canspend) {
274         pr("You don't have %.2f to spend!\n", price);
275         return RET_OK;
276     }
277     if (bid > trade.trd_price) {
278         /* Add five minutes to the time if less than 5 minutes left. */
279         time(&now);
280         if (((TRADE_DELAY - (now - trade.trd_markettime)) < 300) &&
281             trade.trd_maxbidder != player->cnum)
282             trade.trd_markettime += 300;
283         trade.trd_price = bid;
284         trade.trd_maxbidder = player->cnum;
285         trade.trd_x = sx;
286         trade.trd_y = sy;
287         pr("Your bid on lot #%d is being considered.\n", lotno);
288         if (!puttrade(lotno, &trade))
289             pr("Problems with the trade file.  Get help\n");
290     } else
291         pr("Your bid wasn't high enough (you need to bid more than someone else.)\n");
292
293     check_trade();
294
295     return RET_OK;
296 }
297
298 int
299 check_trade(void)
300 {
301     int n;
302     int j;
303     struct nstr_item ni;
304     struct plnstr plane;
305     struct lndstr land;
306     struct natstr *natp;
307     struct trdstr trade;
308     union trdgenstr tg;
309     time_t now;
310     double subleft;
311     double monleft;
312     double tleft;
313     float price;
314     int saveid;
315     struct lonstr loan;
316     long outstanding;           /* Outstanding debt */
317     long couval;                /* Value of country's goods */
318     int foundloan;
319
320 /*    logerror("Checking the trades.\n");*/
321     for (n = 0; gettrade(n, &trade); n++) {
322         if (trade.trd_unitid < 0)
323             continue;
324         if (!trade_getitem(&trade, &tg))
325             continue;
326         if (tg.gen.trg_own == 0) {
327             trade.trd_unitid = -1;
328             puttrade(n, &trade);
329             continue;
330         }
331         if (tg.gen.trg_own != trade.trd_owner) {
332             logerror("Something weird, tg.gen.trg_own != trade.trd_owner!\n");
333             trade.trd_unitid = -1;
334             puttrade(n, &trade);
335             continue;
336         }
337
338         if (trade.trd_owner == trade.trd_maxbidder)
339             continue;
340
341         (void)time(&now);
342         tleft =
343             TRADE_DELAY / 3600.0 - (now - trade.trd_markettime) / 3600.0;
344         if (tleft < 0.0)
345             tleft = 0.0;
346         if (tleft > 0.0)
347             continue;
348
349         saveid = trade.trd_unitid;
350         trade.trd_unitid = -1;
351         if (!puttrade(n, &trade)) {
352             logerror("Couldn't save trade after purchase; get help!\n");
353             continue;
354         }
355
356         monleft = 0;
357         price = trade.trd_price;
358         natp = getnatp(trade.trd_maxbidder);
359         if (natp->nat_money <= 0)
360             monleft = price;
361         if (natp->nat_money < price && natp->nat_money > 0) {
362             monleft = price - (natp->nat_money - 1);
363             natp->nat_money = 1;
364             price = price - monleft;
365         } else if (natp->nat_money > 0) {
366             monleft = 0;
367             natp->nat_money -= price;
368         }
369
370         subleft = monleft;
371
372         if (opt_LOANS) {
373             /* Try to make a loan for the rest from the owner. */
374             if (monleft > 0 && natp->nat_money > 0) {
375                 if ((float)((float)price / (float)(price + monleft)) < 0.1) {
376                     wu(0, trade.trd_maxbidder,
377                        "You need at least 10 percent down to purchase something on credit.\n");
378                 } else {
379                     couval = get_couval(trade.trd_maxbidder);
380                     outstanding = get_outstand(trade.trd_maxbidder);
381                     couval = couval - outstanding;
382                     if (couval > monleft) {
383                         /*  Make the loan */
384                         foundloan = 0;
385                         for (j = 0; getloan(j, &loan); j++) {
386                             if (loan.l_status != LS_FREE)
387                                 continue;
388                             foundloan = 1;
389                             break;
390                         }
391                         if (!foundloan)
392                             ef_extend(EF_LOAN, 1);
393                         loan.l_status = LS_SIGNED;
394                         loan.l_loner = trade.trd_owner;
395                         loan.l_lonee = trade.trd_maxbidder;
396                         loan.l_irate = 25;
397                         loan.l_ldur = 4;
398                         loan.l_amtpaid = 0;
399                         loan.l_amtdue = monleft;
400                         time(&loan.l_lastpay);
401                         loan.l_duedate =
402                             (loan.l_ldur * SECS_PER_DAY) + loan.l_lastpay;
403                         loan.l_uid = j;
404                         if (!putloan(j, &loan))
405                             logerror("Error writing to the loan file.\n");
406                         else
407                             monleft = 0;
408                         nreport(trade.trd_maxbidder, N_FIN_TROUBLE,
409                                 trade.trd_owner, 1);
410                         wu(0, trade.trd_maxbidder,
411                            "You just took loan #%d for $%.2f to cover the cost of your purchase.\n",
412                            j, (float)loan.l_amtdue);
413                         wu(0, trade.trd_owner,
414                            "You just extended loan #%d to %s to help with the purchase cost.\n",
415                            j, cname(trade.trd_maxbidder));
416                     } else {
417                         nreport(trade.trd_maxbidder, N_CREDIT_JUNK,
418                                 trade.trd_owner, 1);
419                         wu(0, trade.trd_maxbidder,
420                            "You don't have enough credit to get a loan.\n");
421                         wu(0, trade.trd_owner,
422                            "You just turned down a loan to %s.\n",
423                            cname(trade.trd_maxbidder));
424                     }
425                 }
426             }
427         }
428         if (monleft > 0) {
429             nreport(trade.trd_maxbidder, N_WELCH_DEAL, trade.trd_owner, 1);
430             wu(0, trade.trd_owner,
431                "%s tried to buy a %s #%d from you for $%.2f\n",
432                cname(trade.trd_maxbidder), trade_nameof(&trade, &tg),
433                saveid, (float)(price * tradetax));
434             wu(0, trade.trd_owner, "   but couldn't afford it.\n");
435             wu(0, trade.trd_owner,
436                "   Your item was taken off the market.\n");
437             wu(0, trade.trd_maxbidder,
438                "You tried to buy %s #%d from %s for $%.2f\n",
439                trade_nameof(&trade, &tg), saveid, cname(trade.trd_owner),
440                (float)(price * tradetax));
441             wu(0, trade.trd_maxbidder, "but couldn't afford it.\n");
442             continue;
443         }
444
445 /* If we get this far, the sale will go through. */
446 /* Only pay tax on the part you actually get cash for.  As a break,
447    we don't tax the part you have to give a loan on. */
448
449         putnat(natp);
450         natp = getnatp(trade.trd_owner);
451         /* Make sure we subtract the extra amount */
452         natp->nat_money += (roundavg(price * tradetax) - subleft);
453         putnat(natp);
454         switch (trade.trd_type) {
455         case EF_NUKE:
456             tg.nuk.nuk_x = trade.trd_x;
457             tg.nuk.nuk_y = trade.trd_y;
458             makelost(EF_NUKE, tg.nuk.nuk_own, tg.nuk.nuk_uid, tg.nuk.nuk_x,
459                      tg.nuk.nuk_y);
460             tg.nuk.nuk_own = trade.trd_maxbidder;
461             makenotlost(EF_NUKE, tg.nuk.nuk_own, tg.nuk.nuk_uid,
462                         tg.nuk.nuk_x, tg.nuk.nuk_y);
463             break;
464         case EF_PLANE:
465             if ((tg.pln.pln_flags & PLN_LAUNCHED) == 0) {
466                 tg.pln.pln_x = trade.trd_x;
467                 tg.pln.pln_y = trade.trd_y;
468             }
469             makelost(EF_PLANE, tg.pln.pln_own, tg.pln.pln_uid,
470                      tg.pln.pln_x, tg.pln.pln_y);
471             tg.pln.pln_own = trade.trd_maxbidder;
472             makenotlost(EF_PLANE, tg.pln.pln_own, tg.pln.pln_uid,
473                         tg.pln.pln_x, tg.pln.pln_y);
474             tg.pln.pln_wing = ' ';
475             /* no cheap version of fly */
476             if (opt_MOB_ACCESS) {
477                 tg.pln.pln_mobil = -(etu_per_update / sect_mob_neg_factor);
478             } else {
479                 tg.pln.pln_mobil = 0;
480             }
481             tg.pln.pln_mission = 0;
482             tg.pln.pln_harden = 0;
483             time(&tg.pln.pln_access);
484             tg.pln.pln_ship = -1;
485             tg.pln.pln_land = -1;
486             break;
487         case EF_SHIP:
488             takeover_ship(&tg.shp, trade.trd_maxbidder, 0);
489             break;
490         case EF_LAND:
491             tg.lnd.lnd_x = trade.trd_x;
492             tg.lnd.lnd_y = trade.trd_y;
493             if (tg.lnd.lnd_ship >= 0) {
494                 struct shpstr ship;
495                 getship(tg.lnd.lnd_ship, &ship);
496                 ship.shp_nland--;
497                 putship(ship.shp_uid, &ship);
498             }
499             makelost(EF_LAND, tg.lnd.lnd_own, tg.lnd.lnd_uid, tg.lnd.lnd_x,
500                      tg.lnd.lnd_y);
501             tg.lnd.lnd_own = trade.trd_maxbidder;
502             makenotlost(EF_LAND, tg.lnd.lnd_own, tg.lnd.lnd_uid,
503                         tg.lnd.lnd_x, tg.lnd.lnd_y);
504             tg.lnd.lnd_army = ' ';
505             /* no cheap version of fly */
506             if (opt_MOB_ACCESS) {
507                 tg.lnd.lnd_mobil = -(etu_per_update / sect_mob_neg_factor);
508             } else {
509                 tg.lnd.lnd_mobil = 0;
510             }
511             tg.lnd.lnd_harden = 0;
512             time(&tg.lnd.lnd_access);
513             tg.lnd.lnd_mission = 0;
514             /* Drop any land units this unit was carrying */
515             snxtitem_xy(&ni, EF_LAND, tg.lnd.lnd_x, tg.lnd.lnd_y);
516             while (nxtitem(&ni, (s_char *)&land)) {
517                 if (land.lnd_land != tg.lnd.lnd_uid)
518                     continue;
519                 land.lnd_land = -1;
520                 wu(0, land.lnd_own, "unit #%d dropped in %s\n",
521                    land.lnd_uid,
522                    xyas(land.lnd_x, land.lnd_y, land.lnd_own));
523                 putland(land.lnd_uid, &land);
524             }
525             /* Drop any planes this unit was carrying */
526             snxtitem_xy(&ni, EF_PLANE, tg.lnd.lnd_x, tg.lnd.lnd_y);
527             while (nxtitem(&ni, (s_char *)&plane)) {
528                 if (plane.pln_flags & PLN_LAUNCHED)
529                     continue;
530                 if (plane.pln_land != land.lnd_uid)
531                     continue;
532                 plane.pln_land = -1;
533                 wu(0, plane.pln_own, "plane #%d dropped in %s\n",
534                    plane.pln_uid,
535                    xyas(plane.pln_x, plane.pln_y, plane.pln_own));
536                 putplane(plane.pln_uid, &plane);
537             }
538             tg.lnd.lnd_ship = -1;
539             tg.lnd.lnd_land = -1;
540             break;
541         default:
542             logerror("Bad trade type %d in trade\n", trade.trd_type);
543             break;
544         }
545         if (!ef_write(trade.trd_type, saveid, (char *)&tg)) {
546             logerror("Couldn't write unit to disk; seek help.\n");
547             continue;
548         }
549         nreport(trade.trd_owner, N_MAKE_SALE, trade.trd_maxbidder, 1);
550         wu(0, trade.trd_owner, "%s bought a %s #%d from you for $%.2f\n",
551            cname(trade.trd_maxbidder), trade_nameof(&trade, &tg),
552            saveid, (float)(price * tradetax));
553         wu(0, trade.trd_maxbidder,
554            "The bidding is over & you bought %s #%d from %s for $%.2f\n",
555            trade_nameof(&trade, &tg), saveid, cname(trade.trd_owner),
556            (float)price);
557     }
558 /*    logerror("Done checking the trades.\n");*/
559     return RET_OK;
560 }
561
562 int
563 ontradingblock(int type, int *ptr)
564           /* Generic pointer */
565 {
566     struct trdstr trade;
567     union trdgenstr tg;
568     int n;
569
570     for (n = 0; gettrade(n, &trade); n++) {
571         if (trade.trd_unitid < 0)
572             continue;
573         if (!trade_getitem(&trade, &tg))
574             continue;
575         if (trade.trd_type != type)
576             continue;
577         if (tg.gen.trg_uid == ((struct genstr *)ptr)->trg_uid)
578             return 1;
579     }
580     return 0;
581 }
582
583 void
584 trdswitchown(int type, int *ptr, int newown)
585           /* Generic pointer */
586 {
587     struct trdstr trade;
588     union trdgenstr tg;
589     int n;
590
591     for (n = 0; gettrade(n, &trade); n++) {
592         if (trade.trd_unitid < 0)
593             continue;
594         if (!trade_getitem(&trade, &tg))
595             continue;
596         if (trade.trd_type != type)
597             continue;
598         if (tg.gen.trg_uid != ((struct genstr *)ptr)->trg_uid)
599             continue;
600         if (trade.trd_owner == trade.trd_maxbidder)
601             trade.trd_maxbidder = newown;
602         trade.trd_owner = newown;
603         if (newown == 0)
604             trade.trd_unitid = -1;
605         puttrade(n, &trade);
606         return;
607     }
608 }