buy set trade: Fix taking lots off the market

To find out whether a lot is in use, some places check for zero
trd_owner, others for negative trd_unitid.  The former is reliable,
the latter is not: set() fails to change trd_unitid when it takes a
lot off the market.  The next trade-related command then runs
check_trade(), which logs "Something weird" and cleans up the mess.
Broken in commit e16e38dfab (v4.2.18).

Replace the unreliable checks by reliable ones.

Clean up set() not to implictly rely on unused lots having negative
trd_unitid.

The trd_unitid = -1 are unnecessary now, so drop them.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
This commit is contained in:
Markus Armbruster 2018-05-10 10:28:49 +02:00
parent 5f0ddfd950
commit 6bcd72e241
3 changed files with 13 additions and 18 deletions

View file

@ -30,7 +30,7 @@
* Dave Pare, 1986
* Pat Loney, 1992
* Steve McClure, 1996-2000
* Markus Armbruster, 2004-2013
* Markus Armbruster, 2004-2018
*/
#include <config.h>
@ -112,8 +112,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;
}
}

View file

@ -105,6 +105,7 @@ set(void)
if (!trade.trd_owner) {
if (freeslot < 0)
freeslot = ni_trade.cur;
continue;
}
if (trade.trd_unitid == ni.cur && trade.trd_type == type) {
foundslot = ni_trade.cur;

View file

@ -30,7 +30,7 @@
* Dave Pare, 1986
* Pat Loney, 1992
* Steve McClure, 1996-2000
* Markus Armbruster, 2004-2013
* Markus Armbruster, 2004-2018
*/
#include <config.h>
@ -123,18 +123,13 @@ trad(void)
pr("Bad lot number\n");
return RET_OK;
}
if (!gettrade(lotno, &trade)) {
if (!gettrade(lotno, &trade) || !trade.trd_owner) {
pr("No such lot number\n");
return RET_OK;
}
if (trade.trd_unitid < 0) {
pr("Invalid lot number.\n");
return RET_OK;
}
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");
pr("Couldn't save after getitem failed; get help!\n");
@ -164,8 +159,10 @@ trad(void)
}
tally = 0.0;
for (i = 0; gettrade(i, &tmpt); i++) {
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;
}
}
@ -282,20 +279,18 @@ check_trade(void)
natid seller;
for (n = 0; gettrade(n, &trade); n++) {
if (trade.trd_unitid < 0)
if (!trade.trd_owner)
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;
}
@ -310,7 +305,6 @@ check_trade(void)
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");
continue;
@ -408,7 +402,7 @@ ontradingblock(int type, void *ptr)
int n;
for (n = 0; gettrade(n, &trade); n++) {
if (trade.trd_unitid < 0)
if (!trade.trd_owner)
continue;
if (!trade_getitem(&trade, &tg))
continue;
@ -428,7 +422,7 @@ trdswitchown(int type, struct empobj *obj, int newown)
int n;
for (n = 0; gettrade(n, &trade); n++) {
if (trade.trd_unitid < 0)
if (!trade.trd_owner)
continue;
if (!trade_getitem(&trade, &tg))
continue;
@ -439,8 +433,6 @@ trdswitchown(int type, struct empobj *obj, int newown)
if (trade.trd_owner == trade.trd_maxbidder)
trade.trd_maxbidder = newown;
trade.trd_owner = newown;
if (newown == 0)
trade.trd_unitid = -1;
puttrade(n, &trade);
return;
}