]> 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-2016, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  trad.c: Buy units/ships/planes/nukes from other nations.
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1986
31  *     Pat Loney, 1992
32  *     Steve McClure, 1996-2000
33  *     Markus Armbruster, 2004-2013
34  */
35
36 #include <config.h>
37
38 #include <ctype.h>
39 #include "chance.h"
40 #include "commands.h"
41 #include "commodity.h"
42 #include "empobj.h"
43 #include "land.h"
44 #include "loan.h"
45 #include "news.h"
46 #include "nuke.h"
47 #include "optlist.h"
48 #include "plane.h"
49 #include "ship.h"
50 #include "trade.h"
51 #include "unit.h"
52
53 /*
54  * format: trade
55  */
56 int
57 trad(void)
58 {
59     struct sctstr sect;
60     struct natstr *natp;
61     struct comstr comt;
62     int lotno;
63     float price;
64     coord sx, sy;
65     int n;
66     char *p;
67     struct nstr_item ni;
68     struct trdstr trade;
69     struct trdstr tmpt;
70     union empobj_storage tg;
71     double canspend;
72     time_t now, tleft;
73     int bid;
74     double tally;
75     int i;
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 = trade.trd_markettime + TRADE_DELAY - now;
102         if (tleft < 0)
103             tleft = 0;
104         pr("$%7d  %2d %5.2f hrs ",
105            trade.trd_price, trade.trd_maxbidder, tleft / 3600.0);
106         trade_desc(&tg.gen);    /* XXX */
107         pr("\n");
108         if (trade.trd_owner == player->cnum && !player->god)
109             pr(" (your own lot)\n");
110         n++;
111     }
112     if (n == 0) {
113         pr("Nothing to buy at the moment...\n");
114         return RET_OK;
115     }
116     p = getstring("Which lot to buy: ", buf);
117     if (!p || !*p)
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 (i = 0; gettrade(i, &tmpt); i++) {
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 (i = 0; getcomm(i, &comt); i++) {
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 trade */
181     if (((trade.trd_type == EF_PLANE) && !pln_is_in_orbit(&tg.plane))
182         || (trade.trd_type == EF_NUKE)) {
183         while (1) {
184             p = getstring("Destination sector: ", buf);
185             if (!p)
186                 return RET_FAIL;
187             if (!trade_check_ok(&trade, &tg.gen))
188                 return RET_FAIL;
189             if (!sarg_xy(p, &sx, &sy) || !getsect(sx, sy, &sect)) {
190                 pr("Bad sector designation; try again!\n");
191                 continue;
192             }
193             if (!player->owner) {
194                 pr("You don't own that sector; try again!\n");
195                 continue;
196             }
197             if (!(plchr[tg.plane.pln_type].pl_flags & P_V)) {
198                 if (!player->god && (sect.sct_type != SCT_AIRPT)) {
199                     pr("Destination sector is not an airfield!\n");
200                     continue;
201                 }
202                 if (!player->god && (sect.sct_effic < 60)) {
203                     pr("That airport still under construction!\n");
204                     continue;
205                 }
206             }
207             break;
208         }
209     } else if (trade.trd_type == EF_LAND) {
210         while (1) {
211             p = getstring("Destination sector: ", buf);
212             if (!p)
213                 return RET_FAIL;
214             if (!trade_check_ok(&trade, &tg.gen))
215                 return RET_FAIL;
216             if (!sarg_xy(p, &sx, &sy) || !getsect(sx, sy, &sect)) {
217                 pr("Bad sector designation; try again!\n");
218                 continue;
219             }
220             if (!player->owner) {
221                 pr("You don't own that sector; try again!\n");
222                 continue;
223             }
224             if (!player->god && (sect.sct_type != SCT_HEADQ)) {
225                 pr("Destination sector is not a headquarters!\n");
226                 continue;
227             }
228             if (!player->god && (sect.sct_effic < 60)) {
229                 pr("That headquarters still under construction!\n");
230                 continue;
231             }
232             break;
233         }
234     } else {
235         /* This trade doesn't teleport; make destination invalid */
236         sx = 1;
237         sy = 0;
238     }
239
240     p = getstring("How much do you bid: ", buf);
241     if (!p || !*p)
242         return RET_OK;
243     if (!trade_check_ok(&trade, &tg.gen))
244         return RET_FAIL;
245     bid = atoi(p);
246     if (bid < price)
247         bid = price;
248     if (bid > canspend) {
249         pr("You don't have %.2f to spend!\n", price);
250         return RET_OK;
251     }
252     if (bid > trade.trd_price) {
253         time(&now);
254         if (trade.trd_markettime  + TRADE_DELAY - now < minutes(5) &&
255             trade.trd_maxbidder != player->cnum)
256             trade.trd_markettime = now + minutes(5) - TRADE_DELAY;
257         trade.trd_price = bid;
258         trade.trd_maxbidder = player->cnum;
259         trade.trd_x = sx;
260         trade.trd_y = sy;
261         pr("Your bid on lot #%d is being considered.\n", lotno);
262         if (!puttrade(lotno, &trade))
263             pr("Problems with the trade file.  Get help\n");
264     } else
265         pr("Your bid wasn't high enough (you need to bid more than someone else.)\n");
266
267     check_trade();
268
269     return RET_OK;
270 }
271
272 int
273 check_trade(void)
274 {
275     int n;
276     struct natstr *natp;
277     struct trdstr trade;
278     union empobj_storage tg;
279     time_t now;
280     int price;
281     int saveid;
282     natid seller;
283
284     for (n = 0; gettrade(n, &trade); n++) {
285         if (trade.trd_unitid < 0)
286             continue;
287         if (!trade_getitem(&trade, &tg))
288             continue;
289         if (tg.gen.own == 0) {
290             trade.trd_owner = 0;
291             trade.trd_unitid = -1;
292             puttrade(n, &trade);
293             continue;
294         }
295         if (tg.gen.own != trade.trd_owner) {
296             logerror("Something weird, tg.gen.own != trade.trd_owner!\n");
297             trade.trd_owner = 0;
298             trade.trd_unitid = -1;
299             puttrade(n, &trade);
300             continue;
301         }
302
303         if (trade.trd_owner == trade.trd_maxbidder)
304             continue;
305
306         (void)time(&now);
307         if (trade.trd_markettime + TRADE_DELAY > now)
308             continue;
309
310         saveid = trade.trd_unitid;
311         seller = trade.trd_owner;
312         trade.trd_owner = 0;
313         trade.trd_unitid = -1;
314         if (!puttrade(n, &trade)) {
315             logerror("Couldn't save trade after purchase; get help!\n");
316             continue;
317         }
318
319         price = trade.trd_price;
320         natp = getnatp(trade.trd_maxbidder);
321         if (natp->nat_money < price) {
322             nreport(trade.trd_maxbidder, N_WELCH_DEAL, seller, 1);
323             wu(0, seller,
324                "%s tried to buy a %s #%d from you for $%.2f\n",
325                cname(trade.trd_maxbidder), trade_nameof(&trade, &tg.gen),
326                saveid, price * tradetax);
327             wu(0, seller, "   but couldn't afford it.\n");
328             wu(0, seller,
329                "   Your item was taken off the market.\n");
330             wu(0, trade.trd_maxbidder,
331                "You tried to buy %s #%d from %s for $%d\n",
332                trade_nameof(&trade, &tg.gen), saveid, cname(seller),
333                price);
334             wu(0, trade.trd_maxbidder, "but couldn't afford it.\n");
335             continue;
336         }
337
338 /* If we get this far, the sale will go through. */
339
340         natp->nat_money -= price;
341         putnat(natp);
342
343         natp = getnatp(seller);
344         natp->nat_money += roundavg(price * tradetax);
345         putnat(natp);
346
347         switch (trade.trd_type) {
348         case EF_NUKE:
349             tg.nuke.nuk_x = trade.trd_x;
350             tg.nuke.nuk_y = trade.trd_y;
351             tg.nuke.nuk_plane = -1;
352             break;
353         case EF_PLANE:
354             if (!pln_is_in_orbit(&tg.plane)) {
355                 tg.plane.pln_x = trade.trd_x;
356                 tg.plane.pln_y = trade.trd_y;
357             }
358             if (opt_MOB_ACCESS) {
359                 tg.plane.pln_mobil = -(etu_per_update / sect_mob_neg_factor);
360                 game_tick_to_now(&tg.plane.pln_access);
361             } else {
362                 tg.plane.pln_mobil = 0;
363             }
364             tg.plane.pln_harden = 0;
365             tg.plane.pln_ship = -1;
366             tg.plane.pln_land = -1;
367             break;
368         case EF_SHIP:
369             break;
370         case EF_LAND:
371             tg.land.lnd_x = trade.trd_x;
372             tg.land.lnd_y = trade.trd_y;
373             if (opt_MOB_ACCESS) {
374                 tg.land.lnd_mobil = -(etu_per_update / sect_mob_neg_factor);
375                 game_tick_to_now(&tg.land.lnd_access);
376             } else {
377                 tg.land.lnd_mobil = 0;
378             }
379             tg.land.lnd_harden = 0;
380             unit_drop_cargo(&tg.gen, 0);
381             tg.land.lnd_ship = -1;
382             tg.land.lnd_land = -1;
383             break;
384         default:
385             logerror("Bad trade type %d in trade\n", trade.trd_type);
386             break;
387         }
388         unit_give_away(&tg.gen, trade.trd_maxbidder, 0);
389         put_empobj(trade.trd_type, saveid, &tg.gen);
390
391         nreport(seller, N_MAKE_SALE, trade.trd_maxbidder, 1);
392         wu(0, seller, "%s bought %s #%d from you for $%.2f\n",
393            cname(trade.trd_maxbidder), trade_nameof(&trade, &tg.gen),
394            saveid, price * tradetax);
395         wu(0, trade.trd_maxbidder,
396            "The bidding is over & you bought %s #%d from %s for $%d\n",
397            trade_nameof(&trade, &tg.gen), saveid, cname(seller),
398            price);
399     }
400     return RET_OK;
401 }
402
403 int
404 ontradingblock(int type, void *ptr)
405 {
406     struct trdstr trade;
407     union empobj_storage tg;
408     int n;
409
410     for (n = 0; gettrade(n, &trade); n++) {
411         if (trade.trd_unitid < 0)
412             continue;
413         if (!trade_getitem(&trade, &tg))
414             continue;
415         if (trade.trd_type != type)
416             continue;
417         if (tg.gen.uid == ((struct empobj *)ptr)->uid)
418             return 1;
419     }
420     return 0;
421 }
422
423 void
424 trdswitchown(int type, struct empobj *obj, int newown)
425 {
426     struct trdstr trade;
427     union empobj_storage tg;
428     int n;
429
430     for (n = 0; gettrade(n, &trade); n++) {
431         if (trade.trd_unitid < 0)
432             continue;
433         if (!trade_getitem(&trade, &tg))
434             continue;
435         if (trade.trd_type != type)
436             continue;
437         if (tg.gen.uid != obj->uid)
438             continue;
439         if (trade.trd_owner == trade.trd_maxbidder)
440             trade.trd_maxbidder = newown;
441         trade.trd_owner = newown;
442         if (newown == 0)
443             trade.trd_unitid = -1;
444         puttrade(n, &trade);
445         return;
446     }
447 }