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