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