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, (char *)&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 pr("WARNING! This market issues credit. If you make more\n");
261 pr(" bids than your treasury can cover at the time of sale,\n");
262 pr(" you can potentially go into financial ruin, and see no\n");
263 pr(" gains. You have been warned.\n\n");
265 if ((p = getstring("How much do you bid: ", buf)) == 0 || *p == 0)
267 if (!trade_check_ok(&trade, &tg))
272 if (bid > canspend) {
273 pr("You don't have %.2f to spend!\n", price);
276 if (bid > trade.trd_price) {
277 /* Add five minutes to the time if less than 5 minutes left. */
279 if (((TRADE_DELAY - (now - trade.trd_markettime)) < 300) &&
280 trade.trd_maxbidder != player->cnum)
281 trade.trd_markettime += 300;
282 trade.trd_price = bid;
283 trade.trd_maxbidder = player->cnum;
286 pr("Your bid on lot #%d is being considered.\n", lotno);
287 if (!puttrade(lotno, &trade))
288 pr("Problems with the trade file. Get help\n");
290 pr("Your bid wasn't high enough (you need to bid more than someone else.)\n");
315 long outstanding; /* Outstanding debt */
316 long couval; /* Value of country's goods */
319 /* logerror("Checking the trades.\n");*/
320 for (n = 0; gettrade(n, &trade); n++) {
321 if (trade.trd_unitid < 0)
323 if (!trade_getitem(&trade, &tg))
325 if (tg.gen.trg_own == 0) {
326 trade.trd_unitid = -1;
330 if (tg.gen.trg_own != trade.trd_owner) {
331 logerror("Something weird, tg.gen.trg_own != trade.trd_owner!\n");
332 trade.trd_unitid = -1;
337 if (trade.trd_owner == trade.trd_maxbidder)
342 TRADE_DELAY / 3600.0 - (now - trade.trd_markettime) / 3600.0;
348 saveid = trade.trd_unitid;
349 trade.trd_unitid = -1;
350 if (!puttrade(n, &trade)) {
351 logerror("Couldn't save trade after purchase; get help!\n");
356 price = trade.trd_price;
357 natp = getnatp(trade.trd_maxbidder);
358 if (natp->nat_money <= 0)
360 if (natp->nat_money < price && natp->nat_money > 0) {
361 monleft = price - (natp->nat_money - 1);
363 price = price - monleft;
364 } else if (natp->nat_money > 0) {
366 natp->nat_money -= price;
372 /* Try to make a loan for the rest from the owner. */
373 if (monleft > 0 && natp->nat_money > 0) {
374 if ((float)((float)price / (float)(price + monleft)) < 0.1) {
375 wu(0, trade.trd_maxbidder,
376 "You need at least 10 percent down to purchase something on credit.\n");
378 couval = get_couval(trade.trd_maxbidder);
379 outstanding = get_outstand(trade.trd_maxbidder);
380 couval = couval - outstanding;
381 if (couval > monleft) {
384 for (j = 0; getloan(j, &loan); j++) {
385 if (loan.l_status != LS_FREE)
391 ef_extend(EF_LOAN, 1);
392 loan.l_status = LS_SIGNED;
393 loan.l_loner = trade.trd_owner;
394 loan.l_lonee = trade.trd_maxbidder;
398 loan.l_amtdue = monleft;
399 time(&loan.l_lastpay);
401 (loan.l_ldur * SECS_PER_DAY) + loan.l_lastpay;
403 if (!putloan(j, &loan))
404 logerror("Error writing to the loan file.\n");
407 nreport(trade.trd_maxbidder, N_FIN_TROUBLE,
409 wu(0, trade.trd_maxbidder,
410 "You just took loan #%d for $%.2f to cover the cost of your purchase.\n",
411 j, (float)loan.l_amtdue);
412 wu(0, trade.trd_owner,
413 "You just extended loan #%d to %s to help with the purchase cost.\n",
414 j, cname(trade.trd_maxbidder));
416 nreport(trade.trd_maxbidder, N_CREDIT_JUNK,
418 wu(0, trade.trd_maxbidder,
419 "You don't have enough credit to get a loan.\n");
420 wu(0, trade.trd_owner,
421 "You just turned down a loan to %s.\n",
422 cname(trade.trd_maxbidder));
428 nreport(trade.trd_maxbidder, N_WELCH_DEAL, trade.trd_owner, 1);
429 wu(0, trade.trd_owner,
430 "%s tried to buy a %s #%d from you for $%.2f\n",
431 cname(trade.trd_maxbidder), trade_nameof(&trade, &tg),
432 saveid, (float)(price * tradetax));
433 wu(0, trade.trd_owner, " but couldn't afford it.\n");
434 wu(0, trade.trd_owner,
435 " Your item was taken off the market.\n");
436 wu(0, trade.trd_maxbidder,
437 "You tried to buy %s #%d from %s for $%.2f\n",
438 trade_nameof(&trade, &tg), saveid, cname(trade.trd_owner),
439 (float)(price * tradetax));
440 wu(0, trade.trd_maxbidder, "but couldn't afford it.\n");
444 /* If we get this far, the sale will go through. */
445 /* Only pay tax on the part you actually get cash for. As a break,
446 we don't tax the part you have to give a loan on. */
449 natp = getnatp(trade.trd_owner);
450 /* Make sure we subtract the extra amount */
451 natp->nat_money += (roundavg(price * tradetax) - subleft);
453 switch (trade.trd_type) {
455 tg.nuk.nuk_x = trade.trd_x;
456 tg.nuk.nuk_y = trade.trd_y;
457 makelost(EF_NUKE, tg.nuk.nuk_own, tg.nuk.nuk_uid, tg.nuk.nuk_x,
459 tg.nuk.nuk_own = trade.trd_maxbidder;
460 makenotlost(EF_NUKE, tg.nuk.nuk_own, tg.nuk.nuk_uid,
461 tg.nuk.nuk_x, tg.nuk.nuk_y);
464 if ((tg.pln.pln_flags & PLN_LAUNCHED) == 0) {
465 tg.pln.pln_x = trade.trd_x;
466 tg.pln.pln_y = trade.trd_y;
468 makelost(EF_PLANE, tg.pln.pln_own, tg.pln.pln_uid,
469 tg.pln.pln_x, tg.pln.pln_y);
470 tg.pln.pln_own = trade.trd_maxbidder;
471 makenotlost(EF_PLANE, tg.pln.pln_own, tg.pln.pln_uid,
472 tg.pln.pln_x, tg.pln.pln_y);
473 tg.pln.pln_wing = ' ';
474 /* no cheap version of fly */
475 if (opt_MOB_ACCESS) {
476 tg.pln.pln_mobil = -(etu_per_update / sect_mob_neg_factor);
478 tg.pln.pln_mobil = 0;
480 tg.pln.pln_mission = 0;
481 tg.pln.pln_harden = 0;
482 time(&tg.pln.pln_access);
483 tg.pln.pln_ship = -1;
484 tg.pln.pln_land = -1;
487 takeover_ship(&tg.shp, trade.trd_maxbidder, 0);
490 tg.lnd.lnd_x = trade.trd_x;
491 tg.lnd.lnd_y = trade.trd_y;
492 if (tg.lnd.lnd_ship >= 0) {
494 getship(tg.lnd.lnd_ship, &ship);
496 putship(ship.shp_uid, &ship);
498 makelost(EF_LAND, tg.lnd.lnd_own, tg.lnd.lnd_uid, tg.lnd.lnd_x,
500 tg.lnd.lnd_own = trade.trd_maxbidder;
501 makenotlost(EF_LAND, tg.lnd.lnd_own, tg.lnd.lnd_uid,
502 tg.lnd.lnd_x, tg.lnd.lnd_y);
503 tg.lnd.lnd_army = ' ';
504 /* no cheap version of fly */
505 if (opt_MOB_ACCESS) {
506 tg.lnd.lnd_mobil = -(etu_per_update / sect_mob_neg_factor);
508 tg.lnd.lnd_mobil = 0;
510 tg.lnd.lnd_harden = 0;
511 time(&tg.lnd.lnd_access);
512 tg.lnd.lnd_mission = 0;
513 /* Drop any land units this unit was carrying */
514 snxtitem_xy(&ni, EF_LAND, tg.lnd.lnd_x, tg.lnd.lnd_y);
515 while (nxtitem(&ni, (s_char *)&land)) {
516 if (land.lnd_land != tg.lnd.lnd_uid)
519 wu(0, land.lnd_own, "unit #%d dropped in %s\n",
521 xyas(land.lnd_x, land.lnd_y, land.lnd_own));
522 putland(land.lnd_uid, &land);
524 /* Drop any planes this unit was carrying */
525 snxtitem_xy(&ni, EF_PLANE, tg.lnd.lnd_x, tg.lnd.lnd_y);
526 while (nxtitem(&ni, (s_char *)&plane)) {
527 if (plane.pln_flags & PLN_LAUNCHED)
529 if (plane.pln_land != land.lnd_uid)
532 wu(0, plane.pln_own, "plane #%d dropped in %s\n",
534 xyas(plane.pln_x, plane.pln_y, plane.pln_own));
535 putplane(plane.pln_uid, &plane);
537 tg.lnd.lnd_ship = -1;
538 tg.lnd.lnd_land = -1;
541 logerror("Bad trade type %d in trade\n", trade.trd_type);
544 if (!ef_write(trade.trd_type, saveid, (char *)&tg)) {
545 logerror("Couldn't write unit to disk; seek help.\n");
548 nreport(trade.trd_owner, N_MAKE_SALE, trade.trd_maxbidder, 1);
549 wu(0, trade.trd_owner, "%s bought a %s #%d from you for $%.2f\n",
550 cname(trade.trd_maxbidder), trade_nameof(&trade, &tg),
551 saveid, (float)(price * tradetax));
552 wu(0, trade.trd_maxbidder,
553 "The bidding is over & you bought %s #%d from %s for $%.2f\n",
554 trade_nameof(&trade, &tg), saveid, cname(trade.trd_owner),
557 /* logerror("Done checking the trades.\n");*/
562 ontradingblock(int type, int *ptr)
563 /* Generic pointer */
569 for (n = 0; gettrade(n, &trade); n++) {
570 if (trade.trd_unitid < 0)
572 if (!trade_getitem(&trade, &tg))
574 if (trade.trd_type != type)
576 if (tg.gen.trg_uid == ((struct genstr *)ptr)->trg_uid)
583 trdswitchown(int type, int *ptr, int newown)
584 /* Generic pointer */
590 for (n = 0; gettrade(n, &trade); n++) {
591 if (trade.trd_unitid < 0)
593 if (!trade_getitem(&trade, &tg))
595 if (trade.trd_type != type)
597 if (tg.gen.trg_uid != ((struct genstr *)ptr)->trg_uid)
599 if (trade.trd_owner == trade.trd_maxbidder)
600 trade.trd_maxbidder = newown;
601 trade.trd_owner = newown;
603 trade.trd_unitid = -1;