2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2008, 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;
295 /* logerror("Checking the trades.\n");*/
296 for (n = 0; gettrade(n, &trade); n++) {
297 if (trade.trd_unitid < 0)
299 if (!trade_getitem(&trade, &tg))
301 if (tg.gen.own == 0) {
302 trade.trd_unitid = -1;
306 if (tg.gen.own != trade.trd_owner) {
307 logerror("Something weird, tg.gen.own != trade.trd_owner!\n");
308 trade.trd_unitid = -1;
313 if (trade.trd_owner == trade.trd_maxbidder)
318 TRADE_DELAY / 3600.0 - (now - trade.trd_markettime) / 3600.0;
324 saveid = trade.trd_unitid;
325 trade.trd_unitid = -1;
326 if (!puttrade(n, &trade)) {
327 logerror("Couldn't save trade after purchase; get help!\n");
331 price = trade.trd_price;
332 natp = getnatp(trade.trd_maxbidder);
333 if (natp->nat_money < price) {
334 nreport(trade.trd_maxbidder, N_WELCH_DEAL, trade.trd_owner, 1);
335 wu(0, trade.trd_owner,
336 "%s tried to buy a %s #%d from you for $%.2f\n",
337 cname(trade.trd_maxbidder), trade_nameof(&trade, &tg),
338 saveid, price * tradetax);
339 wu(0, trade.trd_owner, " but couldn't afford it.\n");
340 wu(0, trade.trd_owner,
341 " Your item was taken off the market.\n");
342 wu(0, trade.trd_maxbidder,
343 "You tried to buy %s #%d from %s for $%.2f\n",
344 trade_nameof(&trade, &tg), saveid, cname(trade.trd_owner),
346 wu(0, trade.trd_maxbidder, "but couldn't afford it.\n");
350 /* If we get this far, the sale will go through. */
352 natp->nat_money -= price;
355 natp = getnatp(trade.trd_owner);
356 natp->nat_money += roundavg(price * tradetax);
359 switch (trade.trd_type) {
361 tg.nuke.nuk_x = trade.trd_x;
362 tg.nuke.nuk_y = trade.trd_y;
363 makelost(EF_NUKE, tg.nuke.nuk_own, tg.nuke.nuk_uid,
364 tg.nuke.nuk_x, tg.nuke.nuk_y);
365 tg.nuke.nuk_own = trade.trd_maxbidder;
366 makenotlost(EF_NUKE, tg.nuke.nuk_own, tg.nuke.nuk_uid,
367 tg.nuke.nuk_x, tg.nuke.nuk_y);
370 if ((tg.plane.pln_flags & PLN_LAUNCHED) == 0) {
371 tg.plane.pln_x = trade.trd_x;
372 tg.plane.pln_y = trade.trd_y;
374 makelost(EF_PLANE, tg.plane.pln_own, tg.plane.pln_uid,
375 tg.plane.pln_x, tg.plane.pln_y);
376 tg.plane.pln_own = trade.trd_maxbidder;
377 makenotlost(EF_PLANE, tg.plane.pln_own, tg.plane.pln_uid,
378 tg.plane.pln_x, tg.plane.pln_y);
379 tg.plane.pln_wing = 0;
380 /* no cheap version of fly */
381 if (opt_MOB_ACCESS) {
382 tg.plane.pln_mobil = -(etu_per_update / sect_mob_neg_factor);
383 game_tick_to_now(&tg.plane.pln_access);
385 tg.plane.pln_mobil = 0;
387 tg.plane.pln_mission = 0;
388 tg.plane.pln_harden = 0;
389 tg.plane.pln_ship = -1;
390 tg.plane.pln_land = -1;
393 takeover_ship(&tg.ship, trade.trd_maxbidder, 0);
396 tg.land.lnd_x = trade.trd_x;
397 tg.land.lnd_y = trade.trd_y;
398 if (tg.land.lnd_ship >= 0) {
400 getship(tg.land.lnd_ship, &ship);
402 putship(ship.shp_uid, &ship);
404 makelost(EF_LAND, tg.land.lnd_own, tg.land.lnd_uid,
405 tg.land.lnd_x, tg.land.lnd_y);
406 tg.land.lnd_own = trade.trd_maxbidder;
407 makenotlost(EF_LAND, tg.land.lnd_own, tg.land.lnd_uid,
408 tg.land.lnd_x, tg.land.lnd_y);
409 tg.land.lnd_army = 0;
410 /* no cheap version of fly */
411 if (opt_MOB_ACCESS) {
412 tg.land.lnd_mobil = -(etu_per_update / sect_mob_neg_factor);
413 game_tick_to_now(&tg.land.lnd_access);
415 tg.land.lnd_mobil = 0;
417 tg.land.lnd_harden = 0;
418 tg.land.lnd_mission = 0;
419 /* Drop any land units this unit was carrying */
420 snxtitem_xy(&ni, EF_LAND, tg.land.lnd_x, tg.land.lnd_y);
421 while (nxtitem(&ni, &land)) {
422 if (land.lnd_land != tg.land.lnd_uid)
425 wu(0, land.lnd_own, "unit #%d dropped in %s\n",
427 xyas(land.lnd_x, land.lnd_y, land.lnd_own));
428 putland(land.lnd_uid, &land);
430 /* Drop any planes this unit was carrying */
431 snxtitem_xy(&ni, EF_PLANE, tg.land.lnd_x, tg.land.lnd_y);
432 while (nxtitem(&ni, &plane)) {
433 if (plane.pln_flags & PLN_LAUNCHED)
435 if (plane.pln_land != land.lnd_uid)
438 wu(0, plane.pln_own, "plane #%d dropped in %s\n",
440 xyas(plane.pln_x, plane.pln_y, plane.pln_own));
441 putplane(plane.pln_uid, &plane);
443 tg.land.lnd_ship = -1;
444 tg.land.lnd_land = -1;
447 logerror("Bad trade type %d in trade\n", trade.trd_type);
450 if (!ef_write(trade.trd_type, saveid, &tg)) {
451 logerror("Couldn't write unit to disk; seek help.\n");
454 nreport(trade.trd_owner, N_MAKE_SALE, trade.trd_maxbidder, 1);
455 wu(0, trade.trd_owner, "%s bought a %s #%d from you for $%.2f\n",
456 cname(trade.trd_maxbidder), trade_nameof(&trade, &tg),
457 saveid, price * tradetax);
458 wu(0, trade.trd_maxbidder,
459 "The bidding is over & you bought %s #%d from %s for $%.2f\n",
460 trade_nameof(&trade, &tg), saveid, cname(trade.trd_owner),
463 /* logerror("Done checking the trades.\n");*/
468 ontradingblock(int type, void *ptr)
471 union empobj_storage tg;
474 for (n = 0; gettrade(n, &trade); n++) {
475 if (trade.trd_unitid < 0)
477 if (!trade_getitem(&trade, &tg))
479 if (trade.trd_type != type)
481 if (tg.gen.uid == ((struct empobj *)ptr)->uid)
488 trdswitchown(int type, void *ptr, int newown)
491 union empobj_storage tg;
494 for (n = 0; gettrade(n, &trade); n++) {
495 if (trade.trd_unitid < 0)
497 if (!trade_getitem(&trade, &tg))
499 if (trade.trd_type != type)
501 if (tg.gen.uid != ((struct empobj *)ptr)->uid)
503 if (trade.trd_owner == trade.trd_maxbidder)
504 trade.trd_maxbidder = newown;
505 trade.trd_owner = newown;
507 trade.trd_unitid = -1;