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