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