]> 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-2009, 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 #include "unit.h"
51
52 /*
53  * format: trade
54  */
55 int
56 trad(void)
57 {
58     struct sctstr sect;
59     struct natstr *natp;
60     struct comstr comt;
61     int lotno;
62     float price;
63     coord sx, sy;
64     int n;
65     char *p;
66     struct nstr_item ni;
67     struct trdstr trade;
68     struct trdstr tmpt;
69     union empobj_storage tg;
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         pr(" %3d ", ni.cur);
100         (void)time(&now);
101         tleft =
102             TRADE_DELAY / 3600.0 - (now - trade.trd_markettime) / 3600.0;
103         if (tleft < 0.0)
104             tleft = 0.0;
105         pr("$%7ld  %2d %5.2f hrs ",
106            trade.trd_price, trade.trd_maxbidder, tleft);
107         (void)trade_desc(&trade, &tg);  /* XXX */
108         pr("\n");
109         if (trade.trd_owner == player->cnum && !player->god)
110             pr(" (your own lot)\n");
111         n++;
112     }
113     if (n == 0) {
114         pr("Nothing to buy at the moment...\n");
115         return RET_OK;
116     }
117     if ((p = getstring("Which lot to buy: ", buf)) == 0 || *p == 0)
118         return RET_OK;
119     if (isdigit(*p) == 0)
120         return RET_OK;
121     lotno = atoi(p);
122     if (lotno < 0 || lotno >= ni.cur) {
123         pr("Bad lot number\n");
124         return RET_OK;
125     }
126     if (!gettrade(lotno, &trade)) {
127         pr("No such lot number\n");
128         return RET_OK;
129     }
130     if (trade.trd_unitid < 0) {
131         pr("Invalid lot number.\n");
132         return RET_OK;
133     }
134     if (!trade_getitem(&trade, &tg)) {
135         pr("Can't find trade #%d!\n", trade.trd_unitid);
136         trade.trd_owner = 0;
137         trade.trd_unitid = -1;
138         if (!puttrade(lotno, &trade)) {
139             logerror("trad: can't write trade");
140             pr("Couldn't save after getitem failed; get help!\n");
141             return RET_FAIL;
142         }
143         return RET_OK;
144     }
145     switch (trade.trd_type) {
146     case EF_NUKE:
147     case EF_PLANE:
148     case EF_SHIP:
149     case EF_LAND:
150         break;
151     default:
152         pr("Bad unit type on lot number %d\n", lotno);
153         return RET_FAIL;
154     }
155     if (trade.trd_owner == player->cnum) {
156         pr("You can't buy from yourself!\n");
157         return RET_OK;
158     }
159     price = trade.trd_price;
160     natp = getnatp(player->cnum);
161     if (natp->nat_money < price) {
162         pr("You don't have %.2f to spend!\n", price);
163         return RET_OK;
164     }
165     tally = 0.0;
166     for (q = 0; gettrade(q, &tmpt); q++) {
167         if (tmpt.trd_maxbidder == player->cnum &&
168             tmpt.trd_unitid >= 0 && tmpt.trd_owner != player->cnum) {
169             tally += tmpt.trd_price * tradetax;
170         }
171     }
172     for (q = 0; getcomm(q, &comt); q++) {
173         if (comt.com_maxbidder == player->cnum &&
174             comt.com_owner != 0 && comt.com_owner != player->cnum) {
175             tally += (comt.com_price * comt.com_amount) * buytax;
176         }
177     }
178     canspend = natp->nat_money - tally;
179     /*
180      * Find the destination sector for the plane before the trade is
181      * actually made, except for satellites in orbit.  Must be owned
182      * and must be a 60% airfield (except for VTOL planes).
183      */
184     if (((trade.trd_type == EF_PLANE) && !pln_is_in_orbit(&tg.plane))
185         || (trade.trd_type == EF_NUKE)) {
186         while (1) {
187             p = getstring("Destination sector: ", buf);
188             if (!trade_check_ok(&trade, &tg))
189                 return RET_FAIL;
190             if (p == 0) {
191                 return RET_FAIL;
192             }
193             if (!sarg_xy(p, &sx, &sy) || !getsect(sx, sy, &sect)) {
194                 pr("Bad sector designation; try again!\n");
195                 continue;
196             }
197             if (!player->owner) {
198                 pr("You don't own that sector; try again!\n");
199                 continue;
200             }
201             if (!(plchr[tg.plane.pln_type].pl_flags & P_V)) {
202                 if (!player->god && (sect.sct_type != SCT_AIRPT)) {
203                     pr("Destination sector is not an airfield!\n");
204                     continue;
205                 }
206                 if (!player->god && (sect.sct_effic < 60)) {
207                     pr("That airport still under construction!\n");
208                     continue;
209                 }
210             }
211             break;
212         }
213     }
214     if (trade.trd_type == EF_LAND) {
215         while (1) {
216             p = getstring("Destination sector: ", buf);
217             if (!trade_check_ok(&trade, &tg))
218                 return RET_FAIL;
219             if (p == 0) {
220                 return RET_FAIL;
221             }
222             if (!sarg_xy(p, &sx, &sy) || !getsect(sx, sy, &sect)) {
223                 pr("Bad sector designation; try again!\n");
224                 continue;
225             }
226             if (!player->owner) {
227                 pr("You don't own that sector; try again!\n");
228                 continue;
229             }
230             if (!player->god && (sect.sct_type != SCT_HEADQ)) {
231                 pr("Destination sector is not a headquarters!\n");
232                 continue;
233             }
234             if (!player->god && (sect.sct_effic < 60)) {
235                 pr("That headquarters still under construction!\n");
236                 continue;
237             }
238             break;
239         }
240     }
241
242     if ((p = getstring("How much do you bid: ", buf)) == 0 || *p == 0)
243         return RET_OK;
244     if (!trade_check_ok(&trade, &tg))
245         return RET_FAIL;
246     bid = atoi(p);
247     if (bid < price)
248         bid = price;
249     if (bid > canspend) {
250         pr("You don't have %.2f to spend!\n", price);
251         return RET_OK;
252     }
253     if (bid > trade.trd_price) {
254         /* Add five minutes to the time if less than 5 minutes left. */
255         time(&now);
256         if (((TRADE_DELAY - (now - trade.trd_markettime)) < 300) &&
257             trade.trd_maxbidder != player->cnum)
258             trade.trd_markettime += 300;
259         trade.trd_price = bid;
260         trade.trd_maxbidder = player->cnum;
261         trade.trd_x = sx;
262         trade.trd_y = sy;
263         pr("Your bid on lot #%d is being considered.\n", lotno);
264         if (!puttrade(lotno, &trade))
265             pr("Problems with the trade file.  Get help\n");
266     } else
267         pr("Your bid wasn't high enough (you need to bid more than someone else.)\n");
268
269     check_trade();
270
271     return RET_OK;
272 }
273
274 int
275 check_trade(void)
276 {
277     int n;
278     struct natstr *natp;
279     struct trdstr trade;
280     union empobj_storage tg;
281     time_t now;
282     double tleft;
283     float price;
284     int saveid;
285
286     for (n = 0; gettrade(n, &trade); n++) {
287         if (trade.trd_unitid < 0)
288             continue;
289         if (!trade_getitem(&trade, &tg))
290             continue;
291         if (tg.gen.own == 0) {
292             trade.trd_owner = 0;
293             trade.trd_unitid = -1;
294             puttrade(n, &trade);
295             continue;
296         }
297         if (tg.gen.own != trade.trd_owner) {
298             logerror("Something weird, tg.gen.own != trade.trd_owner!\n");
299             trade.trd_owner = 0;
300             trade.trd_unitid = -1;
301             puttrade(n, &trade);
302             continue;
303         }
304
305         if (trade.trd_owner == trade.trd_maxbidder)
306             continue;
307
308         (void)time(&now);
309         tleft =
310             TRADE_DELAY / 3600.0 - (now - trade.trd_markettime) / 3600.0;
311         if (tleft < 0.0)
312             tleft = 0.0;
313         if (tleft > 0.0)
314             continue;
315
316         saveid = trade.trd_unitid;
317         trade.trd_owner = 0;
318         trade.trd_unitid = -1;
319         if (!puttrade(n, &trade)) {
320             logerror("Couldn't save trade after purchase; get help!\n");
321             continue;
322         }
323
324         price = trade.trd_price;
325         natp = getnatp(trade.trd_maxbidder);
326         if (natp->nat_money < price) {
327             nreport(trade.trd_maxbidder, N_WELCH_DEAL, trade.trd_owner, 1);
328             wu(0, trade.trd_owner,
329                "%s tried to buy a %s #%d from you for $%.2f\n",
330                cname(trade.trd_maxbidder), trade_nameof(&trade, &tg),
331                saveid, price * tradetax);
332             wu(0, trade.trd_owner, "   but couldn't afford it.\n");
333             wu(0, trade.trd_owner,
334                "   Your item was taken off the market.\n");
335             wu(0, trade.trd_maxbidder,
336                "You tried to buy %s #%d from %s for $%.2f\n",
337                trade_nameof(&trade, &tg), saveid, cname(trade.trd_owner),
338                price);
339             wu(0, trade.trd_maxbidder, "but couldn't afford it.\n");
340             continue;
341         }
342
343 /* If we get this far, the sale will go through. */
344
345         natp->nat_money -= price;
346         putnat(natp);
347
348         natp = getnatp(trade.trd_owner);
349         natp->nat_money += roundavg(price * tradetax);
350         putnat(natp);
351
352         switch (trade.trd_type) {
353         case EF_NUKE:
354             tg.nuke.nuk_x = trade.trd_x;
355             tg.nuke.nuk_y = trade.trd_y;
356             tg.nuke.nuk_plane = -1;
357             break;
358         case EF_PLANE:
359             if (!pln_is_in_orbit(&tg.plane)) {
360                 tg.plane.pln_x = trade.trd_x;
361                 tg.plane.pln_y = trade.trd_y;
362             }
363             if (opt_MOB_ACCESS) {
364                 tg.plane.pln_mobil = -(etu_per_update / sect_mob_neg_factor);
365                 game_tick_to_now(&tg.plane.pln_access);
366             } else {
367                 tg.plane.pln_mobil = 0;
368             }
369             tg.plane.pln_harden = 0;
370             tg.plane.pln_ship = -1;
371             tg.plane.pln_land = -1;
372             break;
373         case EF_SHIP:
374             break;
375         case EF_LAND:
376             tg.land.lnd_x = trade.trd_x;
377             tg.land.lnd_y = trade.trd_y;
378             if (opt_MOB_ACCESS) {
379                 tg.land.lnd_mobil = -(etu_per_update / sect_mob_neg_factor);
380                 game_tick_to_now(&tg.land.lnd_access);
381             } else {
382                 tg.land.lnd_mobil = 0;
383             }
384             tg.land.lnd_harden = 0;
385             unit_drop_cargo(&tg.gen, 0);
386             tg.land.lnd_ship = -1;
387             tg.land.lnd_land = -1;
388             break;
389         default:
390             logerror("Bad trade type %d in trade\n", trade.trd_type);
391             break;
392         }
393         unit_give_away(&tg.gen, trade.trd_maxbidder, 0);
394         put_empobj(trade.trd_type, saveid, &tg.gen);
395
396         nreport(trade.trd_owner, N_MAKE_SALE, trade.trd_maxbidder, 1);
397         wu(0, trade.trd_owner, "%s bought a %s #%d from you for $%.2f\n",
398            cname(trade.trd_maxbidder), trade_nameof(&trade, &tg),
399            saveid, price * tradetax);
400         wu(0, trade.trd_maxbidder,
401            "The bidding is over & you bought %s #%d from %s for $%.2f\n",
402            trade_nameof(&trade, &tg), saveid, cname(trade.trd_owner),
403            price);
404    }
405     return RET_OK;
406 }
407
408 int
409 ontradingblock(int type, void *ptr)
410 {
411     struct trdstr trade;
412     union empobj_storage tg;
413     int n;
414
415     for (n = 0; gettrade(n, &trade); n++) {
416         if (trade.trd_unitid < 0)
417             continue;
418         if (!trade_getitem(&trade, &tg))
419             continue;
420         if (trade.trd_type != type)
421             continue;
422         if (tg.gen.uid == ((struct empobj *)ptr)->uid)
423             return 1;
424     }
425     return 0;
426 }
427
428 void
429 trdswitchown(int type, void *ptr, int newown)
430 {
431     struct trdstr trade;
432     union empobj_storage tg;
433     int n;
434
435     for (n = 0; gettrade(n, &trade); n++) {
436         if (trade.trd_unitid < 0)
437             continue;
438         if (!trade_getitem(&trade, &tg))
439             continue;
440         if (trade.trd_type != type)
441             continue;
442         if (tg.gen.uid != ((struct empobj *)ptr)->uid)
443             continue;
444         if (trade.trd_owner == trade.trd_maxbidder)
445             trade.trd_maxbidder = newown;
446         trade.trd_owner = newown;
447         if (newown == 0)
448             trade.trd_unitid = -1;
449         puttrade(n, &trade);
450         return;
451     }
452 }