2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2000, 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
53 #include "commodity.h"
58 extern int sect_mob_neg_factor;
59 extern int etu_per_update;
60 extern int TRADE_DELAY;
81 extern double tradetax;
93 pr("The market is disabled.\n");
96 /* First, we execute all trades, so that we can only buy what is available. */
100 pr("\n Empire Trade Report\n ");
103 pr(" lot high bid by time left owner description\n");
104 pr(" --- -------- -- --------- ----- -------------------------\n");
106 snxtitem_all(&ni, EF_TRADE);
107 while (nxtitem(&ni, (char *)&trade)) {
108 if (trade.trd_unitid < 0)
110 if (!trade_getitem(&trade, &tg)) {
113 /* fix up database if things get weird */
114 /*if (trade.trd_owner != tg.gen.trg_own) {
115 trade.trd_unitid = -1;
116 (void) puttrade(ni.cur, &trade);
121 tleft = TRADE_DELAY/3600.0 - (now - trade.trd_markettime) / 3600.0;
122 if (tleft < 0.0) tleft = 0.0;
123 pr("$%7d %2d %5.2f hrs ",trade.trd_maxprice,
124 trade.trd_maxbidder, tleft);
125 (void) trade_desc(&trade, &tg); /* XXX */
127 if (trade.trd_owner == player->cnum && !player->god)
128 pr(" (your own lot)\n");
132 pr("Nothing to buy at the moment...\n");
135 if ((p = getstring("Which lot to buy: ", buf)) == 0 || *p == 0)
137 if (isdigit(*p) == 0)
140 if (lotno < 0 || lotno >= ni.cur) {
141 pr("Bad lot number\n");
144 if (!gettrade(lotno, &trade)) {
145 pr("No such lot number\n");
148 if (trade.trd_unitid < 0) {
149 pr("Invalid lot number.\n");
152 if (!trade_getitem(&trade, &tg)) {
153 pr("Can't find trade #%d!\n", trade.trd_unitid);
154 trade.trd_unitid = -1;
155 if (!puttrade(lotno, &trade)) {
156 pr("Couldn't save after getitem failed; get help!\n");
161 switch (trade.trd_type) {
164 if (!getsect(tg.gen.trg_x, tg.gen.trg_y, §)) {
167 trade.trd_owner = sect.sct_own;
175 pr("Bad unit type on lot number %d\n", lotno);
178 if (trade.trd_owner == player->cnum) {
179 pr("You can't buy from yourself!\n");
182 price = trade.trd_maxprice;
183 natp = getnatp(player->cnum);
184 if (natp->nat_money < price) {
185 pr("You don't have %.2f to spend!\n", price);
189 for (q = 0; gettrade(q, &tmpt); q++) {
190 if (tmpt.trd_maxbidder == player->cnum &&
191 tmpt.trd_unitid >= 0 &&
192 tmpt.trd_owner != player->cnum) {
193 tally += tmpt.trd_maxprice * tradetax;
196 for (q = 0; getcomm(q, &comt); q++) {
197 if (comt.com_maxbidder == player->cnum &&
198 comt.com_owner != 0 &&
199 comt.com_owner != player->cnum) {
200 tally += (comt.com_maxprice * comt.com_amount) * buytax;
203 canspend = natp->nat_money - tally;
205 * Find the destination sector for the plane before the trade
206 * is actually made. Must be owned (except for satellites) and
207 * must be a 60% airfield (except for VTOL planes).
209 if (((trade.trd_type == EF_PLANE) || (trade.trd_type == EF_NUKE))
210 && ((trade.trd_type == EF_NUKE) ||
211 !(tg.pln.pln_flags & PLN_LAUNCHED))) {
212 plflags = plchr[(int)tg.pln.pln_type].pl_flags;
214 p = getstring("Destination sector: ", buf);
215 if (!trade_check_ok(lotno, &trade, &tg))
220 if (!sarg_xy(p, &sx, &sy) ||
221 !getsect(sx, sy, §)) {
222 pr("Bad sector designation; try again!\n");
225 if (!player->owner && !(plflags & P_O)) {
226 pr("You don't own that sector; try again!\n");
229 if (!(plflags & (P_V | P_O))) {
230 if (!player->god && (sect.sct_type != SCT_AIRPT)) {
232 "Destination sector is not an airfield!\n");
235 if (!player->god && (sect.sct_effic < 60)) {
237 "That airport still under construction!\n");
244 if (trade.trd_type == EF_LAND){
246 p = getstring("Destination sector: ", buf);
247 if (!trade_check_ok(lotno, &trade, &tg))
252 if (!sarg_xy(p, &sx, &sy) ||
253 !getsect(sx, sy, §)) {
254 pr("Bad sector designation; try again!\n");
257 if (!player->owner) {
258 pr("You don't own that sector; try again!\n");
261 if (!player->god && (sect.sct_type != SCT_HEADQ)) {
262 pr("Destination sector is not a headquarters!\n");
265 if (!player->god && (sect.sct_effic < 60)) {
266 pr("That headquarters still under construction!\n");
273 pr("WARNING! This market issues credit. If you make more\n");
274 pr(" bids than your treasury can cover at the time of sale,\n");
275 pr(" you can potentially go into financial ruin, and see no\n");
276 pr(" gains. You have been warned.\n\n");
278 if((p = getstring("How much do you bid: ", buf)) == 0 || *p == 0)
280 if (!trade_check_ok(lotno, &trade, &tg))
285 if (bid > canspend) {
286 pr("You don't have %.2f to spend!\n", price);
289 if (bid > trade.trd_maxprice) {
290 /* Add five minutes to the time if less than 5 minutes left. */
292 if (((TRADE_DELAY - (now - trade.trd_markettime)) < 300) &&
293 trade.trd_maxbidder != player->cnum)
294 trade.trd_markettime += 300;
295 trade.trd_maxprice = bid;
296 trade.trd_maxbidder = player->cnum;
299 pr("Your bid on lot #%d is being considered.\n",lotno);
300 if (!puttrade(lotno, &trade))
301 pr("Problems with the trade file. Get help\n");
303 pr("Your bid wasn't high enough (you need to bid more than someone else.)\n");
315 extern double tradetax;
329 long outstanding; /* Outstanding debt */
330 long couval; /* Value of country's goods */
333 /* logerror("Checking the trades.\n");*/
334 for (n = 0; gettrade(n, &trade); n++) {
335 if (trade.trd_unitid < 0)
337 if (!trade_getitem(&trade, &tg))
339 if (tg.gen.trg_own == 0) {
340 trade.trd_unitid = -1;
344 if (tg.gen.trg_own != trade.trd_owner) {
345 logerror("SOmething weird, tg.gen.trg_own != trade.trd_owner!\n");
346 trade.trd_unitid = -1;
351 if (trade.trd_owner == trade.trd_maxbidder)
355 tleft = TRADE_DELAY / 3600.0 - (now - trade.trd_markettime) / 3600.0;
361 saveid = trade.trd_unitid;
362 trade.trd_unitid = -1;
363 if (!puttrade(n, &trade)) {
364 logerror("Couldn't save trade after purchase; get help!\n");
369 price = trade.trd_maxprice;
370 natp = getnatp(trade.trd_maxbidder);
371 if (natp->nat_money <= 0)
373 if (natp->nat_money < price && natp->nat_money > 0) {
374 monleft = price - (natp->nat_money - 1);
376 price = price - monleft;
377 } else if (natp->nat_money > 0) {
379 natp->nat_money -= price;
385 /* Try to make a loan for the rest from the owner. */
386 if (monleft > 0 && natp->nat_money > 0) {
387 if ((float)((float)price / (float)(price + monleft)) < 0.1) {
388 wu(0, trade.trd_maxbidder, "You need at least 10 percent down to purchase something on credit.\n");
390 couval = get_couval(trade.trd_maxbidder);
391 outstanding = get_outstand(trade.trd_maxbidder);
392 couval = couval - outstanding;
393 if (couval > monleft) {
396 for (j = 0; getloan(j, &loan); j++) {
397 if (loan.l_status != LS_FREE)
403 ef_extend(EF_LOAN, 1);
404 loan.l_status = LS_SIGNED;
405 loan.l_loner = trade.trd_owner;
406 loan.l_lonee = trade.trd_maxbidder;
410 loan.l_amtdue = monleft;
411 time(&loan.l_lastpay);
412 loan.l_duedate = (loan.l_ldur * SECS_PER_DAY) + loan.l_lastpay;
414 if (!putloan(j, &loan))
415 logerror("Error writing to the loan file.\n");
418 nreport(trade.trd_maxbidder,N_FIN_TROUBLE, trade.trd_owner, 1);
419 wu(0, trade.trd_maxbidder, "You just took loan #%d for $%.2f to cover the cost of your purchase.\n", j, (float)loan.l_amtdue);
420 wu(0, trade.trd_owner, "You just extended loan #%d to %s to help with the purchase cost.\n", j, cname(trade.trd_maxbidder));
422 nreport(trade.trd_maxbidder,N_CREDIT_JUNK, trade.trd_owner, 1);
423 wu(0, trade.trd_maxbidder, "You don't have enough credit to get a loan.\n");
424 wu(0, trade.trd_owner, "You just turned down a loan to %s.\n", cname(trade.trd_maxbidder));
430 nreport(trade.trd_maxbidder, N_WELCH_DEAL, trade.trd_owner, 1);
431 wu(0, trade.trd_owner, "%s tried to buy a %s #%d from you for $%.2f\n",
432 cname(trade.trd_maxbidder), trade_nameof(&trade, &tg),
433 saveid, (float)(price * tradetax));
434 wu(0, trade.trd_owner, " but couldn't afford it.\n");
435 wu(0, trade.trd_owner, " 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, tg.nuk.nuk_y);
458 tg.nuk.nuk_own = trade.trd_maxbidder;
459 makenotlost(EF_NUKE, tg.nuk.nuk_own, tg.nuk.nuk_uid, tg.nuk.nuk_x, tg.nuk.nuk_y);
462 if ((tg.pln.pln_flags & PLN_LAUNCHED) == 0) {
463 tg.pln.pln_x = trade.trd_x;
464 tg.pln.pln_y = trade.trd_y;
466 makelost(EF_PLANE, tg.pln.pln_own, tg.pln.pln_uid, tg.pln.pln_x, tg.pln.pln_y);
467 tg.pln.pln_own = trade.trd_maxbidder;
468 makenotlost(EF_PLANE, tg.pln.pln_own, tg.pln.pln_uid, tg.pln.pln_x, tg.pln.pln_y);
469 tg.pln.pln_wing = ' ';
470 /* no cheap version of fly */
471 if (opt_MOB_ACCESS) {
472 tg.pln.pln_mobil = -(etu_per_update / sect_mob_neg_factor);
474 tg.pln.pln_mobil = 0;
476 tg.pln.pln_mission = 0;
477 tg.pln.pln_harden = 0;
478 time(&tg.pln.pln_access);
479 tg.pln.pln_ship = -1;
480 tg.pln.pln_land = -1;
483 takeover_ship(&tg.shp, trade.trd_maxbidder, 0);
486 tg.lnd.lnd_x = trade.trd_x;
487 tg.lnd.lnd_y = trade.trd_y;
488 if (tg.lnd.lnd_ship >= 0){
490 getship(tg.lnd.lnd_ship, &ship);
492 putship(ship.shp_uid,&ship);
494 makelost(EF_LAND, tg.lnd.lnd_own, tg.lnd.lnd_uid, tg.lnd.lnd_x, tg.lnd.lnd_y);
495 tg.lnd.lnd_own = trade.trd_maxbidder;
496 makenotlost(EF_LAND, tg.lnd.lnd_own, tg.lnd.lnd_uid, tg.lnd.lnd_x, tg.lnd.lnd_y);
497 tg.lnd.lnd_army = ' ';
498 /* no cheap version of fly */
499 if (opt_MOB_ACCESS) {
500 tg.lnd.lnd_mobil = -(etu_per_update / sect_mob_neg_factor);
502 tg.lnd.lnd_mobil = 0;
504 tg.lnd.lnd_harden = 0;
505 time(&tg.lnd.lnd_access);
506 tg.lnd.lnd_mission = 0;
507 /* Drop any land units this unit was carrying */
508 snxtitem_xy(&ni, EF_LAND, tg.lnd.lnd_x, tg.lnd.lnd_y);
509 while (nxtitem(&ni, (s_char *)&land)) {
510 if (land.lnd_land != tg.lnd.lnd_uid)
513 wu(0, land.lnd_own, "unit #%d dropped in %s\n",
515 xyas(land.lnd_x, land.lnd_y, land.lnd_own));
516 putland(land.lnd_uid, &land);
518 /* Drop any planes this unit was carrying */
519 snxtitem_xy(&ni, EF_PLANE, tg.lnd.lnd_x, tg.lnd.lnd_y);
520 while (nxtitem(&ni, (s_char *)&plane)) {
521 if (plane.pln_flags & PLN_LAUNCHED)
523 if (plane.pln_land != land.lnd_uid)
526 wu(0, plane.pln_own, "plane #%d dropped in %s\n",
528 xyas(plane.pln_x, plane.pln_y, plane.pln_own));
529 putplane(plane.pln_uid, &plane);
531 tg.lnd.lnd_ship = -1;
532 tg.lnd.lnd_land = -1;
535 logerror("Bad trade type %d in trade\n", trade.trd_type);
538 if (!ef_write(trade.trd_type, saveid, (char *)&tg)) {
539 logerror("Couldn't write unit to disk; seek help.\n");
542 nreport(trade.trd_owner, N_MAKE_SALE, trade.trd_maxbidder, 1);
543 wu(0, trade.trd_owner, "%s bought a %s #%d from you for $%.2f\n",
544 cname(trade.trd_maxbidder), trade_nameof(&trade, &tg),
545 saveid, (float)(price * tradetax));
546 wu(0, trade.trd_maxbidder,
547 "The bidding is over & you bought %s #%d from %s for $%.2f\n",
548 trade_nameof(&trade, &tg), saveid, cname(trade.trd_owner),
551 /* logerror("Done checking the trades.\n");*/
556 ontradingblock(int type, int *ptr)
557 /* Generic pointer */
563 for (n = 0; gettrade(n, &trade); n++) {
564 if (trade.trd_unitid < 0)
566 if (!trade_getitem(&trade, &tg))
568 if (trade.trd_type != type)
570 if (tg.gen.trg_uid == ((struct genstr *)ptr)->trg_uid)
577 trdswitchown(int type, int *ptr, int newown)
578 /* Generic pointer */
584 for (n = 0; gettrade(n, &trade); n++) {
585 if (trade.trd_unitid < 0)
587 if (!trade_getitem(&trade, &tg))
589 if (trade.trd_type != type)
591 if (tg.gen.trg_uid != ((struct genstr *)ptr)->trg_uid)
593 if (trade.trd_owner == trade.trd_maxbidder)
594 trade.trd_maxbidder = newown;
595 trade.trd_owner = newown;
597 trade.trd_unitid = -1;