1 /*
2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2021, 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 * trad.c: Buy units/ships/planes/nukes from other nations.
28 *
29 * Known contributors to this file:
30 * Dave Pare, 1986
31 * Pat Loney, 1992
32 * Steve McClure, 1996-2000
33 * Markus Armbruster, 2004-2018
34 */
35
36 #include <config.h>
37
38 #include <ctype.h>
39 #include "chance.h"
40 #include "commands.h"
41 #include "commodity.h"
42 #include "empobj.h"
43 #include "land.h"
44 #include "loan.h"
45 #include "news.h"
46 #include "nuke.h"
47 #include "optlist.h"
48 #include "plane.h"
49 #include "ship.h"
50 #include "trade.h"
51 #include "unit.h"
52
53 /*
54 * format: trade
55 */
56 int
c_trade(void)57 c_trade(void)
58 {
59 struct sctstr sect;
60 struct natstr *natp;
61 struct comstr comt;
62 int lotno;
63 float price;
64 coord sx, sy;
65 int n;
66 char *p;
67 struct nstr_item ni;
68 struct trdstr trade;
69 struct trdstr tmpt;
70 union empobj_storage tg;
71 double canspend;
72 time_t now, tleft;
73 int bid;
74 double tally;
75 int i;
76 char buf[1024];
77
78 if (!opt_MARKET) {
79 pr("The market is disabled.\n");
80 return RET_FAIL;
81 }
82 /* First, we execute all trades, so that we can only buy what is available. */
83 check_market();
84 check_trade();
85
86 pr("\n Empire Trade Report\n ");
87 prdate();
88 n = 0;
89 pr(" lot high bid by time left owner description\n");
90 pr(" --- -------- -- --------- ----- -------------------------\n");
91
92 snxtitem_all(&ni, EF_TRADE);
93 while (nxtitem(&ni, &trade)) {
94 if (trade.trd_owner == 0)
95 continue;
96 if (!trade_getitem(&trade, &tg)) {
97 continue;
98 }
99 pr(" %3d ", ni.cur);
100 (void)time(&now);
101 tleft = trade.trd_markettime + TRADE_DELAY - now;
102 if (tleft < 0)
103 tleft = 0;
104 pr("$%7d %2d %5.2f hrs ",
105 trade.trd_price, trade.trd_maxbidder, tleft / 3600.0);
106 trade_desc(&tg.gen); /* XXX */
107 pr("\n");
108 if (trade.trd_owner == player->cnum && !player->god)
109 pr(" (your own lot)\n");
110 n++;
111 }
112 if (n == 0) {
113 pr("Nothing to buy at the moment...\n");
114 return RET_OK;
115 }
116 p = getstring("Which lot to buy: ", buf);
117 if (!p || !*p)
118 return RET_OK;
119 if (isdigit(*p) == 0)
120 return RET_OK;
121 lotno = atoi(p);
122 if (lotno < 0 || lotno >= ni.cur) {
123 pr("Bad lot number\n");
124 return RET_OK;
125 }
126 if (!gettrade(lotno, &trade) || !trade.trd_owner) {
127 pr("No such lot number\n");
128 return RET_OK;
129 }
130 if (!trade_getitem(&trade, &tg)) {
131 pr("Can't find trade #%d!\n", trade.trd_unitid);
132 trade.trd_owner = 0;
133 if (!puttrade(lotno, &trade)) {
134 logerror("trad: can't write trade");
135 pr("Couldn't save after getitem failed; get help!\n");
136 return RET_FAIL;
137 }
138 return RET_OK;
139 }
140 switch (trade.trd_type) {
141 case EF_NUKE:
142 case EF_PLANE:
143 case EF_SHIP:
144 case EF_LAND:
145 break;
146 default:
147 pr("Bad unit type on lot number %d\n", lotno);
148 return RET_FAIL;
149 }
150 if (trade.trd_owner == player->cnum) {
151 pr("You can't buy from yourself!\n");
152 return RET_OK;
153 }
154 price = trade.trd_price;
155 natp = getnatp(player->cnum);
156 if (natp->nat_money < price) {
157 pr("You don't have %.2f to spend!\n", price);
158 return RET_OK;
159 }
160 tally = 0.0;
161 for (i = 0; gettrade(i, &tmpt); i++) {
162 if (!tmpt.trd_owner)
163 continue;
164 if (tmpt.trd_maxbidder == player->cnum &&
165 tmpt.trd_owner != player->cnum) {
166 tally += tmpt.trd_price * tradetax;
167 }
168 }
169 for (i = 0; getcomm(i, &comt); i++) {
170 if (comt.com_maxbidder == player->cnum &&
171 comt.com_owner != 0 && comt.com_owner != player->cnum) {
172 tally += (comt.com_price * comt.com_amount) * buytax;
173 }
174 }
175 canspend = natp->nat_money - tally;
176
177 /* Find the destination sector for the trade */
178 if (((trade.trd_type == EF_PLANE) && !pln_is_in_orbit(&tg.plane))
179 || (trade.trd_type == EF_NUKE)) {
180 while (1) {
181 p = getstring("Destination sector: ", buf);
182 if (!p)
183 return RET_FAIL;
184 if (!trade_check_ok(&trade, &tg.gen))
185 return RET_FAIL;
186 if (!sarg_xy(p, &sx, &sy) || !getsect(sx, sy, §)) {
187 pr("Bad sector designation; try again!\n");
188 continue;
189 }
190 if (!player->owner) {
191 pr("You don't own that sector; try again!\n");
192 continue;
193 }
194 if (!(plchr[tg.plane.pln_type].pl_flags & P_V)) {
195 if (!player->god && (sect.sct_type != SCT_AIRPT)) {
196 pr("Destination sector is not an airfield!\n");
197 continue;
198 }
199 if (!player->god && (sect.sct_effic < 60)) {
200 pr("That airport still under construction!\n");
201 continue;
202 }
203 }
204 break;
205 }
206 } else if (trade.trd_type == EF_LAND) {
207 while (1) {
208 p = getstring("Destination sector: ", buf);
209 if (!p)
210 return RET_FAIL;
211 if (!trade_check_ok(&trade, &tg.gen))
212 return RET_FAIL;
213 if (!sarg_xy(p, &sx, &sy) || !getsect(sx, sy, §)) {
214 pr("Bad sector designation; try again!\n");
215 continue;
216 }
217 if (!player->owner) {
218 pr("You don't own that sector; try again!\n");
219 continue;
220 }
221 if (!player->god && (sect.sct_type != SCT_HEADQ)) {
222 pr("Destination sector is not a headquarters!\n");
223 continue;
224 }
225 if (!player->god && (sect.sct_effic < 60)) {
226 pr("That headquarters still under construction!\n");
227 continue;
228 }
229 break;
230 }
231 } else {
232 /* This trade doesn't teleport; make destination invalid */
233 sx = 1;
234 sy = 0;
235 }
236
237 p = getstring("How much do you bid: ", buf);
238 if (!p || !*p)
239 return RET_OK;
240 if (!trade_check_ok(&trade, &tg.gen))
241 return RET_FAIL;
242 bid = atoi(p);
243 if (bid < price)
244 bid = price;
245 if (bid > canspend) {
246 pr("You don't have %.2f to spend!\n", price);
247 return RET_OK;
248 }
249 if (bid > trade.trd_price) {
250 time(&now);
251 if (trade.trd_markettime + TRADE_DELAY - now < minutes(5) &&
252 trade.trd_maxbidder != player->cnum)
253 trade.trd_markettime = now + minutes(5) - TRADE_DELAY;
254 trade.trd_price = bid;
255 trade.trd_maxbidder = player->cnum;
256 trade.trd_x = sx;
257 trade.trd_y = sy;
258 pr("Your bid on lot #%d is being considered.\n", lotno);
259 if (!puttrade(lotno, &trade))
260 pr("Problems with the trade file. Get help\n");
261 } else
262 pr("Your bid wasn't high enough (you need to bid more than someone else.)\n");
263
264 check_trade();
265
266 return RET_OK;
267 }
268
269 int
check_trade(void)270 check_trade(void)
271 {
272 int n;
273 struct natstr *natp;
274 struct trdstr trade;
275 union empobj_storage tg;
276 time_t now;
277 int price;
278 int saveid;
279 natid seller;
280
281 for (n = 0; gettrade(n, &trade); n++) {
282 if (!trade.trd_owner)
283 continue;
284 if (!trade_getitem(&trade, &tg))
285 continue;
286 /*
287 * FIXME We fail to delete trades right away when the thing on
288 * sale dies. Instead, we delete it here. Doesn't work if it
289 * has been rebuilt by the same owner; in that case, the new
290 * one takes the dead one's place on the market.
291 */
292 if (tg.gen.own != trade.trd_owner) {
293 trade.trd_owner = 0;
294 puttrade(n, &trade);
295 continue;
296 }
297
298 if (trade.trd_owner == trade.trd_maxbidder)
299 continue;
300
301 (void)time(&now);
302 if (trade.trd_markettime + TRADE_DELAY > now)
303 continue;
304
305 saveid = trade.trd_unitid;
306 seller = trade.trd_owner;
307 trade.trd_owner = 0;
308 if (!puttrade(n, &trade)) {
309 logerror("Couldn't save trade after purchase; get help!\n");
310 continue;
311 }
312
313 price = trade.trd_price;
314 natp = getnatp(trade.trd_maxbidder);
315 if (natp->nat_money < price) {
316 nreport(trade.trd_maxbidder, N_WELCH_DEAL, seller, 1);
317 wu(0, seller,
318 "%s tried to buy a %s #%d from you for $%.2f\n",
319 cname(trade.trd_maxbidder), trade_nameof(&trade, &tg.gen),
320 saveid, price * tradetax);
321 wu(0, seller, " but couldn't afford it.\n");
322 wu(0, seller,
323 " Your item was taken off the market.\n");
324 wu(0, trade.trd_maxbidder,
325 "You tried to buy %s #%d from %s for $%d\n",
326 trade_nameof(&trade, &tg.gen), saveid, cname(seller),
327 price);
328 wu(0, trade.trd_maxbidder, "but couldn't afford it.\n");
329 continue;
330 }
331
332 /* If we get this far, the sale will go through. */
333
334 natp->nat_money -= price;
335 putnat(natp);
336
337 natp = getnatp(seller);
338 natp->nat_money += roundavg(price * tradetax);
339 putnat(natp);
340
341 switch (trade.trd_type) {
342 case EF_NUKE:
343 tg.nuke.nuk_x = trade.trd_x;
344 tg.nuke.nuk_y = trade.trd_y;
345 tg.nuke.nuk_plane = -1;
346 break;
347 case EF_PLANE:
348 if (!pln_is_in_orbit(&tg.plane)) {
349 tg.plane.pln_x = trade.trd_x;
350 tg.plane.pln_y = trade.trd_y;
351 }
352 if (opt_MOB_ACCESS) {
353 tg.plane.pln_mobil = -(etu_per_update / sect_mob_neg_factor);
354 game_tick_to_now(&tg.plane.pln_access);
355 } else if (tg.plane.pln_mobil > 0) {
356 tg.plane.pln_mobil = 0;
357 }
358 tg.plane.pln_harden = 0;
359 tg.plane.pln_ship = -1;
360 tg.plane.pln_land = -1;
361 break;
362 case EF_SHIP:
363 break;
364 case EF_LAND:
365 tg.land.lnd_x = trade.trd_x;
366 tg.land.lnd_y = trade.trd_y;
367 if (opt_MOB_ACCESS) {
368 tg.land.lnd_mobil = -(etu_per_update / sect_mob_neg_factor);
369 game_tick_to_now(&tg.land.lnd_access);
370 } else if (tg.plane.pln_mobil > 0) {
371 tg.land.lnd_mobil = 0;
372 }
373 tg.land.lnd_harden = 0;
374 unit_drop_cargo(&tg.gen, 0);
375 tg.land.lnd_ship = -1;
376 tg.land.lnd_land = -1;
377 break;
378 default:
379 logerror("Bad trade type %d in trade\n", trade.trd_type);
380 break;
381 }
382 unit_give_away(&tg.gen, trade.trd_maxbidder, 0);
383 put_empobj(trade.trd_type, saveid, &tg.gen);
384
385 nreport(seller, N_MAKE_SALE, trade.trd_maxbidder, 1);
386 wu(0, seller, "%s bought %s #%d from you for $%.2f\n",
387 cname(trade.trd_maxbidder), trade_nameof(&trade, &tg.gen),
388 saveid, price * tradetax);
389 wu(0, trade.trd_maxbidder,
390 "The bidding is over & you bought %s #%d from %s for $%d\n",
391 trade_nameof(&trade, &tg.gen), saveid, cname(seller),
392 price);
393 }
394 return RET_OK;
395 }
396
397 int
ontradingblock(int type,void * ptr)398 ontradingblock(int type, void *ptr)
399 {
400 struct trdstr trade;
401 union empobj_storage tg;
402 int n;
403
404 for (n = 0; gettrade(n, &trade); n++) {
405 if (!trade.trd_owner)
406 continue;
407 if (!trade_getitem(&trade, &tg))
408 continue;
409 if (trade.trd_type != type)
410 continue;
411 if (tg.gen.uid == ((struct empobj *)ptr)->uid)
412 return 1;
413 }
414 return 0;
415 }
416
417 void
trdswitchown(int type,struct empobj * obj,int newown)418 trdswitchown(int type, struct empobj *obj, int newown)
419 {
420 struct trdstr trade;
421 union empobj_storage tg;
422 int n;
423
424 for (n = 0; gettrade(n, &trade); n++) {
425 if (!trade.trd_owner)
426 continue;
427 if (!trade_getitem(&trade, &tg))
428 continue;
429 if (trade.trd_type != type)
430 continue;
431 if (tg.gen.uid != obj->uid)
432 continue;
433 if (trade.trd_owner == trade.trd_maxbidder)
434 trade.trd_maxbidder = newown;
435 trade.trd_owner = newown;
436 puttrade(n, &trade);
437 return;
438 }
439 }
440