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