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;
78 pr("The market is disabled.\n");
81 /* First, we execute all trades, so that we can only buy what is available. */
85 pr("\n Empire Trade Report\n ");
88 pr(" lot high bid by time left owner description\n");
89 pr(" --- -------- -- --------- ----- -------------------------\n");
91 snxtitem_all(&ni, EF_TRADE);
92 while (nxtitem(&ni, &trade)) {
93 if (trade.trd_owner == 0)
95 if (!trade_getitem(&trade, &tg)) {
98 /* fix up database if things get weird */
99 /*if (trade.trd_owner != tg.gen.own) {
100 trade.trd_unitid = -1;
101 (void) puttrade(ni.cur, &trade);
107 TRADE_DELAY / 3600.0 - (now - trade.trd_markettime) / 3600.0;
110 pr("$%7ld %2d %5.2f hrs ",
111 trade.trd_price, trade.trd_maxbidder, tleft);
112 (void)trade_desc(&trade, &tg); /* XXX */
114 if (trade.trd_owner == player->cnum && !player->god)
115 pr(" (your own lot)\n");
119 pr("Nothing to buy at the moment...\n");
122 if ((p = getstring("Which lot to buy: ", buf)) == 0 || *p == 0)
124 if (isdigit(*p) == 0)
127 if (lotno < 0 || lotno >= ni.cur) {
128 pr("Bad lot number\n");
131 if (!gettrade(lotno, &trade)) {
132 pr("No such lot number\n");
135 if (trade.trd_unitid < 0) {
136 pr("Invalid lot number.\n");
139 if (!trade_getitem(&trade, &tg)) {
140 pr("Can't find trade #%d!\n", trade.trd_unitid);
141 trade.trd_unitid = -1;
142 if (!puttrade(lotno, &trade)) {
143 pr("Couldn't save after getitem failed; get help!\n");
148 switch (trade.trd_type) {
155 pr("Bad unit type on lot number %d\n", lotno);
158 if (trade.trd_owner == player->cnum) {
159 pr("You can't buy from yourself!\n");
162 price = trade.trd_price;
163 natp = getnatp(player->cnum);
164 if (natp->nat_money < price) {
165 pr("You don't have %.2f to spend!\n", price);
169 for (q = 0; gettrade(q, &tmpt); q++) {
170 if (tmpt.trd_maxbidder == player->cnum &&
171 tmpt.trd_unitid >= 0 && tmpt.trd_owner != player->cnum) {
172 tally += tmpt.trd_price * tradetax;
175 for (q = 0; getcomm(q, &comt); q++) {
176 if (comt.com_maxbidder == player->cnum &&
177 comt.com_owner != 0 && comt.com_owner != player->cnum) {
178 tally += (comt.com_price * comt.com_amount) * buytax;
181 canspend = natp->nat_money - tally;
183 * Find the destination sector for the plane before the trade is
184 * actually made, except for satellites in orbit. Must be owned
185 * and must be a 60% airfield (except for VTOL planes).
187 if (((trade.trd_type == EF_PLANE) && !pln_is_in_orbit(&tg.plane))
188 || (trade.trd_type == EF_NUKE)) {
190 p = getstring("Destination sector: ", buf);
191 if (!trade_check_ok(&trade, &tg))
196 if (!sarg_xy(p, &sx, &sy) || !getsect(sx, sy, §)) {
197 pr("Bad sector designation; try again!\n");
200 if (!player->owner) {
201 pr("You don't own that sector; try again!\n");
204 if (!(plchr[tg.plane.pln_type].pl_flags & P_V)) {
205 if (!player->god && (sect.sct_type != SCT_AIRPT)) {
206 pr("Destination sector is not an airfield!\n");
209 if (!player->god && (sect.sct_effic < 60)) {
210 pr("That airport still under construction!\n");
217 if (trade.trd_type == EF_LAND) {
219 p = getstring("Destination sector: ", buf);
220 if (!trade_check_ok(&trade, &tg))
225 if (!sarg_xy(p, &sx, &sy) || !getsect(sx, sy, §)) {
226 pr("Bad sector designation; try again!\n");
229 if (!player->owner) {
230 pr("You don't own that sector; try again!\n");
233 if (!player->god && (sect.sct_type != SCT_HEADQ)) {
234 pr("Destination sector is not a headquarters!\n");
237 if (!player->god && (sect.sct_effic < 60)) {
238 pr("That headquarters still under construction!\n");
245 if ((p = getstring("How much do you bid: ", buf)) == 0 || *p == 0)
247 if (!trade_check_ok(&trade, &tg))
252 if (bid > canspend) {
253 pr("You don't have %.2f to spend!\n", price);
256 if (bid > trade.trd_price) {
257 /* Add five minutes to the time if less than 5 minutes left. */
259 if (((TRADE_DELAY - (now - trade.trd_markettime)) < 300) &&
260 trade.trd_maxbidder != player->cnum)
261 trade.trd_markettime += 300;
262 trade.trd_price = bid;
263 trade.trd_maxbidder = player->cnum;
266 pr("Your bid on lot #%d is being considered.\n", lotno);
267 if (!puttrade(lotno, &trade))
268 pr("Problems with the trade file. Get help\n");
270 pr("Your bid wasn't high enough (you need to bid more than someone else.)\n");
286 union empobj_storage tg;
292 /* logerror("Checking the trades.\n");*/
293 for (n = 0; gettrade(n, &trade); n++) {
294 if (trade.trd_unitid < 0)
296 if (!trade_getitem(&trade, &tg))
298 if (tg.gen.own == 0) {
299 trade.trd_unitid = -1;
303 if (tg.gen.own != trade.trd_owner) {
304 logerror("Something weird, tg.gen.own != trade.trd_owner!\n");
305 trade.trd_unitid = -1;
310 if (trade.trd_owner == trade.trd_maxbidder)
315 TRADE_DELAY / 3600.0 - (now - trade.trd_markettime) / 3600.0;
321 saveid = trade.trd_unitid;
322 trade.trd_unitid = -1;
323 if (!puttrade(n, &trade)) {
324 logerror("Couldn't save trade after purchase; get help!\n");
328 price = trade.trd_price;
329 natp = getnatp(trade.trd_maxbidder);
330 if (natp->nat_money < price) {
331 nreport(trade.trd_maxbidder, N_WELCH_DEAL, trade.trd_owner, 1);
332 wu(0, trade.trd_owner,
333 "%s tried to buy a %s #%d from you for $%.2f\n",
334 cname(trade.trd_maxbidder), trade_nameof(&trade, &tg),
335 saveid, price * tradetax);
336 wu(0, trade.trd_owner, " but couldn't afford it.\n");
337 wu(0, trade.trd_owner,
338 " Your item was taken off the market.\n");
339 wu(0, trade.trd_maxbidder,
340 "You tried to buy %s #%d from %s for $%.2f\n",
341 trade_nameof(&trade, &tg), saveid, cname(trade.trd_owner),
343 wu(0, trade.trd_maxbidder, "but couldn't afford it.\n");
347 /* If we get this far, the sale will go through. */
349 natp->nat_money -= price;
352 natp = getnatp(trade.trd_owner);
353 natp->nat_money += roundavg(price * tradetax);
356 switch (trade.trd_type) {
358 tg.nuke.nuk_x = trade.trd_x;
359 tg.nuke.nuk_y = trade.trd_y;
360 makelost(EF_NUKE, tg.nuke.nuk_own, tg.nuke.nuk_uid,
361 tg.nuke.nuk_x, tg.nuke.nuk_y);
362 tg.nuke.nuk_own = trade.trd_maxbidder;
363 makenotlost(EF_NUKE, tg.nuke.nuk_own, tg.nuke.nuk_uid,
364 tg.nuke.nuk_x, tg.nuke.nuk_y);
367 if (!pln_is_in_orbit(&tg.plane)) {
368 tg.plane.pln_x = trade.trd_x;
369 tg.plane.pln_y = trade.trd_y;
371 makelost(EF_PLANE, tg.plane.pln_own, tg.plane.pln_uid,
372 tg.plane.pln_x, tg.plane.pln_y);
373 tg.plane.pln_own = trade.trd_maxbidder;
374 makenotlost(EF_PLANE, tg.plane.pln_own, tg.plane.pln_uid,
375 tg.plane.pln_x, tg.plane.pln_y);
376 tg.plane.pln_wing = 0;
377 /* no cheap version of fly */
378 if (opt_MOB_ACCESS) {
379 tg.plane.pln_mobil = -(etu_per_update / sect_mob_neg_factor);
380 game_tick_to_now(&tg.plane.pln_access);
382 tg.plane.pln_mobil = 0;
384 tg.plane.pln_mission = 0;
385 tg.plane.pln_harden = 0;
386 tg.plane.pln_ship = -1;
387 tg.plane.pln_land = -1;
390 takeover_ship(&tg.ship, trade.trd_maxbidder, 0);
393 tg.land.lnd_x = trade.trd_x;
394 tg.land.lnd_y = trade.trd_y;
395 if (tg.land.lnd_ship >= 0) {
397 getship(tg.land.lnd_ship, &ship);
399 putship(ship.shp_uid, &ship);
401 makelost(EF_LAND, tg.land.lnd_own, tg.land.lnd_uid,
402 tg.land.lnd_x, tg.land.lnd_y);
403 tg.land.lnd_own = trade.trd_maxbidder;
404 makenotlost(EF_LAND, tg.land.lnd_own, tg.land.lnd_uid,
405 tg.land.lnd_x, tg.land.lnd_y);
406 tg.land.lnd_army = 0;
407 /* no cheap version of fly */
408 if (opt_MOB_ACCESS) {
409 tg.land.lnd_mobil = -(etu_per_update / sect_mob_neg_factor);
410 game_tick_to_now(&tg.land.lnd_access);
412 tg.land.lnd_mobil = 0;
414 tg.land.lnd_harden = 0;
415 tg.land.lnd_mission = 0;
416 /* Drop any land units this unit was carrying */
417 snxtitem_xy(&ni, EF_LAND, tg.land.lnd_x, tg.land.lnd_y);
418 while (nxtitem(&ni, &land)) {
419 if (land.lnd_land != tg.land.lnd_uid)
422 wu(0, land.lnd_own, "unit #%d dropped in %s\n",
424 xyas(land.lnd_x, land.lnd_y, land.lnd_own));
425 putland(land.lnd_uid, &land);
427 /* Drop any planes this unit was carrying */
428 snxtitem_xy(&ni, EF_PLANE, tg.land.lnd_x, tg.land.lnd_y);
429 while (nxtitem(&ni, &plane)) {
430 if (plane.pln_flags & PLN_LAUNCHED)
432 if (plane.pln_land != land.lnd_uid)
435 wu(0, plane.pln_own, "plane #%d dropped in %s\n",
437 xyas(plane.pln_x, plane.pln_y, plane.pln_own));
438 putplane(plane.pln_uid, &plane);
440 tg.land.lnd_ship = -1;
441 tg.land.lnd_land = -1;
444 logerror("Bad trade type %d in trade\n", trade.trd_type);
447 if (!ef_write(trade.trd_type, saveid, &tg)) {
448 logerror("Couldn't write unit to disk; seek help.\n");
451 nreport(trade.trd_owner, N_MAKE_SALE, trade.trd_maxbidder, 1);
452 wu(0, trade.trd_owner, "%s bought a %s #%d from you for $%.2f\n",
453 cname(trade.trd_maxbidder), trade_nameof(&trade, &tg),
454 saveid, price * tradetax);
455 wu(0, trade.trd_maxbidder,
456 "The bidding is over & you bought %s #%d from %s for $%.2f\n",
457 trade_nameof(&trade, &tg), saveid, cname(trade.trd_owner),
460 /* logerror("Done checking the trades.\n");*/
465 ontradingblock(int type, void *ptr)
468 union empobj_storage tg;
471 for (n = 0; gettrade(n, &trade); n++) {
472 if (trade.trd_unitid < 0)
474 if (!trade_getitem(&trade, &tg))
476 if (trade.trd_type != type)
478 if (tg.gen.uid == ((struct empobj *)ptr)->uid)
485 trdswitchown(int type, void *ptr, int newown)
488 union empobj_storage tg;
491 for (n = 0; gettrade(n, &trade); n++) {
492 if (trade.trd_unitid < 0)
494 if (!trade_getitem(&trade, &tg))
496 if (trade.trd_type != type)
498 if (tg.gen.uid != ((struct empobj *)ptr)->uid)
500 if (trade.trd_owner == trade.trd_maxbidder)
501 trade.trd_maxbidder = newown;
502 trade.trd_owner = newown;
504 trade.trd_unitid = -1;