2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2007, 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"
68 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)) {
99 /* fix up database if things get weird */
100 /*if (trade.trd_owner != tg.gen.own) {
101 trade.trd_unitid = -1;
102 (void) puttrade(ni.cur, &trade);
108 TRADE_DELAY / 3600.0 - (now - trade.trd_markettime) / 3600.0;
111 pr("$%7ld %2d %5.2f hrs ",
112 trade.trd_price, trade.trd_maxbidder, tleft);
113 (void)trade_desc(&trade, &tg); /* XXX */
115 if (trade.trd_owner == player->cnum && !player->god)
116 pr(" (your own lot)\n");
120 pr("Nothing to buy at the moment...\n");
123 if ((p = getstring("Which lot to buy: ", buf)) == 0 || *p == 0)
125 if (isdigit(*p) == 0)
128 if (lotno < 0 || lotno >= ni.cur) {
129 pr("Bad lot number\n");
132 if (!gettrade(lotno, &trade)) {
133 pr("No such lot number\n");
136 if (trade.trd_unitid < 0) {
137 pr("Invalid lot number.\n");
140 if (!trade_getitem(&trade, &tg)) {
141 pr("Can't find trade #%d!\n", trade.trd_unitid);
142 trade.trd_unitid = -1;
143 if (!puttrade(lotno, &trade)) {
144 pr("Couldn't save after getitem failed; get help!\n");
149 switch (trade.trd_type) {
156 pr("Bad unit type on lot number %d\n", lotno);
159 if (trade.trd_owner == player->cnum) {
160 pr("You can't buy from yourself!\n");
163 price = trade.trd_price;
164 natp = getnatp(player->cnum);
165 if (natp->nat_money < price) {
166 pr("You don't have %.2f to spend!\n", price);
170 for (q = 0; gettrade(q, &tmpt); q++) {
171 if (tmpt.trd_maxbidder == player->cnum &&
172 tmpt.trd_unitid >= 0 && tmpt.trd_owner != player->cnum) {
173 tally += tmpt.trd_price * tradetax;
176 for (q = 0; getcomm(q, &comt); q++) {
177 if (comt.com_maxbidder == player->cnum &&
178 comt.com_owner != 0 && comt.com_owner != player->cnum) {
179 tally += (comt.com_price * comt.com_amount) * buytax;
182 canspend = natp->nat_money - tally;
184 * Find the destination sector for the plane before the trade
185 * is actually made. Must be owned (except for satellites) and
186 * must be a 60% airfield (except for VTOL planes).
188 if (((trade.trd_type == EF_PLANE) || (trade.trd_type == EF_NUKE))
189 && ((trade.trd_type == EF_NUKE) ||
190 !(tg.plane.pln_flags & PLN_LAUNCHED))) {
191 plflags = plchr[(int)tg.plane.pln_type].pl_flags;
193 p = getstring("Destination sector: ", buf);
194 if (!trade_check_ok(&trade, &tg))
199 if (!sarg_xy(p, &sx, &sy) || !getsect(sx, sy, §)) {
200 pr("Bad sector designation; try again!\n");
203 if (!player->owner && !(plflags & P_O)) {
204 pr("You don't own that sector; try again!\n");
207 if (!(plflags & (P_V | P_O))) {
208 if (!player->god && (sect.sct_type != SCT_AIRPT)) {
209 pr("Destination sector is not an airfield!\n");
212 if (!player->god && (sect.sct_effic < 60)) {
213 pr("That airport still under construction!\n");
220 if (trade.trd_type == EF_LAND) {
222 p = getstring("Destination sector: ", buf);
223 if (!trade_check_ok(&trade, &tg))
228 if (!sarg_xy(p, &sx, &sy) || !getsect(sx, sy, §)) {
229 pr("Bad sector designation; try again!\n");
232 if (!player->owner) {
233 pr("You don't own that sector; try again!\n");
236 if (!player->god && (sect.sct_type != SCT_HEADQ)) {
237 pr("Destination sector is not a headquarters!\n");
240 if (!player->god && (sect.sct_effic < 60)) {
241 pr("That headquarters still under construction!\n");
248 if ((p = getstring("How much do you bid: ", buf)) == 0 || *p == 0)
250 if (!trade_check_ok(&trade, &tg))
255 if (bid > canspend) {
256 pr("You don't have %.2f to spend!\n", price);
259 if (bid > trade.trd_price) {
260 /* Add five minutes to the time if less than 5 minutes left. */
262 if (((TRADE_DELAY - (now - trade.trd_markettime)) < 300) &&
263 trade.trd_maxbidder != player->cnum)
264 trade.trd_markettime += 300;
265 trade.trd_price = bid;
266 trade.trd_maxbidder = player->cnum;
269 pr("Your bid on lot #%d is being considered.\n", lotno);
270 if (!puttrade(lotno, &trade))
271 pr("Problems with the trade file. Get help\n");
273 pr("Your bid wasn't high enough (you need to bid more than someone else.)\n");
289 union empobj_storage tg;
297 /* logerror("Checking the trades.\n");*/
298 for (n = 0; gettrade(n, &trade); n++) {
299 if (trade.trd_unitid < 0)
301 if (!trade_getitem(&trade, &tg))
303 if (tg.gen.own == 0) {
304 trade.trd_unitid = -1;
308 if (tg.gen.own != trade.trd_owner) {
309 logerror("Something weird, tg.gen.own != trade.trd_owner!\n");
310 trade.trd_unitid = -1;
315 if (trade.trd_owner == trade.trd_maxbidder)
320 TRADE_DELAY / 3600.0 - (now - trade.trd_markettime) / 3600.0;
326 saveid = trade.trd_unitid;
327 trade.trd_unitid = -1;
328 if (!puttrade(n, &trade)) {
329 logerror("Couldn't save trade after purchase; get help!\n");
334 price = trade.trd_price;
335 natp = getnatp(trade.trd_maxbidder);
336 if (natp->nat_money <= 0)
338 if (natp->nat_money < price && natp->nat_money > 0) {
339 monleft = price - (natp->nat_money - 1);
341 price = price - monleft;
342 } else if (natp->nat_money > 0) {
344 natp->nat_money -= price;
350 nreport(trade.trd_maxbidder, N_WELCH_DEAL, trade.trd_owner, 1);
351 wu(0, trade.trd_owner,
352 "%s tried to buy a %s #%d from you for $%.2f\n",
353 cname(trade.trd_maxbidder), trade_nameof(&trade, &tg),
354 saveid, price * tradetax);
355 wu(0, trade.trd_owner, " but couldn't afford it.\n");
356 wu(0, trade.trd_owner,
357 " Your item was taken off the market.\n");
358 wu(0, trade.trd_maxbidder,
359 "You tried to buy %s #%d from %s for $%.2f\n",
360 trade_nameof(&trade, &tg), saveid, cname(trade.trd_owner),
362 wu(0, trade.trd_maxbidder, "but couldn't afford it.\n");
366 /* If we get this far, the sale will go through. */
367 /* Only pay tax on the part you actually get cash for. As a break,
368 we don't tax the part you have to give a loan on. */
371 natp = getnatp(trade.trd_owner);
372 /* Make sure we subtract the extra amount */
373 natp->nat_money += (roundavg(price * tradetax) - subleft);
375 switch (trade.trd_type) {
377 tg.nuke.nuk_x = trade.trd_x;
378 tg.nuke.nuk_y = trade.trd_y;
379 makelost(EF_NUKE, tg.nuke.nuk_own, tg.nuke.nuk_uid,
380 tg.nuke.nuk_x, tg.nuke.nuk_y);
381 tg.nuke.nuk_own = trade.trd_maxbidder;
382 makenotlost(EF_NUKE, tg.nuke.nuk_own, tg.nuke.nuk_uid,
383 tg.nuke.nuk_x, tg.nuke.nuk_y);
386 if ((tg.plane.pln_flags & PLN_LAUNCHED) == 0) {
387 tg.plane.pln_x = trade.trd_x;
388 tg.plane.pln_y = trade.trd_y;
390 makelost(EF_PLANE, tg.plane.pln_own, tg.plane.pln_uid,
391 tg.plane.pln_x, tg.plane.pln_y);
392 tg.plane.pln_own = trade.trd_maxbidder;
393 makenotlost(EF_PLANE, tg.plane.pln_own, tg.plane.pln_uid,
394 tg.plane.pln_x, tg.plane.pln_y);
395 tg.plane.pln_wing = 0;
396 /* no cheap version of fly */
397 if (opt_MOB_ACCESS) {
398 tg.plane.pln_mobil = -(etu_per_update / sect_mob_neg_factor);
400 tg.plane.pln_mobil = 0;
402 tg.plane.pln_mission = 0;
403 tg.plane.pln_harden = 0;
404 time(&tg.plane.pln_access);
405 tg.plane.pln_ship = -1;
406 tg.plane.pln_land = -1;
409 takeover_ship(&tg.ship, trade.trd_maxbidder, 0);
412 tg.land.lnd_x = trade.trd_x;
413 tg.land.lnd_y = trade.trd_y;
414 if (tg.land.lnd_ship >= 0) {
416 getship(tg.land.lnd_ship, &ship);
418 putship(ship.shp_uid, &ship);
420 makelost(EF_LAND, tg.land.lnd_own, tg.land.lnd_uid,
421 tg.land.lnd_x, tg.land.lnd_y);
422 tg.land.lnd_own = trade.trd_maxbidder;
423 makenotlost(EF_LAND, tg.land.lnd_own, tg.land.lnd_uid,
424 tg.land.lnd_x, tg.land.lnd_y);
425 tg.land.lnd_army = 0;
426 /* no cheap version of fly */
427 if (opt_MOB_ACCESS) {
428 tg.land.lnd_mobil = -(etu_per_update / sect_mob_neg_factor);
430 tg.land.lnd_mobil = 0;
432 tg.land.lnd_harden = 0;
433 time(&tg.land.lnd_access);
434 tg.land.lnd_mission = 0;
435 /* Drop any land units this unit was carrying */
436 snxtitem_xy(&ni, EF_LAND, tg.land.lnd_x, tg.land.lnd_y);
437 while (nxtitem(&ni, &land)) {
438 if (land.lnd_land != tg.land.lnd_uid)
441 wu(0, land.lnd_own, "unit #%d dropped in %s\n",
443 xyas(land.lnd_x, land.lnd_y, land.lnd_own));
444 putland(land.lnd_uid, &land);
446 /* Drop any planes this unit was carrying */
447 snxtitem_xy(&ni, EF_PLANE, tg.land.lnd_x, tg.land.lnd_y);
448 while (nxtitem(&ni, &plane)) {
449 if (plane.pln_flags & PLN_LAUNCHED)
451 if (plane.pln_land != land.lnd_uid)
454 wu(0, plane.pln_own, "plane #%d dropped in %s\n",
456 xyas(plane.pln_x, plane.pln_y, plane.pln_own));
457 putplane(plane.pln_uid, &plane);
459 tg.land.lnd_ship = -1;
460 tg.land.lnd_land = -1;
463 logerror("Bad trade type %d in trade\n", trade.trd_type);
466 if (!ef_write(trade.trd_type, saveid, &tg)) {
467 logerror("Couldn't write unit to disk; seek help.\n");
470 nreport(trade.trd_owner, N_MAKE_SALE, trade.trd_maxbidder, 1);
471 wu(0, trade.trd_owner, "%s bought a %s #%d from you for $%.2f\n",
472 cname(trade.trd_maxbidder), trade_nameof(&trade, &tg),
473 saveid, price * tradetax);
474 wu(0, trade.trd_maxbidder,
475 "The bidding is over & you bought %s #%d from %s for $%.2f\n",
476 trade_nameof(&trade, &tg), saveid, cname(trade.trd_owner),
479 /* logerror("Done checking the trades.\n");*/
484 ontradingblock(int type, void *ptr)
487 union empobj_storage tg;
490 for (n = 0; gettrade(n, &trade); n++) {
491 if (trade.trd_unitid < 0)
493 if (!trade_getitem(&trade, &tg))
495 if (trade.trd_type != type)
497 if (tg.gen.uid == ((struct empobj *)ptr)->uid)
504 trdswitchown(int type, void *ptr, int newown)
507 union empobj_storage tg;
510 for (n = 0; gettrade(n, &trade); n++) {
511 if (trade.trd_unitid < 0)
513 if (!trade_getitem(&trade, &tg))
515 if (trade.trd_type != type)
517 if (tg.gen.uid != ((struct empobj *)ptr)->uid)
519 if (trade.trd_owner == trade.trd_maxbidder)
520 trade.trd_maxbidder = newown;
521 trade.trd_owner = newown;
523 trade.trd_unitid = -1;