2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2010, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
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.
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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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.
28 * trad.c: Buy units/ships/planes/nukes from other nations.
30 * Known contributors to this file:
33 * Steve McClure, 1996-2000
40 #include "commodity.h"
69 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)) {
102 TRADE_DELAY / 3600.0 - (now - trade.trd_markettime) / 3600.0;
105 pr("$%7ld %2d %5.2f hrs ",
106 trade.trd_price, trade.trd_maxbidder, tleft);
107 (void)trade_desc(&trade, &tg); /* XXX */
109 if (trade.trd_owner == player->cnum && !player->god)
110 pr(" (your own lot)\n");
114 pr("Nothing to buy at the moment...\n");
117 p = getstring("Which lot to buy: ", buf);
120 if (isdigit(*p) == 0)
123 if (lotno < 0 || lotno >= ni.cur) {
124 pr("Bad lot number\n");
127 if (!gettrade(lotno, &trade)) {
128 pr("No such lot number\n");
131 if (trade.trd_unitid < 0) {
132 pr("Invalid lot number.\n");
135 if (!trade_getitem(&trade, &tg)) {
136 pr("Can't find trade #%d!\n", trade.trd_unitid);
138 trade.trd_unitid = -1;
139 if (!puttrade(lotno, &trade)) {
140 logerror("trad: can't write trade");
141 pr("Couldn't save after getitem failed; get help!\n");
146 switch (trade.trd_type) {
153 pr("Bad unit type on lot number %d\n", lotno);
156 if (trade.trd_owner == player->cnum) {
157 pr("You can't buy from yourself!\n");
160 price = trade.trd_price;
161 natp = getnatp(player->cnum);
162 if (natp->nat_money < price) {
163 pr("You don't have %.2f to spend!\n", price);
167 for (i = 0; gettrade(i, &tmpt); i++) {
168 if (tmpt.trd_maxbidder == player->cnum &&
169 tmpt.trd_unitid >= 0 && tmpt.trd_owner != player->cnum) {
170 tally += tmpt.trd_price * tradetax;
173 for (i = 0; getcomm(i, &comt); i++) {
174 if (comt.com_maxbidder == player->cnum &&
175 comt.com_owner != 0 && comt.com_owner != player->cnum) {
176 tally += (comt.com_price * comt.com_amount) * buytax;
179 canspend = natp->nat_money - tally;
181 * Find the destination sector for the plane before the trade is
182 * actually made, except for satellites in orbit. Must be owned
183 * and must be a 60% airfield (except for VTOL planes).
185 if (((trade.trd_type == EF_PLANE) && !pln_is_in_orbit(&tg.plane))
186 || (trade.trd_type == EF_NUKE)) {
188 p = getstring("Destination sector: ", buf);
189 if (!trade_check_ok(&trade, &tg))
194 if (!sarg_xy(p, &sx, &sy) || !getsect(sx, sy, §)) {
195 pr("Bad sector designation; try again!\n");
198 if (!player->owner) {
199 pr("You don't own that sector; try again!\n");
202 if (!(plchr[tg.plane.pln_type].pl_flags & P_V)) {
203 if (!player->god && (sect.sct_type != SCT_AIRPT)) {
204 pr("Destination sector is not an airfield!\n");
207 if (!player->god && (sect.sct_effic < 60)) {
208 pr("That airport still under construction!\n");
215 if (trade.trd_type == EF_LAND) {
217 p = getstring("Destination sector: ", buf);
218 if (!trade_check_ok(&trade, &tg))
223 if (!sarg_xy(p, &sx, &sy) || !getsect(sx, sy, §)) {
224 pr("Bad sector designation; try again!\n");
227 if (!player->owner) {
228 pr("You don't own that sector; try again!\n");
231 if (!player->god && (sect.sct_type != SCT_HEADQ)) {
232 pr("Destination sector is not a headquarters!\n");
235 if (!player->god && (sect.sct_effic < 60)) {
236 pr("That headquarters still under construction!\n");
243 p = getstring("How much do you bid: ", buf);
246 if (!trade_check_ok(&trade, &tg))
251 if (bid > canspend) {
252 pr("You don't have %.2f to spend!\n", price);
255 if (bid > trade.trd_price) {
256 /* Add five minutes to the time if less than 5 minutes left. */
258 if (((TRADE_DELAY - (now - trade.trd_markettime)) < 300) &&
259 trade.trd_maxbidder != player->cnum)
260 trade.trd_markettime += 300;
261 trade.trd_price = bid;
262 trade.trd_maxbidder = player->cnum;
265 pr("Your bid on lot #%d is being considered.\n", lotno);
266 if (!puttrade(lotno, &trade))
267 pr("Problems with the trade file. Get help\n");
269 pr("Your bid wasn't high enough (you need to bid more than someone else.)\n");
282 union empobj_storage tg;
288 for (n = 0; gettrade(n, &trade); n++) {
289 if (trade.trd_unitid < 0)
291 if (!trade_getitem(&trade, &tg))
293 if (tg.gen.own == 0) {
295 trade.trd_unitid = -1;
299 if (tg.gen.own != trade.trd_owner) {
300 logerror("Something weird, tg.gen.own != trade.trd_owner!\n");
302 trade.trd_unitid = -1;
307 if (trade.trd_owner == trade.trd_maxbidder)
312 TRADE_DELAY / 3600.0 - (now - trade.trd_markettime) / 3600.0;
318 saveid = trade.trd_unitid;
320 trade.trd_unitid = -1;
321 if (!puttrade(n, &trade)) {
322 logerror("Couldn't save trade after purchase; get help!\n");
326 price = trade.trd_price;
327 natp = getnatp(trade.trd_maxbidder);
328 if (natp->nat_money < price) {
329 nreport(trade.trd_maxbidder, N_WELCH_DEAL, trade.trd_owner, 1);
330 wu(0, trade.trd_owner,
331 "%s tried to buy a %s #%d from you for $%.2f\n",
332 cname(trade.trd_maxbidder), trade_nameof(&trade, &tg),
333 saveid, price * tradetax);
334 wu(0, trade.trd_owner, " but couldn't afford it.\n");
335 wu(0, trade.trd_owner,
336 " Your item was taken off the market.\n");
337 wu(0, trade.trd_maxbidder,
338 "You tried to buy %s #%d from %s for $%.2f\n",
339 trade_nameof(&trade, &tg), saveid, cname(trade.trd_owner),
341 wu(0, trade.trd_maxbidder, "but couldn't afford it.\n");
345 /* If we get this far, the sale will go through. */
347 natp->nat_money -= price;
350 natp = getnatp(trade.trd_owner);
351 natp->nat_money += roundavg(price * tradetax);
354 switch (trade.trd_type) {
356 tg.nuke.nuk_x = trade.trd_x;
357 tg.nuke.nuk_y = trade.trd_y;
358 tg.nuke.nuk_plane = -1;
361 if (!pln_is_in_orbit(&tg.plane)) {
362 tg.plane.pln_x = trade.trd_x;
363 tg.plane.pln_y = trade.trd_y;
365 if (opt_MOB_ACCESS) {
366 tg.plane.pln_mobil = -(etu_per_update / sect_mob_neg_factor);
367 game_tick_to_now(&tg.plane.pln_access);
369 tg.plane.pln_mobil = 0;
371 tg.plane.pln_harden = 0;
372 tg.plane.pln_ship = -1;
373 tg.plane.pln_land = -1;
378 tg.land.lnd_x = trade.trd_x;
379 tg.land.lnd_y = trade.trd_y;
380 if (opt_MOB_ACCESS) {
381 tg.land.lnd_mobil = -(etu_per_update / sect_mob_neg_factor);
382 game_tick_to_now(&tg.land.lnd_access);
384 tg.land.lnd_mobil = 0;
386 tg.land.lnd_harden = 0;
387 unit_drop_cargo(&tg.gen, 0);
388 tg.land.lnd_ship = -1;
389 tg.land.lnd_land = -1;
392 logerror("Bad trade type %d in trade\n", trade.trd_type);
395 unit_give_away(&tg.gen, trade.trd_maxbidder, 0);
396 put_empobj(trade.trd_type, saveid, &tg.gen);
398 nreport(trade.trd_owner, N_MAKE_SALE, trade.trd_maxbidder, 1);
399 wu(0, trade.trd_owner, "%s bought a %s #%d from you for $%.2f\n",
400 cname(trade.trd_maxbidder), trade_nameof(&trade, &tg),
401 saveid, price * tradetax);
402 wu(0, trade.trd_maxbidder,
403 "The bidding is over & you bought %s #%d from %s for $%.2f\n",
404 trade_nameof(&trade, &tg), saveid, cname(trade.trd_owner),
411 ontradingblock(int type, void *ptr)
414 union empobj_storage tg;
417 for (n = 0; gettrade(n, &trade); n++) {
418 if (trade.trd_unitid < 0)
420 if (!trade_getitem(&trade, &tg))
422 if (trade.trd_type != type)
424 if (tg.gen.uid == ((struct empobj *)ptr)->uid)
431 trdswitchown(int type, void *ptr, int newown)
434 union empobj_storage tg;
437 for (n = 0; gettrade(n, &trade); n++) {
438 if (trade.trd_unitid < 0)
440 if (!trade_getitem(&trade, &tg))
442 if (trade.trd_type != type)
444 if (tg.gen.uid != ((struct empobj *)ptr)->uid)
446 if (trade.trd_owner == trade.trd_maxbidder)
447 trade.trd_maxbidder = newown;
448 trade.trd_owner = newown;
450 trade.trd_unitid = -1;