]> git.pond.sub.org Git - empserver/blobdiff - src/lib/commands/trad.c
Update copyright notice
[empserver] / src / lib / commands / trad.c
index d5e65aa52a1d6e6ac45749544c6f29884cec2bca..cf1b1e28d67abb078de3c679a747b13f7e0df058 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
- *                           Ken Stevens, Steve McClure
+ *  Copyright (C) 1986-2012, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *                Ken Stevens, Steve McClure, Markus Armbruster
  *
- *  This program is free software; you can redistribute it and/or modify
+ *  Empire is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
+ *  the Free Software Foundation, either version 3 of the License, or
  *  (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
@@ -14,8 +14,7 @@
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  *  ---
  *
@@ -26,7 +25,7 @@
  *  ---
  *
  *  trad.c: Buy units/ships/planes/nukes from other nations.
- * 
+ *
  *  Known contributors to this file:
  *     Dave Pare, 1986
  *     Pat Loney, 1992
@@ -72,7 +71,7 @@ trad(void)
     int bid;
     double tleft;
     double tally;
-    int q;
+    int i;
     char buf[1024];
 
     if (!opt_MARKET) {
@@ -114,7 +113,8 @@ trad(void)
        pr("Nothing to buy at the moment...\n");
        return RET_OK;
     }
-    if ((p = getstring("Which lot to buy: ", buf)) == 0 || *p == 0)
+    p = getstring("Which lot to buy: ", buf);
+    if (!p || !*p)
        return RET_OK;
     if (isdigit(*p) == 0)
        return RET_OK;
@@ -133,6 +133,7 @@ trad(void)
     }
     if (!trade_getitem(&trade, &tg)) {
        pr("Can't find trade #%d!\n", trade.trd_unitid);
+       trade.trd_owner = 0;
        trade.trd_unitid = -1;
        if (!puttrade(lotno, &trade)) {
            logerror("trad: can't write trade");
@@ -162,31 +163,28 @@ trad(void)
        return RET_OK;
     }
     tally = 0.0;
-    for (q = 0; gettrade(q, &tmpt); q++) {
+    for (i = 0; gettrade(i, &tmpt); i++) {
        if (tmpt.trd_maxbidder == player->cnum &&
            tmpt.trd_unitid >= 0 && tmpt.trd_owner != player->cnum) {
            tally += tmpt.trd_price * tradetax;
        }
     }
-    for (q = 0; getcomm(q, &comt); q++) {
+    for (i = 0; getcomm(i, &comt); i++) {
        if (comt.com_maxbidder == player->cnum &&
            comt.com_owner != 0 && comt.com_owner != player->cnum) {
            tally += (comt.com_price * comt.com_amount) * buytax;
        }
     }
     canspend = natp->nat_money - tally;
-    /*
-     * Find the destination sector for the plane before the trade is
-     * actually made, except for satellites in orbit.  Must be owned
-     * and must be a 60% airfield (except for VTOL planes).
-     */
+
+    /* Find the destination sector for the trade */
     if (((trade.trd_type == EF_PLANE) && !pln_is_in_orbit(&tg.plane))
        || (trade.trd_type == EF_NUKE)) {
        while (1) {
            p = getstring("Destination sector: ", buf);
            if (!trade_check_ok(&trade, &tg))
                return RET_FAIL;
-           if (p == 0) {
+           if (!p) {
                return RET_FAIL;
            }
            if (!sarg_xy(p, &sx, &sy) || !getsect(sx, sy, &sect)) {
@@ -209,13 +207,12 @@ trad(void)
            }
            break;
        }
-    }
-    if (trade.trd_type == EF_LAND) {
+    } else if (trade.trd_type == EF_LAND) {
        while (1) {
            p = getstring("Destination sector: ", buf);
            if (!trade_check_ok(&trade, &tg))
                return RET_FAIL;
-           if (p == 0) {
+           if (!p) {
                return RET_FAIL;
            }
            if (!sarg_xy(p, &sx, &sy) || !getsect(sx, sy, &sect)) {
@@ -236,9 +233,14 @@ trad(void)
            }
            break;
        }
+    } else {
+       /* This trade doesn't teleport; make destination invalid */
+       sx = 1;
+       sy = 0;
     }
 
-    if ((p = getstring("How much do you bid: ", buf)) == 0 || *p == 0)
+    p = getstring("How much do you bid: ", buf);
+    if (!p || !*p)
        return RET_OK;
     if (!trade_check_ok(&trade, &tg))
        return RET_FAIL;
@@ -281,20 +283,22 @@ check_trade(void)
     double tleft;
     float price;
     int saveid;
+    natid seller;
 
-/*    logerror("Checking the trades.\n");*/
     for (n = 0; gettrade(n, &trade); n++) {
        if (trade.trd_unitid < 0)
            continue;
        if (!trade_getitem(&trade, &tg))
            continue;
        if (tg.gen.own == 0) {
+           trade.trd_owner = 0;
            trade.trd_unitid = -1;
            puttrade(n, &trade);
            continue;
        }
        if (tg.gen.own != trade.trd_owner) {
            logerror("Something weird, tg.gen.own != trade.trd_owner!\n");
+           trade.trd_owner = 0;
            trade.trd_unitid = -1;
            puttrade(n, &trade);
            continue;
@@ -312,6 +316,8 @@ check_trade(void)
            continue;
 
        saveid = trade.trd_unitid;
+       seller = trade.trd_owner;
+       trade.trd_owner = 0;
        trade.trd_unitid = -1;
        if (!puttrade(n, &trade)) {
            logerror("Couldn't save trade after purchase; get help!\n");
@@ -321,17 +327,17 @@ check_trade(void)
        price = trade.trd_price;
        natp = getnatp(trade.trd_maxbidder);
        if (natp->nat_money < price) {
-           nreport(trade.trd_maxbidder, N_WELCH_DEAL, trade.trd_owner, 1);
-           wu(0, trade.trd_owner,
+           nreport(trade.trd_maxbidder, N_WELCH_DEAL, seller, 1);
+           wu(0, seller,
               "%s tried to buy a %s #%d from you for $%.2f\n",
               cname(trade.trd_maxbidder), trade_nameof(&trade, &tg),
               saveid, price * tradetax);
-           wu(0, trade.trd_owner, "   but couldn't afford it.\n");
-           wu(0, trade.trd_owner,
+           wu(0, seller, "   but couldn't afford it.\n");
+           wu(0, seller,
               "   Your item was taken off the market.\n");
            wu(0, trade.trd_maxbidder,
               "You tried to buy %s #%d from %s for $%.2f\n",
-              trade_nameof(&trade, &tg), saveid, cname(trade.trd_owner),
+              trade_nameof(&trade, &tg), saveid, cname(seller),
               price);
            wu(0, trade.trd_maxbidder, "but couldn't afford it.\n");
            continue;
@@ -342,7 +348,7 @@ check_trade(void)
        natp->nat_money -= price;
        putnat(natp);
 
-       natp = getnatp(trade.trd_owner);
+       natp = getnatp(seller);
        natp->nat_money += roundavg(price * tradetax);
        putnat(natp);
 
@@ -368,8 +374,6 @@ check_trade(void)
            tg.plane.pln_land = -1;
            break;
        case EF_SHIP:
-           tg.ship.shp_rflags = 0;
-           memset(tg.ship.shp_rpath, 0, sizeof(tg.ship.shp_rpath));
            break;
        case EF_LAND:
            tg.land.lnd_x = trade.trd_x;
@@ -392,16 +396,15 @@ check_trade(void)
        unit_give_away(&tg.gen, trade.trd_maxbidder, 0);
        put_empobj(trade.trd_type, saveid, &tg.gen);
 
-       nreport(trade.trd_owner, N_MAKE_SALE, trade.trd_maxbidder, 1);
-       wu(0, trade.trd_owner, "%s bought a %s #%d from you for $%.2f\n",
+       nreport(seller, N_MAKE_SALE, trade.trd_maxbidder, 1);
+       wu(0, seller, "%s bought %s #%d from you for $%.2f\n",
           cname(trade.trd_maxbidder), trade_nameof(&trade, &tg),
           saveid, price * tradetax);
        wu(0, trade.trd_maxbidder,
           "The bidding is over & you bought %s #%d from %s for $%.2f\n",
-          trade_nameof(&trade, &tg), saveid, cname(trade.trd_owner),
+          trade_nameof(&trade, &tg), saveid, cname(seller),
           price);
     }
-/*    logerror("Done checking the trades.\n");*/
     return RET_OK;
 }