]> git.pond.sub.org Git - empserver/blob - src/lib/commands/buy.c
Update copyright notice.
[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     pr("WARNING!  This market issues credit.  If you make more\n");
109     pr("  bids than your treasury can cover at the time of sale,\n");
110     pr("  you can potentially go into financial ruin, and see no\n");
111     pr("  gains.  You have been warned.\n\n");
112     if ((p = getstarg(player->argp[3], "How much per unit: ", buf)) == 0)
113         return RET_SYN;
114     bid = atof(p);
115     if (bid <= 0)
116         return RET_FAIL;
117     if (natp->nat_money < (bid * comm.com_amount * buytax)) {
118         pr("This purchase would cost %.2f, %.2f more than you have.\n",
119            bid * comm.com_amount * buytax,
120            bid * comm.com_amount * buytax - natp->nat_money);
121         return RET_FAIL;
122     }
123 /*  check to see if all of the bids that this player has out plus this new bid
124     would make him go broke.  Ken, I ought to skin you alive for making me code
125     this part up.*/
126     tally = 0.0;
127     for (q = 0; gettrade(q, &tmpt); q++) {
128         if (tmpt.trd_maxbidder == player->cnum &&
129             tmpt.trd_unitid >= 0 && tmpt.trd_owner != player->cnum) {
130             tally += tmpt.trd_price * tradetax;
131         }
132     }
133     for (q = 0; getcomm(q, &comt); q++) {
134         if (comt.com_maxbidder == player->cnum &&
135             comt.com_owner != 0 && comt.com_owner != player->cnum) {
136             tally += (comt.com_price * comt.com_amount) * buytax;
137         }
138     }
139     canspend = natp->nat_money - tally;
140     getcomm(o, &comm);
141     if ((bid * comm.com_amount * buytax) > canspend) {
142         pr("You have overextended yourself in the market\n");
143         pr("You can not bid on the current items at that price.\n");
144         return RET_OK;
145     }
146     if (!(p = getstarg(player->argp[4], "destination sector : ", buf)))
147         return RET_SYN;
148     if (!sarg_xy(p, &x, &y))
149         return RET_SYN;
150     if (!getsect(x, y, &sect)) {
151         pr("Could not access sector");
152         return RET_FAIL;
153     }
154     if ((sect.sct_type != SCT_WAREH && sect.sct_type != SCT_HARBR) ||
155         sect.sct_own != player->cnum) {
156         pr("The destination sector is not one of your warehouses.\n");
157         return RET_FAIL;
158     }
159     if (sect.sct_effic < 60) {
160         pr("That sector is under construction.\n");
161         return RET_FAIL;
162     }
163     n = sect.sct_item[ip->i_vtype];
164     qty = comm.com_amount;
165     if (qty + n > ITEM_MAX) {
166         pr("That sector cannot hold %d more %s. It currently holds %d.\n",
167            qty, ip->i_name, n);
168         return RET_FAIL;
169     }
170     if ((bid * comm.com_amount) > natp->nat_money) {
171         pr("You don't have that much to spend!\n");
172         return RET_FAIL;
173     }
174     getcomm(o, &ncomm);
175     if (!ncomm.com_owner) {
176         pr("That lot has been taken off the market.\n");
177         return RET_FAIL;
178     }
179     if (bid > 0.04 + comm.com_price) {
180         comm.com_price = bid;
181         /* Add five minutes to the time if less than 5 minutes */
182         time(&now);
183         if (((MARK_DELAY - (now - comm.com_markettime)) < 300) &&
184             comm.com_maxbidder != player->cnum) {
185             comm.com_markettime += 300;
186             /* Special case - what if so much time has gone by?  Well,
187                Just reset the markettime  so that only 5 minutes are left */
188             if ((MARK_DELAY - (now - comm.com_markettime)) < 0)
189                 comm.com_markettime = (now - (MARK_DELAY - 300));
190         }
191         comm.com_maxbidder = player->cnum;
192         comm.com_x = x;
193         comm.com_y = y;
194         putcomm(o, &comm);
195         pr("Your bid is being considered.\n");
196     } else {
197         pr("Your bid wasn't high enough (you need to bid at least $0.05 higher\n");
198         pr("than the last bid.\n");
199         return RET_OK;
200     }
201
202     check_market();
203
204     return RET_OK;
205 }
206
207 int
208 check_market(void)
209 {
210     struct comstr comm;
211     struct sctstr *sect;
212     struct natstr *natp;
213     int m;
214     int n;
215     time_t now;
216     double tmoney;
217     double tleft;
218     double subleft;
219     double monleft;
220     double gain;
221     double price;
222     struct lonstr loan;
223     long outstanding;           /* Outstanding debt */
224     long couval;                /* Value of country's goods */
225     int foundloan;
226     int j;
227
228 /*    logerror("Checking the market.\n");*/
229     for (n = 0; getcomm(n, &comm); n++) {
230         if (comm.com_maxbidder == comm.com_owner || comm.com_owner == 0)
231             continue;
232         (void)time(&now);
233         tleft = MARK_DELAY / 3600.0 - (now - comm.com_markettime) / 3600.0;
234         if (tleft < 0)
235             tleft = 0;
236         if (tleft > 0.0)
237             continue;
238         if (CANT_HAPPEN(comm.com_type <= I_NONE || comm.com_type > I_MAX))
239             continue;
240         sect = getsectp(comm.com_x, comm.com_y);
241         m = sect->sct_item[comm.com_type];
242
243         monleft = 0;
244
245         price = comm.com_price * comm.com_amount * buytax;
246         gain = comm.com_price * comm.com_amount;
247
248         natp = getnatp(comm.com_maxbidder);
249         tmoney = natp->nat_money;
250         if (tmoney <= 0)
251             monleft = price;
252         if (tmoney < price && tmoney > 0) {
253             monleft = price - (tmoney - 1);
254             tmoney = 1;
255             price = price - monleft;
256         } else if (tmoney > 0) {
257             monleft = 0;
258             tmoney = tmoney - price;
259         }
260
261         /* Subtract the amount of money that needs to come out in a loan. */
262         subleft = monleft;
263
264         if (opt_LOANS) {
265             /* Try to make a loan for the rest from the owner. */
266             if (monleft > 0 && tmoney > 0) {
267                 if ((float)((float)price / (float)(price + monleft)) < 0.1) {
268                     wu(0, comm.com_maxbidder,
269                        "You need at least 10 percent down to purchase something on credit.\n");
270                 } else {
271                     couval = get_couval(comm.com_maxbidder);
272                     outstanding = get_outstand(comm.com_maxbidder);
273                     couval = couval - outstanding;
274                     if (couval > monleft) {
275                         /*  Make the loan */
276                         foundloan = 0;
277                         for (j = 0; getloan(j, &loan); j++) {
278                             if (loan.l_status != LS_FREE)
279                                 continue;
280                             foundloan = 1;
281                             break;
282                         }
283                         if (!foundloan)
284                             ef_extend(EF_LOAN, 1);
285                         loan.l_status = LS_SIGNED;
286                         loan.l_loner = comm.com_owner;
287                         loan.l_lonee = comm.com_maxbidder;
288                         loan.l_irate = 25;
289                         loan.l_ldur = 4;
290                         loan.l_amtpaid = 0;
291                         loan.l_amtdue = monleft;
292                         time(&loan.l_lastpay);
293                         loan.l_duedate =
294                             (loan.l_ldur * SECS_PER_DAY) + loan.l_lastpay;
295                         loan.l_uid = j;
296                         if (!putloan(j, &loan))
297                             logerror("Error writing to the loan file.\n");
298                         else
299                             monleft = 0;
300                         nreport(comm.com_maxbidder, N_FIN_TROUBLE,
301                                 comm.com_owner, 1);
302                         wu(0, comm.com_maxbidder,
303                            "You just took loan #%d for $%ld to cover the cost of your purchase.\n",
304                            j, loan.l_amtdue);
305                         wu(0, comm.com_owner,
306                            "You just extended loan #%d to %s to help with the purchase cose.\n",
307                            j, cname(comm.com_maxbidder));
308                     } else {
309                         nreport(comm.com_maxbidder, N_CREDIT_JUNK,
310                                 comm.com_owner, 1);
311                         wu(0, comm.com_maxbidder,
312                            "You don't have enough credit to get a loan.\n");
313                         wu(0, comm.com_owner,
314                            "You just turned down a loan to %s.\n",
315                            cname(comm.com_maxbidder));
316                     }
317                 }
318             }
319         }
320
321         if (monleft > 0) {
322             nreport(comm.com_maxbidder, N_WELCH_DEAL, comm.com_owner, 1);
323             wu(0, comm.com_maxbidder,
324                "You didn't have enough cash/credit to cover the cost.\n");
325             wu(0, comm.com_owner,
326                "Sale #%d fell through.  Goods remain on the market.\n", n);
327             comm.com_maxbidder = comm.com_owner;
328         } else if (sect->sct_type != SCT_WAREH
329                    && sect->sct_type != SCT_HARBR) {
330             wu(0, comm.com_maxbidder,
331                "Sector not a warehouse now, sale #%d fell though.\n", n);
332             wu(0, comm.com_owner,
333                "Sale #%d fell through.  Goods remain on the market.\n", n);
334             comm.com_maxbidder = comm.com_owner;
335         } else if (m + comm.com_amount > ITEM_MAX) {
336             wu(0, comm.com_maxbidder,
337                "Warehouse full,  sale #%d fell though.\n", n);
338             wu(0, comm.com_owner,
339                "Sale #%d fell through.  Goods remain on the market.\n", n);
340             comm.com_maxbidder = comm.com_owner;
341         } else {
342             sect->sct_item[comm.com_type] = m + comm.com_amount;
343             putsect(sect);
344             nreport(comm.com_owner, N_MAKE_SALE, comm.com_maxbidder, 1);
345             wu(0, comm.com_owner, "%s bought %d %s from you for $%.2f\n",
346                cname(comm.com_maxbidder), comm.com_amount,
347                ichr[comm.com_type].i_name, gain);
348             wu(0, comm.com_maxbidder,
349                "You just bought %d %s from %s for $%.2f\n",
350                comm.com_amount, ichr[comm.com_type].i_name,
351                cname(comm.com_owner), gain * buytax);
352             natp = getnatp(comm.com_owner);
353             /* Make sure we subtract the amount that came out in a loan */
354             natp->nat_money += (gain - subleft);
355             natp = getnatp(comm.com_maxbidder);
356             natp->nat_money = tmoney;
357             comm.com_owner = 0;
358         }
359         comm.com_owner = 0;
360         putcomm(n, &comm);
361     }
362 /*    logerror("Done checking the market.\n");*/
363     return RET_OK;
364 }