]> git.pond.sub.org Git - empserver/blob - src/lib/commands/trad.c
dcce809f2f098363376b5cf507ab9e64b4049936
[empserver] / src / lib / commands / trad.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, 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 <config.h>
37
38 #include <ctype.h>
39 #include "misc.h"
40 #include "sect.h"
41 #include "nat.h"
42 #include "news.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, &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     if ((p = getstring("How much do you bid: ", buf)) == 0 || *p == 0)
263         return RET_OK;
264     if (!trade_check_ok(&trade, &tg))
265         return RET_FAIL;
266     bid = atoi(p);
267     if (bid < price)
268         bid = price;
269     if (bid > canspend) {
270         pr("You don't have %.2f to spend!\n", price);
271         return RET_OK;
272     }
273     if (bid > trade.trd_price) {
274         /* Add five minutes to the time if less than 5 minutes left. */
275         time(&now);
276         if (((TRADE_DELAY - (now - trade.trd_markettime)) < 300) &&
277             trade.trd_maxbidder != player->cnum)
278             trade.trd_markettime += 300;
279         trade.trd_price = bid;
280         trade.trd_maxbidder = player->cnum;
281         trade.trd_x = sx;
282         trade.trd_y = sy;
283         pr("Your bid on lot #%d is being considered.\n", lotno);
284         if (!puttrade(lotno, &trade))
285             pr("Problems with the trade file.  Get help\n");
286     } else
287         pr("Your bid wasn't high enough (you need to bid more than someone else.)\n");
288
289     check_trade();
290
291     return RET_OK;
292 }
293
294 int
295 check_trade(void)
296 {
297     int n;
298     struct nstr_item ni;
299     struct plnstr plane;
300     struct lndstr land;
301     struct natstr *natp;
302     struct trdstr trade;
303     union trdgenstr tg;
304     time_t now;
305     double subleft;
306     double monleft;
307     double tleft;
308     float price;
309     int saveid;
310
311 /*    logerror("Checking the trades.\n");*/
312     for (n = 0; gettrade(n, &trade); n++) {
313         if (trade.trd_unitid < 0)
314             continue;
315         if (!trade_getitem(&trade, &tg))
316             continue;
317         if (tg.gen.trg_own == 0) {
318             trade.trd_unitid = -1;
319             puttrade(n, &trade);
320             continue;
321         }
322         if (tg.gen.trg_own != trade.trd_owner) {
323             logerror("Something weird, tg.gen.trg_own != trade.trd_owner!\n");
324             trade.trd_unitid = -1;
325             puttrade(n, &trade);
326             continue;
327         }
328
329         if (trade.trd_owner == trade.trd_maxbidder)
330             continue;
331
332         (void)time(&now);
333         tleft =
334             TRADE_DELAY / 3600.0 - (now - trade.trd_markettime) / 3600.0;
335         if (tleft < 0.0)
336             tleft = 0.0;
337         if (tleft > 0.0)
338             continue;
339
340         saveid = trade.trd_unitid;
341         trade.trd_unitid = -1;
342         if (!puttrade(n, &trade)) {
343             logerror("Couldn't save trade after purchase; get help!\n");
344             continue;
345         }
346
347         monleft = 0;
348         price = trade.trd_price;
349         natp = getnatp(trade.trd_maxbidder);
350         if (natp->nat_money <= 0)
351             monleft = price;
352         if (natp->nat_money < price && natp->nat_money > 0) {
353             monleft = price - (natp->nat_money - 1);
354             natp->nat_money = 1;
355             price = price - monleft;
356         } else if (natp->nat_money > 0) {
357             monleft = 0;
358             natp->nat_money -= price;
359         }
360
361         subleft = monleft;
362
363         if (monleft > 0) {
364             nreport(trade.trd_maxbidder, N_WELCH_DEAL, trade.trd_owner, 1);
365             wu(0, trade.trd_owner,
366                "%s tried to buy a %s #%d from you for $%.2f\n",
367                cname(trade.trd_maxbidder), trade_nameof(&trade, &tg),
368                saveid, (float)(price * tradetax));
369             wu(0, trade.trd_owner, "   but couldn't afford it.\n");
370             wu(0, trade.trd_owner,
371                "   Your item was taken off the market.\n");
372             wu(0, trade.trd_maxbidder,
373                "You tried to buy %s #%d from %s for $%.2f\n",
374                trade_nameof(&trade, &tg), saveid, cname(trade.trd_owner),
375                (float)(price * tradetax));
376             wu(0, trade.trd_maxbidder, "but couldn't afford it.\n");
377             continue;
378         }
379
380 /* If we get this far, the sale will go through. */
381 /* Only pay tax on the part you actually get cash for.  As a break,
382    we don't tax the part you have to give a loan on. */
383
384         putnat(natp);
385         natp = getnatp(trade.trd_owner);
386         /* Make sure we subtract the extra amount */
387         natp->nat_money += (roundavg(price * tradetax) - subleft);
388         putnat(natp);
389         switch (trade.trd_type) {
390         case EF_NUKE:
391             tg.nuk.nuk_x = trade.trd_x;
392             tg.nuk.nuk_y = trade.trd_y;
393             makelost(EF_NUKE, tg.nuk.nuk_own, tg.nuk.nuk_uid, tg.nuk.nuk_x,
394                      tg.nuk.nuk_y);
395             tg.nuk.nuk_own = trade.trd_maxbidder;
396             makenotlost(EF_NUKE, tg.nuk.nuk_own, tg.nuk.nuk_uid,
397                         tg.nuk.nuk_x, tg.nuk.nuk_y);
398             break;
399         case EF_PLANE:
400             if ((tg.pln.pln_flags & PLN_LAUNCHED) == 0) {
401                 tg.pln.pln_x = trade.trd_x;
402                 tg.pln.pln_y = trade.trd_y;
403             }
404             makelost(EF_PLANE, tg.pln.pln_own, tg.pln.pln_uid,
405                      tg.pln.pln_x, tg.pln.pln_y);
406             tg.pln.pln_own = trade.trd_maxbidder;
407             makenotlost(EF_PLANE, tg.pln.pln_own, tg.pln.pln_uid,
408                         tg.pln.pln_x, tg.pln.pln_y);
409             tg.pln.pln_wing = ' ';
410             /* no cheap version of fly */
411             if (opt_MOB_ACCESS) {
412                 tg.pln.pln_mobil = -(etu_per_update / sect_mob_neg_factor);
413             } else {
414                 tg.pln.pln_mobil = 0;
415             }
416             tg.pln.pln_mission = 0;
417             tg.pln.pln_harden = 0;
418             time(&tg.pln.pln_access);
419             tg.pln.pln_ship = -1;
420             tg.pln.pln_land = -1;
421             break;
422         case EF_SHIP:
423             takeover_ship(&tg.shp, trade.trd_maxbidder, 0);
424             break;
425         case EF_LAND:
426             tg.lnd.lnd_x = trade.trd_x;
427             tg.lnd.lnd_y = trade.trd_y;
428             if (tg.lnd.lnd_ship >= 0) {
429                 struct shpstr ship;
430                 getship(tg.lnd.lnd_ship, &ship);
431                 ship.shp_nland--;
432                 putship(ship.shp_uid, &ship);
433             }
434             makelost(EF_LAND, tg.lnd.lnd_own, tg.lnd.lnd_uid, tg.lnd.lnd_x,
435                      tg.lnd.lnd_y);
436             tg.lnd.lnd_own = trade.trd_maxbidder;
437             makenotlost(EF_LAND, tg.lnd.lnd_own, tg.lnd.lnd_uid,
438                         tg.lnd.lnd_x, tg.lnd.lnd_y);
439             tg.lnd.lnd_army = ' ';
440             /* no cheap version of fly */
441             if (opt_MOB_ACCESS) {
442                 tg.lnd.lnd_mobil = -(etu_per_update / sect_mob_neg_factor);
443             } else {
444                 tg.lnd.lnd_mobil = 0;
445             }
446             tg.lnd.lnd_harden = 0;
447             time(&tg.lnd.lnd_access);
448             tg.lnd.lnd_mission = 0;
449             /* Drop any land units this unit was carrying */
450             snxtitem_xy(&ni, EF_LAND, tg.lnd.lnd_x, tg.lnd.lnd_y);
451             while (nxtitem(&ni, &land)) {
452                 if (land.lnd_land != tg.lnd.lnd_uid)
453                     continue;
454                 land.lnd_land = -1;
455                 wu(0, land.lnd_own, "unit #%d dropped in %s\n",
456                    land.lnd_uid,
457                    xyas(land.lnd_x, land.lnd_y, land.lnd_own));
458                 putland(land.lnd_uid, &land);
459             }
460             /* Drop any planes this unit was carrying */
461             snxtitem_xy(&ni, EF_PLANE, tg.lnd.lnd_x, tg.lnd.lnd_y);
462             while (nxtitem(&ni, &plane)) {
463                 if (plane.pln_flags & PLN_LAUNCHED)
464                     continue;
465                 if (plane.pln_land != land.lnd_uid)
466                     continue;
467                 plane.pln_land = -1;
468                 wu(0, plane.pln_own, "plane #%d dropped in %s\n",
469                    plane.pln_uid,
470                    xyas(plane.pln_x, plane.pln_y, plane.pln_own));
471                 putplane(plane.pln_uid, &plane);
472             }
473             tg.lnd.lnd_ship = -1;
474             tg.lnd.lnd_land = -1;
475             break;
476         default:
477             logerror("Bad trade type %d in trade\n", trade.trd_type);
478             break;
479         }
480         if (!ef_write(trade.trd_type, saveid, &tg)) {
481             logerror("Couldn't write unit to disk; seek help.\n");
482             continue;
483         }
484         nreport(trade.trd_owner, N_MAKE_SALE, trade.trd_maxbidder, 1);
485         wu(0, trade.trd_owner, "%s bought a %s #%d from you for $%.2f\n",
486            cname(trade.trd_maxbidder), trade_nameof(&trade, &tg),
487            saveid, (float)(price * tradetax));
488         wu(0, trade.trd_maxbidder,
489            "The bidding is over & you bought %s #%d from %s for $%.2f\n",
490            trade_nameof(&trade, &tg), saveid, cname(trade.trd_owner),
491            (float)price);
492     }
493 /*    logerror("Done checking the trades.\n");*/
494     return RET_OK;
495 }
496
497 int
498 ontradingblock(int type, int *ptr)
499           /* Generic pointer */
500 {
501     struct trdstr trade;
502     union trdgenstr tg;
503     int n;
504
505     for (n = 0; gettrade(n, &trade); n++) {
506         if (trade.trd_unitid < 0)
507             continue;
508         if (!trade_getitem(&trade, &tg))
509             continue;
510         if (trade.trd_type != type)
511             continue;
512         if (tg.gen.trg_uid == ((struct genstr *)ptr)->trg_uid)
513             return 1;
514     }
515     return 0;
516 }
517
518 void
519 trdswitchown(int type, int *ptr, int newown)
520           /* Generic pointer */
521 {
522     struct trdstr trade;
523     union trdgenstr tg;
524     int n;
525
526     for (n = 0; gettrade(n, &trade); n++) {
527         if (trade.trd_unitid < 0)
528             continue;
529         if (!trade_getitem(&trade, &tg))
530             continue;
531         if (trade.trd_type != type)
532             continue;
533         if (tg.gen.trg_uid != ((struct genstr *)ptr)->trg_uid)
534             continue;
535         if (trade.trd_owner == trade.trd_maxbidder)
536             trade.trd_maxbidder = newown;
537         trade.trd_owner = newown;
538         if (newown == 0)
539             trade.trd_unitid = -1;
540         puttrade(n, &trade);
541         return;
542     }
543 }