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