]> 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-2012, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  buy.c: Buy commodities from other nations
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1986
31  *     Pat Loney, 1992
32  *     Steve McClure, 1996-2000
33  */
34
35 #include <config.h>
36
37 #include "commands.h"
38 #include "commodity.h"
39 #include "item.h"
40 #include "news.h"
41 #include "optlist.h"
42 #include "trade.h"
43
44 /*
45  * format: buy <COMMODITY>
46  *
47  */
48 int
49 buy(void)
50 {
51     struct sctstr sect;
52     struct natstr *natp;
53     struct comstr comm;
54     struct comstr comt;
55     struct trdstr tmpt;
56     struct ichrstr *ip;
57     int qty;
58     int o, n;
59     coord x, y;
60     char *p;
61     float bid;
62     time_t now;
63     double tally;
64     double canspend;
65     char buf[1024];
66
67     if (!opt_MARKET) {
68         pr("The market is disabled.\n");
69         return RET_FAIL;
70     }
71     natp = getnatp(player->cnum);
72     ip = whatitem(player->argp[1], "Commodity you want to buy: ");
73     if (!ip)
74         return RET_SYN;
75     display_mark(ip->i_uid, 0);
76     pr("\n");
77     p = getstarg(player->argp[2], "Which lot are you bidding on: ", buf);
78     if (!p)
79         return RET_SYN;
80     if (*p == 0)
81         return RET_SYN;
82     o = atoi(p);
83     if (o < 0)
84         return RET_SYN;
85     if (!getcomm(o, &comm) || comm.com_owner == 0) {
86         pr("Invalid lot number.\n");
87         return RET_OK;
88     }
89     if (comm.com_type != ip->i_uid) {
90         pr("That lot is not of the type you specified.\n");
91         return RET_OK;
92     }
93     if (comm.com_owner == player->cnum) {
94         pr("You can't bid on your own lot.\n");
95         return RET_OK;
96     }
97     if (!(p = getstarg(player->argp[3], "How much per unit: ", buf)))
98         return RET_SYN;
99     bid = atof(p);
100     if (bid <= 0)
101         return RET_FAIL;
102     if (natp->nat_money < bid * comm.com_amount * buytax) {
103         pr("This purchase would cost %.2f, %.2f more than you have.\n",
104            bid * comm.com_amount * buytax,
105            bid * comm.com_amount * buytax - natp->nat_money);
106         return RET_FAIL;
107     }
108 /*  check to see if all of the bids that this player has out plus this new bid
109     would make him go broke.  Ken, I ought to skin you alive for making me code
110     this part up.*/
111     tally = 0.0;
112     for (n = 0; gettrade(n, &tmpt); n++) {
113         if (tmpt.trd_maxbidder == player->cnum &&
114             tmpt.trd_unitid >= 0 && tmpt.trd_owner != player->cnum) {
115             tally += tmpt.trd_price * tradetax;
116         }
117     }
118     for (n = 0; getcomm(n, &comt); n++) {
119         if (comt.com_maxbidder == player->cnum &&
120             comt.com_owner != 0 && comt.com_owner != player->cnum) {
121             tally += comt.com_price * comt.com_amount * buytax;
122         }
123     }
124     canspend = natp->nat_money - tally;
125     check_comm_ok(&comm);
126     if (bid * comm.com_amount * buytax > canspend) {
127         pr("You have overextended yourself in the market\n");
128         pr("You can not bid on the current items at that price.\n");
129         return RET_OK;
130     }
131     if (!(p = getstarg(player->argp[4], "destination sector : ", buf)))
132         return RET_SYN;
133     if (!sarg_xy(p, &x, &y))
134         return RET_SYN;
135     if (!getsect(x, y, &sect)) {
136         pr("Could not access sector");
137         return RET_FAIL;
138     }
139     if ((sect.sct_type != SCT_WAREH && sect.sct_type != SCT_HARBR) ||
140         sect.sct_own != player->cnum) {
141         pr("The destination sector is not one of your warehouses.\n");
142         return RET_FAIL;
143     }
144     if (sect.sct_effic < 60) {
145         pr("That sector is under construction.\n");
146         return RET_FAIL;
147     }
148     n = sect.sct_item[ip->i_uid];
149     qty = comm.com_amount;
150     if (qty + n > ITEM_MAX) {
151         pr("That sector cannot hold %d more %s. It currently holds %d.\n",
152            qty, ip->i_name, n);
153         return RET_FAIL;
154     }
155     if (bid * comm.com_amount > natp->nat_money) {
156         pr("You don't have that much to spend!\n");
157         return RET_FAIL;
158     }
159     if (!check_comm_ok(&comm))
160         return RET_FAIL;
161     if (bid > 0.04 + comm.com_price) {
162         comm.com_price = bid;
163         /* Add five minutes to the time if less than 5 minutes */
164         time(&now);
165         if (((MARK_DELAY - (now - comm.com_markettime)) < 300) &&
166             comm.com_maxbidder != player->cnum) {
167             comm.com_markettime += 300;
168             /* Special case - what if so much time has gone by?  Well,
169                Just reset the markettime  so that only 5 minutes are left */
170             if ((MARK_DELAY - (now - comm.com_markettime)) < 0)
171                 comm.com_markettime = (now - (MARK_DELAY - 300));
172         }
173         comm.com_maxbidder = player->cnum;
174         comm.com_x = x;
175         comm.com_y = y;
176         putcomm(o, &comm);
177         pr("Your bid is being considered.\n");
178     } else {
179         pr("Your bid wasn't high enough (you need to bid at least $0.05 higher\n");
180         pr("than the last bid.\n");
181         return RET_OK;
182     }
183
184     check_market();
185
186     return RET_OK;
187 }
188
189 int
190 check_market(void)
191 {
192     struct comstr comm;
193     struct sctstr *sect;
194     struct natstr *natp;
195     int m;
196     int n;
197     time_t now;
198     double tleft;
199     double gain;
200     double price;
201
202     for (n = 0; getcomm(n, &comm); n++) {
203         if (comm.com_maxbidder == comm.com_owner || comm.com_owner == 0)
204             continue;
205         (void)time(&now);
206         tleft = MARK_DELAY / 3600.0 - (now - comm.com_markettime) / 3600.0;
207         if (tleft < 0)
208             tleft = 0;
209         if (tleft > 0.0)
210             continue;
211         if (CANT_HAPPEN(comm.com_type <= I_NONE || comm.com_type > I_MAX))
212             continue;
213         sect = getsectp(comm.com_x, comm.com_y);
214         m = sect->sct_item[comm.com_type];
215
216         price = comm.com_price * comm.com_amount * buytax;
217         gain = comm.com_price * comm.com_amount;
218
219         natp = getnatp(comm.com_maxbidder);
220         if (natp->nat_money < price) {
221             nreport(comm.com_maxbidder, N_WELCH_DEAL, comm.com_owner, 1);
222             wu(0, comm.com_maxbidder,
223                "You didn't have enough cash to cover the cost.\n");
224             wu(0, comm.com_owner,
225                "Sale #%d fell through.  Goods remain on the market.\n", n);
226             comm.com_maxbidder = comm.com_owner;
227         } else if (sect->sct_type != SCT_WAREH
228                    && sect->sct_type != SCT_HARBR) {
229             wu(0, comm.com_maxbidder,
230                "Sector not a warehouse now, sale #%d fell though.\n", n);
231             wu(0, comm.com_owner,
232                "Sale #%d fell through.  Goods remain on the market.\n", n);
233             comm.com_maxbidder = comm.com_owner;
234         } else if (m + comm.com_amount > ITEM_MAX) {
235             wu(0, comm.com_maxbidder,
236                "Warehouse full,  sale #%d fell though.\n", n);
237             wu(0, comm.com_owner,
238                "Sale #%d fell through.  Goods remain on the market.\n", n);
239             comm.com_maxbidder = comm.com_owner;
240         } else {
241             sect->sct_item[comm.com_type] = m + comm.com_amount;
242             putsect(sect);
243             nreport(comm.com_owner, N_MAKE_SALE, comm.com_maxbidder, 1);
244             wu(0, comm.com_owner, "%s bought %d %s from you for $%.2f\n",
245                cname(comm.com_maxbidder), comm.com_amount,
246                ichr[comm.com_type].i_name, gain);
247             wu(0, comm.com_maxbidder,
248                "You just bought %d %s from %s for $%.2f\n",
249                comm.com_amount, ichr[comm.com_type].i_name,
250                cname(comm.com_owner), price);
251             natp->nat_money -= roundavg(price);
252             putnat(natp);
253             natp = getnatp(comm.com_owner);
254             natp->nat_money += roundavg(gain);
255             putnat(natp);
256             comm.com_owner = 0;
257         }
258         comm.com_owner = 0;
259         putcomm(n, &comm);
260     }
261     return RET_OK;
262 }