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