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