2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2020, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
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.
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.
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/>.
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.
27 * trad.c: Buy units/ships/planes/nukes from other nations.
29 * Known contributors to this file:
32 * Steve McClure, 1996-2000
33 * Markus Armbruster, 2004-2018
41 #include "commodity.h"
70 union empobj_storage tg;
79 pr("The market is disabled.\n");
82 /* First, we execute all trades, so that we can only buy what is available. */
86 pr("\n Empire Trade Report\n ");
89 pr(" lot high bid by time left owner description\n");
90 pr(" --- -------- -- --------- ----- -------------------------\n");
92 snxtitem_all(&ni, EF_TRADE);
93 while (nxtitem(&ni, &trade)) {
94 if (trade.trd_owner == 0)
96 if (!trade_getitem(&trade, &tg)) {
101 tleft = trade.trd_markettime + TRADE_DELAY - now;
104 pr("$%7d %2d %5.2f hrs ",
105 trade.trd_price, trade.trd_maxbidder, tleft / 3600.0);
106 trade_desc(&tg.gen); /* XXX */
108 if (trade.trd_owner == player->cnum && !player->god)
109 pr(" (your own lot)\n");
113 pr("Nothing to buy at the moment...\n");
116 p = getstring("Which lot to buy: ", buf);
119 if (isdigit(*p) == 0)
122 if (lotno < 0 || lotno >= ni.cur) {
123 pr("Bad lot number\n");
126 if (!gettrade(lotno, &trade) || !trade.trd_owner) {
127 pr("No such lot number\n");
130 if (!trade_getitem(&trade, &tg)) {
131 pr("Can't find trade #%d!\n", trade.trd_unitid);
133 if (!puttrade(lotno, &trade)) {
134 logerror("trad: can't write trade");
135 pr("Couldn't save after getitem failed; get help!\n");
140 switch (trade.trd_type) {
147 pr("Bad unit type on lot number %d\n", lotno);
150 if (trade.trd_owner == player->cnum) {
151 pr("You can't buy from yourself!\n");
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);
161 for (i = 0; gettrade(i, &tmpt); i++) {
164 if (tmpt.trd_maxbidder == player->cnum &&
165 tmpt.trd_owner != player->cnum) {
166 tally += tmpt.trd_price * tradetax;
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;
175 canspend = natp->nat_money - tally;
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)) {
181 p = getstring("Destination sector: ", buf);
184 if (!trade_check_ok(&trade, &tg.gen))
186 if (!sarg_xy(p, &sx, &sy) || !getsect(sx, sy, §)) {
187 pr("Bad sector designation; try again!\n");
190 if (!player->owner) {
191 pr("You don't own that sector; try again!\n");
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");
199 if (!player->god && (sect.sct_effic < 60)) {
200 pr("That airport still under construction!\n");
206 } else if (trade.trd_type == EF_LAND) {
208 p = getstring("Destination sector: ", buf);
211 if (!trade_check_ok(&trade, &tg.gen))
213 if (!sarg_xy(p, &sx, &sy) || !getsect(sx, sy, §)) {
214 pr("Bad sector designation; try again!\n");
217 if (!player->owner) {
218 pr("You don't own that sector; try again!\n");
221 if (!player->god && (sect.sct_type != SCT_HEADQ)) {
222 pr("Destination sector is not a headquarters!\n");
225 if (!player->god && (sect.sct_effic < 60)) {
226 pr("That headquarters still under construction!\n");
232 /* This trade doesn't teleport; make destination invalid */
237 p = getstring("How much do you bid: ", buf);
240 if (!trade_check_ok(&trade, &tg.gen))
245 if (bid > canspend) {
246 pr("You don't have %.2f to spend!\n", price);
249 if (bid > trade.trd_price) {
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;
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");
262 pr("Your bid wasn't high enough (you need to bid more than someone else.)\n");
275 union empobj_storage tg;
281 for (n = 0; gettrade(n, &trade); n++) {
282 if (!trade.trd_owner)
284 if (!trade_getitem(&trade, &tg))
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.
292 if (tg.gen.own != trade.trd_owner) {
298 if (trade.trd_owner == trade.trd_maxbidder)
302 if (trade.trd_markettime + TRADE_DELAY > now)
305 saveid = trade.trd_unitid;
306 seller = trade.trd_owner;
308 if (!puttrade(n, &trade)) {
309 logerror("Couldn't save trade after purchase; get help!\n");
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);
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");
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),
328 wu(0, trade.trd_maxbidder, "but couldn't afford it.\n");
332 /* If we get this far, the sale will go through. */
334 natp->nat_money -= price;
337 natp = getnatp(seller);
338 natp->nat_money += roundavg(price * tradetax);
341 switch (trade.trd_type) {
343 tg.nuke.nuk_x = trade.trd_x;
344 tg.nuke.nuk_y = trade.trd_y;
345 tg.nuke.nuk_plane = -1;
348 if (!pln_is_in_orbit(&tg.plane)) {
349 tg.plane.pln_x = trade.trd_x;
350 tg.plane.pln_y = trade.trd_y;
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;
358 tg.plane.pln_harden = 0;
359 tg.plane.pln_ship = -1;
360 tg.plane.pln_land = -1;
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;
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;
379 logerror("Bad trade type %d in trade\n", trade.trd_type);
382 unit_give_away(&tg.gen, trade.trd_maxbidder, 0);
383 put_empobj(trade.trd_type, saveid, &tg.gen);
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),
398 ontradingblock(int type, void *ptr)
401 union empobj_storage tg;
404 for (n = 0; gettrade(n, &trade); n++) {
405 if (!trade.trd_owner)
407 if (!trade_getitem(&trade, &tg))
409 if (trade.trd_type != type)
411 if (tg.gen.uid == ((struct empobj *)ptr)->uid)
418 trdswitchown(int type, struct empobj *obj, int newown)
421 union empobj_storage tg;
424 for (n = 0; gettrade(n, &trade); n++) {
425 if (!trade.trd_owner)
427 if (!trade_getitem(&trade, &tg))
429 if (trade.trd_type != type)
431 if (tg.gen.uid != obj->uid)
433 if (trade.trd_owner == trade.trd_maxbidder)
434 trade.trd_maxbidder = newown;
435 trade.trd_owner = newown;