]> git.pond.sub.org Git - empserver/blob - src/lib/commands/buy.c
License upgrade to GPL version 3 or later
[empserver] / src / lib / commands / buy.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2011, 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  *  buy.c: Buy commodities from other nations
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1986
31  *     Pat Loney, 1992
32  *     Steve McClure, 1996-2000
33  */
34
35 #include <config.h>
36
37 #include "commands.h"
38 #include "commodity.h"
39 #include "item.h"
40 #include "land.h"
41 #include "loan.h"
42 #include "news.h"
43 #include "optlist.h"
44 #include "plane.h"
45 #include "ship.h"
46 #include "trade.h"
47
48 /*
49  * format: buy <COMMODITY>
50  *
51  */
52 int
53 buy(void)
54 {
55     struct sctstr sect;
56     struct natstr *natp;
57     struct comstr comm;
58     struct comstr ncomm;
59     struct comstr comt;
60     struct trdstr tmpt;
61     struct ichrstr *ip;
62     int qty;
63     int o, n;
64     coord x, y;
65     char *p;
66     float bid;
67     time_t now;
68     double tally;
69     double canspend;
70     char buf[1024];
71
72     if (!opt_MARKET) {
73         pr("The market is disabled.\n");
74         return RET_FAIL;
75     }
76     natp = getnatp(player->cnum);
77     ip = whatitem(player->argp[1], "Commodity you want to buy: ");
78     if (!ip)
79         return RET_SYN;
80     display_mark(ip->i_uid, 0);
81     pr("\n");
82     p = getstarg(player->argp[2], "Which lot are you bidding on: ", buf);
83     if (!p)
84         return RET_SYN;
85     if (*p == 0)
86         return RET_SYN;
87     o = atoi(p);
88     if (o < 0)
89         return RET_SYN;
90     if (!getcomm(o, &comm) || comm.com_owner == 0) {
91         pr("Invalid lot number.\n");
92         return RET_OK;
93     }
94     if (comm.com_type != ip->i_uid) {
95         pr("That lot is not of the type you specified.\n");
96         return RET_OK;
97     }
98     if (comm.com_owner == player->cnum) {
99         pr("You can't bid on your own lot.\n");
100         return RET_OK;
101     }
102     if (!(p = getstarg(player->argp[3], "How much per unit: ", buf)))
103         return RET_SYN;
104     bid = atof(p);
105     if (bid <= 0)
106         return RET_FAIL;
107     if (natp->nat_money < bid * comm.com_amount * buytax) {
108         pr("This purchase would cost %.2f, %.2f more than you have.\n",
109            bid * comm.com_amount * buytax,
110            bid * comm.com_amount * buytax - natp->nat_money);
111         return RET_FAIL;
112     }
113 /*  check to see if all of the bids that this player has out plus this new bid
114     would make him go broke.  Ken, I ought to skin you alive for making me code
115     this part up.*/
116     tally = 0.0;
117     for (n = 0; gettrade(n, &tmpt); n++) {
118         if (tmpt.trd_maxbidder == player->cnum &&
119             tmpt.trd_unitid >= 0 && tmpt.trd_owner != player->cnum) {
120             tally += tmpt.trd_price * tradetax;
121         }
122     }
123     for (n = 0; getcomm(n, &comt); n++) {
124         if (comt.com_maxbidder == player->cnum &&
125             comt.com_owner != 0 && comt.com_owner != player->cnum) {
126             tally += comt.com_price * comt.com_amount * buytax;
127         }
128     }
129     canspend = natp->nat_money - tally;
130     getcomm(o, &comm);
131     if (bid * comm.com_amount * buytax > canspend) {
132         pr("You have overextended yourself in the market\n");
133         pr("You can not bid on the current items at that price.\n");
134         return RET_OK;
135     }
136     if (!(p = getstarg(player->argp[4], "destination sector : ", buf)))
137         return RET_SYN;
138     if (!sarg_xy(p, &x, &y))
139         return RET_SYN;
140     if (!getsect(x, y, &sect)) {
141         pr("Could not access sector");
142         return RET_FAIL;
143     }
144     if ((sect.sct_type != SCT_WAREH && sect.sct_type != SCT_HARBR) ||
145         sect.sct_own != player->cnum) {
146         pr("The destination sector is not one of your warehouses.\n");
147         return RET_FAIL;
148     }
149     if (sect.sct_effic < 60) {
150         pr("That sector is under construction.\n");
151         return RET_FAIL;
152     }
153     n = sect.sct_item[ip->i_uid];
154     qty = comm.com_amount;
155     if (qty + n > ITEM_MAX) {
156         pr("That sector cannot hold %d more %s. It currently holds %d.\n",
157            qty, ip->i_name, n);
158         return RET_FAIL;
159     }
160     if (bid * comm.com_amount > natp->nat_money) {
161         pr("You don't have that much to spend!\n");
162         return RET_FAIL;
163     }
164     getcomm(o, &ncomm);
165     if (!ncomm.com_owner) {
166         pr("That lot has been taken off the market.\n");
167         return RET_FAIL;
168     }
169     if (bid > 0.04 + comm.com_price) {
170         comm.com_price = bid;
171         /* Add five minutes to the time if less than 5 minutes */
172         time(&now);
173         if (((MARK_DELAY - (now - comm.com_markettime)) < 300) &&
174             comm.com_maxbidder != player->cnum) {
175             comm.com_markettime += 300;
176             /* Special case - what if so much time has gone by?  Well,
177                Just reset the markettime  so that only 5 minutes are left */
178             if ((MARK_DELAY - (now - comm.com_markettime)) < 0)
179                 comm.com_markettime = (now - (MARK_DELAY - 300));
180         }
181         comm.com_maxbidder = player->cnum;
182         comm.com_x = x;
183         comm.com_y = y;
184         putcomm(o, &comm);
185         pr("Your bid is being considered.\n");
186     } else {
187         pr("Your bid wasn't high enough (you need to bid at least $0.05 higher\n");
188         pr("than the last bid.\n");
189         return RET_OK;
190     }
191
192     check_market();
193
194     return RET_OK;
195 }
196
197 int
198 check_market(void)
199 {
200     struct comstr comm;
201     struct sctstr *sect;
202     struct natstr *natp;
203     int m;
204     int n;
205     time_t now;
206     double tleft;
207     double gain;
208     double price;
209
210     for (n = 0; getcomm(n, &comm); n++) {
211         if (comm.com_maxbidder == comm.com_owner || comm.com_owner == 0)
212             continue;
213         (void)time(&now);
214         tleft = MARK_DELAY / 3600.0 - (now - comm.com_markettime) / 3600.0;
215         if (tleft < 0)
216             tleft = 0;
217         if (tleft > 0.0)
218             continue;
219         if (CANT_HAPPEN(comm.com_type <= I_NONE || comm.com_type > I_MAX))
220             continue;
221         sect = getsectp(comm.com_x, comm.com_y);
222         m = sect->sct_item[comm.com_type];
223
224         price = comm.com_price * comm.com_amount * buytax;
225         gain = comm.com_price * comm.com_amount;
226
227         natp = getnatp(comm.com_maxbidder);
228         if (natp->nat_money < price) {
229             nreport(comm.com_maxbidder, N_WELCH_DEAL, comm.com_owner, 1);
230             wu(0, comm.com_maxbidder,
231                "You didn't have enough cash to cover the cost.\n");
232             wu(0, comm.com_owner,
233                "Sale #%d fell through.  Goods remain on the market.\n", n);
234             comm.com_maxbidder = comm.com_owner;
235         } else if (sect->sct_type != SCT_WAREH
236                    && sect->sct_type != SCT_HARBR) {
237             wu(0, comm.com_maxbidder,
238                "Sector not a warehouse now, sale #%d fell though.\n", n);
239             wu(0, comm.com_owner,
240                "Sale #%d fell through.  Goods remain on the market.\n", n);
241             comm.com_maxbidder = comm.com_owner;
242         } else if (m + comm.com_amount > ITEM_MAX) {
243             wu(0, comm.com_maxbidder,
244                "Warehouse full,  sale #%d fell though.\n", n);
245             wu(0, comm.com_owner,
246                "Sale #%d fell through.  Goods remain on the market.\n", n);
247             comm.com_maxbidder = comm.com_owner;
248         } else {
249             sect->sct_item[comm.com_type] = m + comm.com_amount;
250             putsect(sect);
251             nreport(comm.com_owner, N_MAKE_SALE, comm.com_maxbidder, 1);
252             wu(0, comm.com_owner, "%s bought %d %s from you for $%.2f\n",
253                cname(comm.com_maxbidder), comm.com_amount,
254                ichr[comm.com_type].i_name, gain);
255             wu(0, comm.com_maxbidder,
256                "You just bought %d %s from %s for $%.2f\n",
257                comm.com_amount, ichr[comm.com_type].i_name,
258                cname(comm.com_owner), price);
259             natp->nat_money -= roundavg(price);
260             putnat(natp);
261             natp = getnatp(comm.com_owner);
262             natp->nat_money += roundavg(gain);
263             putnat(natp);
264             comm.com_owner = 0;
265         }
266         comm.com_owner = 0;
267         putcomm(n, &comm);
268     }
269     return RET_OK;
270 }