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