2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, 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
52 #include "commodity.h"
86 pr("The market is disabled.\n");
89 /* First, we execute all trades, so that we can only buy what is available. */
93 pr("\n Empire Trade Report\n ");
96 pr(" lot high bid by time left owner description\n");
97 pr(" --- -------- -- --------- ----- -------------------------\n");
99 snxtitem_all(&ni, EF_TRADE);
100 while (nxtitem(&ni, &trade)) {
101 if (trade.trd_owner == 0)
103 if (!trade_getitem(&trade, &tg)) {
106 /* fix up database if things get weird */
107 /*if (trade.trd_owner != tg.gen.trg_own) {
108 trade.trd_unitid = -1;
109 (void) puttrade(ni.cur, &trade);
115 TRADE_DELAY / 3600.0 - (now - trade.trd_markettime) / 3600.0;
118 pr("$%7ld %2d %5.2f hrs ", trade.trd_price,
119 trade.trd_maxbidder, tleft);
120 (void)trade_desc(&trade, &tg); /* XXX */
122 if (trade.trd_owner == player->cnum && !player->god)
123 pr(" (your own lot)\n");
127 pr("Nothing to buy at the moment...\n");
130 if ((p = getstring("Which lot to buy: ", buf)) == 0 || *p == 0)
132 if (isdigit(*p) == 0)
135 if (lotno < 0 || lotno >= ni.cur) {
136 pr("Bad lot number\n");
139 if (!gettrade(lotno, &trade)) {
140 pr("No such lot number\n");
143 if (trade.trd_unitid < 0) {
144 pr("Invalid lot number.\n");
147 if (!trade_getitem(&trade, &tg)) {
148 pr("Can't find trade #%d!\n", trade.trd_unitid);
149 trade.trd_unitid = -1;
150 if (!puttrade(lotno, &trade)) {
151 pr("Couldn't save after getitem failed; get help!\n");
156 switch (trade.trd_type) {
159 if (!getsect(tg.gen.trg_x, tg.gen.trg_y, §)) {
162 trade.trd_owner = sect.sct_own;
170 pr("Bad unit type on lot number %d\n", lotno);
173 if (trade.trd_owner == player->cnum) {
174 pr("You can't buy from yourself!\n");
177 price = trade.trd_price;
178 natp = getnatp(player->cnum);
179 if (natp->nat_money < price) {
180 pr("You don't have %.2f to spend!\n", price);
184 for (q = 0; gettrade(q, &tmpt); q++) {
185 if (tmpt.trd_maxbidder == player->cnum &&
186 tmpt.trd_unitid >= 0 && tmpt.trd_owner != player->cnum) {
187 tally += tmpt.trd_price * tradetax;
190 for (q = 0; getcomm(q, &comt); q++) {
191 if (comt.com_maxbidder == player->cnum &&
192 comt.com_owner != 0 && comt.com_owner != player->cnum) {
193 tally += (comt.com_price * comt.com_amount) * buytax;
196 canspend = natp->nat_money - tally;
198 * Find the destination sector for the plane before the trade
199 * is actually made. Must be owned (except for satellites) and
200 * must be a 60% airfield (except for VTOL planes).
202 if (((trade.trd_type == EF_PLANE) || (trade.trd_type == EF_NUKE))
203 && ((trade.trd_type == EF_NUKE) ||
204 !(tg.pln.pln_flags & PLN_LAUNCHED))) {
205 plflags = plchr[(int)tg.pln.pln_type].pl_flags;
207 p = getstring("Destination sector: ", buf);
208 if (!trade_check_ok(&trade, &tg))
213 if (!sarg_xy(p, &sx, &sy) || !getsect(sx, sy, §)) {
214 pr("Bad sector designation; try again!\n");
217 if (!player->owner && !(plflags & P_O)) {
218 pr("You don't own that sector; try again!\n");
221 if (!(plflags & (P_V | P_O))) {
222 if (!player->god && (sect.sct_type != SCT_AIRPT)) {
223 pr("Destination sector is not an airfield!\n");
226 if (!player->god && (sect.sct_effic < 60)) {
227 pr("That airport still under construction!\n");
234 if (trade.trd_type == EF_LAND) {
236 p = getstring("Destination sector: ", buf);
237 if (!trade_check_ok(&trade, &tg))
242 if (!sarg_xy(p, &sx, &sy) || !getsect(sx, sy, §)) {
243 pr("Bad sector designation; try again!\n");
246 if (!player->owner) {
247 pr("You don't own that sector; try again!\n");
250 if (!player->god && (sect.sct_type != SCT_HEADQ)) {
251 pr("Destination sector is not a headquarters!\n");
254 if (!player->god && (sect.sct_effic < 60)) {
255 pr("That headquarters still under construction!\n");
262 if ((p = getstring("How much do you bid: ", buf)) == 0 || *p == 0)
264 if (!trade_check_ok(&trade, &tg))
269 if (bid > canspend) {
270 pr("You don't have %.2f to spend!\n", price);
273 if (bid > trade.trd_price) {
274 /* Add five minutes to the time if less than 5 minutes left. */
276 if (((TRADE_DELAY - (now - trade.trd_markettime)) < 300) &&
277 trade.trd_maxbidder != player->cnum)
278 trade.trd_markettime += 300;
279 trade.trd_price = bid;
280 trade.trd_maxbidder = player->cnum;
283 pr("Your bid on lot #%d is being considered.\n", lotno);
284 if (!puttrade(lotno, &trade))
285 pr("Problems with the trade file. Get help\n");
287 pr("Your bid wasn't high enough (you need to bid more than someone else.)\n");
311 /* logerror("Checking the trades.\n");*/
312 for (n = 0; gettrade(n, &trade); n++) {
313 if (trade.trd_unitid < 0)
315 if (!trade_getitem(&trade, &tg))
317 if (tg.gen.trg_own == 0) {
318 trade.trd_unitid = -1;
322 if (tg.gen.trg_own != trade.trd_owner) {
323 logerror("Something weird, tg.gen.trg_own != trade.trd_owner!\n");
324 trade.trd_unitid = -1;
329 if (trade.trd_owner == trade.trd_maxbidder)
334 TRADE_DELAY / 3600.0 - (now - trade.trd_markettime) / 3600.0;
340 saveid = trade.trd_unitid;
341 trade.trd_unitid = -1;
342 if (!puttrade(n, &trade)) {
343 logerror("Couldn't save trade after purchase; get help!\n");
348 price = trade.trd_price;
349 natp = getnatp(trade.trd_maxbidder);
350 if (natp->nat_money <= 0)
352 if (natp->nat_money < price && natp->nat_money > 0) {
353 monleft = price - (natp->nat_money - 1);
355 price = price - monleft;
356 } else if (natp->nat_money > 0) {
358 natp->nat_money -= price;
364 nreport(trade.trd_maxbidder, N_WELCH_DEAL, trade.trd_owner, 1);
365 wu(0, trade.trd_owner,
366 "%s tried to buy a %s #%d from you for $%.2f\n",
367 cname(trade.trd_maxbidder), trade_nameof(&trade, &tg),
368 saveid, (float)(price * tradetax));
369 wu(0, trade.trd_owner, " but couldn't afford it.\n");
370 wu(0, trade.trd_owner,
371 " Your item was taken off the market.\n");
372 wu(0, trade.trd_maxbidder,
373 "You tried to buy %s #%d from %s for $%.2f\n",
374 trade_nameof(&trade, &tg), saveid, cname(trade.trd_owner),
375 (float)(price * tradetax));
376 wu(0, trade.trd_maxbidder, "but couldn't afford it.\n");
380 /* If we get this far, the sale will go through. */
381 /* Only pay tax on the part you actually get cash for. As a break,
382 we don't tax the part you have to give a loan on. */
385 natp = getnatp(trade.trd_owner);
386 /* Make sure we subtract the extra amount */
387 natp->nat_money += (roundavg(price * tradetax) - subleft);
389 switch (trade.trd_type) {
391 tg.nuk.nuk_x = trade.trd_x;
392 tg.nuk.nuk_y = trade.trd_y;
393 makelost(EF_NUKE, tg.nuk.nuk_own, tg.nuk.nuk_uid, tg.nuk.nuk_x,
395 tg.nuk.nuk_own = trade.trd_maxbidder;
396 makenotlost(EF_NUKE, tg.nuk.nuk_own, tg.nuk.nuk_uid,
397 tg.nuk.nuk_x, tg.nuk.nuk_y);
400 if ((tg.pln.pln_flags & PLN_LAUNCHED) == 0) {
401 tg.pln.pln_x = trade.trd_x;
402 tg.pln.pln_y = trade.trd_y;
404 makelost(EF_PLANE, tg.pln.pln_own, tg.pln.pln_uid,
405 tg.pln.pln_x, tg.pln.pln_y);
406 tg.pln.pln_own = trade.trd_maxbidder;
407 makenotlost(EF_PLANE, tg.pln.pln_own, tg.pln.pln_uid,
408 tg.pln.pln_x, tg.pln.pln_y);
409 tg.pln.pln_wing = ' ';
410 /* no cheap version of fly */
411 if (opt_MOB_ACCESS) {
412 tg.pln.pln_mobil = -(etu_per_update / sect_mob_neg_factor);
414 tg.pln.pln_mobil = 0;
416 tg.pln.pln_mission = 0;
417 tg.pln.pln_harden = 0;
418 time(&tg.pln.pln_access);
419 tg.pln.pln_ship = -1;
420 tg.pln.pln_land = -1;
423 takeover_ship(&tg.shp, trade.trd_maxbidder, 0);
426 tg.lnd.lnd_x = trade.trd_x;
427 tg.lnd.lnd_y = trade.trd_y;
428 if (tg.lnd.lnd_ship >= 0) {
430 getship(tg.lnd.lnd_ship, &ship);
432 putship(ship.shp_uid, &ship);
434 makelost(EF_LAND, tg.lnd.lnd_own, tg.lnd.lnd_uid, tg.lnd.lnd_x,
436 tg.lnd.lnd_own = trade.trd_maxbidder;
437 makenotlost(EF_LAND, tg.lnd.lnd_own, tg.lnd.lnd_uid,
438 tg.lnd.lnd_x, tg.lnd.lnd_y);
439 tg.lnd.lnd_army = ' ';
440 /* no cheap version of fly */
441 if (opt_MOB_ACCESS) {
442 tg.lnd.lnd_mobil = -(etu_per_update / sect_mob_neg_factor);
444 tg.lnd.lnd_mobil = 0;
446 tg.lnd.lnd_harden = 0;
447 time(&tg.lnd.lnd_access);
448 tg.lnd.lnd_mission = 0;
449 /* Drop any land units this unit was carrying */
450 snxtitem_xy(&ni, EF_LAND, tg.lnd.lnd_x, tg.lnd.lnd_y);
451 while (nxtitem(&ni, &land)) {
452 if (land.lnd_land != tg.lnd.lnd_uid)
455 wu(0, land.lnd_own, "unit #%d dropped in %s\n",
457 xyas(land.lnd_x, land.lnd_y, land.lnd_own));
458 putland(land.lnd_uid, &land);
460 /* Drop any planes this unit was carrying */
461 snxtitem_xy(&ni, EF_PLANE, tg.lnd.lnd_x, tg.lnd.lnd_y);
462 while (nxtitem(&ni, &plane)) {
463 if (plane.pln_flags & PLN_LAUNCHED)
465 if (plane.pln_land != land.lnd_uid)
468 wu(0, plane.pln_own, "plane #%d dropped in %s\n",
470 xyas(plane.pln_x, plane.pln_y, plane.pln_own));
471 putplane(plane.pln_uid, &plane);
473 tg.lnd.lnd_ship = -1;
474 tg.lnd.lnd_land = -1;
477 logerror("Bad trade type %d in trade\n", trade.trd_type);
480 if (!ef_write(trade.trd_type, saveid, &tg)) {
481 logerror("Couldn't write unit to disk; seek help.\n");
484 nreport(trade.trd_owner, N_MAKE_SALE, trade.trd_maxbidder, 1);
485 wu(0, trade.trd_owner, "%s bought a %s #%d from you for $%.2f\n",
486 cname(trade.trd_maxbidder), trade_nameof(&trade, &tg),
487 saveid, (float)(price * tradetax));
488 wu(0, trade.trd_maxbidder,
489 "The bidding is over & you bought %s #%d from %s for $%.2f\n",
490 trade_nameof(&trade, &tg), saveid, cname(trade.trd_owner),
493 /* logerror("Done checking the trades.\n");*/
498 ontradingblock(int type, int *ptr)
499 /* Generic pointer */
505 for (n = 0; gettrade(n, &trade); n++) {
506 if (trade.trd_unitid < 0)
508 if (!trade_getitem(&trade, &tg))
510 if (trade.trd_type != type)
512 if (tg.gen.trg_uid == ((struct genstr *)ptr)->trg_uid)
519 trdswitchown(int type, int *ptr, int newown)
520 /* Generic pointer */
526 for (n = 0; gettrade(n, &trade); n++) {
527 if (trade.trd_unitid < 0)
529 if (!trade_getitem(&trade, &tg))
531 if (trade.trd_type != type)
533 if (tg.gen.trg_uid != ((struct genstr *)ptr)->trg_uid)
535 if (trade.trd_owner == trade.trd_maxbidder)
536 trade.trd_maxbidder = newown;
537 trade.trd_owner = newown;
539 trade.trd_unitid = -1;