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