]> git.pond.sub.org Git - empserver/blobdiff - src/lib/commands/buy.c
buy: Fix bogus error when lot gets reused at the last prompt
[empserver] / src / lib / commands / buy.c
index 8e00d961ac0b9fc8e3275c8c6a5a5b6f862231d7..72afa28c20e10c86349a77d15dfadd1a31864f9d 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2009, Dave Pare, Jeff Bailey, Thomas Ruschak,
- *                           Ken Stevens, Steve McClure
+ *  Copyright (C) 1986-2021, 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/>.
  *
  *  ---
  *
  *     Dave Pare, 1986
  *     Pat Loney, 1992
  *     Steve McClure, 1996-2000
+ *     Markus Armbruster, 2004-2021
  */
 
 #include <config.h>
 
+#include "chance.h"
 #include "commands.h"
 #include "commodity.h"
 #include "item.h"
-#include "land.h"
-#include "loan.h"
 #include "news.h"
 #include "optlist.h"
-#include "plane.h"
-#include "ship.h"
 #include "trade.h"
 
 /*
@@ -56,7 +53,6 @@ buy(void)
     struct sctstr sect;
     struct natstr *natp;
     struct comstr comm;
-    struct comstr ncomm;
     struct comstr comt;
     struct trdstr tmpt;
     struct ichrstr *ip;
@@ -105,6 +101,8 @@ buy(void)
     bid = atof(p);
     if (bid <= 0)
        return RET_FAIL;
+    if (!check_comm_ok(&comm))
+       return RET_FAIL;
     if (natp->nat_money < bid * comm.com_amount * buytax) {
        pr("This purchase would cost %.2f, %.2f more than you have.\n",
           bid * comm.com_amount * buytax,
@@ -116,8 +114,10 @@ buy(void)
     this part up.*/
     tally = 0.0;
     for (n = 0; gettrade(n, &tmpt); n++) {
+       if (!tmpt.trd_owner)
+           continue;
        if (tmpt.trd_maxbidder == player->cnum &&
-           tmpt.trd_unitid >= 0 && tmpt.trd_owner != player->cnum) {
+           tmpt.trd_owner != player->cnum) {
            tally += tmpt.trd_price * tradetax;
        }
     }
@@ -128,7 +128,6 @@ buy(void)
        }
     }
     canspend = natp->nat_money - tally;
-    getcomm(o, &comm);
     if (bid * comm.com_amount * buytax > canspend) {
        pr("You have overextended yourself in the market\n");
        pr("You can not bid on the current items at that price.\n");
@@ -142,6 +141,8 @@ buy(void)
        pr("Could not access sector");
        return RET_FAIL;
     }
+    if (!check_comm_ok(&comm))
+       return RET_FAIL;
     if ((sect.sct_type != SCT_WAREH && sect.sct_type != SCT_HARBR) ||
        sect.sct_own != player->cnum) {
        pr("The destination sector is not one of your warehouses.\n");
@@ -162,23 +163,12 @@ buy(void)
        pr("You don't have that much to spend!\n");
        return RET_FAIL;
     }
-    getcomm(o, &ncomm);
-    if (!ncomm.com_owner) {
-       pr("That lot has been taken off the market.\n");
-       return RET_FAIL;
-    }
     if (bid > 0.04 + comm.com_price) {
        comm.com_price = bid;
-       /* Add five minutes to the time if less than 5 minutes */
        time(&now);
-       if (((MARK_DELAY - (now - comm.com_markettime)) < 300) &&
-           comm.com_maxbidder != player->cnum) {
-           comm.com_markettime += 300;
-           /* Special case - what if so much time has gone by?  Well,
-              Just reset the markettime  so that only 5 minutes are left */
-           if ((MARK_DELAY - (now - comm.com_markettime)) < 0)
-               comm.com_markettime = (now - (MARK_DELAY - 300));
-       }
+       if (comm.com_markettime + MARK_DELAY - now < minutes(5) &&
+           comm.com_maxbidder != player->cnum)
+           comm.com_markettime = now + minutes(5) - MARK_DELAY;
        comm.com_maxbidder = player->cnum;
        comm.com_x = x;
        comm.com_y = y;
@@ -204,7 +194,6 @@ check_market(void)
     int m;
     int n;
     time_t now;
-    double tleft;
     double gain;
     double price;
 
@@ -212,10 +201,7 @@ check_market(void)
        if (comm.com_maxbidder == comm.com_owner || comm.com_owner == 0)
            continue;
        (void)time(&now);
-       tleft = MARK_DELAY / 3600.0 - (now - comm.com_markettime) / 3600.0;
-       if (tleft < 0)
-           tleft = 0;
-       if (tleft > 0.0)
+       if (comm.com_markettime + MARK_DELAY > now)
            continue;
        if (CANT_HAPPEN(comm.com_type <= I_NONE || comm.com_type > I_MAX))
            continue;