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