2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2005, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * 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
50 #include "commodity.h"
84 pr("The market is disabled.\n");
87 /* First, we execute all trades, so that we can only buy what is available. */
91 pr("\n Empire Trade Report\n ");
94 pr(" lot high bid by time left owner description\n");
95 pr(" --- -------- -- --------- ----- -------------------------\n");
97 snxtitem_all(&ni, EF_TRADE);
98 while (nxtitem(&ni, &trade)) {
99 if (trade.trd_owner == 0)
101 if (!trade_getitem(&trade, &tg)) {
104 /* fix up database if things get weird */
105 /*if (trade.trd_owner != tg.gen.trg_own) {
106 trade.trd_unitid = -1;
107 (void) puttrade(ni.cur, &trade);
113 TRADE_DELAY / 3600.0 - (now - trade.trd_markettime) / 3600.0;
116 pr("$%7ld %2d %5.2f hrs ", trade.trd_price,
117 trade.trd_maxbidder, tleft);
118 (void)trade_desc(&trade, &tg); /* XXX */
120 if (trade.trd_owner == player->cnum && !player->god)
121 pr(" (your own lot)\n");
125 pr("Nothing to buy at the moment...\n");
128 if ((p = getstring("Which lot to buy: ", buf)) == 0 || *p == 0)
130 if (isdigit(*p) == 0)
133 if (lotno < 0 || lotno >= ni.cur) {
134 pr("Bad lot number\n");
137 if (!gettrade(lotno, &trade)) {
138 pr("No such lot number\n");
141 if (trade.trd_unitid < 0) {
142 pr("Invalid lot number.\n");
145 if (!trade_getitem(&trade, &tg)) {
146 pr("Can't find trade #%d!\n", trade.trd_unitid);
147 trade.trd_unitid = -1;
148 if (!puttrade(lotno, &trade)) {
149 pr("Couldn't save after getitem failed; get help!\n");
154 switch (trade.trd_type) {
157 if (!getsect(tg.gen.trg_x, tg.gen.trg_y, §)) {
160 trade.trd_owner = sect.sct_own;
168 pr("Bad unit type on lot number %d\n", lotno);
171 if (trade.trd_owner == player->cnum) {
172 pr("You can't buy from yourself!\n");
175 price = trade.trd_price;
176 natp = getnatp(player->cnum);
177 if (natp->nat_money < price) {
178 pr("You don't have %.2f to spend!\n", price);
182 for (q = 0; gettrade(q, &tmpt); q++) {
183 if (tmpt.trd_maxbidder == player->cnum &&
184 tmpt.trd_unitid >= 0 && tmpt.trd_owner != player->cnum) {
185 tally += tmpt.trd_price * tradetax;
188 for (q = 0; getcomm(q, &comt); q++) {
189 if (comt.com_maxbidder == player->cnum &&
190 comt.com_owner != 0 && comt.com_owner != player->cnum) {
191 tally += (comt.com_price * comt.com_amount) * buytax;
194 canspend = natp->nat_money - tally;
196 * Find the destination sector for the plane before the trade
197 * is actually made. Must be owned (except for satellites) and
198 * must be a 60% airfield (except for VTOL planes).
200 if (((trade.trd_type == EF_PLANE) || (trade.trd_type == EF_NUKE))
201 && ((trade.trd_type == EF_NUKE) ||
202 !(tg.pln.pln_flags & PLN_LAUNCHED))) {
203 plflags = plchr[(int)tg.pln.pln_type].pl_flags;
205 p = getstring("Destination sector: ", buf);
206 if (!trade_check_ok(&trade, &tg))
211 if (!sarg_xy(p, &sx, &sy) || !getsect(sx, sy, §)) {
212 pr("Bad sector designation; try again!\n");
215 if (!player->owner && !(plflags & P_O)) {
216 pr("You don't own that sector; try again!\n");
219 if (!(plflags & (P_V | P_O))) {
220 if (!player->god && (sect.sct_type != SCT_AIRPT)) {
221 pr("Destination sector is not an airfield!\n");
224 if (!player->god && (sect.sct_effic < 60)) {
225 pr("That airport still under construction!\n");
232 if (trade.trd_type == EF_LAND) {
234 p = getstring("Destination sector: ", buf);
235 if (!trade_check_ok(&trade, &tg))
240 if (!sarg_xy(p, &sx, &sy) || !getsect(sx, sy, §)) {
241 pr("Bad sector designation; try again!\n");
244 if (!player->owner) {
245 pr("You don't own that sector; try again!\n");
248 if (!player->god && (sect.sct_type != SCT_HEADQ)) {
249 pr("Destination sector is not a headquarters!\n");
252 if (!player->god && (sect.sct_effic < 60)) {
253 pr("That headquarters still under construction!\n");
260 if ((p = getstring("How much do you bid: ", buf)) == 0 || *p == 0)
262 if (!trade_check_ok(&trade, &tg))
267 if (bid > canspend) {
268 pr("You don't have %.2f to spend!\n", price);
271 if (bid > trade.trd_price) {
272 /* Add five minutes to the time if less than 5 minutes left. */
274 if (((TRADE_DELAY - (now - trade.trd_markettime)) < 300) &&
275 trade.trd_maxbidder != player->cnum)
276 trade.trd_markettime += 300;
277 trade.trd_price = bid;
278 trade.trd_maxbidder = player->cnum;
281 pr("Your bid on lot #%d is being considered.\n", lotno);
282 if (!puttrade(lotno, &trade))
283 pr("Problems with the trade file. Get help\n");
285 pr("Your bid wasn't high enough (you need to bid more than someone else.)\n");
309 /* logerror("Checking the trades.\n");*/
310 for (n = 0; gettrade(n, &trade); n++) {
311 if (trade.trd_unitid < 0)
313 if (!trade_getitem(&trade, &tg))
315 if (tg.gen.trg_own == 0) {
316 trade.trd_unitid = -1;
320 if (tg.gen.trg_own != trade.trd_owner) {
321 logerror("Something weird, tg.gen.trg_own != trade.trd_owner!\n");
322 trade.trd_unitid = -1;
327 if (trade.trd_owner == trade.trd_maxbidder)
332 TRADE_DELAY / 3600.0 - (now - trade.trd_markettime) / 3600.0;
338 saveid = trade.trd_unitid;
339 trade.trd_unitid = -1;
340 if (!puttrade(n, &trade)) {
341 logerror("Couldn't save trade after purchase; get help!\n");
346 price = trade.trd_price;
347 natp = getnatp(trade.trd_maxbidder);
348 if (natp->nat_money <= 0)
350 if (natp->nat_money < price && natp->nat_money > 0) {
351 monleft = price - (natp->nat_money - 1);
353 price = price - monleft;
354 } else if (natp->nat_money > 0) {
356 natp->nat_money -= price;
362 nreport(trade.trd_maxbidder, N_WELCH_DEAL, trade.trd_owner, 1);
363 wu(0, trade.trd_owner,
364 "%s tried to buy a %s #%d from you for $%.2f\n",
365 cname(trade.trd_maxbidder), trade_nameof(&trade, &tg),
366 saveid, (float)(price * tradetax));
367 wu(0, trade.trd_owner, " but couldn't afford it.\n");
368 wu(0, trade.trd_owner,
369 " Your item was taken off the market.\n");
370 wu(0, trade.trd_maxbidder,
371 "You tried to buy %s #%d from %s for $%.2f\n",
372 trade_nameof(&trade, &tg), saveid, cname(trade.trd_owner),
373 (float)(price * tradetax));
374 wu(0, trade.trd_maxbidder, "but couldn't afford it.\n");
378 /* If we get this far, the sale will go through. */
379 /* Only pay tax on the part you actually get cash for. As a break,
380 we don't tax the part you have to give a loan on. */
383 natp = getnatp(trade.trd_owner);
384 /* Make sure we subtract the extra amount */
385 natp->nat_money += (roundavg(price * tradetax) - subleft);
387 switch (trade.trd_type) {
389 tg.nuk.nuk_x = trade.trd_x;
390 tg.nuk.nuk_y = trade.trd_y;
391 makelost(EF_NUKE, tg.nuk.nuk_own, tg.nuk.nuk_uid, tg.nuk.nuk_x,
393 tg.nuk.nuk_own = trade.trd_maxbidder;
394 makenotlost(EF_NUKE, tg.nuk.nuk_own, tg.nuk.nuk_uid,
395 tg.nuk.nuk_x, tg.nuk.nuk_y);
398 if ((tg.pln.pln_flags & PLN_LAUNCHED) == 0) {
399 tg.pln.pln_x = trade.trd_x;
400 tg.pln.pln_y = trade.trd_y;
402 makelost(EF_PLANE, tg.pln.pln_own, tg.pln.pln_uid,
403 tg.pln.pln_x, tg.pln.pln_y);
404 tg.pln.pln_own = trade.trd_maxbidder;
405 makenotlost(EF_PLANE, tg.pln.pln_own, tg.pln.pln_uid,
406 tg.pln.pln_x, tg.pln.pln_y);
407 tg.pln.pln_wing = ' ';
408 /* no cheap version of fly */
409 if (opt_MOB_ACCESS) {
410 tg.pln.pln_mobil = -(etu_per_update / sect_mob_neg_factor);
412 tg.pln.pln_mobil = 0;
414 tg.pln.pln_mission = 0;
415 tg.pln.pln_harden = 0;
416 time(&tg.pln.pln_access);
417 tg.pln.pln_ship = -1;
418 tg.pln.pln_land = -1;
421 takeover_ship(&tg.shp, trade.trd_maxbidder, 0);
424 tg.lnd.lnd_x = trade.trd_x;
425 tg.lnd.lnd_y = trade.trd_y;
426 if (tg.lnd.lnd_ship >= 0) {
428 getship(tg.lnd.lnd_ship, &ship);
430 putship(ship.shp_uid, &ship);
432 makelost(EF_LAND, tg.lnd.lnd_own, tg.lnd.lnd_uid, tg.lnd.lnd_x,
434 tg.lnd.lnd_own = trade.trd_maxbidder;
435 makenotlost(EF_LAND, tg.lnd.lnd_own, tg.lnd.lnd_uid,
436 tg.lnd.lnd_x, tg.lnd.lnd_y);
437 tg.lnd.lnd_army = ' ';
438 /* no cheap version of fly */
439 if (opt_MOB_ACCESS) {
440 tg.lnd.lnd_mobil = -(etu_per_update / sect_mob_neg_factor);
442 tg.lnd.lnd_mobil = 0;
444 tg.lnd.lnd_harden = 0;
445 time(&tg.lnd.lnd_access);
446 tg.lnd.lnd_mission = 0;
447 /* Drop any land units this unit was carrying */
448 snxtitem_xy(&ni, EF_LAND, tg.lnd.lnd_x, tg.lnd.lnd_y);
449 while (nxtitem(&ni, &land)) {
450 if (land.lnd_land != tg.lnd.lnd_uid)
453 wu(0, land.lnd_own, "unit #%d dropped in %s\n",
455 xyas(land.lnd_x, land.lnd_y, land.lnd_own));
456 putland(land.lnd_uid, &land);
458 /* Drop any planes this unit was carrying */
459 snxtitem_xy(&ni, EF_PLANE, tg.lnd.lnd_x, tg.lnd.lnd_y);
460 while (nxtitem(&ni, &plane)) {
461 if (plane.pln_flags & PLN_LAUNCHED)
463 if (plane.pln_land != land.lnd_uid)
466 wu(0, plane.pln_own, "plane #%d dropped in %s\n",
468 xyas(plane.pln_x, plane.pln_y, plane.pln_own));
469 putplane(plane.pln_uid, &plane);
471 tg.lnd.lnd_ship = -1;
472 tg.lnd.lnd_land = -1;
475 logerror("Bad trade type %d in trade\n", trade.trd_type);
478 if (!ef_write(trade.trd_type, saveid, &tg)) {
479 logerror("Couldn't write unit to disk; seek help.\n");
482 nreport(trade.trd_owner, N_MAKE_SALE, trade.trd_maxbidder, 1);
483 wu(0, trade.trd_owner, "%s bought a %s #%d from you for $%.2f\n",
484 cname(trade.trd_maxbidder), trade_nameof(&trade, &tg),
485 saveid, (float)(price * tradetax));
486 wu(0, trade.trd_maxbidder,
487 "The bidding is over & you bought %s #%d from %s for $%.2f\n",
488 trade_nameof(&trade, &tg), saveid, cname(trade.trd_owner),
491 /* logerror("Done checking the trades.\n");*/
496 ontradingblock(int type, int *ptr)
497 /* Generic pointer */
503 for (n = 0; gettrade(n, &trade); n++) {
504 if (trade.trd_unitid < 0)
506 if (!trade_getitem(&trade, &tg))
508 if (trade.trd_type != type)
510 if (tg.gen.trg_uid == ((struct genstr *)ptr)->trg_uid)
517 trdswitchown(int type, int *ptr, int newown)
518 /* Generic pointer */
524 for (n = 0; gettrade(n, &trade); n++) {
525 if (trade.trd_unitid < 0)
527 if (!trade_getitem(&trade, &tg))
529 if (trade.trd_type != type)
531 if (tg.gen.trg_uid != ((struct genstr *)ptr)->trg_uid)
533 if (trade.trd_owner == trade.trd_maxbidder)
534 trade.trd_maxbidder = newown;
535 trade.trd_owner = newown;
537 trade.trd_unitid = -1;