2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2011, 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
39 #include "commodity.h"
68 union empobj_storage tg;
78 pr("The market is disabled.\n");
81 /* First, we execute all trades, so that we can only buy what is available. */
85 pr("\n Empire Trade Report\n ");
88 pr(" lot high bid by time left owner description\n");
89 pr(" --- -------- -- --------- ----- -------------------------\n");
91 snxtitem_all(&ni, EF_TRADE);
92 while (nxtitem(&ni, &trade)) {
93 if (trade.trd_owner == 0)
95 if (!trade_getitem(&trade, &tg)) {
101 TRADE_DELAY / 3600.0 - (now - trade.trd_markettime) / 3600.0;
104 pr("$%7ld %2d %5.2f hrs ",
105 trade.trd_price, trade.trd_maxbidder, tleft);
106 (void)trade_desc(&trade, &tg); /* 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)) {
127 pr("No such lot number\n");
130 if (trade.trd_unitid < 0) {
131 pr("Invalid lot number.\n");
134 if (!trade_getitem(&trade, &tg)) {
135 pr("Can't find trade #%d!\n", trade.trd_unitid);
137 trade.trd_unitid = -1;
138 if (!puttrade(lotno, &trade)) {
139 logerror("trad: can't write trade");
140 pr("Couldn't save after getitem failed; get help!\n");
145 switch (trade.trd_type) {
152 pr("Bad unit type on lot number %d\n", lotno);
155 if (trade.trd_owner == player->cnum) {
156 pr("You can't buy from yourself!\n");
159 price = trade.trd_price;
160 natp = getnatp(player->cnum);
161 if (natp->nat_money < price) {
162 pr("You don't have %.2f to spend!\n", price);
166 for (i = 0; gettrade(i, &tmpt); i++) {
167 if (tmpt.trd_maxbidder == player->cnum &&
168 tmpt.trd_unitid >= 0 && tmpt.trd_owner != player->cnum) {
169 tally += tmpt.trd_price * tradetax;
172 for (i = 0; getcomm(i, &comt); i++) {
173 if (comt.com_maxbidder == player->cnum &&
174 comt.com_owner != 0 && comt.com_owner != player->cnum) {
175 tally += (comt.com_price * comt.com_amount) * buytax;
178 canspend = natp->nat_money - tally;
180 /* Find the destination sector for the trade */
181 if (((trade.trd_type == EF_PLANE) && !pln_is_in_orbit(&tg.plane))
182 || (trade.trd_type == EF_NUKE)) {
184 p = getstring("Destination sector: ", buf);
185 if (!trade_check_ok(&trade, &tg))
190 if (!sarg_xy(p, &sx, &sy) || !getsect(sx, sy, §)) {
191 pr("Bad sector designation; try again!\n");
194 if (!player->owner) {
195 pr("You don't own that sector; try again!\n");
198 if (!(plchr[tg.plane.pln_type].pl_flags & P_V)) {
199 if (!player->god && (sect.sct_type != SCT_AIRPT)) {
200 pr("Destination sector is not an airfield!\n");
203 if (!player->god && (sect.sct_effic < 60)) {
204 pr("That airport still under construction!\n");
210 } else if (trade.trd_type == EF_LAND) {
212 p = getstring("Destination sector: ", buf);
213 if (!trade_check_ok(&trade, &tg))
218 if (!sarg_xy(p, &sx, &sy) || !getsect(sx, sy, §)) {
219 pr("Bad sector designation; try again!\n");
222 if (!player->owner) {
223 pr("You don't own that sector; try again!\n");
226 if (!player->god && (sect.sct_type != SCT_HEADQ)) {
227 pr("Destination sector is not a headquarters!\n");
230 if (!player->god && (sect.sct_effic < 60)) {
231 pr("That headquarters still under construction!\n");
237 /* This trade doesn't teleport; make destination invalid */
242 p = getstring("How much do you bid: ", buf);
245 if (!trade_check_ok(&trade, &tg))
250 if (bid > canspend) {
251 pr("You don't have %.2f to spend!\n", price);
254 if (bid > trade.trd_price) {
255 /* Add five minutes to the time if less than 5 minutes left. */
257 if (((TRADE_DELAY - (now - trade.trd_markettime)) < 300) &&
258 trade.trd_maxbidder != player->cnum)
259 trade.trd_markettime += 300;
260 trade.trd_price = bid;
261 trade.trd_maxbidder = player->cnum;
264 pr("Your bid on lot #%d is being considered.\n", lotno);
265 if (!puttrade(lotno, &trade))
266 pr("Problems with the trade file. Get help\n");
268 pr("Your bid wasn't high enough (you need to bid more than someone else.)\n");
281 union empobj_storage tg;
287 for (n = 0; gettrade(n, &trade); n++) {
288 if (trade.trd_unitid < 0)
290 if (!trade_getitem(&trade, &tg))
292 if (tg.gen.own == 0) {
294 trade.trd_unitid = -1;
298 if (tg.gen.own != trade.trd_owner) {
299 logerror("Something weird, tg.gen.own != trade.trd_owner!\n");
301 trade.trd_unitid = -1;
306 if (trade.trd_owner == trade.trd_maxbidder)
311 TRADE_DELAY / 3600.0 - (now - trade.trd_markettime) / 3600.0;
317 saveid = trade.trd_unitid;
319 trade.trd_unitid = -1;
320 if (!puttrade(n, &trade)) {
321 logerror("Couldn't save trade after purchase; get help!\n");
325 price = trade.trd_price;
326 natp = getnatp(trade.trd_maxbidder);
327 if (natp->nat_money < price) {
328 nreport(trade.trd_maxbidder, N_WELCH_DEAL, trade.trd_owner, 1);
329 wu(0, trade.trd_owner,
330 "%s tried to buy a %s #%d from you for $%.2f\n",
331 cname(trade.trd_maxbidder), trade_nameof(&trade, &tg),
332 saveid, price * tradetax);
333 wu(0, trade.trd_owner, " but couldn't afford it.\n");
334 wu(0, trade.trd_owner,
335 " Your item was taken off the market.\n");
336 wu(0, trade.trd_maxbidder,
337 "You tried to buy %s #%d from %s for $%.2f\n",
338 trade_nameof(&trade, &tg), saveid, cname(trade.trd_owner),
340 wu(0, trade.trd_maxbidder, "but couldn't afford it.\n");
344 /* If we get this far, the sale will go through. */
346 natp->nat_money -= price;
349 natp = getnatp(trade.trd_owner);
350 natp->nat_money += roundavg(price * tradetax);
353 switch (trade.trd_type) {
355 tg.nuke.nuk_x = trade.trd_x;
356 tg.nuke.nuk_y = trade.trd_y;
357 tg.nuke.nuk_plane = -1;
360 if (!pln_is_in_orbit(&tg.plane)) {
361 tg.plane.pln_x = trade.trd_x;
362 tg.plane.pln_y = trade.trd_y;
364 if (opt_MOB_ACCESS) {
365 tg.plane.pln_mobil = -(etu_per_update / sect_mob_neg_factor);
366 game_tick_to_now(&tg.plane.pln_access);
368 tg.plane.pln_mobil = 0;
370 tg.plane.pln_harden = 0;
371 tg.plane.pln_ship = -1;
372 tg.plane.pln_land = -1;
377 tg.land.lnd_x = trade.trd_x;
378 tg.land.lnd_y = trade.trd_y;
379 if (opt_MOB_ACCESS) {
380 tg.land.lnd_mobil = -(etu_per_update / sect_mob_neg_factor);
381 game_tick_to_now(&tg.land.lnd_access);
383 tg.land.lnd_mobil = 0;
385 tg.land.lnd_harden = 0;
386 unit_drop_cargo(&tg.gen, 0);
387 tg.land.lnd_ship = -1;
388 tg.land.lnd_land = -1;
391 logerror("Bad trade type %d in trade\n", trade.trd_type);
394 unit_give_away(&tg.gen, trade.trd_maxbidder, 0);
395 put_empobj(trade.trd_type, saveid, &tg.gen);
397 nreport(trade.trd_owner, N_MAKE_SALE, trade.trd_maxbidder, 1);
398 wu(0, trade.trd_owner, "%s bought a %s #%d from you for $%.2f\n",
399 cname(trade.trd_maxbidder), trade_nameof(&trade, &tg),
400 saveid, price * tradetax);
401 wu(0, trade.trd_maxbidder,
402 "The bidding is over & you bought %s #%d from %s for $%.2f\n",
403 trade_nameof(&trade, &tg), saveid, cname(trade.trd_owner),
410 ontradingblock(int type, void *ptr)
413 union empobj_storage tg;
416 for (n = 0; gettrade(n, &trade); n++) {
417 if (trade.trd_unitid < 0)
419 if (!trade_getitem(&trade, &tg))
421 if (trade.trd_type != type)
423 if (tg.gen.uid == ((struct empobj *)ptr)->uid)
430 trdswitchown(int type, void *ptr, int newown)
433 union empobj_storage tg;
436 for (n = 0; gettrade(n, &trade); n++) {
437 if (trade.trd_unitid < 0)
439 if (!trade_getitem(&trade, &tg))
441 if (trade.trd_type != type)
443 if (tg.gen.uid != ((struct empobj *)ptr)->uid)
445 if (trade.trd_owner == trade.trd_maxbidder)
446 trade.trd_maxbidder = newown;
447 trade.trd_owner = newown;
449 trade.trd_unitid = -1;