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)) {
101 TRADE_DELAY / 3600.0 - (now - trade.trd_markettime) / 3600.0;
104 pr("$%7ld %2d %5.2f hrs ",
105 trade.trd_price, trade.trd_maxbidder, tleft);
106 (void)trade_desc(&trade, &tg); /* XXX */
108 if (trade.trd_owner == player->cnum && !player->god)
109 pr(" (your own lot)\n");
113 pr("Nothing to buy at the moment...\n");
116 if ((p = getstring("Which lot to buy: ", buf)) == 0 || *p == 0)
118 if (isdigit(*p) == 0)
121 if (lotno < 0 || lotno >= ni.cur) {
122 pr("Bad lot number\n");
125 if (!gettrade(lotno, &trade)) {
126 pr("No such lot number\n");
129 if (trade.trd_unitid < 0) {
130 pr("Invalid lot number.\n");
133 if (!trade_getitem(&trade, &tg)) {
134 pr("Can't find trade #%d!\n", trade.trd_unitid);
135 trade.trd_unitid = -1;
136 if (!puttrade(lotno, &trade)) {
137 logerror("trad: can't write trade");
138 pr("Couldn't save after getitem failed; get help!\n");
143 switch (trade.trd_type) {
150 pr("Bad unit type on lot number %d\n", lotno);
153 if (trade.trd_owner == player->cnum) {
154 pr("You can't buy from yourself!\n");
157 price = trade.trd_price;
158 natp = getnatp(player->cnum);
159 if (natp->nat_money < price) {
160 pr("You don't have %.2f to spend!\n", price);
164 for (q = 0; gettrade(q, &tmpt); q++) {
165 if (tmpt.trd_maxbidder == player->cnum &&
166 tmpt.trd_unitid >= 0 && tmpt.trd_owner != player->cnum) {
167 tally += tmpt.trd_price * tradetax;
170 for (q = 0; getcomm(q, &comt); q++) {
171 if (comt.com_maxbidder == player->cnum &&
172 comt.com_owner != 0 && comt.com_owner != player->cnum) {
173 tally += (comt.com_price * comt.com_amount) * buytax;
176 canspend = natp->nat_money - tally;
178 * Find the destination sector for the plane before the trade is
179 * actually made, except for satellites in orbit. Must be owned
180 * and must be a 60% airfield (except for VTOL planes).
182 if (((trade.trd_type == EF_PLANE) && !pln_is_in_orbit(&tg.plane))
183 || (trade.trd_type == EF_NUKE)) {
185 p = getstring("Destination sector: ", buf);
186 if (!trade_check_ok(&trade, &tg))
191 if (!sarg_xy(p, &sx, &sy) || !getsect(sx, sy, §)) {
192 pr("Bad sector designation; try again!\n");
195 if (!player->owner) {
196 pr("You don't own that sector; try again!\n");
199 if (!(plchr[tg.plane.pln_type].pl_flags & P_V)) {
200 if (!player->god && (sect.sct_type != SCT_AIRPT)) {
201 pr("Destination sector is not an airfield!\n");
204 if (!player->god && (sect.sct_effic < 60)) {
205 pr("That airport still under construction!\n");
212 if (trade.trd_type == EF_LAND) {
214 p = getstring("Destination sector: ", buf);
215 if (!trade_check_ok(&trade, &tg))
220 if (!sarg_xy(p, &sx, &sy) || !getsect(sx, sy, §)) {
221 pr("Bad sector designation; try again!\n");
224 if (!player->owner) {
225 pr("You don't own that sector; try again!\n");
228 if (!player->god && (sect.sct_type != SCT_HEADQ)) {
229 pr("Destination sector is not a headquarters!\n");
232 if (!player->god && (sect.sct_effic < 60)) {
233 pr("That headquarters still under construction!\n");
240 if ((p = getstring("How much do you bid: ", buf)) == 0 || *p == 0)
242 if (!trade_check_ok(&trade, &tg))
247 if (bid > canspend) {
248 pr("You don't have %.2f to spend!\n", price);
251 if (bid > trade.trd_price) {
252 /* Add five minutes to the time if less than 5 minutes left. */
254 if (((TRADE_DELAY - (now - trade.trd_markettime)) < 300) &&
255 trade.trd_maxbidder != player->cnum)
256 trade.trd_markettime += 300;
257 trade.trd_price = bid;
258 trade.trd_maxbidder = player->cnum;
261 pr("Your bid on lot #%d is being considered.\n", lotno);
262 if (!puttrade(lotno, &trade))
263 pr("Problems with the trade file. Get help\n");
265 pr("Your bid wasn't high enough (you need to bid more than someone else.)\n");
281 union empobj_storage tg;
287 /* logerror("Checking the trades.\n");*/
288 for (n = 0; gettrade(n, &trade); n++) {
289 if (trade.trd_unitid < 0)
291 if (!trade_getitem(&trade, &tg))
293 if (tg.gen.own == 0) {
294 trade.trd_unitid = -1;
298 if (tg.gen.own != trade.trd_owner) {
299 logerror("Something weird, tg.gen.own != trade.trd_owner!\n");
300 trade.trd_unitid = -1;
305 if (trade.trd_owner == trade.trd_maxbidder)
310 TRADE_DELAY / 3600.0 - (now - trade.trd_markettime) / 3600.0;
316 saveid = trade.trd_unitid;
317 trade.trd_unitid = -1;
318 if (!puttrade(n, &trade)) {
319 logerror("Couldn't save trade after purchase; get help!\n");
323 price = trade.trd_price;
324 natp = getnatp(trade.trd_maxbidder);
325 if (natp->nat_money < price) {
326 nreport(trade.trd_maxbidder, N_WELCH_DEAL, trade.trd_owner, 1);
327 wu(0, trade.trd_owner,
328 "%s tried to buy a %s #%d from you for $%.2f\n",
329 cname(trade.trd_maxbidder), trade_nameof(&trade, &tg),
330 saveid, price * tradetax);
331 wu(0, trade.trd_owner, " but couldn't afford it.\n");
332 wu(0, trade.trd_owner,
333 " Your item was taken off the market.\n");
334 wu(0, trade.trd_maxbidder,
335 "You tried to buy %s #%d from %s for $%.2f\n",
336 trade_nameof(&trade, &tg), saveid, cname(trade.trd_owner),
338 wu(0, trade.trd_maxbidder, "but couldn't afford it.\n");
342 /* If we get this far, the sale will go through. */
344 natp->nat_money -= price;
347 natp = getnatp(trade.trd_owner);
348 natp->nat_money += roundavg(price * tradetax);
351 switch (trade.trd_type) {
353 tg.nuke.nuk_x = trade.trd_x;
354 tg.nuke.nuk_y = trade.trd_y;
355 makelost(EF_NUKE, tg.nuke.nuk_own, tg.nuke.nuk_uid,
356 tg.nuke.nuk_x, tg.nuke.nuk_y);
357 tg.nuke.nuk_own = trade.trd_maxbidder;
358 makenotlost(EF_NUKE, tg.nuke.nuk_own, tg.nuke.nuk_uid,
359 tg.nuke.nuk_x, tg.nuke.nuk_y);
362 if (!pln_is_in_orbit(&tg.plane)) {
363 tg.plane.pln_x = trade.trd_x;
364 tg.plane.pln_y = trade.trd_y;
366 makelost(EF_PLANE, tg.plane.pln_own, tg.plane.pln_uid,
367 tg.plane.pln_x, tg.plane.pln_y);
368 tg.plane.pln_own = trade.trd_maxbidder;
369 makenotlost(EF_PLANE, tg.plane.pln_own, tg.plane.pln_uid,
370 tg.plane.pln_x, tg.plane.pln_y);
371 tg.plane.pln_wing = 0;
372 /* no cheap version of fly */
373 if (opt_MOB_ACCESS) {
374 tg.plane.pln_mobil = -(etu_per_update / sect_mob_neg_factor);
375 game_tick_to_now(&tg.plane.pln_access);
377 tg.plane.pln_mobil = 0;
379 tg.plane.pln_mission = 0;
380 tg.plane.pln_harden = 0;
381 tg.plane.pln_ship = -1;
382 tg.plane.pln_land = -1;
385 takeover_ship(&tg.ship, trade.trd_maxbidder, 0);
388 tg.land.lnd_x = trade.trd_x;
389 tg.land.lnd_y = trade.trd_y;
390 if (tg.land.lnd_ship >= 0) {
392 getship(tg.land.lnd_ship, &ship);
394 putship(ship.shp_uid, &ship);
396 makelost(EF_LAND, tg.land.lnd_own, tg.land.lnd_uid,
397 tg.land.lnd_x, tg.land.lnd_y);
398 tg.land.lnd_own = trade.trd_maxbidder;
399 makenotlost(EF_LAND, tg.land.lnd_own, tg.land.lnd_uid,
400 tg.land.lnd_x, tg.land.lnd_y);
401 tg.land.lnd_army = 0;
402 /* no cheap version of fly */
403 if (opt_MOB_ACCESS) {
404 tg.land.lnd_mobil = -(etu_per_update / sect_mob_neg_factor);
405 game_tick_to_now(&tg.land.lnd_access);
407 tg.land.lnd_mobil = 0;
409 tg.land.lnd_harden = 0;
410 tg.land.lnd_mission = 0;
411 /* Drop any land units this unit was carrying */
412 snxtitem_xy(&ni, EF_LAND, tg.land.lnd_x, tg.land.lnd_y);
413 while (nxtitem(&ni, &land)) {
414 if (land.lnd_land != tg.land.lnd_uid)
417 wu(0, land.lnd_own, "unit #%d dropped in %s\n",
419 xyas(land.lnd_x, land.lnd_y, land.lnd_own));
420 putland(land.lnd_uid, &land);
422 /* Drop any planes this unit was carrying */
423 snxtitem_xy(&ni, EF_PLANE, tg.land.lnd_x, tg.land.lnd_y);
424 while (nxtitem(&ni, &plane)) {
425 if (plane.pln_flags & PLN_LAUNCHED)
427 if (plane.pln_land != land.lnd_uid)
430 wu(0, plane.pln_own, "plane #%d dropped in %s\n",
432 xyas(plane.pln_x, plane.pln_y, plane.pln_own));
433 putplane(plane.pln_uid, &plane);
435 tg.land.lnd_ship = -1;
436 tg.land.lnd_land = -1;
439 logerror("Bad trade type %d in trade\n", trade.trd_type);
442 if (!ef_write(trade.trd_type, saveid, &tg)) {
443 logerror("Couldn't write unit to disk; seek help.\n");
446 nreport(trade.trd_owner, N_MAKE_SALE, trade.trd_maxbidder, 1);
447 wu(0, trade.trd_owner, "%s bought a %s #%d from you for $%.2f\n",
448 cname(trade.trd_maxbidder), trade_nameof(&trade, &tg),
449 saveid, price * tradetax);
450 wu(0, trade.trd_maxbidder,
451 "The bidding is over & you bought %s #%d from %s for $%.2f\n",
452 trade_nameof(&trade, &tg), saveid, cname(trade.trd_owner),
455 /* logerror("Done checking the trades.\n");*/
460 ontradingblock(int type, void *ptr)
463 union empobj_storage tg;
466 for (n = 0; gettrade(n, &trade); n++) {
467 if (trade.trd_unitid < 0)
469 if (!trade_getitem(&trade, &tg))
471 if (trade.trd_type != type)
473 if (tg.gen.uid == ((struct empobj *)ptr)->uid)
480 trdswitchown(int type, void *ptr, int newown)
483 union empobj_storage tg;
486 for (n = 0; gettrade(n, &trade); n++) {
487 if (trade.trd_unitid < 0)
489 if (!trade_getitem(&trade, &tg))
491 if (trade.trd_type != type)
493 if (tg.gen.uid != ((struct empobj *)ptr)->uid)
495 if (trade.trd_owner == trade.trd_maxbidder)
496 trade.trd_maxbidder = newown;
497 trade.trd_owner = newown;
499 trade.trd_unitid = -1;