]> git.pond.sub.org Git - empserver/blob - src/lib/commands/buy.c
(genitem, ship, plane, land): Remove unused member sell & equivalents.
[empserver] / src / lib / commands / buy.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, 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     display_mark(player->argp[1]);
86     pr("\n");
87     p = getstarg(player->argp[2], "Which lot are you bidding on: ", buf);
88     if (p == 0)
89         return RET_SYN;
90     if (*p == 0)
91         return RET_SYN;
92     o = atoi(p);
93     if (o < 0)
94         return RET_SYN;
95     if (!getcomm(o, &comm) || comm.com_owner == 0) {
96         pr("Invalid lot number.\n");
97         return RET_OK;
98     }
99     if (player->argp[1] && *(player->argp[1]) &&
100         comm.com_type != player->argp[1][0]) {
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     ip = whichitem(comm.com_type);
164     n = getvar(ip->i_vtype, (char *)&sect, EF_SECTOR);
165     qty = comm.com_amount;
166     if (qty + n > 9990) {
167         pr("That sector cannot hold %d more %s. It currently holds %d.\n",
168            qty, ip->i_name, n);
169         return RET_FAIL;
170     }
171     /* First we check for room, then we yank back.  Probably not necessary. */
172     if (putvar(ip->i_vtype, n + qty, (char *)&sect, EF_SECTOR) <= 0) {
173         pr("No room to store %s in %s\n",
174            ip->i_name, xyas(sect.sct_x, sect.sct_y, player->cnum));
175         return RET_FAIL;
176     }
177     if (putvar(ip->i_vtype, n, (char *)&sect, EF_SECTOR) <= 0) {
178         pr("Something weird just happened, tell the deity.\n");
179         logerror("buy.c: putvar failed.\n");
180         return RET_FAIL;
181     }
182     if ((bid * comm.com_amount) > natp->nat_money) {
183         pr("You don't have that much to spend!\n");
184         return RET_FAIL;
185     }
186     getcomm(o, &ncomm);
187     if (!ncomm.com_owner) {
188         pr("That lot has been taken off the market.\n");
189         return RET_FAIL;
190     }
191     if (bid > 0.04 + comm.com_price) {
192         comm.com_price = bid;
193         /* Add five minutes to the time if less than 5 minutes */
194         time(&now);
195         if (((MARK_DELAY - (now - comm.com_markettime)) < 300) &&
196             comm.com_maxbidder != player->cnum) {
197             comm.com_markettime += 300;
198             /* Special case - what if so much time has gone by?  Well,
199                Just reset the markettime  so that only 5 minutes are left */
200             if ((MARK_DELAY - (now - comm.com_markettime)) < 0)
201                 comm.com_markettime = (now - (MARK_DELAY - 300));
202         }
203         comm.com_maxbidder = player->cnum;
204         comm.com_x = x;
205         comm.com_y = y;
206         putcomm(o, &comm);
207         pr("Your bid is being considered.\n");
208     } else {
209         pr("Your bid wasn't high enough (you need to bid at least $0.05 higher\n");
210         pr("than the last bid.\n");
211         return RET_OK;
212     }
213
214     check_market();
215
216     return RET_OK;
217 }
218
219 int
220 check_market(void)
221 {
222     struct comstr comm;
223     struct sctstr *sect;
224     struct ichrstr *ip;
225     struct natstr *natp;
226     int m;
227     int n;
228     time_t now;
229     double tmoney;
230     double tleft;
231     double subleft;
232     double monleft;
233     double gain;
234     double price;
235     struct lonstr loan;
236     long outstanding;           /* Outstanding debt */
237     long couval;                /* Value of country's goods */
238     int foundloan;
239     int j;
240
241 /*    logerror("Checking the market.\n");*/
242     for (n = 0; getcomm(n, &comm); n++) {
243         if (comm.com_maxbidder == comm.com_owner || comm.com_owner == 0)
244             continue;
245         (void)time(&now);
246         tleft = MARK_DELAY / 3600.0 - (now - comm.com_markettime) / 3600.0;
247         if (tleft < 0)
248             tleft = 0;
249         if (tleft > 0.0)
250             continue;
251         ip = whichitem(comm.com_type);
252         sect = getsectp(comm.com_x, comm.com_y);
253         m = getvar(ip->i_vtype, (char *)sect, EF_SECTOR);
254
255         monleft = 0;
256
257         price = comm.com_price * comm.com_amount * buytax;
258         gain = comm.com_price * comm.com_amount;
259
260         natp = getnatp(comm.com_maxbidder);
261         tmoney = natp->nat_money;
262         if (tmoney <= 0)
263             monleft = price;
264         if (tmoney < price && tmoney > 0) {
265             monleft = price - (tmoney - 1);
266             tmoney = 1;
267             price = price - monleft;
268         } else if (tmoney > 0) {
269             monleft = 0;
270             tmoney = tmoney - price;
271         }
272
273         /* Subtract the amount of money that needs to come out in a loan. */
274         subleft = monleft;
275
276         if (opt_LOANS) {
277             /* Try to make a loan for the rest from the owner. */
278             if (monleft > 0 && tmoney > 0) {
279                 if ((float)((float)price / (float)(price + monleft)) < 0.1) {
280                     wu(0, comm.com_maxbidder,
281                        "You need at least 10 percent down to purchase something on credit.\n");
282                 } else {
283                     couval = get_couval(comm.com_maxbidder);
284                     outstanding = get_outstand(comm.com_maxbidder);
285                     couval = couval - outstanding;
286                     if (couval > monleft) {
287                         /*  Make the loan */
288                         foundloan = 0;
289                         for (j = 0; getloan(j, &loan); j++) {
290                             if (loan.l_status != LS_FREE)
291                                 continue;
292                             foundloan = 1;
293                             break;
294                         }
295                         if (!foundloan)
296                             ef_extend(EF_LOAN, 1);
297                         loan.l_status = LS_SIGNED;
298                         loan.l_loner = comm.com_owner;
299                         loan.l_lonee = comm.com_maxbidder;
300                         loan.l_irate = 25;
301                         loan.l_ldur = 4;
302                         loan.l_amtpaid = 0;
303                         loan.l_amtdue = monleft;
304                         time(&loan.l_lastpay);
305                         loan.l_duedate =
306                             (loan.l_ldur * SECS_PER_DAY) + loan.l_lastpay;
307                         loan.l_uid = j;
308                         if (!putloan(j, &loan))
309                             logerror("Error writing to the loan file.\n");
310                         else
311                             monleft = 0;
312                         nreport(comm.com_maxbidder, N_FIN_TROUBLE,
313                                 comm.com_owner, 1);
314                         wu(0, comm.com_maxbidder,
315                            "You just took loan #%d for $%.2f to cover the cost of your purchase.\n",
316                            j, loan.l_amtdue);
317                         wu(0, comm.com_owner,
318                            "You just extended loan #%d to %s to help with the purchase cose.\n",
319                            j, cname(comm.com_maxbidder));
320                     } else {
321                         nreport(comm.com_maxbidder, N_CREDIT_JUNK,
322                                 comm.com_owner, 1);
323                         wu(0, comm.com_maxbidder,
324                            "You don't have enough credit to get a loan.\n");
325                         wu(0, comm.com_owner,
326                            "You just turned down a loan to %s.\n",
327                            cname(comm.com_maxbidder));
328                     }
329                 }
330             }
331         }
332
333         if (monleft > 0) {
334             nreport(comm.com_maxbidder, N_WELCH_DEAL, comm.com_owner, 1);
335             wu(0, comm.com_maxbidder,
336                "You didn't have enough cash/credit to cover the cost.\n");
337             wu(0, comm.com_owner,
338                "Sale #%d fell through.  Goods remain on the market.\n", n);
339             comm.com_maxbidder = comm.com_owner;
340         } else if (sect->sct_type != SCT_WAREH
341                    && sect->sct_type != SCT_HARBR) {
342             wu(0, comm.com_maxbidder,
343                "Sector not a warehouse now, sale #%d fell though.\n", n);
344             wu(0, comm.com_owner,
345                "Sale #%d fell through.  Goods remain on the market.\n", n);
346             comm.com_maxbidder = comm.com_owner;
347         } else if (putvar(ip->i_vtype, m + comm.com_amount,
348                           (char *)sect, EF_SECTOR) <= 0) {
349             wu(0, comm.com_maxbidder,
350                "Warehouse full,  sale #%d fell though.\n", n);
351             wu(0, comm.com_owner,
352                "Sale #%d fell through.  Goods remain on the market.\n", n);
353             comm.com_maxbidder = comm.com_owner;
354         } else {
355             putsect(sect);
356             nreport(comm.com_owner, N_MAKE_SALE, comm.com_maxbidder, 1);
357             wu(0, comm.com_owner, "%s bought %d %c's from you for $%.2f\n",
358                cname(comm.com_maxbidder), comm.com_amount,
359                comm.com_type, gain);
360             wu(0, comm.com_maxbidder,
361                "You just bought %d %c's from %s for $%.2f\n",
362                comm.com_amount, comm.com_type, cname(comm.com_owner),
363                gain * buytax);
364             natp = getnatp(comm.com_owner);
365             /* Make sure we subtract the amount that came out in a loan */
366             natp->nat_money += (gain - subleft);
367             natp = getnatp(comm.com_maxbidder);
368             natp->nat_money = tmoney;
369             comm.com_owner = 0;
370         }
371         comm.com_owner = 0;
372         putcomm(n, &comm);
373     }
374 /*    logerror("Done checking the market.\n");*/
375     return RET_OK;
376 }