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