]> git.pond.sub.org Git - empserver/blob - src/lib/commands/trad.c
Import of Empire 4.2.12
[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 = TRADE_DELAY/3600.0 - (now - trade.trd_markettime) / 3600.0;
122         if (tleft < 0.0) 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 &&
192             tmpt.trd_owner != player->cnum) {
193             tally += tmpt.trd_maxprice * tradetax;
194         }
195     }
196     for (q = 0; getcomm(q, &comt); q++) {
197         if (comt.com_maxbidder == player->cnum &&
198             comt.com_owner != 0 &&
199             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) ||
221                 !getsect(sx, sy, &sect)) {
222                 pr("Bad sector designation; try again!\n");
223                 continue;
224             }
225             if (!player->owner && !(plflags & P_O)) {
226                 pr("You don't own that sector; try again!\n");
227                 continue;
228             }
229             if (!(plflags & (P_V | P_O))) {
230                 if (!player->god && (sect.sct_type != SCT_AIRPT)) {
231                     pr(
232                        "Destination sector is not an airfield!\n");
233                     continue;
234                 }
235                 if (!player->god && (sect.sct_effic < 60))  {
236                     pr(
237                        "That airport still under construction!\n");
238                     continue;
239                 }
240             }
241             break;
242         }
243     }
244     if (trade.trd_type == EF_LAND){
245         while (1) {
246             p = getstring("Destination sector: ", buf);
247             if (!trade_check_ok(lotno, &trade, &tg))
248                 return RET_FAIL;
249             if (p == 0) {
250                 return RET_FAIL;
251             }
252             if (!sarg_xy(p, &sx, &sy) ||
253                 !getsect(sx, sy, &sect)) {
254                 pr("Bad sector designation; try again!\n");
255                 continue;
256             }
257             if (!player->owner) {
258                 pr("You don't own that sector; try again!\n");
259                 continue;
260             }
261             if (!player->god && (sect.sct_type != SCT_HEADQ)) {
262                 pr("Destination sector is not a headquarters!\n");
263                 continue;
264             }
265             if (!player->god && (sect.sct_effic < 60))  {
266                 pr("That headquarters still under construction!\n");    
267                 continue;
268             }
269             break;
270         }
271     }
272     
273     pr("WARNING!  This market issues credit.  If you make more\n");
274     pr("  bids than your treasury can cover at the time of sale,\n");
275     pr("  you can potentially go into financial ruin, and see no\n");
276     pr("  gains.  You have been warned.\n\n");
277     
278     if((p = getstring("How much do you bid: ", buf)) == 0 || *p == 0)
279         return RET_OK;
280     if (!trade_check_ok(lotno, &trade, &tg))
281         return RET_FAIL;
282     bid = atoi(p);
283     if (bid < price)
284         bid = price;
285     if (bid > canspend) {
286         pr("You don't have %.2f to spend!\n", price);
287         return RET_OK;
288     }
289     if (bid > trade.trd_maxprice) {
290         /* Add five minutes to the time if less than 5 minutes left. */
291         time(&now);
292         if (((TRADE_DELAY - (now - trade.trd_markettime)) < 300) &&
293             trade.trd_maxbidder != player->cnum)
294             trade.trd_markettime += 300;
295         trade.trd_maxprice = bid;
296         trade.trd_maxbidder = player->cnum;
297         trade.trd_x = sx;
298         trade.trd_y = sy;
299         pr("Your bid on lot #%d is being considered.\n",lotno);
300         if (!puttrade(lotno, &trade))
301             pr("Problems with the trade file.  Get help\n");
302     } else
303         pr("Your bid wasn't high enough (you need to bid more than someone else.)\n");
304     
305     check_trade();
306     
307     return RET_OK;
308 }
309
310 int
311 check_trade(void)
312 {
313     int n;
314     int j;
315     extern double tradetax;
316     struct nstr_item ni;
317     struct plnstr plane;
318     struct lndstr land;
319     struct natstr *natp;
320     struct trdstr trade;
321     union  trdgenstr tg;
322     time_t now;
323     double subleft;
324     double monleft;
325     double tleft;
326     float price;
327     int saveid;
328     struct lonstr loan;
329     long outstanding;  /* Outstanding debt */
330     long couval;  /* Value of country's goods */
331     int foundloan;
332     
333 /*    logerror("Checking the trades.\n");*/
334     for (n = 0; gettrade(n, &trade); n++) {
335         if (trade.trd_unitid < 0)
336             continue;
337         if (!trade_getitem(&trade, &tg))
338             continue;
339         if (tg.gen.trg_own == 0) {
340             trade.trd_unitid = -1;
341             puttrade(n, &trade);
342             continue;
343         }
344         if (tg.gen.trg_own != trade.trd_owner) {
345             logerror("SOmething weird, tg.gen.trg_own != trade.trd_owner!\n");
346             trade.trd_unitid = -1;
347             puttrade(n, &trade);
348             continue;
349         }
350         
351         if (trade.trd_owner == trade.trd_maxbidder)
352             continue;
353         
354         (void) time(&now);
355         tleft = TRADE_DELAY / 3600.0 - (now - trade.trd_markettime) / 3600.0;
356         if (tleft < 0.0)
357             tleft = 0.0;
358         if (tleft > 0.0)
359             continue;
360         
361         saveid = trade.trd_unitid;
362         trade.trd_unitid = -1;
363         if (!puttrade(n, &trade)) {
364                 logerror("Couldn't save trade after purchase; get help!\n");
365                 continue;
366         }
367
368         monleft = 0;
369         price = trade.trd_maxprice;
370         natp = getnatp(trade.trd_maxbidder);
371         if (natp->nat_money <= 0)
372             monleft = price;
373         if (natp->nat_money < price && natp->nat_money > 0) {
374             monleft = price - (natp->nat_money - 1);
375             natp->nat_money = 1;
376             price = price - monleft;
377         } else if (natp->nat_money > 0) {
378             monleft = 0;
379             natp->nat_money -= price;
380         }
381
382         subleft = monleft;
383
384         if (opt_LOANS) {
385             /* Try to make a loan for the rest from the owner. */
386             if (monleft > 0 && natp->nat_money > 0) {
387                 if ((float)((float)price / (float)(price + monleft)) < 0.1) {
388                     wu(0, trade.trd_maxbidder, "You need at least 10 percent down to purchase something on credit.\n");
389                 } else {
390                     couval = get_couval(trade.trd_maxbidder);
391                     outstanding = get_outstand(trade.trd_maxbidder);
392                     couval = couval - outstanding;
393                     if (couval > monleft) {
394                         /*  Make the loan */
395                         foundloan = 0;
396                         for (j = 0; getloan(j, &loan); j++) {
397                             if (loan.l_status != LS_FREE)
398                                 continue;
399                             foundloan = 1;
400                             break;
401                         }
402                         if (!foundloan)
403                             ef_extend(EF_LOAN, 1);
404                         loan.l_status = LS_SIGNED;
405                         loan.l_loner = trade.trd_owner;
406                         loan.l_lonee = trade.trd_maxbidder;
407                         loan.l_irate = 25;
408                         loan.l_ldur = 4;
409                         loan.l_amtpaid = 0;
410                         loan.l_amtdue = monleft;
411                         time(&loan.l_lastpay);
412                         loan.l_duedate = (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, trade.trd_owner, 1);
419                         wu(0, trade.trd_maxbidder, "You just took loan #%d for $%.2f to cover the cost of your purchase.\n", j, (float)loan.l_amtdue);
420                         wu(0, trade.trd_owner, "You just extended loan #%d to %s to help with the purchase cost.\n", j, cname(trade.trd_maxbidder));
421                     } else {
422                         nreport(trade.trd_maxbidder,N_CREDIT_JUNK, trade.trd_owner, 1);
423                         wu(0, trade.trd_maxbidder, "You don't have enough credit to get a loan.\n");
424                         wu(0, trade.trd_owner, "You just turned down a loan to %s.\n", 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, "%s tried to buy a %s #%d from you for $%.2f\n",
432                cname(trade.trd_maxbidder), trade_nameof(&trade, &tg), 
433                saveid, (float)(price * tradetax));
434             wu(0, trade.trd_owner, "   but couldn't afford it.\n");
435             wu(0, trade.trd_owner, "   Your item was taken off the market.\n");
436             wu(0, trade.trd_maxbidder, 
437                "You tried to buy %s #%d from %s for $%.2f\n",
438                 trade_nameof(&trade, &tg), saveid, cname(trade.trd_owner),
439                 (float)(price * tradetax));
440             wu(0, trade.trd_maxbidder, "but couldn't afford it.\n");
441             continue;
442         }
443
444 /* If we get this far, the sale will go through. */
445 /* Only pay tax on the part you actually get cash for.  As a break,
446    we don't tax the part you have to give a loan on. */
447
448         putnat(natp);
449         natp = getnatp(trade.trd_owner);
450         /* Make sure we subtract the extra amount */
451         natp->nat_money += (roundavg(price * tradetax) - subleft);
452         putnat(natp);
453         switch (trade.trd_type) {
454         case EF_NUKE:
455                 tg.nuk.nuk_x =  trade.trd_x;
456                 tg.nuk.nuk_y =  trade.trd_y;
457                 makelost(EF_NUKE, tg.nuk.nuk_own, tg.nuk.nuk_uid, tg.nuk.nuk_x, tg.nuk.nuk_y);
458                 tg.nuk.nuk_own = trade.trd_maxbidder;
459                 makenotlost(EF_NUKE, tg.nuk.nuk_own, tg.nuk.nuk_uid, tg.nuk.nuk_x, tg.nuk.nuk_y);
460                 break;
461         case EF_PLANE:
462                 if ((tg.pln.pln_flags & PLN_LAUNCHED) == 0) {
463                         tg.pln.pln_x = trade.trd_x;
464                         tg.pln.pln_y = trade.trd_y;
465                 }
466                 makelost(EF_PLANE, tg.pln.pln_own, tg.pln.pln_uid, tg.pln.pln_x, tg.pln.pln_y);
467                 tg.pln.pln_own = trade.trd_maxbidder;
468                 makenotlost(EF_PLANE, tg.pln.pln_own, tg.pln.pln_uid, tg.pln.pln_x, tg.pln.pln_y);
469                 tg.pln.pln_wing = ' ';
470                 /* no cheap version of fly */
471                 if (opt_MOB_ACCESS) {
472                     tg.pln.pln_mobil = -(etu_per_update / sect_mob_neg_factor);
473                 } else {
474                     tg.pln.pln_mobil = 0;
475                 }
476                 tg.pln.pln_mission = 0;
477                 tg.pln.pln_harden = 0;
478                 time(&tg.pln.pln_access);
479                 tg.pln.pln_ship = -1;
480                 tg.pln.pln_land = -1;
481                 break;
482         case EF_SHIP:
483                 takeover_ship(&tg.shp, trade.trd_maxbidder, 0);
484                 break;
485         case EF_LAND:
486                 tg.lnd.lnd_x = trade.trd_x;
487                 tg.lnd.lnd_y = trade.trd_y;
488                 if (tg.lnd.lnd_ship >= 0){
489                         struct shpstr ship;
490                         getship(tg.lnd.lnd_ship, &ship);
491                         ship.shp_nland--;
492                         putship(ship.shp_uid,&ship);
493                 }
494                 makelost(EF_LAND, tg.lnd.lnd_own, tg.lnd.lnd_uid, tg.lnd.lnd_x, tg.lnd.lnd_y);
495                 tg.lnd.lnd_own = trade.trd_maxbidder;
496                 makenotlost(EF_LAND, tg.lnd.lnd_own, tg.lnd.lnd_uid, tg.lnd.lnd_x, tg.lnd.lnd_y);
497                 tg.lnd.lnd_army = ' ';
498                 /* no cheap version of fly */
499                 if (opt_MOB_ACCESS) {
500                     tg.lnd.lnd_mobil = -(etu_per_update / sect_mob_neg_factor);
501                 } else {
502                     tg.lnd.lnd_mobil = 0;
503                 }
504                 tg.lnd.lnd_harden = 0;
505                 time(&tg.lnd.lnd_access);
506                 tg.lnd.lnd_mission = 0;
507                 /* Drop any land units this unit was carrying */
508                 snxtitem_xy(&ni, EF_LAND, tg.lnd.lnd_x, tg.lnd.lnd_y);
509                 while (nxtitem(&ni, (s_char *)&land)) {
510                     if (land.lnd_land != tg.lnd.lnd_uid)
511                         continue;
512                     land.lnd_land = -1;
513                     wu(0, land.lnd_own, "unit #%d dropped in %s\n",
514                        land.lnd_uid,
515                        xyas(land.lnd_x, land.lnd_y, land.lnd_own));
516                     putland(land.lnd_uid, &land);
517                 }
518                 /* Drop any planes this unit was carrying */
519                 snxtitem_xy(&ni, EF_PLANE, tg.lnd.lnd_x, tg.lnd.lnd_y);
520                 while (nxtitem(&ni, (s_char *)&plane)) {
521                     if (plane.pln_flags & PLN_LAUNCHED)
522                         continue;
523                     if (plane.pln_land != land.lnd_uid)
524                         continue;
525                     plane.pln_land = -1;
526                     wu(0, plane.pln_own, "plane #%d dropped in %s\n",
527                        plane.pln_uid,
528                        xyas(plane.pln_x, plane.pln_y, plane.pln_own));
529                     putplane(plane.pln_uid, &plane);
530                 }
531                 tg.lnd.lnd_ship = -1;
532                 tg.lnd.lnd_land = -1;
533                 break;
534         default:
535                 logerror("Bad trade type %d in trade\n", trade.trd_type);
536                 break;
537         }
538         if (!ef_write(trade.trd_type, saveid, (char *)&tg)) {
539                 logerror("Couldn't write unit to disk; seek help.\n");
540                 continue;
541         }
542         nreport(trade.trd_owner, N_MAKE_SALE, trade.trd_maxbidder, 1);
543         wu(0, trade.trd_owner, "%s bought a %s #%d from you for $%.2f\n",
544                 cname(trade.trd_maxbidder), trade_nameof(&trade, &tg), 
545                 saveid, (float)(price * tradetax));
546         wu(0, trade.trd_maxbidder, 
547                 "The bidding is over & you bought %s #%d from %s for $%.2f\n",
548                 trade_nameof(&trade, &tg), saveid, cname(trade.trd_owner),
549                 (float)price);
550     }
551 /*    logerror("Done checking the trades.\n");*/
552     return RET_OK;
553 }
554
555 int
556 ontradingblock(int type, int *ptr)
557           /* Generic pointer */
558 {
559     struct trdstr trade;
560     union trdgenstr tg;
561     int n;
562
563     for (n = 0; gettrade(n, &trade); n++) {
564         if (trade.trd_unitid < 0)
565             continue;
566         if (!trade_getitem(&trade, &tg))
567             continue;
568         if (trade.trd_type != type)
569             continue;
570         if (tg.gen.trg_uid == ((struct genstr *)ptr)->trg_uid)
571             return 1;
572     }
573     return 0;
574 }
575
576 void
577 trdswitchown(int type, int *ptr, int newown)
578           /* Generic pointer */           
579 {
580     struct trdstr trade;
581     union trdgenstr tg;
582     int n;
583
584     for (n = 0; gettrade(n, &trade); n++) {
585         if (trade.trd_unitid < 0)
586             continue;
587         if (!trade_getitem(&trade, &tg))
588             continue;
589         if (trade.trd_type != type)
590             continue;
591         if (tg.gen.trg_uid != ((struct genstr *)ptr)->trg_uid)
592             continue;
593         if (trade.trd_owner == trade.trd_maxbidder)
594             trade.trd_maxbidder = newown;
595         trade.trd_owner = newown;
596         if (newown == 0)
597             trade.trd_unitid = -1;
598         puttrade(n, &trade);
599         return;
600     }
601 }