1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2021, 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  *     Markus Armbruster, 2004-2021
34  */
35 
36 #include <config.h>
37 
38 #include "chance.h"
39 #include "commands.h"
40 #include "commodity.h"
41 #include "item.h"
42 #include "news.h"
43 #include "optlist.h"
44 #include "trade.h"
45 
46 /*
47  * format: buy <COMMODITY>
48  *
49  */
50 int
c_buy(void)51 c_buy(void)
52 {
53     struct sctstr sect;
54     struct natstr *natp;
55     struct comstr comm;
56     struct comstr comt;
57     struct trdstr tmpt;
58     struct ichrstr *ip;
59     int qty;
60     int o, n;
61     coord x, y;
62     char *p;
63     float bid;
64     time_t now;
65     double tally;
66     double canspend;
67     char buf[1024];
68 
69     if (!opt_MARKET) {
70 	pr("The market is disabled.\n");
71 	return RET_FAIL;
72     }
73     natp = getnatp(player->cnum);
74     ip = whatitem(player->argp[1], "Commodity you want to buy: ");
75     if (!ip)
76 	return RET_SYN;
77     display_mark(ip->i_uid, 0);
78     pr("\n");
79     p = getstarg(player->argp[2], "Which lot are you bidding on: ", buf);
80     if (!p)
81 	return RET_SYN;
82     if (*p == 0)
83 	return RET_SYN;
84     o = atoi(p);
85     if (o < 0)
86 	return RET_SYN;
87     if (!getcomm(o, &comm) || comm.com_owner == 0) {
88 	pr("Invalid lot number.\n");
89 	return RET_OK;
90     }
91     if (comm.com_type != ip->i_uid) {
92 	pr("That lot is not of the type you specified.\n");
93 	return RET_OK;
94     }
95     if (comm.com_owner == player->cnum) {
96 	pr("You can't bid on your own lot.\n");
97 	return RET_OK;
98     }
99     if (!(p = getstarg(player->argp[3], "How much per unit: ", buf)))
100 	return RET_SYN;
101     bid = atof(p);
102     if (bid <= 0)
103 	return RET_FAIL;
104     if (!check_comm_ok(&comm))
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_owner)
118 	    continue;
119 	if (tmpt.trd_maxbidder == player->cnum &&
120 	    tmpt.trd_owner != player->cnum) {
121 	    tally += tmpt.trd_price * tradetax;
122 	}
123     }
124     for (n = 0; getcomm(n, &comt); n++) {
125 	if (comt.com_maxbidder == player->cnum &&
126 	    comt.com_owner != 0 && comt.com_owner != player->cnum) {
127 	    tally += comt.com_price * comt.com_amount * buytax;
128 	}
129     }
130     canspend = natp->nat_money - tally;
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 (!check_comm_ok(&comm))
145 	return RET_FAIL;
146     if ((sect.sct_type != SCT_WAREH && sect.sct_type != SCT_HARBR) ||
147 	sect.sct_own != player->cnum) {
148 	pr("The destination sector is not one of your warehouses.\n");
149 	return RET_FAIL;
150     }
151     if (sect.sct_effic < 60) {
152 	pr("That sector is under construction.\n");
153 	return RET_FAIL;
154     }
155     n = sect.sct_item[ip->i_uid];
156     qty = comm.com_amount;
157     if (qty + n > ITEM_MAX) {
158 	pr("That sector cannot hold %d more %s. It currently holds %d.\n",
159 	   qty, ip->i_name, n);
160 	return RET_FAIL;
161     }
162     if (bid * comm.com_amount > natp->nat_money) {
163 	pr("You don't have that much to spend!\n");
164 	return RET_FAIL;
165     }
166     if (bid > 0.04 + comm.com_price) {
167 	comm.com_price = bid;
168 	time(&now);
169 	if (comm.com_markettime + MARK_DELAY - now < minutes(5) &&
170 	    comm.com_maxbidder != player->cnum)
171 	    comm.com_markettime = now + minutes(5) - MARK_DELAY;
172 	comm.com_maxbidder = player->cnum;
173 	comm.com_x = x;
174 	comm.com_y = y;
175 	putcomm(o, &comm);
176 	pr("Your bid is being considered.\n");
177     } else {
178 	pr("Your bid wasn't high enough (you need to bid at least $0.05 higher\n");
179 	pr("than the last bid.\n");
180 	return RET_OK;
181     }
182 
183     check_market();
184 
185     return RET_OK;
186 }
187 
188 int
check_market(void)189 check_market(void)
190 {
191     struct comstr comm;
192     struct sctstr *sect;
193     struct natstr *natp;
194     int m;
195     int n;
196     time_t now;
197     double gain;
198     double price;
199 
200     for (n = 0; getcomm(n, &comm); n++) {
201 	if (comm.com_maxbidder == comm.com_owner || comm.com_owner == 0)
202 	    continue;
203 	(void)time(&now);
204 	if (comm.com_markettime + MARK_DELAY > now)
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 }
258