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