]> git.pond.sub.org Git - empserver/blob - src/lib/commands/buy.c
s_char purge directed by compiler warnings.
[empserver] / src / lib / commands / buy.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, 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 files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future 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 <config.h>
37
38 #include "misc.h"
39 #include "xy.h"
40 #include "file.h"
41 #include "sect.h"
42 #include "nat.h"
43 #include "news.h"
44 #include "nsc.h"
45 #include "item.h"
46 #include "land.h"
47 #include "commodity.h"
48 #include "plane.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     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_uid, 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_uid) {
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     if ((p = getstarg(player->argp[3], "How much per unit: ", buf)) == 0)
111         return RET_SYN;
112     bid = atof(p);
113     if (bid <= 0)
114         return RET_FAIL;
115     if (natp->nat_money < (bid * comm.com_amount * buytax)) {
116         pr("This purchase would cost %.2f, %.2f more than you have.\n",
117            bid * comm.com_amount * buytax,
118            bid * comm.com_amount * buytax - natp->nat_money);
119         return RET_FAIL;
120     }
121 /*  check to see if all of the bids that this player has out plus this new bid
122     would make him go broke.  Ken, I ought to skin you alive for making me code
123     this part up.*/
124     tally = 0.0;
125     for (q = 0; gettrade(q, &tmpt); q++) {
126         if (tmpt.trd_maxbidder == player->cnum &&
127             tmpt.trd_unitid >= 0 && tmpt.trd_owner != player->cnum) {
128             tally += tmpt.trd_price * tradetax;
129         }
130     }
131     for (q = 0; getcomm(q, &comt); q++) {
132         if (comt.com_maxbidder == player->cnum &&
133             comt.com_owner != 0 && comt.com_owner != player->cnum) {
134             tally += (comt.com_price * comt.com_amount) * buytax;
135         }
136     }
137     canspend = natp->nat_money - tally;
138     getcomm(o, &comm);
139     if ((bid * comm.com_amount * buytax) > canspend) {
140         pr("You have overextended yourself in the market\n");
141         pr("You can not bid on the current items at that price.\n");
142         return RET_OK;
143     }
144     if (!(p = getstarg(player->argp[4], "destination sector : ", buf)))
145         return RET_SYN;
146     if (!sarg_xy(p, &x, &y))
147         return RET_SYN;
148     if (!getsect(x, y, &sect)) {
149         pr("Could not access sector");
150         return RET_FAIL;
151     }
152     if ((sect.sct_type != SCT_WAREH && sect.sct_type != SCT_HARBR) ||
153         sect.sct_own != player->cnum) {
154         pr("The destination sector is not one of your warehouses.\n");
155         return RET_FAIL;
156     }
157     if (sect.sct_effic < 60) {
158         pr("That sector is under construction.\n");
159         return RET_FAIL;
160     }
161     n = sect.sct_item[ip->i_uid];
162     qty = comm.com_amount;
163     if (qty + n > ITEM_MAX) {
164         pr("That sector cannot hold %d more %s. It currently holds %d.\n",
165            qty, ip->i_name, n);
166         return RET_FAIL;
167     }
168     if ((bid * comm.com_amount) > natp->nat_money) {
169         pr("You don't have that much to spend!\n");
170         return RET_FAIL;
171     }
172     getcomm(o, &ncomm);
173     if (!ncomm.com_owner) {
174         pr("That lot has been taken off the market.\n");
175         return RET_FAIL;
176     }
177     if (bid > 0.04 + comm.com_price) {
178         comm.com_price = bid;
179         /* Add five minutes to the time if less than 5 minutes */
180         time(&now);
181         if (((MARK_DELAY - (now - comm.com_markettime)) < 300) &&
182             comm.com_maxbidder != player->cnum) {
183             comm.com_markettime += 300;
184             /* Special case - what if so much time has gone by?  Well,
185                Just reset the markettime  so that only 5 minutes are left */
186             if ((MARK_DELAY - (now - comm.com_markettime)) < 0)
187                 comm.com_markettime = (now - (MARK_DELAY - 300));
188         }
189         comm.com_maxbidder = player->cnum;
190         comm.com_x = x;
191         comm.com_y = y;
192         putcomm(o, &comm);
193         pr("Your bid is being considered.\n");
194     } else {
195         pr("Your bid wasn't high enough (you need to bid at least $0.05 higher\n");
196         pr("than the last bid.\n");
197         return RET_OK;
198     }
199
200     check_market();
201
202     return RET_OK;
203 }
204
205 int
206 check_market(void)
207 {
208     struct comstr comm;
209     struct sctstr *sect;
210     struct natstr *natp;
211     int m;
212     int n;
213     time_t now;
214     double tmoney;
215     double tleft;
216     double subleft;
217     double monleft;
218     double gain;
219     double price;
220
221 /*    logerror("Checking the market.\n");*/
222     for (n = 0; getcomm(n, &comm); n++) {
223         if (comm.com_maxbidder == comm.com_owner || comm.com_owner == 0)
224             continue;
225         (void)time(&now);
226         tleft = MARK_DELAY / 3600.0 - (now - comm.com_markettime) / 3600.0;
227         if (tleft < 0)
228             tleft = 0;
229         if (tleft > 0.0)
230             continue;
231         if (CANT_HAPPEN(comm.com_type <= I_NONE || comm.com_type > I_MAX))
232             continue;
233         sect = getsectp(comm.com_x, comm.com_y);
234         m = sect->sct_item[comm.com_type];
235
236         monleft = 0;
237
238         price = comm.com_price * comm.com_amount * buytax;
239         gain = comm.com_price * comm.com_amount;
240
241         natp = getnatp(comm.com_maxbidder);
242         tmoney = natp->nat_money;
243         if (tmoney <= 0)
244             monleft = price;
245         if (tmoney < price && tmoney > 0) {
246             monleft = price - (tmoney - 1);
247             tmoney = 1;
248             price = price - monleft;
249         } else if (tmoney > 0) {
250             monleft = 0;
251             tmoney = tmoney - price;
252         }
253
254         /* Subtract the amount of money that needs to come out in a loan. */
255         subleft = monleft;
256
257         if (monleft > 0) {
258             nreport(comm.com_maxbidder, N_WELCH_DEAL, comm.com_owner, 1);
259             wu(0, comm.com_maxbidder,
260                "You didn't have enough cash/credit to cover the cost.\n");
261             wu(0, comm.com_owner,
262                "Sale #%d fell through.  Goods remain on the market.\n", n);
263             comm.com_maxbidder = comm.com_owner;
264         } else if (sect->sct_type != SCT_WAREH
265                    && sect->sct_type != SCT_HARBR) {
266             wu(0, comm.com_maxbidder,
267                "Sector not a warehouse now, sale #%d fell though.\n", n);
268             wu(0, comm.com_owner,
269                "Sale #%d fell through.  Goods remain on the market.\n", n);
270             comm.com_maxbidder = comm.com_owner;
271         } else if (m + comm.com_amount > ITEM_MAX) {
272             wu(0, comm.com_maxbidder,
273                "Warehouse full,  sale #%d fell though.\n", n);
274             wu(0, comm.com_owner,
275                "Sale #%d fell through.  Goods remain on the market.\n", n);
276             comm.com_maxbidder = comm.com_owner;
277         } else {
278             sect->sct_item[comm.com_type] = m + comm.com_amount;
279             putsect(sect);
280             nreport(comm.com_owner, N_MAKE_SALE, comm.com_maxbidder, 1);
281             wu(0, comm.com_owner, "%s bought %d %s from you for $%.2f\n",
282                cname(comm.com_maxbidder), comm.com_amount,
283                ichr[comm.com_type].i_name, gain);
284             wu(0, comm.com_maxbidder,
285                "You just bought %d %s from %s for $%.2f\n",
286                comm.com_amount, ichr[comm.com_type].i_name,
287                cname(comm.com_owner), gain * buytax);
288             natp = getnatp(comm.com_owner);
289             /* Make sure we subtract the amount that came out in a loan */
290             natp->nat_money += (gain - subleft);
291             natp = getnatp(comm.com_maxbidder);
292             natp->nat_money = tmoney;
293             comm.com_owner = 0;
294         }
295         comm.com_owner = 0;
296         putcomm(n, &comm);
297     }
298 /*    logerror("Done checking the market.\n");*/
299     return RET_OK;
300 }